RSA sync with private
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginMain.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 "MsgException.h"
21 #include "MsgGconfWrapper.h"
22
23 #include "MsgMutex.h"
24 #include "SmsPluginTransport.h"
25 #include "SmsPluginSimMsg.h"
26 #include "SmsPluginStorage.h"
27 #include "SmsPluginSetting.h"
28 #include "SmsPluginCallback.h"
29 #include "SmsPluginEventHandler.h"
30 #include "SmsPluginUAManager.h"
31 #include "SmsPluginMain.h"
32
33 extern "C"
34 {
35         #include <tapi_common.h>
36         #include <TelSms.h>
37         #include <TapiUtility.h>
38         #include <ITapiSim.h>
39         #include <ITapiNetText.h>
40 }
41
42 struct tapi_handle *pTapiHandle = NULL;
43
44 Mutex mx;
45 CndVar cv;
46
47 /*==================================================================================================
48                                      FUNCTION IMPLEMENTATION
49 ==================================================================================================*/
50 static void MsgTapiInitCB(keynode_t *key, void* data)
51 {
52         MSG_DEBUG("MsgTapiInitCB is called.");
53         mx.lock();
54         cv.signal();
55         mx.unlock();
56 }
57
58 msg_error_t MsgPlgCreateHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
59 {
60         if (pPluginHandle == NULL)
61         {
62                 MSG_DEBUG("SMS plugin: create handler error ");
63                 return MSG_ERR_NULL_POINTER;
64         }
65         else
66         {
67                 pPluginHandle->pfInitialize = SmsPlgInitialize;
68                 pPluginHandle->pfFinalize = SmsPlgFinalize;
69                 pPluginHandle->pfRegisterListener = SmsPlgRegisterListener;
70                 pPluginHandle->pfCheckSimStatus = SmsPlgCheckSimStatus;
71                 pPluginHandle->pfCheckDeviceStatus = SmsPlgCheckDeviceStatus;
72                 pPluginHandle->pfSubmitRequest = SmsPlgSubmitRequest;
73                 pPluginHandle->pfInitSimMessage = SmsPlgInitSimMessage;
74                 pPluginHandle->pfSaveSimMessage = SmsPlgSaveSimMessage;
75                 pPluginHandle->pfDeleteSimMessage = SmsPlgDeleteSimMessage;
76                 pPluginHandle->pfSetReadStatus = SmsPlgSetReadStatus;
77                 pPluginHandle->pfSetMemoryStatus = SmsPlgSetMemoryStatus;
78                 pPluginHandle->pfInitConfigData = SmsPlgInitConfigData;
79                 pPluginHandle->pfSetConfigData = SmsPlgSetConfigData;
80                 pPluginHandle->pfGetConfigData = SmsPlgGetConfigData;
81
82                 pPluginHandle->pfRestoreMsg = NULL;
83
84                 MSG_DEBUG("SMS plugin: create handler OK");
85                 MSG_DEBUG ("SMS plugin %p", pPluginHandle);
86         }
87
88         return MSG_SUCCESS;
89 }
90
91
92 msg_error_t MsgPlgDestroyHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
93 {
94         if (pPluginHandle != NULL)
95         {
96                 free(pPluginHandle);
97                 pPluginHandle = NULL;
98         }
99
100         MSG_DEBUG("SMS plugin: destory handler OK");
101
102         return MSG_SUCCESS;
103 }
104
105
106 msg_error_t SmsPlgInitialize()
107 {
108         MSG_BEGIN();
109
110         MSG_DEBUG("set MSG_SIM_CHANGED to MSG_SIM_STATUS_NOT_FOUND.");
111         if (MsgSettingSetInt(MSG_SIM_CHANGED, MSG_SIM_STATUS_NOT_FOUND) != MSG_SUCCESS)
112                 MSG_DEBUG("MsgSettingSetInt is failed!!");
113
114         bool bReady;
115         MsgSettingGetBool(VCONFKEY_TELEPHONY_READY, &bReady);
116         MSG_DEBUG("Get VCONFKEY_TELEPHONY_READY [%d].", bReady);
117
118         int ret = 0;
119
120         if(!bReady) {
121                 MsgSettingRegVconfCBCommon(VCONFKEY_TELEPHONY_READY, MsgTapiInitCB);
122                 mx.lock();
123                 ret = cv.timedwait(mx.pMutex(), 90);
124                 mx.unlock();
125         }
126
127         try
128         {
129                 if (ret != ETIMEDOUT) {
130                         pTapiHandle = tel_init(NULL);
131                         SmsPluginCallback::instance()->registerEvent();
132                 }
133         }
134         catch (MsgException& e)
135         {
136                 MSG_FATAL("%s", e.what());
137                 return MSG_ERR_PLUGIN_REGEVENT;
138         }
139         catch (exception& e)
140         {
141                 MSG_FATAL("%s", e.what());
142                 return MSG_ERR_PLUGIN_REGEVENT;
143         }
144
145         MSG_END();
146
147         return MSG_SUCCESS;
148 }
149
150
151 msg_error_t SmsPlgFinalize()
152 {
153         MSG_BEGIN();
154
155         if (!pTapiHandle)
156                 return MSG_ERR_PLUGIN_TAPIINIT;
157
158         SmsPluginCallback::instance()->deRegisterEvent();
159
160         tel_deinit(pTapiHandle);
161
162         MSG_END();
163
164         return MSG_SUCCESS;
165 }
166
167
168 msg_error_t SmsPlgRegisterListener(MSG_PLUGIN_LISTENER_S *pListener)
169 {
170         MSG_BEGIN();
171
172         SmsPluginEventHandler::instance()->registerListener(pListener);
173
174         MSG_END();
175
176         return MSG_SUCCESS;
177 }
178
179
180 msg_error_t SmsPlgCheckSimStatus(MSG_SIM_STATUS_T *pStatus)
181 {
182         MSG_BEGIN();
183
184         if (!pTapiHandle)
185                 return MSG_ERR_PLUGIN_TAPIINIT;
186
187         int tryNum = 0, tapiRet = TAPI_API_SUCCESS;
188
189         TelSimCardStatus_t status = TAPI_SIM_STATUS_CARD_ERROR;
190         int cardChanged = 0;
191
192         // initialize pStatus.
193         *pStatus = MSG_SIM_STATUS_NOT_FOUND;
194
195         // Check SIM Status
196         while (1)
197         {
198                 if (tryNum > 30) return MSG_ERR_PLUGIN_TAPIINIT;
199
200                 tapiRet = tel_get_sim_init_info(pTapiHandle, &status, &cardChanged);
201
202                 if (tapiRet == TAPI_API_SUCCESS) {
203                         if (status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED || status == TAPI_SIM_STATUS_SIM_PUK_REQUIRED) {
204                                 MSG_DEBUG("PIN or PUK is required [%d]", status);
205
206                                 sleep(3);
207
208                                 continue;
209                         }
210
211                         if (status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
212                                 MSG_DEBUG("SIM status is OK [%d]", status);
213
214                                 MSG_DEBUG("SIM Changed [%d]", cardChanged);
215
216                                 if (cardChanged == 1)
217                                         *pStatus = MSG_SIM_STATUS_CHANGED;
218                                 else
219                                         *pStatus = MSG_SIM_STATUS_NORMAL;
220
221                                 break;
222                         } else if (status == TAPI_SIM_STATUS_CARD_NOT_PRESENT) {
223                                 MSG_DEBUG("SIM is not present [%d]", status);
224                                 break;
225                         } else {
226                                 MSG_DEBUG("SIM status is not OK [%d]", status);
227                                 tryNum++;
228
229                                 sleep(3);
230                         }
231                 } else if (tapiRet == TAPI_API_SIM_NOT_FOUND) {
232                         MSG_DEBUG("tel_get_sim_init_info() result is TAPI_API_SIM_NOT_FOUND");
233                         break;
234                 } else {
235                         MSG_DEBUG("tel_get_sim_init_info() result is unknown!!!!!!!!!! [%d]", tapiRet);
236                         tryNum++;
237
238                         sleep(3);
239                 }
240         }
241
242
243         char imsi[7];
244         memset(imsi, 0x00, sizeof(imsi));
245
246         // Get IMSI
247         if (*pStatus != MSG_SIM_STATUS_NOT_FOUND)
248         {
249                 // Get IMSI
250                 TelSimImsiInfo_t imsiInfo;
251                 memset(&imsiInfo, 0x00, sizeof(TelSimImsiInfo_t));
252
253                 tapiRet = tel_get_sim_imsi(pTapiHandle, &imsiInfo);
254
255                 if (tapiRet == TAPI_API_SUCCESS)
256                 {
257                         MSG_DEBUG("tel_get_sim_imsi() Success - MCC [%s], MNC [%s]", imsiInfo.szMcc, imsiInfo.szMnc);
258
259                         sprintf(imsi, "%03d%03d", atoi(imsiInfo.szMcc), atoi(imsiInfo.szMnc));
260
261                         MSG_DEBUG("IMSI [%d]", atoi(imsi));
262                 }
263                 else
264                 {
265                         MSG_DEBUG("tel_get_sim_imsi() Error![%d]", tapiRet);
266
267                         MsgSettingSetBool(MSG_NATIONAL_SIM, false);
268                 }
269         }
270         else
271         {
272                 MsgSettingSetBool(MSG_NATIONAL_SIM, false);
273         }
274
275         MsgSettingSetString(MSG_SIM_IMSI, imsi);
276
277         MSG_END();
278
279         return MSG_SUCCESS;
280 }
281
282
283 msg_error_t SmsPlgCheckDeviceStatus()
284 {
285         MSG_BEGIN();
286
287         if (!pTapiHandle)
288                 return MSG_ERR_PLUGIN_TAPIINIT;
289
290         int status = 0, tapiRet = TAPI_API_SUCCESS;
291
292         tapiRet = tel_check_sms_device_status(pTapiHandle, &status);
293
294         if (tapiRet != TAPI_API_SUCCESS) {
295                 MSG_DEBUG("tel_check_sms_device_status() Error! [%d], Status [%d]", tapiRet, status);
296                 return MSG_ERR_PLUGIN_TAPI_FAILED;
297         }
298
299         if (status == 1) {
300                 MSG_DEBUG("Device Is Ready");
301                 return MSG_SUCCESS;
302         } else if (status == 0) {
303                 MSG_DEBUG("Device Is Not Ready.. Waiting For Ready Callback");
304
305                 if (SmsPluginEventHandler::instance()->getDeviceStatus() == true) {
306                         MSG_DEBUG("Device Is Ready");
307                         return MSG_SUCCESS;
308                 } else {
309                         MSG_DEBUG("Device Is Not Ready.");
310                         return MSG_ERR_PLUGIN_TAPI_FAILED;
311                 }
312         }
313
314         return MSG_SUCCESS;
315 }
316
317
318 msg_error_t SmsPlgSubmitRequest(MSG_REQUEST_INFO_S *pReqInfo)
319 {
320         msg_error_t err = MSG_SUCCESS;
321
322         // Add Submit SMS into DB
323         if (pReqInfo->msgInfo.msgId == 0) {
324                 if (pReqInfo->msgInfo.msgPort.valid == false) {
325                         err = SmsPluginStorage::instance()->addMessage(&(pReqInfo->msgInfo));
326                         if (err != MSG_SUCCESS) {
327                                 MSG_DEBUG("########  addMessage Fail !!");
328                                 return MSG_ERR_PLUGIN_STORAGE;
329                         }
330                 }
331         }
332
333         // Check SIM is present or not
334         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
335
336         if (simStatus == MSG_SIM_STATUS_NOT_FOUND)
337         {
338                 MSG_DEBUG("SIM is not present..");
339
340                 // Update Msg Status
341                 if (pReqInfo->msgInfo.msgPort.valid == false)
342                         SmsPluginStorage::instance()->updateSentMsg(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
343
344                 return MSG_ERR_NO_SIM;
345         }
346
347         SMS_REQUEST_INFO_S *request = NULL;
348
349         request = (SMS_REQUEST_INFO_S *)calloc(1, sizeof(SMS_REQUEST_INFO_S));
350
351         if (request != NULL) {
352                 request->reqId = pReqInfo->reqId;
353
354                 memcpy(&(request->msgInfo), &(pReqInfo->msgInfo), sizeof(MSG_MESSAGE_INFO_S));
355                 memcpy(&(request->sendOptInfo), &(pReqInfo->sendOptInfo), sizeof(MSG_SENDINGOPT_INFO_S));
356
357                 /* Add Request into Queue and Start UA Manger */
358                 SmsPluginUAManager::instance()->addReqEntity(request);
359
360                 free(request);
361         }
362
363
364         return MSG_SUCCESS;
365 }
366
367
368 msg_error_t SmsPlgInitSimMessage()
369 {
370         // Check SIM is present or not
371         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
372
373         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
374                 MSG_DEBUG("SIM is not present..");
375                 return MSG_ERR_NO_SIM;
376         }
377
378         try
379         {
380                 SmsPluginSimMsg::instance()->initSimMessage();
381         }
382         catch (MsgException& e)
383         {
384                 MSG_FATAL("%s", e.what());
385                 return MSG_ERR_PLUGIN_STORAGE;
386         }
387         catch (exception& e)
388         {
389                 MSG_FATAL("%s", e.what());
390                 return MSG_ERR_PLUGIN_STORAGE;
391         }
392
393         return MSG_SUCCESS;
394 }
395
396
397 msg_error_t SmsPlgSaveSimMessage(const MSG_MESSAGE_INFO_S *pMsgInfo, SMS_SIM_ID_LIST_S *pSimIdList)
398 {
399         // Check SIM is present or not
400         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
401
402         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
403                 MSG_DEBUG("SIM is not present..");
404                 return MSG_ERR_NO_SIM;
405         }
406
407         msg_error_t err = MSG_SUCCESS;
408
409         err = SmsPluginSimMsg::instance()->saveSimMessage(pMsgInfo, pSimIdList);
410
411         return err;
412 }
413
414
415 msg_error_t SmsPlgDeleteSimMessage(msg_sim_id_t SimMsgId)
416 {
417         // Check SIM is present or not
418         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
419
420         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
421                 MSG_DEBUG("SIM is not present..");
422                 return MSG_ERR_NO_SIM;
423         }
424
425         try
426         {
427                 SmsPluginSimMsg::instance()->deleteSimMessage(SimMsgId);
428         }
429         catch (MsgException& e)
430         {
431                 MSG_FATAL("%s", e.what());
432                 return MSG_ERR_PLUGIN_STORAGE;
433         }
434         catch (exception& e)
435         {
436                 MSG_FATAL("%s", e.what());
437                 return MSG_ERR_PLUGIN_STORAGE;
438         }
439
440         return MSG_SUCCESS;
441 }
442
443
444 msg_error_t SmsPlgSetReadStatus(msg_sim_id_t SimMsgId)
445 {
446         // Check SIM is present or not
447         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
448
449         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
450                 MSG_DEBUG("SIM is not present..");
451                 return MSG_ERR_NO_SIM;
452         }
453
454         try
455         {
456                 SmsPluginSimMsg::instance()->setReadStatus(SimMsgId);
457         }
458         catch (MsgException& e)
459         {
460                 MSG_FATAL("%s", e.what());
461                 return MSG_ERR_PLUGIN_STORAGE;
462         }
463         catch (exception& e)
464         {
465                 MSG_FATAL("%s", e.what());
466                 return MSG_ERR_PLUGIN_STORAGE;
467         }
468
469         return MSG_SUCCESS;
470 }
471
472
473 msg_error_t SmsPlgSetMemoryStatus(msg_error_t Error)
474 {
475         // Check SIM is present or not
476         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
477
478         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
479                 MSG_DEBUG("SIM is not present..");
480                 return MSG_ERR_NO_SIM;
481         }
482
483         int tapiRet = TAPI_API_SUCCESS;
484         int status = TAPI_NETTEXT_PDA_MEMORY_STATUS_AVAILABLE;
485
486         if (Error == MSG_ERR_SIM_STORAGE_FULL || Error == MSG_ERR_MESSAGE_COUNT_FULL)
487         {
488                 status = TAPI_NETTEXT_PDA_MEMORY_STATUS_FULL;
489         }
490
491         MSG_DEBUG("Set Status : [%d]", status);
492
493         tapiRet = tel_set_sms_memory_status(pTapiHandle, status, TapiEventMemoryStatus, NULL);
494
495         if (tapiRet == TAPI_API_SUCCESS)
496         {
497                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! #######");
498         }
499         else
500         {
501                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! return : [%d] #######", tapiRet);
502         }
503
504         return MSG_SUCCESS;
505 }
506
507
508 msg_error_t SmsPlgInitConfigData(MSG_SIM_STATUS_T SimStatus)
509 {
510         // Check SIM is present or not
511         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
512
513         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
514                 MSG_DEBUG("SIM is not present..");
515                 return MSG_ERR_NO_SIM;
516         }
517
518         try
519         {
520                 SmsPluginSetting::instance()->initConfigData(SimStatus);
521         }
522         catch (MsgException& e)
523         {
524                 MSG_FATAL("%s", e.what());
525                 return MSG_ERR_PLUGIN_SETTING;
526         }
527         catch (exception& e)
528         {
529                 MSG_FATAL("%s", e.what());
530                 return MSG_ERR_PLUGIN_SETTING;
531         }
532
533         return MSG_SUCCESS;
534 }
535
536
537 msg_error_t SmsPlgSetConfigData(const MSG_SETTING_S *pSetting)
538 {
539         // Check SIM is present or not
540         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
541
542         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
543                 MSG_DEBUG("SIM is not present..");
544                 return MSG_ERR_NO_SIM;
545         }
546
547         try
548         {
549                 SmsPluginSetting::instance()->setConfigData(pSetting);
550         }
551         catch (MsgException& e)
552         {
553                 MSG_FATAL("%s", e.what());
554                 return MSG_ERR_PLUGIN_SETTING;
555         }
556         catch (exception& e)
557         {
558                 MSG_FATAL("%s", e.what());
559                 return MSG_ERR_PLUGIN_SETTING;
560         }
561
562         return MSG_SUCCESS;
563 }
564
565
566 msg_error_t SmsPlgGetConfigData(MSG_SETTING_S *pSetting)
567 {
568         // Check SIM is present or not
569         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
570
571         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
572                 MSG_DEBUG("SIM is not present..");
573                 return MSG_ERR_NO_SIM;
574         }
575
576         try
577         {
578                 SmsPluginSetting::instance()->getConfigData(pSetting);
579         }
580         catch (MsgException& e)
581         {
582                 MSG_FATAL("%s", e.what());
583                 return MSG_ERR_PLUGIN_SETTING;
584         }
585         catch (exception& e)
586         {
587                 MSG_FATAL("%s", e.what());
588                 return MSG_ERR_PLUGIN_SETTING;
589         }
590
591         return MSG_SUCCESS;
592 }
593