+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <app_alarm.h>
+#include <app_common.h>
+#include <stdlib.h>
+
+#include "log.h"
+
+static int recurring_alarm_id = -1;
+
+static bool
+on_foreach_registered_alarm(int alarm_id, void *user_data)
+{
+ bool *result = (bool *)user_data;
+
+ LOGD("alarm id : %d", alarm_id);
+
+ *result = *result | 0x1;
+
+ return false;
+}
+
+bool sync_alarm_register(const char *operation, int delay, int period)
+{
+ int ret;
+ app_control_h app_control = NULL;
+ char *app_id = NULL;
+ bool result = false;
+
+ ret = app_control_create(&app_control);
+ if (ret != APP_CONTROL_ERROR_NONE)
+ {
+ LOGE("Function app_control_create() failed.");
+ return false;
+ }
+
+ ret = app_control_set_operation(app_control, operation);
+ if (ret != APP_CONTROL_ERROR_NONE)
+ {
+ LOGE("Function app_control_set_operation() failed.");
+ goto cleanup;
+ }
+
+ if (app_get_id(&app_id) != APP_ERROR_NONE) {
+ goto cleanup;
+ }
+
+ LOGD("app id : %s", app_id);
+ if (!app_id) {
+ goto cleanup;
+ }
+
+ ret = app_control_set_app_id(app_control, app_id);
+ if (ret != APP_CONTROL_ERROR_NONE)
+ {
+ LOGE("Function app_control_set_app_id() failed.");
+ goto cleanup;
+ }
+
+ alarm_cancel_all();
+
+ ret = alarm_schedule_after_delay(app_control, delay, period, &recurring_alarm_id);
+ if (ret != ALARM_ERROR_NONE)
+ LOGE("Function alarm_schedule_after_delay() failed.");
+ else
+ LOGD("Function alarm_schedule_after_delay() succeed. delay : %d, interval : %d", delay, period);
+
+ result = true;
+
+cleanup:
+ if (app_id)
+ free(app_id);
+
+ if (app_control) {
+ ret = app_control_destroy(app_control);
+ if (ret != APP_CONTROL_ERROR_NONE)
+ LOGE("Function app_control_destroy() failed.");
+ else
+ LOGD("Set recurring alarm with id: %i", recurring_alarm_id);
+ }
+
+ return result;
+}
+
+bool sync_alarm_exist()
+{
+ bool result = false;
+ int ret = alarm_foreach_registered_alarm(on_foreach_registered_alarm, &result);
+ if (ret != ALARM_ERROR_NONE)
+ LOGD("Listing error : %d", ret);
+
+ return result;
+}
\ No newline at end of file