Fix issue : fail to make thumbnail
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginMain.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 "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 #include "SmsPluginDSHandler.h"
33 #include <gio/gio.h>
34
35 extern "C" {
36         #include <tapi_common.h>
37         #include <TelSms.h>
38         #include <TapiUtility.h>
39         #include <ITapiSim.h>
40         #include <ITapiNetText.h>
41 }
42
43 #define BUS_NAME "org.tizen.system.deviced"
44 #define PATH_NAME "/Org/Tizen/System/DeviceD/Lowmem"
45 #define INTERFACE_NAME BUS_NAME".lowmem"
46 #define MEMBER_NAME "Full"
47
48 GDBusConnection *gdbus_conn = NULL;
49 GDBusProxy *gdbus_proxy = NULL;
50 gint subs_id = 0;
51
52 bool isMemAvailable = true;
53
54 MsgMutex mx;
55 MsgCndVar cv;
56
57
58 void MsgResourceMonitorInit(void);
59 void MsgResourceMonitorDeinit(void);
60
61 /*==================================================================================================
62                                      FUNCTION IMPLEMENTATION
63 ==================================================================================================*/
64
65 msg_error_t MsgPlgCreateHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
66 {
67         if (pPluginHandle == NULL) {
68                 MSG_DEBUG("SMS plugin: create handler error ");
69                 return MSG_ERR_NULL_POINTER;
70         } else {
71                 pPluginHandle->pfInitialize = SmsPlgInitialize;
72                 pPluginHandle->pfFinalize = SmsPlgFinalize;
73                 pPluginHandle->pfRegisterListener = SmsPlgRegisterListener;
74                 pPluginHandle->pfSubmitRequest = SmsPlgSubmitRequest;
75                 pPluginHandle->pfSaveSimMessage = SmsPlgSaveSimMessage;
76                 pPluginHandle->pfDeleteSimMessage = SmsPlgDeleteSimMessage;
77                 pPluginHandle->pfSetReadStatus = SmsPlgSetReadStatus;
78                 pPluginHandle->pfSetMemoryStatus = SmsPlgSetMemoryStatus;
79                 pPluginHandle->pfSetConfigData = SmsPlgSetConfigData;
80                 pPluginHandle->pfGetConfigData = SmsPlgGetConfigData;
81                 pPluginHandle->pfAddMessage = SmsPlgAddMessage;
82                 pPluginHandle->pfGetDefaultNetworkSimId = SmsPlgGetDefaultNetworkSimId;
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                 free(pPluginHandle);
96                 pPluginHandle = NULL;
97         }
98
99         MSG_DEBUG("SMS plugin: destory handler OK");
100
101         return MSG_SUCCESS;
102 }
103
104
105 msg_error_t SmsPlgInitialize()
106 {
107         MSG_BEGIN();
108
109         bool bReady = false;
110
111         for (int i = 0; i < 100; i++) {
112                 if (MsgSettingGetBool(VCONFKEY_TELEPHONY_READY, &bReady) != MSG_SUCCESS)
113                         MSG_INFO("MsgSettingGetBool() is failed");
114
115                 MSG_DEBUG("Get VCONFKEY_TELEPHONY_READY [%d].", bReady ? 1 : 0);
116
117                 if (bReady)
118                         break;
119
120                 sleep(1);
121         }
122
123         if (!bReady) {
124                 MSG_ERR("Fail to wait telephony init complete.");
125                 return MSG_ERR_PLUGIN_TAPIINIT;
126         }
127
128         int simCnt = 0;
129         char keyName[MAX_VCONFKEY_NAME_LEN];
130
131         SmsPluginDSHandler::instance()->initTelHandle();
132         simCnt = SmsPluginDSHandler::instance()->getTelHandleCount();
133
134         MSG_DEBUG("simCnt [%d]", simCnt);
135
136         for (int i = 1; i <= simCnt; i++) {
137                 memset(keyName, 0x00, sizeof(keyName));
138                 snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, i);
139                 MSG_DEBUG("set MSG_SIM_CHANGED to MSG_SIM_STATUS_NOT_FOUND.");
140                 if (MsgSettingSetInt(keyName, MSG_SIM_STATUS_NOT_FOUND) != MSG_SUCCESS)
141                         MSG_DEBUG("MsgSettingSetInt is failed!!");
142         }
143
144         SmsPluginCallback::instance()->registerEvent();
145
146         for (int i = 1; i <= simCnt; ++i) {
147                 TapiHandle *handle;
148                 handle = SmsPluginDSHandler::instance()->getTelHandle(i);
149                 SmsPluginSetting::instance()->setSimChangeStatus(handle, true);
150         }
151
152         /* set resource monitor */
153         MsgResourceMonitorInit();
154
155         MSG_END();
156
157         return MSG_SUCCESS;
158 }
159
160
161 msg_error_t SmsPlgFinalize()
162 {
163         MSG_BEGIN();
164
165         MsgResourceMonitorDeinit();
166
167         SmsPluginCallback::instance()->deRegisterEvent();
168
169         SmsPluginDSHandler::instance()->deinitTelHandle();
170
171         MSG_END();
172
173         return MSG_SUCCESS;
174 }
175
176
177 msg_error_t SmsPlgRegisterListener(MSG_PLUGIN_LISTENER_S *pListener)
178 {
179         MSG_BEGIN();
180
181         SmsPluginEventHandler::instance()->registerListener(pListener);
182
183         MSG_END();
184
185         return MSG_SUCCESS;
186 }
187
188
189 msg_error_t SmsPlgSubmitRequest(MSG_REQUEST_INFO_S *pReqInfo)
190 {
191         msg_error_t err = MSG_SUCCESS;
192
193         /* Add Submit SMS into DB */
194         if (pReqInfo->msgInfo.msgId == 0) {
195                 if (pReqInfo->msgInfo.msgPort.valid == false) {
196                         err = SmsPluginStorage::instance()->checkMessage(&(pReqInfo->msgInfo));
197
198                         if (err != MSG_SUCCESS) {
199                                 MSG_DEBUG("########  checkMessage Fail !! [err=%d]", err);
200                                 return MSG_ERR_PLUGIN_STORAGE;
201                         }
202
203                         err = SmsPluginStorage::instance()->addSmsMessage(&(pReqInfo->msgInfo));
204                         if (err != MSG_SUCCESS) {
205                                 MSG_DEBUG("########  addSmsMessage Fail !! [err=%d]", err);
206                                 return MSG_ERR_PLUGIN_STORAGE;
207                         }
208
209                         if (SmsPluginStorage::instance()->addSmsSendOption(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo)) != MSG_SUCCESS) {
210                                 MSG_DEBUG("########  addSmsSendOption Fail !!");
211                                 return MSG_ERR_PLUGIN_STORAGE;
212                         }
213                 }
214         }
215
216         /* Check SIM is present or not */
217         char keyName[MAX_VCONFKEY_NAME_LEN] = {0, };
218         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, pReqInfo->msgInfo.sim_idx);
219         int tmpValue = 0;
220         if (MsgSettingGetInt(keyName, &tmpValue) != MSG_SUCCESS) {
221                 MSG_INFO("MsgSettingGetInt() is failed");
222         }
223         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpValue;
224
225         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
226                 MSG_DEBUG("SIM is not present..");
227
228                 /* Update Msg Status */
229                 if (pReqInfo->msgInfo.msgPort.valid == false)
230                         SmsPluginStorage::instance()->updateSentMsg(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
231
232                 return MSG_ERR_NO_SIM;
233         }
234
235         SMS_REQUEST_INFO_S *request = NULL;
236
237         request = (SMS_REQUEST_INFO_S *)calloc(1, sizeof(SMS_REQUEST_INFO_S));
238
239         if (request != NULL) {
240                 request->reqId = pReqInfo->reqId;
241
242                 memcpy(&(request->msgInfo), &(pReqInfo->msgInfo), sizeof(MSG_MESSAGE_INFO_S));
243                 memcpy(&(request->sendOptInfo), &(pReqInfo->sendOptInfo), sizeof(MSG_SENDINGOPT_INFO_S));
244
245                 /* Add Request into Queue and Start UA Manger */
246                 SmsPluginUAManager::instance()->addReqEntity(request);
247
248                 free(request);
249         }
250
251
252         return MSG_SUCCESS;
253 }
254
255
256 msg_error_t SmsPlgSaveSimMessage(const MSG_MESSAGE_INFO_S *pMsgInfo, SMS_SIM_ID_LIST_S *pSimIdList)
257 {
258         /*  Check SIM is present or not */
259         char keyName[MAX_VCONFKEY_NAME_LEN];
260         memset(keyName, 0x00, sizeof(keyName));
261         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, pMsgInfo->sim_idx);
262         int tmpValue = 0;
263         if (MsgSettingGetInt(keyName, &tmpValue) != MSG_SUCCESS) {
264                 MSG_INFO("MsgSettingGetInt() is failed");
265         }
266         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpValue;
267
268         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
269                 MSG_DEBUG("SIM is not present..");
270                 return MSG_ERR_NO_SIM;
271         }
272
273         msg_error_t err = MSG_SUCCESS;
274
275         try {
276                 err = SmsPluginSimMsg::instance()->saveSimMessage(pMsgInfo, pSimIdList);
277         } catch (MsgException& e) {
278                 MSG_FATAL("%s", e.what());
279                 return MSG_ERR_PLUGIN_STORAGE;
280         } catch (exception& e) {
281                 MSG_FATAL("%s", e.what());
282                 return MSG_ERR_PLUGIN_STORAGE;
283         }
284
285         return err;
286 }
287
288
289 msg_error_t SmsPlgDeleteSimMessage(msg_sim_slot_id_t sim_idx, msg_sim_id_t SimMsgId)
290 {
291         /* Check SIM is present or not */
292         char keyName[MAX_VCONFKEY_NAME_LEN] = {0, };
293         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, sim_idx);
294         int tmpValue = 0;
295         if (MsgSettingGetInt(keyName, &tmpValue) != MSG_SUCCESS) {
296                 MSG_INFO("MsgSettingGetInt() is failed");
297         }
298         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpValue;
299
300         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
301                 MSG_DEBUG("SIM is not present..");
302                 return MSG_ERR_NO_SIM;
303         }
304
305         try {
306                 SmsPluginSimMsg::instance()->deleteSimMessage(sim_idx, SimMsgId);
307         } catch (MsgException& e) {
308                 MSG_FATAL("%s", e.what());
309                 return MSG_ERR_PLUGIN_STORAGE;
310         } catch (exception& e) {
311                 MSG_FATAL("%s", e.what());
312                 return MSG_ERR_PLUGIN_STORAGE;
313         }
314
315         return MSG_SUCCESS;
316 }
317
318
319 msg_error_t SmsPlgSetReadStatus(msg_sim_slot_id_t sim_idx, msg_sim_id_t SimMsgId)
320 {
321         /* Check SIM is present or not */
322         char keyName[MAX_VCONFKEY_NAME_LEN] = {0, };
323         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, sim_idx);
324         int tmpValue = 0;
325         if (MsgSettingGetInt(keyName, &tmpValue) != MSG_SUCCESS) {
326                 MSG_INFO("MsgSettingGetInt() is failed");
327         }
328         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpValue;
329
330         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
331                 MSG_DEBUG("SIM is not present..");
332                 return MSG_ERR_NO_SIM;
333         }
334
335         try {
336                 SmsPluginSimMsg::instance()->setReadStatus(sim_idx, SimMsgId);
337         } catch (MsgException& e) {
338                 MSG_FATAL("%s", e.what());
339                 return MSG_ERR_PLUGIN_STORAGE;
340         } catch (exception& e) {
341                 MSG_FATAL("%s", e.what());
342                 return MSG_ERR_PLUGIN_STORAGE;
343         }
344
345         return MSG_SUCCESS;
346 }
347
348
349 msg_error_t SmsPlgSetMemoryStatus(msg_sim_slot_id_t simIndex, msg_error_t Error)
350 {
351         /* Check SIM is present or not */
352         char keyName[MAX_VCONFKEY_NAME_LEN];
353
354         memset(keyName, 0x00, sizeof(keyName));
355         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, simIndex);
356         int tmpValue = 0;
357         if (MsgSettingGetInt(keyName, &tmpValue) != MSG_SUCCESS) {
358                 MSG_INFO("MsgSettingGetInt() is failed");
359         }
360         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpValue;
361
362         if (simStatus == MSG_SIM_STATUS_NOT_FOUND) {
363                 MSG_DEBUG("SIM is not present..");
364                 return MSG_ERR_NO_SIM;
365         }
366
367         int tapiRet = TAPI_API_SUCCESS;
368         int status = TAPI_NETTEXT_PDA_MEMORY_STATUS_AVAILABLE;
369
370         if (Error == MSG_ERR_SIM_STORAGE_FULL || Error == MSG_ERR_MESSAGE_COUNT_FULL)
371                 status = TAPI_NETTEXT_PDA_MEMORY_STATUS_FULL;
372
373         MSG_DEBUG("Set Status : [%d]", status);
374
375         TapiHandle *handle = SmsPluginDSHandler::instance()->getTelHandle(simIndex);
376
377         tapiRet = tel_set_sms_memory_status(handle, status, TapiEventMemoryStatus, NULL);
378
379         if (tapiRet == TAPI_API_SUCCESS)
380                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! #######");
381         else
382                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! return : [%d] #######", tapiRet);
383
384         return MSG_SUCCESS;
385 }
386
387
388 msg_error_t SmsPlgSetConfigData(const MSG_SETTING_S *pSetting)
389 {
390         try {
391                 SmsPluginSetting::instance()->setConfigData(pSetting);
392         } catch (MsgException& e) {
393                 MSG_FATAL("%s", e.what());
394                 return MSG_ERR_PLUGIN_SETTING;
395         } catch (exception& e) {
396                 MSG_FATAL("%s", e.what());
397                 return MSG_ERR_PLUGIN_SETTING;
398         }
399
400         return MSG_SUCCESS;
401 }
402
403
404 msg_error_t SmsPlgGetConfigData(MSG_SETTING_S *pSetting)
405 {
406         try {
407                 SmsPluginSetting::instance()->getConfigData(pSetting);
408         } catch (MsgException& e) {
409                 MSG_FATAL("%s", e.what());
410                 return MSG_ERR_PLUGIN_SETTING;
411         } catch (exception& e) {
412                 MSG_FATAL("%s", e.what());
413                 return MSG_ERR_PLUGIN_SETTING;
414         }
415
416         return MSG_SUCCESS;
417 }
418
419
420 msg_error_t SmsPlgAddMessage(MSG_MESSAGE_INFO_S *pMsgInfo,  MSG_SENDINGOPT_INFO_S* pSendOptInfo, char* pFileData)
421 {
422         int *simIdList = (int*)pFileData;
423         try {
424                 SmsPluginStorage::instance()->addSmsSendOption(pMsgInfo, pSendOptInfo);
425                 if (simIdList)
426                         SmsPluginStorage::instance()->addSimMessage(pMsgInfo, simIdList);
427         } catch (MsgException& e) {
428                 MSG_FATAL("%s", e.what());
429                 return MSG_ERR_PLUGIN_SETTING;
430         } catch (exception& e) {
431                 MSG_FATAL("%s", e.what());
432                 return MSG_ERR_PLUGIN_SETTING;
433         }
434
435         return MSG_SUCCESS;
436 }
437
438
439 msg_error_t SmsPlgGetDefaultNetworkSimId(int *simId)
440 {
441         try {
442                 SmsPluginDSHandler::instance()->getDefaultNetworkSimId(simId);
443         } catch (MsgException& e) {
444                 MSG_FATAL("%s", e.what());
445                 return MSG_ERR_PLUGIN_SETTING;
446         } catch (exception& e) {
447                 MSG_FATAL("%s", e.what());
448                 return MSG_ERR_PLUGIN_SETTING;
449         }
450
451         return MSG_SUCCESS;
452 }
453
454
455 static void on_change_received(GDBusConnection *connection, const gchar *sender_name,
456                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
457                 GVariant *parameters, gpointer user_data)
458 {
459         MSG_DEBUG("signal_name = [%s]", signal_name);
460
461         if (g_strcmp0(signal_name, MEMBER_NAME) == 0) {
462                 gint memStatus;
463                 g_variant_get(parameters, "(i)", &memStatus);
464                 MSG_DEBUG("memStatus = [%d]", memStatus);
465                 if (memStatus == 0) {
466                         int sim_count = SmsPluginDSHandler::instance()->getTelHandleCount();
467
468                         for (int i = 0; i < sim_count; i++) {
469                                 SmsPlgSetMemoryStatus(i, MSG_SUCCESS);
470                         }
471                 }
472         }
473 }
474
475 void MsgResourceMonitorInit(void)
476 {
477         MSG_BEGIN();
478
479         GError *error = NULL;
480
481         if (gdbus_conn) {
482                 g_object_unref(gdbus_conn);
483                 gdbus_conn = NULL;
484         }
485
486         gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
487         if (error) {
488                 MSG_FATAL("g_bus_get_sync() failed : %s", error->message);
489                 g_error_free(error);
490                 error = NULL;
491                 goto _DBUS_ERROR;
492         }
493
494         if (gdbus_proxy) {
495                 g_object_unref(gdbus_proxy);
496                 gdbus_proxy = NULL;
497         }
498
499         gdbus_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE,
500                                                         NULL, BUS_NAME, PATH_NAME, INTERFACE_NAME, NULL, &error);
501         if (error) {
502                 MSG_FATAL("g_dbus_proxy_new_sync() failed : %s", error->message);
503                 g_error_free(error);
504                 error = NULL;
505                 goto _DBUS_ERROR;
506         }
507
508         subs_id = g_dbus_connection_signal_subscribe(gdbus_conn, NULL,
509                                                         INTERFACE_NAME, MEMBER_NAME, PATH_NAME,
510                                                         NULL, G_DBUS_SIGNAL_FLAGS_NONE,
511                                                         on_change_received,
512                                                         NULL, NULL);
513         MSG_END();
514         return;
515
516 _DBUS_ERROR:
517         if (gdbus_conn) {
518                 g_object_unref(gdbus_conn);
519                 gdbus_conn = NULL;
520         }
521
522         if (gdbus_proxy) {
523                 g_object_unref(gdbus_proxy);
524                 gdbus_proxy = NULL;
525         }
526
527         MSG_END();
528         return;
529 }
530
531
532 void MsgResourceMonitorDeinit(void)
533 {
534         MSG_BEGIN();
535
536         if (subs_id) {
537                 g_dbus_connection_signal_unsubscribe(gdbus_conn, subs_id);
538                 subs_id = 0;
539         }
540
541         if (gdbus_conn) {
542                 g_object_unref(gdbus_conn);
543                 gdbus_conn = NULL;
544         }
545
546         if (gdbus_proxy) {
547                 g_object_unref(gdbus_proxy);
548                 gdbus_proxy = NULL;
549         }
550
551         MSG_END();
552 }