-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCardCtl.cpp
360 lines (316 loc) · 9.03 KB
/
CCardCtl.cpp
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
/*
* C-Card Controller
*/
#include <getopt.h>
#include <string>
#include <string.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <syslog.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <polysat/polysat.h>
#include <Mutex.h>
#include <MutexLock.h>
#include "cCardMessages.h"
#include "CCardMsgCodec.h"
#include "ccardDefs.h"
#include "CCardI2CX.h"
#include "InitialDeployer.h"
#include "DsaOpContext.h"
#include "DsaOp.h"
#define DBG_LEVEL_DEBUG DBG_LEVEL_ALL
static Process *gProc=NULL;
using namespace IrvCS;
static uint8_t gPortState=0;
static CCardI2CX *gI2cExpander=NULL;
static std::string gInitDeployFile;
static volatile bool dsaOpInProgress=false; // Allow only one DSA Op at any given time.
static Mutex dsaOpMutex;
static DsaId id2DsaId(uint8_t id)
{
DsaId dsaId=DSA_UNKNOWN;
if (id == DSA_1)
{
dsaId=DSA_1;
} else if (id == DSA_2)
{
dsaId=DSA_2;
}
return dsaId;
}
static DsaCmd cmd2DsaCmd(uint8_t cmd)
{
if (cmd >= CmdUnknown)
{
return CmdUnknown;
}
return static_cast<DsaCmd>(cmd);
}
extern "C"
{
/**
* Process the status command. Provide the cached values since
* this is called frequently by the watchdog. We could periodically
* refresh the value here.
**/
void ccard_status(int socket, unsigned char cmd, void *data, size_t dataLen,
struct sockaddr_in *src)
{
CCardStatus status;
static int counter=0;
status.status=0;
if (NULL == gI2cExpander)
{
DBG_print(LOG_ERR, "%s gI2cExpander is NULL", __FILENAME__);
status.status=-1;
} else
{
int getStatus=0;
// refresh the first time
if (gPortState == 0)
{
getStatus=gI2cExpander->getState(gPortState);
}
if (getStatus==0)
{
status.portStatus=gPortState;
} else
{
status.status=getStatus;
}
status.dsaDeployState=gI2cExpander->getDsaDeployState();
}
syslog(LOG_DEBUG, "Sending status message response %02x", status.portStatus);
PROC_cmd_sockaddr(gProc->getProcessData(), CMD_STATUS_RESPONSE,
&status, sizeof(status), src);
}
/**
* Process the CCard cmd message
**/
void ccard_cmd(int socket, unsigned char cmd, void *data, size_t dataLen,
struct sockaddr_in *src)
{
if (NULL == gI2cExpander)
{
syslog(LOG_ERR, "%s gI2cExpander is NULL", __FILENAME__);
return;
}
CCardMsg *msg=(CCardMsg *)data;
if (dataLen != sizeof(CCardMsg))
{
DBG_print(DBG_LEVEL_WARN, "Incoming size is incorrect (%d != %d)",
dataLen, sizeof(CCardMsg));
return;
}
CCardStatus status;
status.status=0;
int setStatus=0;
uint8_t msgType=0;
uint8_t devId=0;
uint8_t msgCmd=0;
uint32_t hostData=ntohl(msg->data);
IrvCS::CCardMsgCodec::decodeMsgData(hostData, msgType, devId, msgCmd);
int timeout=5;
DsaId dsaId=DSA_UNKNOWN;
DsaCmd dsaCmd=CmdUnknown;
DsaOp *dsaOp=NULL;
void * dsaEvt=NULL;
switch (msgType)
{
case IrvCS::MsgDsa:
dsaId = id2DsaId(devId);
if (DSA_UNKNOWN == dsaId)
{
DBG_print(LOG_ERR, "Unknown DSA ID: %d", devId);
status.status=-1;
break;
}
dsaCmd = cmd2DsaCmd(msgCmd);
if (CmdUnknown == dsaCmd)
{
DBG_print(LOG_ERR, "Uknown DSA Cmd: %d", cmd);
status.status=-1;
break;
}
//
// Release and Deploy operations run in separate thread. All
// other operations can run directly
//
if (dsaCmd != Release && dsaCmd != Deploy)
{
gI2cExpander->dsaPerform(dsaId, dsaCmd);
} else {
MutexLock lock(dsaOpMutex);
if (!dsaOpInProgress)
{
DsaOpContext *context=new DsaOpContext(gProc, src, gI2cExpander, dsaOpInProgress);
dsaOp=new DsaOp(dsaId, dsaCmd,*gI2cExpander, context);
dsaOp->start();
return;
} else
{
DBG_print(LOG_ERR, "Operation in progress");
status.status=StatOpInProgress;
}
}
status.portStatus=gPortState=(uint8_t)setStatus;
break;
case IrvCS::MsgMt:
// TODO: Implement
// gI2cExpander->mtPerform(devId, msgCmd);
setStatus=gPortState;
if (setStatus < 0)
{
status.status=setStatus;
}
else
{
status.portStatus=gPortState=(uint8_t)setStatus;
}
break;
default:
DBG_print(LOG_WARNING, "%s Unknown msg type: %d",
__FILENAME__, msgType);
}
status.dsaDeployState=gI2cExpander->getDsaDeployState();
PROC_cmd_sockaddr(gProc->getProcessData(), CCARD_RESPONSE,
&status, sizeof(status), src);
}
}
static int executeInitialDeploymentOp(void *arg)
{
IrvCS::DsaController *dsaController=static_cast<IrvCS::DsaController *>(arg);
IrvCS::InitialDeployer deployer(dsaController, gInitDeployFile);
DBG_print(LOG_NOTICE, "Launching Initial Deployer");
deployer.start();
return EVENT_REMOVE;
}
/**
* Usage
**/
void usage(char *argv[])
{
std::cout<<"Usage: "<<argv[0]<<" [options]"
<<std::endl<<std::endl
<<" C-card controller daemon"<<std::endl<<std::endl
<<"Options:"<<std::endl<<std::endl
<<" -D {file} Set the initial deploy operation flag file."<<std::endl
<<" If file exists, skip initial deployment op."<<std::endl
<<" Must be in persistent location which is cleared before launch."<<std::endl
<<" -T {seconds} Set the initial deploy delay in seconds"<<std::endl
<<" May be set with /data/deployDelay"<<std::endl
<<" -d {log level} set log level"<<std::endl
<<" -s syslog output to stderr"<<std::endl
<<" -h this message"<<std::endl
<<std::endl;
exit(1);
}
static int sigint_handler(int signum, void *arg)
{
Process *proc=(Process *)arg;
EVT_exit_loop(PROC_evt(proc->getProcessData()));
return EVENT_KEEP;
}
int main(int argc, char *argv[])
{
int status=0;
int syslogOption=0;
int opt;
// initial Deploy delay time in seconds
int initDeployDelayTime=INITIAL_DEPLOY_DELAY; // 45 min default
const char *deployDelayOverrideFile=DEBUG_DEPLOY_DELAY_FILE;
const char *idleDisableFile=IDLE_MODE_DISABLE_FILE;
struct stat statBuf;
bool initDeployFlag = false;
int idleCheckInterval=C_CARD_IDLE_CHECK_INTERVAL;
int logLevel=DBG_LEVEL_INFO;
while ((opt=getopt(argc,argv,"sd:hT:D:")) != -1)
{
switch (opt)
{
case 'D':
// @TODO we could register a ppod deployment event callback
// which would then make this file unnecessary.
// Not sure how to test it to see if it works.
gInitDeployFile=optarg;
if (0 == stat(optarg, &statBuf))
{
initDeployFlag = false;
} else
{
initDeployFlag = true;
}
break;
case 'T':
initDeployDelayTime=strtol(optarg, NULL, 10);
break;
case 's':
syslogOption=LOG_PERROR;
openlog("ccardctl", syslogOption, LOG_USER);
break;
case 'd':
logLevel=strtol(optarg, NULL, 10);
break;
case 'h':
usage(argv);
break;
default:
usage(argv);
}
}
gProc = new Process("ccardctl");
DBG_setLevel(logLevel);
DBG_print(DBG_LEVEL_INFO, "Starting up ccardctl");
IrvCS::CCardI2CX i2cX;
gI2cExpander = &i2cX;
gProc->AddSignalEvent(SIGINT, &sigint_handler, gProc);
void *initDeployEvt=NULL;
void *checkIdleEvt=NULL;
EventManager *events=gProc->event_manager();
// look for deploy delay override setting
if (!stat(idleDisableFile, &statBuf))
{
DBG_print(LOG_NOTICE, "Idle mode disabled with %s", idleDisableFile);
} else
{
DBG_print(LOG_NOTICE, "Scheduling idle check - %d seconds",
idleCheckInterval);
/**
TODO: Probably remove this
checkIdleEvt=EVT_sched_add(PROC_evt(gProc->getProcessData()),
EVT_ms2tv(idleCheckInterval*1000),
checkCCardIdle,
&i2cX);
**/
}
if (initDeployFlag)
{
// look for deploy delay override setting
if (!stat(deployDelayOverrideFile, &statBuf))
{
DBG_print(LOG_INFO, "Overriding deploy option with %s", deployDelayOverrideFile);
std::ifstream ifs(deployDelayOverrideFile, std::ios::in);
ifs >> initDeployDelayTime;
}
DBG_print(LOG_NOTICE, "Scheduling Initial Deployment in %d seconds (%d min)",
initDeployDelayTime, initDeployDelayTime/60);
initDeployEvt=EVT_sched_add(PROC_evt(gProc->getProcessData()),
EVT_ms2tv(initDeployDelayTime*1000),
executeInitialDeploymentOp,
&i2cX);
}
DBG_print(DBG_LEVEL_INFO, "%s Ready to process messages",__FILENAME__);
events->EventLoop();
//
// Cleanup
//
if (initDeployEvt)
{
EVT_sched_remove(PROC_evt(gProc->getProcessData()), initDeployEvt);
}
delete gProc;
gI2cExpander=NULL;
return status;
}