[feature]:
1.add sms service
[fix]:
1.delete printf

Change-Id: Iaacd7aa743c2f0c64a359f4c95cd6e3de2686f0d
diff --git a/framework/lynq-ril-service/src/commands.h b/framework/lynq-ril-service/src/commands.h
index bdc8736..133fd06 100755
--- a/framework/lynq-ril-service/src/commands.h
+++ b/framework/lynq-ril-service/src/commands.h
@@ -194,6 +194,11 @@
     {"RIL_REQUEST_AUTO_SAVE_SMS_TO_SIM",setAutoSaveSmsToSimFlag,"auto save sms to sim",-1},
     {"RIL_REQUEST_GSM_GET_BROADCAST_LANGUAGE",getGsmBroadcastLanguage, "get broadcast language config",RIL_REQUEST_GSM_GET_BROADCAST_LANGUAGE},
     {"RIL_REQUEST_GSM_SET_BROADCAST_LANGUAGE",setGsmBroadcastLanguage, "set broadcast language config",RIL_REQUEST_GSM_SET_BROADCAST_LANGUAGE},
+    /*Warren change for t800 ril service 2022/1/18 start*/
+    {"LYNQ_REQUEST_READ_SMS_FROM_MEMORY",readSmsToMemory, "write sms to memory",LYNQ_REQUEST_READ_SMS_FROM_MEMORY},
+    {"LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY",deleteSmsToMemory, "write sms to memory",LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY},
+    {"LYNQ_REQUEST_LIST_SMS_FROM_MEMORY",listSmsToMemory, "write sms to memory",LYNQ_REQUEST_LIST_SMS_FROM_MEMORY},
+    /*Warren change for t800 ril service 2022/1/18 end*/
 
     #ifdef C2K_SUPPORT
     {"RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG",getCdmaBroadcastConfig, "get broadcast config",RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG},
diff --git a/framework/lynq-ril-service/src/lynq_interface.h b/framework/lynq-ril-service/src/lynq_interface.h
index f1d0f7c..6179538 100755
--- a/framework/lynq-ril-service/src/lynq_interface.h
+++ b/framework/lynq-ril-service/src/lynq_interface.h
@@ -6,6 +6,12 @@
 #define LYNQ_URC_DATA_CALL_STATUS_IND (LYNQ_URC_VENDOR_BASE+3)
 #define LYNQ_AUTO_ANSWER_CALL (LYNQ_REQUEST_VENDOR_BASE +2)
 #define LYNQ_REQUEST_SET_DTMF_VOLUME (LYNQ_REQUEST_VENDOR_BASE +3)
+
+
+#define LYNQ_REQUEST_WRITE_SMS_TO_MEMORY (LYNQ_REQUEST_VENDOR_BASE +4)
+#define LYNQ_REQUEST_READ_SMS_FROM_MEMORY (LYNQ_REQUEST_VENDOR_BASE + 5)
+#define LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY (LYNQ_REQUEST_VENDOR_BASE + 6)
+#define LYNQ_REQUEST_LIST_SMS_FROM_MEMORY (LYNQ_REQUEST_VENDOR_BASE + 7)
 typedef struct{
     int request;
     int waitTime;
diff --git a/framework/lynq-ril-service/src/makefile b/framework/lynq-ril-service/src/makefile
index 97e66a5..0d72666 100755
--- a/framework/lynq-ril-service/src/makefile
+++ b/framework/lynq-ril-service/src/makefile
@@ -52,6 +52,7 @@
   -I$(LOCAL_PATH)/sms \
   -I$(LOCAL_PATH)/sms/cdma \
   -I$(LOCAL_PATH)/sms/gsm \
+  -I$(LOCAL_PATH)/sms/lynqSmsManager \
   -I$(LOCAL_PATH)/atci \
   -I$(LOCAL_PATH)/stateManager \
   -I$(LOCAL_PATH)/ecall/ \
@@ -107,7 +108,7 @@
 endif
 
 
-SOURCES = $(wildcard util/*.cpp *.cpp ecall/*.cpp ecall/gost/*.cpp ecall/gost/utils/*.cpp ecall/gost/sslp/*.cpp ecall/gost/sslp/auth/*.cpp ecall/gost/sslp/firmware/*.cpp ecall/gost/sslp/commands/*.cpp ecall/gost/sslp/ecall/*.cpp ecall/gost/sslp/teledata/*.cpp data/*.cpp  em/rfdesense/*.cpp em/networkinfo/*.cpp em/*.cpp sms/*.cpp sms/gsm/*.cpp sms/cdma/*.cpp atci/*.cpp stateManager/*.cpp)
+SOURCES = $(wildcard util/*.cpp *.cpp ecall/*.cpp ecall/gost/*.cpp ecall/gost/utils/*.cpp ecall/gost/sslp/*.cpp ecall/gost/sslp/auth/*.cpp ecall/gost/sslp/firmware/*.cpp ecall/gost/sslp/commands/*.cpp ecall/gost/sslp/ecall/*.cpp ecall/gost/sslp/teledata/*.cpp data/*.cpp  em/rfdesense/*.cpp em/networkinfo/*.cpp em/*.cpp sms/*.cpp sms/gsm/*.cpp sms/cdma/*.cpp atci/*.cpp stateManager/*.cpp sms/lynqSmsManager/*.cpp)
 
 EXECUTABLE = lynq-ril-service
 
diff --git a/framework/lynq-ril-service/src/ril.cpp b/framework/lynq-ril-service/src/ril.cpp
index 0bf1c6e..612d680 100755
--- a/framework/lynq-ril-service/src/ril.cpp
+++ b/framework/lynq-ril-service/src/ril.cpp
@@ -73,6 +73,8 @@
 /*Warren add for t800 RIL service 2021_12_10 start*/
 #include <sys/epoll.h>
 #include "lynq_interface.h"
+#include "lynq_common.h"
+#include "lynq_sms_manager.h"
 /*Warren add for t800 RIL service 2021_12_10 end*/
 
 #define LOG_TAG "DEMO_RIL"
@@ -4261,6 +4263,8 @@
     int64_t timeReceived = 0;
     bool shouldScheduleTimeout = false;
     RIL_RadioState newState;
+    int temp = 0;
+    char *tempChar=NULL;
     RIL_SOCKET_ID soc_id = (RIL_SOCKET_ID)Phone_utils::get_enable_sim_for_dsss();
 
 #if defined(ANDROID_MULTI_SIM)
@@ -4330,7 +4334,6 @@
     appendPrintBuf("[UNSL][SIM%d]< %s", soc_id, requestToString(unsolResponse));
 
     Parcel p;
-
     p.writeInt32 (RESPONSE_UNSOLICITED);
     p.writeInt32 (unsolResponse);
     /*Warren add for t800 ril service 2021/12/16 start*/
@@ -4376,10 +4379,7 @@
 #if VDBG
     RLOGI("%s UNSOLICITED: %s length:%d", rilSocketIdToString(soc_id), requestToString(unsolResponse), p.dataSize());
 #endif
-    /*Warren add for t800 ril service 2021/12/16 start*/
-    ret = LYNQ_RIL_urcBroadcast(p);
-    /*Warren add for t800 ril service 2021/12/16 end*/
-    ret = sendResponse(p, soc_id);
+    //ret = sendResponse(p, soc_id);
 
 //unsol trigger other things.
     switch(unsolResponse) {
@@ -4396,7 +4396,7 @@
         getVoiceAndDataRegistrationState(soc_id);
         break;
     case RIL_UNSOL_RESPONSE_NEW_SMS:
-        responseNewSMS((const char*)data, datalen,soc_id);
+        responseNewSMS((const char*)data, datalen,soc_id,unsolResponse,p);
         unreadStatusWriteSMSToSim((const char*)data, datalen, soc_id);
         sendSMSACK(soc_id);
         break;
@@ -4530,6 +4530,9 @@
     default:
         break;
     }
+    /*Warren add for t800 ril service 2021/12/16 start*/
+    ret = LYNQ_RIL_urcBroadcast(p);
+    /*Warren add for t800 ril service 2021/12/16 end*/
 #if 0
     if (ret != 0 && unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
 
@@ -4848,6 +4851,10 @@
         /*LYNQ CMD*/
         case LYNQ_AUTO_ANSWER_CALL: return "LYNQ_AUTO_ANSWER_CALL";
         case LYNQ_REQUEST_SET_DTMF_VOLUME: return "RIL_REQUEST_SET_DTMF_VOLUME";
+        case LYNQ_REQUEST_WRITE_SMS_TO_MEMORY: return "LYNQ_REQUEST_WRITE_SMS_TO_MEMORY";
+        case LYNQ_REQUEST_READ_SMS_FROM_MEMORY: return "LYNQ_REQUEST_READ_SMS_FROM_MEMORY";
+        case LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY: return "LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY";
+        case LYNQ_REQUEST_LIST_SMS_FROM_MEMORY: return "LYNQ_REQUEST_LIST_SMS_FROM_MEMORY";
         default: return "<unknown request>";
     }
 }
@@ -5104,6 +5111,12 @@
         case RIL_REQUEST_QUERY_SIM_RETRY_COUNT: return "RIL_REQUEST_QUERY_SIM_RETRY_COUNT";
         case RIL_REQUEST_QUERY_EID: return "RIL_REQUEST_QUERY_EID";
         case LYNQ_REQUEST_SET_DTMF_VOLUME: return "RIL_REQUEST_SET_DTMF_VOLUME";
+        /*warren add for t800 ril service 2022/1/22 start*/
+        case LYNQ_REQUEST_WRITE_SMS_TO_MEMORY: return "LYNQ_REQUEST_WRITE_SMS_TO_MEMORY";
+        case LYNQ_REQUEST_READ_SMS_FROM_MEMORY: return "LYNQ_REQUEST_READ_SMS_FROM_MEMORY";
+        case LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY: return "LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY";
+        case LYNQ_REQUEST_LIST_SMS_FROM_MEMORY: return "LYNQ_REQUEST_LIST_SMS_FROM_MEMORY";
+        /*warren add for t800 ril service 2022/1/22 end*/
         default: return "<unknown request>";
     }
 }
@@ -5732,14 +5745,11 @@
     int dataSize = p.dataSize();
     const uint8_t* data = p.data();
     RLOGD("lynqSocketSend RESPONSE!!!! ");
-    printf("lynqSocketSend RESPONSE!!!!\n");
     sent = sendto(server_socket_fd, data, dataSize, 0, (struct sockaddr *)&pRI->uClient_addr, sizeof(pRI->uClient_addr));
     if( sent < 0 )
     {
         RLOGE("lynqSocketSend send datalen fail (sent=%d, sendFD=%d, dataSize=%d)",
                 sent,server_socket_fd, dataSize);
-        printf("lynqSocketSend send datalen fail (sent=%d, sendFD=%d, dataSize=%d)",
-        sent,server_socket_fd, dataSize);
         return -1;
     }
     return 0;
@@ -5760,15 +5770,12 @@
         return -1;
     }
     RLOGD("lynqSocketSend RESPONSE!!!! ");
-    printf("lynqSocketSend RESPONSE!!!!\n");
 
     sent = sendto(server_socket_fd, data, dataSize, 0, (struct sockaddr *)&pRI->uClient_addr, sizeof(pRI->uClient_addr));
     if( sent < 0 )
     {
         RLOGE("lynqSocketSend send datalen fail (sent=%d, sendFD=%d, dataSize=%d)",
                 sent,server_socket_fd, dataSize);
-        printf("lynqSocketSend send datalen fail (sent=%d, sendFD=%d, dataSize=%d)",
-        sent,server_socket_fd, dataSize);
         return -1;
     }
     return 0;
@@ -5782,6 +5789,7 @@
     ssize_t sent = 0;
     uint8_t dataLength[4];
     int verify = 0x55aa;
+    p.setDataPosition(0);
     int dataSize = p.dataSize();
     const uint8_t* data = p.data();
     sent = sendto(lynq_urc_socket_fd, data, dataSize, 0, (struct sockaddr *)&urc_broadcast_addr, sizeof(urc_broadcast_addr));
@@ -6259,6 +6267,7 @@
 void RIL_startEventLoop(void)
 {
     RLOGD("RIL_startEventLoop()");
+    lynq_init_sms_manager();
     /* spin up eventLoop thread and wait for it to get started */
     s_started = 0;
     pthread_mutex_lock(&s_startupMutex);
diff --git a/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp b/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp
new file mode 100755
index 0000000..d5e3aee
--- /dev/null
+++ b/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.cpp
@@ -0,0 +1,453 @@
+#include <pthread.h>

+#include "lynq_sms_manager.h"

+#include <liblog/lynq_deflog.h>

+#include <sqlite3.h>

+#include <log/log.h>

+#include <stdlib.h>

+#include <string.h>

+#include "common.h"

+sms_manager * g_smsManagement;

+int sms_indexs[LYNQ_MAX_SMS_LIST]={0};

+int next_index = 1;

+sms_manager::sms_manager()

+{

+    set_sms_total(0);

+    return;

+}

+sms_manager:: ~ sms_manager(){}

+int lynq_sms_callback(void *data, int argc, char **argv, char **azColName){

+    int i;

+    if(next_index < LYNQ_MAX_SMS_LIST)

+    {

+        sms_indexs[next_index] = atoi(argv[0]);

+        next_index++;

+    }

+    LYVERBLOG("next_index=%d",next_index);

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

+       LYDBGLOG("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");

+    }

+    return 0;

+}

+int read_sms_from_memory_cb(void *data, int argc, char **argv, char **azColName)

+{

+    if(data==NULL)

+    {

+        return 1;

+    }

+    sms_indexs[next_index] = atoi(argv[0]);

+    next_index++;

+    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");

+    }

+    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);

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

+    memcpy(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);

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

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

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

+    return 0;

+}

+

+/*

+int list_sms_from_memory_cb(void *data, int argc, char **argv, char **azColName)

+{

+    if(data==NULL)

+    {

+        return 1;

+    }

+    lynq_sms_list_t* temp = (lynq_sms_list_t *)data;

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

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

+    }

+}

+*/

+int callback(void *NotUsed, int argc, char **argv, char **azColName){

+   int i;

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

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

+   }

+   return 0;

+}

+

+int sms_manager::find_unuse_sms_index(char *path)

+{

+    char *zErrMsg = 0;

+    int rc;

+    int count =1;

+    bool sms_usable = false;

+    char *sql;

+    //const char* data = "Callback function called";

+    //int indexs[LYNQ_MAX_SMS_LIST]={0};

+    memset(sms_indexs,0,LYNQ_MAX_SMS_LIST);

+    next_index = 1;

+    LYVERBLOG("index = %p",sms_indexs);

+    /* Open database */

+    rc = sqlite3_open(SMS_DB_PATH, &smsDb);

+    if( rc )

+    {

+       LYERRLOG("Can't open database: %s\n", sqlite3_errmsg(smsDb));

+       return -1;

+    }

+    else

+    {

+       LYDBGLOG("Opened database successfully\n");

+    }

+

+    /* Create SQL statement */

+    sql = "SELECT ID from LYNQSMS";

+    /* Execute SQL statement */

+    //int* temp_index = indexs;

+    rc = sqlite3_exec(smsDb, sql, lynq_sms_callback,NULL, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

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

+        sqlite3_free(zErrMsg);

+        return -1;

+    }

+    else

+    {

+       LYDBGLOG("Operation done successfully\n");

+    }

+    //indexs = temp_index;

+    LYVERBLOG("index = %p",sms_indexs);

+    for(count;count<=LYNQ_MAX_SMS_LIST;count++)

+    {

+        LYVERBLOG("indexs[%d]=%d",count,sms_indexs[count]);

+        if(sms_indexs[count]!=count)

+        {

+            sms_usable=true;

+            break;

+        }

+    }

+    if((!sms_usable)&&(count==LYNQ_MAX_SMS_LIST))

+    {

+       set_sms_full(true);

+    }

+    sqlite3_close(smsDb);

+    return count;

+}

+int sms_manager::lynq_write_sms_to_memory(const char* data,const char *num,const char *smsc,const char *msg,const int charset,const int slot_id)

+{

+    int id = find_unuse_sms_index(SMS_DB_PATH);

+    RLOGD("[%s] id = %d\n",__FUNCTION__,id);

+    //lynq_sms_list_t * sms_list = (lynq_sms_list_t * )malloc(sizeof(lynq_sms_list_t *));

+    if(!get_sms_full())

+    {

+        RLOGD("[%s] sms full is false\n",__FUNCTION__);

+        lynq_write_sms_to_sms_db(id, slot_id,1,smsc, charset,data,msg,SMS_DB_PATH,num);

+        return id;//sms story index

+    }

+    LYERRLOG("sms storage space is full!!!");

+    return -1;

+}

+int sms_manager::lynq_delete_sms_from_memory(const int index)

+{

+    char *zErrMsg = 0;

+    int rc;

+    char sql[128]={0};

+    /* Open database */

+    rc = sqlite3_open(SMS_DB_PATH, &smsDb);

+    if( rc )

+    {

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

+        return LYNQ_E_SMS_DB_FAIL;

+    }

+    LYVERBLOG("[%s] Opened database successfully\n",__FUNCTION__);

+    /* Create SQL statement */

+    //sql = "SELECT * from LYNQSMS";

+    sprintf(sql,"DELETE  from LYNQSMS WHERE ID=%d",index);

+   /* Execute SQL statement */

+    rc = sqlite3_exec(smsDb, sql, NULL, NULL, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

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

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return LYNQ_E_SMS_SQL_FAIL;

+    }

+    LYVERBLOG("delete sms %d successfully\n",index);

+    sqlite3_close(smsDb);

+    return 0;

+}

+int sms_manager::lynq_read_sms_from_memory(const int index, lynq_sms_t *sms)

+{

+    char *zErrMsg = 0;

+    int rc;

+    char sql[128]={0};

+    /* Open database */

+    rc = sqlite3_open(SMS_DB_PATH, &smsDb);

+    if( rc )

+    {

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

+        return LYNQ_E_SMS_DB_FAIL;

+    }

+    LYVERBLOG("[%s] Opened database successfully\n",__FUNCTION__);

+    /* Create SQL statement */

+    //sql = "SELECT * from LYNQSMS";

+    sprintf(sql,"SELECT * from LYNQSMS WHERE ID=%d",index);

+   /* Execute SQL statement */

+    rc = sqlite3_exec(smsDb, sql, read_sms_from_memory_cb, (void *)sms, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

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

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return LYNQ_E_SMS_SQL_FAIL;

+    }

+    memset(sql,0,128);

+    sprintf(sql,"UPDATE LYNQSMS set STATUS = 2 WHERE ID=%d",index);

+    rc = sqlite3_exec(smsDb, sql, NULL,NULL, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

+        LYVERBLOG("update SQL error: %s\n", zErrMsg);

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return LYNQ_E_SMS_SQL_FAIL;

+    }

+    LYVERBLOG("read sms %d successfully\n",index);

+    sqlite3_close(smsDb);

+    return 0;

+}

+int sms_manager::lynq_list_sms_from_memory(const int status,lynq_sms_list_t * sms_list)

+{

+    if(sms_list==NULL)

+    {

+        return 1;

+    }

+    char *zErrMsg = 0;

+    int rc;

+    char sql[128]={0};

+    memset(sms_indexs,0,LYNQ_MAX_SMS_LIST);

+    next_index = 0;

+    /* Open database */

+    rc = sqlite3_open(SMS_DB_PATH, &smsDb);

+    if( rc )

+    {

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

+        return LYNQ_E_SMS_DB_FAIL;

+    }

+    LYVERBLOG("[%s] Opened database successfully\n",__FUNCTION__);

+    /* Create SQL statement */

+    //sql = "SELECT * from LYNQSMS";

+    if(status==0)

+    {

+        sprintf(sql,"SELECT ID from LYNQSMS");

+    }

+    else

+    {

+        sprintf(sql,"SELECT ID from LYNQSMS WHERE STATUS=%d",status);

+    }

+   /* Execute SQL statement */

+    rc = sqlite3_exec(smsDb, sql, lynq_sms_callback,NULL, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

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

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return LYNQ_E_SMS_SQL_FAIL;

+    }

+    for(int i=0;i<next_index;i++)

+    {

+        sms_list->index[i]=sms_indexs[i];

+    }

+    sms_list->num_of_indexs = next_index;

+    for(int i = 0;i<next_index;i++)

+    {

+        printf("test index =%d\n",sms_list->index[i]);

+    }

+    LYVERBLOG("list sms successfully");

+    sqlite3_close(smsDb);

+    return 0;

+}

+int has_sms_db_created(char *path,sqlite3* smsDb_l)

+{

+    if (path == NULL || strlen(path) == 0)

+    {

+        LYVERBLOG("initSmsDb error with null path!");

+        return SMS_DB_ERR;

+    }

+    int rc = sqlite3_open_v2(path, &smsDb_l, SQLITE_OPEN_READWRITE,NULL);

+    if (rc == SQLITE_OK) 

+    {

+        LYVERBLOG("check init success!");

+        return SMS_DB_READED;

+    } 

+    else 

+    {

+        LYVERBLOG("db has not create %s!", sqlite3_errmsg(smsDb_l));

+        sqlite3_close_v2(smsDb_l);

+        return SMS_DB_ERR;

+    }

+}

+int sms_manager::create_sms_db(char *path)

+{

+    if (path == NULL || strlen(path) == 0) {

+        LYVERBLOG("initSmsDb error with null path!");

+        return SMS_DB_ERR;

+    }

+    if(SMS_DB_READED==has_sms_db_created(path,smsDb))

+    {

+        return SMS_DB_READED;

+    }

+    int rc = sqlite3_open(path, &smsDb);

+    if (rc != SQLITE_OK) {

+        LYVERBLOG("initDb failed %s", sqlite3_errcode(smsDb));

+        return SMS_DB_ERR;

+    } else {

+        LYVERBLOG("create db in %s", path);

+    }

+    sqlite3_close(smsDb);

+    return SMS_DB_CREATE;

+}

+int sms_manager::create_sms_table(char*path)

+{

+    char *zErrMsg = 0;

+    int rc;

+    char *sql;

+

+    /* Open database */

+    rc = sqlite3_open(path, &smsDb);

+    if( rc )

+    {

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

+        return 1;

+    }

+    LYVERBLOG("[%s] Opened database successfully\n",__FUNCTION__);

+    /* Create SQL statement */

+    sql = "CREATE TABLE LYNQSMS(" \

+         "ID                       INT PRIMARY KEY      NOT NULL," \

+         "SLOT                 INT                                 NOT NULL," \

+         "STATUS            INT                                 NOT NULL," \

+         "ADDRESS         NONE                                 NOT NULL," \

+         "SMSC                NONE                             NOT NULL," \

+         "CHARSET         INT                                 NOT NULL," \

+         "PDU                   NONE                             NOT NULL," \

+         "CONTENT         NONE                             NOT NULL);";

+   /* Execute SQL statement */

+    rc = sqlite3_exec(smsDb, sql, NULL, 0, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

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

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return 1;

+    }

+    LYVERBLOG("Table LYNQSMS created successfully\n");

+    sqlite3_close(smsDb);

+    return 0;

+}

+int sms_manager::create_sms_number_table(char * path)

+{

+    char *zErrMsg = 0;

+    int rc;

+    char *sql;

+

+    /* Open database */

+    rc = sqlite3_open(path, &smsDb);

+    if( rc )

+    {

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

+        return 1;

+    }

+    LYVERBLOG("[%s] Opened database successfully",__FUNCTION__);

+    /* Create SQL statement */

+    sql = "CREATE TABLE LYNQ_SMS_TOTAL(" \

+         "ID                      INT PRIMARY KEY      NOT NULL," \

+         "TOTAL             INT                                 NOT NULL);";

+   /* Execute SQL statement */

+    rc = sqlite3_exec(smsDb, sql, callback, 0, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

+        LYVERBLOG("SQL error: %s", zErrMsg);

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return 1;

+    }

+    LYDBGLOG("LYNQ_SMS_TOTAL table created successfully");

+    sqlite3_close(smsDb);

+    return 0;

+}

+int sms_manager::lynq_write_sms_to_sms_db(int id,int slot_id,int status,const char *smsc,int charset,const char *pdu,const char *content,char *path,const char*address)

+{

+    char *zErrMsg = 0;

+    int rc;

+    char sql[LYNQ_SMS_MAX_SCA_NUMBER_LEN+LYNQ_SMS_MAX_PDU_NUMBER_LEN+LYNQ_SMS_MAX_TEXT_NUMBER_LEN+3+1+100]={0};

+    /* Open database */

+    rc = sqlite3_open(path, &smsDb);

+    if( rc )

+    {

+        LYERRLOG("[%s] Can't open database: %s",__FUNCTION__,sqlite3_errmsg(smsDb));

+        return 1;

+    }

+    LYDBGLOG("[%s] Opened database successfully",__FUNCTION__);

+    /* Create SQL statement */

+    sprintf(sql,"INSERT INTO LYNQSMS (ID,SLOT,STATUS,ADDRESS,SMSC,CHARSET,PDU,CONTENT) VALUES (%d,%d,%d,'%s','%s',%d,'%s','%s');",id,slot_id,status,address,smsc,charset,pdu,content);

+    /* Execute SQL statement */

+    //printf("%s\n",sql);

+    rc = sqlite3_exec(smsDb, sql, NULL,NULL, &zErrMsg);

+    if( rc != SQLITE_OK )

+    {

+        LYVERBLOG( "SQL error: %s", zErrMsg);

+        sqlite3_free(zErrMsg);

+        sqlite3_close(smsDb);

+        return 1;

+    }

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

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

+    return 0;

+}

+int sms_manager::initSmsManager()

+{

+    int db_ret=0;

+    int table_ret=0;

+    db_ret = create_sms_db(SMS_DB_PATH);

+    if(db_ret==SMS_DB_CREATE)

+    {

+        table_ret = create_sms_table(SMS_DB_PATH);

+        if(table_ret!=0)

+        {

+            LYERRLOG("create sms table fail!!!");

+            return 1;

+        }

+        table_ret = create_sms_number_table(SMS_DB_PATH);

+        if(table_ret!=0)

+        {

+            LYERRLOG("create sms number table fail!!!");

+            return 1;

+        }

+    }

+    else if(db_ret==SMS_DB_READED)

+    {

+        LYDBGLOG("[%s] sms db has be build!!!",__FUNCTION__);

+    }

+    else

+    {

+        LYDBGLOG("init sms db fail!!!");

+    }

+    set_sms_full(false);

+    //get_sms_total();

+    return 0;

+}

+

+int lynq_init_sms_manager()

+{

+    g_smsManagement = new sms_manager();

+    if(g_smsManagement==NULL)

+    {

+        RLOGE("init class sms manager fail!!!");

+        exit(EXIT_FAILURE);

+    }

+    RLOGD("init class sms manager success!!!");

+    g_smsManagement->initSmsManager();

+    return 0;

+}

+

+

diff --git a/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.h b/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.h
new file mode 100755
index 0000000..5c68d06
--- /dev/null
+++ b/framework/lynq-ril-service/src/sms/lynqSmsManager/lynq_sms_manager.h
@@ -0,0 +1,114 @@
+/*============================================================================= 

+#     FileName: lynq_sms_manager.cpp

+#     Desc: about SMS API

+#     Author: Warren

+#     Version: V1.0

+#     LastChange: 2021-08-27 

+#     History: 

+=============================================================================*/

+#ifndef __LYNQ_SMS_MANAGER__

+#define __LYNQ_SMS_MANAGER__

+#include <sqlite3.h>

+#ifdef __cplusplus

+extern "C" {

+#endif

+#define LYNQ_MAX_SMS_LIST 255

+#define LYNQ_SMS_MAX_SCA_NUMBER_LEN   22

+#define LYNQ_SMS_MAX_PDU_NUMBER_LEN 2504

+#define LYNQ_SMS_MAX_TEXT_NUMBER_LEN 255

+#define LYNQ_TELEPHONE_NUM_LEN 64

+#define SMS_DB_PATH "/system/etc/tele/sms.db"

+#define SMS_DB_READED 0

+#define SMS_DB_ERR 1

+#define SMS_DB_CREATE 2

+

+typedef enum {

+    LYNQ_SMS_GSM=0,

+    LYNQ_SMS_CDMA

+}lynq_sms_technology_t;

+typedef enum{

+    LYNQ_SMS_ALL=0,

+    LYNQ_SMS_UREA,

+    LYNQ_SMS_READ,

+    LYNQ_SMS_UNSENT,

+    LYNQ_SMS_SENT

+}lynq_sms_status_t;

+

+typedef struct

+{

+    int index;//Maximum number of SMS that can be storaged.

+    int slot_id;

+    int charset;

+    char address[LYNQ_TELEPHONE_NUM_LEN];

+    int addrLen;

+    char smsc[LYNQ_SMS_MAX_SCA_NUMBER_LEN];

+    int smscLen;

+    char pdu[LYNQ_SMS_MAX_PDU_NUMBER_LEN];

+    int pduLen;

+    char sms_text[LYNQ_SMS_MAX_TEXT_NUMBER_LEN];

+    int sms_text_len;

+    lynq_sms_technology_t lynq_sms_technology;

+    lynq_sms_status_t lynq_sms_status;

+}lynq_sms_t;

+

+typedef struct{

+    int index[LYNQ_MAX_SMS_LIST];

+    int num_of_indexs;

+}lynq_sms_list_t;

+class sms_manager

+{

+    public:

+        sms_manager(void );

+        virtual ~ sms_manager();

+        int initSmsManager();

+        int lynq_write_sms_to_memory(const char* data,const char *num,const char *smsc,const char *msg,const int charset,const int slot_id);

+        int lynq_delete_sms_from_memory(const int index);

+        int lynq_read_sms_from_memory(const int index, lynq_sms_t *sms);

+        int lynq_list_sms_from_memory(const int status,lynq_sms_list_t * sms_list);

+        int get_sms_total()

+        {

+            return sms_total;

+        }

+        int set_sms_total(int smsTotal)

+        {

+            sms_total = smsTotal;

+            return 0;

+        }

+        bool get_sms_full()

+        {

+            return sms_full;

+        }

+        int set_sms_full(bool status)

+        {

+            sms_full = status;

+            return 0;

+        }

+        int get_sms_usable_id()

+        {

+            return sms_usable_id;

+        }

+        int set_sms_usable_id(int index)

+        {

+             sms_usable_id = index;

+             return 0;

+        }

+    private:

+        int create_sms_db(char *path);

+        int create_sms_table(char*path);

+        int create_sms_number_table(char * path);

+        int lynq_write_sms_to_sms_db(int id,int slot_id,int status,const char *smsc,int charset,const char *pdu,const char *content,char *path,const char*address);

+        int find_unuse_sms_index(char *path);

+        int sms_total;

+        int sms_usable_id;

+        bool sms_full;

+        sqlite3* smsDb;

+};

+extern sms_manager *g_smsManagement;

+int lynq_init_sms_manager();

+#ifdef __cplusplus

+}

+#endif

+

+#endif

+

+

diff --git a/framework/lynq-ril-service/src/sms/sms.cpp b/framework/lynq-ril-service/src/sms/sms.cpp
index d7f0055..fe11515 100755
--- a/framework/lynq-ril-service/src/sms/sms.cpp
+++ b/framework/lynq-ril-service/src/sms/sms.cpp
@@ -43,6 +43,14 @@
 #include "common.h"
 #include "ecall/eCall.h"
 #include "util/utils.h"
+/*Warren add for T800 platform 2021/11/19 start*/
+#include "lynq_common.h"
+#include "lynq_sms_manager.h"
+#include "lynq_interface.h"
+#include <cutils/jstring.h>//temp
+
+/*Warren add for T800 platform 2021/11/19 end*/
+
 
 #define GSM_PHONE 1
 
@@ -51,6 +59,8 @@
 
 #define PROP_ECALL_NUM "vendor.gost.ecall.ecall_sms_fallback_number"
 
+extern sms_manager * g_smsManagement;
+
 static void constructGsmSendSmsRilRequest (android::Parcel &p, char *smscPDU, char *pdu) {
     p.writeInt32(2);
     writeStringToParcel(p, (const char *)smscPDU);
@@ -117,6 +127,11 @@
                 pRI_backup->pCI = pRI->pCI;
                 pRI_backup->socket_id = pRI->socket_id;
                 pRI_backup->p_next = pRI->p_next;
+                pRI_backup->uToken = pRI->uToken;
+                pRI_backup->lynqEvent = pRI->lynqEvent;
+                pRI_backup->uClient_addr.sin_family = pRI->uClient_addr.sin_family;
+                pRI_backup->uClient_addr.sin_addr.s_addr = pRI->uClient_addr.sin_addr.s_addr;
+                pRI_backup->uClient_addr.sin_port = pRI->uClient_addr.sin_port;
                 constructGsmSendSmsRilRequest(p, smscPDU, pdu[index]);
                 p.setDataPosition(pos);
                 pRI->pCI->dispatchFunction(p, pRI_backup);
@@ -509,20 +524,26 @@
     return;
 }
 
-int responseNewSMS(const char *data, size_t datalen, int soc_id){
+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};
     char num[512] = {0};
     int charset = 0;
+    int index = -1;
     RLOGD("slot: %d, len: %d, sms: %s",soc_id, datalen, data);
     smsPduDecode(data, datalen, num, smsc, msg, &charset);
-    printf("[EVENT][MT_SMS][SIM%d]PDU decode:smsc: %s, phone number: %s ,charset: %d, msg_len: %d, message content: %s\n", soc_id, smsc, num, charset, strlen(msg), msg);
+    RLOGD("SMS :%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__);
     RLOGD("[EVENT][MT_SMS][SIM%d]PDU decode:smsc: %s, phone number: %s , charset: %d, msg_len: %d, message content: %s", soc_id, smsc, num, charset, strlen(msg), msg);
     if(isGostEcall() && (MDAPI_SMS_CHARSET_GSM_8BIT == charset))
     {
         gostParseSmsHandle(soc_id, num, msg);
     }
-
+    /*Warren change for t800 ril service 2022/1/18 start
+    **By default, the SMS is stored in the memory after receiving it
+    */
+    index = g_smsManagement->lynq_write_sms_to_memory(data,(const char*)num,(const char*)smsc,(const char*)msg,charset,soc_id);
+    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;
 }
 
@@ -904,4 +925,102 @@
     pRI->pCI->dispatchFunction(p, pRI);
     return 0;
 }
+/*Warren change for t800 ril service 2022/1/18 start*/
+int readSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
+{
+    android::Parcel p;
+    int error = -1;
+    if(argc != 2)
+    {
+        RLOGE("%s parameter error!",__func__);
+        android::lynqAssemblyParcelheader(p,socket_id,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,0,2);//RIL_E_GENERIC_FAILURE
+        android::LYNQ_RIL_respSocket(p,(void *)pRI);
+        free(pRI);
+        return -1;
+    }
+    lynq_sms_t smsInfo;
+    error = g_smsManagement->lynq_read_sms_from_memory(atoi(argv[1]),&smsInfo);
+    android::lynqAssemblyParcelheader(p,socket_id,LYNQ_REQUEST_READ_SMS_FROM_MEMORY,0,error);
+    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);
+        writeStringToParcel(p,smsInfo.address);
+        writeStringToParcel(p,smsInfo.smsc);
+        writeStringToParcel(p,smsInfo.sms_text);
+    }
+    android::LYNQ_RIL_respSocket(p,(void *)pRI);
+    free(pRI);
+    return 0;
+}
+int deleteSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
+{
+    android::Parcel p;
+    int error = -1;
+    if(argc != 2)
+    {
+        RLOGE("%s parameter error!",__func__);
+        android::lynqAssemblyParcelheader(p,socket_id,LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY,0,2);//RIL_E_GENERIC_FAILURE
+        android::LYNQ_RIL_respSocket(p,(void *)pRI);
+        free(pRI);
+        return -1;
+    }
+    error = g_smsManagement->lynq_delete_sms_from_memory(atoi(argv[1]));
+    android::lynqAssemblyParcelheader(p,socket_id,LYNQ_REQUEST_DELETE_SMS_FROM_MEMORY,0,error);
+    android::LYNQ_RIL_respSocket(p,(void *)pRI);
+    free(pRI);
+    return 0;
+}
+static char *strdupReadString(android::Parcel &p) {
+    size_t stringlen;
+    const char16_t *s16;
+
+    s16 = p.readString16Inplace(&stringlen);
+
+    return strndup16to8(s16, stringlen);
+}
+
+int listSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
+{
+    android::Parcel p;
+    int error = -1;
+    char indexBuf[256]={0};
+    lynq_sms_list_t sms_list;
+    if(argc<1)
+    {
+        RLOGE("%s parameter error!",__func__);
+        android::lynqAssemblyParcelheader(p,socket_id,LYNQ_REQUEST_LIST_SMS_FROM_MEMORY,0,2);//RIL_E_GENERIC_FAILURE
+        android::LYNQ_RIL_respSocket(p,(void *)pRI);
+        free(pRI);
+        return -1;
+    }
+    error = g_smsManagement->lynq_list_sms_from_memory(0,&sms_list);//list all
+    bzero(indexBuf,256);
+    for(int n=0;n<255;n++)
+    {
+        indexBuf[n] = '0';
+    }
+    indexBuf[255]='\0';
+    if(error==0)
+    {
+        for(int i = 0;i < sms_list.num_of_indexs;i++)
+        {
+            if(sms_list.index[i] >= 1 && sms_list.index[i] < 255)
+            {
+                indexBuf[sms_list.index[i]]='1';
+            }
+        }
+        //writeStringToParcel(p,indexBuf);
+    }
+    //printf("indexbuf=%s\n",indexBuf);
+    android::lynqAssemblyParcelheader(p,socket_id,LYNQ_REQUEST_LIST_SMS_FROM_MEMORY,0,error);
+    writeStringToParcel(p,indexBuf);
+    android::LYNQ_RIL_respSocket(p,(void *)pRI);
+    free(pRI);
+    return 0;
+}
+
+/*Warren change for t800 ril service 2022/1/18 end*/
 
diff --git a/framework/lynq-ril-service/src/sms/sms.h b/framework/lynq-ril-service/src/sms/sms.h
index 50f75f2..82f747b 100755
--- a/framework/lynq-ril-service/src/sms/sms.h
+++ b/framework/lynq-ril-service/src/sms/sms.h
@@ -70,7 +70,9 @@
 //RIL_REQUEST_GET_SMSC_ADDRESS
 int getSmscAddress(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
 void sendSMSACK(RIL_SOCKET_ID soc_id);
-int responseNewSMS(const char *data, size_t datalen, int soc_id);
+/*Warren chenge for t800 ril service 2022/1/19 start*/
+int responseNewSMS(const char *data, size_t datalen, int soc_id,int32_t unsol,android::Parcel &p);
+/*Warren chenge for t800 ril service 2022/1/19 end*/
 
 //#ifdef C2K_SUPPORT
 int getCdmaBroadcastConfig(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
@@ -94,4 +96,11 @@
 int gostSendSmsForMsd(int id, char *num, char *msd);
 int getGsmBroadcastLanguage(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
 int setGsmBroadcastLanguage(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
+/*Warren change for t800 ril service 2022/1/18 start*/
+int readSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
+int deleteSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
+int listSmsToMemory(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI);
+/*Warren change for t800 ril service 2022/1/18 end*/
+
+
 #endif