Changed the deprecated thumbnail_util APIs in msg-service
[platform/core/messaging/msg-service.git] / externals / MsgAlarm.cpp
1 /*
2  * Copyright (c) 2014 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 <map>
19
20 #include "MsgDebug.h"
21 #include "MsgAlarm.h"
22
23 #include <alarm.h>
24
25
26 /*==================================================================================================
27                                      DEFINES
28 ==================================================================================================*/
29 typedef std::map<int, msg_alarm_cb> callBackMap;
30
31 /*==================================================================================================
32                                      VARIABLES
33 ==================================================================================================*/
34 bool alarmInit = false;
35 callBackMap alarmCBMap;
36
37 /*==================================================================================================
38                             INTERNAL FUNCTION IMPLEMENTATION
39 ==================================================================================================*/
40 int MsgAlarmCB(int alarmId, void *pUserParam)
41 {
42         MSG_DEBUG("MsgAlarmCB is called. alarmId [%d]", alarmId);
43
44         callBackMap::iterator it = alarmCBMap.find(alarmId);
45
46         if (it == alarmCBMap.end()) {
47                 MSG_DEBUG("alarmId is not found.");
48         } else {
49                 msg_alarm_cb alarmCBfunction = it->second;
50                 if (alarmCBfunction)
51                         alarmCBfunction(alarmId);
52
53                 alarmCBMap.erase(it);
54         }
55
56         return 0;
57 }
58
59
60 msg_error_t MsgAlarmInit()
61 {
62         MSG_BEGIN();
63
64         alarmCBMap.clear();
65
66         int retval = alarmmgr_init("msg-service-tools");
67         if (retval != ALARMMGR_RESULT_SUCCESS) {
68                 MSG_DEBUG("alarmmgr_init error [%d]", retval);
69                 return MSG_ERR_UNKNOWN;
70         }
71
72         retval = alarmmgr_set_cb(MsgAlarmCB, NULL);
73         if (retval != ALARMMGR_RESULT_SUCCESS)
74                 MSG_DEBUG("alarmmgr_set_cb() [%d]", retval);
75
76         alarmInit = true;
77
78         MSG_END();
79         return MSG_SUCCESS;
80 }
81
82 /*==================================================================================================
83                             FUNCTION IMPLEMENTATION
84 ==================================================================================================*/
85 msg_error_t MsgAlarmRegistration(struct tm *timeInfo, msg_alarm_cb userCB, int *alarmId)
86 {
87         MSG_BEGIN();
88
89         if (!alarmInit) {
90                 MSG_DEBUG("alarm manager is not initialized. Retry once.");
91                 MsgAlarmInit();
92                 if (!alarmInit) {
93                         MSG_DEBUG("alarm manager is still not initialized. So return error.");
94                         return MSG_ERR_UNKNOWN;
95                 }
96         }
97
98         if (timeInfo == NULL || alarmId == NULL) {
99                 MSG_DEBUG("(timeInfo == NULL || alarmId == NULL)");
100                 return MSG_ERR_INVALID_PARAMETER;
101         }
102
103         *alarmId = 0;
104         alarm_info_t *alarm_info = NULL;
105         alarm_id_t alarm_id = -1;
106         alarm_date_t target_time;
107
108         int retval = ALARMMGR_RESULT_SUCCESS;
109
110         alarm_info = alarmmgr_create_alarm();
111         if (alarm_info == NULL) {
112                 MSG_DEBUG("alarmmgr_create_alarm error.");
113                 return MSG_ERR_UNKNOWN;
114         }
115
116         target_time.year = timeInfo->tm_year + 1900;
117         target_time.month = timeInfo->tm_mon + 1;
118         target_time.day = timeInfo->tm_mday;
119         target_time.hour = timeInfo->tm_hour;
120         target_time.min = timeInfo->tm_min;
121         target_time.sec = timeInfo->tm_sec;
122
123         MSG_DEBUG("Alarm Time : [%d-%d-%d %d:%d:%d] ",
124                         target_time.year, target_time.month, target_time.day,
125                         target_time.hour, target_time.min, target_time.sec);
126
127         retval = alarmmgr_set_time(alarm_info, target_time);
128         if (retval != ALARMMGR_RESULT_SUCCESS)
129                 MSG_DEBUG("alarmmgr_set_time ret[%d]", retval);
130
131         retval = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
132         if (retval != ALARMMGR_RESULT_SUCCESS)
133                 MSG_DEBUG("alarmmgr_set_repeat_mode ret[%d]", retval);
134
135         retval = alarmmgr_set_type(alarm_info, ALARM_TYPE_DEFAULT);
136         if (retval != ALARMMGR_RESULT_SUCCESS)
137                 MSG_DEBUG("alarmmgr_set_type ret[%d]", retval);
138
139         retval = alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &alarm_id);
140         MSG_DEBUG("alarmmgr_add_alarm_with_localtime ret[%d], alarm_id[%d]", retval, alarm_id);
141
142         retval = alarmmgr_free_alarm(alarm_info);
143         if (retval != ALARMMGR_RESULT_SUCCESS)
144                 MSG_DEBUG("alarmmgr_free_alarm ret[%d]", retval);
145
146         *alarmId = (int)alarm_id;
147
148         alarmCBMap[*alarmId] = userCB;
149
150         MSG_END();
151         return MSG_SUCCESS;
152 }
153
154
155 msg_error_t MsgAlarmRemove(int alarmId)
156 {
157         MSG_BEGIN();
158
159         if (!alarmInit) {
160                 MSG_DEBUG("alarm manager is not initialized. Retry once.");
161                 MsgAlarmInit();
162                 if (!alarmInit) {
163                         MSG_DEBUG("alarm manager is still not initialized. So return error.");
164                         return MSG_ERR_UNKNOWN;
165                 }
166         }
167
168         if (alarmmgr_remove_alarm(alarmId) != ALARMMGR_RESULT_SUCCESS) {
169                 MSG_DEBUG("alarmmgr_remove_alarm faild.");
170                 return MSG_ERR_UNKNOWN;
171         }
172
173         MSG_END();
174         return MSG_SUCCESS;
175 }