forked from mukund1403/Search-and-Rescue-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtls-alex-server.cpp
More file actions
361 lines (277 loc) · 7.37 KB
/
Copy pathtls-alex-server.cpp
File metadata and controls
361 lines (277 loc) · 7.37 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
#include "make_tls_server.h"
#include "tls_common_lib.h"
#include "netconstants.h"
#include "constants.h"
#include "packet.h"
#include "serial.h"
#include "serialize.h"
/* TODO: Set PORT_NAME to the port name of your Arduino */
#define PORT_NAME "/dev/ttyACM0"
/* END TODO */
#define BAUD_RATE B57600
// TLS Port Number
#define SERVER_PORT 5000
/* TODO: #define constants for the filenames for Alex's private key, certificate, CA certificate name,
and the Common Name for your laptop */
/* END TODO */
// Our network buffer consists of 1 byte of packet type, and 128 bytes of data
#define BUF_LEN 129
// This variable shows whether a network connection is active
// We will also use this variable to prevent the server from serving
// more than one connection, to keep connection management simple.
static volatile int networkActive;
// This variable is used by sendNetworkData to send back responses
// to the TLS connection. It is sent by handleNetworkData
static void *tls_conn = NULL;
/*
Alex Serial Routines to the Arduino
*/
// Prototype for sendNetworkData
void sendNetworkData(const char *, int);
void handleErrorResponse(TPacket *packet)
{
printf("UART ERROR: %d\n", packet->command);
char buffer[2];
buffer[0] = NET_ERROR_PACKET;
buffer[1] = packet->command;
sendNetworkData(buffer, sizeof(buffer));
}
void handleMessage(TPacket *packet)
{
char data[33];
printf("UART MESSAGE PACKET: %s\n", packet->data);
data[0] = NET_MESSAGE_PACKET;
memcpy(&data[1], packet->data, sizeof(packet->data));
sendNetworkData(data, sizeof(data));
}
void handleStatus(TPacket *packet)
{
char data[65];
printf("UART STATUS PACKET\n");
data[0] = NET_STATUS_PACKET;
memcpy(&data[1], packet->params, sizeof(packet->params));
sendNetworkData(data, sizeof(data));
}
void handleResponse(TPacket *packet)
{
// The response code is stored in command
switch(packet->command)
{
case RESP_OK:
char resp[2];
printf("Command OK\n");
resp[0] = NET_ERROR_PACKET;
resp[1] = RESP_OK;
sendNetworkData(resp, sizeof(resp));
break;
case RESP_STATUS:
handleStatus(packet);
break;
default:
printf("Boo\n");
}
}
void handleUARTPacket(TPacket *packet)
{
switch(packet->packetType)
{
case PACKET_TYPE_COMMAND:
// Only we send command packets, so ignore
break;
case PACKET_TYPE_RESPONSE:
handleResponse(packet);
break;
case PACKET_TYPE_ERROR:
handleErrorResponse(packet);
break;
case PACKET_TYPE_MESSAGE:
handleMessage(packet);
break;
}
}
void uartSendPacket(TPacket *packet)
{
char buffer[PACKET_SIZE];
int len = serialize(buffer, packet, sizeof(TPacket));
serialWrite(buffer, len);
}
void handleError(TResult error)
{
switch(error)
{
case PACKET_BAD:
printf("ERROR: Bad Magic Number\n");
break;
case PACKET_CHECKSUM_BAD:
printf("ERROR: Bad checksum\n");
break;
default:
printf("ERROR: UNKNOWN ERROR\n");
}
}
void *uartReceiveThread(void *p)
{
char buffer[PACKET_SIZE];
int len;
TPacket packet;
TResult result;
int counter=0;
while(1)
{
len = serialRead(buffer);
counter+=len;
if(len > 0)
{
result = deserialize(buffer, len, &packet);
if(result == PACKET_OK)
{
counter=0;
handleUARTPacket(&packet);
}
else
if(result != PACKET_INCOMPLETE)
{
printf("PACKET ERROR\n");
handleError(result);
} // result
} // len > 0
} // while
}
/*
Alex Network Routines
*/
void sendNetworkData(const char *data, int len)
{
// Send only if network is active
if(networkActive)
{
// Use this to store the number of bytes actually written to the TLS connection.
int c;
printf("WRITING TO CLIENT\n");
if(tls_conn != NULL) {
/* TODO: Implement SSL write here to write data to the network. Note that
handleNetworkData should already have set tls_conn to point to the TLS
connection we want to write to. */
/* END TODO */
}
// Network is still active if we can write more then 0 bytes.
networkActive = (c > 0);
}
}
void handleCommand(void *conn, const char *buffer)
{
// The first byte contains the command
char cmd = buffer[1];
uint32_t cmdParam[2];
// Copy over the parameters.
memcpy(cmdParam, &buffer[2], sizeof(cmdParam));
TPacket commandPacket;
commandPacket.packetType = PACKET_TYPE_COMMAND;
commandPacket.params[0] = cmdParam[0];
commandPacket.params[1] = cmdParam[1];
printf("COMMAND RECEIVED: %c %d %d\n", cmd, cmdParam[0], cmdParam[1]);
switch(cmd)
{
case 'f':
case 'F':
commandPacket.command = COMMAND_FORWARD;
uartSendPacket(&commandPacket);
break;
case 'b':
case 'B':
commandPacket.command = COMMAND_REVERSE;
uartSendPacket(&commandPacket);
break;
case 'l':
case 'L':
commandPacket.command = COMMAND_TURN_LEFT;
uartSendPacket(&commandPacket);
break;
case 'r':
case 'R':
commandPacket.command = COMMAND_TURN_RIGHT;
uartSendPacket(&commandPacket);
break;
case 's':
case 'S':
commandPacket.command = COMMAND_STOP;
uartSendPacket(&commandPacket);
break;
case 'c':
case 'C':
commandPacket.command = COMMAND_CLEAR_STATS;
commandPacket.params[0] = 0;
uartSendPacket(&commandPacket);
break;
case 'g':
case 'G':
commandPacket.command = COMMAND_GET_STATS;
uartSendPacket(&commandPacket);
break;
default:
printf("Bad command\n");
}
}
void handleNetworkData(void *conn, const char *buffer, int len)
{
/* Note: A problem with our design is that we actually get data to be written
to the SSL network from the serial port. I.e. we send a command to the Arduino,
get back a status, then write to the TLS connection. So we do a hack:
we assume that whatever we get back from the Arduino is meant for the most
recent client, so we just simply store conn, which contains the TLS
connection, in a global variable called tls_conn */
tls_conn = conn; // This is used by sendNetworkData
if(buffer[0] == NET_COMMAND_PACKET)
handleCommand(conn, buffer);
}
void *worker(void *conn)
{
int len;
char buffer[BUF_LEN];
while(networkActive)
{
/* TODO: Implement SSL read into buffer */
/* END TODO */
// As long as we are getting data, network is active
networkActive=(len > 0);
if(len > 0)
handleNetworkData(conn, buffer, len);
else
if(len < 0)
perror("ERROR READING NETWORK: ");
}
// Reset tls_conn to NULL.
tls_conn = NULL;
EXIT_THREAD(conn);
}
void sendHello()
{
// Send a hello packet
TPacket helloPacket;
helloPacket.packetType = PACKET_TYPE_HELLO;
uartSendPacket(&helloPacket);
}
int main()
{
// Start the uartReceiveThread. The network thread is started by
// createServer
pthread_t serThread;
printf("\nALEX REMOTE SUBSYSTEM\n\n");
printf("Opening Serial Port\n");
// Open the serial port
startSerial(PORT_NAME, BAUD_RATE, 8, 'N', 1, 5);
printf("Done. Waiting 3 seconds for Arduino to reboot\n");
sleep(3);
printf("DONE. Starting Serial Listener\n");
pthread_create(&serThread, NULL, uartReceiveThread, NULL);
printf("Starting Alex Server\n");
networkActive = 1;
/* TODO: Call createServer with the necessary parameters to do client authentication and to send
Alex's certificate. Use the #define names you defined earlier */
/* TODO END */
printf("DONE. Sending HELLO to Arduino\n");
sendHello();
printf("DONE.\n");
// Loop while the server is active
while(server_is_running());
}