2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
17 /*==================================================================================================
19 ==================================================================================================*/
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"
39 #include <dbus/dbus-glib.h>
43 static GMainLoop* mainloop = NULL;
46 /*==================================================================================================
48 ==================================================================================================*/
49 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
52 /*==================================================================================================
53 FUNCTION IMPLEMENTATION
54 ==================================================================================================*/
55 msg_error_t InitMmsDir()
57 if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
61 MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
65 MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
66 return MSG_ERR_DB_MAKE_DIR;
70 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
74 MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
78 MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
79 return MSG_ERR_DB_MAKE_DIR;
83 if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
87 MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
91 MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
92 return MSG_ERR_DB_MAKE_DIR;
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);
100 MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
101 return MSG_ERR_DB_MAKE_DIR;
105 if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
109 MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
111 // if IPC data path is already exist, clear all files in folder.
113 snprintf(exeStr, 1024, "rm %s*.DATA", MSG_IPC_DATA_PATH);
118 MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
119 return MSG_ERR_DB_MAKE_DIR;
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);
126 chown( MSG_IPC_DATA_PATH, 0, 6502 );
132 void SendMobileTrackerMsg()
136 bool bEnabled = false;
138 if (MsgSettingGetBool(VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL, &bEnabled) < 0)
140 MSG_DEBUG("Can't read VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL");
144 if (bEnabled == false)
146 MSG_DEBUG("Mobile Tracker Option [%d]", bEnabled);
149 // to wait modem init
153 MSG_DEBUG("Waiting for modem ready, 22 sec.");
157 MSG_REQUEST_INFO_S req = {0};
159 req.sendOptInfo.bSetting = false;
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;
179 char* msgText = NULL;
181 msgText = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_MESSAGE_STR);
185 MSG_DEBUG("Assign Default Msg");
186 msgText = strdup(MSG_MOBILE_TRACKER_MSG);
187 if (msgText == NULL) {
188 MSG_DEBUG("msgText is NULL.");
193 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
195 req.msgInfo.dataSize = strlen(msgText);
196 strncpy(req.msgInfo.msgText, msgText, req.msgInfo.dataSize);
199 char *orgRecipientList = NULL;
201 orgRecipientList = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_RECIPIENTS_STR);
203 if (orgRecipientList == NULL)
205 MSG_DEBUG("recipient list is NULL");
211 int len = strlen(orgRecipientList);
212 char recipientList[len + 1];
214 memset(recipientList, 0, len + 1);
215 memcpy(recipientList, orgRecipientList, len);
217 MSG_DEBUG("recipient list : [%s]", recipientList);
219 req.msgInfo.nAddressCnt = 1;
223 token = strtok(recipientList, "|");
225 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
226 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
228 msg_error_t err = MSG_SUCCESS;
232 req.msgInfo.addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
233 req.msgInfo.addressList[0].recipientType = MSG_RECIPIENTS_TYPE_TO;
235 memset(req.msgInfo.addressList[0].addressVal, 0x00, MAX_ADDRESS_VAL_LEN);
236 strncpy(req.msgInfo.addressList[0].addressVal, token, MAX_ADDRESS_VAL_LEN);
238 MSG_DEBUG("address : [%s]", req.msgInfo.addressList[0].addressVal);
242 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
244 err = plg->submitReq(&req);
246 if (err != MSG_SUCCESS)
248 MSG_DEBUG("fail to send mobile tracker msg : [%d]", err);
253 token = strtok(NULL, "|");
261 if (orgRecipientList) {
262 free(orgRecipientList);
263 orgRecipientList = NULL;
273 void* StartMsgServer(void*)
280 MsgTransactionManager::instance()->run();
282 catch (MsgException& e)
284 MSG_FATAL("%s", e.what());
288 MSG_FATAL("%s", e.what());
291 if (g_main_loop_is_running(mainloop))
292 g_main_loop_quit(mainloop);
294 MsgCloseContactSvc();
300 void* InitMsgServer(void*)
302 msg_error_t err = MSG_SUCCESS;
308 // plugin manager initialize
309 MsgPluginManager::instance()->initialize();
311 catch (MsgException& e)
313 MSG_FATAL("%s", e.what());
317 MSG_FATAL("%s", e.what());
320 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
321 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
323 // storage handler initialize
324 err = MsgStoInitDB(false);
326 if (err != MSG_SUCCESS) {
327 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
330 // Set Msg FW Ready Flag
331 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
332 MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
335 MSG_DEBUG("No plugin for %d type", mainType);
341 // Clear and reset notification
342 MsgCleanAndResetNoti();
344 MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
347 if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
349 // Add the change of SIM to vconf
350 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
351 MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
354 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
355 // Check Device Status
356 if (plg->checkDeviceStatus() != MSG_SUCCESS) {
357 MSG_DEBUG("checkDeviceStatus() error");
366 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
367 MSG_DEBUG("Fail to Initialize SIM Message");
370 // Init SIM Configuration
371 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
372 MSG_DEBUG("Fail to Initialize SIM Configuration");
375 // Add SendMobileTrackerMsg() to GMainLoop
376 if (simStatus == MSG_SIM_STATUS_CHANGED) {
377 MSG_DEBUG("Send Mobile Tracker Message");
379 SendMobileTrackerMsg();
382 MSG_DEBUG("checkSimStatus() error");
387 // Register Callback to get the change of contact
388 MsgInitContactSvc(&MsgContactChangedCallback);
390 MsgSoundInitRepeatAlarm();
396 static gboolean InitThreadFunc(void* pData)
400 pthread_t initThreadId;
403 if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
405 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
409 pthread_detach(initThreadId);
419 #if !GLIB_CHECK_VERSION(2, 31, 0)
422 dbus_g_thread_init();
424 ////////////////////////////////////
426 /// set to ignore child process terminated signal.
427 signal( SIGCHLD, SIG_IGN );
429 ////////////////////////////////////
432 MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
434 // Reset message server ready flag
435 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
438 // MsgStoConnectDB();
440 // Open Contact Service
447 MsgSettingRegVconfCB();
449 pthread_t startThreadId;
451 // start transaction manager
452 if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
454 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
458 MsgTransactionManager::instance()->getTMStatus();
460 mainloop = g_main_loop_new(NULL, FALSE);
464 g_idle_add(InitThreadFunc, NULL);
466 if (mainloop != NULL)
468 MSG_DEBUG("Start Messaging Framework!!!");
471 g_main_loop_run(mainloop);
475 MSG_DEBUG("Fail to start Messaging Framework!!!");
479 MsgSettingRemoveVconfCB();
481 // Close Contact Sevice
482 MsgCloseContactSvc();
485 MsgStoDisconnectDB();