-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjon_piper_main.cpp
536 lines (453 loc) · 14.7 KB
/
pjon_piper_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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#ifdef _WIN32
#include "stdafx.h"
#endif
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <inttypes.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <mutex>
#ifdef __arm__
#ifndef RPI
#define RPI true
// RPI serial interface
#include <wiringPi.h>
#include <wiringSerial.h>
#include <unistd.h>
#include <sys/select.h>
#include <stropts.h> //inputAvailable test 3
#include <sys/poll.h>
#include <sys/ioctl.h> // test6
#endif
#endif
//#define TS_RESPONSE_TIME_OUT 25000
//#define TS_COLLISION_DELAY 3
#define PJON_INCLUDE_TS true // Include only ThroughSerial
#include "PJON/src/PJON.h"
#include "PJON/src/PJONDefines.h"
#include "version.h"
std::mutex bus_mutex;
bool inputAvailable()
{
#ifdef RPI
int numAvailable = 0;
int iterCnt = 0;
while (ioctl(STDIN_FILENO, FIONREAD, &numAvailable) > 0) {
sleep(0.001);
if (iterCnt > 10) break;
}
if (numAvailable <= 0) return false;
return true;
#elif defined(_WIN32)
return false;
#endif
}
std::string read_std_in(){
std::string inStr;
#ifdef _WIN32
DWORD fdwMode, fdwOldMode;
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hStdIn, &fdwOldMode);
// disable mouse and window input
fdwMode = fdwOldMode ^ ENABLE_MOUSE_INPUT ^ ENABLE_WINDOW_INPUT;
SetConsoleMode(hStdIn, fdwMode);
if (WaitForSingleObject(hStdIn, 20) == WAIT_OBJECT_0){
std::getline(std::cin, inStr);
if (inStr.length() > 1) {
return inStr;
}
}
#elif defined(RPI)
if (inputAvailable()) {
std::getline(std::cin, inStr);
if (inStr.length() > 1) {
return inStr;
}
}
#endif
return "NO_INPUT";
}
uint64_t rcvd_cnt = 0; // std::time(nullptr);
static void receiver_function(
uint8_t *payload,
uint16_t length,
const PJON_Packet_Info &packet_info){
rcvd_cnt += 1;
std::cout << "#RCV snd_id=" << std::to_string(packet_info.sender_id)
<< " snd_net=";
for (uint32_t i = 0; i < sizeof(packet_info.sender_bus_id); i++) {
std::cout << std::to_string(packet_info.sender_bus_id[i]);
if (i < sizeof(packet_info.sender_bus_id) - 1)
std::cout << ".";
}
std::cout << " rcv_id=" << std::to_string(packet_info.receiver_id)
<< " rcv_net=";
for (uint32_t i = 0; i < sizeof(packet_info.receiver_bus_id); i++) {
std::cout << std::to_string(packet_info.receiver_bus_id[i]);
if (i < sizeof(packet_info.receiver_bus_id) - 1)
std::cout << ".";
}
std::cout << " id=" << std::to_string(packet_info.id)
<< " hdr=" << std::to_string(packet_info.header)
<< " pckt_cnt=" << std::to_string(rcvd_cnt)
<< " len=" << length
<< " data=";
for (uint32_t i = 0; i != length; i++){
std::cout << payload[i];
}
std::cout << std::endl;
}
static void error_handler_function(uint8_t code, uint16_t data, void *custom_pointer) {
std::cout << "#ERR code=" << std::to_string(code);
std::cout << " data=" << std::to_string(data);
std::cout << std::endl;
};
std::string get_token_at_pos_for_delim(
std::string const& str,
std::string const& delim,
int index){
std::string s = str;
std::string delimiter = delim;
size_t last = 0;
size_t next = 0;
int current_token = 0;
while ((next = s.find(delimiter, last)) != std::string::npos){
//std::cout << s.substr(last, next - last) << std::endl;
if (current_token == index)
return s.substr(last, next - last);
last = next + delimiter.length();
current_token += 1;
}
if (current_token == index)
return s.substr(last);
return "";
}
bool is_enough_args(int argc, char **argv) {
if (argc < 4)
return false;
return true;
}
bool is_first_arg_com_port(int argc, char **argv) {
//std::cout << "argv[1]: " << std::string(argv[1]) << "\n";
#ifdef _WIN32
if (std::string(argv[1]).find("COM") != std::string::npos)
return true;
return false;
#endif
return true;
}
bool is_second_arg_bitrate(int argc, char **argv) {
//std::cout << "CHECKING BITRATE: " << std::stoi(std::string(argv[2])) << "\n";
if (0 < std::stoi(std::string(argv[2])))
if(std::stoi(std::string(argv[2])) <= 153600)
return true;
return false;
}
bool is_third_arg_bus_id(int argc, char **argv) {
if (0 <= std::stoi(std::string(argv[3])))
if (std::stoi(std::string(argv[3])) <= 255)
return true;
return false;
}
bool is_fourth_arg_console_switch(int argc, char **argv) {
if (argc < 5)
return false;
std::cout << argc;
if (std::string(argv[4]).find("console") != std::string::npos)
return true;
return false;
}
void print_usage_help() {
std::cout
<< "PJON-piper - a lightweight command line client to PJON bus\n"
<< "VERSION: " << PJON_PIPER_VERSION << "\n"
<< "\n"
<< "usage: pjon_piper.exe COM PORT BITRATE NODE ID <console>\n"
<< " \\ \\ \\ | \n"
<< " \\ \\ 0-255 | \n"
<< " COMXX 1200 - 153600 | \n"
<< " freezes on typing send/set commands \n"
<< std::endl
<< "example: pjon_piper.exe COM3 57600 254" << std::endl
<< std::endl
<< "other options:" << std::endl
<< " help - print this help" << std::endl
<< " coms - displays available COM ports" << std::endl
<< "version - displays program version" << std::endl
<< "--------------------------------------" << std::endl
;
}
void print_send_help(){
std::cout
<< "send <NODE ID> data=<CHAR PAYLOAD>\n"
<< " \\ \\ \\\n"
<< " \\ 0-255 SOME DATA\n"
<< " send - use configured ACK\n"
<< " send_noack - force no ACK\n"
<< " send_syn - force sync. ACK\n"
<< " send_asyn - force async. ACK\n"
<< " send_synasyn - force syn. asyn. ACK\n"
<< "--------------------------------------\n"
<< "example: send 33 data=B123\n";
}
void print_ack_help() {
std::cout
<< "set ack <none|synasyn|syn|asyn> \n"
<< " /______/______/__/ \n"
<< " none - no ACK \n"
<< " syn - synchronous ACK \n"
<< " asyn - asynchronous ACK \n"
<< " synasyn - synchr. ACKed async. ACK \n"
<< "--------------------------------------\n"
<< "example: set ack none\n";
}
void print_commands_help() {
std::cout << "available commands:\n"
<< "======================================\n";
print_send_help();
std::cout << "======================================\n";
print_ack_help();
std::cout << "======================================\n";
}
void print_available_com_ports(){
#ifdef _WIN32
HANDLE hCom = NULL;
std::cout << "available COM ports:" << std::endl
<< "--------------------------------------" << std::endl;
for (int i = 1; i <= 99; ++i) {
std::string com_str = std::string("\\\\.\\COM") + std::to_string(i);
hCom =
CreateFile(
std::wstring(com_str.begin(), com_str.end()).c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hCom != INVALID_HANDLE_VALUE){
std::cout << "COM" << std::to_string(i) << std::endl;
CloseHandle(hCom);
}
}
std::cout << "--------------------------------------" << std::endl
;
#endif
}
void listen_on_bus(PJON<ThroughSerial> bus, bool is_console_mode) {
if(!is_console_mode)
while(true) {
bus_mutex.lock();
bus.update();
bus.receive(1000);
bus_mutex.unlock();
delayMicroseconds(1 * 1000);
}
}
int main(int argc, char **argv) {
bool is_console_mode = false;
if (argc == 2) {
if (std::string(argv[1]) == "help") {
print_usage_help();
print_commands_help();
return 0;
}
else if (std::string(argv[1]) == "coms") {
print_available_com_ports();
return 0;
}
else if (std::string(argv[1]) == "version") {
std::cout << "VERSION: " << PJON_PIPER_VERSION << "\n";
return 0;
}
print_usage_help();
std::cerr << "ERROR: option not supported\n";
return 1;
}
if (!is_enough_args(argc, argv)) {
print_usage_help();
std::cerr << "ERROR: not enough args\n";
return 1;
}
if (!is_first_arg_com_port(argc, argv)) {
print_usage_help();
std::cerr << "ERROR: first arg <COM PORT> should be COMXX\n";
return 1;
}
if (!is_second_arg_bitrate(argc, argv)) {
print_usage_help();
std::cerr << "ERROR: second arg <BITRATE> should specify bitrate 1 - 153600 like 2400, 19200, 38400, 57600, 115200, 153600\n";
return 1;
}
if (!is_third_arg_bus_id(argc, argv)) {
print_usage_help();
std::cerr << "ERROR: third arg <NODE ID> should specify bus address 0 - 255\n";
return 1;
}
if (argc > 4) {
if (!is_fourth_arg_console_switch(argc, argv)) {
print_usage_help();
std::cerr << "ERROR: fourth optional arg <console> should be console or empty\n";
return 1;
}
else {
is_console_mode = true;
}
}
else {
is_console_mode = false;
}
#ifdef _WIN32
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
FlushConsoleInputBuffer(hStdIn);
#endif
bool resetComOnStratup = false;
bool testComOnStartup = false;
#ifdef _WIN32
std::string com_str = std::string("\\\\.\\") + std::string(argv[1]);
#elif defined(RPI)
std::string com_str = std::string(argv[1]);
#endif
int bitRate = std::stoi(std::string(argv[2]));
printf("PJON instantiation... \n");
uint8_t bus_id[] = { 0, 0, 0, 1 };
PJON<ThroughSerial> bus(std::stoi(std::string(argv[3])));
bus.set_router(false);
try {
printf("Opening serial... \n");
#ifdef _WIN32
Serial serial_handle(com_str, bitRate, testComOnStartup, resetComOnStratup);
#elif defined(RPI)
int s = serialOpen(com_str.c_str(), bitRate);
if (s < 0) printf("Serial open fail!");
if (wiringPiSetup() == -1) printf("WiringPi setup fail");
#endif
if (resetComOnStratup)
delayMicroseconds(2 * 1000 * 1000);
printf("Setting serial... \n");
#ifdef _WIN32
bus.strategy.set_serial(&serial_handle);
#elif defined(RPI)
bus.strategy.set_serial(s);
#endif
printf("Opening bus... \n");
bus.begin();
bus.set_receiver(receiver_function);
bus.set_error(error_handler_function);
bus.set_synchronous_acknowledge(true);
std::thread bus_receive_thd(listen_on_bus, bus, is_console_mode);
while (true) {
//std::string stdIn = "NO_INPUT";//read_std_in();
std::string stdIn = read_std_in();
if (stdIn != "NO_INPUT") {
auto command = get_token_at_pos_for_delim(stdIn, " ", 0);
if (command == "send" ||
command == "send_noack" ||
command == "send_syn" ||
command == "send_asyn" ||
command == "send_synasyn") {
// send <node_address> data=<payload>
try {
uint16_t original_config = bus.config;
std::string node_addr_str = get_token_at_pos_for_delim(stdIn, " ", 1);
std::string data_str = get_token_at_pos_for_delim(stdIn, "data=", 1);
if (command == "send_syn") {
bus.set_synchronous_acknowledge(true);
bus.set_asynchronous_acknowledge(false);
}
else if (command == "send_noack") {
bus.set_synchronous_acknowledge(false);
bus.set_asynchronous_acknowledge(false);
}
else if (command == "send_asyn") {
bus.set_synchronous_acknowledge(false);
bus.set_asynchronous_acknowledge(true);
}
else if (command == "send_synasyn") {
bus.set_synchronous_acknowledge(true);
bus.set_asynchronous_acknowledge(true);
}
std::cout << "snd"
<< " rcv_id=" << node_addr_str
<< " data=" << data_str
<< std::endl;
int node_adr = std::stoi(node_addr_str);
bus_mutex.lock();
bus.send(
node_adr,
data_str.c_str(),
data_str.length()
);
for (int i = 0; i < 10; i++) {
bus.update();
//delayMicroseconds(10);
}
bus_mutex.unlock();
bus.config = original_config;
}
catch (const std::exception&) {
std::cerr << "ERROR: SEND failure\n";
}
}
else if (command == "set") {
std::string parameter = get_token_at_pos_for_delim(stdIn, " ", 1);
std::string parameter_value = get_token_at_pos_for_delim(stdIn, " ", 2);
if (parameter == "ack") {
if (parameter_value == "syn") {
std::cout << " ack set to: " << "syn";
bus.set_synchronous_acknowledge(true);
bus.set_asynchronous_acknowledge(false);
}
else if (parameter_value == "asyn") {
std::cout << " ack set to: " << "asyn";
bus.set_asynchronous_acknowledge(true);
bus.set_synchronous_acknowledge(false);
}
else if (parameter_value == "synasyn") {
std::cout << " ack set to: " << "synasyn";
bus.set_asynchronous_acknowledge(true);
bus.set_synchronous_acknowledge(true);
}
else if (parameter_value == "none") {
std::cout << " ack set to: " << "none";
bus.set_asynchronous_acknowledge(false);
bus.set_synchronous_acknowledge(false);
}
else {
print_ack_help();
std::cout << "ERROR: ack mode not supported";
}
std::cout << std::endl;
}
}
else {
print_commands_help();
std::cout << "ERROR: command not supported\n";
}
}
bus_mutex.lock();
bus.update();
if(is_console_mode){
bus.receive(1000);
}
bus_mutex.unlock();
delayMicroseconds(1 * 1000);
}
//printf("Attempting to receive from bus on exit... \n");
//bus.receive(100);
//printf("Success! \n");
return 0;
}
catch (const char* msg) {
std::cout << "exc: "
<< msg
<< std::endl;
print_available_com_ports();
return 1;
}
};