123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- #include "includes.h"
- #define SIZE_FROM_INNER (TUP_DATA_MAX+EXT_DATA) //内部(module)传输最大缓存
- #define SIZE_FROM_OUTTER (TUP_DATA_MAX+EXT_DATA) //外部(串口)传输最大缓存
- /*
- bspStartNotice
- 应用就绪标识
- 用于通知外部(串口)应用已正常启动
- */
- static void bspStartNotice(void){
- //char cmd[]="+POC:START\r\n";
- //msgToOutter(cmd);
- }
- static void msgFromInnerHandler(T_UINT8 *data, T_UINT16 length);
- static void msgFromOutterHandler(T_UINT8 *data, T_UINT16 length);
- #define VIR_WAIT_FOREVER (-1U)
- static LSAPI_OSI_Thread_t *gUartWRTd = NULL;
- static void usartCallBack(void *param, uint32_t evt);
- LSAPI_Device_UartConfig_t uart_cfg = {
- .name = LSAPI_MAKE_TAG('U', 'R', 'T', '1'),//LSAPI_MAKE_TAG('U', 'R', 'T', '1')/LSAPI_MAKE_TAG('U', 'R', 'T', '3')
- .baud = 115200,
- .format = LSAPI_DEVICE_FORMAT_8N1,
- .parity = LSAPI_DEVICE_PARITY_ODD,
- .event_cb=usartCallBack,
- };
- LSAPI_Device_t *uart=NULL;
- LSAPI_OSI_Pipe_t *at_rx_pipe = NULL;
- LSAPI_OSI_Pipe_t *at_tx_pipe = NULL;
- static void usartCallBack(void *param, uint32_t evt){
- LSAPI_OSI_Event_t uart_event;
- //wlog_info("usartCallBack(evt:%d)>> enter",evt);
- uart_event.id = USER_EVENT_UART;
- uart_event.param1 = evt;
- /*Note: must use osiEventTrySend, not to use osiEventSend*/
- if(osiEventTrySend((LSAPI_OSI_Thread_t *)param, &uart_event, 0) == FALSE)
- wlog_error("uart send event failed");
- }
- /*
- UART1初始化及处理线程
- */
- void uartThread(void *param){
- int read_size;
- static char gPtrReadBuf[SIZE_FROM_OUTTER];
- LSAPI_OSI_Event_t waitevent;
- wlog_info("uartThread start");
- for(;;){
- LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent);
- switch(waitevent.param1){
- case LS_UART_EVENT_RX_ARRIVED:
- while(LSAPI_Device_ReadAvail(uart)>0){
- read_size = LSAPI_Device_Read(uart, gPtrReadBuf, SIZE_FROM_OUTTER);
- if(read_size< SIZE_FROM_OUTTER) gPtrReadBuf[read_size]=0;
- //wlog_info("UART:%d,%02x,%02x,%s",read_size,gPtrReadBuf[0],gPtrReadBuf[1],gPtrReadBuf);
- if(TRUE==app.tmMode){
- #ifdef ENABLE_USB_PORT
- usbOutPut(gPtrReadBuf,read_size);
- #endif
- }else{
- if(0==miapRecvFromUart(gPtrReadBuf,read_size))//优先处理PCTA消息
- msgFromOutterHandler(gPtrReadBuf,read_size);
- }
- }
- WakeupNow("usartCallBack");
- break;
- case LS_UART_EVENT_RX_OVERFLOW:
- break;
- case LS_UART_EVENT_TX_COMPLETE:
- break;
- default:break;
- }
- }
- gUartWRTd=NULL;
- LSAPI_Device_Close(uart);
- LSAPI_OSI_ThreadExit();
- }
- static void prvVirtAtRespCallback(void *param, unsigned event)
- {
- LSAPI_OSI_Pipe_t *pipe = (LSAPI_OSI_Pipe_t *)param;
- char buf[256];
-
- for (;;)
- {
- int bytes = LSAPI_OSI_PipeRead(pipe, buf, sizeof(buf)-1);
- if (bytes <= 0)
- break;
- buf[bytes] = '\0';
- //wlog_info("VAT1:%d,%s",bytes,buf);
- msgFromInnerHandler(buf,bytes);
- }
- WakeupNow("prvVirtAtRespCallback");
- }
- void ptInitViratThread(void *param){
- at_rx_pipe = LSAPI_OSI_PipeCreate(1024);
- at_tx_pipe = LSAPI_OSI_PipeCreate(1024);
- LSAPI_OSI_PipeSetReaderCallback(at_tx_pipe, LSAPI_PIPE_EVENT_RX_ARRIVED,
- prvVirtAtRespCallback, at_tx_pipe);
- LSAPI_Device_AtVirtConfig_t cfg = {
- .name = LSAPI_MAKE_TAG('V', 'A', 'T', '1'),
- .rx_pipe = at_rx_pipe,
- .tx_pipe = at_tx_pipe,
- };
- LSAPI_Device_t *device = LSAPI_Device_AtVirtCreate(&cfg);
- LSAPI_Device_AtDispatch_t *dispatch = LSAPI_Device_AtDispatchCreate(device);
- LSAPI_Device_AtSetDispatch(device, dispatch);
- LSAPI_Device_Open(device);
- LSAPI_OSI_ThreadExit();
- }
- #ifdef ENABLE_USB_PORT
- void usbComInit(void);
- #endif
- void uartComInit(void){
- bool ret;
- //init usart part
- gUartWRTd=LSAPI_OSI_ThreadCreate("uart1", uartThread, NULL, LSAPI_OSI_PRIORITY_NORMAL, UART_THREAD_STACK, 4);
- if(NULL==gUartWRTd) wlog_error("uart thread create err");
- else{
- uart_cfg.event_cb_ctx = gUartWRTd;
- uart = LSAPI_Device_UartCreate(&uart_cfg);
- if(uart==NULL){
- wlog_error("LSAPI_Device_UartCreate err");
- return;
- }
- ret = LSAPI_Device_Open(uart);
- if(false==ret){
- wlog_error("LSAPI_Device_Open failed");
- return;
- }
- LSAPI_Device_SetAutoSleep(uart, 500);
- }
- }
- void viratInit(void){
- if(NULL==LSAPI_OSI_ThreadCreate("virat", ptInitViratThread, NULL, LSAPI_OSI_PRIORITY_NORMAL, VAT_THREAD_STACK, 4))
- wlog_error("virat thread create err");
- }
- void bspInit(void){
- uartComInit();
- viratInit();
- //他奶奶的,下面的延时是一定要的,否则VAT1的回调无法进入
- //情况是:如果不加下面的延时,只要执行bspStartNotice往串口输出数据,VAT1就无法回调了
- //搞死我了,我顶你的肺
- LSAPI_OSI_ThreadSleep(500);
- bspStartNotice();
- setAudioChannelAndVol();
- #ifdef ENABLE_USB_PORT
- usbComInit();
- #endif
- }
- void uartOutPut(unsigned char *info, int len){
- if(NULL==uart) return;
- if(LSAPI_Device_WriteAvail(uart) != -1) LSAPI_Device_Write(uart, info,len);
- }
- /*
- outterInfo
- 发送数据到外部(串口)API
- */
- void outterInfo(T_UINT8 *info, T_UINT32 len){
- #ifndef ENABLE_UART1_MCU_CMD_ONLY
- uartOutPut(info, len);
- #endif
- #ifdef ENABLE_USB_PORT
- usbOutPut(info, len);
- #endif
- }
- /*
- innerInfo
- 发送数据到内部(module)API
- */
- void innerInfo(T_UINT8 *info, T_UINT32 len){
- if(len>0){
- if(0>LSAPI_OSI_PipeWriteAll(at_rx_pipe, info, len, VIR_WAIT_FOREVER))
- wlog_info("LSAPI_OSI_PipeWriteAll write failed");
- }
- }
- /*
- msgFromInnerHandler
- 内部(module)数据入队列操作
- */
- static void msgFromInnerHandler(T_UINT8 *data, T_UINT16 length){
- static T_UINT8 msg[SIZE_FROM_INNER];
- static T_UINT16 dLen=0;
- T_UINT16 i;
- static T_UINT8 lastData=0;
- for(i=0;i<length;i++){
- if(dLen < sizeof(msg)) msg[dLen++]=data[i];
- else{
- dLen=0;
- lastData=0;
- wlog_warn("Inner Data over");
- continue;
- }
- if(lastData=='\r' && data[i] == '\n'){
- switch(msgFromInner(msg, dLen)){
- case 0://queue ok
- break;
- case 1://queue full
- wlog_warn("queue full");
- break;
- case 2://queue buffer full
- wlog_warn("queue buffer full");
- break;
- case 3://unsupport queue
- wlog_warn("queue invalid");
- break;
- default:break;
- }
- dLen=0;
- }
- lastData=data[i];
- }
- }
- /*
- msgFromOutterHandler
- 外部(串口)数据入队列操作
- */
- static void msgFromOutterHandler(T_UINT8 *data, T_UINT16 length){
- static T_UINT8 msg[SIZE_FROM_OUTTER];
- static T_UINT16 dLen=0;
- static T_UINT8 douhaoNum=0;
- static T_UINT32 num;
- static T_UINT8 getNum=0,strLen;
- static T_UINT16 i;
- static T_UINT8 lastData=0;
- for(i=0;i<length;i++){
- if(dLen < sizeof(msg)) msg[dLen++]=data[i];
- else{
- dLen=0;
- lastData=0;
- wlog_warn("Outter data over");
- continue;
- }
- if(0==memcmp(msg, "AT+TCPSEND=", 11)){//AT+TUPSEND=1,1,1
- if(dLen >= 12 && (msg[11] < '0' || msg[11] > '9')) goto CMD_ERR_CLEAR;
- if(dLen >= 13 && msg[12] != ',') goto CMD_ERR_CLEAR;
- if(dLen >= 14 && (msg[13] < '0' || msg[13] > '9')) goto CMD_ERR_CLEAR;
- if(dLen >= 15){
- if(data[i]== ','){
- douhaoNum++;
- if(douhaoNum==1){
- strLen=dLen;
- num=atoi(msg+13);
- if(num > TUP_DATA_MAX) goto CMD_ERR_CLEAR;
- else getNum=1;
- }
- }else if(data[i] < '0' || data[i] > '9'){
- if(douhaoNum==0) goto CMD_ERR_CLEAR;
- }
- if(getNum!=0){
- if(dLen >= (num+strLen)){
- getNum=0;
- douhaoNum=0;
- num=0;
- strLen=0;
- goto CMD_QUE_PACKET;
- }
- }
- }
- }else if(lastData=='\r' && data[i] == '\n')
- goto CMD_QUE_PACKET;
- goto T_CONINUE;
- CMD_QUE_PACKET:
- switch(msgFromOutter(msg, dLen))
- {
- case 0://queue ok
- break;
- case 1://queue full
- wlog_warn("queue full");
- break;
- case 2://queue buffer full
- wlog_warn("queue buffer full");
- break;
- case 3://unsupport queue
- wlog_warn("queue invalid");
- break;
- default:
- break;
- }
- //memset(msg,0,sizeof(msg));
- dLen=0;
- goto T_CONINUE;
- CMD_ERR_CLEAR:
- getNum=0;
- douhaoNum=0;
- num=0;
- strLen=0;
- msgToOutter("+ERROR\r\n");
- wlog_error("outter cmd error");
- //memset(msg,0,sizeof(msg));
- dLen=0;
- T_CONINUE:
- lastData=data[i];
- }
- }
- #ifdef ENABLE_USB_PORT
- /////////////////////////////////////////////////////
- #include "lsapi_device.h"
- #include "lsapi_menu.h"
- #include "lsapi_event.h"
- static LSAPI_OSI_Thread_t *gUsbWRTd = NULL;
- LSAPI_Device_t *usbdev=NULL;
- void my_usb_evt_cb(lsUSBEvent_t event, unsigned long p){
- LSAPI_OSI_Event_t usb_event;
- usb_event.id = USER_EVENT_USB;
- usb_event.param1=event;
- wlog_info("my_usb_evt_cb:%d",event);
- osiEventTrySend((LSAPI_OSI_Thread_t *)p, &usb_event, 0);
- }
- LSAPI_Device_USBConfig_t usb_cfg = {
- .name = LSAPI_DEV_USB_COM7,
- .event_cb = my_usb_evt_cb,
- };
- void usbOutPut(unsigned char *info, int len){
- if(NULL==usbdev) return;
- if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,len);
- }
- void usbPortHandler(void *param){
- int ret;
- unsigned char buffer[100];
- LSAPI_OSI_Event_t waitevent;
- wlog_info("usb create ok");
- for(;;){
- LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent);
- if(waitevent.param1==LS_USB_EVENT_RX_ARRIVED){
- while(LSAPI_Device_ReadAvail(usbdev)>0){
- ret = LSAPI_Device_Read(usbdev, buffer, sizeof(buffer));
- if(ret< sizeof(buffer)) buffer[ret]=0;
- usbCmdHandler(buffer,ret);
- }
- WakeupNow("usbCallBack");
- }
- }
- wlog_info("usbPortHandler exit");
- LSAPI_Device_Close(usbdev);
- gUsbWRTd=NULL;
- LSAPI_OSI_ThreadExit();
- }
- void usbComInit(void){
- bool ret;
- //init usart part
- gUsbWRTd=LSAPI_OSI_ThreadCreate("usb", usbPortHandler, NULL, LSAPI_OSI_PRIORITY_NORMAL, USB_THREAD_STACK, 4);
- if(NULL==gUsbWRTd) wlog_error("usb thread create err");
- else{
- usb_cfg.event_cb_ctx = gUsbWRTd;
- usbdev = LSAPI_Device_USBCreateEx(&usb_cfg);
- if(usbdev==NULL){
- wlog_error("usbex create err");
- return;
- }
- ret = LSAPI_Device_Open(usbdev);
- if(false==ret){
- wlog_error("usb LSAPI_Device_Open failed");
- return;
- }
- LSAPI_Device_SetAutoSleep(usbdev, 500);
- wlog_info("usbComInit done");
- }
- }
- #endif
|