forked from NachiketKelkar/AESD-Project_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintask.c
1423 lines (1269 loc) · 43.1 KB
/
maintask.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************************
* File name : maintask.c *
* Authors : Nachiket Kelkar and Puneet Bansal *
* Description : The main logic of the code *
* Tools used : GNU make, gcc, arm-linux-gcc *
************************************************************************************************/
https://stackoverflow.com/questions/18021189/how-to-connect-two-computers-over-internet-using-socket-programming-in-c
#include <unistd.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <stdbool.h>
#include <stdint.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
/*User defined headers*/
#include "maintask.h"
#include "temp_i2c.h"
#include "lightsensor.h"
#include "gpio.h"
#include "bist.h"
#include "myi2c.h"
typedef struct
{
char *logFileName;
int noOfParam;
}mainInfoToOthers;
typedef enum
{
init,
dark,
light
}state;
int fd,fd1;
pthread_t tempSensorTask,lightSensorTask,loggerTask,socketTask;
static void signal_handler(int , siginfo_t *, void*);
bool measureTemperature = true;
bool measureLight=false;
bool loggerHeartBeat=false;
bool socketHeartBeat=false;
bool exitThread = false;
/**************************************************************************************
* Function name:- tempSensorRoutine *
* Description:- This function is used by a Temperature sensor thread for execution of *
* temperature sensor task. *
* @param:- void* (data from main) *
* @return:- static void* (pthread exit value) *
**************************************************************************************/
static void* tempSensorRoutine(void *dataObj)
{
mainStruct dataToSendToMain;
logStruct dataToSendToLogger;
socketStruct dataToSendToSocket;
tempStruct dataReceived;
int i2c_file_des;
float temperature;
uint16_t config;
int gpio_val_fd;
int gpio_state;
struct pollfd fds;
int init_status = init_success;
struct sigevent tempEvent;
struct sigaction tempAction;
struct itimerspec timerSpec;
timer_t tempTimer;
bool sendTempToSocket = false;
strcpy(dataToSendToMain.source,"temperature");
dataToSendToMain.messageType = update;
/* Initialize gpio LED pin as output and pin 60 for interrupts */
gpio_init(53, out);
gpio_interrupt_state(60, both);
gpio_val_fd = gpio_open_value(60);
fds.fd = gpio_val_fd;
fds.events = POLLPRI | POLLERR;
/* Setting up all queues */
mqd_t tempToMain = mqueue_init(MAINQUEUENAME, MAIN_QUEUE_SIZE, sizeof(dataToSendToMain));
mqd_t tempToLogger = mqueue_init(LOGQUEUENAME, LOG_QUEUE_SIZE, sizeof(dataToSendToLogger));
mqd_t tempQueue = mqueue_init(TEMPQUEUENAME, TEMP_QUEUE_SIZE, sizeof(dataReceived));
mqd_t socketQueue = mqueue_init(SOCKETQUEUENAME, SOCKET_QUEUE_SIZE, sizeof(dataToSendToSocket));
if(tempToMain < 0)
{
perror("TemptoMain queue creation failed");
}
if(tempToLogger <0)
{
perror("TempToLog queue creation failed");
}
if(tempQueue < 0)
{
perror("Temp queue creation failed");
}
if(socketQueue < 0)
{
perror("socketQueue creation failed");
}
dataToSendToLogger.logLevel = alert;
dataToSendToLogger.status = fail;
dataToSendToLogger.source= "temperature";
/* Register signal handler for a signal*/
tempAction.sa_flags = SA_SIGINFO;
tempAction.sa_sigaction = signal_handler;
if((sigaction(SIGRTMIN + 4, &tempAction, NULL))<0)
{
perror("Failed setting signal handler for temp measurement");
dataToSendToLogger.message = "Signal handler set failed";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
init_status = init_failure;
}
/* Assigning signal to timer */
tempEvent.sigev_notify = SIGEV_SIGNAL;
tempEvent.sigev_signo = SIGRTMIN + 4;
tempEvent.sigev_value.sival_ptr = &tempTimer;
if((timer_create(CLOCK_REALTIME, &tempEvent, &tempTimer)) < 0)
{
perror("Timer creation failed for temperature task");
dataToSendToLogger.message = "Assigning signal failed";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
init_status = init_failure;
}
/* Setting the time and starting the timer */
timerSpec.it_interval.tv_nsec = 200000000;
timerSpec.it_interval.tv_sec = 0;
timerSpec.it_value.tv_nsec = 200000000;
timerSpec.it_value.tv_sec = 0;
if((timer_settime(tempTimer, 0, &timerSpec, NULL)) < 0)
{
perror("Starting timer in temperature task failed");
dataToSendToLogger.message = "Failed to start timer";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
init_status = init_failure;
}
/* Open i2c file to start communication with temperature sensor. */
i2c_file_des = fd1;
/* Initialize temperature sensor lower limit alert register */
int success = temp_i2c_write_to_reg(i2c_file_des, TLOW_REG_ADDR, 26);
if(success < 0)
{
printf("Write failure\nExiting Temperature measurement task\n");
dataToSendToLogger.message = "Write failure";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
init_status = init_failure;
gpio_write_value(53,high);
}
/* Initialize temperature sensor higer limit alert register. */
success = temp_i2c_write_to_reg(i2c_file_des, THIGH_REG_ADDR, 30);
if(success < 0)
{
printf("Write failure\nExiting Temperature measurement task\n");
dataToSendToLogger.message = "Write failure";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
init_status = init_failure;
gpio_write_value(53,high);
}
/* Send inititalization success/fail message to main. */
dataToSendToMain.status = init_status;
int x = mq_send(tempToMain,(char*)&dataToSendToMain,sizeof(dataToSendToMain),0);
if(x < 0)
{
perror("Sending data failed");
dataToSendToLogger.message = "Fail sending temp init success";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
}
if(init_status == init_failure)
{
dataToSendToLogger.message = "Temperature sensor init failure";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
pthread_exit(NULL);
}
strcpy(dataToSendToMain.source,"temperature");
dataToSendToLogger.source= "temperature";
strcpy(dataToSendToLogger.unit,"Celsius");
while(1)
{
/* Do only each 100msec */
if(measureTemperature)
{
int ret = mq_receive(tempQueue,(char*)&dataReceived,sizeof(dataReceived),0);
if(ret < 0)
{
//perror("No requests received");
}
else
{
sendTempToSocket = true;
}
measureTemperature = false;
dataToSendToMain.messageType = update;
dataToSendToMain.status = 1;
dataToSendToLogger.status = 1;
/* Get the temperature values in Celsius */
temperature = read_temperature(i2c_file_des, TEMP_REG_ADDR);
if(temperature == 10000)
{
printf("Temperature sensor pulled out\nExiting Temperature Sensor Task\n");
dataToSendToLogger.logLevel = alert;
dataToSendToLogger.status = fail;
dataToSendToLogger.source= "temperature";
dataToSendToLogger.message = "Read from sensor failed";
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
gpio_write_value(53,1);
close(i2c_file_des);
mq_close(tempToMain);
mq_close(tempToLogger);
mq_close(tempQueue);
mq_close(socketQueue);
mq_unlink(MAINQUEUENAME);
mq_unlink(TEMPQUEUENAME);
mq_unlink(SOCKETQUEUENAME);
mq_unlink(LOGQUEUENAME);
timer_delete(tempTimer);
pthread_exit(NULL);
}
dataToSendToLogger.logLevel = info;
dataToSendToLogger.value = temperature;
/* Send heartbeat message to main */
mq_send(tempToMain,(char*)&dataToSendToMain,sizeof(mainStruct),0);
/* Send temperature values to logger */
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
/* Send temperature values to socket only if it is requested. */
if(sendTempToSocket)
{
sendTempToSocket = false;
dataToSendToSocket.source = "temperature";
if(strcmp(dataReceived.unit,"Celsius") == 0)
{
temperature = convert_to_unit(temperature,Celsius);
}
else if(strcmp(dataReceived.unit,"Fahrenheit") == 0)
{
temperature = convert_to_unit(temperature,Fahrenheit);
}
else if(strcmp(dataReceived.unit,"Kelvin") == 0)
{
temperature = convert_to_unit(temperature,Kelvin);
}
dataToSendToSocket.value = temperature;
int ret = mq_send(socketQueue, (char*)&dataToSendToSocket, sizeof(dataToSendToSocket),0);
if (ret < 0)
{
perror("Temperature sending to socket failed");
dataToSendToLogger.logLevel = alert;
dataToSendToLogger.status = fail;
dataToSendToLogger.source= "temperature";
strcpy(dataToSendToLogger.message,"Sending temp to socket failed");
mq_send(tempToLogger,(char*)&dataToSendToLogger,sizeof(logStruct),0);
}
}
if(exitThread)
{
break;
}
}
int ret = poll(&fds, 1, 1);
if(ret > 0)
{
gpio_state = gpio_read_val_with_fd(gpio_val_fd);
if(gpio_state == 0x01)
printf("Temperature is below 26C\n");
else
printf("Temperature rise above 30C\n");
}
}
printf("_____________________Temperature task exiting_______________________\n");
/* Cleanup all initializations */
close(i2c_file_des);
mq_close(tempToMain);
mq_close(tempToLogger);
mq_close(tempQueue);
mq_close(socketQueue);
mq_unlink(MAINQUEUENAME);
mq_unlink(TEMPQUEUENAME);
mq_unlink(SOCKETQUEUENAME);
mq_unlink(LOGQUEUENAME);
timer_delete(tempTimer);
pthread_exit(NULL);
}
/********************************************************************************
* Function name:- lightSensorRoutine *
* Description:- This function is used by a light sensor thread for execution of *
* light sensor task. *
* @param:- void* (data from main) *
* @return:- static void* (pthread exit value) *
********************************************************************************/
static void* lightSensorRoutine(void *dataObj)
{
mainStruct dataToSendToMain;
logStruct dataTOSendTOLogger;
lightStruct dataReceivedFromMain;
socketStruct dataToSendToSocket;
static state presentState, prevState;
presentState=init;
prevState=init;
int fd;
bool sendLightToSocket=false;
strcpy(dataToSendToMain.source,"light");
dataToSendToMain.messageType = update;
dataToSendToMain.status=init_success;
dataTOSendTOLogger.source="main";
dataTOSendTOLogger.logLevel=alert;
dataTOSendTOLogger.status=fail;
/* Initialize gpio LED pin as output */
gpio_init(54, out);
/* Setting up queues */
mqd_t lightToMain = mqueue_init(MAINQUEUENAME,MAIN_QUEUE_SIZE,sizeof(dataToSendToMain));
mqd_t lightToLogger = mqueue_init(LOGQUEUENAME,LOG_QUEUE_SIZE,sizeof(logStruct));
mqd_t lightQueue = mqueue_init(LIGHTQUEUENAME,LIGHT_QUEUE_SIZE,sizeof(lightStruct));
mqd_t lightToSocket = mqueue_init(SOCKETQUEUENAME,SOCKET_QUEUE_SIZE,sizeof(socketStruct));
if(lightToMain < 0)
{
perror("Light queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Light To Main queue creation failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
if(lightToLogger<0)
{
perror("LightToLogger queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="LightToLogger queue creation failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
if(lightQueue<0)
{
perror("LightQueue queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="LightQueue queue creation failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
if(lightToSocket<0)
{
perror("LightToSocket Queue queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="LightToSocket Queue queue creation failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
/* Initialising I2C */
fd=myi2cInit(slaveAddFloat);
int ret = lightSensorWrite(fd,CNTRLREG,0x03,2); //writing to control register.
if(ret < 0)
{
dataTOSendTOLogger.message="Writing to control register failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
gpio_write_value(54,high);
}
/* Initialising Timer */
struct sigevent lightEvent;
struct sigaction lightAction;
struct itimerspec lightTimerSpec;
timer_t lightTimer;
/* Assigning signal handler to a signal */
lightAction.sa_flags = SA_SIGINFO;
lightAction.sa_sigaction = signal_handler;
if(sigaction(SIGRTMIN + 5 , &lightAction, NULL)<0)
{
perror("Light sensor, timer handler initialisation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Light sensor, timer handler initialisation failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
/* Setting up timer */
lightEvent.sigev_notify = SIGEV_SIGNAL;
lightEvent.sigev_signo = SIGRTMIN + 5;
lightEvent.sigev_value.sival_ptr = &lightTimer;
if((timer_create(CLOCK_REALTIME, &lightEvent, &lightTimer)) < 0)
{
perror("Timer creation failed for temperature task");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Timer creation failed for temperature task";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
//exit(1);
}
/* Start the timer */
lightTimerSpec.it_interval.tv_nsec = 100000000; //To get the value after every 100 ms
lightTimerSpec.it_interval.tv_sec = 0;
lightTimerSpec.it_value.tv_nsec = 100000000;
lightTimerSpec.it_value.tv_sec = 0;
if(timer_settime(lightTimer,0,&lightTimerSpec,NULL)<0)
{
perror("Light sensor timer settime failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Light sensor timer settime failed";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
//exit(1);
}
/*Sending initialisation success/fail message*/
mq_send(lightToMain,(char*)&dataToSendToMain,sizeof(mainStruct),0);
int y=0;
float lux=0;
while(1)
{
if(measureLight)
{
if(mq_receive(lightQueue,(char*)&dataReceivedFromMain,sizeof(lightStruct),0)>-1)
{
sendLightToSocket=true;
}
measureLight=false;
//take the light sensor value and send it to logger task
dataToSendToMain.messageType = update;
dataToSendToMain.status = success;
dataTOSendTOLogger.source= "light";
dataTOSendTOLogger.logLevel=alert;
dataTOSendTOLogger.status=fail;
lux=luxCalc(fd);
if(lux < 0)
{
printf("Light sensor pulled out\n Exiting Light task\n");
dataTOSendTOLogger.message = "Failed reading the light value";
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(dataTOSendTOLogger),0);
gpio_write_value(54,high);
close(fd);
mq_close(lightToMain);
mq_close(lightToLogger);
mq_close(lightQueue);
mq_close(lightToSocket);
mq_unlink(MAINQUEUENAME);
mq_unlink(LIGHTQUEUENAME);
mq_unlink(SOCKETQUEUENAME);
mq_unlink(LOGQUEUENAME);
timer_delete(lightTimer);
pthread_exit(NULL);
}
if(lux>50)
{
presentState=light;
if(prevState!=light)
{
printf("State changed to Light\n");
dataTOSendTOLogger.message="State changed to Light";
prevState=presentState;
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(dataTOSendTOLogger),0);
}
}
else
{
presentState=dark;
if(prevState!=dark)
{
printf("State changed to Dark\n");
dataTOSendTOLogger.message="State changed to Dark";
prevState=presentState;
mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(dataTOSendTOLogger),0);
}
}
dataTOSendTOLogger.status = success;
dataTOSendTOLogger.source= "light";
dataTOSendTOLogger.value = lux;
dataTOSendTOLogger.logLevel=info;
int ret=mq_send(lightToMain,(char*)&dataToSendToMain,sizeof(mainStruct),0);
int ret1=mq_send(lightToLogger,(char*)&dataTOSendTOLogger,sizeof(dataTOSendTOLogger),0);
if(ret1==-1) perror("sending data to log queue failed");
if(sendLightToSocket)
{
sendLightToSocket=false;
dataToSendToSocket.source="light";
dataToSendToSocket.value=lux;
if(mq_send(lightToSocket,(char*)&dataToSendToSocket,sizeof(socketStruct),0)<0)
{
perror("sending data to socket failed\n");
}
}
y++;
}
if(exitThread)
{
break;
}
}
printf("_____________________Light task exiting_______________________\n");
close(fd);
mq_close(lightToMain);
mq_close(lightToLogger);
mq_close(lightQueue);
mq_close(lightToSocket);
mq_unlink(MAINQUEUENAME);
mq_unlink(LIGHTQUEUENAME);
mq_unlink(SOCKETQUEUENAME);
mq_unlink(LOGQUEUENAME);
timer_delete(lightTimer);
pthread_exit(NULL);
}
/*********************************************************************************
* Function name:- socketRoutine *
* Description:- This function is used by a socket sensor thread for execution of *
* socket sensor task. *
* @param:- void* (data from main) *
* @return:- static void* (pthread exit value) *
*********************************************************************************/
static void* socketRoutine(void *dataObj)
{
printf("Entered socketRoutine\n");
int serverSocket,clientConnected,len;
struct sockaddr_in clientAddr,serverAddr;
struct hostent *ptr;
int n=0;
bool receive = false;
mainStruct dataToSendToMain;
logStruct dataTOSendTOLogger;
socketStruct dataReceivedFromSensors;
dataToSendToMain.status=init_success;
strcpy(dataToSendToMain.source,"socket");
dataToSendToMain.messageType = update;
dataTOSendTOLogger.source="socket";
dataTOSendTOLogger.logLevel=alert;
dataTOSendTOLogger.status=fail;
mqd_t socketToMain = mqueue_init(MAINQUEUENAME,MAIN_QUEUE_SIZE,sizeof(mainStruct));
mqd_t socketToLogger = mqueue_init(LOGQUEUENAME,LOG_QUEUE_SIZE,sizeof(logStruct));
struct mq_attr queue_attr1;
queue_attr1.mq_maxmsg = SOCKET_QUEUE_SIZE;
queue_attr1.mq_msgsize = sizeof(socketStruct);
mqd_t sensorToSocket = mq_open(SOCKETQUEUENAME, O_CREAT | O_RDWR, 0666, &queue_attr1);
if(socketToMain < 0)
{
perror("socketTomain queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="socketTomain queue creation failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
if(socketToLogger<0)
{
perror("socketToLogger queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="socketToLogger queue creation failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
if(sensorToSocket<0)
{
perror("sensorToSocket queue creation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="sensorToSocket queue creation failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
/*Initialising Timer*/
struct sigevent socketEvent;
struct sigaction socketAction;
struct itimerspec socketTimerSpec;
timer_t socketTimer;
socketAction.sa_flags = SA_SIGINFO;
socketAction.sa_sigaction = signal_handler;
if(sigaction(SIGRTMIN + 7 , &socketAction, NULL)<0)
{
perror("Socket, timer handler initialisation failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Socket, timer handler initialisation failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
socketEvent.sigev_notify = SIGEV_SIGNAL;
socketEvent.sigev_signo = SIGRTMIN + 7;
socketEvent.sigev_value.sival_ptr = &socketTimer;
/* Creating timer */
if((timer_create(CLOCK_REALTIME, &socketEvent, &socketTimer)) < 0)
{
perror("Timer creation failed for sensor task");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Timer creation failed for sensor task";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
socketTimerSpec.it_interval.tv_nsec = 500000000; //To get the heart beat value after every 1 s
socketTimerSpec.it_interval.tv_sec = 0;//1;
socketTimerSpec.it_value.tv_nsec = 500000000;
socketTimerSpec.it_value.tv_sec = 0;//1;
/* Starting timer */
if(timer_settime(socketTimer,0,&socketTimerSpec,NULL)<0)
{
perror("Socket task timer settime failed");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Socket task timer settime failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
//exit(1);
}
/*Setting up server socket*/
serverSocket=socket(AF_INET, SOCK_STREAM, 0);
memset((char*)&serverAddr,0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(10000);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(serverSocket,(struct sockaddr*)&serverAddr,sizeof(serverAddr)) == -1)
{
printf("Bind Failure\n");
dataToSendToMain.status=init_failure;
dataTOSendTOLogger.message="Bind Failure";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
else
{
printf("Bind Success:<%u>\n", serverSocket);
dataTOSendTOLogger.status=success;
dataTOSendTOLogger.message="Bind Success";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
/*Sending init success/fail message to main*/
mq_send(socketToMain,(char*)&dataToSendToMain,sizeof(mainStruct),0);
listen(serverSocket,5);
len=sizeof(struct sockaddr_in);
printf("----------> Calling accept system call.\n");
clientConnected=accept(serverSocket,(struct sockaddr*)&clientAddr,&len);
if (clientConnected !=-1)
{
printf("Connection accepted:<%u>\n", clientConnected);
dataTOSendTOLogger.status=success;
dataTOSendTOLogger.message="Connection accepted";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
int input;
const int errval=-500;
while(1)
{
if(receive)
{
receive = false;
int ret = mq_receive(sensorToSocket,(char*)&dataReceivedFromSensors,sizeof(socketStruct),0);
if(ret > -1)
{
printf("Data received by server queue from %s\n",dataReceivedFromSensors.source);
if(dataReceivedFromSensors.source=="temperature")
{
printf("Temperature readings received from Temp task to socket server\n");
if(send(clientConnected, (void*)&dataReceivedFromSensors.value,sizeof(dataReceivedFromSensors.value),0)!=sizeof(dataReceivedFromSensors.value))
{
perror("Sending temp value to client failed\n");
dataTOSendTOLogger.status=fail;
dataTOSendTOLogger.message="Sending temp value to client failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
}
else if(dataReceivedFromSensors.source=="light")
{
printf("Light request received in server. Sending data to client\n");
if(send(clientConnected, (void*)&dataReceivedFromSensors.value,sizeof(dataReceivedFromSensors.value),0)!=sizeof(dataReceivedFromSensors.value))
{
perror("Sending light value to client failed\n");
dataTOSendTOLogger.status=fail;
dataTOSendTOLogger.message="Sending light value to client failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
}
else
{
strcpy(dataTOSendTOLogger.message,dataReceivedFromSensors.message);
strcpy(dataTOSendTOLogger.source,"socket");
dataTOSendTOLogger.status=fail;
dataTOSendTOLogger.logLevel=alert;
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
if(send(clientConnected, (void*)&errval,sizeof(errval),0)!=sizeof(errval))
{
perror("Sending error value to client failed\n");
dataTOSendTOLogger.status=fail;
dataTOSendTOLogger.message="Sending error value to client failed";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
}
}
}
}
int rb=read(clientConnected,&input, sizeof(input));
if(rb==sizeof(input))
{
receive = true;
printf("Message received from client is %d\n",input);
strcpy(dataToSendToMain.source,"socket");
dataTOSendTOLogger.status=success;
dataTOSendTOLogger.logLevel=alert;
dataTOSendTOLogger.message="Request message received from client";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
dataToSendToMain.messageType= request;
switch(input)
{
case 1:
strcpy(dataToSendToMain.unit,"Celsius");
break;
case 2:
strcpy(dataToSendToMain.unit,"Kelvin");
break;
case 3:
strcpy(dataToSendToMain.unit,"Fahrenheit");
break;
case 4:
strcpy(dataToSendToMain.unit,"Light");
break;
}
mq_send(socketToMain,(char *) &dataToSendToMain,sizeof(mainStruct),0);
}
if(socketHeartBeat)
{
socketHeartBeat=false;
strcpy(dataToSendToMain.source,"socket");
dataToSendToMain.messageType = update;
dataToSendToMain.status = success;
mq_send(socketToMain,(char *) &dataToSendToMain,sizeof(mainStruct),0);
}
if(exitThread)
{
break;
}
}
printf("_____________________Socket task exiting_______________________\n");
mq_close(socketToMain);
mq_close(socketToLogger);
mq_close(sensorToSocket);
mq_unlink(MAINQUEUENAME);
mq_unlink(LOGQUEUENAME);
mq_unlink(SOCKETQUEUENAME);
close(serverSocket);
dataTOSendTOLogger.logLevel=alert;
dataTOSendTOLogger.message="Server Socket Closed !!";
mq_send(socketToLogger,(char*)&dataTOSendTOLogger,sizeof(logStruct),0);
printf("\nServer Socket Closed !!\n");
pthread_exit(NULL);
}
/*********************************************************************************
* Function name:- loggerRoutine *
* Description:- This function is used by a logger thread. *
* @param:- void* (data from main) *
* @return:- static void* (pthread exit value) *
*********************************************************************************/
static void* loggerRoutine(void *dataObj)
{
mainStruct dataToSend;
logStruct dataToReceive;
mainInfoToOthers *obj1;
obj1=malloc(sizeof(mainInfoToOthers));
obj1=(mainInfoToOthers *)dataObj;
/*Initialising Timer*/
struct sigevent loggerEvent;
struct sigaction loggerAction;
struct itimerspec loggerTimerSpec;
timer_t loggerTimer;
loggerAction.sa_flags = SA_SIGINFO;
loggerAction.sa_sigaction = signal_handler;
if(sigaction(SIGRTMIN + 6 , &loggerAction, NULL)<0)
{
perror("Logger, timer handler initialisation failed");
}
loggerEvent.sigev_notify = SIGEV_SIGNAL;
loggerEvent.sigev_signo = SIGRTMIN + 6;
loggerEvent.sigev_value.sival_ptr = &loggerTimer;
if((timer_create(CLOCK_REALTIME, &loggerEvent, &loggerTimer)) < 0)
{
perror("Timer creation failed for logger task");
exit(1);
}
loggerTimerSpec.it_interval.tv_nsec = 0;
loggerTimerSpec.it_interval.tv_sec = 2;
loggerTimerSpec.it_value.tv_nsec = 0;
loggerTimerSpec.it_value.tv_sec = 2;
if(timer_settime(loggerTimer,0,&loggerTimerSpec,NULL)<0)
{
perror("logger timer settime failed");
exit(1);
}
strcpy(dataToSend.source,"logger");
dataToSend.messageType = update;
dataToSend.status = init_success;
mqd_t loggerToMain = mqueue_init(MAINQUEUENAME,MAIN_QUEUE_SIZE,sizeof(mainStruct));
if(loggerToMain < 0)
{
perror("logger queue creation failed");
}
mqd_t loggerQueue = mqueue_init(LOGQUEUENAME,LOG_QUEUE_SIZE,sizeof(logStruct));
if(loggerQueue < 0)
{
perror("logger queue creation failed");
}
mq_send(loggerToMain,(char*)&dataToSend,sizeof(mainStruct),0);
int y=0;
while(1)
{
if(loggerHeartBeat)
{
loggerHeartBeat=false;
dataToSend.messageType = update;
dataToSend.status = success;
mq_send(loggerToMain,(char*)&dataToSend,sizeof(mainStruct),0);
y++;
}
else
{
int ret= mq_receive(loggerQueue, (char*)&dataToReceive, sizeof(logStruct),0);
if(ret!=-1)
{
logToFile(obj1->logFileName, dataToReceive);
}
}
if(exitThread)
{
break;
}
}
printf("_____________________Logger task exiting_______________________\n");
mq_close(loggerToMain);
mq_close(loggerQueue);
mq_unlink(MAINQUEUENAME);
mq_unlink(LOGQUEUENAME);
timer_delete(loggerTimer);
pthread_exit(NULL);
}
uint8_t isAlive = 0x0F;
int count;
int main(int argc, char *argv[])
{
printf("Inside main task\n");
count=1;
mainStruct dataToReceive;
tempStruct requestForTemp;
socketStruct responseToSocket;
lightStruct requestForLight;
logStruct dataToLog;
mainInfoToOthers dataObj;
if(argc != 2)
{
printf("Try execution in below syntax\n");
printf("Execute in format -> ./maintask <log file name>\n");
return 0;
}
/* I2C initialisations */
fd=myi2cInit(slaveAddFloat);
fd1=temp_i2c_init(DEFAULT_SLAVE_ADDRESS);
printf("fd in maintask is %d\n",fd);
/* Initialize logger queue */
mqd_t logQueue = mqueue_init(LOGQUEUENAME, LOG_QUEUE_SIZE, sizeof(logStruct));
if(logQueue < 0)
{
perror("Main queue creation failed");
}
/*Built in self test calls*/
int lightBIST=lightSensorBIST(fd);
if(lightBIST==-1)
{
dataToLog.source = "main";
dataToLog.message = "Light sensor BIST failed";
mq_send(logQueue, (char*)&dataToLog, sizeof(dataToLog),0);
mq_close(logQueue);
mq_unlink(LOGQUEUENAME);
printf("Light sensor BIST failed\n");
return 0;
}
else
{
dataToLog.source = "main";
dataToLog.message = "Light sensor BIST passed";
mq_send(logQueue, (char*)&dataToLog, sizeof(dataToLog),0);
printf("Light sensor BIST passed\n");
}
int tempBIST=tempSensorBIST(fd1);
if(tempBIST==-1)
{
dataToLog.source = "main";
dataToLog.message = "Temp sensor BIST failed";
mq_send(logQueue, (char*)&dataToLog, sizeof(dataToLog),0);
mq_close(logQueue);
mq_unlink(LOGQUEUENAME);
printf("Temp sensor BIST failed\n");
return 0;
}
else
{
dataToLog.source = "main";
dataToLog.message = "Temp sensor BIST passed";
mq_send(logQueue, (char*)&dataToLog, sizeof(dataToLog),0);
printf("Temp sensor BIST passed\n");
}
dataObj.logFileName=malloc(20);
strcpy(dataObj.logFileName,argv[1]);
printf("Received file name is %s\n",dataObj.logFileName);
dataToLog.source = "main";
//Creating queues for Inter Process Communication
mqd_t mainQueue = mqueue_init(MAINQUEUENAME, MAIN_QUEUE_SIZE, sizeof(mainStruct));
if(mainQueue < 0)
{
dataToLog.message = "Main queue creation failed";
mq_send(logQueue, (char*)&dataToLog, sizeof(dataToLog),0);
perror("Main queue creation failed");
}
mqd_t tempQueue = mqueue_init(TEMPQUEUENAME, TEMP_QUEUE_SIZE, sizeof(tempStruct));
if(tempQueue < 0)
{