[Bugfix][bug-view-1349]Resolve the issue of probability of not receiving text messages

Only Configure:No
Affected branch:master
Affected module:sms
Is it affected on both ZXIC and MTK:only ZXIC
Self-test:Yes

Change-Id: I41e6e0f20aa51a410882d8843e2bd3b30de817a3
diff --git a/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp b/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp
index 599c9fc..050b1c5 100755
--- a/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp
+++ b/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp
@@ -64,9 +64,11 @@
     {

         if(index == sms_indexs[cnt])

         {

+            sqlite3_close(smsDb);

             return 1;

         }

     }

+    sqlite3_close(smsDb);

     return 0;

 }

 

@@ -81,19 +83,24 @@
     LYVERBLOG("next_index=%d",next_index);

     lynq_sms_t* temp = (lynq_sms_t *)data;

     for(int i=0; i<argc; i++){

-       LYVERBLOG("[%s]%s = %s", __FUNCTION__,azColName[i], argv[i] ? argv[i] : "NULL");

+       LYVERBLOG("[%s]%s = %s\n", __FUNCTION__,azColName[i], argv[i] ? argv[i] : "NULL");

     }

+    LYVERBLOG("lcz1111 smsInfo address %p\n",temp);

     temp->index = atoi(argv[0]);

     temp->slot_id = atoi(argv[1]);

     temp->lynq_sms_status=(lynq_sms_status_t)atoi(argv[2]);

-    memcpy(temp->address,argv[3],LYNQ_TELEPHONE_NUM_LEN);

+    LYVERBLOG("lcz2222 smsInfo.adress address %p\n",temp->address);

+    strncpy(temp->address,argv[3],LYNQ_TELEPHONE_NUM_LEN);

     temp->addrLen = strlen(argv[3]);

-    memcpy(temp->smsc,argv[4],LYNQ_SMS_MAX_SCA_NUMBER_LEN);

+    LYVERBLOG("lcz3333 smsInfo.sms %p\n",temp->smsc);

+    strncpy(temp->smsc,argv[4],LYNQ_SMS_MAX_SCA_NUMBER_LEN);

     temp->smscLen = strlen(argv[4]);

     temp->charset = atoi(argv[5]);

-    memcpy(temp->pdu,argv[6],LYNQ_SMS_MAX_PDU_NUMBER_LEN);

+    LYVERBLOG("lcz_pdu smsInfo.pdu %p\n",temp->pdu);

+    strncpy(temp->pdu,argv[6],LYNQ_SMS_MAX_PDU_NUMBER_LEN);

     temp->pduLen = strlen(argv[6]);

-    memcpy(temp->sms_text,argv[7],LYNQ_SMS_MAX_TEXT_NUMBER_LEN);

+    LYVERBLOG("lcz5555 smsInfo.text %p\n",temp->sms_text);

+    strncpy(temp->sms_text,argv[7],LYNQ_SMS_MAX_TEXT_NUMBER_LEN);

     temp->sms_text_len = strlen(argv[7]);

     /*lei add for gsw 2022/5/12*/

     temp->current = atoi(argv[8]);

@@ -158,6 +165,7 @@
     {

         LYERRLOG("SQL error: %s\n", zErrMsg);

         sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

         return -1;

     }

     else

@@ -211,6 +219,15 @@
     char *zErrMsg = 0;

     int rc;

     char sql[128]={0};

+    if(index != -1)

+    {

+        if(!lynq_check_index(index))

+        {

+            return LYNQ_E_SMS_SQL_FAIL;

+        }

+    }

+    /*lei add*/

+    

     /* Open database */

     rc = sqlite3_open(SMS_DB_PATH, &smsDb);

     if(index == -1)

@@ -239,12 +256,6 @@
     }

     else

     {

-        /*lei add*/

-        if(!lynq_check_index(index))

-        {

-            return LYNQ_E_SMS_SQL_FAIL;

-        }

-        /*lei add*/

         if( rc )

         {

             LYVERBLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(smsDb));

@@ -504,6 +515,7 @@
         return 1;

     }

     LYDBGLOG("write sms to sms db successfully");

+    sqlite3_close(smsDb);

     //printf("write sms to sms db successfully\n");

     return 0;

 }

diff --git a/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/sms.cpp b/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/sms.cpp
index a3e19ae..798baad 100755
--- a/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/sms.cpp
+++ b/cap/zx297520v3/src/lynq/framework/lynq-ril-service/src/sms/sms.cpp
@@ -60,6 +60,7 @@
 #define PROP_ECALL_NUM "vendor.gost.ecall.ecall_sms_fallback_number"
 
 extern sms_manager * g_smsManagement;
+static pthread_mutex_t read_sms_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static void constructGsmSendSmsRilRequest (android::Parcel &p, char *smscPDU, char *pdu) {
     p.writeInt32(2);
@@ -70,7 +71,10 @@
 int getsmsDatabasestatus(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
 {
     android::Parcel p;
+    pthread_mutex_lock(&read_sms_mutex);
+    LYVERBLOG("lcz_lock smsInfo getdatabase\n");
     int id = g_smsManagement->lynq_check_sms_full();
+    pthread_mutex_unlock(&read_sms_mutex);
     RLOGD("[%s] id = %d\n",__FUNCTION__,id);
     android::lynqAssemblyParcelheader(p,socket_id,pRI->uToken,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,0,0);
 
@@ -537,6 +541,19 @@
     return;
 }
 
+static void acquire_wake_lock()
+{
+    int ret = 0;
+
+    ret = system("echo sms_read 5000000000 > /sys/power/wake_lock");//timeout 5s
+    if(ret != 0)
+    {
+        LYERRLOG("sms_read error !!!");
+    }
+    return;
+}
+
+
 int responseNewSMS(const char *data, size_t datalen, int soc_id,int32_t unsol,android::Parcel &p){
     char smsc[512] = {0};
     char msg[512] = {0};
@@ -558,12 +575,16 @@
     {
         gostParseSmsHandle(soc_id, num, msg);
     }
+    acquire_wake_lock();
     /*Warren change for t800 ril service 2022/1/18 start
     **By default, the SMS is stored in the memory after receiving it
     */
     /*lei modify for gsw 2022/5/11*/
+    pthread_mutex_lock(&read_sms_mutex);
+    LYVERBLOG("lcz_lock smsInfo write\n");
     index = g_smsManagement->lynq_write_sms_to_memory(data,(const char*)num,(const char*)smsc,(const char*)msg,charset,soc_id,curr_pack,total_pack);
     /*lei modify for gsw 2022/5/11*/
+    pthread_mutex_unlock(&read_sms_mutex);
     p.writeInt32(index);//if index == -1 ,it means sms storage space is full.
     /*Warren change for t800 ril service 2022/1/18 end*/
     return 0;
@@ -969,25 +990,39 @@
         free(pRI);
         return -1;
     }
-    lynq_sms_t smsInfo;
-    error = g_smsManagement->lynq_read_sms_from_memory(atoi(argv[1]),&smsInfo);
+    lynq_sms_t *smsInfo = (lynq_sms_t*) malloc(sizeof(lynq_sms_t));
+    if (smsInfo == NULL)
+    {
+        RLOGE("malloc fail\n");
+        android::lynqAssemblyParcelheader(p,socket_id,pRI->uToken,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,0,2);//RIL_E_GENERIC_FAILURE
+        android::LYNQ_RIL_respSocket(p,(void *)pRI);
+        free(pRI);
+        return -1;
+    }
+
+    pthread_mutex_lock(&read_sms_mutex);
+    LYVERBLOG("lcz_lock smsInfo address %p\n",smsInfo);
+    error = g_smsManagement->lynq_read_sms_from_memory(atoi(argv[1]),smsInfo);
+    pthread_mutex_unlock(&read_sms_mutex);
+
     android::lynqAssemblyParcelheader(p,socket_id,pRI->uToken,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,0,error);
-    RLOGD("addr=%s,smsc=%s,msg=%s\n",smsInfo.address,smsInfo.smsc,smsInfo.sms_text);
+    RLOGD("addr=%s,smsc=%s,msg=%s\n",smsInfo->address,smsInfo->smsc,smsInfo->sms_text);
     if(error==0)
     {
-        p.writeInt32(smsInfo.index);
-        p.writeInt32(smsInfo.lynq_sms_status);
-        p.writeInt32(smsInfo.charset);
+        p.writeInt32(smsInfo->index);
+        p.writeInt32(smsInfo->lynq_sms_status);
+        p.writeInt32(smsInfo->charset);
         /*lei add for gsw 2022/5/12*/
-        p.writeInt32(smsInfo.current);
-        p.writeInt32(smsInfo.total);
+        p.writeInt32(smsInfo->current);
+        p.writeInt32(smsInfo->total);
         /*lei add for gsw 2022/5/12*/
-        writeStringToParcel(p,smsInfo.address);
-        writeStringToParcel(p,smsInfo.smsc);
-        writeStringToParcel(p,smsInfo.sms_text);
+        writeStringToParcel(p,smsInfo->address);
+        writeStringToParcel(p,smsInfo->smsc);
+        writeStringToParcel(p,smsInfo->sms_text);
     }
     android::LYNQ_RIL_respSocket(p,(void *)pRI);
     free(pRI);
+    free(smsInfo);
     return 0;
 }
 int deleteSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
@@ -1002,7 +1037,10 @@
         free(pRI);
         return -1;
     }
+    pthread_mutex_lock(&read_sms_mutex);
+    LYVERBLOG("lcz_lock sms delete\n");
     error = g_smsManagement->lynq_delete_sms_from_memory(atoi(argv[1]));
+    pthread_mutex_unlock(&read_sms_mutex);
     android::lynqAssemblyParcelheader(p,socket_id,pRI->uToken,LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY,0,error);
     android::LYNQ_RIL_respSocket(p,(void *)pRI);
     free(pRI);
@@ -1032,7 +1070,10 @@
         free(pRI);
         return -1;
     }
+    pthread_mutex_lock(&read_sms_mutex);
+    LYVERBLOG("lcz_lock sms list get\n");
     error = g_smsManagement->lynq_list_sms_from_memory(0,&sms_list);//list all
+    pthread_mutex_unlock(&read_sms_mutex);
     bzero(indexBuf,256);
     for(int n=0;n<255;n++)
     {
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-sms/lynq_qser_sms.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-sms/lynq_qser_sms.cpp
index 1990166..7b35e6e 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-sms/lynq_qser_sms.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-sms/lynq_qser_sms.cpp
@@ -21,6 +21,18 @@
     SMS_PARAM_ERR
 };
 
+static void release_wake_lock()
+{
+    int ret = 0;
+    
+    ret = system("echo sms_read > /sys/power/wake_unlock");
+    if(ret != 0)
+    {
+        LYERRLOG("sms_read error !!!");
+    }
+    return;
+}
+
 void *new_sms_thread_recv(void *p)
 {
     int handle = -1;
@@ -65,6 +77,7 @@
             {
                 LYDBGLOG("lynq_read_sms error\n");
             }
+            release_wake_lock();
         }
         else
         {
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-sms/lynq_sms.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-sms/lynq_sms.cpp
index d45ff0a..27431c9 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-sms/lynq_sms.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-sms/lynq_sms.cpp
@@ -178,11 +178,19 @@
         return LYNQ_E_CONFLICT;

     }

     Parcel* p=NULL;

-    int ret=lynq_send_common_request(p,g_wait_time,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,1,"%d",index);

-    if(ret!=0)

+    int ret = 0;

+    for(int count = 0;count < 3;count++)

     {

-        return ret;

+        ret=lynq_send_common_request(p,g_wait_time,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,1,"%d",index);

+        if(ret!=0)

+        {

+            usleep(10000);

+            continue;

+        }

+            

     }

+    if(ret)

+        return ret;

     LYDBGLOG("LYNQ_REQUEST_READ_SMS_FROM_MEMORY SUCCESS!");

     p->readInt32(&index);

     p->readInt32(status);