4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 /*==================================================================================================
22 ==================================================================================================*/
24 #include "MsgException.h"
25 #include "MsgContact.h"
26 #include "MsgMemory.h"
27 #include "MsgGconfWrapper.h"
28 #include "MsgPluginManager.h"
29 #include "MsgSettingHandler.h"
30 #include "MsgStorageHandler.h"
31 #include "MsgSubmitHandler.h"
32 #include "MsgDeliverHandler.h"
33 #include "MsgTransManager.h"
34 #include "MsgStorageTypes.h"
35 #include "MsgSoundPlayer.h"
36 #include "MsgCmdHandler.h"
37 #include "MsgUtilStorage.h"
38 #include "MsgNotificationWrapper.h"
42 #include <dbus/dbus-glib.h>
46 static GMainLoop* mainloop = NULL;
49 /*==================================================================================================
51 ==================================================================================================*/
52 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
55 /*==================================================================================================
56 FUNCTION IMPLEMENTATION
57 ==================================================================================================*/
58 msg_error_t InitMmsDir()
60 if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
64 MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
68 MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
69 return MSG_ERR_DB_MAKE_DIR;
73 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
77 MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
81 MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
82 return MSG_ERR_DB_MAKE_DIR;
86 if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
90 MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
94 MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
95 return MSG_ERR_DB_MAKE_DIR;
99 if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
100 if (errno == EEXIST) {
101 MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
103 MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
104 return MSG_ERR_DB_MAKE_DIR;
108 if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
112 MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
114 // if IPC data path is already exist, clear all files in folder.
116 snprintf(exeStr, 1024, "rm %s/*.DATA", MSG_IPC_DATA_PATH);
121 MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
122 return MSG_ERR_DB_MAKE_DIR;
126 if(chmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG ) !=0) { //public shared file: pass data by file
127 MSG_DEBUG("Fail to chmod [%s].", MSG_IPC_DATA_PATH);
129 chown( MSG_IPC_DATA_PATH, 0, 6502 );
135 void SendMobileTrackerMsg()
139 bool bEnabled = false;
141 if (MsgSettingGetBool(VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL, &bEnabled) < 0)
143 MSG_DEBUG("Can't read VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL");
147 if (bEnabled == false)
149 MSG_DEBUG("Mobile Tracker Option [%d]", bEnabled);
152 // to wait modem init
156 MSG_DEBUG("Waiting for modem ready, 22 sec.");
160 MSG_REQUEST_INFO_S req = {0};
162 req.sendOptInfo.bSetting = false;
164 req.msgInfo.msgId = 0;
165 req.msgInfo.threadId = 0;
166 req.msgInfo.folderId = MSG_DRAFT_ID;
167 req.msgInfo.msgType.mainType = MSG_SMS_TYPE;
168 req.msgInfo.msgType.subType = MSG_NORMAL_SMS;
169 req.msgInfo.msgType.classType = MSG_CLASS_NONE;
170 req.msgInfo.storageId = MSG_STORAGE_PHONE;
171 req.msgInfo.displayTime = 0;
172 req.msgInfo.networkStatus = MSG_NETWORK_NOT_SEND;
173 req.msgInfo.encodeType = MSG_ENCODE_AUTO;
174 req.msgInfo.bRead = false;
175 req.msgInfo.bProtected = false;
176 req.msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL;
177 req.msgInfo.direction = MSG_DIRECTION_TYPE_MO;
178 req.msgInfo.msgPort.valid = false;
179 req.msgInfo.bTextSms = true;
182 char* msgText = NULL;
184 msgText = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_MESSAGE_STR);
188 MSG_DEBUG("Assign Default Msg");
189 msgText = strdup(MSG_MOBILE_TRACKER_MSG);
190 if (msgText == NULL) {
191 MSG_DEBUG("msgText is NULL.");
196 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
198 req.msgInfo.dataSize = strlen(msgText);
199 strncpy(req.msgInfo.msgText, msgText, req.msgInfo.dataSize);
202 char *orgRecipientList = NULL;
204 orgRecipientList = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_RECIPIENTS_STR);
206 if (orgRecipientList == NULL)
208 MSG_DEBUG("recipient list is NULL");
214 int len = strlen(orgRecipientList);
215 char recipientList[len + 1];
217 memset(recipientList, 0, len + 1);
218 memcpy(recipientList, orgRecipientList, len);
220 MSG_DEBUG("recipient list : [%s]", recipientList);
222 req.msgInfo.nAddressCnt = 1;
226 token = strtok(recipientList, "|");
228 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
229 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
231 msg_error_t err = MSG_SUCCESS;
235 req.msgInfo.addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
236 req.msgInfo.addressList[0].recipientType = MSG_RECIPIENTS_TYPE_TO;
238 memset(req.msgInfo.addressList[0].addressVal, 0x00, MAX_ADDRESS_VAL_LEN);
239 strncpy(req.msgInfo.addressList[0].addressVal, token, MAX_ADDRESS_VAL_LEN);
241 MSG_DEBUG("address : [%s]", req.msgInfo.addressList[0].addressVal);
245 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
247 err = plg->submitReq(&req);
249 if (err != MSG_SUCCESS)
251 MSG_DEBUG("fail to send mobile tracker msg : [%d]", err);
256 token = strtok(NULL, "|");
264 if (orgRecipientList) {
265 free(orgRecipientList);
266 orgRecipientList = NULL;
276 void* StartMsgServer(void*)
283 MsgTransactionManager::instance()->run();
285 catch (MsgException& e)
287 MSG_FATAL("%s", e.what());
291 MSG_FATAL("%s", e.what());
294 if (g_main_loop_is_running(mainloop))
295 g_main_loop_quit(mainloop);
297 MsgCloseContactSvc();
303 void* InitMsgServer(void*)
305 msg_error_t err = MSG_SUCCESS;
311 // plugin manager initialize
312 MsgPluginManager::instance()->initialize();
314 catch (MsgException& e)
316 MSG_FATAL("%s", e.what());
320 MSG_FATAL("%s", e.what());
323 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
324 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
326 // storage handler initialize
327 err = MsgStoInitDB(false);
329 if (err != MSG_SUCCESS) {
330 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
333 // Set Msg FW Ready Flag
334 if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true) != MSG_SUCCESS)
335 MSG_DEBUG("MsgSettingSetBool FAIL : VCONFKEY_MSG_SERVER_READY");
336 MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
339 MSG_DEBUG("No plugin for %d type", mainType);
345 // Clear and reset notification
346 MsgCleanAndResetNoti();
348 MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
351 if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
353 // Add the change of SIM to vconf
354 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
355 MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
358 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
359 // Check Device Status
360 if (plg->checkDeviceStatus() != MSG_SUCCESS) {
361 MSG_DEBUG("checkDeviceStatus() error");
370 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
371 MSG_DEBUG("Fail to Initialize SIM Message");
374 // Init SIM Configuration
375 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
376 MSG_DEBUG("Fail to Initialize SIM Configuration");
379 // Add SendMobileTrackerMsg() to GMainLoop
380 if (simStatus == MSG_SIM_STATUS_CHANGED) {
381 MSG_DEBUG("Send Mobile Tracker Message");
383 SendMobileTrackerMsg();
386 MSG_DEBUG("checkSimStatus() error");
391 // Register Callback to get the change of contact
392 MsgInitContactSvc(&MsgContactChangedCallback);
394 MsgSoundInitRepeatAlarm();
400 static gboolean InitThreadFunc(void* pData)
404 pthread_t initThreadId;
407 if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
409 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
413 pthread_detach(initThreadId);
423 #if !GLIB_CHECK_VERSION(2, 31, 0)
426 dbus_g_thread_init();
428 ////////////////////////////////////
430 /// set to ignore child process terminated signal.
431 signal( SIGCHLD, SIG_IGN );
433 ////////////////////////////////////
436 MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
438 // Reset message server ready flag
439 if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false) != MSG_SUCCESS)
440 MSG_DEBUG("MsgSettingSetBool FAIL: VCONFKEY_MSG_SERVER_READY");
443 // MsgStoConnectDB();
445 // Open Contact Service
452 MsgSettingRegVconfCB();
454 pthread_t startThreadId;
456 // start transaction manager
457 if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
459 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
463 MsgTransactionManager::instance()->getTMStatus();
465 mainloop = g_main_loop_new(NULL, FALSE);
467 #if !GLIB_CHECK_VERSION(2,35,0)
471 g_idle_add(InitThreadFunc, NULL);
473 if (mainloop != NULL)
475 MSG_DEBUG("Start Messaging Framework!!!");
478 g_main_loop_run(mainloop);
482 MSG_DEBUG("Fail to start Messaging Framework!!!");
486 MsgSettingRemoveVconfCB();
488 // Close Contact Sevice
489 MsgCloseContactSvc();
492 MsgStoDisconnectDB();