-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_client.cpp
More file actions
516 lines (456 loc) · 12.7 KB
/
Copy pathfinal_client.cpp
File metadata and controls
516 lines (456 loc) · 12.7 KB
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
// Routines to create a TLS client
#include "make_tls_client.h"
// Network packet types
#include "netconstants.h"
// Packet types, error codes, etc.
#include "constants.h"
#include <ncurses.h>
// Tells us that the network is running.
static volatile int networkActive=0;
volatile char lastCommand = 'r';
void handleError(const char *buffer)
{
switch(buffer[1])
{
case RESP_OK:
printf("Command / Status OK\n");
break;
case RESP_BAD_PACKET:
printf("BAD MAGIC NUMBER FROM ARDUINO\n");
break;
case RESP_BAD_CHECKSUM:
printf("BAD CHECKSUM FROM ARDUINO\n");
break;
case RESP_BAD_COMMAND:
printf("PI SENT BAD COMMAND TO ARDUINO\n");
break;
case RESP_BAD_RESPONSE:
printf("PI GOT BAD RESPONSE FROM ARDUINO\n");
break;
default:
printf("PI IS CONFUSED!\n");
}
}
void handleStatus(const char *buffer)
{
int32_t data[16];
memcpy(data, &buffer[1], sizeof(data));
printf("\n ------- ALEX STATUS REPORT ------- \n\n");
printf("Left Forward Ticks:\t\t%d\n", data[0]);
printf("Right Forward Ticks:\t\t%d\n", data[1]);
printf("Left Reverse Ticks:\t\t%d\n", data[2]);
printf("Right Reverse Ticks:\t\t%d\n", data[3]);
printf("Left Forward Ticks Turns:\t%d\n", data[4]);
printf("Right Forward Ticks Turns:\t%d\n", data[5]);
printf("Left Reverse Ticks Turns:\t%d\n", data[6]);
printf("Right Reverse Ticks Turns:\t%d\n", data[7]);
printf("Forward Distance:\t\t%d\n", data[8]);
printf("Reverse Distance:\t\t%d\n", data[9]);
printf("\n---------------------------------------\n\n");
}
void handleMessage(const char *buffer)
{
printf("MESSAGE FROM ALEX: %s\n", &buffer[1]);
}
void handleCommand(const char *buffer)
{
// We don't do anything because we issue commands
// but we don't get them. Put this here
// for future expansion
}
void handleNetwork(const char *buffer, int len)
{
// The first byte is the packet type
int type = buffer[0];
switch(type)
{
case NET_ERROR_PACKET:
handleError(buffer);
break;
case NET_STATUS_PACKET:
handleStatus(buffer);
break;
case NET_MESSAGE_PACKET:
handleMessage(buffer);
break;
case NET_COMMAND_PACKET:
handleCommand(buffer);
break;
}
}
void sendData(void *conn, const char *buffer, int len)
{
int c;
printf("\nSENDING %d BYTES DATA\n\n", len);
if(networkActive)
{
/* TODO: Insert SSL write here to write buffer to network */
c = sslWrite(conn, buffer, len);
/* END TODO */
networkActive = (c > 0);
}
}
void *readerThread(void *conn)
{
char buffer[128];
int len;
while(networkActive)
{
/* TODO: Insert SSL read here into buffer */
len = sslRead(conn, buffer, sizeof(buffer));
printf("read %d bytes from server.\n", len);
// if (len > 0 && buffer[0] == NET_LIDAR_PACKET) {
// buffer[len] = '\0'; // Null-terminate to be safe
// printf("Received LiDAR scan data: %s\n", buffer);
// }
/* END TODO */
networkActive = (len > 0);
if(networkActive)
handleNetwork(buffer, len);
}
printf("Exiting network listener thread\n");
/* TODO: Stop the client loop and call EXIT_THREAD */
stopClient();
EXIT_THREAD(conn);
/* END TODO */
return NULL;
}
void flushInput()
{
char c;
while((c = getchar()) != '\n' && c != EOF);
}
void getParams(int32_t *params)
{
printf("Enter distance/angle in cm/degrees (e.g. 50) and power in %% (e.g. 75) separated by space.\n");
printf("E.g. 50 75 means go at 50 cm at 75%% power for forward/backward, or 50 degrees left or right turn at 75%% power\n");
scanf("%d %d", ¶ms[0], ¶ms[1]);
flushInput();
}
void *writerThread(void *conn)
{
int quit=0;
while(!quit)
{
char ch;
printf("NORMAL MODE: f, b, l, k(right), STOP IS 'r' || OTHER MODE: m (WASD) || GET COLOUR = c, OPEN CLAW = o, CLOSE CLAW = p, MEDPACK = i, h=clear stats, g=get stats q=exit)\n");
scanf("%c", &ch);
// Purge extraneous characters from input stream
flushInput();
char buffer[10];
int32_t params[2];
buffer[0] = NET_COMMAND_PACKET;
switch(ch)
{
case 'm':
case 'M': {
initscr();
cbreak(); // Initialize ncurses window
noecho();
timeout(300); // Check every 100ms (responsive yet safe)
curs_set(0); // Invisible cursor
int running = 1;
char CommandSent = 'r'; // Track if forward command is active
while (running) {
int c = getch(); // Non-blocking check every 100ms
if (c == 'w') {
flushinp();
if (CommandSent != 'w') {
buffer[1] = 'w';
params[0] = 10; // Safe default distance
params[1] = 100; // Speed
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = 'w';
}
}
if(c == 's'){
flushinp();
if (CommandSent != 's') {
buffer[1] = 's';
params[0] = 10; // Safe default distance
params[1] = 100; // Speed
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = 's';
}
}
else if(c == 'a') {
flushinp();
if (CommandSent != 'a') {
params[0] = 0; // Safe default distance
params[1] = 0; // Speed
buffer[1] = 'a';
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = 'a';
}
}
else if(c == 'd') {
flushinp();
if (CommandSent != 'd') {
buffer[1] = 'd';
params[0] = 0; // Safe default distance
params[1] = 0; // Speed
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = 'd';
}
}
else if (c == ERR) {
flushinp(); // Key is released (no key detected)
if (CommandSent != 'r') {
buffer[1] = 'r'; // send stop command exactly once
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = 'r'; // Clear buffer to prevent flooding
}
}
else if (c == 'q' || c == 'Q') {
flushinp();
running = 0;
if (CommandSent != 'r') {
// Ensure robot stops if quitting
buffer[1] = 'r';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = 'r';
}
}
else if (c == '=') {
flushinp();
if (CommandSent != '=') { // increase speed
// Ensure robot stops if quitting
buffer[1] = '=';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '=';
}
}
else if (c == '-') {
flushinp();
if (CommandSent != '-') { // increase speed
// Ensure robot stops if quitting
buffer[1] = '-';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '-';
}
}
else if (c == '1') {
flushinp();
if (CommandSent != '1') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '1';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '1';
}
}
else if (c == '2') {
flushinp();
if (CommandSent != '2') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '2';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '2';
}
}
else if (c == '3') {
flushinp();
if (CommandSent != '3') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '3';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '3';
}
}
else if (c == '4') {
flushinp();
if (CommandSent != '4') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '4';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '4';
}
}
else if (c == '5') {
flushinp();
if (CommandSent != '5') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '5';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '5';
}
}
else if (c == '6') {
flushinp();
if (CommandSent != '6') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '6';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '6';
}
}
else if (c == '7') {
flushinp();
if (CommandSent != '7') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '7';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '7';
}
}
else if (c == '8') {
flushinp();
if (CommandSent != '8') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '8';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '8';
}
}
else if (c == '9') {
flushinp();
if (CommandSent != '9') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '9';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '9';
}
}
else if (c == '0') {
flushinp();
if (CommandSent != '0') { // toggle speed
// Ensure robot stops if quitting
buffer[1] = '0';
params[0] = 0;
params[1] = 0;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
CommandSent = '0';
}
}
napms(150);
// consistent, safe delay between loops
}
endwin();
break;
}
case 'f':
case 'F':
case 'b':
case 'B':
case 'l':
case 'L':
case 'k':
case 'K':
getParams(params);
buffer[1] = ch;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
break;
case 'r':
case 'R':
case 'c':
case 'C':
case 'g':
case 'G':
case 'z':
case 'Z':
case 'o':
case 'i':
case 'p':
case 'h':
case 'w':
case 's':
case 'd':
case 'a':
params[0]=0;
params[1]=0;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'q':
case 'Q':
quit=1;
break;
default:
printf("BAD COMMAND\n");
}
}
printf("Exiting keyboard thread\n");
/* TODO: Stop the client loop and call EXIT_THREAD */
stopClient();
EXIT_THREAD(conn);
/* END TODO */
return NULL;
}
/* TODO: #define filenames for the client private key, certificatea,
CA filename, etc. that you need to create a client */
#define SERVER_NAME "172.20.10.6"
#define PORT_NUM 5001
#define CA_CERT_FNAME "signing.pem"
#define CLIENT_CERT_FNAME "laptop.crt"
#define CLIENT_KEY_FNAME "laptop.key"
#define SERVER_NAME_ON_CERT "mine.com"
/* END TODO */
void connectToServer(const char *serverName, int portNum)
{
/* TODO: Create a new client */
createClient(serverName, portNum, 1, CA_CERT_FNAME, SERVER_NAME_ON_CERT, 1, CLIENT_CERT_FNAME, CLIENT_KEY_FNAME, readerThread, writerThread);
/* END TODO */
}
int main(int ac, char **av)
{
/* want to put this in the begining of my program which sets up the terminal*/
if(ac != 3)
{
fprintf(stderr, "\n\n%s <IP address> <Port Number>\n\n", av[0]);
exit(-1);
}
networkActive = 1;
connectToServer(av[1], atoi(av[2]));
/* TODO: Add in while loop to prevent main from exiting while the
client loop is running */
while(client_is_running())
{
}
/* END TODO */
printf("\nMAIN exiting\n\n");
}