[P161228-01293] change on-boot attribute to true from false
[platform/core/messaging/msg-service.git] / manager / src / msg-manager.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <glib.h>
24
25 extern "C"
26 {
27 #include <tizen.h>
28 #include <service_app.h>
29 #include <app_event.h>
30 #include <notification_list.h>
31 #include <notification_text_domain.h>
32 #include <notification_internal.h>
33 #include <vconf.h>
34 #include <call-manager.h>
35 }
36
37 #include "msg.h"
38 #include "msg_storage.h"
39 #include "msg_transport.h"
40
41 #include "msg-manager-contact.h"
42 #include "msg-manager-debug.h"
43 #include "msg-manager-notification.h"
44 #include "msg-manager-sound.h"
45 #include "msg-manager-util.h"
46
47 /*==================================================================================================
48                                      VARIABLES
49 ==================================================================================================*/
50 msg_handle_t msg_handle = NULL;
51 cm_client_h cm_handle = NULL;
52
53 pthread_mutex_t mx;
54 static int job_cnt = 0;
55 static bool terminated = false;
56
57 /*==================================================================================================
58                             FUNCTION IMPLEMENTATION
59 ==================================================================================================*/
60 static int _service_app_exit(void *data)
61 {
62         MSG_MGR_INFO("kill msg-manager");
63         service_app_exit();
64
65         return 0;
66 }
67
68 static int _check_app_terminate(void *data)
69 {
70         pthread_mutex_lock(&mx);
71
72         job_cnt--;
73         MSG_MGR_DEBUG("job_cnt [%d]", job_cnt);
74         if (job_cnt == 0) {
75                 terminated = true;
76                 g_idle_add(_service_app_exit, NULL);
77         }
78
79         pthread_mutex_unlock(&mx);
80
81         return 0;
82 }
83
84 bool service_app_create(void *data)
85 {
86         MSG_MGR_INFO("app_create");
87
88         int msg_server_ready = 0;
89         for (int i = 0; i < 100; i++) {
90                 vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &msg_server_ready);
91                 if (msg_server_ready == 1) {
92                         int msg_err = msg_open_msg_handle(&msg_handle);
93                         if (msg_err != MSG_SUCCESS)
94                                 MSG_MGR_ERR("msg_open_msg_handle() failed [%d]", msg_err);
95                         else
96                                 MSG_MGR_DEBUG("msg_open_msg_handle() success");
97
98                         break;
99                 } else {
100                         MSG_MGR_DEBUG("msg-server is not ready.");
101                         sleep(1);
102                 }
103         }
104
105         if (msg_handle == NULL)
106                 return false;
107
108         MsgMgrInitNoti();
109         initMsgMgrSoundPlayer();
110         cm_init(&cm_handle);
111
112         return true;
113 }
114
115 void service_app_terminate(void *data)
116 {
117         MSG_MGR_INFO("app_terminate");
118
119         cm_deinit(cm_handle);
120         MsgMgrCloseContactSvc();
121         msg_close_msg_handle(&msg_handle);
122
123         return;
124 }
125
126 void _incoming_msg_func(app_control_h app_control)
127 {
128         MSG_MGR_BEGIN();
129
130         int ret = 0;
131         char *rcv_msg_type = NULL;
132         char *rcv_msg_id = NULL;
133
134         ret = app_control_get_extra_data(app_control, EVENT_KEY_MSG_ID, &rcv_msg_id);
135         if (ret != APP_CONTROL_ERROR_NONE || rcv_msg_id == NULL) {
136                 MSG_MGR_ERR("app_control_get_extra_data failed");
137                 return;
138         }
139
140         ret = app_control_get_extra_data(app_control, EVENT_KEY_MSG_TYPE, &rcv_msg_type);
141         if (ret != APP_CONTROL_ERROR_NONE || rcv_msg_type == NULL) {
142                 MSG_MGR_ERR("app_control_get_extra_data failed");
143                 g_free(rcv_msg_id);
144                 return;
145         }
146
147         MSG_MGR_INFO("rcv_msg_type(%s), rcv_msg_id(%s)", rcv_msg_type, rcv_msg_id);
148
149         int msg_err = MSG_SUCCESS;
150         msg_message_id_t msg_id = atoi(rcv_msg_id);
151         msg_struct_t msg = NULL;
152         msg_struct_t opt = NULL;
153         contactInfo contact_info = {0, };
154         contact_info.msgId = msg_id;
155
156         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
157         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
158         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
159         if (msg_err != MSG_SUCCESS) {
160                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
161                 msg_release_struct(&msg);
162                 msg_release_struct(&opt);
163                 return;
164         }
165
166         msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &contact_info.msgType);
167         msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &contact_info.folderId);
168         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &contact_info.simIndex);
169         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&contact_info.addrList);
170         msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, contact_info.subject, MAX_CONTACT_TEXT_LEN);
171         int msgSize = 0;
172         msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
173         if (msgSize > 0)
174                 msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, contact_info.msgText, MAX_CONTACT_TEXT_LEN);
175
176         if ((contact_info.folderId == MSG_INBOX_ID || contact_info.folderId == MSG_SPAMBOX_ID)) {
177                 MsgMgrAddPhoneLog(&contact_info);
178         }
179
180         msg_release_struct(&msg);
181         msg_release_struct(&opt);
182
183         g_free(rcv_msg_id);
184         g_free(rcv_msg_type);
185
186         MSG_MGR_END();
187 }
188
189 void _outgoing_msg_func(app_control_h app_control)
190 {
191         MSG_MGR_BEGIN();
192
193         int ret = 0;
194         char *sent_msg_type = NULL;
195         char *sent_msg_id = NULL;
196
197         ret = app_control_get_extra_data(app_control, EVENT_KEY_OUT_MSG_ID, &sent_msg_id);
198         if (ret != APP_CONTROL_ERROR_NONE || sent_msg_id == NULL) {
199                 MSG_MGR_ERR("app_control_get_extra_data failed");
200                 return;
201         }
202
203         ret = app_control_get_extra_data(app_control, EVENT_KEY_OUT_MSG_TYPE, &sent_msg_type);
204         if (ret != APP_CONTROL_ERROR_NONE || sent_msg_type == NULL) {
205                 MSG_MGR_ERR("app_control_get_extra_data failed");
206                 g_free(sent_msg_id);
207                 return;
208         }
209
210         MSG_MGR_INFO("sent_msg_type(%s) sent_msg_id(%s)", sent_msg_type, sent_msg_id);
211
212         int msg_err = MSG_SUCCESS;
213         msg_message_id_t msg_id = atoi(sent_msg_id);
214         msg_struct_t msg = NULL;
215         msg_struct_t opt = NULL;
216         contactInfo contact_info = {0, };
217         contact_info.msgId = msg_id;
218
219         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
220         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
221         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
222         if (msg_err != MSG_SUCCESS) {
223                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
224                 msg_release_struct(&msg);
225                 msg_release_struct(&opt);
226                 return;
227         }
228
229         msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &contact_info.msgType);
230         msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &contact_info.folderId);
231         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &contact_info.simIndex);
232         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&contact_info.addrList);
233         msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, contact_info.subject, 100);
234         int msgSize = 0;
235         msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
236         if (msgSize > 0)
237                 msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, contact_info.msgText, 100);
238
239         MsgMgrAddPhoneLog(&contact_info);
240
241         msg_release_struct(&msg);
242         msg_release_struct(&opt);
243
244         g_free(sent_msg_id);
245         g_free(sent_msg_type);
246
247         MSG_MGR_END();
248 }
249
250 void _refresh_noti_func(app_control_h app_control)
251 {
252         char *type = NULL;
253         char *feedback = NULL;
254         char *active_type = NULL;
255         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
256         bool bFeedback = true;
257         msg_mgr_active_notification_type_t active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_NONE;
258
259         int ret = app_control_get_extra_data(app_control, "type", &type);
260         if (ret == APP_CONTROL_ERROR_NONE && type) {
261                 MSG_MGR_DEBUG("type [%s]", type);
262                 if (g_strcmp0(type, "normal") == 0)
263                         noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
264                 else if (g_strcmp0(type, "failed") == 0)
265                         noti_type = MSG_MGR_NOTI_TYPE_FAILED;
266
267                 g_free(type);
268         } else {
269                 MSG_MGR_ERR("app_control_get_extra_data failed");
270                 return;
271         }
272
273         ret = app_control_get_extra_data(app_control, "feedback", &feedback);
274         if (ret == APP_CONTROL_ERROR_NONE && feedback) {
275                 MSG_MGR_DEBUG("feedback [%s]", feedback);
276                 if (g_strcmp0(feedback, "false") == 0)
277                         bFeedback = false;
278                 else if (g_strcmp0(feedback, "true") == 0)
279                         bFeedback = true;
280
281                 g_free(feedback);
282         }
283
284         ret = app_control_get_extra_data(app_control, "active_type", &active_type);
285         if (ret == APP_CONTROL_ERROR_NONE && active_type) {
286                 MSG_MGR_DEBUG("active_type [%s]", active_type);
287                 if (g_strcmp0(active_type, "none") == 0)
288                         active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_NONE;
289                 else if (g_strcmp0(active_type, "active") == 0)
290                         active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_ACTIVE;
291                 else if (g_strcmp0(active_type, "instant") == 0)
292                         active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_INSTANT;
293
294                 g_free(active_type);
295         }
296
297         MsgMgrRefreshNotification(noti_type, bFeedback, active_noti_type);
298 }
299
300 void _add_noti_func(app_control_h app_control)
301 {
302         char *type;
303         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
304
305         int ret = app_control_get_extra_data(app_control, "type", &type);
306         if (ret == APP_CONTROL_ERROR_NONE && type) {
307                 if (g_strcmp0(type, "voice1") == 0) {
308                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_1;
309                 } else if (g_strcmp0(type, "voice2") == 0) {
310                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_2;
311                 } else if (g_strcmp0(type, "mwi") == 0) {
312                         noti_type = MSG_MGR_NOTI_TYPE_MWI;
313                 } else if (g_strcmp0(type, "class0") == 0) {
314                         noti_type = MSG_MGR_NOTI_TYPE_CLASS0;
315                 } else if (g_strcmp0(type, "read_report_sent") == 0) {
316                         noti_type = MSG_MGR_NOTI_TYPE_MMS_READ_REPORT_SENT;
317                 }
318
319                 g_free(type);
320         } else {
321                 MSG_MGR_ERR("app_control_get_extra_data failed");
322                 return;
323         }
324
325         char *msgId = NULL;
326         ret = app_control_get_extra_data(app_control, "msg_id", &msgId);
327         if (ret != APP_CONTROL_ERROR_NONE || msgId == NULL) {
328                 MSG_MGR_ERR("app_control_get_extra_data failed");
329                 return;
330         }
331
332         int msg_err = MSG_SUCCESS;
333         msg_message_id_t msg_id = atoi(msgId);
334         msg_struct_t msg = NULL;
335         msg_struct_t opt = NULL;
336         msg_list_handle_t addr_list = NULL;
337         MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
338         msg_info.msgId = msg_id;
339
340         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
341         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
342         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
343         if (msg_err != MSG_SUCCESS) {
344                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
345                 msg_release_struct(&msg);
346                 msg_release_struct(&opt);
347                 return;
348         }
349
350         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &msg_info.sim_idx);
351         msg_get_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, (int *)&msg_info.displayTime);
352         msg_get_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, (int *)&msg_info.networkStatus);
353         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
354
355         msg_struct_t addr_data = msg_list_nth_data(addr_list, 0);
356         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, msg_info.addressVal, MAX_ADDRESS_VAL_LEN);
357         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_DISPLAYNAME_STR, msg_info.displayName, MAX_DISPLAY_NAME_LEN);
358
359         if (noti_type == MSG_MGR_NOTI_TYPE_MWI || noti_type == MSG_MGR_NOTI_TYPE_CLASS0) {
360                 int msg_size = 0;
361                 msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msg_size);
362                 if (msg_size > 0)
363                         msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, msg_info.msgText, MAX_MSG_TEXT_LEN);
364         }
365
366         msg_release_struct(&msg);
367         msg_release_struct(&opt);
368
369         g_free(msgId);
370
371         MsgMgrAddNotification(noti_type, &msg_info);
372 }
373
374 void _del_noti_func(app_control_h app_control)
375 {
376         char *type;
377         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
378
379         int ret = app_control_get_extra_data(app_control, "type", &type);
380         if (ret == APP_CONTROL_ERROR_NONE && type) {
381                 if (g_strcmp0(type, "all") == 0) {
382                         noti_type = MSG_MGR_NOTI_TYPE_ALL;
383                 } else if (g_strcmp0(type, "normal") == 0) {
384                         noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
385                 } else if (g_strcmp0(type, "sim") == 0) {
386                         noti_type = MSG_MGR_NOTI_TYPE_SIM;
387                 } else if (g_strcmp0(type, "voice1") == 0) {
388                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_1;
389                 } else if (g_strcmp0(type, "voice2") == 0) {
390                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_2;
391                 }
392
393                 g_free(type);
394         } else {
395                 MSG_MGR_ERR("app_control_get_extra_data failed");
396                 return;
397         }
398
399         char *simIndex = NULL;
400         ret = app_control_get_extra_data(app_control, "sim_index", &simIndex);
401         if (ret != APP_CONTROL_ERROR_NONE || simIndex == NULL) {
402                 MSG_MGR_ERR("app_control_get_extra_data failed");
403                 return;
404         }
405
406         int sim_index = atoi(simIndex);
407
408         MsgMgrDeleteNoti(noti_type, sim_index);
409
410         g_free(simIndex);
411 }
412
413 void _add_report_noti_func(app_control_h app_control)
414 {
415         char *type;
416         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
417
418         int ret = app_control_get_extra_data(app_control, "type", &type);
419         if (ret == APP_CONTROL_ERROR_NONE && type) {
420                 if (g_strcmp0(type, "sms_delivery") == 0) {
421                         noti_type = MSG_MGR_NOTI_TYPE_SMS_DELIVERY_REPORT;
422                 } else if (g_strcmp0(type, "mms_delivery") == 0) {
423                         noti_type = MSG_MGR_NOTI_TYPE_MMS_DELIVERY_REPORT;
424                 } else if (g_strcmp0(type, "mms_read") == 0) {
425                         noti_type = MSG_MGR_NOTI_TYPE_MMS_READ_REPORT;
426                 }
427
428                 g_free(type);
429         } else {
430                 MSG_MGR_ERR("app_control_get_extra_data failed");
431                 return;
432         }
433
434         char *msgId = NULL;
435         ret = app_control_get_extra_data(app_control, "msg_id", &msgId);
436         if (ret != APP_CONTROL_ERROR_NONE || msgId == NULL) {
437                 MSG_MGR_ERR("app_control_get_extra_data failed");
438                 return;
439         }
440
441         int msg_err = MSG_SUCCESS;
442         msg_message_id_t msg_id = atoi(msgId);
443         msg_struct_t msg = NULL;
444         msg_struct_t opt = NULL;
445         msg_list_handle_t addr_list = NULL;
446         MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
447         msg_info.msgId = msg_id;
448
449         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
450         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
451         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
452         if (msg_err != MSG_SUCCESS) {
453                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
454                 msg_release_struct(&msg);
455                 msg_release_struct(&opt);
456                 return;
457         }
458
459         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &msg_info.sim_idx);
460         msg_get_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, (int *)&msg_info.displayTime);
461         msg_get_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, (int *)&msg_info.networkStatus);
462         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
463
464         msg_struct_t addr_data = msg_list_nth_data(addr_list, 0);
465         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, msg_info.addressVal, MAX_ADDRESS_VAL_LEN);
466         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_DISPLAYNAME_STR, msg_info.displayName, MAX_DISPLAY_NAME_LEN);
467
468         msg_release_struct(&msg);
469         msg_release_struct(&opt);
470
471         g_free(msgId);
472
473         MsgMgrAddReportNotification(noti_type, &msg_info);
474 }
475
476 void _del_report_noti_func(app_control_h app_control)
477 {
478         char *addr = NULL;
479         int ret = app_control_get_extra_data(app_control, "address", &addr);
480         if (ret != APP_CONTROL_ERROR_NONE || addr == NULL) {
481                 MSG_MGR_ERR("app_control_get_extra_data failed");
482                 return;
483         }
484
485         MsgMgrDeleteReportNotification(addr);
486
487         g_free(addr);
488 }
489
490 void _del_sent_read_report_noti_func(app_control_h app_control)
491 {
492         MsgMgrDeleteSentReadReportNotification();
493 }
494
495 void _insert_only_active_noti_func(app_control_h app_control)
496 {
497         char *type;
498         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
499
500         int ret = app_control_get_extra_data(app_control, "type", &type);
501         if (ret == APP_CONTROL_ERROR_NONE && type) {
502                 if (g_strcmp0(type, "normal") == 0) {
503                         noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
504                 } else if (g_strcmp0(type, "class0") == 0) {
505                         noti_type = MSG_MGR_NOTI_TYPE_CLASS0;
506                 }
507
508                 g_free(type);
509         } else {
510                 MSG_MGR_ERR("app_control_get_extra_data failed");
511                 return;
512         }
513
514         char *msgId = NULL;
515         ret = app_control_get_extra_data(app_control, "msg_id", &msgId);
516         if (ret != APP_CONTROL_ERROR_NONE || msgId == NULL) {
517                 MSG_MGR_ERR("app_control_get_extra_data failed");
518                 return;
519         }
520
521         int msg_err = MSG_SUCCESS;
522         msg_message_id_t msg_id = atoi(msgId);
523         msg_struct_t msg = NULL;
524         msg_struct_t opt = NULL;
525         msg_list_handle_t addr_list = NULL;
526         MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
527         msg_info.msgId = msg_id;
528
529         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
530         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
531         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
532         if (msg_err != MSG_SUCCESS) {
533                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
534                 msg_release_struct(&msg);
535                 msg_release_struct(&opt);
536                 return;
537         }
538
539         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &msg_info.sim_idx);
540
541         if (noti_type == MSG_MGR_NOTI_TYPE_CLASS0) {
542                 msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
543
544                 msg_struct_t addr_data = msg_list_nth_data(addr_list, 0);
545                 msg_get_str_value(addr_data, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, msg_info.addressVal, MAX_ADDRESS_VAL_LEN);
546                 msg_get_str_value(addr_data, MSG_ADDRESS_INFO_DISPLAYNAME_STR, msg_info.displayName, MAX_DISPLAY_NAME_LEN);
547
548                 int msg_size = 0;
549                 msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msg_size);
550                 if (msg_size > 0)
551                         msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, msg_info.msgText, MAX_MSG_TEXT_LEN);
552         }
553
554         msg_release_struct(&msg);
555         msg_release_struct(&opt);
556
557         g_free(msgId);
558
559         MsgMgrInsertOnlyActiveNotification(noti_type, &msg_info);
560 }
561
562 void _insert_ticker_func(app_control_h app_control)
563 {
564         char *ticker_msg = NULL;
565         char *locale_ticker_msg = NULL;
566         char *feedback_str = NULL;
567         bool feedback = false;
568         char *msg_id_str = NULL;
569         int msg_id = 0;
570
571         int ret = app_control_get_extra_data(app_control, "ticker_msg", &ticker_msg);
572         if (ret != APP_CONTROL_ERROR_NONE || ticker_msg == NULL) {
573                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
574                 goto _END_OF_INSERT_TICKER;
575         }
576
577         ret = app_control_get_extra_data(app_control, "locale_ticker_msg", &locale_ticker_msg);
578         if (ret != APP_CONTROL_ERROR_NONE || locale_ticker_msg == NULL) {
579                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
580                 goto _END_OF_INSERT_TICKER;
581         }
582
583         ret = app_control_get_extra_data(app_control, "feedback", &feedback_str);
584         if (ret != APP_CONTROL_ERROR_NONE || feedback_str == NULL) {
585                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
586                 goto _END_OF_INSERT_TICKER;
587         } else {
588                 if (g_strcmp0(feedback_str, "true") == 0)
589                         feedback = true;
590                 else
591                         feedback = false;
592         }
593
594         ret = app_control_get_extra_data(app_control, "msg_id", &msg_id_str);
595         if (ret != APP_CONTROL_ERROR_NONE || msg_id_str == NULL) {
596                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
597                 goto _END_OF_INSERT_TICKER;
598         } else {
599                 msg_id = atoi(msg_id_str);
600         }
601
602         ret = MsgMgrInsertTicker(ticker_msg, locale_ticker_msg, feedback, msg_id);
603         if (ret != 0)
604                 MSG_MGR_ERR("MsgMgrInsertTicker failed [%d]", ret);
605
606 _END_OF_INSERT_TICKER:
607         if (ticker_msg)
608                 g_free(ticker_msg);
609
610         if (locale_ticker_msg)
611                 g_free(locale_ticker_msg);
612
613         if (feedback_str)
614                 g_free(feedback_str);
615
616         if (msg_id_str)
617                 g_free(msg_id_str);
618 }
619
620 void _sound_play_start_func(app_control_h app_control)
621 {
622         char *type;
623         MSG_MGR_SOUND_TYPE_T sound_type = MSG_MGR_SOUND_PLAY_DEFAULT;
624
625         int ret = app_control_get_extra_data(app_control, "type", &type);
626         if (ret == APP_CONTROL_ERROR_NONE && type) {
627                 if (g_strcmp0(type, "default") == 0) {
628                         sound_type = MSG_MGR_SOUND_PLAY_DEFAULT;
629                 } else if (g_strcmp0(type, "user") == 0) {
630                         sound_type = MSG_MGR_SOUND_PLAY_USER;
631                 } else if (g_strcmp0(type, "emergency") == 0) {
632                         sound_type = MSG_MGR_SOUND_PLAY_EMERGENCY;
633                 } else if (g_strcmp0(type, "voicemail") == 0) {
634                         sound_type = MSG_MGR_SOUND_PLAY_VOICEMAIL;
635                 }
636
637                 g_free(type);
638         } else {
639                 MSG_MGR_ERR("app_control_get_extra_data failed");
640                 return;
641         }
642
643         char *addr = NULL;
644         ret = app_control_get_extra_data(app_control, "address", &addr);
645
646         if (addr) {
647                 MSG_MGR_ADDRESS_INFO_S addr_info = {0, };
648                 snprintf(addr_info.addressVal, MAX_ADDRESS_VAL_LEN, "%s", addr);
649
650                 MsgMgrSoundPlayStart(&addr_info, sound_type);
651
652                 g_free(addr);
653         } else {
654                 MsgMgrSoundPlayStart(NULL, sound_type);
655         }
656 }
657
658 void _change_pm_state_func(app_control_h app_control)
659 {
660         MsgMgrChangePmState();
661 }
662
663 void _on_boot_func()
664 {
665         MsgMgrOnBoot();
666 }
667
668 void _reply_msg_func(app_control_h app_control)
669 {
670         char *text = NULL;
671         char *addr = NULL;
672         char *slot_id = NULL;
673
674         int ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_TEXT, &text);
675         if (ret != APP_CONTROL_ERROR_NONE || text == NULL) {
676                 MSG_MGR_ERR("app_control_get_extra_data failed");
677                 return;
678         }
679
680         ret = app_control_get_extra_data(app_control, "addr", &addr);
681         if (ret != APP_CONTROL_ERROR_NONE || addr == NULL) {
682                 MSG_MGR_ERR("app_control_get_extra_data failed");
683                 g_free(text);
684                 return;
685         }
686
687         ret = app_control_get_extra_data(app_control, "slot_id", &slot_id);
688         if (ret != APP_CONTROL_ERROR_NONE || slot_id == NULL) {
689                 MSG_MGR_ERR("app_control_get_extra_data failed");
690                 g_free(text);
691                 g_free(addr);
692                 return ;
693         }
694
695         MSG_MGR_DEBUG("text [%s]", text);
696         MSG_MGR_DEBUG("addr [%s]", addr);
697         MSG_MGR_DEBUG("slot_id [%s]", slot_id);
698
699         msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
700         msg_struct_t opt = msg_create_struct(MSG_STRUCT_SENDOPT);
701
702         msg_struct_t addr_item = NULL;
703         msg_list_add_item(msg, MSG_MESSAGE_ADDR_LIST_HND, &addr_item);
704         msg_set_int_value(addr_item, MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
705         msg_set_int_value(addr_item, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_TO);
706         msg_set_str_value(addr_item, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr, strlen(addr));
707
708         msg_set_int_value(msg, MSG_MESSAGE_TYPE_INT, MSG_TYPE_SMS);
709         msg_set_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, atoi(slot_id));
710         msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, text, strlen(text));
711
712         msg_struct_t req = msg_create_struct(MSG_STRUCT_REQUEST_INFO);
713         msg_set_struct_handle(req, MSG_REQUEST_MESSAGE_HND, msg);
714         msg_set_struct_handle(req, MSG_REQUEST_SENDOPT_HND, opt);
715
716         int msg_err = msg_sms_send_message(msg_handle, req);
717         if (msg_err != MSG_SUCCESS)
718                 MSG_MGR_ERR("msg_sms_send_message() failed [%d]", msg_err);
719
720         char *msg_id = NULL;
721         ret = app_control_get_extra_data(app_control, "msg_id", &msg_id);
722         if (ret != APP_CONTROL_ERROR_NONE || msg_id == NULL) {
723                 MSG_MGR_ERR("app_control_get_extra_data failed");
724         } else {
725                 msg_err = msg_update_read_status(msg_handle, atoi(msg_id), true);
726                 if (msg_err != MSG_SUCCESS)
727                         MSG_MGR_ERR("msg_update_read_status() failed [%d]", msg_err);
728
729                 g_free(msg_id);
730         }
731
732         msg_release_struct(&msg);
733         msg_release_struct(&opt);
734         msg_release_struct(&req);
735
736         g_free(text);
737         g_free(addr);
738         g_free(slot_id);
739 }
740
741 void service_app_control(app_control_h app_control, void *data)
742 {
743         MSG_MGR_INFO("service_app_control called");
744
745         pthread_mutex_lock(&mx);
746         job_cnt++;
747         pthread_mutex_unlock(&mx);
748
749         int ret = 0;
750         char *operation = NULL;
751         char *cmd = NULL;
752
753         ret = app_control_get_operation(app_control, &operation);
754         if (ret == APP_CONTROL_ERROR_NONE && operation) {
755                 if (g_strcmp0(operation, APP_CONTROL_OPERATION_DEFAULT) == 0) {
756                         ret = app_control_get_extra_data(app_control, "cmd", &cmd);
757                         if (ret == APP_CONTROL_ERROR_NONE && cmd) {
758                                 MSG_MGR_DEBUG("cmd [%s]", cmd);
759
760                                 if (g_strcmp0(cmd, "incoming_msg") == 0) {
761                                         _incoming_msg_func(app_control);
762                                 } else if (g_strcmp0(cmd, "outgoing_msg") == 0) {
763                                         _outgoing_msg_func(app_control);
764                                 } else if (g_strcmp0(cmd, "refresh_noti") == 0) {
765                                         _refresh_noti_func(app_control);
766                                 } else if (g_strcmp0(cmd, "add_noti") == 0) {
767                                         _add_noti_func(app_control);
768                                 } else if (g_strcmp0(cmd, "del_noti") == 0) {
769                                         _del_noti_func(app_control);
770                                 } else if (g_strcmp0(cmd, "add_report_noti") == 0) {
771                                         _add_report_noti_func(app_control);
772                                 } else if (g_strcmp0(cmd, "del_report_noti") == 0) {
773                                         _del_report_noti_func(app_control);
774                                 } else if (g_strcmp0(cmd, "del_sent_read_report_noti") == 0) {
775                                         _del_sent_read_report_noti_func(app_control);
776                                 } else if (g_strcmp0(cmd, "insert_only_active_noti") == 0) {
777                                         _insert_only_active_noti_func(app_control);
778                                 } else if (g_strcmp0(cmd, "insert_ticker") == 0) {
779                                         _insert_ticker_func(app_control);
780                                 } else if (g_strcmp0(cmd, "sound_play_start") == 0) {
781                                         _sound_play_start_func(app_control);
782                                 } else if (g_strcmp0(cmd, "change_pm_state") == 0) {
783                                         _change_pm_state_func(app_control);
784                                 } else if (g_strcmp0(cmd, "reply_msg") == 0) {
785                                         _reply_msg_func(app_control);
786                                 }
787
788                                 g_free(cmd);
789                         } else if (ret == APP_CONTROL_ERROR_KEY_NOT_FOUND) {
790                                 MSG_MGR_INFO("on boot");
791                                 _on_boot_func();
792                         }
793                 }
794                 g_free(operation);
795         }
796
797         pthread_mutex_lock(&mx);
798         if (!terminated)
799                 g_timeout_add_seconds(60, _check_app_terminate, NULL);
800         pthread_mutex_unlock(&mx);
801
802         return;
803 }
804
805 int main(int argc, char* argv[])
806 {
807         service_app_lifecycle_callback_s event_callback = {0, };
808
809         event_callback.create = service_app_create;
810         event_callback.terminate = service_app_terminate;
811         event_callback.app_control = service_app_control;
812
813         return service_app_main(argc, argv, &event_callback, NULL);
814 }