-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_cmd_server.c
More file actions
453 lines (385 loc) · 9.57 KB
/
app_cmd_server.c
File metadata and controls
453 lines (385 loc) · 9.57 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
/**
* @file app_cmd.c
* @brief
* @author wwk (1162431386@qq.com)
* @version 1.0
* @date 2023-07-06
* @copyright Copyright (c) 2023 by MOVE-X
*
* @par 修改日志:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2023-07-06 <td>1.0 <td>wwk <td>修改?
* </table>
*/
#include <ctype.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <stdlib.h>
#include <sys/un.h>
#include "app_cmd.h"
#define ERR(fmt, args...) printf("\033[31mE[%s:%4d]\033[0m " fmt, __FILE__, __LINE__, ##args)
char *g_pSendData = NULL;
void help_app_cmd_help(void);
int app_cmd_help(int argc, char *argv[]);
/**@brief 接受对应clint数据
* @param fd 套接字句柄
* @param pData 接收数据缓存
* @param uLen 接收数据缓存大小
* @return 成功返回 0
* @return 错误返回 -1
* @see
* @note
*/
int cmd_readn(int iFd, void *pData, unsigned int uLen)
{
unsigned int uLeft = 0;
unsigned int uRead = 0;
int iRet = 0;
unsigned int uTry = 0;
char *pDst = NULL;
fd_set rdFds;
struct timeval tv= {0};
uLeft = uLen;
pDst = pData;
tv.tv_sec = 10;
tv.tv_usec = 0;
while (uLeft > 0)
{
FD_ZERO(&rdFds);
FD_SET(iFd, &rdFds);
iRet = select(iFd + 1, &rdFds, NULL, NULL, &tv);
if (iRet < 0)
{
ERR("Select error out.");
return ERROR;
}
else if (iRet == 0)
{
uTry++;
if (uTry > 10)
{
break;
}
ERR("Select time out.");
}
if (FD_ISSET(iFd, &rdFds))
{
if ((int)(uRead = recv(iFd, pDst, uLeft, 0)) <= 0)
{
break;
}
uLeft -= uRead;
pDst += uRead;
}
}
return uLen - uLeft;
}
/**@brief 发送数据给对应clint端
* @param fd 套接字句柄
* @param pData 要发送的数据
* @param uLen 发送数据的长度
* @return 成功返回 0
* @return 错误返回 -1
* @see
* @note
*/
unsigned int cmd_writen(int iFd, void *pData, unsigned int uLen)
{
unsigned int uLeft = 0;
int iWriten = 0;
char *pSrc = NULL;
uLeft = uLen;
pSrc = pData;
while (uLeft > 0)
{
if ((iWriten = send(iFd, pSrc, uLeft, 0)) < 0)
{
ERR("send %s.",strerror(errno));
break;
}
uLeft -= iWriten;
pSrc += iWriten;
}
return uLen- uLeft;
}
CMD_CTRL_T g_stShellCmd[] =
{
CMD_LIST
};
void help_app_cmd_help(void)
{
int iCnt = 0;
int i = 0;
CMD_CTRL_T *pstShell = NULL;
iCnt = sizeof(g_stShellCmd) / sizeof(g_stShellCmd[0]);
printf("应用支持的命令个数 : %d \n", iCnt);
pstShell = &g_stShellCmd[0];
do
{
printf(" %-30s %-40s \n", pstShell[i].pName, pstShell[i].pHelpStr);
}while (++i < iCnt);
printf("\n");
return;
}
int app_cmd_help(int argc, char *argv[])
{
int iCnt = 0;
int i = 0;
CMD_CTRL_T *pstShell = NULL;
if (argc == 1)
{
iCnt = sizeof(g_stShellCmd) / sizeof(g_stShellCmd[0]);
printf("应用支持的命令个数 : %d", iCnt);
pstShell = &g_stShellCmd[0];
do
{
if (i%2 == 0)
{
printf("\n");
}
printf(" %-30s", pstShell[i].pName);
}while (++i < iCnt);
printf("\n");
return OK;
}
else if (argc == 2)
{
if (strncmp(argv[1], "-a", strlen(argv[1])) == 0)
{
help_app_cmd_help();
return OK;
}
}
return ERROR;
}
/**@brief 命令解析之后,调用对应的命令函数
* @param CMD_DATA_T 调试命令结构体指针
* @return 成功返回 0
* @return 错误返回 -1
* @see
* @note
*/
static unsigned int cmd_param_prase(CMD_DATA_T *pShellData)
{
int iRet = 0;
int argc = 0;
unsigned int uCnt = 0;
CMD_CTRL_T *pstShell = NULL;
void * uData[MAX_SHELL_ARGC]= {0};
if (pShellData == NULL)
{
return ERROR;
}
argc = pShellData->uArgc;
for (uCnt = 0; uCnt < argc; uCnt++)
{
uData[uCnt] = (pShellData->aArgv[uCnt]);
}
uCnt = 0;
pstShell = &g_stShellCmd[0];
do
{
if (strlen(pstShell[uCnt].pName) == strlen(pShellData->aArgv[0]) &&
strncmp(pstShell[uCnt].pName, pShellData->aArgv[0], strlen(pShellData->aArgv[0])) == 0)
{
if (argc != 1 && strncmp(pShellData->aArgv[1], "--help", strlen(pShellData->aArgv[1])) == 0)
{
if (pstShell[uCnt].help)
{
pstShell[uCnt].help();
return OK;
}
else
{
printf("no help\n");
}
}
/*每次命令输出先输出一个换行,比较美观*/
printf("\r\n");
iRet = pstShell[uCnt].entry(argc, (char **)&uData);
if (iRet == ERROR)
{
printf("试用 %s --help 获取更多信息\n", pShellData->aArgv[0]);
}
return iRet;
}
}while (++uCnt < (sizeof(g_stShellCmd) / sizeof(g_stShellCmd[0])));
printf("Unknow cmd : %s\n", pShellData->aArgv[0]);
return ERROR;
}
int app_cmd_process(char* pCmdParam,int uCmdLen)
{
if(NULL == pCmdParam || 0 == uCmdLen)
{
return ERROR;
}
CMD_DATA_T stCmdData;
memset(&stCmdData,0,sizeof(stCmdData));
stCmdData.uMagic = CMD_MAGIC_NUM;
stCmdData.uArgc = 0;
char cmdTmpParam[256] = {0};
snprintf(cmdTmpParam,sizeof(cmdTmpParam),"%s ",pCmdParam);
/*skip ' '*/
char* pStart = cmdTmpParam;
char* pEnd = pStart;
unsigned int uCopy = 0;
while(stCmdData.uArgc < MAX_SHELL_ARGC)
{
while(' ' == *pStart && '\0' != *pStart)
{
pStart++;
}
if('\0' == *pStart)
{
break;
}
pEnd = pStart + 1;
while(' ' != *pEnd && '\0' != *pEnd)
{
pEnd++;
}
if(' ' != *pEnd || ' ' == *pStart)
{
break;
}
uCopy = pEnd - pStart;
uCopy = MIN(uCopy,MAX_SHELL_LENGTH);
memcpy(stCmdData.aArgv[stCmdData.uArgc],pStart,uCopy);
stCmdData.uArgc++;
pStart = pEnd + 1;
}
return cmd_param_prase(&stCmdData);
}
/**@brief soket编程,创建socket bind listen
* @param
* @return 成功返回 大于0值
* @return 错误返回 -1
* @see
* @note
*/
int cmd_serv_listen(const char *name)
{
socklen_t iLen = 0;
int iSockFd = 0;
struct sockaddr_un un;
iSockFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (iSockFd == -1)
{
ERR("Socket %s\n", strerror(errno));
return ERROR;
}
unlink(name);
un.sun_family = AF_UNIX;
strncpy(un.sun_path, name, sizeof(un.sun_path) - 1);
iLen = sizeof(struct sockaddr_un);
do
{
if (bind(iSockFd, (struct sockaddr *)&un, iLen) < 0)
{
ERR("Bind fd:%d failed %s", iSockFd, strerror(errno));
break;
}
if (listen(iSockFd, 10) < 0)
{
ERR("Listen fd:%d failed %s", iSockFd, strerror(errno));
break;
}
return iSockFd;
} while(0);
close(iSockFd);
return ERROR;
}
/**@brief 命令调试服务线程,接受串口发送的命令
* @param
* @return
* @see
* @note
*/
void *cmd_server_task(void *arg)
{
int iSockFd = 0;
int iCliFd = 0;
int iWriten = 0;
int iReadn = 0;
char *pReadBuf = NULL;
CMD_DATA_T *pstCmdData = NULL;
socklen_t scokLen = 0;
struct sockaddr_un un;
iSockFd = cmd_serv_listen(SOCKED_CMD_PATH);
if (iSockFd < 0)
{
ERR("Listen %s.", strerror(errno));
return NULL;
}
pReadBuf = (char *)malloc(MAX_BUFF_LEN);
if (pReadBuf == NULL)
{
ERR("Malloc failed.");
close(iSockFd);
return NULL;
}
g_pSendData = (char *)malloc(MAX_BUFF_LEN);
if (g_pSendData == NULL)
{
ERR("Malloc failed.");
SAFE_FREE(pReadBuf);
close(iSockFd);
return NULL;
}
while (1)
{
memset(g_pSendData, 0, MAX_BUFF_LEN);
memset(pReadBuf, 0, MAX_BUFF_LEN);
iCliFd = accept(iSockFd, (struct sockaddr *)&un, &scokLen);
if (iCliFd < 0)
{
ERR("Accept failed.");
continue;
}
if ((iReadn = cmd_readn(iCliFd, pReadBuf, sizeof(CMD_DATA_T))) != sizeof(CMD_DATA_T))
{
iWriten = snprintf(g_pSendData, MAX_BUFF_LEN, "readn failed read = %d\n", iReadn);
cmd_writen(iCliFd, g_pSendData, iWriten);
close(iCliFd);
continue;
}
pstCmdData = (CMD_DATA_T *)pReadBuf;
if (pstCmdData->uMagic != CMD_MAGIC_NUM)
{
iWriten = snprintf(g_pSendData, MAX_BUFF_LEN, "readn data magic err = %#x.\n", pstCmdData->uMagic);
cmd_writen(iCliFd, g_pSendData, iWriten);
close(iCliFd);
continue;
}
cmd_param_prase(pstCmdData);
close(iCliFd);
}
SAFE_FREE(g_pSendData);
SAFE_FREE(pReadBuf);
close(iSockFd);
unlink(SOCKED_CMD_PATH);
}
/**@brief 命令调试初始化函数
* @param
* @return 成功返回 0
* @return 错误返回 !0
* @see
* @note
*/
int cmd_server_init(void)
{
unsigned int uRet = 0;
pthread_t thread = (pthread_t)-1;
uRet = pthread_create(&thread, NULL, cmd_server_task, NULL);
if (uRet != OK)
{
ERR("Create task cmd_server failed %s.\n", strerror(errno));
}
pthread_detach(thread); /*线程分离*/
return uRet;
}