-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
244 lines (201 loc) · 6.3 KB
/
main.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//This is an open source non-commercial project. Dear PVS-Studio, please check it.
//#define ECHO_RESPONSE
#include <iostream>
#include <getopt.h>
#include "client.h"
#include "server.h"
#include "configfileparser.h"
using namespace std;
enum class PairingType
{
NONE = 0, // режим не установлен
CLIENT, // клиент
SERVER // сервер
};
static const char* defaultConfServer = "config/server.json";
static const char* defaultConfClient = "config/client.json";
void printErrorMessage()
{
cout << "Error. Usage: " << endl;
cout << "Server mode: PairingCheck --server [--config filename.json]" << endl;
cout << "Client mode: PairingCheck --client filename [--config filename.json]" << endl;
cout << "Default params: speed 9600, size 8bit, parity none, stop bite 1" << endl;
cout << "Default conf files " << endl << "for server: " << defaultConfServer << endl << "for client: " << defaultConfClient << endl;
cout << "PairingCheck --generate type[RS232|UDP]" << endl;
}
TypeInterface interfaceFactory(TypeParams params)
{
TypeInterface interface = nullptr;
switch (params->getType())
{
case TypeParam::RS232:
{ //set up RS-interface
interface = make_shared<RSInterface>(params, getSerial());
}
break;
case TypeParam::UDP:
{
interface = make_shared<UDPInterface>(params);
}
break;
case TypeParam::None:
cout << "Error get param, interfaceFactory!!!" << endl;
break;
}
return interface;
}
TypeParams getParamFromFile(const char* fileName)
{
ConfigFileParser parser(fileName);
if(!parser.Init())
{
cout << "Error parse file " << optarg << endl;
printErrorMessage();
exit(0);
}
return parser.getParams();
}
int main(int argc, char *argv[])
{
std::string fileName;
PairingType Mode = PairingType::NONE;
TypeParams params = nullptr;
if(argc < 2)
{
printErrorMessage();
}
else
{
static struct option long_options[] =
{
{"client", required_argument, nullptr, 'c'},
{"server", no_argument, nullptr, 's'},
{"config", required_argument, nullptr, 'f'},
{"devpath", required_argument, nullptr, 'd'},
{"type", required_argument, nullptr, 't'},
{"speed", required_argument, nullptr, 'b'},
{"generate", required_argument, nullptr, 'g'},
{"wakeupbit", no_argument, nullptr, 'w'},
};
const char *optString = "c:d:b:sw?";
while(true)
{
int c;
int option_index = 0;
c = getopt_long_only(argc, argv, optString, long_options, &option_index);
if(c == -1)
{
break;
}
switch(c)
{
case 's':
Mode = PairingType::SERVER;
break;
case 'c':
Mode = PairingType::CLIENT;
fileName = optarg;
break;
case 'f':
{
params = getParamFromFile(optarg);
// ConfigFileParser parser(optarg);
// if(!parser.Init())
// {
// cout << "Error parse file " << optarg << endl;
// printErrorMessage();
// exit(0);
// }
// params = parser.getParams();
}
break;
case 'g':
{
TypeParam type = IParams::getTypeParam(optarg);
if(type != TypeParam::None)
{
ConfigFileParser::generateJSON(type);
}
else
{
cout << "Error generate configfile. Wrong type " << optarg << endl;
printErrorMessage();
}
exit(0);
}
case '?':
{
printErrorMessage();
return 0;
}
}
}
if(params == nullptr)
{
if(Mode == PairingType::CLIENT)
{
params = getParamFromFile(defaultConfClient);
}
else
if(Mode == PairingType::SERVER)
{
params = getParamFromFile(defaultConfServer);
}
if(params == nullptr)
{
printErrorMessage();
cout << "Do not find config file" << endl;
return 0;
}
}
TypeInterface interface = interfaceFactory(params);
if(!interface)
{
cout << "Unkown interface type" << endl;
return 0;
}
if(!params)
{
cout << "Unkown config data file" << endl;
return 0;
}
switch(Mode)
{
case PairingType::CLIENT:
{
try
{
cout << "create client fileName=" << fileName << endl;
cout << "params->getDevPath() = " << params->getDevPath() << endl;
Client client(params, interface, fileName);
getchar();
}
catch (Worker::WorkerEx ex)
{
cout << "Exception client: " << ex.message << endl;
}
}
break;
case PairingType::SERVER:
{
try
{
cout << "create server" << endl;
cout << "params->getDevPath() = " << params->getDevPath() << endl;
Server server(params, interface);
getchar();
}
catch (Worker::WorkerEx ex)
{
cout << "Exception server: " << ex.message << endl;
}
}
break;
default:
{
printErrorMessage();
}
}
}
return 0;
}