Improve typing of constants data
[platform/core/messaging/msg-service.git] / utils / MsgSoundPlayer.cpp
1 /*
2  * msg-service
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include <wait.h>
21
22 #include "MsgHelper.h"
23 #include "MsgDebug.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgSettingTypes.h"
26 #include "MsgSoundPlayer.h"
27
28 #include <alarm.h>
29
30 /*==================================================================================================
31                                      VARIABLES
32 ==================================================================================================*/
33 static bool g_bRepeat = false;
34 static alarm_id_t g_alarmId = 0;
35
36
37 /*==================================================================================================
38                                      FUNCTION IMPLEMENTATION
39 ==================================================================================================*/
40
41
42 void MsgSoundPlayStart(bool isEmergency)
43 {
44         MSG_BEGIN();
45
46         // Run helper App
47         pid_t childpid;
48         childpid = fork();
49
50         if (childpid == -1)
51         {
52                 MSG_DEBUG("Failed to fork");
53         }
54
55         if (childpid == 0)
56         {
57                 MSG_DEBUG("Child Process - Run helper app for Sound");
58
59                 if (isEmergency)
60                         execl("/usr/bin/msg-helper", MSG_EMERGENCY_SOUND_START, NULL);
61                 else
62                         execl("/usr/bin/msg-helper", MSG_NORMAL_SOUND_START, NULL);
63
64                 MSG_DEBUG("Faild to run helper app for Sound");
65
66                 exit(0);
67         }
68         else if (childpid != 0)
69         {
70                 MSG_DEBUG("Parent Process - Mms Plugin Storage.");
71         }
72
73         if (g_bRepeat == false)
74                 g_bRepeat = MsgSoundSetRepeatAlarm();
75
76         MSG_END();
77 }
78
79 void MsgSoundPlayStop()
80 {
81         MSG_BEGIN();
82
83         // Run helper App
84         pid_t childpid;
85         childpid = fork();
86
87         if (childpid == -1)
88         {
89                 MSG_DEBUG("Failed to fork");
90         }
91
92         if (childpid == 0)
93         {
94                 MSG_DEBUG("Child Process - Run helper app for Sound");
95
96                 execl("/usr/bin/msg-helper", MSG_SOUND_STOP, NULL);
97
98                 MSG_DEBUG("Faild to run helper app for Sound");
99
100                 exit(0);
101         }
102         else if (childpid != 0)
103         {
104                 MSG_DEBUG("Parent Process - Mms Plugin Storage.");
105         }
106
107         MSG_END();
108 }
109
110
111 bool MsgSoundSetRepeatAlarm()
112 {
113         bool bRet = false;
114
115         int nRepeatValue = 0;
116         long    nRepeatTime = 0;
117
118         nRepeatValue = MsgSettingGetInt(MSG_ALERT_TONE);
119
120         switch (nRepeatValue)
121         {
122                 case MSG_ALERT_TONE_ONCE:
123                         nRepeatTime = 0;
124                 break;
125
126                 case MSG_ALERT_TONE_2MINS:
127                         nRepeatTime = 2;
128                 break;
129
130                 case MSG_ALERT_TONE_5MINS:
131                         nRepeatTime = 5;
132                 break;
133
134                 case MSG_ALERT_TONE_10MINS:
135                         nRepeatTime = 10;
136                 break;
137
138                 default:
139                         MSG_DEBUG("Invalid Repetition time");
140                 break;
141         }
142
143         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
144
145         if (nRepeatTime > 0)
146         {
147                 bRet = MsgSoundCreateRepeatAlarm(nRepeatTime);
148         }
149
150         return bRet;
151 }
152
153
154 bool MsgSoundCreateRepeatAlarm(int RepeatTime)
155 {
156         MSG_BEGIN();
157
158         time_t current_time;
159         struct tm current_tm;
160
161         time(&current_time);
162
163         localtime_r(&current_time, &current_tm);
164
165         int retval =0;
166
167         retval = alarmmgr_init("msg-service-tools");
168
169         if (retval != 0)
170         {
171                 MSG_FATAL("alarmmgr_init() error [%d]", retval);
172                 return false;
173         }
174
175         alarm_info_t* alarm_info;
176         alarm_date_t target_time;
177
178         alarm_info = alarmmgr_create_alarm();
179
180         target_time.year = 0;
181         target_time.month = 0;
182         target_time.day = 0;
183
184         if (current_tm.tm_min+RepeatTime < 60)
185         {
186                 target_time.hour = current_tm.tm_hour;
187                 target_time.min = current_tm.tm_min+RepeatTime;
188         }
189         else
190         {
191                 if (current_tm.tm_hour < 12)
192                 {
193                         target_time.hour = current_tm.tm_hour+1;
194                 }
195                 else
196                 {
197                         target_time.hour = (current_tm.tm_hour+1)%12;
198                 }
199
200                 target_time.min = (current_tm.tm_min+RepeatTime)%60;
201         }
202
203         target_time.sec = current_tm.tm_sec;
204
205         retval = alarmmgr_set_time(alarm_info, target_time);
206         MSG_DEBUG("alarmmgr_set_time return = [%d]", retval);
207         retval = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
208         MSG_DEBUG("alarmmgr_set_repeat_mode return = [%d]", retval);
209         retval = alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
210         MSG_DEBUG("alarmmgr_set_type return = [%d]", retval);
211         retval = alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &g_alarmId);
212         MSG_DEBUG("alarmmgr_add_alarm_with_localtime return = [%d]", retval);
213
214         retval = alarmmgr_set_cb(MsgSoundRepeatAlarmCB, (void *)alarm_info);
215
216         if (retval != 0)
217         {
218                 MSG_DEBUG("alarmmgr_set_cb() error [%d]", retval);
219                 return false;
220         }
221
222         MSG_DEBUG("Repeat Alarm Time : [%d-%d-%d %d:%d:%d]",
223                 target_time.year,target_time.month,target_time.day,
224                 target_time.hour, target_time.min, target_time.sec);
225
226         MSG_END();
227
228         return true;
229 }
230
231
232 int MsgSoundRepeatAlarmCB(int TimerId, void *pUserParam)
233 {
234         MSG_BEGIN();
235
236         alarm_info_t *alarm_info = (alarm_info_t *)pUserParam;
237
238         g_bRepeat = false;
239
240         if (MsgSoundGetUnreadMsgCnt() <= 0) {
241                 MSG_DEBUG("no unread msg");
242         } else {
243                 MsgSoundPlayStart(false);
244         }
245
246         if (alarmmgr_free_alarm(alarm_info) != ALARMMGR_RESULT_SUCCESS)
247                 MSG_DEBUG("alarmmgr_free_alarm is failed");
248
249         MSG_END();
250
251         return 0;
252 }
253
254
255 int MsgSoundGetUnreadMsgCnt()
256 {
257         int unreadCnt = 0;
258
259         // Get SMS Count
260         unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
261
262         // Get MMS Count
263         unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
264
265         MSG_DEBUG("unread count : [%d]", unreadCnt);
266
267         return unreadCnt;
268 }
269
270 void MsgSoundInitRepeatAlarm()
271 {
272         MSG_BEGIN();
273
274         int nRepeatValue = 0;
275         long    nRepeatTime = 0;
276
277         g_bRepeat = false;
278
279         if (MsgSoundGetUnreadMsgCnt() <= 0) {
280                 MSG_DEBUG("no unread msg");
281                 return;
282         }
283
284         nRepeatValue = MsgSettingGetInt(MSG_ALERT_TONE);
285
286         switch (nRepeatValue)
287         {
288                 case MSG_ALERT_TONE_ONCE:
289                         nRepeatTime = 0;
290                 break;
291
292                 case MSG_ALERT_TONE_2MINS:
293                         nRepeatTime = 2;
294                 break;
295
296                 case MSG_ALERT_TONE_5MINS:
297                         nRepeatTime = 5;
298                 break;
299
300                 case MSG_ALERT_TONE_10MINS:
301                         nRepeatTime = 10;
302                 break;
303
304                 default:
305                         MSG_DEBUG("Invalid Repetition time");
306                 break;
307         }
308
309         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
310
311         if (nRepeatTime > 0)
312                 MsgSoundPlayStart(false);
313
314         MSG_END();
315 }