blob: f8a4f4712a46f024d87a5ef5504a4afe23842990 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/************************************************************************
2*¹¦ÄܽéÉÜ£ºÓÃÓÚ¹¦ÄÜ»úap²à´¦ÀíµÄAP\CPºË¼äUSB\TcardÀ©Õ¹at£¬ÓÉat_ctlÔÚÆô¶¯Ê±µ÷ÓÃ
3*¸ºÔðÈË£º
4*±¸·ÝÈË£º
5*ÐÞ¸ÄÈÕ£º
6*ÐÞ¸ÄÄÚÈÝ£º
7*°æ±¾ºÅ£º
8************************************************************************/
9#if (APP_OS_TYPE == APP_OS_LINUX)
10#include "at_msg.h"
11#include "at_com.h"
12#include "at_context.h"
13#include "ext_dev_func.h"
14#include "ext_cmux_func.h"
15
16//add for cmux
17#include <sys/socket.h>
18#include <linux/if.h>
19#include <linux/gsmmux.h>
20#include <poll.h>
21#include <termios.h>
22#include <limits.h>
23
24#define OPENECHO 1 //´ò¿ª»ØÏÔ¹¦ÄÜ
25#define CLOSEECHO 0 //¹Ø±Õ»ØÏÔ¹¦ÄÜ
26#define USB_GPIO_DETECT_ENABLE_EXT "/sys/dwc_usb/usbconfig/gpio_detect"
27
28static int writefile(char*path, char*buf, unsigned len)
29{
30 FILE *fp;
31 int rtv;
32 if((fp=fopen(path,"w"))==NULL)
33 {
34 slog(USBCFGMNG_PRINT,SLOG_ERR, "[ext_dev_fun] open file %s error.\n",path);
35 return -1;
36 }
37 rtv = fwrite(buf,len,1, fp);
38 fclose(fp);
39 return rtv;
40}
41
42
43// USB¶Ë¿ÚÐÅÏ¢
44T_PCS_USB_INFO s_tUsbInfo =
45{
46 '0', // print prompt after receiving
47 0, // baudrate: 0
48 '8', // databit: 8
49 '0', // debug: off
50 '0', // echo: off
51 '0', // flow control: software
52 '0', // default tty: COM1
53 '0', // parity: none
54 '1', // stopbit: 1
55 0 // reserved
56};
57
58
59// ²¨ÌØÂÊת»¯×ª»»º¯Êý
60static int32_t iFnConvbaud(int32_t iBaudrate)
61{
62 //PC_SERVER_LOG();
63
64 switch(iBaudrate)
65 {
66 case 2400:
67 return B2400;
68 case 4800:
69 return B4800;
70 case 9600:
71 return B9600;
72 case 19200:
73 return B19200;
74 case 38400:
75 return B38400;
76 case 57600:
77 return B57600;
78 case 115200:
79 return B115200;
80 default:
81 return B9600;
82 }
83}
84
85// ²¨ÌØÂÊ·´×ª»¯×ª»»º¯Êý
86static int32_t iFnBaudconv(int32_t Baudrate)
87{
88 switch(Baudrate)
89 {
90 case B2400:
91 return 2400;
92 case B4800:
93 return 4800;
94 case B9600:
95 return 9600;
96 case B19200:
97 return 19200;
98 case B38400:
99 return 38400;
100 case B57600:
101 return 57600;
102 case B115200:
103 return 115200;
104 default:
105 return 9600;
106 }
107}
108
109// ÉèÖö˿ÚÐÅÏ¢¡£iFdCom: ´®¿ÚÎļþÃèÊö·û, ptPortInfo: ´ýÉèÖõĶ˿ÚÐÅÏ¢¡£
110void vFnSetUsbInfo(int32_t iFdCom, const PT_PCS_USB_INFO ptPortInfo, USB_PORT_MODE usbPortMode)
111{
112 struct termios tOldTermios = {0}, tNewTermios = {0};
113 int32_t iBaudrate = 0;
114 char cDatabit = 0, cStopbit = 0, cParity = 0, cFctl = 0;
115
116 //PC_SERVER_LOG();
117
118 bzero(&tOldTermios, sizeof(tOldTermios));
119 bzero(&tNewTermios, sizeof(tNewTermios));
120 cfmakeraw(&tNewTermios);
121 tcgetattr(iFdCom, &tOldTermios); // get the serial port attributions
122 /*------------ÉèÖö˿ÚÊôÐÔ----------------*/
123 if(usbPortMode == AUTO_MODE)
124 {
125 iBaudrate = cfgetispeed(&tOldTermios);
126 }
127 else
128 {
129 iBaudrate = iFnConvbaud(ptPortInfo->iBaudrate);
130 tcflush(iFdCom, TCIOFLUSH);
131 cfsetispeed(&tOldTermios, iBaudrate); //ÌîÈë´®¿ÚÊäÈë¶Ë²¨ÌØÂÊ
132 cfsetospeed(&tOldTermios, iBaudrate); //ÌîÈë´®¿ÚÊä³ö¶Ë²¨ÌØÂÊ
133 if(tcsetattr(iFdCom, TCSANOW, &tOldTermios) != 0) //ÉèÖÃÐÂÊôÐÔ, TCSANOW: ËùÓɸıäÁ¢¼´ÉúЧ
134 {
135 return;
136 }
137 tcflush(iFdCom, TCIOFLUSH);
138 return;
139 }
140 cfsetispeed(&tNewTermios, iBaudrate); //ÌîÈë´®¿ÚÊäÈë¶Ë²¨ÌØÂÊ
141 cfsetospeed(&tNewTermios, iBaudrate); //ÌîÈë´®¿ÚÊä³ö¶Ë²¨ÌØÂÊ
142 tNewTermios.c_cflag |= CLOCAL; //¿ØÖÆÄ£Ê½, ±£Ö¤³ÌÐò²»»á³ÉΪ¶Ë¿ÚµÄÕ¼ÓÐÕß
143 tNewTermios.c_cflag |= CREAD; //¿ØÖÆÄ£Ê½, ʹÄܶ˿ڶÁÈ¡ÊäÈëµÄÊý¾Ý
144 // ¿ØÖÆÄ£Ê½, flow control
145 cFctl = ptPortInfo->cFctl;
146 switch(cFctl)
147 {
148 case '0':
149 {
150 tNewTermios.c_cflag &= ~CRTSCTS; //no flow control
151 }break;
152 case '1':
153 {
154 tNewTermios.c_cflag |= CRTSCTS; //hardware flow control
155 }break;
156 case '2':
157 {
158 tNewTermios.c_iflag |= IXON | IXOFF | IXANY; //software flow control
159 }break;
160 default:
161 {
162 break;
163 }
164 }
165 // ¿ØÖÆÄ£Ê½, data bits
166 tNewTermios.c_cflag &= ~CSIZE; //¿ØÖÆÄ£Ê½, ÆÁ±Î×Ö·û´óСλ
167 cDatabit = ptPortInfo->cDatabit;
168 switch(cDatabit)
169 {
170 case '5':
171 tNewTermios.c_cflag |= CS5;
172 // lint -fallthrough
173 case '6':
174 tNewTermios.c_cflag |= CS6;
175 // lint -fallthrough
176 case '7':
177 tNewTermios.c_cflag |= CS7;
178 // lint -fallthrough
179 default:
180 tNewTermios.c_cflag |= CS8;
181 }
182 // ¿ØÖÆÄ£Ê½ parity check
183 cParity = ptPortInfo->cParity;
184 switch(cParity)
185 {
186 case '0':
187 {
188 tNewTermios.c_cflag &= ~PARENB; //no parity check
189 }break;
190 case '1':
191 {
192 tNewTermios.c_cflag |= PARENB; //odd check
193 tNewTermios.c_cflag &= ~PARODD;
194 }break;
195 case '2':
196 {
197 tNewTermios.c_cflag |= PARENB; //even check
198 tNewTermios.c_cflag |= PARODD;
199 }break;
200 default:
201 {
202 break;
203 }
204 }
205 // ¿ØÖÆÄ£Ê½, stop bits
206 cStopbit = ptPortInfo->cStopbit;
207 if('2' == cStopbit)
208 {
209 tNewTermios.c_cflag |= CSTOPB;
210 }
211 else
212 {
213 tNewTermios.c_cflag &= ~CSTOPB;
214 }
215 // other attributions default
216 tNewTermios.c_oflag &= ~OPOST; //Êä³öģʽ, ԭʼÊý¾ÝÊä³ö
217 tNewTermios.c_cc[VMIN] = 1; //¿ØÖÆ×Ö·û, ËùÒª¶ÁÈ¡×Ö·ûµÄ×îСÊýÁ¿
218 tNewTermios.c_cc[VTIME] = 1; //¿ØÖÆ×Ö·û, ¶ÁÈ¡µÚÒ»¸ö×Ö·ûµÄµÈ´ýʱ¼ä£¬unit: (1/10)second
219 tcflush(iFdCom, TCIFLUSH); //Òç³öµÄÊý¾Ý¿ÉÒÔ½ÓÊÕ,µ«²»¶Á
220 tcsetattr(iFdCom, TCSANOW, &tNewTermios); //ÉèÖÃÐÂÊôÐÔ, TCSANOW: ËùÓɸıäÁ¢¼´ÉúЧ
221 tcgetattr(iFdCom, &tOldTermios);
222}
223
224static int32_t vFnPortEchoType(int32_t fdcom, int32_t echoType)
225{
226 struct termios termios_old;
227 int32_t setResult = 0;
228
229 bzero(&termios_old, sizeof(termios_old));
230 tcgetattr(fdcom, &termios_old); //get the serial port attributions
231 /*------------ÉèÖö˿ÚÊôÐÔ----------------*/
232 if(echoType == OPENECHO)
233 {
234 termios_old.c_lflag |= ECHO; //±¾µØÄ£Ê½ÉèÖûØÏÔ
235 //termios_old.c_lflag |= ECHONL;
236 }
237 else
238 {
239 termios_old.c_lflag &= ~ECHO; //±¾µØÄ£Ê½¹Ø±Õ»ØÏÔ
240 //termios_old.c_lflag &= ~ECHONL;
241 }
242
243 tcflush(fdcom, TCIFLUSH); //Òç³öµÄÊý¾Ý¿ÉÒÔ½ÓÊÕ,µ«²»¶Á
244 setResult = tcsetattr(fdcom, TCSANOW, &termios_old); //ÉèÖÃÐÂÊôÐÔ, TCSANOW: ËùÓɸıäÁ¢¼´ÉúЧ
245 return setResult;
246}
247
248int ziprSet_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
249{
250 char* at_str = NULL;
251 int32_t iBaudrate = 0;
252 char strAtReplyCmd[AT_CMD_MAX] = {0};
253
254 if(at_paras==NULL)
255 softap_assert("ziprSet_req:at_paras is null");
256
257 at_str = at_paras;
258 get_at_cmd_param_int(at_str, &iBaudrate, &at_str);
259 at_print(AT_DEBUG,"ziprSet_rsq:iBaudrate == %d\n", iBaudrate);
260 if (iBaudrate < 0 || iBaudrate > INT_MAX-1) //kw 3
261 iBaudrate = 0;
262 s_tUsbInfo.iBaudrate = iBaudrate;
263 sleep(0.002);
264 vFnSetUsbInfo(at_fd, &s_tUsbInfo, SET_MODE);
265 sprintf(strAtReplyCmd,"%d", iBaudrate);
266 *res_msg = at_query_result_build("IPR",strAtReplyCmd);
267 *res_msglen = strlen(*res_msg);
268
269 return AT_END;
270}
271
272int ziprGet_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
273{
274 struct termios tOldTermios = {0};
275 int32_t iBaudrate = 0;
276 char strAtReplyCmd[AT_CMD_MAX] = {0};
277
278 tcgetattr(at_fd, &tOldTermios); // get the serial port attributions
279 iBaudrate = iFnBaudconv(cfgetispeed(&tOldTermios));
280
281 sprintf(strAtReplyCmd,"%d", iBaudrate);
282 *res_msg = at_query_result_build("IPR",strAtReplyCmd);
283 *res_msglen = strlen(*res_msg);
284
285 return AT_END;
286}
287
288int zsetUsb_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
289{
290 int32_t usbMode = -1;
291 char msgBuf[10] = { 0 };
292 char* at_str = NULL;
293
294 if(at_paras==NULL)
295 softap_assert("zsetUsb_req:at_paras is null");
296 at_str = at_paras;
297 get_at_cmd_param_int(at_str, &usbMode, &at_str);
298 at_print(AT_DEBUG,"zsetUsb_req:usbMode == %d\n", usbMode);
299
300 if(!is_at_cmd_end(at_str))
301 {
302 *res_msg = at_err_build(ATERR_PARAM_INVALID);
303 *res_msglen = strlen(*res_msg);
304 return AT_END;
305 }
306
307 if(usbMode != 0 && usbMode != 1)
308 {
309 *res_msg = at_err_build(ATERR_PARAM_INVALID);
310 *res_msglen = strlen(*res_msg);
311 return AT_END;
312 }
313
314 snprintf(msgBuf,sizeof(msgBuf)-1,"%d",usbMode);
315 int result = ipc_send_message(MODULE_ID_AT_CTL, MODULE_ID_USBCFGMNG, MSG_CMD_USBMOD_SETREQ, sizeof(msgBuf), msgBuf,0);
316 at_print(AT_DEBUG,"result == %d\n", result);
317 if(result != 0)
318 {
319 *res_msg = at_err_build(ATERR_PROC_FAILED);
320 *res_msglen = strlen(*res_msg);
321 return AT_END;
322 }
323
324 *res_msg = at_ok_build();
325 *res_msglen = strlen(*res_msg);
326 return AT_END;
327}
328
329int ate_req_rcv_act(char *at_paras,int at_fd,struct at_context *context)
330{
331 int32_t setResult = 0;
lh9ed821d2023-04-07 01:36:19 -0700332
333 if(atoi(at_paras) == 0 || atoi(at_paras) == 1)
334 {
335//#ifdef GUODIAN
336 if(g_customer_type == CUSTOMER_GUODIAN || g_customer_type == CUSTOMER_NANDIAN)
337 {
338 at_write(at_fd, "\r\nOK\r\n", strlen("\r\nOK\r\n"));
339 }
340 else
341 {
342//#else
343 if(atoi(at_paras) == 0)
344 setResult = vFnPortEchoType(at_fd, CLOSEECHO);
345 else
346 setResult = vFnPortEchoType(at_fd, OPENECHO);
347
348 if(setResult != 0)
xf.li6236ea72023-07-26 04:58:33 -0700349 {
350 at_write(at_fd, "\r\nERROR\r\n", strlen("\r\nERROR\r\n"));
lh9ed821d2023-04-07 01:36:19 -0700351 }
352 else
353 {
354 at_write(at_fd, "\r\nOK\r\n", strlen("\r\nOK\r\n"));
355 }
356 }
357//#endif
358 return AT_END;
359 }
360 return AT_CONTINUE;
361}
362
363
364int zudiskstat_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
365{
366 at_print(AT_ERR, "Enter zudiskstat_req\n");
367 *res_msg = malloc(AT_CMD_PREFIX);
368 assert(*res_msg!=NULL);
369 memset(*res_msg, 0, AT_CMD_PREFIX);
370 sprintf(*res_msg, "AT+ZUDISKSTAT=%s\r", at_paras);
371 at_print(AT_ERR, "*res_msg:%s, len: %d\n", (char *)(*res_msg), strlen(*res_msg));
372 *res_msglen = strlen(*res_msg);
373 return AT_CONTINUE;
374}
375
376int zudiskstat_rsp( void *rsp_msg, void **ret, int *retlen)
377{
378 *ret = malloc(strlen(rsp_msg)+1);
379 assert(*ret!=NULL);
380 memset(*ret,0,strlen(rsp_msg)+1);
381 memcpy(*ret,rsp_msg,strlen(rsp_msg));
382 *retlen = strlen(*ret);
383 return AT_END;
384}
385
386char *zusbStat_req(void *msg,struct at_context *context)
387{
388 char *at_next=malloc(AT_CMD_PREFIX);
389 assert(at_next!=NULL);
390 memset(at_next,0, AT_CMD_PREFIX);
391
392 snprintf(at_next, AT_CMD_PREFIX, "AT+USBINQ=%s\r\n", ((MSG_BUF *)msg)->aucDataBuf);
393 return at_next;
394}
395
396//AP²àNVÇå³ý
397int zaprest_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
398{
399 int32_t restMode = 0;
400 char* at_str = NULL;
401
402 if(at_paras==NULL)
403 softap_assert("zaprest_req:at_paras is null");
404 at_str = at_paras;
405 get_at_cmd_param_int(at_str, &restMode, &at_str);
406 at_print(AT_DEBUG,"zaprest_req:restMode == %d\n", restMode);
407
408 if(!is_at_cmd_end(at_str))
409 {
410 *res_msg = at_err_build(ATERR_PARAM_INVALID);
411 *res_msglen = strlen(*res_msg);
412 return AT_END;
413 }
414
415 //µ÷WiFiÌṩµÄ½Ó¿Ú,Çå³ýapËùÓÐnv
416 if(restMode==1)
417 {
418 //nv_clear(NV_RT2860);
419 sc_cfg_reset();
420 }
421
422 *res_msg = at_ok_build();
423 *res_msglen = strlen(*res_msg);
424
425 return AT_END;
426}
427
428int zflowcontrolset_req(int at_fd,char * at_paras,void * *res_msg,int * res_msglen)
429{
430 int cFctl = -1;
431 void *p[1] = {&cFctl};
432
433 parse_param2("%d", at_paras, p);
434 at_print(AT_NORMAL,"zflowcontrolset_req:cFctl = %d\n", cFctl);
435
436 switch(cFctl)
437 {
438 case 0://¹Ø±ÕÁ÷¿Ø
439 {
440 //sc_cfg_set("uart_control","1");
441 sc_cfg_set("uart_ctstrs_enable","");
442 sc_cfg_set("uart_softcontrol_enable","");
443 *res_msg = at_ok_build();
444 *res_msglen = strlen(*res_msg);
445 sc_cfg_save();
446 ipc_send_message(MODULE_ID_AT_CTL,MODULE_ID_MAIN_CTRL, MSG_CMD_RESTART_REQUEST, 0, NULL,0);
447 break;
448 }
449 case 1://¿ªÆôÓ²¼þÁ÷¿Ø
450 {
451 //sc_cfg_set("uart_control","1");
452 sc_cfg_set("uart_ctstrs_enable","1");
453 sc_cfg_set("uart_softcontrol_enable","");
454 *res_msg = at_ok_build();
455 *res_msglen = strlen(*res_msg);
456 sc_cfg_save();
457 ipc_send_message(MODULE_ID_AT_CTL,MODULE_ID_MAIN_CTRL, MSG_CMD_RESTART_REQUEST, 0, NULL,0);
458 break;
459 }
460 case 2://¿ªÆôÈí¼þÁ÷¿Ø
461 {
462 sc_cfg_set("uart_ctstrs_enable","");
463 sc_cfg_set("uart_softcontrol_enable","1");
464 *res_msg = at_ok_build();
465 *res_msglen = strlen(*res_msg);
466 sc_cfg_save();
467 ipc_send_message(MODULE_ID_AT_CTL,MODULE_ID_MAIN_CTRL, MSG_CMD_RESTART_REQUEST, 0, NULL,0);
468 break;
469 }
470 default:
471 {
472 *res_msg = at_err_build(ATERR_PARAM_INVALID);
473 *res_msglen = strlen(*res_msg);
474 break;
475 }
476 }
477 return AT_END;
478}
479
480int zSetgpio_detect_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
481{
482 char* at_str = NULL;
483 int32_t set_value = 0;
484 int ret = 0;
485 if(at_paras==NULL)
486 softap_assert("zSetgpio_detect_req:at_paras is null");
487
488 at_str = at_paras;
489 get_at_cmd_param_int(at_str, &set_value, &at_str);
490 at_print(AT_DEBUG,"zSetgpio_detect_req:set_value = %d\n", set_value);
491
492 if(!is_at_cmd_end(at_str))
493 {
494 *res_msg = at_err_build(ATERR_PARAM_INVALID);
495 *res_msglen = strlen(*res_msg);
496 return AT_END;
497 }
498
499 if(set_value < 0 || set_value > 1)
500 {
501 *res_msg = at_err_build(ATERR_PARAM_INVALID);
502 *res_msglen = strlen(*res_msg);
503 return AT_END;
504 }
505 sc_cfg_set("usb_gpio_detect", (set_value == 1) ? "1" : "0");
506
507 sc_cfg_save();
508 //write this attr,
509 ret = writefile(USB_GPIO_DETECT_ENABLE_EXT, (set_value == 1) ? "1" : "0", 1);
510 if(ret < 0)
511 at_print(AT_DEBUG,"zSetgpio_detect_req:set attr fail \n");
512 *res_msg = at_ok_build();
513 *res_msglen = strlen(*res_msg);
514
515 return AT_END;
516}
517
518int zGetgpio_detect_req(int at_fd, char *at_paras,void ** res_msg, int *res_msglen)
519{
520 char gpio_detect[4] = {0};
521
522 sc_cfg_get("usb_gpio_detect",gpio_detect,sizeof(gpio_detect));
523
524 *res_msg = at_query_result_build("gpio_detect",gpio_detect);
525
526 *res_msglen = strlen(*res_msg);
527
528 return AT_END;
529}
530
531int ext_dev_ser_regist()
532{
533 register_serv_func2("ZAPRESET=",0,0,0,zaprest_req,NULL);
534
xf.lice873192023-11-08 17:10:35 -0800535#ifndef USE_CAP_SUPPORT
lh9ed821d2023-04-07 01:36:19 -0700536 //ATE»ØÏÔ£¬¶ÔÓÚATE0ºÍATE1£¬ÔÚ¼à¿Øº¯ÊýÖÐÖ±½Ó´¦Àíºó·µ»Ø½á¹û£¬ÆäËûÇé¿öÏÂ͸´«¸øÐ­ÒéÕ»
537 register_fwd_func("ate",ate_req_rcv_act,NULL,NULL);
538
539 //¹Ì¶¨²¨ÌØÂÊÉèÖúͲéѯÃüÁî
540 register_serv_func2("ipr=",0,0,0,ziprSet_req,NULL);
541 register_serv_func2("ipr?",0,0,0,ziprGet_req,NULL);
xf.lice873192023-11-08 17:10:35 -0800542#endif
lh9ed821d2023-04-07 01:36:19 -0700543 //Ä£ÄâUSB²å°Î
544 register_serv_func2("zsetusb=",MODULE_ID_USBCFGMNG,MSG_CMD_USBMOD_SETREQ,0,zsetUsb_req,NULL);
xf.lice873192023-11-08 17:10:35 -0800545#ifndef USE_CAP_SUPPORT
lh9ed821d2023-04-07 01:36:19 -0700546 //CMUXģʽÇл»
547 register_serv_func2("cmux=",MODULE_ID_DRVCOMMNG,MSG_CMD_CMUX_SET_REQ,MSG_CMD_CMUX_SET_RSP,zcmuxSet_req,zcmuxSet_rsp);
xf.lice873192023-11-08 17:10:35 -0800548#endif
lh9ed821d2023-04-07 01:36:19 -0700549 //
550 register_serv_func2("ifc=",0,0,0,zflowcontrolset_req,NULL);
551
552 //GPIO¼ì²âusb²å°ÎÉèÖÃ
553 register_serv_func2("gpio_detect=",0,0,0,zSetgpio_detect_req,NULL);
554 register_serv_func2("gpio_detect?",0,0,0,zGetgpio_detect_req,NULL);
555 return 0;
556}
557
558void ext_dev_regist_init()
559{
560 //intercore_ext_clt_regist();
561 ext_dev_ser_regist();
562
563}
564#endif
565