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"
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;
290 // plugin manager initialize
291 MsgPluginManager::instance()->initialize();
293 catch (MsgException& e)
295 MSG_FATAL("%s", e.what());
299 MSG_FATAL("%s", e.what());
302 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
303 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
305 // storage handler initialize
306 err = MsgStoInitDB(false);
308 if (err != MSG_SUCCESS) {
309 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
312 // Set Msg FW Ready Flag
313 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
314 MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
317 MSG_DEBUG("No plugin for %d type", mainType);
323 MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
326 if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
328 // Add the change of SIM to vconf
329 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
330 MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
333 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
334 // Check Device Status
335 if (plg->checkDeviceStatus() != MSG_SUCCESS) {
336 MSG_DEBUG("checkDeviceStatus() error");
345 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
346 MSG_DEBUG("Fail to Initialize SIM Message");
349 // Init SIM Configuration
350 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
351 MSG_DEBUG("Fail to Initialize SIM Configuration");
354 // Add SendMobileTrackerMsg() to GMainLoop
355 if (simStatus == MSG_SIM_STATUS_CHANGED) {
356 MSG_DEBUG("Send Mobile Tracker Message");
358 SendMobileTrackerMsg();
361 MSG_DEBUG("checkSimStatus() error");
366 // Try to connect contact server if it is not opened.
369 // Register Callback to get the change of contact
370 MsgInitContactSvc(&MsgContactChangedCallback);
372 MsgSoundInitRepeatAlarm();
378 static gboolean InitThreadFunc(void* pData)
382 pthread_t initThreadId;
385 if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
387 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
391 pthread_detach(initThreadId);
401 #if !GLIB_CHECK_VERSION(2, 31, 0)
404 dbus_g_thread_init();
406 ////////////////////////////////////
408 /// set to ignore child process terminated signal.
409 signal( SIGCHLD, SIG_IGN );
411 ////////////////////////////////////
414 MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
416 // Reset message server ready flag
417 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
420 // MsgStoConnectDB();
422 // Open Contact Service
429 MsgSettingRegVconfCB();
431 pthread_t startThreadId;
433 // start transaction manager
434 if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
436 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
440 MsgTransactionManager::instance()->getTMStatus();
442 mainloop = g_main_loop_new(NULL, FALSE);
446 g_idle_add(InitThreadFunc, NULL);
448 if (mainloop != NULL)
450 MSG_DEBUG("Start Messaging Framework!!!");
453 g_main_loop_run(mainloop);
457 MSG_DEBUG("Fail to start Messaging Framework!!!");
461 MsgSettingRemoveVconfCB();
463 // Close Contact Sevice
464 MsgCloseContactSvc();
467 MsgStoDisconnectDB();