-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientApp.c
More file actions
executable file
·2256 lines (2170 loc) · 118 KB
/
clientApp.c
File metadata and controls
executable file
·2256 lines (2170 loc) · 118 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
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: clientApp.c
//
// CLASSES:
//
// DESCRIPTION: This is the file which contains the main function for the
// client processing.
//
//******************************************************************************
//
// 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 <MM>/<DD>/<YYYY> by <USER>
// $Header:$
// $Revision: 1.3 $
// $Log:$
//
//******************************************************************************
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <mqueue.h>
#include "reconn.h"
#include "clientApp.h"
#include "socket.h"
#include "spectrum.h"
#include "gps.h"
#include "powerMeter.h"
#include "dmm.h"
#include "powerMgmt.h"
#include "eqptResponse.h"
#include "gpio.h"
#include "version.h"
#include "upgrade.h"
#include "debugMenu.h"
#include "wifi.h"
#include "remoteMonitor.h"
#include "fuel_gauge.h"
#include "libiphoned.h"
#include "aov_protocol.h"
#include "aov_errno.h"
struct timeval startTime;
struct timeval stopTime;
struct timeval result;
struct timeval deviceStartTime;
struct timeval deviceStopTime;
struct timeval deviceResult;
struct timeval queueStartTime;
struct timeval queueStopTime;
struct timeval queueResult;
// only used by a master client process
mqd_t masterClientMsgQid = -1;
static char masterClientMsgBuf[4];
static struct mq_attr masterClientMsgQAttr;
static pthread_mutex_t clientMutex = PTHREAD_MUTEX_INITIALIZER;
//
extern int libiphoned_tx(unsigned char *, unsigned int);
extern sem_t insertedMasterSemaphore;
extern ReconnEqptDescriptors gEqptDescriptors;
int masterClientSocketFd = -1;
int insertedMasterSocketFd = -1;
int MasterTransmitListenFd = -1;
/*
* This pointer is only used during the process of switching the current master
* client to a slave and the correspoding slave to a master.
*/
static CLIENTCONTEXT * theSlaveContextPtr = NULL;
YESNO swUpgradeInProgress = NO;
//******************************************************************************
//******************************************************************************
// FUNCTION: receive_packet_data
//
// CLASSES:
//
// DESCRIPTION: Interface used, by functions within this file, to receive data from
// from the socket. The data is coming from a wifi client.
//
// PARAMETERS:
// socket - the socket file descriptor
// buffer - Pointer a buffer into which this interface will place the data
// length - The data's length
//
//******************************************************************************
static int receive_packet_data(int socket, unsigned char *buffer, int *length)
{
int count;
int loop;
int len = 0;
unsigned char *ptr;
#ifdef COMM_DEBUG
int i;
#endif
/*
* The packet length must be at least RECONN_PACKET_HEADER_SIZE bytes
* which includes the message ID (2 bytes) and the data length (2 bytes)
*/
if((len = recv(socket, buffer, RECONN_PACKET_HEADER_SIZE, 0)) == 0)
{
*length = RECONN_CLIENT_SOCKET_CLOSED;
return(-1);
}
else if((len == -1) && ((errno == EAGAIN) || ((errno == EWOULDBLOCK))))
{
*length = RECONN_EAGAINOREWOULDBLOCK;
return(-1);
}
else if(len == -1)
{
*length = -1;
return(-1);
}
else if (len != RECONN_PACKET_HEADER_SIZE)
{
reconnDebugPrint("%s: recv is less than %d len = %d\n", __FUNCTION__, RECONN_PACKET_HEADER_SIZE, len);
*length = -1;
return (-1);
}
else
{
#ifdef COMM_DEBUG
reconnDebugPrint("%s %d: ", __FUNCTION__, __LINE__);
ptr = buffer;
for (i = 0; i < RECONN_PACKET_HEADER_SIZE; ++i, ptr++)
{
reconnDebugPrint("%x ", (unsigned int) *ptr);
}
#endif
count = buffer[2] << 8;
count = count + buffer[3];
if(count > RECONN_PAYLOAD_SIZE)
{
reconnDebugPrint("%s: received payload of %u is larger than %u\n", __FUNCTION__, count, RECONN_PAYLOAD_SIZE);
}
ptr = (unsigned char *)&buffer[RECONN_PACKET_HEADER_SIZE];
for (loop = 0; loop < count; loop++, ptr++)
{
recv(socket, ptr, 1, 0);
#ifdef COMM_DEBUG
reconnDebugPrint("%x ", *ptr);
#endif
}
#ifdef COMM_DEBUG
reconnDebugPrint("\n");
#endif
*length = count + RECONN_PACKET_HEADER_SIZE;
return (count + RECONN_PACKET_HEADER_SIZE);
}
}
//******************************************************************************
//******************************************************************************
// FUNCTION: reconnClientTask
//
// CLASSES:
//
// DESCRIPTION: This file is the main work horse of the reconn embedded software.
// Any reconn iPhone application (client) the connects to the toolkit,
// whether the connection is via WiFi or the 30 pin front panel connector,
// will have its own reconnClientTask() thread. The differences are
//
// 1. Slave clients have a limited number of executable opcodes that
// the thread will process.
// 2. Master clients process all opcodes and create a message queue used
// to receives messages about the front panel connection state.
// 3. Inserted master clients process all opcodes and do NOT create a
// message queue.
// 4. All client types except the inserted master use sockets with which
// to communicate with the iPhone application. The inserted master uses
// libiphoned() library calls.
//
//******************************************************************************
void *reconnClientTask(void *args)
{
CLIENTCONTEXT *contextPtr = (CLIENTCONTEXT *)args;
contextPtr->retStatus = 1;
contextPtr->theResponsePktPtr = &(contextPtr->theResponsePkt);
contextPtr->retCode = RECONN_SUCCESS;
contextPtr->connectionOpen = TRUE;
reconnDebugPrint("%s: Sending %s to client %d\n", __FUNCTION__, RECONNBEGIN, contextPtr->index);
pthread_mutex_lock(&clientMutex);
if(contextPtr->mode == INSERTEDMASTERMODE)
{
/*
* Give the MFI a chance to settle down before
* sending the handshake between this app and the iphone app.
*/
sleep(1);
reconnDebugPrint("%s: sending %s \n", __FUNCTION__, RECONNBEGIN);
// Send response out the 30 pin USB
libiphoned_tx((unsigned char *)RECONNBEGIN, strlen(RECONNBEGIN));
reconnDebugPrint("%s: registering client %d with eqptTask\n", __FUNCTION__, contextPtr->index);
if(reconnRegisterClientApp(contextPtr) != RECONN_SUCCESS)
{
reconnDebugPrint("%s: Inserted Master reconnRegisterClientApp() index %u failed\n", __FUNCTION__, contextPtr->index);
}
//
// An iPhone that is inserted into the front panel
}
else
{
sendSocket(contextPtr->socketFd, (unsigned char *)RECONNBEGIN, strlen(RECONNBEGIN), 0);
}
reconnDebugPrint("%s: reconnClientTask index %d\n", __FUNCTION__, contextPtr->index);
reconnDebugPrint("%s: mode == %s\n", __FUNCTION__, (contextPtr->mode == MASTERMODE) ? "Master": (contextPtr->mode == SLAVEMODE) ? "Slave" : (contextPtr->mode == REMOTEMODE) ? "Remote" : "Inserted Master");
reconnDebugPrint("%s: socketFd %d\n", __FUNCTION__, contextPtr->socketFd);
//reconnDebugPrint("\n%s: gpsFd %d\n", __FUNCTION__, contextPtr->eqptDescriptors->gpsFd);
reconnDebugPrint("%s: powerMeterFd %d\n", __FUNCTION__, contextPtr->eqptDescriptors->powerMeterFd);
//reconnDebugPrint("%s: lnbFd %d\n", __FUNCTION__, contextPtr->eqptDescriptors->lnbFd);
reconnDebugPrint("%s: dmmFd %d\n", __FUNCTION__, contextPtr->eqptDescriptors->dmmFd);
reconnDebugPrint("%s: analyzerFd %d\n", __FUNCTION__, contextPtr->eqptDescriptors->analyzerFd);
pthread_mutex_unlock(&clientMutex);
while (contextPtr->connectionOpen == TRUE)
{
contextPtr->responseNeeded = FALSE;
memset((unsigned char *) &(contextPtr->thePacket), 0, sizeof(ReconnPacket));
/* receive the command from the client */
#if 0
reconnDebugPrint("%s: client with index %d waiting for command\n", __FUNCTION__, contextPtr->index);
#endif
contextPtr->packetRetCode = receive_packet_data(contextPtr->socketFd, (unsigned char *)&(contextPtr->thePacket), &contextPtr->length);
if((contextPtr->packetRetCode == -1) && (contextPtr->length == RECONN_CLIENT_SOCKET_CLOSED))
{
reconnDebugPrint("%s: Socket closed by Client %d mode = %s\n", __FUNCTION__, contextPtr->index, (contextPtr->mode == MASTERMODE) ? "Master": (contextPtr->mode == SLAVEMODE) ? "Slave" : (contextPtr->mode == REMOTEMODE) ? "Remote" : "Inserted Master");
contextPtr->connectionOpen = FALSE;
reconnReturnClientIndex(contextPtr->index);
reconnDeRegisterClientApp(contextPtr);
if(contextPtr->mode == MASTERMODE)
{
if((masterClientMsgQid != -1) && (mq_close(masterClientMsgQid) == -1))
{
reconnDebugPrint("%s: mq_close() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
dmmSaveConfig();
masterClientMsgQid = -1;
masterClientSocketFd = -1;
}
contextPtr->connectionOpen = FALSE;
continue;
}
else if((contextPtr->packetRetCode == -1) && (contextPtr->length == -1))
{
reconnDebugPrint("%s: Error reading from socket\n", __FUNCTION__);
reconnDeRegisterClientApp(contextPtr);
reconnReturnClientIndex(contextPtr->index);
if(contextPtr->mode != SLAVEMODE)
{
masterClientSocketFd = -1;
}
contextPtr->connectionOpen = FALSE;
continue;
}
else
{
/*
* Something has returned from receive_packet_data(). If we are a master device then
* some data has returned OR there was a recv() timeout within receive_packet_data(). In either
* case, see if there is anything on the masterClientMsgQid informing this task of an iphone
* front panel insertion/extraction.
*
* The master device, whether Wifi or MFI inserted, should
* never stop sending data with one exception:
* when an iPhone is a WiFi master and is then inserted into the toolkit's front panel it will stop
* sending data for a short amount of time and receive_packet_data() will timeout. If the amount
* of time the master device (MFI or WIFI) is longer than 2 seconds something bad has happend so
* remove mastership and kill the thread. This prevents an unpredictable event from holding
* mastership forever.
*
* Note: Slave clients block inside receive_packet_data() and will never return
* RECONN_EAGAINOREWOULDBLOCK.
*/
if((masterClientMsgQid != -1) && (mq_getattr(masterClientMsgQid, &masterClientMsgQAttr) == 0))
{
if(masterClientMsgQAttr.mq_curmsgs)
{
if((contextPtr->numBytes = mq_receive(masterClientMsgQid, (char *)&masterClientMsgBuf, masterClientMsgQAttr.mq_msgsize, NULL)) == -1)
{
if(errno != EAGAIN)
{
reconnDebugPrint("%s: mq_receive failed %d (%s)\n", __FUNCTION__, errno, strerror(errno));
}
continue;
}
else
{
reconnDebugPrint("%s: Client %d message on mq_receive \n", __FUNCTION__, contextPtr->index);
if(masterClientMsgBuf[0] == MASTER_INSERTED)
{
// send message to the iPhone client telling it
// that mastership has been removed because an
// iPhone has been inserted into the toolkit's
// front panel.
reconnDebugPrint("%s: Client %d Received MASTER_INSERTED \n", __FUNCTION__, contextPtr->index);
sendReconnResponse (contextPtr->socketFd,
((MASTER_MODE_REMOVED_NOTIF & 0xff00) >> 8),
MASTER_MODE_REMOVED_NOTIF & 0x00ff, RECONN_ERROR_UNKNOWN, contextPtr->mode);
contextPtr->mode = SLAVEMODE;
masterClientSocketFd = -1;
contextPtr->flags = fcntl(contextPtr->socketFd, F_GETFL, NULL);
contextPtr->flags &= (~O_NONBLOCK);
fcntl(contextPtr->socketFd, F_SETFL, contextPtr->flags);
if(mq_close(masterClientMsgQid) == -1)
{
reconnDebugPrint("%s: mq_close() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
masterClientMsgQid = -1;
sem_post(&insertedMasterSemaphore);
usleep(CLIENTSLEEPTIME);
continue;
}
else if(masterClientMsgBuf[0] == MASTER_EXTRACTED)
{
reconnDebugPrint("%s: Client %d Received MASTER_EXTRACTED \n", __FUNCTION__, contextPtr->index);
reconnReturnClientIndex(contextPtr->index);
reconnDeRegisterClientApp(contextPtr);
close(insertedMasterSocketFd);
masterClientSocketFd = insertedMasterSocketFd = -1;
if(mq_close(masterClientMsgQid) == -1)
{
reconnDebugPrint("%s: mq_close() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
masterClientMsgQid = -1;
contextPtr->connectionOpen = FALSE;
continue;
}
else if(masterClientMsgBuf[0] == LOW_BATTERY)
{
reconnDebugPrint("%s: Client %d Received LOW_BATTERY indication from power management\n", __FUNCTION__, contextPtr->index);
memset(contextPtr->theResponsePktPtr, 0, sizeof(ReconnResponsePacket));
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(RECONN_POWER_DOWN_NOTIF, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(0, contextPtr->theResponsePktPtr);
if(contextPtr->mode == INSERTEDMASTERMODE)
{
// Send response out the 30 pin USB
libiphoned_tx((unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE);
}
reconnEqptAddMsgToQ((char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE);
continue;
}
else if(masterClientMsgBuf[0] == METER_INSERTED)
{
reconnDebugPrint("%s: Client %d Received METER_INSERTED indication\n", __FUNCTION__, contextPtr->index);
memset(contextPtr->theResponsePktPtr, 0, sizeof(ReconnResponsePacket));
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(PMETER_STATUS_NOTIF, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(1, contextPtr->theResponsePktPtr);
contextPtr->theResponsePktPtr->dataPayload[0] = INSERTED;
if(insertedMasterSocketFd != -1)
{
libiphoned_tx((unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1);
}
/*
* Tell all Slave devices
*/
reconnEqptAddMsgToQ((char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1);
continue;
}
else if(masterClientMsgBuf[0] == METER_EXTRACTED)
{
reconnDebugPrint("%s: Client %d Received METER_EXTRACTED indication from power management\n", __FUNCTION__, contextPtr->index);
memset(contextPtr->theResponsePktPtr, 0, sizeof(ReconnResponsePacket));
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(PMETER_STATUS_NOTIF, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(1, contextPtr->theResponsePktPtr);
contextPtr->theResponsePktPtr->dataPayload[0] = EXTRACTED;
if(insertedMasterSocketFd != -1)
{
libiphoned_tx((unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1);
}
/*
* Tell all Slave devices
*/
reconnEqptAddMsgToQ((char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1);
continue;
}
else if(masterClientMsgBuf[0] == RM_STATE_CHANGE)
{
reconnDebugPrint("%s: Client %d Received RM_STATE_CHANGE (%s) masterClientMsgBuf[1] == %d\n",
__FUNCTION__, contextPtr->index,
(masterClientMsgBuf[1] == CONNECTED) ? "Connected" :
(masterClientMsgBuf[1] == DISCONNECTED) ? "Disconnected": "unknown",
masterClientMsgBuf[1]);
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(WEB_SERVICE_STATE, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(1, contextPtr->theResponsePktPtr);
contextPtr->theResponsePktPtr->dataPayload[0] = masterClientMsgBuf[1];
if(insertedMasterSocketFd != -1)
{
libiphoned_tx((unsigned char *)&(contextPtr->theResponsePktPtr), RECONN_RSPPACKET_HEADER_SIZE + 1);
}
/*
* Tell all Slave devices
*/
reconnEqptAddMsgToQ((char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1);
continue;
}
else
{
reconnDebugPrint("%s: invalid msg type %d\n", __FUNCTION__, masterClientMsgBuf[0]);
continue;
}
}
}
else
{
if((contextPtr->packetRetCode == -1 ) && (contextPtr->length == RECONN_EAGAINOREWOULDBLOCK))
{
usleep(CLIENTSLEEPTIME);
/*
* This counter keeps track of the amount of time the master has not been transmitting
* data. If the "no data" time is too long then something bad has happened. Disconnect
* this master session.
*/
if(contextPtr->mode == MASTERMODE)
{
contextPtr->noDataCount += CLIENTSLEEPTIME;
#ifndef __SIMULATION__
if(contextPtr->noDataCount >= CLIENTNODATATIME)
{
reconnDebugPrint("%s: Client %d being disconnected to due inactivity timeout (%d microseconds) \n", __FUNCTION__, contextPtr->index, contextPtr->noDataCount );
contextPtr->connectionOpen = FALSE;
reconnReturnClientIndex(contextPtr->index);
reconnDeRegisterClientApp(contextPtr);
if((masterClientMsgQid != -1) && (mq_close(masterClientMsgQid) == -1))
{
reconnDebugPrint("%s: mq_close() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
dmmSaveConfig();
masterClientMsgQid = -1;
masterClientSocketFd = -1;
}
#endif
}
continue;
}
}
}
#if 0
else
{
if((contextPtr->packetRetCode == -1 ) && (contextPtr->length == RECONN_EAGAINOREWOULDBLOCK))
{
continue;
}
}
#endif
}
#ifdef DEBUG_CLIENT
reconnDebugPrint("%s %d: Packet received from client %d with length %d\n", __FUNCTION__, __LINE__, contextPtr->index, contextPtr->length);
debugPtr = (char *)&(contextPtr->thePacket);
for(debugIndex = 0; debugIndex < contextPtr->length + 4; debugIndex++)
{
reconnDebugPrint("0x%x ", debugPtr[debugIndex]);
}
reconnDebugPrint("\n");
#endif
/* everyone needs to know the packet length */
GET_DATA_LENGTH_FROM_PACKET(contextPtr->pktLength, contextPtr->thePacket);
//reconnDebugPrint("%s %d: pktLength = %d\n", __FUNCTION__, __LINE__, contextPtr->pktLength);
GET_MSGID_FROM_PACKET(contextPtr->cmdid, contextPtr->thePacket);
//reconnDebugPrint("%s %d: cmdid = 0x%x\n", __FUNCTION__, __LINE__, cmdid);
if((contextPtr->mode == SLAVEMODE) &&
((contextPtr->cmdid != KEEPALIVE_MESSAGE) &&
(contextPtr->cmdid != CLIENT_RESIGN_REQ) &&
(contextPtr->cmdid != CLIENT_ACCESS_REQ) &&
(contextPtr->cmdid != MASTER_MODE_RESIGN_REQ) &&
(contextPtr->cmdid != MASTER_MODE_REQ)))
{
// a command came in from the client that we do not like, so
// do nothing and the client will deal with the lack of
// response.
}
else if((contextPtr->mode == REMOTEMODE) &&
((contextPtr->cmdid != KEEPALIVE_MESSAGE) &&
(contextPtr->cmdid != CLIENT_RESIGN_REQ) &&
(contextPtr->cmdid != CLIENT_ACCESS_REQ) &&
(contextPtr->cmdid != MASTER_MODE_REQ)))
{
// a command came in from the remote client that we do not like so
// do nothing, and the client will deal with the lack of
// response.
}
else
{
contextPtr->noDataCount = 0;
switch (contextPtr->cmdid)
{
case KEEPALIVE_MESSAGE:
{
//reconnDebugPrint("%s: Client %d Received KEEPALIVE_MESSAGE\n", __FUNCTION__, contextPtr->index);
sendReconnResponse(contextPtr->socketFd,
((KEEPALIVE_MESSAGE & 0xff00) >> 8),
(KEEPALIVE_MESSAGE & 0x00ff),
RECONN_SUCCESS, contextPtr->mode);
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
case RECONN_CONNECTION_STATUS_REQ:
{
reconnDebugPrint("%s: Client %d Received RECONN_CONNECTION_STATUS_REQ\n", __FUNCTION__, contextPtr->index);
sendReconnResponse(contextPtr->socketFd,
((RECONN_CONNECTION_STATUS_REQ & 0xff00) >> 8),
(RECONN_CONNECTION_STATUS_REQ & 0x00ff),
RECONN_SUCCESS, contextPtr->mode);
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
case RECONN_ID_REQ:
{
reconnDebugPrint("%s: Client %d Received RECONN_ID_REQ\n", __FUNCTION__, contextPtr->index);
if(getDeviceId(contextPtr) == RECONN_SUCCESS)
{
/*
* Send the data to the master then to all connected clients
*/
if (contextPtr->mode == INSERTEDMASTERMODE)
{
libiphoned_tx((unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + contextPtr->serialBytesRead);
}
reconnEqptAddMsgToQ((char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + contextPtr->serialBytesRead);
}
else
{
reconnDebugPrint("%s: getDeviceId() failed.\n", __FUNCTION__);
sendReconnResponse(contextPtr->socketFd, contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1], RECONN_FAILURE, contextPtr->mode);
}
break;
}
case RECONN_INIT_STATE_SET_REQ:
{
char tmpSSID[WIFI_SSID_MAX_LENGTH+1];
reconnDebugPrint("%s: Client %d Received RECONN_INIT_STATE_SET_REQ %d\n", __FUNCTION__, contextPtr->index, contextPtr->thePacket.dataPayload[0]);
contextPtr->retCode = RECONN_SUCCESS;
if ((contextPtr->thePacket.dataPayload[0] == NON_DEFAULT) ||
(contextPtr->thePacket.dataPayload[0] == DEFAULT))
{
if(contextPtr->thePacket.dataPayload[0] == NON_DEFAULT)
{
unlink(RECONN_DEFAULT_FILE_NAME);
system("/sbin/siconfig usb0 down");
system("udhcpc --interface usb0 --host reconn --background --release");
system("iptables -X");
system("iptables -t nat -X");
system("iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE");
system("iptables -A FORWARD -i usb0 -j ACCEPT");
system("echo 1 > /proc/sys/net/ipv4/ip_forward");
}
else
{
if((contextPtr->tmpFd = fopen(RECONN_DEFAULT_FILE_NAME, "w")) == 0)
{
reconnDebugPrint("%s: fopen(%s,w) failed %d(%s)\n", __FUNCTION__,
RECONN_DEFAULT_FILE_NAME);
contextPtr->retCode = RECONN_FAILURE;
}
else
{
fclose(contextPtr->tmpFd);
contextPtr->tmpFd = (FILE *)-1;
if((contextPtr->retCode = wifiUpdateHostapdConfFile(WIFI_PASSWD_TOKEN, "coolfire1")) == RECONN_SUCCESS)
{
memset(&tmpSSID, 0, WIFI_SSID_MAX_LENGTH+1);
strcat((char *)&tmpSSID, "reconn");
if((contextPtr->tmpFd = fopen(RECONN_SERIALNUM_FILE_NAME, "r")) != 0)
{
if(fread(&(contextPtr->newSSID), 1, WIFI_SSID_MAX_LENGTH, contextPtr->tmpFd) <= 0)
{
/*
* inform the developer of the failure. At least "reconn"
* will be written to the config file.
*/
reconnDebugPrint("%s: fopen(%s, r) failed %d (%s)\n",
__FUNCTION__, RECONN_SERIALNUM_FILE_NAME, errno,
strerror(errno));
}
else
{
strcat((char *)&tmpSSID, (char *)&(contextPtr->newSSID[1]));
}
fclose(contextPtr->tmpFd);
contextPtr->tmpFd = (FILE *)-1;
}
else
{
/*
* inform the developer of the failure. At least "reconn" will be
* written to the config file.
*/
reconnDebugPrint("%s: fopen(%s, r) failed %d (%s)\n", __FUNCTION__, RECONN_SERIALNUM_FILE_NAME, errno, strerror(errno));
}
contextPtr->retCode = wifiUpdateHostapdConfFile(WIFI_SSID_TOKEN, (char *)&tmpSSID);
}
else
{
reconnDebugPrint("%s: wifiUpdateHostapdConfFile(WIFI_PASSWD_TOKEN, coolfire1) failed\n", __FUNCTION__);
contextPtr->retCode = RECONN_FAILURE;
}
}
}
}
else
{
reconnDebugPrint("%s: Client %d invalid state %d\n", __FUNCTION__, contextPtr->thePacket.dataPayload[0]);
contextPtr->retCode = RECONN_INVALID_MESSAGE;
}
sendReconnResponse (contextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1],
contextPtr->retCode, contextPtr->mode);
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
case CLIENT_RESIGN_REQ:
{
reconnDebugPrint("%s: Client %d Received CLIENT_RESIGN_REQ\n", __FUNCTION__, contextPtr->index);
pthread_mutex_lock(&clientMutex);
if(contextPtr->mode == INSERTEDMASTERMODE)
{
sendReconnResponse(contextPtr->socketFd, contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1], RECONN_SUCCESS, contextPtr->mode);
}
else
{
if(contextPtr->mode == MASTERMODE)
{
if((masterClientMsgQid != -1) && (mq_close(masterClientMsgQid) == -1))
{
reconnDebugPrint("%s: mq_close() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
dmmSaveConfig();
masterClientMsgQid = -1;
masterClientSocketFd = -1;
}
/* The client has requested to be disconnected */
sendReconnResponse(contextPtr->socketFd, contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1], RECONN_SUCCESS, contextPtr->mode);
//usleep(CLIENTSLEEPTIME);
reconnDeRegisterClientApp(contextPtr);
reconnReturnClientIndex(contextPtr->index);
contextPtr->connectionOpen = FALSE;
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
}
pthread_mutex_unlock(&clientMutex);
break;
}
case CLIENT_ACCESS_REQ:
{
reconnDebugPrint("%s: Client %d Received CLIENT_ACCESS_REQ\n", __FUNCTION__, contextPtr->index);
sendReconnResponse(contextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1],
RECONN_SUCCESS, contextPtr->mode);
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
case MASTER_MODE_REQ:
{
reconnDebugPrint("%s: Client %d Received MASTER_MODE_REQ\n", __FUNCTION__, contextPtr->index);
if (masterClientSocketFd == contextPtr->socketFd)
{
// This process is already the master client
sendReconnResponse(contextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1], RECONN_SUCCESS, contextPtr->mode);
reconnDebugPrint("%s %d: Client already is Master. Sending Success \n", __FUNCTION__, __LINE__);
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
else if (masterClientSocketFd == -1)
{
/*
* Open the queue which is used to communicate between
* reconnMasterIphone(), powerMeterPresenceTask() and the master client
* application. When an iphone is inserted into the toolkit's front panel a
* message will be sent here via masterClientMsgQid. The queue is then
* read to determine what needs to be done.
*/
if(mq_unlink(INSERTED_MASTER_MSG_Q_NAME) == -1)
{
reconnDebugPrint("%s: mq_unlink() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
}
masterClientMsgQAttr.mq_flags = 0;
masterClientMsgQAttr.mq_maxmsg = 5;
masterClientMsgQAttr.mq_msgsize = 10;
if((masterClientMsgQid =
mq_open(INSERTED_MASTER_MSG_Q_NAME,
(O_RDWR | O_CREAT |
O_NONBLOCK), 0, &masterClientMsgQAttr)) == (mqd_t) -1)
{
reconnDebugPrint("%s: mq_open() failed %d(%s)\n", __FUNCTION__, errno, strerror(errno));
contextPtr->connectionOpen = FALSE;
free((void *)contextPtr->thisContext);
return(&contextPtr->retStatus);
}
reconnDebugPrint("%s: masterClientMsgQid = %d\n", __FUNCTION__, masterClientMsgQid);
contextPtr->flags = fcntl(contextPtr->socketFd, F_GETFL, NULL);
/*
* Change the Master's socketFd from Blocking to non blocking. This allows
* the master task to check the masterClientMsgQid for front panel insertion
* events. It also allows this task to make sure the master device has not
* stopped communicating. If it does stop communicating for some length of time
* mastership is removed and the thread is killed.
*/
fcntl(contextPtr->socketFd, F_SETFL, contextPtr->flags |= O_NONBLOCK);
contextPtr->mode = (contextPtr->mode == SLAVEMODE) ? MASTERMODE : contextPtr->mode;
// This process becomes the master client
sendReconnResponse(contextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1],
RECONN_SUCCESS, contextPtr->mode);
/*
* If the box is defaulted then we have to send this OPCODE. The opcode
* causes the iphone application to display a setup wizard to the user.
*/
if(stat(RECONN_DEFAULT_FILE_NAME, &(contextPtr->fileStat)) == 0)
{
sendReconnResponse(contextPtr->socketFd,
((RECONN_INIT_STATE_REQ & 0xff00) >> 8),
(RECONN_INIT_STATE_REQ & 0x00ff),
RECONN_ERROR_UNKNOWN, contextPtr->mode);
}
else if(errno != ENOENT)
{
reconnDebugPrint("%s: fstat(%s) failed %d(%s)\n", __FUNCTION__,
RECONN_DEFAULT_FILE_NAME, errno, strerror(errno));
}
masterClientSocketFd = contextPtr->socketFd;
/*
* Check to see if the power meter is present. If it is tell the master.
*/
if(isPowerMeterPresent() == RECONN_SUCCESS)
{
reconnDebugPrint("%s: Sending insertion message for powerMeterFd %d\n", __FUNCTION__, gEqptDescriptors.powerMeterFd);
memset(contextPtr->theResponsePktPtr, 0, sizeof(ReconnResponsePacket));
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(PMETER_STATUS_NOTIF, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(1, contextPtr->theResponsePktPtr);
contextPtr->theResponsePktPtr->dataPayload[0] = INSERTED;
if(insertedMasterSocketFd != -1)
{
libiphoned_tx((unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1);
}
else
{
sendSocket(masterClientSocketFd, (unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1, 0);
}
}
reconnDebugPrint("%s %d: Client %d is now the Master. Sending Success \n", __FUNCTION__, __LINE__, contextPtr->index);
}
else
{
sendReconnResponse(contextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1],
RECONN_INVALID_MESSAGE, contextPtr->mode);
reconnDebugPrint("%s %d: Sending Failure because there is already a master (%d)\n", __FUNCTION__, __LINE__, masterClientSocketFd);
/*
* Notify the master that this slave client has attached.
*/
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(RECONN_SLAVE_ATTACHED, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(0, contextPtr->theResponsePktPtr);
if (insertedMasterSocketFd != -1)
{
libiphoned_tx((unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE);
}
else if(masterClientSocketFd != -1)
{
sendSocket(masterClientSocketFd, (unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE, 0);
}
/*
* Check to see if the power meter is present. If it is tell the slave device.
*/
if(isPowerMeterPresent() == RECONN_SUCCESS)
{
reconnDebugPrint("%s: Sending insertion message for powerMeterFd %d\n", __FUNCTION__, gEqptDescriptors.powerMeterFd);
memset(contextPtr->theResponsePktPtr, 0, sizeof(ReconnResponsePacket));
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(PMETER_STATUS_NOTIF, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(1, contextPtr->theResponsePktPtr);
contextPtr->theResponsePktPtr->dataPayload[0] = INSERTED;
sendSocket(contextPtr->socketFd, (unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE + 1, 0);
}
}
reconnDebugPrint("%s: registering client with eqptTask\n", __FUNCTION__);
if(reconnRegisterClientApp(contextPtr) != RECONN_SUCCESS)
{
reconnDebugPrint("%s: reconnRegisterClientApp() index %d failed\n", __FUNCTION__,
contextPtr->index);
if(contextPtr->socketFd >= 0)
{
close(contextPtr->socketFd);
free((void *)contextPtr->thisContext);
return &contextPtr->retStatus;
}
}
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
/*
* Should only be sent by a slave client
*/
case MASTER_MODE_RESIGN_REQ:
{
reconnDebugPrint("%s: Client %d Received MASTER_MODE_RESIGN_REQ\n", __FUNCTION__, contextPtr->index);
if (masterClientSocketFd == contextPtr->socketFd)
{
sendReconnResponse(contextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1],
RECONN_INVALID_STATE, contextPtr->mode);
}
else
{
/*
* Any iphone inserted in the reconn front panel is ALWAYS the
* master and therefore will never give up mastership.
*/
if(insertedMasterSocketFd != -1)
{
sendReconnResponse(contextPtr->socketFd,
((MASTER_MODE_RESIGN_RESP & 0xff00) >> 8),
(MASTER_MODE_RESIGN_RESP & 0x00ff),
RECONN_DENIED, contextPtr->mode);
}
else
{
/*
* Ask the master, via its file descriptor to release
* mastership.
*/
if(formatReconnPacket(MASTER_MODE_RESIGN_QUERY, (char *)0, 0, &(contextPtr->thePacket)) == RECONN_SUCCESS)
{
sendSocket(masterClientSocketFd, (unsigned char *)&(contextPtr->thePacket), RECONN_PACKET_HEADER_SIZE, 0);
}
/*
* Remember the slave's socket descriptor
*/
theSlaveContextPtr = (CLIENTCONTEXT *)contextPtr->thisContext;
}
}
resetPowerStandbyCounter(RESET_SYSTEM_SHUTDOWN_TIME);
break;
}
/*
* Only sent by a master client
*/
case MASTER_MODE_RESIGN_RESP:
{
reconnDebugPrint("n%s: Client %d Received MASTER_MODE_RESIGN_RESP\n", __FUNCTION__, contextPtr->index);
if ((contextPtr->thePacket.dataPayload[0] == RECONN_SUCCESS) ||
(contextPtr->thePacket.dataPayload[0] == RECONN_DENIED))
{
if(contextPtr->thePacket.dataPayload[0] == RECONN_SUCCESS)
{
/*
* Tell the master iPhone to switch to a slave
*/
if(formatReconnPacket(MASTER_MODE_REMOVED_NOTIF, (char *)0, 0, &(contextPtr->thePacket)) == RECONN_SUCCESS)
{
sendSocket(masterClientSocketFd, (unsigned char *)&(contextPtr->thePacket), RECONN_PACKET_HEADER_SIZE, 0);
/*
* Switch this task to slave, change its socket from non-blocking to blocking.
*/
contextPtr->mode = SLAVEMODE;
contextPtr->flags = fcntl(contextPtr->socketFd, F_GETFL, NULL);
contextPtr->flags &= (~O_NONBLOCK);
fcntl(contextPtr->socketFd, F_SETFL, contextPtr->flags);
/*
* Now that this task is going to be master, change its socketFd from Blocking to
* non blocking. This allows the task to check the masterClientMsgQid for front panel
* insertion events. It also allows this task to make sure the master iPhone device has not
* stopped communicating. If it does stop communicating for some length of time
* mastership is removed and the thread is killed.
*/
fcntl(theSlaveContextPtr->socketFd, F_SETFL, theSlaveContextPtr->flags |= O_NONBLOCK);
/*
* This slave task which started the slave to master transition becomes the master.
*/
masterClientSocketFd = theSlaveContextPtr->socketFd;
theSlaveContextPtr->mode = MASTERMODE;
if(formatReconnPacket(MASTER_MODE_SET_NOTIF, (char *)0, 0, &(theSlaveContextPtr->thePacket)) == RECONN_SUCCESS)
{
reconnDebugPrint("%s: sending MASTER_MODE_SET_NOTIF\n", __FUNCTION__);
sendSocket(theSlaveContextPtr->socketFd, (unsigned char *)&(theSlaveContextPtr->thePacket), RECONN_PACKET_HEADER_SIZE, 0);
}
/*
* Notify the new master that a slave client has attached.
*/
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(RECONN_SLAVE_ATTACHED, contextPtr->theResponsePktPtr);
ADD_DATA_LENGTH_TO_PACKET(0, contextPtr->theResponsePktPtr);
sendSocket(masterClientSocketFd, (unsigned char *)contextPtr->theResponsePktPtr, RECONN_RSPPACKET_HEADER_SIZE, 0);
theSlaveContextPtr = NULL;
}
}
else
{
sendReconnResponse(theSlaveContextPtr->socketFd,
contextPtr->thePacket.messageId.Byte[0],
contextPtr->thePacket.messageId.Byte[1],
RECONN_DENIED, contextPtr->mode);
}
}
else
{
reconnDebugPrint("%s: Invalid payload %d\n", __FUNCTION__, contextPtr->thePacket.dataPayload[0]);
}
break;
}
case RECONN_SW_VERSION_REQ:
{
unsigned int length;
char *theSwVersionString;
reconnDebugPrint("n%s: Client %d Received RECONN_SW_VERSION_NOTIF\n", __FUNCTION__, contextPtr->index);
memset(contextPtr->theResponsePktPtr, 0, sizeof(ReconnResponsePacket));
ADD_RSPID_TO_PACKET(GENERIC_RESPONSE, contextPtr->theResponsePktPtr);
ADD_MSGID_TO_PACKET(RECONN_SW_VERSION_REQ, contextPtr->theResponsePktPtr);
theSwVersionString = getReconnSwVersion();
length = strlen(theSwVersionString);
ADD_DATA_LENGTH_TO_PACKET(length, contextPtr->theResponsePktPtr);
strncpy(&(contextPtr->theResponsePktPtr->dataPayload[0]), theSwVersionString, length);
reconnDebugPrint("%s: returning version %s\n", __FUNCTION__, theSwVersionString);