-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoteMonitor.c
More file actions
821 lines (794 loc) · 35.8 KB
/
remoteMonitor.c
File metadata and controls
821 lines (794 loc) · 35.8 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
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
//******************************************************************************
//******************************************************************************
//
// FILE: remoteMonitor.c
//
// CLASSES:
//
// DESCRIPTION: This is the file which contains the remote monitoring functionality.
// Remote Monitor is responsible for opening a TCP/IP socket connection
// to a remove server. Once connected, remote monitor will simply send
// the equipment data to the remote sight. The data sent to the remote
// site is the same data that is sent to the iPhone application.
//
//******************************************************************************
//
// CONFIDENTIALITY NOTICE:
//
// THIS FILE CONTAINS MATERIAL THAT IS "HARRIS PROPRIETARY INFORMATION" ANY
// REVIEW, RELIANCE, DISTRIBUTION, DISCLOSURE, OR FORWARDING WITHOUT EXPRESSED
// PERMISSION IS STRICTLY PROHIBITED. PLEASE BE SURE TO PROPERLY DISPOSE ANY
// HARDCOPIES OF THIS DOCUMENT.
//
//******************************************************************************
//
// Government Use Rights:
//
// (Applicable only for source code delivered under U. S.
// Government contracts)
//
// RESTRICTED RIGHTS LEGEND
// Use, duplication, or disclosure is subject to restrictions
// stated in the Government's contract with Harris Corporation,
// RF Communications Division. The applicable contract number is
// indicated on the media containing this software. As a minimum,
// the Government has restricted rights in the software as
// defined in DFARS 252.227-7013.
//
// Commercial Use Rights:
//
// (Applicable only for source code procured under contracts other
// than with the U. S. Government)
//
// TRADE SECRET
// Contains proprietary information of Harris Corporation.
//
// Copyright:
// Protected as an unpublished copyright work,
// (c) Harris Corporation
// First fixed in 2004, all rights reserved.
//
//******************************************************************************
//
// HISTORY: Created 11/08/2012 by Michael A. Carrier
// $Header:$
// $Revision: 1.3 $
// $Log:$
//
//******************************************************************************
//******************************************************************************
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <errno.h>
#include <netdb.h>
#include "reconn.h"
#include "debugMenu.h"
#include "clientApp.h"
#include "remoteMonitor.h"
#include "eqptResponse.h"
#include "socket.h"
static int remoteMonitorDone = FALSE;
int stopRemoteMonitorHb = FALSE;
static YESNO remoteMonitorActive = NO;
static int webServerSocket = -1;
static struct sockaddr_in serverAddress;
static void *remoteMonitorKeepAliveTask(void *);
static CLIENTCONTEXT *aContextPtr = NULL;
static REMOTE_MONITOR_STATES theState = RM_INIT;
//*********************************************************************************
//*********************************************************************************
// FUNCTION: sendRemoteMonitorConnectionStatus
//
// DESCRIPTION: This interface sends a message to the master client process
// informing it that the remote monitoring connection status has
// changed. The master client process will then format and message
// and send that message to its iPhone application all connected
// clients.
// Parameters:
// theState - the connection state
//
//*********************************************************************************
static void sendRemoteMonitorConnectionStatus(REMOTE_MONITOR_CONNECTION_STATES theState)
{
unsigned char theMessage[MASTER_MSG_SIZE];
memset(&theMessage, 0, MASTER_MSG_SIZE);
theMessage[0] = RM_STATE_CHANGE;
theMessage[1] = theState;
if(masterClientMsgQid != -1)
{
if(mq_send(masterClientMsgQid, (const char *)&theMessage, MASTER_MSG_SIZE, 0) != 0)
{
reconnDebugPrint("%s: mq_send(masterClientMsgQid) failed. %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
else
{
reconnDebugPrint("%s: mq_send(masterClientMsgQid) success.\n", __FUNCTION__);
}
}
else
{
reconnDebugPrint("%s: masterClientMsgQid == -1\n", __FUNCTION__);
}
}
//******************************************************************************
//******************************************************************************
// FUNCTION: remoteMonitorActivate
//
// DESCRIPTION: Interface used to activate/deactivate the remote monitor.
//
//******************************************************************************
ReconnErrCodes remoteMonitorActivate(YESNO activate, CLIENTCONTEXT *theContextPtr)
{
ReconnErrCodes retCode = RECONN_SUCCESS;
struct addrinfo hints, *webServiceInfo;
int errCode, i, length;
char *ptr;
YESNO IPaddressIsName = NO;
reconnDebugPrint("%s: Function Entered with activate == %s remoteMonitorActive == %s\n",
__FUNCTION__, (activate == YES) ? "YES" : "NO", (remoteMonitorActive == YES) ? "YES" : "NO");
if(activate == YES)
{
if(remoteMonitorActive == NO)
{
if((webServerSocket = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
retCode = RECONN_FAILURE;
reconnDebugPrint("%s: socket() failed %d (%s)\n", __FUNCTION__, errno, strerror(errno));
}
else
{
/*
* Check to see if the IP address is a hostname or IP address
*/
ptr = &(theContextPtr->theWebServerHostName[0]);
length = strlen(theContextPtr->theWebServerHostName);
for(i = 0; i < length; i++, ptr++)
{
/*
* If we find a letter then the IP is a hostname not an IP address
*/
if(isalpha(*ptr))
{
IPaddressIsName = YES;
break;
}
}
if(IPaddressIsName == YES)
{
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
if((errCode = getaddrinfo(&(theContextPtr->theWebServerHostName[0]), NULL, &hints, &webServiceInfo)) != 0)
{
reconnDebugPrint("%s: gethostbyname() failed %d\n", __FUNCTION__, errCode);
retCode = RECONN_HOSTNAME_UNRESOLVED;
}
}
if(retCode == RECONN_SUCCESS)
{
if(IPaddressIsName == YES)
{
serverAddress.sin_addr.s_addr = ((struct sockaddr_in *) (webServiceInfo->ai_addr))->sin_addr.s_addr;
}
else
{
serverAddress.sin_addr.s_addr = inet_addr(theContextPtr->theWebServerHostName);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(theContextPtr->portNum);
if(connect(webServerSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
{
if(errno == ECONNREFUSED)
{
retCode = RECONN_CONNECTION_REFUSED;
}
else if(errno == ENETUNREACH)
{
retCode = RECONN_NETWORK_UNREACHABLE;
}
else if(errno == ETIMEDOUT)
{
retCode = RECONN_CONNECTION_TIMEDOUT;
}
else
{
retCode = RECONN_FAILURE;
reconnDebugPrint("%s: connect() failed %d (%s)\n", __FUNCTION__, errno, strerror(errno));
}
}
else
{
/*
* Now create the thread which will watch for connections on the RJ-45.
*/
remoteMonitorDone = FALSE;
reconnDebugPrint("%s: creating remoteMonitorTask\n", __FUNCTION__);
if(pthread_create(&reconnThreadIds[RECONN_REMOTE_MONITOR_TASK], NULL, remoteMonitorTask, theContextPtr) < 0)
{
remoteMonitorActive = NO;
retCode = RECONN_FAILURE;
reconnDebugPrint("%s: Could not start remoteMonitorTask %d %s\n", __FUNCTION__, errno, strerror(errno));
}
else
{
remoteMonitorActive = YES;
}
}
}
}
}
}
else if(activate == NO)
{
if(remoteMonitorActive == YES)
{
sendRemoteMonitorConnectionStatus(DISCONNECTED);
remoteMonitorCleanup();
}
}
else
{
reconnDebugPrint("%s: Invalid value for activate %d\n", __FUNCTION__, activate);
retCode = RECONN_INVALID_PARAMETER;
}
return (retCode);
}
static YESNO remoteMonitorCleanupRunning = NO;
//******************************************************************************
//******************************************************************************
// FUNCTION: remoteMonitorCleanup
//
// DESCRIPTION: Function used to clean up the Remote Monitor resources.
//
//******************************************************************************
void remoteMonitorCleanup()
{
int i;
reconnDebugPrint("%s: Function entered, remoteMonitorCleanupRunning = %s\n", __FUNCTION__, (remoteMonitorCleanupRunning == YES) ? "YES": "NO");
if(remoteMonitorCleanupRunning == NO)
{
remoteMonitorCleanupRunning = YES;
pthread_mutex_lock(&clientListMutex);
for(i = 0; i < (RECONN_MAX_NUM_CLIENTS); i++)
{
if (activeClientsList[i])
{
if(activeClientsList[i]->mode == REMOTEMODE)
{
reconnDebugPrint("%s: DeRegistering client %u\n", __FUNCTION__, activeClientsList[i]->index);
reconnReturnClientIndex(activeClientsList[i]->index);
pthread_mutex_unlock(&clientListMutex);
reconnDeRegisterClientApp(activeClientsList[i]);
pthread_mutex_lock(&clientListMutex);
}
}
}
pthread_mutex_unlock(&clientListMutex);
if(webServerSocket != -1)
{
reconnDebugPrint("%s: Calling shutdown(%u, SHUT_RDWR)\n", __FUNCTION__, webServerSocket);
if(shutdown(webServerSocket, SHUT_RDWR))
{
reconnDebugPrint("%s: shutdown(%u, SHUT_RDWR) failed %d (%s)\n", __FUNCTION__, webServerSocket,
errno, strerror(errno));
}
reconnDebugPrint("%s: Calling close(%u)\n", __FUNCTION__, webServerSocket);
if(close(webServerSocket))
{
reconnDebugPrint("%s: close(%u) failed %d (%s)\n", __FUNCTION__, webServerSocket, errno,
strerror(errno));
}
webServerSocket = -1;
}
remoteMonitorDone = TRUE;
remoteMonitorActive = NO;
remoteMonitorCleanupRunning = NO;
}
reconnDebugPrint("%s: Function exiting\n", __FUNCTION__);
}
//******************************************************************************
//******************************************************************************
// FUNCTION: remoteMonitorTask
//
// DESCRIPTION: This is the task responsible for processing all data coming from
// the web service.
// Upon rececption of the data begin opcode this task will register
// itself with the reconn embedded software so that all equipment
// data responses will be sent to the remote web service.
//******************************************************************************
void *remoteMonitorTask(void *args)
{
static int processState = 0;
int bytesRead;
//int dataLength;
fd_set theFileDescriptor;
struct timeval waitTime;
short theMsgId;
CLIENTCONTEXT *theMasterContextPtr = (CLIENTCONTEXT *)args; //pointer to the master client context
union
{
char data[strlen(RECONNBEGIN)+1];
ReconnPacket thePacket;
ReconnResponsePacket theResponsePacket;
}theRecvBuf;
#if 0
ReconnPacket *thePacketPtr = &theRecvBuf.thePacket;
#endif
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: **** Task started\n", __FUNCTION__);
}
if((aContextPtr = (CLIENTCONTEXT *)malloc(sizeof(CLIENTCONTEXT))) != 0)
{
memset(aContextPtr, 0, sizeof(CLIENTCONTEXT));
aContextPtr->thisContext = (int *)aContextPtr;
aContextPtr->socketFd = webServerSocket;
aContextPtr->eqptDescriptors = &gEqptDescriptors;
aContextPtr->mode = REMOTEMODE;
aContextPtr->index = -1;
aContextPtr->tmpFd = (FILE *)-1;
aContextPtr->sourceIp = serverAddress;
aContextPtr->theResponsePktPtr = &aContextPtr->theResponsePkt;
/*
* Create the task responsible for sending keep alive messages to the remote web service.
*/
if(pthread_create(&reconnThreadIds[RECONN_KEEPALIVE_TASK], NULL, remoteMonitorKeepAliveTask, NULL) < 0)
{
reconnDebugPrint("%: Could not start remoteMonitorKeepAliveTask(), %d (%s)\n",
__FUNCTION__, errno , strerror(errno));
remoteMonitorActivate(NO, theMasterContextPtr);
}
else
{
waitTime.tv_sec = 30;
waitTime.tv_usec = 0;
while (remoteMonitorDone == FALSE)
{
FD_ZERO(&theFileDescriptor);
FD_SET(webServerSocket, &theFileDescriptor);
if((aContextPtr->retStatus = select((webServerSocket + 1),
&theFileDescriptor, NULL, NULL, &waitTime)) < 0)
{
reconnDebugPrint("%s: select() failed %d (%s)\n", __FUNCTION__,
errno, strerror(errno));
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
switch(theState)
{
case RM_INIT:
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: RM_INIT\n", __FUNCTION__);
}
if(aContextPtr->retStatus == 0)
{
/*
* The handshake reception timed out. Close the socket and terminate
* Remote Monitor.
*/
reconnDebugPrint("%s: select() timed out\n", __FUNCTION__);
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
/*
* Get the data and fall through to the next state.
*/
memset(&theRecvBuf, 0, sizeof(theRecvBuf));
if((bytesRead = recv(webServerSocket, &theRecvBuf, sizeof(theRecvBuf), 0)) <= 0)
{
reconnDebugPrint("%s: bytesRead = %d recvfrom(%d) returned %d (%s)\n", __FUNCTION__, bytesRead, webServerSocket+1,
errno, strerror(errno));
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
theState = RM_GET_HANDSHAKE;
}
case RM_GET_HANDSHAKE:
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: RM_GET_HANDSHAKE\n", __FUNCTION__);
}
if(strcmp(&(theRecvBuf.data[0]), RECONNBEGIN) == 0)
{
//theState = RM_SEND_CLIENT_ACCESS;
theState = RM_DEVICE_ID;
/*
* Fall through to the next state
*/
}
else
{
sendReconnResponse(webServerSocket,
theRecvBuf.thePacket.messageId.Byte[0],
theRecvBuf.thePacket.messageId.Byte[1],
RECONN_FAILURE, aContextPtr->mode);
reconnDebugPrint("%s: Invalid Handshake, killing this task. \n", __FUNCTION__);
remoteMonitorActivate(NO, aContextPtr);
break;
}
}
#if 0 // Currently not used. Original design was to have the reconn send out client access request and expect a response
case RM_SEND_CLIENT_ACCESS:
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: RM_SEND_CLIENT_ACCESS\n", __FUNCTION__);
}
memset(thePacketPtr, 0, sizeof(ReconnPacket));
ADD_MSGID_TO_PACKET(CLIENT_ACCESS_REQ, thePacketPtr);
dataLength = 0;
ADD_DATA_LENGTH_TO_PACKET(dataLength, thePacketPtr);
/*
* Send CLIENT ACCESS REQuest, break out of this state and wait for the
* response.
*/
if(sendSocket(webServerSocket, thePacketPtr, (RECONN_PACKET_HEADER_SIZE + dataLength), 0) < 0)
{
reconnDebugPrint("%s: sendSocket(webServerSocket) failed %d (%s)\n", __FUNCTION__, errno,
strerror(errno));
remoteMonitorActivate(NO, theMasterContextPtr);
}
else
{
FD_ZERO(&theFileDescriptor);
FD_SET(webServerSocket, &theFileDescriptor);
#ifndef __SIMULATION__
waitTime.tv_sec = 30;
#else
waitTime.tv_sec = 5;
#endif
waitTime.tv_usec = 0;
theState = RM_WAIT_RESP;
}
break;
}
case RM_WAIT_RESP:
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: RM_WAIT_RESP\n", __FUNCTION__);
}
if(aContextPtr->retStatus == 0)
{
/*
* The CLIENT ACCESS REQuest response timed out. Close the socket and terminate
* Remote Monitor.
*/
reconnDebugPrint("%s: select() timed out\n", __FUNCTION__);
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
/*
* Get the response
*/
memset(&theRecvBuf, 0, sizeof(theRecvBuf));
if(recv(webServerSocket, &theRecvBuf, sizeof(theRecvBuf), 0) <= 0)
{
reconnDebugPrint("%s: recv(%d) failed %d (%s)\n", __FUNCTION__, webServerSocket,
errno, strerror(errno));
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
GET_RSPID_FROM_PACKET(theMsgId, theRecvBuf.theResponsePacket);
if((theMsgId == CLIENT_ACCESS_REQ) &&
(theRecvBuf.theResponsePacket.dataPayload[0] == RECONN_SUCCESS))
{
/*
* Set the state, break out of this case and wait for web service
* to send the next request.
*/
FD_ZERO(&theFileDescriptor);
FD_SET(webServerSocket, &theFileDescriptor);
#ifndef __SIMULATION__
waitTime.tv_sec = 30;
#else
waitTime.tv_sec = 5;
#endif
waitTime.tv_usec = 0;
theState = RM_DEVICE_ID;
}
else
{
reconnDebugPrint("%s: msgId (%u) or payload (%u) invalid, killing task\n",
__FUNCTION__, theMsgId, theRecvBuf.theResponsePacket.dataPayload[0]);
remoteMonitorActivate(NO, theMasterContextPtr);
}
break;
}
#endif
case RM_DEVICE_ID:
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: RM_DEVICE_ID\n", __FUNCTION__);
}
#if 0
if(aContextPtr->retStatus == 0)
{
/*
* The CLIENT ACCESS REQuest response timed out. Close the socket and terminate
* Remote Monitor.
*/
reconnDebugPrint("%s: select() timed out\n", __FUNCTION__);
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
/*
* Get the response
*/
memset(&theRecvBuf, 0, sizeof(theRecvBuf));
if(recv(webServerSocket, &theRecvBuf, sizeof(theRecvBuf), 0) <= 0)
{
reconnDebugPrint("%s: recv(%d) failed %d (%s)\n", __FUNCTION__, webServerSocket,
errno, strerror(errno));
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
GET_MSGID_FROM_PACKET(theMsgId, theRecvBuf.thePacket);
if(theMsgId == RECONN_ID_REQ)
{
#endif
/*
* Get the Serial Number
*/
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: calling getDeviceId()\n", __FUNCTION__);
}
/*
* Get the serial and format the return packet
*/
if(getDeviceId(aContextPtr) == RECONN_SUCCESS)
{
sendSocket(webServerSocket, (unsigned char *)aContextPtr->theResponsePktPtr,
(RECONN_RSPPACKET_HEADER_SIZE + aContextPtr->serialBytesRead), 0);
}
else
{
sendReconnResponse(webServerSocket,
theRecvBuf.thePacket.messageId.Byte[0],
theRecvBuf.thePacket.messageId.Byte[1],
RECONN_FAILURE, aContextPtr->mode);
remoteMonitorActivate(NO, aContextPtr);
break;
}
/*
* Tell all clients we are connected.
*/
sendRemoteMonitorConnectionStatus(CONNECTED);
theState = RM_RCV_DATA;
#if 0
}
else
{
sendReconnResponse(webServerSocket,
theRecvBuf.thePacket.messageId.Byte[0],
theRecvBuf.thePacket.messageId.Byte[1],
RECONN_INVALID_PARAMETER, aContextPtr->mode);
}
#endif
break;
}
case RM_RCV_DATA:
{
if((bytesRead = recv(webServerSocket, &theRecvBuf, sizeof(theRecvBuf), 0)) <= 0)
{
if(bytesRead == -1)
{
reconnDebugPrint("%s: recv(%d) failed %d (%s) bytesRead = %d \n",
__FUNCTION__, webServerSocket, errno, strerror(errno), bytesRead);
}
remoteMonitorActivate(NO, theMasterContextPtr);
break;
}
GET_MSGID_FROM_PACKET(theMsgId, theRecvBuf.thePacket);
if(theMsgId == WEB_SERVICE_DATA_FLOW)
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: Received WEB_SERVICE_DATA_FLOW -> ", __FUNCTION__);
}
if(theRecvBuf.thePacket.dataPayload[0] == RM_SEND_DATA)
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint(" RM_SEND_DATA\n");
}
/*
* Register the remote client. This will start data
* transmission to the remote web service.
*/
if(reconnClientsRegistered(REMOTEMODE) == 0)
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint(" Calling reconnGetFreeClientIndex()\n");
}
if(reconnGetFreeClientIndex(&(aContextPtr->index)) == RECONN_SUCCESS)
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint(" Calling reconnRegisterClientApp()\n");
}
if(reconnRegisterClientApp(aContextPtr) != RECONN_SUCCESS)
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: reconnRegisterClientApp() index %d failed\n",
__FUNCTION__, aContextPtr->index);
}
remoteMonitorActivate(NO, theMasterContextPtr);
}
}
else
{
reconnDebugPrint("%s: reconnGetFreeClientIndex(%u) failed\n",
__FUNCTION__, aContextPtr->index);
remoteMonitorActivate(NO, theMasterContextPtr);
}
}
else
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: received WEB_SERVICE_DATA_FLOW when WEB_SERVICE_DATA_FLOW is already active.\n", __FUNCTION__);
}
}
}
else if(theRecvBuf.thePacket.dataPayload[0] == RM_STOP_DATA)
{
reconnDebugPrint(" RM_STOP_DATA\n");
/*
* De-register the remote client. This will stop data
* transmission to the remote web service.
*/
reconnDeRegisterClientApp(aContextPtr);
}
else
{
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: Invalid payload request %d\n", __FUNCTION__,
theRecvBuf.thePacket.dataPayload[0]);
}
sendReconnResponse(theMasterContextPtr->socketFd,
theRecvBuf.thePacket.messageId.Byte[0],
theRecvBuf.thePacket.messageId.Byte[1],
RECONN_INVALID_PARAMETER, theMasterContextPtr->mode);
}
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;
}
else if(theMsgId == CLIENT_RESIGN_REQ)
{
remoteMonitorActivate(NO, theMasterContextPtr);
}
else
{
reconnDebugPrint("%s: Invalid msgId %d\n", __FUNCTION__,
theRecvBuf.thePacket.dataPayload[0]);
sendReconnResponse(theMasterContextPtr->socketFd,
theRecvBuf.thePacket.messageId.Byte[0],
theRecvBuf.thePacket.messageId.Byte[1],
RECONN_INVALID_PARAMETER, theMasterContextPtr->mode);
}
break;
}
default:
{
reconnDebugPrint("%s: Invalid state %d\n", __FUNCTION__, theState);
break;
}
} // switch(theState)
} // while (remoteMonitorDone == FALSE)
} // if(pthread_create
}
else
{
reconnDebugPrint("%s: malloc(%u) failed %d (%s)\n", __FUNCTION__, sizeof(CLIENTCONTEXT));
remoteMonitorActivate(NO, theMasterContextPtr);
}
if(aContextPtr)
{
free(aContextPtr);
}
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: **** Task exiting\n", __FUNCTION__);
}
return(&processState);
}
//******************************************************************************
//******************************************************************************
// FUNCTION: getRemoteMonitorState
//
// DESCRIPTION: Interface used to return Remote Monitor's state
//
//******************************************************************************
YESNO getRemoteMonitorState()
{
return remoteMonitorActive;
}
//******************************************************************************
//******************************************************************************
// FUNCTION: remoteMonitorKeepAliveTask
//
// DESCRIPTION: This task is started by remoteMonitorTask and will simply send
// a keepalive message, every 15 seconds, to the remote web service.
// If it can't send due to a socket error it will call
// remoteMonitorActivate(no,) to disconnect Remote Monitor.
//
//******************************************************************************
static void *remoteMonitorKeepAliveTask(void *args)
{
ReconnPacket theKeepAlivePacket;
ReconnPacket *thePacketPtr = &theKeepAlivePacket;
static int theStatus = 1;
UNUSED_PARAM(args);
memset(&theKeepAlivePacket, 0, sizeof(ReconnPacket));
//ADD_MSGID_TO_PACKET(KEEPALIVE_MESSAGE, thePacketPtr);
/*
* TODO this is temporary, use the above call for the final product
*/
ADD_MSGID_TO_PACKET(0xFEFF, thePacketPtr);
ADD_DATA_LENGTH_TO_PACKET(0, thePacketPtr);
while((remoteMonitorDone == FALSE) && (stopRemoteMonitorHb == FALSE))
{
sleep(REMOTE_MONITOR_KEEPALIVE_TIME);
if(RECONN_DEBUG_ON(RM_DEBUG))
{
reconnDebugPrint("%s: Sending keepalive to web service\n", __FUNCTION__);
}
errno = 0;
//sendSocket(webServerSocket, (unsigned char *)&theKeepAlivePacket, RECONN_PACKET_HEADER_SIZE, 0);
/*
* TODO this is temporary, use the above call for the final product
*/
sendSocket(webServerSocket, (unsigned char *)&theKeepAlivePacket, 2, 0);
if(errno)
{
reconnDebugPrint("%s:sendSocket failed %d (%s)\n", __FUNCTION__, errno, strerror(errno));
remoteMonitorActivate(NO, NULL);
}
}
reconnDebugPrint("%s: **** Task ending\n", __FUNCTION__);
return(&theStatus);
}
//******************************************************************************
//******************************************************************************
// FUNCTION: remoteMonitorGetDebugValues
//
// DESCRIPTION: This interface is used by debug menus to return variables that
// are static to this file.
//
// Parameters:
// theDebugPtr - a pointer to a data structure into which this
// interface will place that data.
//
//******************************************************************************
void remoteMonitorGetDebugValues(REMOTE_MONITOR_DEBUG_DATA *theDebugPtr)
{
theDebugPtr->webServerSocket = webServerSocket;
//memset(&(theDebugPtr->IPAddress[0]), 0, IPADDR_LEN);
//strncpy(&(theDebugPtr->IPAddress[0]), &webServerIpAddress, IPADDR_LEN);
//theDebugPtr->port = webServerSocket;
theDebugPtr->remoteMonitorDone = remoteMonitorDone;
theDebugPtr->remoteMonitorActive = remoteMonitorActive;
theDebugPtr->theContextPtr = aContextPtr;
theDebugPtr->theState = theState;
}