d068e093bc2d0fcdf7ee3a27e9b77996724d3c25
[framework/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 "MsgDebug.h"
18 #include "MsgException.h"
19 #include "MsgGconfWrapper.h"
20
21 #include "SmsPluginTransport.h"
22 #include "SmsPluginSimMsg.h"
23 #include "SmsPluginStorage.h"
24 #include "SmsPluginSetting.h"
25 #include "SmsPluginCallback.h"
26 #include "SmsPluginEventHandler.h"
27 #include "SmsPluginUAManager.h"
28 #include "SmsPluginMain.h"
29
30 extern "C"
31 {
32         #include <ITapiSim.h>
33         #include <ITapiNetText.h>
34 }
35
36
37 /*==================================================================================================
38                                      FUNCTION IMPLEMENTATION
39 ==================================================================================================*/
40 MSG_ERROR_T MsgPlgCreateHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
41 {
42         if (pPluginHandle == NULL)
43         {
44                 MSG_DEBUG("SMS plugin: create handler error ");
45                 return MSG_ERR_NULL_POINTER;
46         }
47         else
48         {
49                 pPluginHandle->pfInitialize = SmsPlgInitialize;
50                 pPluginHandle->pfFinalize = SmsPlgFinalize;
51                 pPluginHandle->pfRegisterListener = SmsPlgRegisterListener;
52                 pPluginHandle->pfCheckSimStatus = SmsPlgCheckSimStatus;
53                 pPluginHandle->pfCheckDeviceStatus = SmsPlgCheckDeviceStatus;
54                 pPluginHandle->pfSubmitRequest = SmsPlgSubmitRequest;
55                 pPluginHandle->pfInitSimMessage = SmsPlgInitSimMessage;
56                 pPluginHandle->pfSaveSimMessage = SmsPlgSaveSimMessage;
57                 pPluginHandle->pfDeleteSimMessage = SmsPlgDeleteSimMessage;
58                 pPluginHandle->pfSetReadStatus = SmsPlgSetReadStatus;
59                 pPluginHandle->pfSetMemoryStatus = SmsPlgSetMemoryStatus;
60                 pPluginHandle->pfInitConfigData = SmsPlgInitConfigData;
61                 pPluginHandle->pfSetConfigData = SmsPlgSetConfigData;
62                 pPluginHandle->pfGetConfigData = SmsPlgGetConfigData;
63
64                 pPluginHandle->pfRestoreMsg = NULL;
65
66                 MSG_DEBUG("SMS plugin: create handler OK");
67                 MSG_DEBUG ("SMS plugin %p", pPluginHandle);
68         }
69
70         return MSG_SUCCESS;
71 }
72
73
74 MSG_ERROR_T MsgPlgDestroyHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
75 {
76         if (pPluginHandle != NULL)
77         {
78                 free(pPluginHandle);
79                 pPluginHandle = NULL;
80         }
81
82         MSG_DEBUG("SMS plugin: destory handler OK");
83
84         return MSG_SUCCESS;
85 }
86
87
88 MSG_ERROR_T SmsPlgInitialize()
89 {
90         MSG_BEGIN();
91
92         int tapiRet = TAPI_API_SUCCESS;
93
94         tapiRet = tel_init();
95
96         if (tapiRet != TAPI_API_SUCCESS)
97                 return MSG_ERR_PLUGIN_TAPIINIT;
98
99         try
100         {
101                 SmsPluginCallback::instance()->registerEvent();
102         }
103         catch (MsgException& e)
104         {
105                 MSG_FATAL("%s", e.what());
106                 return MSG_ERR_PLUGIN_REGEVENT;
107         }
108         catch (exception& e)
109         {
110                 MSG_FATAL("%s", e.what());
111                 return MSG_ERR_PLUGIN_REGEVENT;
112         }
113
114         MSG_END();
115
116         return MSG_SUCCESS;
117 }
118
119
120 MSG_ERROR_T SmsPlgFinalize()
121 {
122         MSG_BEGIN();
123
124         SmsPluginCallback::instance()->deRegisterEvent();
125
126          tel_deinit();
127
128         MSG_END();
129
130         return MSG_SUCCESS;
131 }
132
133
134 MSG_ERROR_T SmsPlgRegisterListener(MSG_PLUGIN_LISTENER_S *pListener)
135 {
136         MSG_BEGIN();
137
138         SmsPluginEventHandler::instance()->registerListener(pListener);
139
140         MSG_END();
141
142         return MSG_SUCCESS;
143 }
144
145
146 MSG_ERROR_T SmsPlgCheckSimStatus(MSG_SIM_STATUS_T *pStatus)
147 {
148         MSG_BEGIN();
149
150         int tryNum = 0, tapiRet = TAPI_API_SUCCESS;
151
152         TelSimCardStatus_t status = TAPI_SIM_STATUS_CARD_ERROR;
153         int cardChanged = 0;
154
155         // Check SIM Status
156         while (1)
157         {
158                 if (tryNum > 30) return MSG_ERR_PLUGIN_TAPIINIT;
159
160                 tapiRet = tel_get_sim_init_info(&status, &cardChanged);
161
162                 if (tapiRet == TAPI_API_SUCCESS)
163                 {
164                         if (status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED || status == TAPI_SIM_STATUS_SIM_PUK_REQUIRED)
165                         {
166                                 MSG_DEBUG("PIN or PUK is required [%d]", status);
167
168                                 sleep(3);
169
170                                 continue;
171                         }
172
173                         if (status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED)
174                         {
175                                 MSG_DEBUG("SIM status is OK [%d]", status);
176
177                                 MSG_DEBUG("SIM Changed [%d]", cardChanged);
178
179                                 if (cardChanged == 1)
180                                         *pStatus = MSG_SIM_STATUS_CHANGED;
181                                 else
182                                         *pStatus = MSG_SIM_STATUS_NORMAL;
183
184                                 break;
185                         }
186                         else if (status == TAPI_SIM_STATUS_CARD_NOT_PRESENT)
187                         {
188                                 MSG_DEBUG("SIM is not present [%d]", status);
189
190                                 *pStatus = MSG_SIM_STATUS_NOT_FOUND;
191
192                                 break;
193                         }
194                         else
195                         {
196                                 MSG_DEBUG("SIM status is not OK [%d]", status);
197
198                                 tryNum++;
199
200                                 sleep(3);
201                         }
202                 }
203                 else if (tapiRet == TAPI_API_SIM_NOT_FOUND)
204                 {
205                         MSG_DEBUG("tel_get_sim_init_info() result is TAPI_API_SIM_NOT_FOUND");
206
207                         *pStatus = MSG_SIM_STATUS_NOT_FOUND;
208
209                         break;
210                 }
211                 else
212                 {
213                         MSG_DEBUG("tel_get_sim_init_info() result is unknown!!!!!!!!!! [%d]", tapiRet);
214
215                         tryNum++;
216
217                         sleep(3);
218                 }
219         }
220
221         MSG_END();
222
223         return MSG_SUCCESS;
224 }
225
226
227 MSG_ERROR_T SmsPlgCheckDeviceStatus()
228 {
229         MSG_BEGIN();
230
231         int status = 0, tapiRet = TAPI_API_SUCCESS;
232
233         tapiRet = tel_check_sms_device_status(&status);
234
235         if (tapiRet != TAPI_API_SUCCESS)
236         {
237                 MSG_DEBUG("tel_check_sms_device_status() Error! [%d], Status [%d]", tapiRet, status);
238
239                 return MSG_ERR_PLUGIN_TAPI_FAILED;
240         }
241
242         if (status == TAPI_NETTEXT_DEVICE_READY)
243         {
244                 MSG_DEBUG("Device Is Ready");
245                 return MSG_SUCCESS;
246         }
247         else if (status == TAPI_NETTEXT_DEVICE_NOT_READY)
248         {
249                 MSG_DEBUG("Device Is Not Ready.. Waiting For Ready Callback");
250
251                 if (SmsPluginEventHandler::instance()->getDeviceStatus() == true)
252                 {
253                         MSG_DEBUG("Device Is Ready");
254                         return MSG_SUCCESS;
255                 }
256         }
257
258         return MSG_SUCCESS;
259 }
260
261
262 MSG_ERROR_T SmsPlgSubmitRequest(MSG_REQUEST_INFO_S *pReqInfo, bool bReqCb)
263 {
264         // Add Submit SMS into DB
265         if ((pReqInfo->msgInfo.msgId == 0) && pReqInfo->msgInfo.msgPort.valid == false) {
266                 if (SmsPluginStorage::instance()->addMessage(&(pReqInfo->msgInfo)) != MSG_SUCCESS) {
267                         MSG_DEBUG("########  addMessage Fail !!");
268                         return MSG_ERR_PLUGIN_STORAGE;
269                 }
270         }
271
272         // Check SIM is present or not
273         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
274
275         if (simStatus == MSG_SIM_STATUS_NOT_FOUND)
276         {
277                 MSG_DEBUG("SIM is not present..");
278
279                 // Update Msg Status
280                 if (pReqInfo->msgInfo.msgPort.valid == false)
281                         SmsPluginStorage::instance()->updateSentMsg(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
282
283                 return MSG_ERR_NO_SIM;
284         }
285
286         SMS_REQUEST_INFO_S request = {};
287
288         request.reqId = pReqInfo->reqId;
289
290         memcpy(&(request.msgInfo), &(pReqInfo->msgInfo), sizeof(MSG_MESSAGE_INFO_S));
291         memcpy(&(request.sendOptInfo), &(pReqInfo->sendOptInfo), sizeof(MSG_SENDINGOPT_INFO_S));
292
293         request.bReqCb = bReqCb;
294
295         // Add Request into Queue and Start UA Manger
296         SmsPluginUAManager::instance()->addReqEntity(request);
297
298         return MSG_SUCCESS;
299 }
300
301
302 MSG_ERROR_T SmsPlgInitSimMessage()
303 {
304         try
305         {
306                 SmsPluginSimMsg::instance()->initSimMessage();
307         }
308         catch (MsgException& e)
309         {
310                 MSG_FATAL("%s", e.what());
311                 return MSG_ERR_PLUGIN_STORAGE;
312         }
313         catch (exception& e)
314         {
315                 MSG_FATAL("%s", e.what());
316                 return MSG_ERR_PLUGIN_STORAGE;
317         }
318
319         return MSG_SUCCESS;
320 }
321
322
323 MSG_ERROR_T SmsPlgSaveSimMessage(const MSG_MESSAGE_INFO_S *pMsgInfo, SMS_SIM_ID_LIST_S *pSimIdList)
324 {
325         MSG_ERROR_T err = MSG_SUCCESS;
326
327         err = SmsPluginSimMsg::instance()->saveSimMessage(pMsgInfo, pSimIdList);
328
329         return err;
330 }
331
332
333 MSG_ERROR_T SmsPlgDeleteSimMessage(MSG_SIM_ID_T SimMsgId)
334 {
335         try
336         {
337                 SmsPluginSimMsg::instance()->deleteSimMessage(SimMsgId);
338         }
339         catch (MsgException& e)
340         {
341                 MSG_FATAL("%s", e.what());
342                 return MSG_ERR_PLUGIN_STORAGE;
343         }
344         catch (exception& e)
345         {
346                 MSG_FATAL("%s", e.what());
347                 return MSG_ERR_PLUGIN_STORAGE;
348         }
349
350         return MSG_SUCCESS;
351 }
352
353
354 MSG_ERROR_T SmsPlgSetReadStatus(MSG_SIM_ID_T SimMsgId)
355 {
356         try
357         {
358                 SmsPluginSimMsg::instance()->setReadStatus(SimMsgId);
359         }
360         catch (MsgException& e)
361         {
362                 MSG_FATAL("%s", e.what());
363                 return MSG_ERR_PLUGIN_STORAGE;
364         }
365         catch (exception& e)
366         {
367                 MSG_FATAL("%s", e.what());
368                 return MSG_ERR_PLUGIN_STORAGE;
369         }
370
371         return MSG_SUCCESS;
372 }
373
374
375 MSG_ERROR_T SmsPlgSetMemoryStatus(MSG_ERROR_T Error)
376 {
377         int tapiRet = TAPI_API_SUCCESS, reqId = 0;
378         int status = TAPI_NETTEXT_PDA_MEMORY_STATUS_AVAILABLE;
379
380         if (Error == MSG_ERR_SIM_STORAGE_FULL || Error == MSG_ERR_MESSAGE_COUNT_FULL)
381         {
382                 status = TAPI_NETTEXT_PDA_MEMORY_STATUS_FULL;
383         }
384
385         MSG_DEBUG("Set Status : [%d]", status);
386
387         tapiRet = tel_set_sms_memory_status(status, &reqId);
388
389         if (tapiRet == TAPI_API_SUCCESS)
390         {
391                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! req Id : [%d] #######", reqId);
392         }
393         else
394         {
395                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! req Id : [%d] return : [%d] #######", reqId, tapiRet);
396         }
397
398         return MSG_SUCCESS;
399 }
400
401
402 MSG_ERROR_T SmsPlgInitConfigData(MSG_SIM_STATUS_T SimStatus)
403 {
404         try
405         {
406                 SmsPluginSetting::instance()->initConfigData(SimStatus);
407         }
408         catch (MsgException& e)
409         {
410                 MSG_FATAL("%s", e.what());
411                 return MSG_ERR_PLUGIN_SETTING;
412         }
413         catch (exception& e)
414         {
415                 MSG_FATAL("%s", e.what());
416                 return MSG_ERR_PLUGIN_SETTING;
417         }
418
419         return MSG_SUCCESS;
420 }
421
422
423 MSG_ERROR_T SmsPlgSetConfigData(const MSG_SETTING_S *pSetting)
424 {
425         try
426         {
427                 SmsPluginSetting::instance()->setConfigData(pSetting);
428         }
429         catch (MsgException& e)
430         {
431                 MSG_FATAL("%s", e.what());
432                 return MSG_ERR_PLUGIN_SETTING;
433         }
434         catch (exception& e)
435         {
436                 MSG_FATAL("%s", e.what());
437                 return MSG_ERR_PLUGIN_SETTING;
438         }
439
440         return MSG_SUCCESS;
441 }
442
443
444 MSG_ERROR_T SmsPlgGetConfigData(MSG_SETTING_S *pSetting)
445 {
446         try
447         {
448                 SmsPluginSetting::instance()->getConfigData(pSetting);
449         }
450         catch (MsgException& e)
451         {
452                 MSG_FATAL("%s", e.what());
453                 return MSG_ERR_PLUGIN_SETTING;
454         }
455         catch (exception& e)
456         {
457                 MSG_FATAL("%s", e.what());
458                 return MSG_ERR_PLUGIN_SETTING;
459         }
460
461         return MSG_SUCCESS;
462 }
463