RSA sync with private
[platform/core/messaging/msg-service.git] / framework / main.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 /*==================================================================================================
18                                          INCLUDE FILES
19 ==================================================================================================*/
20 #include "MsgDebug.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgMemory.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgPluginManager.h"
26 #include "MsgSettingHandler.h"
27 #include "MsgStorageHandler.h"
28 #include "MsgSubmitHandler.h"
29 #include "MsgDeliverHandler.h"
30 #include "MsgTransManager.h"
31 #include "MsgStorageTypes.h"
32 #include "MsgSoundPlayer.h"
33 #include "MsgCmdHandler.h"
34 #include "MsgUtilStorage.h"
35 #include "MsgNotificationWrapper.h"
36
37 #include <errno.h>
38 #include <glib.h>
39 #include <dbus/dbus-glib.h>
40 #include <sys/stat.h>
41 #include <wait.h>
42
43 static GMainLoop* mainloop = NULL;
44
45
46 /*==================================================================================================
47                                      DEFINES
48 ==================================================================================================*/
49 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
50
51
52 /*==================================================================================================
53                                      FUNCTION IMPLEMENTATION
54 ==================================================================================================*/
55 msg_error_t InitMmsDir()
56 {
57         if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
58         {
59                 if (errno == EEXIST)
60                 {
61                         MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
62                 }
63                 else
64                 {
65                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
66                         return MSG_ERR_DB_MAKE_DIR;
67                 }
68         }
69
70         if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
71         {
72                 if (errno == EEXIST)
73                 {
74                         MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
75                 }
76                 else
77                 {
78                         MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
79                         return MSG_ERR_DB_MAKE_DIR;
80                 }
81         }
82
83         if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
84         {
85                 if (errno == EEXIST)
86                 {
87                         MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
88                 }
89                 else
90                 {
91                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
92                         return MSG_ERR_DB_MAKE_DIR;
93                 }
94         }
95
96         if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
97                 if (errno == EEXIST) {
98                         MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
99                 } else {
100                         MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
101                         return MSG_ERR_DB_MAKE_DIR;
102                 }
103         }
104
105         if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
106         {
107                 if (errno == EEXIST)
108                 {
109                         MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
110
111                         // if IPC data path is already exist, clear all files in folder.
112                         char exeStr[1024];
113                         snprintf(exeStr, 1024, "rm %s*.DATA", MSG_IPC_DATA_PATH);
114                         system(exeStr);
115                 }
116                 else
117                 {
118                         MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
119                         return MSG_ERR_DB_MAKE_DIR;
120                 }
121         }
122
123         if(chmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG ) !=0) { //public shared file: pass data by file
124                 MSG_DEBUG("Fail to chmod [%s].", MSG_IPC_DATA_PATH);
125         }
126         chown( MSG_IPC_DATA_PATH, 0, 6502 );
127
128         return MSG_SUCCESS;
129 }
130
131
132 void SendMobileTrackerMsg()
133 {
134         MSG_BEGIN();
135
136         bool bEnabled = false;
137
138         if (MsgSettingGetBool(VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL, &bEnabled) < 0)
139         {
140                 MSG_DEBUG("Can't read VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL");
141                 return;
142         }
143
144         if (bEnabled == false)
145         {
146                 MSG_DEBUG("Mobile Tracker Option [%d]", bEnabled);
147                 return;
148         }
149         // to wait modem init
150         // temporary code.
151         else
152         {
153                 MSG_DEBUG("Waiting for modem ready, 22 sec.");
154                 sleep(22);
155         }
156
157         MSG_REQUEST_INFO_S req = {0};
158
159         req.sendOptInfo.bSetting = false;
160
161         req.msgInfo.msgId = 0;
162         req.msgInfo.threadId = 0;
163         req.msgInfo.folderId = MSG_DRAFT_ID;
164         req.msgInfo.msgType.mainType = MSG_SMS_TYPE;
165         req.msgInfo.msgType.subType = MSG_NORMAL_SMS;
166         req.msgInfo.msgType.classType = MSG_CLASS_NONE;
167         req.msgInfo.storageId = MSG_STORAGE_PHONE;
168         req.msgInfo.displayTime = 0;
169         req.msgInfo.networkStatus = MSG_NETWORK_NOT_SEND;
170         req.msgInfo.encodeType = MSG_ENCODE_AUTO;
171         req.msgInfo.bRead = false;
172         req.msgInfo.bProtected = false;
173         req.msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL;
174         req.msgInfo.direction = MSG_DIRECTION_TYPE_MO;
175         req.msgInfo.msgPort.valid = false;
176         req.msgInfo.bTextSms = true;
177
178         // Get Message Text
179         char* msgText = NULL;
180
181         msgText = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_MESSAGE_STR);
182
183         if (msgText == NULL)
184         {
185                 MSG_DEBUG("Assign Default Msg");
186                 msgText = strdup(MSG_MOBILE_TRACKER_MSG);
187                 if (msgText == NULL) {
188                         MSG_DEBUG("msgText is NULL.");
189                         return;
190                 }
191         }
192
193         MSG_DEBUG("mobile tracker msg : [%s]", msgText);
194
195         req.msgInfo.dataSize = strlen(msgText);
196         strncpy(req.msgInfo.msgText, msgText, req.msgInfo.dataSize);
197
198         // Get Address List
199         char *orgRecipientList = NULL;
200
201         orgRecipientList = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_RECIPIENTS_STR);
202
203         if (orgRecipientList == NULL)
204         {
205                 MSG_DEBUG("recipient list is NULL");
206                 free(msgText);
207                 msgText = NULL;
208                 return;
209         }
210
211         int len = strlen(orgRecipientList);
212         char recipientList[len + 1];
213
214         memset(recipientList, 0, len + 1);
215         memcpy(recipientList, orgRecipientList, len);
216
217         MSG_DEBUG("recipient list : [%s]", recipientList);
218
219         req.msgInfo.nAddressCnt = 1;
220
221         char *token;
222
223         token = strtok(recipientList, "|");
224
225         MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
226         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
227
228         msg_error_t err = MSG_SUCCESS;
229
230         while (token)
231         {
232                 req.msgInfo.addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
233                 req.msgInfo.addressList[0].recipientType = MSG_RECIPIENTS_TYPE_TO;
234
235                 memset(req.msgInfo.addressList[0].addressVal, 0x00, MAX_ADDRESS_VAL_LEN);
236                 strncpy(req.msgInfo.addressList[0].addressVal, token, MAX_ADDRESS_VAL_LEN);
237
238                 MSG_DEBUG("address : [%s]", req.msgInfo.addressList[0].addressVal);
239
240                 if (plg != NULL)
241                 {
242                         MSG_DEBUG("mobile tracker msg : [%s]", msgText);
243
244                         err = plg->submitReq(&req);
245
246                         if (err != MSG_SUCCESS)
247                         {
248                                 MSG_DEBUG("fail to send mobile tracker msg : [%d]", err);
249                                 break;
250                         }
251                 }
252
253                 token = strtok(NULL, "|");
254         }
255
256         if (msgText) {
257                 free(msgText);
258                 msgText = NULL;
259         }
260
261         if (orgRecipientList) {
262                 free(orgRecipientList);
263                 orgRecipientList = NULL;
264         }
265
266
267         MSG_END();
268
269         return;
270 }
271
272
273 void* StartMsgServer(void*)
274 {
275
276         MsgOpenContactSvc();
277
278         try
279         {
280                 MsgTransactionManager::instance()->run();
281         }
282         catch (MsgException& e)
283         {
284                 MSG_FATAL("%s", e.what());
285         }
286         catch (exception& e)
287         {
288                 MSG_FATAL("%s", e.what());
289         }
290
291         if (g_main_loop_is_running(mainloop))
292                 g_main_loop_quit(mainloop);
293
294         MsgCloseContactSvc();
295
296         return (void*)0;
297 }
298
299
300 void* InitMsgServer(void*)
301 {
302         msg_error_t err = MSG_SUCCESS;
303
304         try
305         {
306                 // plugin manager initialize
307                 MsgPluginManager::instance()->initialize();
308         }
309         catch (MsgException& e)
310         {
311                 MSG_FATAL("%s", e.what());
312         }
313         catch (exception& e)
314         {
315                 MSG_FATAL("%s", e.what());
316         }
317
318         MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
319         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
320
321         // storage handler initialize
322         err = MsgStoInitDB(false);
323
324         if (err != MSG_SUCCESS) {
325                 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
326         }
327
328         // Set Msg FW Ready Flag
329         MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
330         MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
331
332         if (plg == NULL) {
333                 MSG_DEBUG("No plugin for %d type", mainType);
334
335                 MsgReleaseMemory();
336                 return (void*)0;
337         }
338
339         MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
340
341         // Check Sim Status
342         if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
343
344                 // Add the change of SIM to vconf
345                 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
346                         MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
347                 }
348
349                 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
350                         // Check Device Status
351                         if (plg->checkDeviceStatus() != MSG_SUCCESS) {
352                                 MSG_DEBUG("checkDeviceStatus() error");
353
354                                 MsgReleaseMemory();
355
356                                 return (void*)0;
357                         }
358                 }
359
360                 // Init SIM Message
361                 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
362                         MSG_DEBUG("Fail to Initialize SIM Message");
363                 }
364
365                 // Init SIM Configuration
366                 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
367                         MSG_DEBUG("Fail to Initialize SIM Configuration");
368                 }
369
370                 // Add SendMobileTrackerMsg() to GMainLoop
371                 if (simStatus == MSG_SIM_STATUS_CHANGED) {
372                         MSG_DEBUG("Send Mobile Tracker Message");
373
374                         SendMobileTrackerMsg();
375                 }
376         } else {
377                 MSG_DEBUG("checkSimStatus() error");
378         }
379
380         MsgReleaseMemory();
381
382         // Try to connect contact server  if it is not opened.
383         MsgOpenContactSvc();
384
385         // Register Callback to get the change of contact
386         MsgInitContactSvc(&MsgContactChangedCallback);
387
388         MsgCleanAndResetNoti();
389         MsgSoundInitRepeatAlarm();
390
391         return (void*)0;
392 }
393
394
395 static gboolean InitThreadFunc(void* pData)
396 {
397         MSG_BEGIN();
398
399         pthread_t initThreadId;
400
401         // initialize msg fw
402         if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
403         {
404                 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
405                 return -1;
406         }
407
408         pthread_detach(initThreadId);
409
410         MSG_END();
411
412         return FALSE;
413 }
414
415
416 int main(void)
417 {
418 #if !GLIB_CHECK_VERSION(2, 31, 0)
419         g_thread_init(NULL);
420 #endif
421         dbus_g_thread_init();
422
423 ////////////////////////////////////
424
425 /// set to ignore child process terminated signal.
426 signal( SIGCHLD, SIG_IGN );
427
428 ////////////////////////////////////
429
430
431         MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
432
433         // Reset message server ready flag
434         MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
435
436         // Connect to DB
437         //      MsgStoConnectDB();
438
439         // Open Contact Service
440         MsgOpenContactSvc();
441
442         // Clean up mms dir
443         InitMmsDir();
444
445         // Regist vconf CB.
446         MsgSettingRegVconfCB();
447
448         pthread_t startThreadId;
449
450         // start transaction manager
451         if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
452         {
453                 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
454                 return -1;
455         }
456
457         MsgTransactionManager::instance()->getTMStatus();
458
459         mainloop = g_main_loop_new(NULL, FALSE);
460
461         g_type_init();
462
463         g_idle_add(InitThreadFunc, NULL);
464
465         if (mainloop != NULL)
466         {
467                 MSG_DEBUG("Start Messaging Framework!!!");
468
469                 // Run GMainLoop
470                 g_main_loop_run(mainloop);
471         }
472         else
473         {
474                 MSG_DEBUG("Fail to start Messaging Framework!!!");
475         }
476
477         // Remove vconf CB
478         MsgSettingRemoveVconfCB();
479
480         // Close Contact Sevice
481         MsgCloseContactSvc();
482
483         // Disconnect to DB
484         MsgStoDisconnectDB();
485
486         return 0;
487 }
488