-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial2console-gateway.cpp
348 lines (301 loc) · 9.26 KB
/
serial2console-gateway.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Copyright 2019 Jan-Eric Schober
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// C Standard Libraries
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
// C++ Standard Libraries
#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>
#include <thread>
// SerialPortGateway Class
#include "SerialPortGateway.hpp"
/**
* serial2console-gateway application
* File: serial2console-gateway.cpp
* Purpose: Defines a CLI for interacting with a SerialPortGateway
*
* @author Jan-Eric Schober
* @version 1.0 20.01.2019
*/
// Constants
const std::string COMMAND_USAGE = "u";
const std::string COMMAND_LISTDEVICES = "ld";
const std::string COMMAND_LISTSERIALPORTS = "lp";
const std::string COMMAND_LISTMAPPINGS = "lm";
const std::string COMMAND_SEND = "s";
const std::string COMMAND_BROADCAST = "b";
const std::string COMMAND_ADDDEVICE = "a";
const std::string COMMAND_ADDNEWDEVICES = "an";
const std::string COMMAND_DELETEDEVICE = "d";
const std::string COMMAND_DELETEALLDEVICES = "da";
const std::string COMMAND_QUIT = "q";
// Variables
SerialPortGateway * gateway = nullptr;
bool gatewayStarted = true;
/**
* Prints the usage of the CLI.
*/
void printUsage()
{
std::cout
<< "Usage:" << std::endl
<< "\t" << COMMAND_LISTDEVICES << ": List all registered devices." << std::endl
<< "\t" << COMMAND_LISTSERIALPORTS << ": List all of the system's serial ports." << std::endl
<< "\t" << COMMAND_LISTMAPPINGS << ": Lists all mappings from deviceIds to serial ports." << std::endl
<< "\t" << COMMAND_SEND << ": Send a message to a single device." << std::endl
<< "\t" << COMMAND_BROADCAST << ": Broadcast a message to all registered devices." << std::endl
<< "\t" << COMMAND_ADDDEVICE << ": Adds a device." << std::endl
<< "\t" << COMMAND_ADDNEWDEVICES << ": Adds all new devices." << std::endl
<< "\t" << COMMAND_DELETEDEVICE << ": Deletes a device." << std::endl
<< "\t" << COMMAND_DELETEALLDEVICES << ": Deletes all devices." << std::endl
<< "\t" << COMMAND_QUIT << ": Quit the gateway." << std::endl;
}
/**
* Lists all registered/connected devices of the SerialPortGateway by their device ID.
*/
void listDevices()
{
std::cout << gateway->getDeviceIdList() << std::endl;
}
/**
* Lists all of the system's available serial ports which can be connected (and possibly are already connected).
*/
void listSerialPorts()
{
std::cout << gateway->getSerialPortList() << std::endl;
}
/**
* Lists all mappings between connected devices by their ID & their corresponding serial port.
*/
void listDeviceIdToSerialPortMappings()
{
std::cout << gateway->getDeviceIdToSerialPortMappingList() << std::endl;
}
/**
* Sends a message to a specific device.
*/
void sendMessage()
{
std::string deviceId, message;
std::cout << "-> Enter Device ID: " << std::endl;
std::getline( std::cin, deviceId );
std::cout << "-> Enter Message: " << std::endl;
std::getline( std::cin, message );
gateway->sendMessageToSerialDevice( deviceId, message );
}
/**
* Sends a message to all devices connected.
*/
void broadcastMessage()
{
std::string message;
std::cout << "-> Enter Message: " << std::endl;
std::getline( std::cin, message );
gateway->broadcastMessageToSerialDevices( message );
}
/**
* Tries to add a device on a specific serial port.
*/
void addDevice()
{
std::string serialPort;
std::cout << "-> Enter serial port: " << std::endl;
std::getline( std::cin, serialPort);
gateway->addSerialDevice( serialPort );
}
/**
* Tries to add all new devices which are not currently registered.
* This includes devices disconnected (either by user or because of and error) and reconnected to the system.
*/
void addNewDevices()
{
gateway->addNewSerialPorts();
}
/**
* Tries to delete a specific device by ID.
*/
void deleteDevice()
{
std::string deviceId;
std::cout << "-> Enter Device ID: " << std::endl;
std::getline( std::cin, deviceId );
gateway->deleteSerialDevice( deviceId );
}
/**
* Tries to delete all devices currently registered.
*/
void deleteAllDevices()
{
gateway->deleteAllSerialDevices();
}
/**
* Stops the SerialPortGateway gracefully.
*
* @param param Param passed by the invoked signal. Does nothing.
*/
void stopGateway( int param )
{
std::cout << std::endl << "Stopping Gateway..." << std::endl;
gatewayStarted = false;
gateway->stop();
// This loop waits for the all read loops being gracefully quit, before exiting the application
while( !gateway->isEveryReadLoopQuitted() )
{
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
}
std::cout << "Gateway stopped." << std::endl;
exit( 0 );
}
/**
* Command loop which takes input form the user and decides which function needs to be called.
*/
void fetchCommandsLoop()
{
printUsage();
while ( gatewayStarted )
{
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
std::string command;
std::cout << "Enter Command: " << std::endl;
std::getline( std::cin, command );
if ( command == COMMAND_USAGE )
{
printUsage();
}
else if ( command == COMMAND_LISTDEVICES )
{
listDevices();
}
else if ( command == COMMAND_LISTSERIALPORTS )
{
listSerialPorts();
}
else if ( command == COMMAND_LISTMAPPINGS )
{
listDeviceIdToSerialPortMappings();
}
else if ( command == COMMAND_SEND )
{
sendMessage();
}
else if ( command == COMMAND_BROADCAST )
{
broadcastMessage();
}
else if ( command == COMMAND_ADDDEVICE )
{
addDevice();
}
else if ( command == COMMAND_ADDNEWDEVICES )
{
addNewDevices();
}
else if ( command == COMMAND_DELETEDEVICE )
{
deleteDevice();
}
else if ( command == COMMAND_DELETEALLDEVICES )
{
deleteAllDevices();
}
else if ( command == COMMAND_QUIT )
{
stopGateway( 0 );
}
else
{
std::cout << std::string( "Unknown command \"" + command + "\". Enter \"" + COMMAND_USAGE + "\" for usage." ) << std::endl;
}
}
}
/**
* Main function.
*
* @return Returns the exitcode. 0 means success, 1 means there was an error while executing.
*/
int main( int argc, char* argv[] )
{
signal( SIGINT, stopGateway );
try
{
if ( argc < 5 )
{
throw std::invalid_argument( "Not all start parameters given. Format: \"" + std::string( argv[0] ) + " <configFile> <hardwareWhitelistFile> <serialPortBlacklistFile> <logPath>\"." );
}
std::string configFile = argv[1];
std::string hardwareWhitelistFile = argv[2];
std::string serialPortBlacklistFile = argv[3];
std::string logPath = argv[4];
gateway = new SerialPortGateway( configFile, hardwareWhitelistFile, serialPortBlacklistFile, logPath );
std::cout << "Starting Gateway.." << std::endl;
gateway->start();
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
std::thread( fetchCommandsLoop ).detach();
std::cout << "Gateway started." << std::endl;
std::cout << "CTRL+C to exit." << std::endl;
// Infinite Loop
while ( gatewayStarted )
{
std::this_thread::sleep_for( std::chrono::seconds( 10 ) );
}
}
catch ( ConfigMalformedException & e )
{
std::cerr << "ConfigMalformedException: " << e.what() << std::endl;
return 1;
}
catch ( ConfigMissingException & e )
{
std::cerr << "ConfigMissingException: " << e.what() << std::endl;
return 1;
}
catch ( ConfigKeyNotFoundException & e )
{
std::cerr << "ConfigKeyNotFoundException: " << e.what() << std::endl;
return 1;
}
catch ( NumericConfigValueMalformedException & e )
{
std::cerr << "NumericConfigValueMalformedException: " << e.what() << std::endl;
return 1;
}
catch ( serial::IOException & e )
{
std::cerr << "IOException: " << e.what() << std::endl;
return 1;
}
catch ( serial::SerialException & e )
{
std::cerr << "SerialException: " << e.what() << std::endl;
return 1;
}
catch ( serial::PortNotOpenedException & e )
{
std::cerr << "PortNotOpenedException: " << e.what() << std::endl;
return 1;
}
catch ( Exception & e )
{
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
catch ( std::exception & e )
{
std::cerr << "exception: " << e.what() << std::endl;
return 1;
}
return 0;
}