-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientMenu.c
More file actions
executable file
·307 lines (266 loc) · 13.3 KB
/
clientMenu.c
File metadata and controls
executable file
·307 lines (266 loc) · 13.3 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <net/if.h>
#include "debugMenu.h"
#include "reconn.h"
#include "clientApp.h"
#include "socket.h"
#include "upgrade.h"
#include "eqptResponse.h"
#include "remoteMonitor.h"
#include "libiphoned.h"
extern void reconnMasterIphone();
extern int theDebugSocketFd;
static int numberClients();
static int upgrade();
static int showClient();
#ifdef __SIMULATION__
static int iPhoneInserted = -1;
static int iPhoneInsert();
static int iPhoneExtract();
static int slaveMode();
static int masterMode();
#endif
static char *errMsg = "\r\nInvalid client index requested\r\n";
static char *sessionQuestion = "\r\nEnter client index > ";
pthread_mutex_t clientListMutex = PTHREAD_MUTEX_INITIALIZER;
CLIENTCONTEXT *activeClientsList[RECONN_MAX_NUM_CLIENTS];
debugMenuStruct clientDebugMenu[] =
{
{"client", "Client debug Menus", NULL, NULL, NULL},
#ifdef __SIMULATION__
{NULL, NULL, "insert", "Simulates front panel iPhone insertion", iPhoneInsert},
{NULL, NULL, "extract", "Simulates front panel iPhone extraction", iPhoneExtract},
{NULL, NULL, "slave", "Simulates USB client access request", slaveMode},
{NULL, NULL, "master", "Simulates USB Master mode request", masterMode},
#endif
{NULL, NULL, "list", "Display all active client source IPs", clientList},
{NULL, NULL, "session", "Display a clients session info ", showClient},
{NULL, NULL, "sac", "Show the number of active client sessions", numberClients},
{NULL, NULL, "upgrade", "Execute a software upgrade", upgrade},
};
void registerClientDebugMenu()
{
registerDebugCommand(&clientDebugMenu[0], sizeof(clientDebugMenu)/sizeof(debugMenuStruct));
}
#ifdef __SIMULATION__
static int iPhoneInsert()
{
iPhoneInserted = 0;
reconnMasterIphone();
return RECONN_SUCCESS;
}
static int iPhoneExtract()
{
iPhoneInserted = -1;
reconnMasterIphone();
return RECONN_SUCCESS;
}
static int slaveMode()
{
ReconnPacket thePacket;
ReconnPacket *thePacketPtr = &thePacket;
ADD_MSGID_TO_PACKET(CLIENT_ACCESS_REQ, thePacketPtr);
ADD_DATA_LENGTH_TO_PACKET(0, thePacketPtr);
insertedMasterRead((unsigned char *)&thePacket, 4);
return RECONN_SUCCESS;
}
static int masterMode()
{
ReconnPacket thePacket;
ReconnPacket *thePacketPtr = &thePacket;
ADD_MSGID_TO_PACKET(MASTER_MODE_REQ, thePacketPtr);
ADD_DATA_LENGTH_TO_PACKET(0, thePacketPtr);
insertedMasterRead((unsigned char *)&thePacket, 4);
return RECONN_SUCCESS;
}
#endif
static int numberClients()
{
memset((char *)&debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf((char *)&debugOutputString, "\r\nThe number of active clients is %d\r\n", reconnClientsRegistered(ALL));
sendSocket(theDebugSocketFd, (unsigned char *)&debugOutputString, strlen(debugOutputString), 0);
return RECONN_SUCCESS;
}
static int upgrade(void)
{
ReconnErrCodes retCode;
if(reconnClientsRegistered(ALL) > 0)
{
sprintf((char *)&debugOutputString, "\r\nCan't upgrade because there are %d active clients.\r\n", reconnClientsRegistered(ALL));
sendSocket(theDebugSocketFd, (unsigned char *)&debugOutputString, strlen((char *)&debugOutputString), 0);
}
else
{
if((retCode = extractBundle()) == RECONN_SUCCESS)
{
libiphoned_stop();
system("killall -SIGTERM reconn-service");
}
else
{
memset((char *)&debugOutputString, 0, DEBUG_OUTPUT_LEN);
switch (retCode)
{
case RECONN_UPGRADE_FILE_NOT_FOUND:
{
strcpy((char *)&debugOutputString, "\r\nUPGRADE ABORTED: upgrade file not found.\r\n");
sendSocket(theDebugSocketFd, (unsigned char *)&debugOutputString, strlen(debugOutputString), 0);
break;
}
case RECONN_UPGRADE_BAD_CHECKSUM:
{
strcpy((char *)&debugOutputString, "\r\nUPGRADE ABORTED: /tmp/upgradeBundle has an invalid checksum.\r\n");
sendSocket(theDebugSocketFd, (unsigned char *)&debugOutputString, strlen(debugOutputString), 0);
break;
}
default:
{
strcpy((char *)&debugOutputString, "\r\nUPGRADE ABORTED: ");
strcat((char *)&debugOutputString, strerror(errno));
strcat((char *)&debugOutputString, "\r\n");
sendSocket(theDebugSocketFd, (unsigned char *)&debugOutputString, strlen(debugOutputString), 0);
break;
}
}
}
}
return RECONN_SUCCESS;
}
#ifdef __SIMULATION__
int simulate_isiphonepresent()
{
return iPhoneInserted;
}
#endif
int clientList(int theSocketFd)
{
int i;
CLIENTCONTEXT *aContext;
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "\r\n%-8s%-16s%-8s%-16s%-9s\r\n", "Client", "IP", "Port", "Client Mode", "SocketFd");
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "=========================================================\r\n");
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
#ifdef DEBUG_MUTEX
reconnDebugPrint("%s: Calling pthread_mutex_lock(&clientListMutex) %d %d %d\r\n", __FUNCTION__, clientListMutex.__data.__lock, clientListMutex.__data.__count, clientListMutex.__data.__owner);
#endif
pthread_mutex_lock(&clientListMutex);
for(i = 0; i < RECONN_MAX_NUM_CLIENTS; i++, aContext++)
{
aContext = activeClientsList[i];
if(aContext)
{
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
if(i == 0)
{
sprintf(debugOutputString, "%-8d%-16s%-8s%-16s%-9s\r\n", aContext->index, "Usb Connector", "NA", "Inserted Master", "NA");
}
else
{
sprintf(debugOutputString, "%-8d%-16s%-8d%-16s%-9d\r\n", aContext->index, inet_ntoa(aContext->sourceIp.sin_addr), aContext->sourceIp.sin_port, (activeClientsList[i]->mode == SLAVEMODE) ? "SLAVE" : (activeClientsList[i]->mode == MASTERMODE) ? "WIFI MASTER": "REMOTE", aContext->socketFd);
}
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
}
}
#ifdef DEBUG_MUTEX
reconnDebugPrint("%s: Calling pthread_mutex_unlock(&clientListMutex) %d %d %d\r\n", __FUNCTION__, clientListMutex.__data.__lock, clientListMutex.__data.__count, clientListMutex.__data.__owner);
#endif
pthread_mutex_unlock(&clientListMutex);
return RECONN_SUCCESS;
}
static int showClient(int theSocketFd)
{
int index;
char *theAnswer;
CLIENTCONTEXT *theContext = NULL;
// Ask the user for an index
sendSocket(theDebugSocketFd, (unsigned char *)sessionQuestion, strlen(sessionQuestion), 0);
theAnswer = getInput(theSocketFd, YES);
index = atoi(theAnswer);
sendSocket(theDebugSocketFd, (unsigned char *)DEBUG_CLEARSCREEN, strlen(DEBUG_CLEARSCREEN), 0);
sendSocket(theDebugSocketFd, (unsigned char *)DEBUG_CURSORHOME, strlen(DEBUG_CURSORHOME), 0);
if((index < 0) || (index > RECONN_MAX_NUM_CLIENTS))
{
sendSocket(theSocketFd, (unsigned char *)errMsg, strlen(errMsg), 0);
}
else
{
#ifdef DEBUG_MUTEX
reconnDebugPrint("%s: Calling pthread_mutex_lock(&clientListMutex) %d %d %d\r\n", __FUNCTION__, clientListMutex.__data.__lock, clientListMutex.__data.__count, clientListMutex.__data.__owner);
#endif
pthread_mutex_lock(&clientListMutex);
theContext = activeClientsList[index];
if(theContext)
{
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "\r\n%-17s %s\r\n", "Variable", "Value");
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "=========================\r\n");
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s 0x%x\r\n", "thisContext", (unsigned int)theContext->thisContext);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "index", theContext->index);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s 0x%x\r\n", "theEqptFd", (unsigned int)theContext->eqptDescriptors);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s 0x%x\r\n","socketFd", theContext->socketFd);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "connectionOpen", theContext->connectionOpen);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "pktLength", theContext->pktLength);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "length", theContext->length);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "responseNeeded", theContext->responseNeeded);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "retStatus", theContext->retStatus);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
#ifdef DEBUG_CLIENT
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "debugIndex", theContext->debugIndex);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
#endif
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN); sprintf(debugOutputString, "%-17s 0x%x\r\n", "cmdid", theContext->cmdid);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);sprintf(debugOutputString, "%-17s %d\r\n", "responseId", theContext->responseId); sprintf(debugOutputString, "%-17s %d\r\n", "responseId", theContext->responseId);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN); sprintf(debugOutputString, "%-17s %d\r\n", "retCode", theContext->retCode);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN); sprintf(debugOutputString, "%-17s %s\r\n", "mode", (theContext->mode == MASTERMODE) ? "Master": (theContext->mode == SLAVEMODE) ? "Slave" : "Inserted Master" );
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN); sprintf(debugOutputString, "%-17s %d\r\n", "DmmFd", theContext->eqptDescriptors->dmmFd);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN); sprintf(debugOutputString, "%-17s %d\r\n", "powerMeterFd", theContext->eqptDescriptors->powerMeterFd);
sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN); sprintf(debugOutputString, "%-17s %d\r\n", "dmmFd", theContext->eqptDescriptors->dmmFd);
memset(debugOutputString, 0, DEBUG_OUTPUT_LEN);
sprintf(debugOutputString, "%-17s %d\r\n", "analyzerFd", theContext->eqptDescriptors->analyzerFd); sendSocket(theSocketFd, (unsigned char *)debugOutputString, strlen(debugOutputString), 0);
}
else
{
sendSocket(theSocketFd, (unsigned char *)errMsg, strlen(errMsg), 0);
}
#ifdef DEBUG_MUTEX
reconnDebugPrint("%s: Calling pthread_mutex_unlock(&clientListMutex) %d %d %d\r\n", __FUNCTION__, clientListMutex.__data.__lock, clientListMutex.__data.__count, clientListMutex.__data.__owner);
#endif
pthread_mutex_unlock(&clientListMutex);
}
free(theAnswer);
return RECONN_SUCCESS;
}