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