-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4c_tcp.c
504 lines (429 loc) · 16.1 KB
/
lab4c_tcp.c
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
#include <stdlib.h>
#include <getopt.h>
#include <mraa.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <math.h>
#include <errno.h>
#define LAB "lab4b"
int DEBUG = 0;
// Global variables
int SOCK;
char *scale = NULL;
int period = 1;
int startFlag = 1;
float cToF(float c)
{
return (c * (9.0/5.0)) + 32;
}
float readTemp(const mraa_aio_context sensor, const char * scale)
{
// Constants
int R0 = 100000;
int B = 4275;
int reading = mraa_aio_read(sensor);
float R = 1023.0 / reading - 1.0;
R *= R0;
float temp = 1.0/(log(R/R0)/B+1/298.15)-273.15;;
if (strcmp(scale, "F") == 0)
{
temp = cToF(temp);
}
return temp;
}
// OLD BUTTON CODE NOT PART OF THIS LAB
// void* buttonHandler(void *fd)
// {
// int logFd = *(int *) fd;
// time_t rawTime;
// time(&rawTime);
// struct tm *formattedTime_curr = localtime(&rawTime);
// printf("%02d:%02d:%02d SHUTDOWN\n", formattedTime_curr->tm_hour, formattedTime_curr->tm_min, formattedTime_curr->tm_sec);
// if (logFd != -2)
// {
// char shutdownOutput[128];
// sprintf(shutdownOutput, "%02d:%02d:%02d SHUTDOWN\n", formattedTime_curr->tm_hour, formattedTime_curr->tm_min, formattedTime_curr->tm_sec);
// write(logFd, &shutdownOutput, strlen(shutdownOutput));
// }
// exit(0);
// }
void processCmd(char *cmd, int logFd)
{
if (DEBUG)
{
fprintf(stderr, "%s| cmd: %s", LAB, cmd);
}
if (logFd != -2)
{
write(logFd, cmd, strlen(cmd));
}
if (strstr(cmd, "SCALE=") != NULL)
{
if (cmd[6] == 'F')
{
scale = "F";
}
else if (cmd[6] == 'C')
{
scale = "C";
}
if (DEBUG)
{
fprintf(stderr, "%s| SCALE cmd, new val:%s original string:%s\n", LAB, scale, cmd);
}
}
else if (strstr(cmd, "PERIOD=") != NULL)
{
period = atoi(&cmd[7]);
if (DEBUG)
{
fprintf(stderr, "%s| period changed to %d, original string %s\n", LAB, period, cmd);
}
}
else if (strstr(cmd, "STOP") != NULL)
{
startFlag = 0;
if (DEBUG)
{
fprintf(stderr, "%s| STOP cmd, new val:%d original string:%s\n", LAB, startFlag, cmd);
}
}
else if (strstr(cmd, "START") != NULL)
{
startFlag = 1;
if (DEBUG)
{
fprintf(stderr, "%s| START cmd, new val:%d original string:%s\n", LAB, startFlag, cmd);
}
}
else if (strstr(cmd, "OFF") != NULL)
{
time_t rawTime;
time(&rawTime);
struct tm *formattedTime_curr = localtime(&rawTime);
char shutdownOutput[128];
sprintf(shutdownOutput, "%02d:%02d:%02d SHUTDOWN\n", formattedTime_curr->tm_hour, formattedTime_curr->tm_min, formattedTime_curr->tm_sec);
if (write(SOCK, &shutdownOutput, strlen(shutdownOutput)) == -1)
{
fprintf(stderr, "%s| error writing to socket (error code: %d), exiting...\n", LAB, errno);
exit(2);
}
if (logFd != -2)
{
write(logFd, &shutdownOutput, strlen(shutdownOutput));
}
exit(0);
}
// Any incoming cmd is first logged and LOG cmds only need to be logged so do nothing
else if (strstr(cmd, "LOG") != NULL)
{
;
}
// Bogus command, already logged so do nothing (no echo to stdout)
else
{
;
}
}
void printCmd(int cmdStartIndex, int newlineIndex, char *buf)
{
fprintf(stderr, "%s| printing cmd being sent to processCmd: ", LAB);
for (int i = cmdStartIndex; i < newlineIndex; i++)
{
fprintf(stderr, "%c", buf[i]);
}
fprintf(stderr, " ... end of cmd\n");
}
void processInput(int logFd)
{
int CMDBUFMAXSIZE = 1024, RDBUFMAXSIZE = 512, cmdBufSize = CMDBUFMAXSIZE, cmdBufIndex = 0, rdBufIndex, rdcnt, done = 0, readFlag = 1;
char cmdBuf[sizeof(char) * (CMDBUFMAXSIZE + 1)]; // +1 safety for '\0', reading up to 512 actual chars
char *rdBuf = NULL;
// Read from read()
// process char by char until newline, end of read buf, end of cmdBuf
// if newline, send to processCmd
// if end of buf, read again and continue adding to cmdBuf until above conditions met
while (!done)
{
if (readFlag)
{
rdBuf = malloc(sizeof(char) * (RDBUFMAXSIZE + 1));
rdBuf[RDBUFMAXSIZE + 1] = '\0';
rdcnt = read(SOCK, rdBuf, RDBUFMAXSIZE);
rdBufIndex = 0;
readFlag = 0;
}
if (DEBUG)
{
fprintf(stderr, "%s| read from socket: %s ... End of read\n", LAB, rdBuf);
}
if (rdcnt == -1)
{
fprintf(stderr, "%s| error reading from socket (error code: %d -> %s), exiting...\n", LAB, errno, strerror(errno));
exit(2);
}
while (rdBufIndex < rdcnt && rdBuf[rdBufIndex] != '\n' && cmdBufSize > 0)
{
cmdBuf[cmdBufIndex] = rdBuf[rdBufIndex];
cmdBufIndex++;
rdBufIndex++;
cmdBufSize--;
}
// If current char is '\n' (we're at end of cmd) && next index is out of bounds,
// we've read entire cmd and (assume) nothing more to read after processing this cmd -> done processing, set done flag
if (rdBuf[rdBufIndex] == '\n' && rdBufIndex + 1 >= rdcnt)
{
done = 1;
}
if (rdBuf[rdBufIndex] == '\n')
{
// processCmd, reset indices/sizes, do not reset read flag b/c there may be more to read in rdBuf, continue statement
cmdBuf[cmdBufIndex++] = rdBuf[rdBufIndex]; // Copy over newline char
cmdBuf[cmdBufIndex] = '\0'; // Null terminate cmd string
processCmd(cmdBuf, logFd);
cmdBufSize = CMDBUFMAXSIZE; // Reset size of cmdBuf, since cmd is now processed we can use full buf for next cmd
cmdBufIndex = 0; // Reset cmdBufIndex b/c we are writing new cmd to buf so start at beginning of buf
rdBufIndex++; // Increment rdBufIndex to start of next command
// continue;
}
else if (rdBufIndex >= rdcnt)
{
// read everything from read call but at partial cmd or LOG cmd w/ long text
// reset indices/sizes (no need actually, done in read if statement above),
// free old rdBuf, reset read flag, continue statement to read again and continue processing
free(rdBuf);
readFlag = 1; // Reset read flag so we read again at next iteration
// continue;
}
else if (cmdBufSize <= 0) // Most likely in case of a long LOG text cmd (not fully sure tho) so we just process cmd and start again
{
// cmdBuf full, process cmd, reset, continue statement
cmdBuf[cmdBufIndex] = '\0'; // Set +1 overflow spot from earlier to null terminate string
processCmd(cmdBuf, logFd);
cmdBufSize = CMDBUFMAXSIZE; // Reset size of cmdBuf, since cmd is now processed we can use full buf to continue processing
cmdBufIndex = 0; // Reset cmdBufIndex b/c we continue processing with 'clean' buf so start at beginning of buf
// continue;
}
}
}
// Difference in time in seconds
int timeDiff(const struct tm oldTime, const struct tm newTime)
{
return ((newTime.tm_hour - oldTime.tm_hour) * 3600) + ((newTime.tm_min - oldTime.tm_min) * 60) + (newTime.tm_sec - oldTime.tm_sec);
}
int main(int argc, char * const argv[])
{
// Arg processing
int opt;
char *logFN = NULL, *host = NULL, *id = NULL, *port = NULL;
struct option long_options[] =
{
{"period", required_argument, NULL, 'p'},
{"scale", required_argument, NULL, 's'},
{"log", required_argument, NULL, 'l'},
{"id", required_argument, NULL, 'i'},
{"host", required_argument, NULL, 'h'},
{"debug", no_argument, &DEBUG, 1},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, ":p:s:l:di:h:", long_options, NULL)) != -1)
{
switch (opt)
{
case 'p':
period = atoi(optarg);
break;
case 's':
scale = optarg;
break;
case 'l':
logFN = optarg;
break;
case 'i':
id = optarg;
break;
case 'h':
host = optarg;
break;
case 'd':
DEBUG = 1;
break;
case 0:
break;
case ':':
fprintf(stderr, "%s| option -%c has required argument, see usage below; exiting...\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB, optopt, LAB);
exit(1);
case '?':
default:
fprintf(stderr, "%s| option -%c is not allowed, see usage below; exiting...\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB, optopt, LAB);
exit(1);
}
}
if (host == NULL)
{
fprintf(stderr, "%s| Required host argument option missing, see usage below; exiting...\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB, LAB);
exit(1);
}
if (id == NULL)
{
fprintf(stderr, "%s| Required id argument option missing, see usage below; exiting...\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB, LAB);
exit(1);
}
if (optind >= argc)
{
fprintf(stderr, "%s| Required port number argument option missing, see usage below; exiting...\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB, LAB);
exit(1);
}
port = argv[optind++];
if (optind < argc)
{
fprintf(stderr, "%s| Extra argument(s):", LAB);
for (; optind < argc; optind++)
{
fprintf(stderr, " %s", argv[optind]);
}
fprintf(stderr, ", see usage below; exiting...\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB);
exit(1);
}
if (scale != NULL && ((strcmp(scale, "C") != 0) && (strcmp(scale, "F") != 0)))
{
fprintf(stderr, "%s| --scale option takes only 'C' or 'F'\n usage: %s [--period=<time in seconds>] [--scale=<C or F>] [--log=<filename>] --host=<hostname> --id=<ID> <port>\n", LAB, LAB);
exit(1);
}
else if (scale == NULL)
{
scale = "F";
}
if (DEBUG)
{
fprintf(stderr, "%s| Parsed input - period: %d, scale: %s, log: %s, hostname: %s, port: %s, id: %s\n", LAB, period, scale, logFN, host, port, id);
}
int logFd = -2;
if (logFN != NULL && ((logFd = open(logFN, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1))
{
fprintf(stderr, "%s| error opening log file (error code: %d -> %s), exiting...\n", LAB, errno, strerror(errno));
exit(1);
}
mraa_aio_context tempSensor;
if ((tempSensor = mraa_aio_init(1)) == NULL)
{
fprintf(stderr, "%s| error initializing temperator sensor, exiting...\n", LAB);
exit(2);
}
// ====== OLD BUTTON CODE NOT PART OF THIS LAB ======
// mraa_gpio_context button;
// if ((button = mraa_gpio_init(60)) == NULL)
// {
// fprintf(stderr, "%s| error initializing button, exiting...\n", LAB);
// exit(1);
// }
// if ((mraa_rc = mraa_gpio_dir(button, MRAA_GPIO_IN)) != MRAA_SUCCESS)
// {
// fprintf(stderr, "%s| error setting direction of button (mraa_result_t error code: %d), exiting...\n", LAB, mraa_rc);
// exit(1);
// }
// if ((mraa_rc = mraa_gpio_isr(button, MRAA_GPIO_EDGE_RISING, (void *) buttonHandler, &logFd)) != MRAA_SUCCESS)
// {
// fprintf(stderr, "%s| error setting direction of button (mraa_result_t error code: %d), exiting...\n", LAB, mraa_rc);
// exit(1);
// }
// ==================================================
if ((SOCK = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "%s| error creating socket (error code: %d), exiting...\n", LAB, errno);
exit(2);
}
struct hostent *servaddr = gethostbyname(host);
if (servaddr == NULL)
{
fprintf(stderr, "%s| error calling 'gethostbyname' (error code: %d), exiting...\n", LAB, errno);
exit(1);
}
struct sockaddr_in servinfo;
bzero((char *) &(servinfo), sizeof(servinfo));
servinfo.sin_family = AF_INET;
servinfo.sin_port = htons(atoi(port));
servinfo.sin_addr.s_addr = *(long *) servaddr->h_addr_list[0];
if (connect(SOCK,(struct sockaddr *) &servinfo, sizeof(servinfo)) == -1)
{
fprintf(stderr, "%s| error connecting socket (error code: %d), exiting...\n", LAB, errno);
exit(1);
}
char concatId[50];
sprintf(concatId, "ID=%s\n", id);
if (write(SOCK, concatId, strlen(concatId)) == -1)
{
fprintf(stderr, "%s| error writing to socket (error code: %d), exiting...\n", LAB, errno);
exit(2);
}
if (logFd > 0)
{
write(logFd, concatId, strlen(concatId));
}
if (DEBUG)
{
fprintf(stderr, "%s| connected and ID should be sent; id: %s\n", LAB, concatId);
}
time_t rawTime;
time(&rawTime);
struct tm formattedTime_last = *localtime(&rawTime);
struct pollfd pollList[1];
// 0 index is keyboard
pollList[0].fd = SOCK;
pollList[0].events = POLLIN;
while (1)
{
int rc = poll(pollList, 1, 0);
if (rc == -1)
{
fprintf(stderr, "%s| error polling socket (error code: %d => %s), exiting...\n", LAB, errno, strerror(errno));
exit(2);
}
if (rc > 0)
{
if (pollList[0].revents & POLLIN)
{
processInput(logFd);
}
else if (pollList[0].revents == POLLHUP || pollList[0].revents == POLLERR)
{
if (DEBUG)
{
fprintf(stderr, "%s| Entered POLLHUP/POLLERR condition\r\n", LAB);
}
if (logFd > 0 && close(logFd) == -1)
{
fprintf(stderr, "%s| error closing log file (error code: %d => %s), exiting...\n", LAB, errno, strerror(errno));
exit(2);
}
exit(0);
}
}
time_t rawTime;
time(&rawTime);
struct tm formattedTime_curr = *localtime(&rawTime);
if (DEBUG)
{
// fprintf(stderr, "%s| last: h:%d m:%d s:%d curr: h:%d m:%d s:%d\n", LAB, formattedTime_last.tm_hour, formattedTime_last.tm_min, formattedTime_last.tm_sec, formattedTime_curr.tm_hour, formattedTime_curr.tm_min, formattedTime_curr.tm_sec);
}
if (timeDiff(formattedTime_last, formattedTime_curr) >= period && startFlag)
{
formattedTime_last = formattedTime_curr;
float temp = readTemp(tempSensor, scale);
char tempOutput[128];
sprintf(tempOutput, "%02d:%02d:%02d %0.1f\n", formattedTime_curr.tm_hour, formattedTime_curr.tm_min, formattedTime_curr.tm_sec, temp);
if (write(SOCK, &tempOutput, strlen(tempOutput)) == -1)
{
fprintf(stderr, "%s| error writing to socket (error code: %d), exiting...\n", LAB, errno);
exit(2);
}
if (logFN != NULL)
{
write(logFd, &tempOutput, strlen(tempOutput));
}
}
}
}