-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls-alex-client.cpp
338 lines (288 loc) · 6.93 KB
/
tls-alex-client.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
// 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"
// Tells us that the network is running.
static volatile int networkActive=0;
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 ------- COLOUR REPORT! ------- \n\n");
if (data[0] == 0) {
printf("RED!!");
}
else if (data[0] == 1) {
printf("GREEN!!");
}
else {
printf("WHAT IS THIS??");
}
printf("\n\n ------------------------------ \n");
printf("\n ------- ALEX STATUS REPORT ------- \n\n");
printf("Left Forward Ticks:\t\t%d\n", data[1]);
printf("Right Forward Ticks:\t\t%d\n", data[2]);
printf("Left Reverse Ticks:\t\t%d\n", data[3]);
printf("Right Reverse Ticks:\t\t%d\n", data[4]);
printf("Left Forward Ticks Turns:\t%d\n", data[5]);
printf("Right Forward Ticks Turns:\t%d\n", data[6]);
printf("Left Reverse Ticks Turns:\t%d\n", data[7]);
printf("Right Reverse Ticks Turns:\t%d\n", data[8]);
printf("Forward Distance:\t\t%d\n", data[9]);
printf("Reverse Distance:\t\t%d\n", data[10]);
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, sizeof(buffer));
/* 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);
/* 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 */
}
void flushInput()
{
char c;
while((c = getchar()) != '\n' && c != EOF);
}
void getParams(int32_t *params)
{
printf("Enter distance/angle and power.\n");
scanf("%d %d", ¶ms[0], ¶ms[1]);
flushInput();
}
void *writerThread(void *conn)
{
int quit=0;
while(!quit)
{
char ch;
printf("Movement Command.\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 'f':
case 'F':
case 'b':
case 'B':
case 'l':
case 'L':
case 'r':
case 'R':
getParams(params);
buffer[1] = ch;
memcpy(&buffer[2], params, sizeof(params));
sendData(conn, buffer, sizeof(buffer));
break;
case 'x': //stop
case 'X':
case 'c':
case 'C':
case 'z': //get stats
case 'Z':
case 'u':
case 'U':
case 'k':
case 'K':
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;
case 'w':
case 'W':
params[0] = 6;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'p':
case 'P':
params[0] = 55;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 's':
case 'S':
params[0] = 10;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'a':
case 'A':
params[0] = 18;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'd':
case 'D':
params[0] = 20;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'y':
case 'Y':
params[0] = 1;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'h':
case 'H':
params[0] = 2;
params[1] = 100;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
break;
case 'g':
case 'G':
case 'j':
case 'J':
params[0] = 10;
params[1] = 85;
memcpy(&buffer[2], params, sizeof(params));
buffer[1] = ch;
sendData(conn, buffer, sizeof(buffer));
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 */
}
/* TODO: #define filenames for the client private key, certificatea,
CA filename, etc. that you need to create a client */
#define SERVER_PORT 5000
#define privateKey "alex.key"
#define cert "alex.crt"
#define caCertName "signing.pem"
#define serverNameOnCert "Surely Laptop"
#define serverName "192.168.188.153"
#define clientCertName "laptop.crt"
#define clientPrivateKey "laptop.key"
/* END TODO */
void connectToServer(const char *server, int portNum)
{
/* TODO: Create a new client */
createClient(server, portNum, 1, caCertName, serverNameOnCert, 1, clientCertName, clientPrivateKey, readerThread, writerThread);
/* END TODO */
}
int main(int ac, char **av)
{
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");
}