1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h> /* strerror() */
#include <stdint.h>
#include <iostream>
#include <csignal>
#include <string>
#include <sstream>
#include <vector>
#include <memory>
#include "ExitCodes.hpp"
#include "Logger.hpp"
#include "ExitException.hpp"
#include "UserConfig.hpp"
#include "SerialPortFactory.hpp"
#include "SerialPort.hpp"
#include "Mcu.hpp"
using std::cout;
using std::endl;
using std::string;
using std::ostringstream;
using std::vector;
using std::unique_ptr;
void opSpeeds(CUserConfig & uc);
void opRead(CUserConfig & uc, CMcu & mcu);
void opErase(CUserConfig & uc, CMcu & mcu);
void opWrite(CUserConfig & uc, CMcu & mcu);
vector<uint8_t> readDataFile(const string fpath);
void writeDataFile(const string fpath, vector<uint8_t> data);
// Global variable to be accessed in signal handler
unique_ptr<CSerialPort> sp;
// Uvolnime zdroje pri ukonceni na signal
void
signalHandler(int dummy)
{
CLogger::info("Exiting on signal");
sp->close();
exit(EXIT_MAIN_SIGNAL);
}
int
main(int argc, char **argv)
{
// Set signal handler
signal(SIGINT, signalHandler);
CSerialPortFactory serialPortFactory;
sp = serialPortFactory.getSerialPort();
try {
// Parse user command line configuration
CUserConfig uc(argc, argv);
// Configure logging
if (uc.isVerboseModeSet())
CLogger::setLogInfo(true);
// Execute selected operation
if (uc.isHelpSet()) {
cout << uc.getHelpMessage(string(argv[0]));
} else if (uc.isVersionSet()) {
cout << PROGRAM_NAME << " " << PROGRAM_VERSION << endl;
} else if (uc.isSpeedsSet()) {
opSpeeds(uc);
} else if (uc.isIdentSet() || uc.isEraseSet() || uc.isReadSet() || uc.isWriteSet()) {
// Open serial port
sp->open(uc.getSerialPortName(), uc.getSerialSpeed());
// Get MCU model
unique_ptr<CMcu> mcu(new CMcu(*sp, uc.getMcuFrequency()));
// Execute requested operation
if (!uc.isIdentSet()) {
if (uc.isReadSet())
opRead(uc, *mcu);
else if (uc.isEraseSet())
opErase(uc, *mcu);
else if (uc.isWriteSet())
opWrite(uc, *mcu);
} else {
cout << mcu->ident() << endl;
}
} else {
CLogger::error("No operation requested. To get help type: " + string(argv[0]) + " help" , EXIT_MAIN_NOOP);
}
sp->close();
return 0;
} catch (CExitException & e) {
return e.getReturnValue();
}
}
void
opSpeeds(CUserConfig & uc)
{
string s = sp->getSpeeds(uc.getSerialPortName());
cout << "Baudrates supported by serial port " << uc.getSerialPortName() << ":" << endl;
cout << s;
}
void
opRead(CUserConfig & uc, CMcu & mcu)
{
CLogger::info("Reading memory");
int rl = uc.getReadLength();
vector<uint8_t> r;
if (rl == -1)
r = mcu.read(uc.isPrintProgressSet());
else
r = mcu.read(rl, uc.isPrintProgressSet());
// Write data file musi dostat spravnu hodnotu
writeDataFile(uc.getReadOutputFname(), r);
}
void
opErase(CUserConfig & uc, CMcu & mcu)
{
if (uc.getEraseBlockList().size() != 0) {
CLogger::info("Erasing memory by blocks");
mcu.erase(uc.getEraseBlockList());
} else {
CLogger::info("Erasing whole memory");
mcu.erase();
}
}
void
opWrite(CUserConfig & uc, CMcu & mcu)
{
vector<uint8_t> data;
data = readDataFile(uc.getWriteInputFname());
if (uc.getWriteEraseWholeMemory()) {
CLogger::info("Erasing whole memory");
mcu.erase();
} else {
CLogger::info("Erasing memory by blocks");
mcu.erase(0, data.size() - 1);
}
CLogger::info("Writing memory");
mcu.write(data, uc.isPrintProgressSet());
if (uc.getWriteCheckByRead()) {
CLogger::info("Checking result of write operation by reading");
vector<uint8_t> rdata = mcu.read(data.size());
for (size_t i = 0; i < data.size(); i++) {
if (rdata[i] != data[i])
CLogger::error("Write operation unsucessful", EXIT_MAIN_PROG_VERIFY);
}
CLogger::info("Write operation was successful");
}
}
vector<uint8_t>
readDataFile(const string fpath)
{
FILE *f;
size_t size;
#ifdef UNIX
char mode[3] = "r";
#else
char mode[3] = "rb";
#endif
if ((f = fopen(fpath.c_str(), mode)) == NULL)
CLogger::error("Cannot open file for reading: " + fpath, EXIT_MAIN_FILE_INOUT);
// Determine file size
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
vector<uint8_t> data;
data.resize(size);
size_t r = fread(data.data(), sizeof(uint8_t), size, f);
fclose(f);
if (r != size)
CLogger::error("Cannot read from file: " + fpath, EXIT_MAIN_FILE_INOUT);
return data;
}
void
writeDataFile(const string fpath, vector<uint8_t> data)
{
FILE *out;
#ifdef UNIX
char mode[3] = "w";
#else
char mode[3] = "wb";
#endif
if ((out = fopen(fpath.c_str(), mode)) == NULL) {
ostringstream os;
os << "Cannot open file for writing: " << fpath << ": " << strerror(errno);
CLogger::error(os.str(), EXIT_MAIN_FILE_INOUT);
}
size_t r = fwrite(data.data(), sizeof(uint8_t), data.size(), out);
fclose(out);
if (r != data.size())
CLogger::error("Cannot write to file: " + fpath, EXIT_MAIN_FILE_INOUT);
}
|