apply FSL(Flora Software License)
[platform/core/messaging/msg-service.git] / framework / main.cpp
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
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
7   *
8   *    http://www.tizenopensource.org/license
9   *
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.
15   */
16
17 /*==================================================================================================
18                                          INCLUDE FILES
19 ==================================================================================================*/
20 #include "MsgDebug.h"
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
33 #include <errno.h>
34 #include <glib.h>
35 #include <dbus/dbus-glib.h>
36 #include <sys/stat.h>
37 #include <wait.h>
38
39 static GMainLoop* mainloop = NULL;
40
41
42 /*==================================================================================================
43                                      DEFINES
44 ==================================================================================================*/
45 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
46
47
48 /*==================================================================================================
49                                      FUNCTION IMPLEMENTATION
50 ==================================================================================================*/
51 MSG_ERROR_T InitMmsDir()
52 {
53         if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
54         {
55                 if (errno == EEXIST)
56                 {
57                         MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
58                 }
59                 else
60                 {
61                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
62                         return MSG_ERR_DB_MAKE_DIR;
63                 }
64         }
65
66         if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
67         {
68                 if (errno == EEXIST)
69                 {
70                         MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
71                 }
72                 else
73                 {
74                         MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
75                         return MSG_ERR_DB_MAKE_DIR;
76                 }
77         }
78
79         if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
80         {
81                 if (errno == EEXIST)
82                 {
83                         MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
84                 }
85                 else
86                 {
87                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
88                         return MSG_ERR_DB_MAKE_DIR;
89                 }
90         }
91
92         if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
93                 if (errno == EEXIST) {
94                         MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
95                 } else {
96                         MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
97                         return MSG_ERR_DB_MAKE_DIR;
98                 }
99         }
100
101         if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
102         {
103                 if (errno == EEXIST)
104                 {
105                         MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
106
107                         // if IPC data path is already exist, clear all files in folder.
108                         char exeStr[1024];
109                         snprintf(exeStr, 1024, "rm %s*.DATA", MSG_IPC_DATA_PATH);
110                         system(exeStr);
111                 }
112                 else
113                 {
114                         MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
115                         return MSG_ERR_DB_MAKE_DIR;
116                 }
117         }
118
119         chmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG ); //public shared file: pass data by file
120         chown( MSG_IPC_DATA_PATH, 0, 6502 );
121
122         return MSG_SUCCESS;
123 }
124
125
126 void* StartMsgServer(void*)
127 {
128         try
129         {
130                 MsgTransactionManager::instance()->run();
131         }
132         catch (MsgException& e)
133         {
134                 MSG_FATAL("%s", e.what());
135         }
136         catch (exception& e)
137         {
138                 MSG_FATAL("%s", e.what());
139         }
140
141         if (g_main_loop_is_running(mainloop))
142                 g_main_loop_quit(mainloop);
143
144         return (void*)0;
145 }
146
147
148 void* InitMsgServer(void*)
149 {
150         MSG_ERROR_T err = MSG_SUCCESS;
151
152         MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
153         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
154
155         // storage handler initialize
156         err = MsgStoInitDB(false);
157
158         if (err != MSG_SUCCESS) {
159                 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
160         }
161
162         if (plg == NULL) {
163                 MSG_DEBUG("No plugin for %d type", mainType);
164
165                 MsgReleaseMemory();
166
167                 // Set Msg FW Ready Flag
168                 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
169
170                 return (void*)0;
171         }
172
173         MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
174
175         // Check Sim Status
176         if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
177
178                 // Add the change of SIM to vconf
179                 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
180                         MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
181                 }
182
183                 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
184                         // Check Device Status
185                         if (plg->checkDeviceStatus() != MSG_SUCCESS) {
186                                 MSG_DEBUG("checkDeviceStatus() error");
187
188                                 MsgReleaseMemory();
189
190                                 // Set Msg FW Ready Flag
191                                 MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
192
193                                 return (void*)0;
194                         }
195                 }
196
197                 // Init SIM Message
198                 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
199                         MSG_DEBUG("Fail to Initialize SIM Message");
200                 }
201
202                 // Init SIM Configuration
203                 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
204                         MSG_DEBUG("Fail to Initialize SIM Configuration");
205                 }
206         } else {
207                 MSG_DEBUG("checkSimStatus() error");
208         }
209
210         MsgReleaseMemory();
211
212         // Try to connect contact server  if it is not opened.
213         MsgOpenContactSvc();
214
215         // Register Callback to get the change of contact
216         MsgInitContactSvc(&MsgContactChangedCallback);
217
218         // Set Msg FW Ready Flag
219         MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
220
221         return (void*)0;
222 }
223
224
225 static gboolean InitThreadFunc(void* pData)
226 {
227         MSG_BEGIN();
228
229         pthread_t initThreadId;
230
231         // initialize msg fw
232         if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
233         {
234                 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
235                 return -1;
236         }
237
238         pthread_detach(initThreadId);
239
240         MSG_END();
241
242         return FALSE;
243 }
244
245
246 int main(void)
247 {
248         g_thread_init(NULL);
249         dbus_g_thread_init();
250
251 ////////////////////////////////////
252
253 /// set to ignore child process terminated signal.
254 signal( SIGCHLD, SIG_IGN );
255
256 ////////////////////////////////////
257
258
259         MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
260
261         // Reset message server ready flag
262         MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
263
264         // Connect to DB
265         //      MsgStoConnectDB();
266
267         // Open Contact Service
268         MsgOpenContactSvc();
269
270         // Clean up mms dir
271         InitMmsDir();
272
273         try
274         {
275                 // plugin manager initialize
276                 MsgPluginManager::instance()->initialize();
277         }
278         catch (MsgException& e)
279         {
280                 MSG_FATAL("%s", e.what());
281         }
282         catch (exception& e)
283         {
284                 MSG_FATAL("%s", e.what());
285         }
286
287         pthread_t startThreadId;
288
289         // start transaction manager
290         if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
291         {
292                 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
293                 return -1;
294         }
295
296         MsgTransactionManager::instance()->getTMStatus();
297
298         mainloop = g_main_loop_new(NULL, FALSE);
299
300         g_type_init();
301
302         g_idle_add(InitThreadFunc, NULL);
303
304         if (mainloop != NULL)
305         {
306                 MSG_DEBUG("Start Messaging Framework!!!");
307
308                 // Run GMainLoop
309                 g_main_loop_run(mainloop);
310         }
311         else
312         {
313                 MSG_DEBUG("Fail to start Messaging Framework!!!");
314         }
315
316         // Close Contact Sevice
317         MsgCloseContactSvc();
318
319         // Disconnect to DB
320         MsgStoDisconnectDB();
321
322         return 0;
323 }
324