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