[feature]add at service

Change-Id: I04fca1abfa7e324b719e08cd5c0725594e1ecc02
diff --git a/framework/lynq-ril-service/src/ril.cpp b/framework/lynq-ril-service/src/ril.cpp
index bc6133a..3dd15d3 100755
--- a/framework/lynq-ril-service/src/ril.cpp
+++ b/framework/lynq-ril-service/src/ril.cpp
@@ -76,6 +76,12 @@
 #include "lynq_interface.h"
 #include "lynq_common.h"
 #include "lynq_sms_manager.h"
+/*lei add*/
+#include "lynq_at.h"
+#include "lynq_user.h"
+#include "lynq_rndis.h"
+#include "lynq_factory.h"
+/*lei add*/
 /*Warren add for t800 RIL service 2021_12_10 end*/
 
 #define LOG_TAG "DEMO_RIL"
@@ -3794,6 +3800,49 @@
 
 #endif
 
+static void parse_version_buf(char *buf){
+    char *p = buf;
+    int flag = 0;
+    int recv = 0;
+    int tmp = -1;
+    while (*p != '\0')
+    {   
+        recv++;
+        if(*p == '"'){
+            tmp = recv;        
+        }
+        if(tmp >= 0){
+            buf[flag++] = buf[tmp++];
+            if(buf[flag-1] == '\"'){
+                buf[flag-1] = '\0';
+            }
+        }
+        *p++;
+    }
+    return;
+}
+
+static int lynq_get_version(){
+    FILE *fp;
+    char buf[128] = {0};
+    char cgmr[128] = {0};
+    sprintf(buf, "uci get lynq_uci_ro.lynq_version.LYNQ_SW_VERSION 2>&1");
+    fp=popen(buf, "r");
+    int n;
+    while(fgets(buf, sizeof(buf), fp) != NULL){
+        
+    }
+    //parse_version_buf(buf);
+    sprintf(cgmr, "%s %s", "+CGMR: ", buf);
+    n = write(ttyGS3_fd,cgmr,strlen(cgmr));
+    if(n<0)
+    {
+        perror("lynq resp write:");
+    }
+    pclose(fp);
+    return 0;
+}
+
 extern "C" void
 RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen) {
     RequestInfo *pRI;
@@ -3944,6 +3993,9 @@
                    {
                        update_reg_data_radio_tech(RIL_REQUEST_DATA_REGISTRATION_STATE, atoi((char *)p_cur[3]), socket_id, pRI->token);
                    }
+                   /*Warren add for SZZT 2021/11/14 start*/
+                   lynqAtRespWatingEvent();
+                   /*Warren add for SZZT 2021/11/14 end*/
                    break;
                }
                case RIL_REQUEST_GET_CURRENT_CALLS:
@@ -4013,11 +4065,51 @@
                    }
                    break;
                }
+               case RIL_REQUEST_OEM_HOOK_RAW:
+               {
+                   if(pRI->lynqEvent==1)
+                   {    
+                        /*lei add for at+cgmr*/
+                        if(strstr((const char*)response,"+CGMR")){
+                            lynq_get_version();
+                        }
+                        /*lei add for at+cgmr*/
+                        int n = write(ttyGS3_fd,response,responselen);
+                        if(n<0)
+                        {
+                            perror("lynq resp write:");
+                        }
+                        printf("n = %d\n",n);
+                   }
+               }
                default:
                    break;
                }
         }
-
+       /*Warren add for FAW platform 2021/10/8 start*/
+       else
+       {
+           if(pRI->lynqEvent==1)
+           {
+               char eBuf[64] = {0};
+               bzero(eBuf, 64);
+               if(e==RIL_E_SUCCESS)
+               {
+                    sprintf(eBuf,"\nOK\n\0");
+               }
+               else
+               {
+                   sprintf(eBuf,"\nCME ERROR: %d\n\0",e);
+               }
+               int n = write(ttyGS3_fd,eBuf,strlen(eBuf));
+               if(n<0)
+               {
+                   perror("lynq resp write:");
+               }
+               //printf("n = %d\n",n);
+           }
+       }
+       /*Warren add for FAW platform 2021/10/8 start*/
         if (e != RIL_E_SUCCESS) {
             appendPrintBuf("%s fails by %s", printBuf, failCauseToString(e));
         }
@@ -5958,6 +6050,149 @@
     return 0;
 }
 
+void startUsbLoop(void)
+{
+    int nread=-1;
+    int n = -1;
+    int routeId = -1;
+    char buffer[1024]={};
+    char tempbuf[1024]={};
+    char buf_parser[64] = {};
+    int argc = 0;
+    char *argv[MAX_ARGS];//argv[0]:at name,argv[1]:raw data,argv[2]:at type,argv[3]:paramter1,argv[4]:paramter2 ....
+    char eBuf[1024];
+    ttyGS3_fd = open("/dev/ttyGS3",O_RDWR);
+    if(ttyGS3_fd==-1)
+    {
+        RLOGE("open ttyGS3 failure!!!");
+        printf("open ttyGS3 failure!!!\n");
+        //printf("%s\n",strerr(errno));
+        perror("--test--");
+        kill(0, SIGKILL);
+    }
+    printf("[%s]open %s successfully!!!\n",__FUNCTION__,ttyname(ttyGS3_fd));
+    while(1)
+    {
+        bzero(buffer, 1024);
+        bzero(tempbuf, 1024);
+        bzero(buf_parser, 64);
+        if((nread=read(ttyGS3_fd,buffer,1024))>0)
+        {
+            if(nread<2)
+            {
+                //RLOGD("input is space!!!");
+                continue;
+            }
+            buffer[nread-1] = '\0';
+            //printf("buffer is %s\n",buffer);
+            for(int i = 0 ; i < nread ; i++)
+            {
+                if(buffer[i]=='=')
+                {
+                    break;
+                }
+                if(buffer[i]>='a'&&buffer[i]<='z')
+                {
+                    buffer[i] = buffer[i]-32;
+                }
+            }
+            printf("buffer is %s\n",buffer);
+            argc = lynqParseUsbCommand(buffer,argv,tempbuf,buf_parser,MAX_ARGS);
+            if(argc<0)
+            {  
+               bzero(eBuf, 1024);
+               sprintf(eBuf,"LYNQ:%s not support!!!\n",buffer);
+               int n = write(ttyGS3_fd,eBuf,strlen(eBuf));
+               if(n<0)
+               {
+                   perror("lynq resp write:");
+               }
+               printf("n = %d\n",n);
+               continue;
+            }
+            usb_at_transfer_t *atCmd = lynqFindId(argv[0]);
+            if(atCmd==NULL)
+            {
+                RLOGD("LYNQ send ATCMD:%s!!!",argv[1]);
+                lynqSendAt(argc,argv,1010);
+                continue;
+            }
+            if(!((1<<atoi(argv[2])) & (atCmd->support)))
+            {
+                RLOGD("LYNQ %s not support!!!",atCmd->cmdName);
+                int n = write(ttyGS3_fd,"\n+CME ERROR: 100\n",strlen("\n+CME ERROR: 100\n"));
+                if(n<0)
+                {
+                    perror("lynq resp write:");
+                }
+                continue;
+            }
+            routeId = routeCmd(atCmd->cmdId);
+            //routeId = routeCmd(atcmd->cmdId);
+            //routeId = LYNQ_GOTO_AT;
+            switch(routeId)
+            {
+                case LYNQ_GOTO_AT:
+                {
+                    lynqSendAt(argc,argv,1010);
+                    break;
+                }
+                case LYNQ_GOTO_TELE_REQ:
+                {
+                    usb_cmd_t *atCmdEvn = lynqFindUsbEvent(argv[0]);
+                    if(!atCmdEvn)
+                    {
+                        RLOGD("can not find at cmd event!!!");
+                        continue;
+                    }
+                    atCmdEvn->fun(argc,argv,atCmdEvn->rilRequest,1011);
+                    break;
+                }
+                case LYNQ_GOTO_USER_REQ:
+                {
+                    usb_cmd_t *atCmdEvn = lynqFindUsbEvent(argv[0]);
+                    if(!atCmdEvn)
+                    {
+                        RLOGD("can not find at cmd event!!!");
+                        continue;
+                    }
+                    atCmdEvn->ufun(argc,argv,1012);
+                    break;
+                }
+                case LYNQ_GOTO_LINFO_REQ:
+                {
+                    lynqInfo(argv);
+                    break;
+                }
+                // case LYNQ_GOTO_PLAT_REQ:
+                // {
+                //     lynq_deal_with_log_at(&argv[3]);
+                //     break;
+                // }
+                case LYNQ_GOTO_RNDIS_REQ:
+                {
+                    lynq_get_rndis_data(buffer);
+                    break;
+                }
+                case LYNQ_GOTO_FACTORY:
+                {
+                    lynq_get_factory_data(argc,argv);
+                    break;
+                }
+                // case LYNQ_GOTO_FOTA:
+                // {
+                //     lynq_atfota_test(argv);
+                //     break;
+                // }
+                default:
+                    break;
+            }
+        }
+    }
+    close(ttyGS3_fd);
+    return;
+}
+
 void *
 eventLoop(void *param) {
     pthread_mutex_lock(&s_startupMutex);
@@ -5979,6 +6214,23 @@
     return NULL;
 }
 
+
+void *
+eventLoop_at(void *param) {
+    pthread_mutex_lock(&s_startupMutex);
+    s_started = 1;
+    pthread_cond_broadcast(&s_startupCond);
+    pthread_mutex_unlock(&s_startupMutex);
+    //RIL_StartRevSocket();
+    startUsbLoop();
+    //startUsbLoop_test();
+    RLOGD("error in event_loop_base errno:%d", errno);
+    // kill self to restart on error
+    kill(0, SIGKILL);
+
+    return NULL;
+}
+
 const int RspDispFunction(int request,char* arg, RIL_SOCKET_ID socket_id)
 {
     int waittoken;
@@ -6221,6 +6473,82 @@
 done:
     pthread_mutex_unlock(&s_startupMutex);
 }
+/*Warren add for  FAW 2021/09/23 start
+** use ttyGS0 port
+*/
+int lynqSendToRil(int argc,char *argv[],int uToken)
+{
+    if(argc < 1)
+    {
+        RLOGE("lynqSendToRil error input.");
+        return 1;
+    }
+    for(int i =0;i<argc;i++)
+    {
+        printf("argv[%d]=%s\n",i,argv[i]);
+    }
+    COMMAND *command = find_command(argv[0]);
+    if(!command)
+    {
+        RLOGE("%s: No such command for DemoApp", argv[0]);
+        return 1;
+    }
+
+    int32_t request;
+
+    request = command->request;
+
+    RIL_SOCKET_ID id = RIL_SOCKET_1;
+    if(utils::is_support_dsds()) {
+        id = (RIL_SOCKET_ID)get_default_sim_all_except_data();
+    } else if(utils::is_suppport_dsss()) {
+        id = (RIL_SOCKET_ID)Phone_utils::get_enable_sim_for_dsss();
+    }
+
+    if(request == -1)
+    {
+        (*(command->func)) (argc, argv, id, NULL);
+        return 1;
+    }
+
+    if (request < 1 || (request >= (int32_t)NUM_ELEMS(s_commands) && request < RIL_REQUEST_VENDOR_BASE)) {
+        RLOGW("unsupported request code %d token %d", request);
+        // FIXME this should perhaps return a response
+        return 1;
+    }
+
+    RLOGD("REQUEST: %s ParamterNum:%d", requestToString(request), argc);
+
+
+    RequestInfo *pRI  = creatRILInfoAndInit(request, UDP, (RIL_SOCKET_ID)(id));
+    pRI->lynqEvent = 1;
+    //Radio on/off only allow one thread operate.
+    if(request == RIL_REQUEST_RADIO_POWER)
+    {
+        speciaRequest_wait();
+    }
+    memset(Time_buf,0,sizeof(Time_buf));
+    GetTimeString(Time_buf);
+    //FUNCTION_CALLED(Time_buf,requestToString(request));
+    int waittoken = pRI->token;
+    (*(command->func)) (argc, argv, pRI->socket_id, pRI);
+    FUNCTION_CALLED(Time_buf,requestToString(request));
+    waitResponse(waittoken);
+    memset(Time_buf,0,sizeof(Time_buf));
+    GetTimeString(Time_buf);
+    FUNCTION_RETURN(Time_buf,requestToString(request));
+    return 0;
+}
+
+int sendRespToUsb(char *cmd)
+{
+    return 0;
+}
+int sendUrcToUsb(char *cmd)
+{
+    return 0;
+}
+/*Warren add for  FAW 2021/09/23 end*/
 
 void startPMLoop(void)
 {
@@ -6302,11 +6630,22 @@
         RLOGW("Failed to create dispatch thread: %s", strerror(result));
         goto done;
     }
+   
+    while (s_started == 0) {
+        pthread_cond_wait(&s_startupCond, &s_startupMutex);
+    }
+    /*mobiletek add*/
+    s_started = 0;
+    result = pthread_create(&s_tid_dispatch, &attr, eventLoop_at, NULL);
+    if (result != 0) {
+        RLOGW("Failed to create dispatch thread: %s", strerror(result));
+        goto done;
+    }
 
     while (s_started == 0) {
         pthread_cond_wait(&s_startupCond, &s_startupMutex);
     }
-
+    /*mobiletek add*/
     result = pthread_create(&s_tid_dispatch, &attr, responseLoop, NULL);
     if (result != 0) {
         RLOGW("Failed to create response dispatch thread: %s", strerror(result));