ebb3f268a039e48c762e0f0f5951ab1d93becdb0
[platform/core/messaging/msg-service.git] / plugin / sms_cdma_plugin / SmsCdmaPluginMain.cpp
1 /*
2  * Copyright (c) 2015 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 "SmsCdmaPluginTransport.h"
25 #include "SmsCdmaPluginStorage.h"
26 #include "SmsCdmaPluginCallback.h"
27 #include "SmsCdmaPluginEventHandler.h"
28 #include "SmsCdmaPluginUAManager.h"
29 #include "SmsCdmaPluginSetting.h"
30 #include "SmsCdmaPluginMain.h"
31 #include <gio/gio.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 #define BUS_NAME "org.tizen.system.deviced"
43 #define PATH_NAME "/Org/Tizen/System/DeviceD/Lowmem"
44 #define INTERFACE_NAME BUS_NAME".lowmem"
45 #define MEMBER_NAME "Full"
46
47 struct tapi_handle *pTapiHandle = NULL;
48 bool isMemAvailable = true;
49
50 GDBusConnection *gdbus_conn = NULL;
51 GDBusProxy *gdbus_proxy = NULL;
52 gint subs_id = 0;
53
54 Mutex mx;
55 CndVar cv;
56
57
58 void MsgResourceMonitorInit(void);
59 void MsgResourceMonitorDeinit(void);
60
61 /*==================================================================================================
62                                 FUNCTION IMPLEMENTATION
63 ==================================================================================================*/
64
65 static void MsgTapiInitCB(keynode_t *key, void* data)
66 {
67         MSG_DEBUG("MsgTapiInitCB is called.");
68         mx.lock();
69         cv.signal();
70         mx.unlock();
71 }
72
73
74 msg_error_t MsgPlgCreateHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
75 {
76         if (pPluginHandle == NULL) {
77                 MSG_DEBUG("SMS plugin: create handler error ");
78                 return MSG_ERR_NULL_POINTER;
79         } else {
80                 pPluginHandle->pfInitialize = SmsPlgInitialize;
81                 pPluginHandle->pfFinalize = SmsPlgFinalize;
82                 pPluginHandle->pfRegisterListener = SmsPlgRegisterListener;
83                 pPluginHandle->pfSubmitRequest = SmsPlgSubmitRequest;
84                 pPluginHandle->pfSaveSimMessage = SmsPlgSaveSimMessage;
85                 pPluginHandle->pfDeleteSimMessage = SmsPlgDeleteSimMessage;
86                 pPluginHandle->pfSetReadStatus = SmsPlgSetReadStatus;
87                 pPluginHandle->pfSetMemoryStatus = SmsPlgSetMemoryStatus;
88                 pPluginHandle->pfSetConfigData = SmsPlgSetConfigData;
89                 pPluginHandle->pfGetConfigData = SmsPlgGetConfigData;
90                 pPluginHandle->pfAddMessage = SmsPlgAddMessage;
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                 free(pPluginHandle);
104                 pPluginHandle = NULL;
105         }
106
107         MSG_DEBUG("SMS plugin: destory handler OK");
108
109         return MSG_SUCCESS;
110 }
111
112
113 msg_error_t SmsPlgInitialize()
114 {
115         MSG_BEGIN();
116
117         bool bReady = false;
118
119         for (int i = 0; i < 100; i++) {
120                 if (MsgSettingGetBool(VCONFKEY_TELEPHONY_READY, &bReady) != MSG_SUCCESS)
121                         MSG_INFO("MsgSettingGetBool() is failed");
122
123                 MSG_DEBUG("Get VCONFKEY_TELEPHONY_READY [%d].", bReady ? 1 : 0);
124
125                 if (bReady)
126                         break;
127
128                 sleep(1);
129         }
130
131         if (!bReady) {
132                 MSG_DEBUG("Fail to wait telephony init complete.");
133                 return MSG_ERR_PLUGIN_TAPIINIT;
134         }
135
136         pTapiHandle = tel_init(NULL);
137
138         if (pTapiHandle) {
139                 /* register event. */
140                 SmsPluginCallback::instance()->registerEvent();
141
142                 /* set sim change status. */
143                 MSG_DEBUG("Try to initialize SIM on init");
144                 SmsPluginSetting::instance()->setSimChangeStatus();
145
146                 /* set resource monitor */
147                 MsgResourceMonitorInit();
148         }
149
150         MSG_END();
151
152         return MSG_SUCCESS;
153 }
154
155
156 msg_error_t SmsPlgFinalize()
157 {
158         MSG_BEGIN();
159
160         MsgResourceMonitorDeinit();
161
162         if (!pTapiHandle)
163                 return MSG_ERR_PLUGIN_TAPIINIT;
164
165         SmsPluginCallback::instance()->deRegisterEvent();
166
167         tel_deinit(pTapiHandle);
168
169         MSG_END();
170
171         return MSG_SUCCESS;
172 }
173
174
175 msg_error_t SmsPlgRegisterListener(MSG_PLUGIN_LISTENER_S *pListener)
176 {
177         MSG_BEGIN();
178
179         SmsPluginEventHandler::instance()->registerListener(pListener);
180
181         MSG_END();
182
183         return MSG_SUCCESS;
184 }
185
186
187 msg_error_t SmsPlgSubmitRequest(MSG_REQUEST_INFO_S *pReqInfo)
188 {
189         msg_error_t err = MSG_SUCCESS;
190
191         /* Add Submit SMS into DB */
192         if (pReqInfo->msgInfo.msgId == 0) {
193                 if (pReqInfo->msgInfo.msgPort.valid == false) {
194                         err = SmsPluginStorage::instance()->checkMessage(&(pReqInfo->msgInfo));
195
196                         if (err != MSG_SUCCESS) {
197                                 MSG_DEBUG("########  checkMessage Fail !! [err=%d]", err);
198                                 return MSG_ERR_PLUGIN_STORAGE;
199                         }
200
201                         err = SmsPluginStorage::instance()->addSmsMessage(&(pReqInfo->msgInfo));
202                         if (err != MSG_SUCCESS) {
203                                 MSG_DEBUG("########  addSmsMessage Fail !! [err=%d]", err);
204                                 return MSG_ERR_PLUGIN_STORAGE;
205                         }
206
207                         if (SmsPluginStorage::instance()->addSmsSendOption(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo)) != MSG_SUCCESS) {
208                                 MSG_DEBUG("########  addSmsSendOption Fail !!");
209                                 return MSG_ERR_PLUGIN_STORAGE;
210                         }
211                 }
212         }
213
214         sms_request_info_s *request = NULL;
215
216         request = (sms_request_info_s *)calloc(1, sizeof(sms_request_info_s));
217
218         if (request != NULL) {
219                 request->reqId = pReqInfo->reqId;
220
221                 memcpy(&(request->msgInfo), &(pReqInfo->msgInfo), sizeof(MSG_MESSAGE_INFO_S));
222                 memcpy(&(request->sendOptInfo), &(pReqInfo->sendOptInfo), sizeof(MSG_SENDINGOPT_INFO_S));
223
224                 /* Add Request into Queue and Start UA Manger */
225                 SmsPluginUAManager::instance()->addReqEntity(request);
226
227                 free(request);
228         }
229
230
231         return MSG_SUCCESS;
232 }
233
234
235 msg_error_t SmsPlgSaveSimMessage(const MSG_MESSAGE_INFO_S *pMsgInfo, SMS_SIM_ID_LIST_S *pSimIdList)
236 {
237         MSG_DEBUG("CDMA does not support sim card operations.");
238         return MSG_SUCCESS;
239 }
240
241
242 msg_error_t SmsPlgDeleteSimMessage(msg_sim_id_t SimMsgId)
243 {
244         MSG_DEBUG("CDMA does not support sim card operations.");
245         return MSG_SUCCESS;
246 }
247
248
249 msg_error_t SmsPlgSetReadStatus(msg_sim_id_t SimMsgId)
250 {
251         MSG_DEBUG("CDMA does not support sim card operations.");
252         return MSG_SUCCESS;
253 }
254
255
256 msg_error_t SmsPlgSetMemoryStatus(msg_error_t Error)
257 {
258         int tapiRet = TAPI_API_SUCCESS;
259         int status = TAPI_NETTEXT_PDA_MEMORY_STATUS_AVAILABLE;
260
261         if (Error == MSG_ERR_SIM_STORAGE_FULL || Error == MSG_ERR_MESSAGE_COUNT_FULL) {
262                 status = TAPI_NETTEXT_PDA_MEMORY_STATUS_FULL;
263         }
264
265         MSG_DEBUG("Set Status : [%d]", status);
266
267         tapiRet = tel_set_sms_memory_status(pTapiHandle, status, TapiEventMemoryStatus, NULL);
268
269         if (tapiRet == TAPI_API_SUCCESS) {
270                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! #######");
271         } else {
272                 MSG_DEBUG("########  tel_set_sms_memory_status() Success !!! return : [%d] #######", tapiRet);
273         }
274
275         return MSG_SUCCESS;
276 }
277
278
279 msg_error_t SmsPlgSetConfigData(const MSG_SETTING_S *pSetting)
280 {
281         try {
282                 SmsPluginSetting::instance()->setConfigData(pSetting);
283         } catch (MsgException& e) {
284                 MSG_FATAL("%s", e.what());
285                 return MSG_ERR_PLUGIN_SETTING;
286         } catch (exception& e) {
287                 MSG_FATAL("%s", e.what());
288                 return MSG_ERR_PLUGIN_SETTING;
289         }
290
291         return MSG_SUCCESS;
292 }
293
294
295 msg_error_t SmsPlgGetConfigData(MSG_SETTING_S *pSetting)
296 {
297         try {
298                 SmsPluginSetting::instance()->getConfigData(pSetting);
299         } catch (MsgException& e) {
300                 MSG_FATAL("%s", e.what());
301                 return MSG_ERR_PLUGIN_SETTING;
302         } catch (exception& e) {
303                 MSG_FATAL("%s", e.what());
304                 return MSG_ERR_PLUGIN_SETTING;
305         }
306
307         return MSG_SUCCESS;
308 }
309
310
311 msg_error_t SmsPlgAddMessage(MSG_MESSAGE_INFO_S *pMsgInfo,  MSG_SENDINGOPT_INFO_S* pSendOptInfo, char* pFileData)
312 {
313         MSG_DEBUG("CDMA does not support sim card operations.");
314         return MSG_SUCCESS;
315 }
316
317
318 static void on_change_received(GDBusConnection *connection, const gchar *sender_name,
319                 const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
320                 GVariant *parameters, gpointer user_data)
321 {
322         MSG_DEBUG("signal_name = [%s]", signal_name);
323
324         if (g_strcmp0(signal_name, MEMBER_NAME) == 0) {
325                 gint memStatus;
326                 g_variant_get(parameters, "(i)", &memStatus);
327                 MSG_DEBUG("memStatus = [%d]", memStatus);
328                 if (memStatus == 0) {
329                         SmsPlgSetMemoryStatus(MSG_SUCCESS);
330                 }
331         }
332 }
333
334
335 void MsgResourceMonitorInit(void)
336 {
337     MSG_BEGIN();
338
339         GError *error = NULL;
340
341         if (gdbus_conn) {
342                 g_object_unref(gdbus_conn);
343                 gdbus_conn = NULL;
344         }
345
346         gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
347         if (error) {
348                 MSG_FATAL("g_bus_get_sync() failed : %s", error->message);
349                 g_error_free(error);
350                 error = NULL;
351                 goto _DBUS_ERROR;
352         }
353
354         if (gdbus_proxy) {
355                 g_object_unref(gdbus_proxy);
356                 gdbus_proxy = NULL;
357         }
358
359         gdbus_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE,
360                                                         NULL, BUS_NAME, PATH_NAME, INTERFACE_NAME, NULL, &error);
361         if (error) {
362                 MSG_FATAL("g_dbus_proxy_new_sync() failed : %s", error->message);
363                 g_error_free(error);
364                 error = NULL;
365                 goto _DBUS_ERROR;
366         }
367
368         subs_id = g_dbus_connection_signal_subscribe(gdbus_conn, NULL,
369                                                         INTERFACE_NAME, MEMBER_NAME, PATH_NAME,
370                                                         NULL, G_DBUS_SIGNAL_FLAGS_NONE,
371                                                         on_change_received,
372                                                         NULL, NULL);
373         MSG_END();
374         return;
375
376 _DBUS_ERROR:
377         if (gdbus_conn) {
378                 g_object_unref(gdbus_conn);
379                 gdbus_conn = NULL;
380         }
381
382         if (gdbus_proxy) {
383                 g_object_unref(gdbus_proxy);
384                 gdbus_proxy = NULL;
385         }
386
387         MSG_END();
388         return;
389 }
390
391
392 void MsgResourceMonitorDeinit(void)
393 {
394         MSG_BEGIN();
395
396         if (subs_id) {
397                 g_dbus_connection_signal_unsubscribe(gdbus_conn, subs_id);
398                 subs_id = 0;
399         }
400
401         if (gdbus_conn) {
402                 g_object_unref(gdbus_conn);
403                 gdbus_conn = NULL;
404         }
405
406         if (gdbus_proxy) {
407                 g_object_unref(gdbus_proxy);
408                 gdbus_proxy = NULL;
409         }
410
411         MSG_END();
412 }