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"
33 #include "MsgSoundPlayer.h"
37 #include <dbus/dbus-glib.h>
41 static GMainLoop* mainloop = NULL;
44 /*==================================================================================================
46 ==================================================================================================*/
47 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
50 /*==================================================================================================
51 FUNCTION IMPLEMENTATION
52 ==================================================================================================*/
53 msg_error_t InitMmsDir()
55 if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
59 MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
63 MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
64 return MSG_ERR_DB_MAKE_DIR;
68 if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
72 MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
76 MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
77 return MSG_ERR_DB_MAKE_DIR;
81 if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
85 MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
89 MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
90 return MSG_ERR_DB_MAKE_DIR;
94 if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
95 if (errno == EEXIST) {
96 MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
98 MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
99 return MSG_ERR_DB_MAKE_DIR;
103 if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
107 MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
109 // if IPC data path is already exist, clear all files in folder.
111 snprintf(exeStr, 1024, "rm %s*.DATA", MSG_IPC_DATA_PATH);
116 MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
117 return MSG_ERR_DB_MAKE_DIR;
121 chmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG ); //public shared file: pass data by file
122 chown( MSG_IPC_DATA_PATH, 0, 6502 );
128 void SendMobileTrackerMsg()
132 bool bEnabled = false;
134 if (MsgSettingGetBool(VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL, &bEnabled) < 0)
136 MSG_DEBUG("Can't read VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL");
140 if (bEnabled == false)
142 MSG_DEBUG("Mobile Tracker Option [%d]", bEnabled);
145 // to wait modem init
149 MSG_DEBUG("Waiting for modem ready, 22 sec.");
153 MSG_REQUEST_INFO_S req = {0};
155 req.sendOptInfo.bSetting = false;
157 req.msgInfo.msgId = 0;
158 req.msgInfo.threadId = 0;
159 req.msgInfo.folderId = MSG_DRAFT_ID;
160 req.msgInfo.msgType.mainType = MSG_SMS_TYPE;
161 req.msgInfo.msgType.subType = MSG_NORMAL_SMS;
162 req.msgInfo.msgType.classType = MSG_CLASS_NONE;
163 req.msgInfo.storageId = MSG_STORAGE_PHONE;
164 req.msgInfo.displayTime = 0;
165 req.msgInfo.networkStatus = MSG_NETWORK_NOT_SEND;
166 req.msgInfo.encodeType = MSG_ENCODE_AUTO;
167 req.msgInfo.bRead = false;
168 req.msgInfo.bProtected = false;
169 req.msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL;
170 req.msgInfo.direction = MSG_DIRECTION_TYPE_MO;
171 req.msgInfo.msgPort.valid = false;
172 req.msgInfo.bTextSms = true;
175 char* msgText = NULL;
177 msgText = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_MESSAGE_STR);
181 MSG_DEBUG("Assign Default Msg");
182 msgText = strdup(MSG_MOBILE_TRACKER_MSG);
185 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
187 req.msgInfo.dataSize = strlen(msgText);
188 strncpy(req.msgInfo.msgText, msgText, req.msgInfo.dataSize);
191 char *orgRecipientList = NULL;
193 orgRecipientList = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_RECIPIENTS_STR);
195 if (orgRecipientList == NULL)
197 MSG_DEBUG("recipient list is NULL");
201 int len = strlen(orgRecipientList);
202 char recipientList[len + 1];
204 memset(recipientList, 0, len + 1);
205 memcpy(recipientList, orgRecipientList, len);
207 MSG_DEBUG("recipient list : [%s]", recipientList);
209 req.msgInfo.nAddressCnt = 1;
213 token = strtok(recipientList, "|");
215 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
216 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
218 msg_error_t err = MSG_SUCCESS;
222 req.msgInfo.addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
223 req.msgInfo.addressList[0].recipientType = MSG_RECIPIENTS_TYPE_TO;
225 memset(req.msgInfo.addressList[0].addressVal, 0x00, MAX_ADDRESS_VAL_LEN);
226 strncpy(req.msgInfo.addressList[0].addressVal, token, MAX_ADDRESS_VAL_LEN);
228 MSG_DEBUG("address : [%s]", req.msgInfo.addressList[0].addressVal);
232 MSG_DEBUG("mobile tracker msg : [%s]", msgText);
234 err = plg->submitReq(&req);
236 if (err != MSG_SUCCESS)
238 MSG_DEBUG("fail to send mobile tracker msg : [%d]", err);
243 token = strtok(NULL, "|");
251 if (orgRecipientList) {
252 free(orgRecipientList);
253 orgRecipientList = NULL;
263 void* StartMsgServer(void*)
267 MsgTransactionManager::instance()->run();
269 catch (MsgException& e)
271 MSG_FATAL("%s", e.what());
275 MSG_FATAL("%s", e.what());
278 if (g_main_loop_is_running(mainloop))
279 g_main_loop_quit(mainloop);
285 void* InitMsgServer(void*)
287 msg_error_t err = MSG_SUCCESS;
289 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
290 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
292 // storage handler initialize
293 err = MsgStoInitDB(false);
295 if (err != MSG_SUCCESS) {
296 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
299 // Set Msg FW Ready Flag
300 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
301 MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
304 MSG_DEBUG("No plugin for %d type", mainType);
310 MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
313 if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
315 // Add the change of SIM to vconf
316 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
317 MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
320 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
321 // Check Device Status
322 if (plg->checkDeviceStatus() != MSG_SUCCESS) {
323 MSG_DEBUG("checkDeviceStatus() error");
332 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
333 MSG_DEBUG("Fail to Initialize SIM Message");
336 // Init SIM Configuration
337 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
338 MSG_DEBUG("Fail to Initialize SIM Configuration");
341 // Add SendMobileTrackerMsg() to GMainLoop
342 if (simStatus == MSG_SIM_STATUS_CHANGED) {
343 MSG_DEBUG("Send Mobile Tracker Message");
345 SendMobileTrackerMsg();
348 MSG_DEBUG("checkSimStatus() error");
353 // Try to connect contact server if it is not opened.
356 // Register Callback to get the change of contact
357 MsgInitContactSvc(&MsgContactChangedCallback);
364 static gboolean InitThreadFunc(void* pData)
368 pthread_t initThreadId;
371 if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
373 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
377 pthread_detach(initThreadId);
387 #if !GLIB_CHECK_VERSION(2, 31, 0)
390 dbus_g_thread_init();
392 ////////////////////////////////////
394 /// set to ignore child process terminated signal.
395 signal( SIGCHLD, SIG_IGN );
397 ////////////////////////////////////
400 MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
402 // Reset message server ready flag
403 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
406 // MsgStoConnectDB();
408 // Open Contact Service
415 MsgSettingRegVconfCB();
419 // plugin manager initialize
420 MsgPluginManager::instance()->initialize();
422 catch (MsgException& e)
424 MSG_FATAL("%s", e.what());
428 MSG_FATAL("%s", e.what());
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();