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 "MsgSpamFilter.h"
26 #include "MsgPluginManager.h"
27 #include "MsgSettingHandler.h"
28 #include "MsgStorageHandler.h"
29 #include "MsgSubmitHandler.h"
30 #include "MsgDeliverHandler.h"
31 #include "MsgTransManager.h"
32 #include "MsgStorageTypes.h"
36 #include <dbus/dbus-glib.h>
40 static GMainLoop* mainloop = NULL;
43 /*==================================================================================================
45 ==================================================================================================*/
46 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
49 /*==================================================================================================
50 FUNCTION IMPLEMENTATION
51 ==================================================================================================*/
52 msg_error_t InitMmsDir()
54 if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
58 MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
62 MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
63 return MSG_ERR_DB_MAKE_DIR;
67 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
71 MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
75 MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
76 return MSG_ERR_DB_MAKE_DIR;
80 if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
84 MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
88 MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
89 return MSG_ERR_DB_MAKE_DIR;
93 if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
94 if (errno == EEXIST) {
95 MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
97 MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
98 return MSG_ERR_DB_MAKE_DIR;
102 if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
106 MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
108 // if IPC data path is already exist, clear all files in folder.
110 snprintf(exeStr, 1024, "rm %s*.DATA", MSG_IPC_DATA_PATH);
115 MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
116 return MSG_ERR_DB_MAKE_DIR;
120 chmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG ); //public shared file: pass data by file
121 chown( MSG_IPC_DATA_PATH, 0, 6502 );
127 void SendMobileTrackerMsg()
131 bool bEnabled = false;
133 if (MsgSettingGetBool(VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL, &bEnabled) < 0)
135 MSG_DEBUG("Can't read VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL");
139 if (bEnabled == false)
141 MSG_DEBUG("Mobile Tracker Option [%d]", bEnabled);
144 // to wait modem init
148 MSG_DEBUG("Waiting for modem ready, 22 sec.");
152 MSG_REQUEST_INFO_S req = {0};
154 req.sendOptInfo.bSetting = false;
156 req.msgInfo.msgId = 0;
157 req.msgInfo.threadId = 0;
158 req.msgInfo.folderId = MSG_DRAFT_ID;
159 req.msgInfo.msgType.mainType = MSG_SMS_TYPE;
160 req.msgInfo.msgType.subType = MSG_NORMAL_SMS;
161 req.msgInfo.msgType.classType = MSG_CLASS_NONE;
162 req.msgInfo.storageId = MSG_STORAGE_PHONE;
163 req.msgInfo.displayTime = 0;
164 req.msgInfo.networkStatus = MSG_NETWORK_NOT_SEND;
165 req.msgInfo.encodeType = MSG_ENCODE_AUTO;
166 req.msgInfo.bRead = false;
167 req.msgInfo.bProtected = false;
168 req.msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL;
169 req.msgInfo.direction = MSG_DIRECTION_TYPE_MO;
170 req.msgInfo.msgPort.valid = false;
171 req.msgInfo.bTextSms = true;
174 char* msgText = NULL;
176 msgText = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_MESSAGE_STR);
180 MSG_DEBUG("Assign Default Msg");
181 msgText = strdup(MSG_MOBILE_TRACKER_MSG);
184 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
186 req.msgInfo.dataSize = strlen(msgText);
187 strncpy(req.msgInfo.msgText, msgText, req.msgInfo.dataSize);
190 char *orgRecipientList = NULL;
192 orgRecipientList = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_RECIPIENTS_STR);
194 if (orgRecipientList == NULL)
196 MSG_DEBUG("recipient list is NULL");
200 int len = strlen(orgRecipientList);
201 char recipientList[len + 1];
203 memset(recipientList, 0, len + 1);
204 memcpy(recipientList, orgRecipientList, len);
206 MSG_DEBUG("recipient list : [%s]", recipientList);
208 req.msgInfo.nAddressCnt = 1;
212 token = strtok(recipientList, "|");
214 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
215 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
217 msg_error_t err = MSG_SUCCESS;
221 req.msgInfo.addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
222 req.msgInfo.addressList[0].recipientType = MSG_RECIPIENTS_TYPE_TO;
224 memset(req.msgInfo.addressList[0].addressVal, 0x00, MAX_ADDRESS_VAL_LEN);
225 strncpy(req.msgInfo.addressList[0].addressVal, token, MAX_ADDRESS_VAL_LEN);
227 MSG_DEBUG("address : [%s]", req.msgInfo.addressList[0].addressVal);
231 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
233 err = plg->submitReq(&req);
235 if (err != MSG_SUCCESS)
237 MSG_DEBUG("fail to send mobile tracker msg : [%d]", err);
242 token = strtok(NULL, "|");
250 if (orgRecipientList) {
251 free(orgRecipientList);
252 orgRecipientList = NULL;
262 void* StartMsgServer(void*)
266 MsgTransactionManager::instance()->run();
268 catch (MsgException& e)
270 MSG_FATAL("%s", e.what());
274 MSG_FATAL("%s", e.what());
277 if (g_main_loop_is_running(mainloop))
278 g_main_loop_quit(mainloop);
284 void* InitMsgServer(void*)
286 msg_error_t err = MSG_SUCCESS;
288 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
289 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
291 // storage handler initialize
292 err = MsgStoInitDB(false);
294 if (err != MSG_SUCCESS) {
295 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
298 // Set Msg FW Ready Flag
299 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
300 MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
303 MSG_DEBUG("No plugin for %d type", mainType);
309 MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
312 if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
314 // Add the change of SIM to vconf
315 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
316 MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
319 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
320 // Check Device Status
321 if (plg->checkDeviceStatus() != MSG_SUCCESS) {
322 MSG_DEBUG("checkDeviceStatus() error");
331 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
332 MSG_DEBUG("Fail to Initialize SIM Message");
335 // Init SIM Configuration
336 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
337 MSG_DEBUG("Fail to Initialize SIM Configuration");
340 // Add SendMobileTrackerMsg() to GMainLoop
341 if (simStatus == MSG_SIM_STATUS_CHANGED) {
342 MSG_DEBUG("Send Mobile Tracker Message");
344 SendMobileTrackerMsg();
347 MSG_DEBUG("checkSimStatus() error");
352 // Try to connect contact server if it is not opened.
355 // Register Callback to get the change of contact
356 MsgInitContactSvc(&MsgContactChangedCallback);
362 static gboolean InitThreadFunc(void* pData)
366 pthread_t initThreadId;
369 if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
371 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
375 pthread_detach(initThreadId);
385 #if !GLIB_CHECK_VERSION(2, 31, 0)
388 dbus_g_thread_init();
390 ////////////////////////////////////
392 /// set to ignore child process terminated signal.
393 signal( SIGCHLD, SIG_IGN );
395 ////////////////////////////////////
398 MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
400 // Reset message server ready flag
401 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
404 // MsgStoConnectDB();
406 // Open Contact Service
413 MsgSettingRegVconfCB();
417 // plugin manager initialize
418 MsgPluginManager::instance()->initialize();
420 catch (MsgException& e)
422 MSG_FATAL("%s", e.what());
426 MSG_FATAL("%s", e.what());
429 pthread_t startThreadId;
431 // start transaction manager
432 if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
434 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
438 MsgTransactionManager::instance()->getTMStatus();
440 mainloop = g_main_loop_new(NULL, FALSE);
444 g_idle_add(InitThreadFunc, NULL);
446 if (mainloop != NULL)
448 MSG_DEBUG("Start Messaging Framework!!!");
451 g_main_loop_run(mainloop);
455 MSG_DEBUG("Fail to start Messaging Framework!!!");
459 MsgSettingRemoveVconfCB();
461 // Close Contact Sevice
462 MsgCloseContactSvc();
465 MsgStoDisconnectDB();