2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 /*==================================================================================================
28 ==================================================================================================*/
29 typedef std::map<int, msg_alarm_cb> callBackMap;
31 /*==================================================================================================
33 ==================================================================================================*/
34 bool alarmInit = false;
35 callBackMap alarmCBMap;
37 /*==================================================================================================
38 INTERNAL FUNCTION IMPLEMENTATION
39 ==================================================================================================*/
40 int MsgAlarmCB(int alarmId, void *pUserParam)
42 MSG_DEBUG("MsgAlarmCB is called. alarmId [%d]", alarmId);
44 callBackMap::iterator it = alarmCBMap.find(alarmId);
46 if (it == alarmCBMap.end()) {
47 MSG_DEBUG("alarmId is not found.");
49 msg_alarm_cb alarmCBfunction = it->second;
51 alarmCBfunction(alarmId);
60 msg_error_t MsgAlarmInit()
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;
72 retval = alarmmgr_set_cb(MsgAlarmCB, NULL);
73 if (retval != ALARMMGR_RESULT_SUCCESS)
74 MSG_DEBUG("alarmmgr_set_cb() [%d]", retval);
82 /*==================================================================================================
83 FUNCTION IMPLEMENTATION
84 ==================================================================================================*/
85 msg_error_t MsgAlarmRegistration(struct tm *timeInfo, msg_alarm_cb userCB, int *alarmId)
90 MSG_DEBUG("alarm manager is not initialized. Retry once.");
93 MSG_DEBUG("alarm manager is still not initialized. So return error.");
94 return MSG_ERR_UNKNOWN;
98 if (timeInfo == NULL || alarmId == NULL) {
99 MSG_DEBUG("(timeInfo == NULL || alarmId == NULL)");
100 return MSG_ERR_INVALID_PARAMETER;
104 alarm_info_t *alarm_info = NULL;
105 alarm_id_t alarm_id = -1;
106 alarm_date_t target_time;
108 int retval = ALARMMGR_RESULT_SUCCESS;
110 alarm_info = alarmmgr_create_alarm();
111 if (alarm_info == NULL) {
112 MSG_DEBUG("alarmmgr_create_alarm error.");
113 return MSG_ERR_UNKNOWN;
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;
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);
127 retval = alarmmgr_set_time(alarm_info, target_time);
128 if (retval != ALARMMGR_RESULT_SUCCESS)
129 MSG_DEBUG("alarmmgr_set_time ret[%d]", retval);
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);
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);
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);
142 retval = alarmmgr_free_alarm(alarm_info);
143 if (retval != ALARMMGR_RESULT_SUCCESS)
144 MSG_DEBUG("alarmmgr_free_alarm ret[%d]", retval);
146 *alarmId = (int)alarm_id;
148 alarmCBMap[*alarmId] = userCB;
155 msg_error_t MsgAlarmRemove(int alarmId)
160 MSG_DEBUG("alarm manager is not initialized. Retry once.");
163 MSG_DEBUG("alarm manager is still not initialized. So return error.");
164 return MSG_ERR_UNKNOWN;
168 if (alarmmgr_remove_alarm(alarmId) != ALARMMGR_RESULT_SUCCESS) {
169 MSG_DEBUG("alarmmgr_remove_alarm faild.");
170 return MSG_ERR_UNKNOWN;