4bf3d015a5336986ceaccee88b4afeca064eb2a4
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginSimMsg.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <errno.h>
18
19 #include "MsgDebug.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgGconfWrapper.h"
23 #include "SmsPluginParamCodec.h"
24 #include "SmsPluginTpduCodec.h"
25 #include "SmsPluginTransport.h"
26 #include "SmsPluginStorage.h"
27 #include "SmsPluginEventHandler.h"
28 #include "SmsPluginCallback.h"
29 #include "MsgUtilStorage.h"
30 #include "MsgNotificationWrapper.h"
31
32 #include "SmsPluginSimMsg.h"
33 #include "SmsPluginDSHandler.h"
34
35
36 /*==================================================================================================
37                                      IMPLEMENTATION OF SmsPluginSimMsg - Member Functions
38 ==================================================================================================*/
39 SmsPluginSimMsg* SmsPluginSimMsg::pInstance = NULL;
40
41
42 SmsPluginSimMsg::SmsPluginSimMsg()
43 {
44         /* Initialize member variables */
45         simMsgId = 0;
46         delSimMsgId = -1;
47         usedCnt = 0;
48         totalCnt = 0;
49         bTapiResult = false;
50         memset(&simMsgDataInfo, 0x00, sizeof(simMsgDataInfo));
51         memset(simIdList, 0, sizeof(int) * MAX_SIM_SMS_NUM);
52         memset(&simMsgCnt, 0x00, sizeof(simMsgCnt));
53         memset(&simMsgInfo, 0x00, sizeof(simMsgInfo));
54         memset(&simAddrInfo, 0x00, sizeof(simAddrInfo));
55 }
56
57
58 SmsPluginSimMsg::~SmsPluginSimMsg()
59 {
60 }
61
62
63 SmsPluginSimMsg* SmsPluginSimMsg::instance()
64 {
65         if (!pInstance)
66                 pInstance = new SmsPluginSimMsg();
67
68         return pInstance;
69 }
70
71
72 void SmsPluginSimMsg::initSimMessage(TapiHandle *handle)
73 {
74         MSG_BEGIN();
75
76         char keyName[MAX_VCONFKEY_NAME_LEN] = {0, };
77         int sim_idx = SmsPluginDSHandler::instance()->getSimIndex(handle);
78
79         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, sim_idx);
80         /* Set SIM count of vconf to 0 */
81         if (MsgSettingSetInt(keyName, 0) != MSG_SUCCESS) {
82                 MSG_DEBUG("Error to set config data [%s]", keyName);
83         }
84         memset(keyName, 0, sizeof(keyName));
85         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_TOTAL_COUNT, sim_idx);
86
87         if (MsgSettingSetInt(keyName, 0) != MSG_SUCCESS) {
88                 MSG_DEBUG("Error to set config data [%s]", keyName);
89         }
90
91         MSG_SIM_COUNT_S tmpMsgCnt;
92         memset(&tmpMsgCnt, 0x00, sizeof(MSG_SIM_COUNT_S));
93         getSimMsgCount(handle, &tmpMsgCnt);
94
95         MSG_MESSAGE_INFO_S tmpMsgInfo;
96         int simIdList[MAX_SIM_SMS_NUM];
97         int unreadSimMsg = 0;
98
99         for (int i = 0; i < tmpMsgCnt.usedCount; i++) {
100                 memset(&tmpMsgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
101                 memset(simIdList, 0, sizeof(int) * MAX_SIM_SMS_NUM);
102
103                 /* Get SIM Msg */
104                 if (getSimMsg(handle, tmpMsgCnt.indexList[i], &tmpMsgInfo, simIdList) == false)
105                         continue;
106
107                 if (tmpMsgInfo.bRead == false)
108                         unreadSimMsg++;
109
110                 if (SmsPluginEventHandler::instance()->handleSimMsg(&tmpMsgInfo, simIdList, NULL, MAX_SIM_SMS_NUM) < 0) {
111                         MSG_DEBUG("Fail to handleSimMsg()");
112                 }
113
114                 if (tmpMsgInfo.addressList) {
115                         free(tmpMsgInfo.addressList);
116                         tmpMsgInfo.addressList = NULL;
117                 }
118         }
119
120         MSG_DEBUG("Unread SIM message count = [%d]", unreadSimMsg);
121 #ifndef MSG_NOTI_INTEGRATION
122         if (unreadSimMsg > 0) {
123                 MsgRefreshNotification(MSG_NOTI_TYPE_SIM, true, MSG_ACTIVE_NOTI_TYPE_NONE);
124         }
125 #endif
126
127         if (SmsPluginEventHandler::instance()->updateIMSI(sim_idx) != MSG_SUCCESS) {
128                 MSG_ERR("Fail to handleSimMsg()");
129         }
130
131         MSG_END();
132 }
133
134
135 msg_error_t SmsPluginSimMsg::saveSimMessage(const MSG_MESSAGE_INFO_S *pMsgInfo, SMS_SIM_ID_LIST_S *pSimIdList)
136 {
137         bool bSimSst = true;
138
139         TapiHandle *handle = SmsPluginDSHandler::instance()->getTelHandle(pMsgInfo->sim_idx);
140         char keyName[MAX_VCONFKEY_NAME_LEN];
141         memset(keyName, 0x00, sizeof(keyName));
142         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SERVICE_TABLE, pMsgInfo->sim_idx);
143         if (MsgSettingGetBool(keyName, &bSimSst) != MSG_SUCCESS)
144                 MSG_DEBUG("MsgSettingGetBool [%s] failed", keyName);
145                 /* No return, default value is true. */
146
147         if (bSimSst == false)
148                 return MSG_ERR_STORE_RESTRICT;
149
150         /* Reset Out Parameter */
151         pSimIdList->count = 0;
152
153         SMS_TPDU_S tpdu;
154         memset(&tpdu, 0x00, sizeof(SMS_TPDU_S));
155         if (pMsgInfo->direction == MSG_DIRECTION_TYPE_MO) {
156                 tpdu.tpduType = SMS_TPDU_SUBMIT;
157                 tpdu.data.submit.dcs.msgClass = SMS_MSG_CLASS_NONE;
158                 /*SmsPluginTransport::instance()->setSmsSendOptions(&(tpdu.data.submit));*/
159                 tpdu.data.submit.vpf = SMS_VPF_NOT_PRESENT;
160                 tpdu.data.submit.dcs.codingScheme = SMS_CHARSET_AUTO;
161         } else {
162                 tpdu.tpduType = SMS_TPDU_DELIVER;
163                 setSmsOptions(pMsgInfo, &(tpdu.data.deliver));
164                 /* Set TimeStamp */
165                 convertTimeStamp(pMsgInfo, &(tpdu.data.deliver));
166                 tpdu.data.deliver.dcs.codingScheme = SMS_CHARSET_AUTO;
167         }
168
169         for (int i = 0; i <pMsgInfo->nAddressCnt; ++i) {
170                 SMS_SUBMIT_DATA_S submitData;
171                 memset(&submitData, 0x00, sizeof(SMS_SUBMIT_DATA_S));
172                 int bufLen = 0;
173                 char buf[MAX_TPDU_DATA_LEN];
174
175                 if (pMsgInfo->direction == MSG_DIRECTION_TYPE_MO) { /* SUBMIT MSG */
176                         SmsPluginTransport::instance()->msgInfoToSubmitData(pMsgInfo, &submitData, &(tpdu.data.submit.dcs.codingScheme), i);
177
178                         int addLen = strlen(submitData.destAddress.address);
179
180                         tpdu.data.submit.destAddress.ton = submitData.destAddress.ton;
181                         tpdu.data.submit.destAddress.npi = submitData.destAddress.npi;
182
183                         if (addLen < MAX_ADDRESS_LEN) {
184                                 memcpy(tpdu.data.submit.destAddress.address, submitData.destAddress.address, addLen);
185                                 tpdu.data.submit.destAddress.address[addLen] = '\0';
186                         } else {
187                                 memcpy(tpdu.data.submit.destAddress.address, submitData.destAddress.address, MAX_ADDRESS_LEN);
188                                 tpdu.data.submit.destAddress.address[MAX_ADDRESS_LEN] = '\0';
189                         }
190
191                 } else { /* DELIVER MSG */
192                         SmsPluginTransport::instance()->msgInfoToSubmitData(pMsgInfo, &submitData, &(tpdu.data.deliver.dcs.codingScheme), i);
193
194                         int addLen = strlen(submitData.destAddress.address);
195
196                         tpdu.data.deliver.originAddress.ton = submitData.destAddress.ton;
197                         tpdu.data.deliver.originAddress.npi = submitData.destAddress.npi;
198
199                         if (addLen < MAX_ADDRESS_LEN) {
200                                 memcpy(tpdu.data.deliver.originAddress.address, submitData.destAddress.address, addLen);
201                                 tpdu.data.deliver.originAddress.address[addLen] = '\0';
202                         } else {
203                                 memcpy(tpdu.data.deliver.originAddress.address, submitData.destAddress.address, MAX_ADDRESS_LEN);
204                                 tpdu.data.deliver.originAddress.address[MAX_ADDRESS_LEN] = '\0';
205                         }
206                 }
207
208                 /* Check sim message full. */
209                 if (checkSimMsgFull(pMsgInfo->sim_idx, submitData.segCount) == true) {
210                         MSG_DEBUG("SIM storage is full.");
211                         MsgInsertTicker("Sim memory full. Delete some items", SMS_MESSAGE_SIM_MESSAGE_FULL, true, 0);
212
213                         return MSG_ERR_SIM_STORAGE_FULL;
214                 }
215
216                 if (submitData.segCount > 1) {
217                         if (pMsgInfo->direction == MSG_DIRECTION_TYPE_MO) { /* SUBMIT MSG */
218                                 tpdu.data.submit.bHeaderInd = true;
219                         } else {
220                                 tpdu.data.deliver.bHeaderInd = true;
221                         }
222                 }
223
224                 for (unsigned int segCnt = 0; segCnt < submitData.segCount; segCnt++) {
225                         if (pMsgInfo->direction == MSG_DIRECTION_TYPE_MO) {
226                                 memcpy(&(tpdu.data.submit.userData), &(submitData.userData[segCnt]), sizeof(SMS_USERDATA_S));
227                         } else {
228                                 memcpy(&(tpdu.data.deliver.userData), &(submitData.userData[segCnt]), sizeof(SMS_USERDATA_S));
229                         }
230
231                         memset(buf, 0x00, sizeof(buf));
232
233                         /* Encode SMS-DELIVER TPDU */
234                         bufLen = SmsPluginTpduCodec::encodeTpdu(&tpdu, buf);
235
236                         /* Make Telephony Structure */
237                         TelSmsData_t simSmsData;
238                         memset((void*)&simSmsData, 0x00, sizeof(simSmsData));
239
240                         /* Set TPDU data */
241                         memcpy((void*)simSmsData.SmsData.szData, buf, bufLen);
242
243                         simSmsData.SmsData.szData[bufLen] = 0;
244                         simSmsData.SmsData.MsgLength = bufLen;
245                         simSmsData.SmsData.format = TAPI_NETTEXT_NETTYPE_3GPP;
246
247                         if (pMsgInfo->direction == MSG_DIRECTION_TYPE_MT) { /* MT messages */
248                                 if (pMsgInfo->bRead == true)
249                                         simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_READ;
250                                 else
251                                         simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_UNREAD;
252                         } else { /* MO messages */
253                                 if (pMsgInfo->networkStatus == MSG_NETWORK_SEND_SUCCESS)
254                                         simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_SENT;
255                                 else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS)
256                                         simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_DELIVERED;
257                                 else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_FAIL)
258                                         simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_DELIVERY_UNCONFIRMED;
259                                 else
260                                         simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_UNSENT;
261                         }
262
263                         /* Save SMS in SIM */
264                         int ret = 0;
265
266                         ret = tel_write_sms_in_sim(handle, &simSmsData, TapiEventSaveSimMsg, NULL);
267
268                         if (ret == TAPI_API_SUCCESS) {
269                                 MSG_DEBUG("########  tel_write_sms_in_sim Success !!!#######");
270                         } else {
271                                 MSG_DEBUG("########  tel_write_sms_in_sim Fail !!! return : [%d] #######", ret);
272
273                                 return MSG_ERR_PLUGIN_STORAGE;
274                         }
275
276                         msg_sim_id_t SimId = 0;
277
278                         bool bResult = false;
279
280                         bResult = getSimEvent(&SimId);
281
282                         int usedCnt = 0;
283
284                         if (bResult == true) {
285                                 MSG_DEBUG("########  Saving Msg was Successful !!! SIM ID : [%d] #######", SimId);
286
287                                 char keyName[MAX_VCONFKEY_NAME_LEN];
288                                 memset(keyName, 0x00, sizeof(keyName));
289                                 snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, pMsgInfo->sim_idx);
290                                 if (MsgSettingGetInt(keyName, &usedCnt) != MSG_SUCCESS) {
291                                         MSG_INFO("MsgSettingGetInt() is failed");
292                                 }
293                                 usedCnt++;
294
295                                 if (MsgSettingSetInt(keyName, usedCnt) != MSG_SUCCESS)
296                                         MSG_DEBUG("Error to set config data [%s]", SIM_USED_COUNT);
297
298                                 pSimIdList->simId[pSimIdList->count] = SimId;
299                                 pSimIdList->count++;
300                         } else {
301                                 MSG_DEBUG("########  Saving Msg was Failed !!! SIM ID : [%d] #######", SimId);
302
303                                 return MSG_ERR_PLUGIN_STORAGE;
304                         }
305                 }
306         }
307         return MSG_SUCCESS;
308 }
309
310
311 msg_error_t SmsPluginSimMsg::saveClass2Message(const MSG_MESSAGE_INFO_S *pMsgInfo)
312 {
313         msg_error_t err = MSG_SUCCESS;
314         bool bSimSst = true;
315         int tapiRet = TAPI_API_SUCCESS;
316         int simId = -1;
317         int replaceSimId = -1;
318         int replaceMsgId = 0;
319
320         TapiHandle *handle = SmsPluginDSHandler::instance()->getTelHandle(pMsgInfo->sim_idx);
321
322         char keyName[MAX_VCONFKEY_NAME_LEN];
323         memset(keyName, 0x00, sizeof(keyName));
324         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SERVICE_TABLE, pMsgInfo->sim_idx);
325         if (MsgSettingGetBool(keyName, &bSimSst) != MSG_SUCCESS)
326                 MSG_DEBUG("MsgSettingGetBool [%s] failed", keyName);
327         /* No return, default value is true. */
328
329         if (bSimSst == false) {
330                 SmsPluginTransport::instance()->sendDeliverReport(handle, MSG_SUCCESS);
331                 return MSG_SUCCESS;
332         }
333
334         /* Reset Flag */
335         SMS_TPDU_S tpdu;
336
337         tpdu.tpduType = SMS_TPDU_DELIVER;
338
339         convertTimeStamp(pMsgInfo, &(tpdu.data.deliver));
340
341         /* Set SMS TPDU Options */
342         setSmsOptions(pMsgInfo, &(tpdu.data.deliver));
343
344         SMS_SUBMIT_DATA_S submitData;
345         int bufLen = 0;
346         char buf[MAX_TPDU_DATA_LEN];
347
348         SmsPluginTransport::instance()->msgInfoToSubmitData(pMsgInfo, &submitData, &(tpdu.data.deliver.dcs.codingScheme), 0);
349
350         if (pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS && pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS) {
351                 err = SmsPluginStorage::instance()->getReplaceSimMsg(pMsgInfo, &replaceMsgId, &replaceSimId);
352                 MSG_DEBUG("getReplaceSimMsg(): err=[%d], Replace Sim Id = [%d], Replace message id = [%d]", err, replaceSimId, replaceMsgId);
353                 if (replaceMsgId < 0)
354                         replaceMsgId = 0;
355         }
356
357         /* Check SIM ID */
358         if (replaceSimId < 0) { /* Normal message type */
359                 /* Check sim message full. */
360                 if (checkSimMsgFull(pMsgInfo->sim_idx, submitData.segCount) == true) {
361                         MSG_ERR("SIM storage is full.");
362
363                         SmsPluginTransport::instance()->sendDeliverReport(handle, MSG_ERR_SIM_STORAGE_FULL);
364
365                         return MSG_ERR_SIM_STORAGE_FULL;
366                 }
367
368                 int addLen = strlen(submitData.destAddress.address);
369
370                 tpdu.data.deliver.originAddress.ton = submitData.destAddress.ton;
371                 tpdu.data.deliver.originAddress.npi = submitData.destAddress.npi;
372
373                 if (addLen < MAX_ADDRESS_LEN) {
374                         memcpy(tpdu.data.deliver.originAddress.address, submitData.destAddress.address, addLen);
375                         tpdu.data.deliver.originAddress.address[addLen] = '\0';
376                 } else {
377                         memcpy(tpdu.data.deliver.originAddress.address, submitData.destAddress.address, MAX_ADDRESS_LEN);
378                         tpdu.data.deliver.originAddress.address[MAX_ADDRESS_LEN] = '\0';
379                 }
380
381                 if (submitData.segCount > 1)
382                         tpdu.data.deliver.bHeaderInd = true;
383         } else { /* Replace message type */
384                 tapiRet = tel_delete_sms_in_sim(handle, replaceSimId, TapiEventDeleteSimMsg, NULL);
385
386                 if (tapiRet == TAPI_API_SUCCESS) {
387                         MSG_DEBUG("########  tel_delete_sms_in_sim Success !!! #######");
388
389                         simId = -1;
390                         if (getDelSimEvent(&simId) == true) {
391                                 err = SmsPluginStorage::instance()->deleteSimMessage(pMsgInfo->sim_idx, replaceSimId);
392                         }
393                         MSG_DEBUG("tel_delete_sms_in_sim() : Err=[%d], Replace Sim Id=[%d], Result Sim id=[%d]", err, replaceSimId, simId);
394
395                 } else {
396                         MSG_DEBUG("########  tel_delete_sms_in_sim Fail !!! return : [%d] #######", tapiRet);
397                 }
398         }
399
400         for (unsigned int segCnt = 0; segCnt < submitData.segCount; segCnt++) {
401                 /* Create TelSmsData_t data */
402                 TelSmsData_t simSmsData = {0, };
403
404                 if (submitData.segCount == 1) {
405                         memcpy(&simSmsData.SmsData.Sca, &simMsgDataInfo.sca, sizeof(simSmsData.SmsData.Sca)-1);
406                         memcpy(&simSmsData.SmsData.szData, &simMsgDataInfo.szData, sizeof(simSmsData.SmsData.szData)-1);
407                         simSmsData.SmsData.MsgLength = simMsgDataInfo.msgLength;
408
409                 } else {
410                         memcpy(&(tpdu.data.deliver.userData), &(submitData.userData[segCnt]), sizeof(SMS_USERDATA_S));
411
412                         memset(buf, 0x00, sizeof(buf));
413
414                         /* Encode SMS-DELIVER TPDU */
415                         bufLen = SmsPluginTpduCodec::encodeTpdu(&tpdu, buf);
416
417                         /* Set TPDU data */
418                         int copyLen = (bufLen > TAPI_NETTEXT_SMDATA_SIZE_MAX) ? TAPI_NETTEXT_SMDATA_SIZE_MAX : bufLen;
419                         memcpy((void*)simSmsData.SmsData.Sca, &simMsgDataInfo.sca, sizeof(simSmsData.SmsData.Sca)-1);
420                         memcpy((void*)simSmsData.SmsData.szData, buf, (size_t)copyLen);
421                         simSmsData.SmsData.szData[copyLen] = 0;
422                         simSmsData.SmsData.MsgLength = copyLen;
423                 }
424
425                 simSmsData.SmsData.format = TAPI_NETTEXT_NETTYPE_3GPP;
426                 simSmsData.MsgStatus = TAPI_NETTEXT_STATUS_UNREAD;
427
428                 MSG_MESSAGE_INFO_S *tmpSimMsgInfo = (MSG_MESSAGE_INFO_S *)calloc(1, sizeof(MSG_MESSAGE_INFO_S));
429                 if (tmpSimMsgInfo) {
430                         memcpy(tmpSimMsgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
431
432                         tmpSimMsgInfo->msgId = replaceMsgId;
433
434                         tmpSimMsgInfo->addressList = NULL;
435                         tmpSimMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S)];
436                         memcpy(&tmpSimMsgInfo->addressList[0], &pMsgInfo->addressList[0], sizeof(MSG_ADDRESS_INFO_S));
437
438                         tapiRet = tel_write_sms_in_sim(handle, &simSmsData, TapiEventSaveClass2Msg, tmpSimMsgInfo);
439
440                         if (tapiRet == TAPI_API_SUCCESS) {
441                                 MSG_DEBUG("########  tel_write_sms_in_sim Success !!!, segNum = [%d] #######", segCnt);
442                         } else {
443                                 MSG_DEBUG("########  tel_write_sms_in_sim Fail !!! return : [%d] #######", tapiRet);
444
445                                 SmsPluginTransport::instance()->sendDeliverReport(handle, MSG_ERR_STORAGE_ERROR);
446
447                                 return MSG_ERR_PLUGIN_STORAGE;
448                         }
449                 }
450
451                 msg_sim_id_t retSimId;
452                 if (!getSimEvent(&retSimId))
453                         return MSG_ERR_PLUGIN_STORAGE;
454         }
455
456         return MSG_SUCCESS;
457 }
458
459
460 void SmsPluginSimMsg::deleteSimMessage(msg_sim_slot_id_t sim_idx, msg_sim_id_t SimMsgId)
461 {
462         int tapiRet = TAPI_API_SUCCESS;
463
464         TapiHandle *handle = SmsPluginDSHandler::instance()->getTelHandle(sim_idx);
465         tapiRet = tel_delete_sms_in_sim(handle, (int)SimMsgId, TapiEventDeleteSimMsg, NULL);
466
467         if (tapiRet == TAPI_API_SUCCESS)
468                 MSG_DEBUG("########  tel_delete_sms_in_sim Success !!! #######");
469         else
470                 THROW(MsgException::SMS_PLG_ERROR, "########  tel_delete_sms_in_sim Fail !!! return : [%d] #######", tapiRet);
471
472         int SimId = 0;
473         bool bResult = false;
474
475         bResult = getDelSimEvent(&SimId);
476
477         int usedCnt = 0, totalCnt = 0;
478
479         if (bResult == true) {
480                 MSG_DEBUG("########  Deleting Msg was Successful !!! SIM ID : [%d] #######", SimId);
481                 char keyName[MAX_VCONFKEY_NAME_LEN];
482                 memset(keyName, 0x00, sizeof(keyName));
483                 snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, sim_idx);
484                 if (MsgSettingGetInt(keyName, &usedCnt) != MSG_SUCCESS) {
485                         MSG_INFO("MsgSettingGetInt() is failed");
486                 }
487                 memset(keyName, 0x00, sizeof(keyName));
488                 snprintf(keyName, sizeof(keyName), "%s/%d", SIM_TOTAL_COUNT, sim_idx);
489                 if (MsgSettingGetInt(keyName, &totalCnt) != MSG_SUCCESS) {
490                         MSG_INFO("MsgSettingGetInt() is failed");
491                 }
492
493                 if (usedCnt == totalCnt) {
494                         tapiRet = tel_set_sms_memory_status(handle, TAPI_NETTEXT_PDA_MEMORY_STATUS_AVAILABLE, NULL, NULL);
495
496                         if (tapiRet == TAPI_API_SUCCESS)
497                                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! #######");
498                         else
499                                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! return : [%d] #######", tapiRet);
500                 }
501
502                 usedCnt--;
503
504                 memset(keyName, 0x00, sizeof(keyName));
505                 snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, sim_idx);
506
507                 if (MsgSettingSetInt(keyName, usedCnt) != MSG_SUCCESS)
508                         MSG_DEBUG("Error to set config data [%s]", keyName);
509         } else {
510                 THROW(MsgException::SMS_PLG_ERROR, "########  Deleting Msg was Failed !!! SIM ID : [%d] #######", SimId);
511         }
512 }
513
514
515 bool SmsPluginSimMsg::checkSimMsgFull(msg_sim_slot_id_t sim_idx, unsigned int SegCnt)
516 {
517         int usedCnt = 0, totalCnt = 0;
518
519         char keyName[MAX_VCONFKEY_NAME_LEN];
520         memset(keyName, 0x00, sizeof(keyName));
521         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, sim_idx);
522         if (MsgSettingGetInt(keyName, &usedCnt) != MSG_SUCCESS) {
523                 MSG_INFO("MsgSettingGetInt() is failed");
524         }
525         memset(keyName, 0x00, sizeof(keyName));
526         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_TOTAL_COUNT, sim_idx);
527         if (MsgSettingGetInt(keyName, &totalCnt) != MSG_SUCCESS) {
528                 MSG_INFO("MsgSettingGetInt() is failed");
529         }
530
531         MSG_DEBUG("Segment Count [%d]", SegCnt);
532         MSG_DEBUG("usedCnt [%d], totalCnt [%d]", usedCnt, totalCnt);
533
534         if ((usedCnt + (int)SegCnt) <= totalCnt)
535                 return false;
536         else
537                 return true;
538 }
539
540
541 void SmsPluginSimMsg::setReadStatus(msg_sim_slot_id_t sim_idx, msg_sim_id_t SimMsgId)
542 {
543         MSG_DEBUG("Sim Message ID [%d]", SimMsgId);
544
545         int ret = TAPI_API_SUCCESS;
546         TapiHandle *handle = SmsPluginDSHandler::instance()->getTelHandle(sim_idx);
547
548         ret = tel_set_sms_message_status(handle, (int)SimMsgId, TAPI_NETTEXT_STATUS_READ, TapiEventSetMsgStatus, (void *)&SimMsgId);
549
550         if (ret == TAPI_API_SUCCESS)
551                 MSG_DEBUG("########  tel_set_sms_message_status Success !!! return : %d #######", ret);
552         else
553                 THROW(MsgException::SMS_PLG_ERROR, "########  tel_set_sms_message_status Fail !!! return : %d #######", ret);
554
555         msg_sim_id_t SimId = 0;
556         bool bResult = false;
557
558         bResult = getSimEvent(&SimId);
559
560         if (bResult == true)
561                 MSG_DEBUG("######## Setting Read Status was Successful !!!, sim id=[%d] #######", SimId);
562         else
563                 THROW(MsgException::SMS_PLG_ERROR, "######## Setting Read Status was Failed !!! #######");
564 }
565
566
567 void SmsPluginSimMsg::getSimMsgCount(TapiHandle *handle, MSG_SIM_COUNT_S *pSimMsgCnt)
568 {
569         int ret = TAPI_API_SUCCESS;
570
571         ret = tel_get_sms_count(handle, TapiEventGetSimMsgCnt, NULL);
572
573         if (ret == TAPI_API_SUCCESS)
574                 MSG_DEBUG("######## tel_get_sms_count() Success !!! #######");
575         else
576                 THROW(MsgException::SMS_PLG_ERROR, "########  tel_get_sms_count() Fail !!! return : %d #######", ret);
577
578         if (getSimMsgCntEvent(handle, pSimMsgCnt) == true)
579                 MSG_DEBUG("######## Get Sim Msg Count was Successful !!! #######");
580         else
581                 THROW(MsgException::SMS_PLG_ERROR, "######## Get Sim Msg Count was Failed !!! #######");
582 }
583
584
585 bool SmsPluginSimMsg::getSimMsg(TapiHandle *handle, msg_sim_id_t SimMsgId, MSG_MESSAGE_INFO_S *pMsgInfo, int *simIdList)
586 {
587         int ret = TAPI_API_SUCCESS;
588
589         ret = tel_read_sms_in_sim(handle, SimMsgId, TapiEventGetSimMsg, simIdList);
590
591         if (ret == TAPI_API_SUCCESS) {
592                 MSG_DEBUG("######## tel_read_sms_in_sim() Success !!! Sim ID : [%d] #######", SimMsgId);
593         } else {
594                 MSG_DEBUG("########  tel_read_sms_in_sim() Fail !!! return : %d #######", ret);
595                 return false;
596         }
597
598         if (getSimMsgEvent(handle, pMsgInfo) == true) {
599                 MSG_DEBUG("######## Get Sim Msg was Successful !!! #######");
600         } else {
601                 MSG_DEBUG("######## Get Sim Msg was Failed !!! #######");
602                 return false;
603         }
604
605         return true;
606 }
607
608
609 void SmsPluginSimMsg::setSmsOptions(const MSG_MESSAGE_INFO_S* pMsgInfo, SMS_DELIVER_S *pDeliver)
610 {
611         pDeliver->bMoreMsg = false;
612         pDeliver->bStatusReport = false;
613         pDeliver->bHeaderInd = false;
614         pDeliver->bReplyPath = false;
615
616         pDeliver->dcs.bCompressed = false;
617         pDeliver->dcs.msgClass = SMS_MSG_CLASS_NONE;
618         pDeliver->dcs.codingGroup = SMS_GROUP_GENERAL;
619
620         pDeliver->dcs.codingScheme = pMsgInfo->encodeType;
621
622         MSG_DEBUG("DCS : %d", pDeliver->dcs.codingScheme);
623
624         pDeliver->pid = SMS_PID_NORMAL;
625
626         MSG_DEBUG("PID : %d", pDeliver->pid);
627 }
628
629
630 void SmsPluginSimMsg::convertTimeStamp(const MSG_MESSAGE_INFO_S* pMsgInfo, SMS_DELIVER_S *pDeliver)
631 {
632         MSG_BEGIN();
633
634         /* encode time stamp */
635         pDeliver->timeStamp.format = SMS_TIME_ABSOLUTE;
636
637         /* encode absolute time */
638         struct tm timeinfo = {0, };
639         tzset();
640         localtime_r(&pMsgInfo->displayTime, &timeinfo);
641
642         pDeliver->timeStamp.time.absolute.year = timeinfo.tm_year - 100;
643         MSG_DEBUG("pDeliver->timeStamp.time.absolute.year is %d", pDeliver->timeStamp.time.absolute.year);
644
645         pDeliver->timeStamp.time.absolute.month = timeinfo.tm_mon + 1;
646         MSG_DEBUG("pDeliver->timeStamp.time.absolute.month is %d", pDeliver->timeStamp.time.absolute.month);
647
648         pDeliver->timeStamp.time.absolute.day = timeinfo.tm_mday;
649         MSG_DEBUG("pDeliver->timeStamp.time.absolute.day is %d", pDeliver->timeStamp.time.absolute.day);
650
651         pDeliver->timeStamp.time.absolute.hour = timeinfo.tm_hour;
652         MSG_DEBUG("pDeliver->timeStamp.time.absolute.hour is %d", pDeliver->timeStamp.time.absolute.hour);
653
654         pDeliver->timeStamp.time.absolute.minute = timeinfo.tm_min;
655         MSG_DEBUG("pDeliver->timeStamp.time.absolute.minute is %d", pDeliver->timeStamp.time.absolute.minute);
656
657         pDeliver->timeStamp.time.absolute.second = timeinfo.tm_sec;
658         MSG_DEBUG("pDeliver->timeStamp.time.absolute.second is %d", pDeliver->timeStamp.time.absolute.second);
659
660         pDeliver->timeStamp.time.absolute.timeZone = 0;
661         MSG_DEBUG("pDeliver->timeStamp.time.absolute.timeZone is %d", pDeliver->timeStamp.time.absolute.timeZone);
662
663         MSG_END();
664 }
665
666
667 void SmsPluginSimMsg::setSimMsgCntEvent(TapiHandle *handle, const MSG_SIM_COUNT_S *pSimMsgCnt)
668 {
669         mx.lock();
670
671         MSG_INFO("Sim Message Count is %d.", pSimMsgCnt->usedCount);
672
673         int sim_idx = SmsPluginDSHandler::instance()->getSimIndex(handle);
674         char keyName[MAX_VCONFKEY_NAME_LEN] = {0, };
675
676         for (int i = 0; i < pSimMsgCnt->usedCount; i++) {
677                 MSG_DEBUG("Sim Message Index is %d.", pSimMsgCnt->indexList[i]);
678         }
679
680         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, sim_idx);
681         if (MsgSettingSetInt(keyName, pSimMsgCnt->usedCount) != MSG_SUCCESS)
682                 MSG_ERR("Error to set config data [%s]", SIM_USED_COUNT);
683
684         memset(keyName, 0, sizeof(keyName));
685         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_TOTAL_COUNT, sim_idx);
686         if (MsgSettingSetInt(keyName, (int)pSimMsgCnt->totalCount) != MSG_SUCCESS)
687                 MSG_ERR("Error to set config data [%s]", SIM_TOTAL_COUNT);
688
689         memset(&simMsgCnt, 0x00, sizeof(MSG_SIM_COUNT_S));
690         memcpy(&simMsgCnt, pSimMsgCnt, sizeof(MSG_SIM_COUNT_S));
691
692         cv.signal();
693
694         mx.unlock();
695 }
696
697
698 bool SmsPluginSimMsg::getSimMsgCntEvent(TapiHandle *handle, MSG_SIM_COUNT_S *pSimMsgCnt)
699 {
700         int ret = 0;
701
702         mx.lock();
703
704         ret = cv.timedwait(mx.pMutex(), 10);
705
706         mx.unlock();
707
708         if (ret == ETIMEDOUT) {
709                 MSG_ERR("WARNING: TAPI callback TIME-OUT");
710                 return false;
711         }
712
713         memcpy(pSimMsgCnt, &simMsgCnt, sizeof(MSG_SIM_COUNT_S));
714
715         return true;
716 }
717
718 void SmsPluginSimMsg::setSimMsgEvent(TapiHandle *handle, const MSG_MESSAGE_INFO_S *pMsgInfo, bool bSuccess)
719 {
720         mx.lock();
721
722         bTapiResult = bSuccess;
723
724         memset(&simMsgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
725         memset(&simAddrInfo, 0x00, sizeof(MSG_ADDRESS_INFO_S));
726
727         if (bTapiResult  == true) {
728                 MSG_DEBUG("Success to get sim msg - Id : [%d]", pMsgInfo->msgId);
729
730                 memcpy(&simMsgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
731                 simMsgInfo.addressList = &simAddrInfo;
732                 memcpy(&simAddrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
733         }
734
735         cv.signal();
736
737         mx.unlock();
738 }
739
740
741 bool SmsPluginSimMsg::getSimMsgEvent(TapiHandle *handle, MSG_MESSAGE_INFO_S *pMsgInfo)
742 {
743         int ret = 0;
744
745         mx.lock();
746
747         bTapiResult = false;
748         ret = cv.timedwait(mx.pMutex(), 10);
749
750         mx.unlock();
751
752         if (ret == ETIMEDOUT) {
753                 MSG_DEBUG("WARNING: TAPI callback TIME-OUT");
754                 return false;
755         }
756
757         memset(pMsgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
758
759         if (bTapiResult == true) {
760                 memcpy(pMsgInfo, &simMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
761                 pMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)calloc(1, sizeof(MSG_ADDRESS_INFO_S));
762                 memset(pMsgInfo->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S));
763                 memcpy(pMsgInfo->addressList, simMsgInfo.addressList, sizeof(MSG_ADDRESS_INFO_S));
764                 pMsgInfo->sim_idx = SmsPluginDSHandler::instance()->getSimIndex(handle);
765         }
766
767
768         return bTapiResult;
769 }
770
771
772 void SmsPluginSimMsg::setSaveSimMsgEvent(TapiHandle *handle, int simId, int result)
773 {
774         msg_error_t err = MSG_SUCCESS;
775
776         mx.lock();
777
778         if (result != TAPI_NETTEXT_SENDSMS_SUCCESS) {
779                 if (result == TAPI_NETTEXT_ROUTING_NOT_AVAILABLE || result == TAPI_NETTEXT_SIM_FULL)
780                         err = MSG_ERR_SIM_STORAGE_FULL;
781                 else
782                         err = MSG_ERR_UNKNOWN;
783         }
784
785         if (err == MSG_SUCCESS)
786                 bTapiResult = true;
787         else
788                 bTapiResult = false;
789
790         simMsgId = simId;
791
792         cv.signal();
793
794         mx.unlock();
795
796         int tapiRet = TAPI_API_SUCCESS;
797
798         if (err == MSG_SUCCESS) {
799                 tapiRet = tel_set_sms_memory_status(handle, TAPI_NETTEXT_PDA_MEMORY_STATUS_AVAILABLE, TapiEventMemoryStatus, NULL);
800         } else if (err == MSG_ERR_SIM_STORAGE_FULL) {
801                 tapiRet = tel_set_sms_memory_status(handle, TAPI_NETTEXT_PDA_MEMORY_STATUS_FULL, TapiEventMemoryStatus, NULL);
802         } else {
803                 return;
804         }
805
806         if (tapiRet == TAPI_API_SUCCESS) {
807                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! #######");
808         } else {
809                 MSG_DEBUG("########  tel_set_sms_memory_status() Failed !!! return : [%d] #######", tapiRet);
810         }
811 }
812
813
814 void SmsPluginSimMsg::setSaveClass2MsgEvent(TapiHandle *handle, int simId, int result, MSG_MESSAGE_INFO_S *pMsgInfo)
815 {
816         msg_error_t err = MSG_SUCCESS;
817         /*int sim_idx = SmsPluginDSHandler::instance()->getSimIndex(handle); */
818
819         if (result == TAPI_NETTEXT_SENDSMS_SUCCESS && simId >= 0 && pMsgInfo) {
820                 bool isNewSimMsg = true;
821
822                 for (int i = 0; i < MAX_SIM_SMS_NUM; i++) {
823                         if (simIdList[i] != 0) {
824                                 MSG_DEBUG("simIdList[%d] is exist [%d]", i, simIdList[i]);
825                                 continue;
826                         } else {
827                                 simIdList[i] = simId + 1;
828                                 MSG_DEBUG("simIdList[%d] is assigned [%d]", i, simId + 1);
829                                 break;
830                         }
831                 }
832
833                 if (pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS && pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS) {
834                         if (pMsgInfo->msgId > 0) {
835                                 isNewSimMsg = false;
836                         }
837                 }
838
839                 if (simMsgDataInfo.totalSegment >= 1 && simIdList[simMsgDataInfo.totalSegment-1] != 0) {
840                         msg_message_id_t saved_msg_id = 0;
841                         SmsPluginEventHandler::instance()->handleSimMsg(pMsgInfo, simIdList, &saved_msg_id, MAX_SIM_SMS_NUM);
842
843                         MSG_DEBUG("Saved message ID = [%d]", saved_msg_id);
844
845                         if (saved_msg_id > 0)
846                                 pMsgInfo->msgId = saved_msg_id;
847
848                         err = SmsPluginEventHandler::instance()->callbackMsgIncoming(pMsgInfo);
849
850                         if (err != MSG_SUCCESS)
851                                 MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
852                 }
853
854                 if (isNewSimMsg == true) {
855                         char keyName[MAX_VCONFKEY_NAME_LEN];
856                         memset(keyName, 0x00, sizeof(keyName));
857                         snprintf(keyName, sizeof(keyName), "%s/%d", SIM_USED_COUNT, pMsgInfo->sim_idx);
858                         if (MsgSettingGetInt(keyName, &usedCnt) != MSG_SUCCESS) {
859                                 MSG_INFO("MsgSettingGetInt() is failed");
860                         }
861                         usedCnt++;
862
863                         if (MsgSettingSetInt(keyName, usedCnt) != MSG_SUCCESS)
864                                 MSG_DEBUG("Error to set config data [%s]", SIM_USED_COUNT);
865                 }
866
867                 if (simMsgDataInfo.totalSegment >= 1 && simIdList[simMsgDataInfo.totalSegment-1] != 0) {
868                         memset(simIdList, 0, sizeof(int) * MAX_SIM_SMS_NUM);
869                 }
870         } else {
871                 if (result == TAPI_NETTEXT_SIM_FULL)
872                         err = MSG_ERR_SIM_STORAGE_FULL;
873                 else
874                         err = MSG_ERR_UNKNOWN;
875         }
876
877         /* Send Deliver Report */
878         SmsPluginTransport::instance()->sendDeliverReport(handle, err);
879 }
880
881
882 void SmsPluginSimMsg::setSimEvent(msg_sim_id_t SimId, bool bResult)
883 {
884         mx.lock();
885
886         simMsgId = SimId;
887         bTapiResult = bResult;
888
889         cv.signal();
890
891         mx.unlock();
892 }
893
894
895 bool SmsPluginSimMsg::getSimEvent(msg_sim_id_t *pSimId)
896 {
897         int ret = 0;
898
899         mx.lock();
900
901         bTapiResult = false;
902         ret = cv.timedwait(mx.pMutex(), 10);
903
904         mx.unlock();
905
906         if (ret == ETIMEDOUT) {
907                 MSG_DEBUG("WARNING: TAPI callback TIME-OUT");
908                 return false;
909         }
910
911         *pSimId = simMsgId;
912
913         MSG_DEBUG("Returned SimMsgId is %d.", simMsgId);
914
915         return bTapiResult;
916 }
917
918
919 void SmsPluginSimMsg::setDelSimEvent(int SimId, bool bResult)
920 {
921         mx.lock();
922
923         delSimMsgId = SimId;
924         bTapiResult = bResult;
925
926         cv.signal();
927
928         mx.unlock();
929 }
930
931
932 bool SmsPluginSimMsg::getDelSimEvent(int *pSimId)
933 {
934         int ret = 0;
935
936         mx.lock();
937
938         delSimMsgId = -1;
939         bTapiResult = false;
940         ret = cv.timedwait(mx.pMutex(), 10);
941
942         mx.unlock();
943
944         if (ret == ETIMEDOUT) {
945                 MSG_DEBUG("WARNING: TAPI callback TIME-OUT");
946                 return false;
947         }
948
949         *pSimId = delSimMsgId;
950
951         MSG_DEBUG("Returned delSimMsgId is %d.", delSimMsgId);
952
953         return bTapiResult;
954 }
955
956
957 void SmsPluginSimMsg::setSmsData(const char *sca, const char *szData, int msgLength)
958 {
959         MSG_DEBUG("Set SMS data(class2 message)");
960
961         memset(&simMsgDataInfo, 0x00, sizeof(simMsgDataInfo));
962
963         memcpy(&simMsgDataInfo.sca, sca, sizeof(simMsgDataInfo.sca)-1);
964         memcpy(&simMsgDataInfo.szData, szData, sizeof(simMsgDataInfo.szData)-1);
965         simMsgDataInfo.msgLength = msgLength;
966 }
967
968 void SmsPluginSimMsg::setSmsTpduTotalSegCount(int totalSeg)
969 {
970         MSG_DEBUG("Set SMS Segements Info");
971
972         simMsgDataInfo.totalSegment = totalSeg;
973 }
974