Add plugin for alarm-server 14/220014/27
authormk5004.lee <mk5004.lee@samsung.com>
Thu, 12 Dec 2019 09:31:57 +0000 (18:31 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Tue, 21 Jan 2020 08:13:16 +0000 (17:13 +0900)
- To sync with tizen_wearable branch
  server/plugin/platform

Change-Id: If8cadd532bd7735d99b4c515cf4a22d02edbb36e
Signed-off-by: mk5004.lee <mk5004.lee@samsung.com>
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
packaging/alarm-manager.spec
packaging/alarm-server-plugin.manifest [new file with mode: 0644]
server/CMakeLists.txt
server/alarm-manager.c
server/plugin/CMakeLists.txt [new file with mode: 0644]
server/plugin/platform/CMakeLists.txt [new file with mode: 0644]
server/plugin/platform/alarm-server-plugin.pc.in [new file with mode: 0644]
server/plugin/platform/include/plugin.h [new file with mode: 0644]
server/plugin/platform/src/alarm-server-plugin.c [new file with mode: 0644]

index 70d0fd2..8ebb196 100644 (file)
@@ -12,6 +12,7 @@ Source4:    alarm-manager.conf
 Source5:    99-rtc.rules
 Source1001: alarm-server.manifest
 Source1002: alarm-lib.manifest
+Source1003: alarm-server-plugin.manifest
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
 
@@ -109,6 +110,19 @@ Requires:   libalarm = %{version}-%{release}
 A configuration file package for restricting service applications
 
 #################################################
+# plugin
+#################################################
+%package -n alarm-server-plugin
+Summary:    Alarm server plugin libraries
+License:    apache-2.0
+Group:      Development/Libraries
+Requires:   alarm-server-plugin = %{version}-%{release}
+
+%description -n alarm-server-plugin
+A plugin package for alarm-server
+
+
+#################################################
 # unittests
 #################################################
 %package unittests
@@ -137,6 +151,7 @@ Alarm library gcov objects
 %setup -q
 cp %{SOURCE1001} ./
 cp %{SOURCE1002} ./
+cp %{SOURCE1003} ./
 
 %build
 %if 0%{?gcov:1}
@@ -257,6 +272,17 @@ fi
 %files -n alarm-config-service-restricted
 %{_datadir}/alarm-manager/alarm-config-service-restricted
 
+#################################################
+# plugin
+#################################################
+%files -n alarm-server-plugin
+%manifest alarm-server-plugin.manifest
+%{_libdir}/libalarm-server-plugin.so
+%license LICENSE
+
+#################################################
+# unittests
+#################################################
 %if 0%{?gtests:1}
 %files unittests
 %{_bindir}/gtest*
diff --git a/packaging/alarm-server-plugin.manifest b/packaging/alarm-server-plugin.manifest
new file mode 100644 (file)
index 0000000..97e8c31
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_"/>
+       </request>
+</manifest>
index fd55d4b..2dc595d 100755 (executable)
@@ -30,3 +30,5 @@ INSTALL(FILES alarm-service.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/syste
 
 CONFIGURE_FILE(${DBUS_INTERFACE}.service.in ${DBUS_INTERFACE}.service @ONLY)
 INSTALL(FILES ${DBUS_INTERFACE}.service DESTINATION ${SHARE_INSTALL_PREFIX}/dbus-1/system-services/)
+
+ADD_SUBDIRECTORY(plugin)
index 9bbb591..0717b50 100644 (file)
@@ -24,8 +24,8 @@
 #include <unistd.h>
 #include <errno.h>
 #include <linux/rtc.h>
-#include <sys/ioctl.h>
 #include <fcntl.h>
+#include <dlfcn.h>
 
 #include <tzplatform_config.h>
 #include <aul.h>
 
 #define _APPFW_FEATURE_WAKEUP_USING_RTC (_get_profile() != PROFILE_TV)
 
+#define PATH_LIB_ALARMMGR_PLUGIN "/usr/lib/libalarm-server-plugin.so"
 
-static const char default_rtc[] = "/dev/rtc";
-static int gfd = -1;
+static void *plugin_handle;
+static int (*RTC_INIT)(void);
+static int (*CLEAR_WAKEUP_RTC)(void);
+static int (*SET_WAKEUP_RTC)(time_t due_time);
+static int (*SET_WAKEUP_RTC_WITH_DATETIME)(struct tm *tm);
+static int (*SET_SYSTIME_RTC)(time_t new_time);
+static void (*DO_AFTER_TIMEZONE_SET)(void);
+static void (*DO_AFTER_ALARM_CREATE)(int pid, const char *unique_name);
+static void (*DO_AFTER_ALARM_EXPIRE)(int pid, const char *unique_name);
+static int (*PS_MODE_INIT)(void);
+static int (*GET_POWER_SAVING_MODE)(void);
 
 __alarm_server_context_t alarm_context;
 
@@ -223,22 +233,12 @@ void __free_cached_value(gpointer data)
 void _rtc_set()
 {
        if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
-               const char *rtc = default_rtc;
-               struct tm due_tm;
                char log_message[ALARMMGR_LOG_MESSAGE_SIZE] = {0,};
 #ifdef _SIMUL /* RTC does not work in simulator. */
                LOGE("because it is simulator's mode, we don't set RTC.");
                return;
 #endif
 
-               if (gfd < 0) {
-                       gfd = open(rtc, O_RDWR);
-                       if (gfd < 0) {
-                               LOGE("RTC open failed.");
-                               return;
-                       }
-               }
-
                /* Read the RTC time/date */
                int retval = 0;
                char buf[1024];
@@ -251,16 +251,7 @@ void _rtc_set()
                LOGD("alarm_context.c_due_time is %d.", (int)alarm_context.c_due_time);
 
                if (alarm_context.c_due_time != -1) {
-                       struct rtc_wkalrm rtc_wkalarm = { 0, };
-                       rtc_wkalarm.enabled = 0;
-                       rtc_wkalarm.time.tm_year = 1900;
-                       rtc_wkalarm.time.tm_mon = 0;
-                       rtc_wkalarm.time.tm_mday = 1;
-                       rtc_wkalarm.time.tm_hour = 0;
-                       rtc_wkalarm.time.tm_min = 0;
-                       rtc_wkalarm.time.tm_sec = 0;
-
-                       retval = ioctl(gfd, RTC_WKALM_SET, &rtc_wkalarm);
+                       retval = CLEAR_WAKEUP_RTC();
                        if (retval == -1) {
                                if (errno == ENOTTY)
                                        LOGE("Alarm IRQs is not supported.");
@@ -271,21 +262,7 @@ void _rtc_set()
                        }
                        LOGD("[alarm-server]RTC_WKALM_SET disabled ioctl is successfully done.");
 
-                       time_t due_time = alarm_context.c_due_time;
-                       gmtime_r(&due_time, &due_tm);
-
-                       LOGD("Setted RTC Alarm date/time is %d-%d-%d, %02d:%02d:%02d (UTC).",
-                                       due_tm.tm_mday, due_tm.tm_mon + 1, due_tm.tm_year + 1900,
-                                       due_tm.tm_hour, due_tm.tm_min, due_tm.tm_sec);
-
-                       rtc_wkalarm.enabled = 1;
-                       rtc_wkalarm.time.tm_year = due_tm.tm_year;
-                       rtc_wkalarm.time.tm_mon = due_tm.tm_mon;
-                       rtc_wkalarm.time.tm_mday = due_tm.tm_mday;
-                       rtc_wkalarm.time.tm_hour = due_tm.tm_hour;
-                       rtc_wkalarm.time.tm_min = due_tm.tm_min;
-                       rtc_wkalarm.time.tm_sec = due_tm.tm_sec;
-                       retval = ioctl(gfd, RTC_WKALM_SET, &rtc_wkalarm);
+                       retval = SET_WAKEUP_RTC(alarm_context.c_due_time);
                        if (retval == -1) {
                                if (errno == ENOTTY)
                                        LOGE("Alarm IRQs is not supported.");
@@ -310,17 +287,9 @@ static bool __set_time(time_t _time)
 {
        int ret = 0;
        struct timeval tv;
-       struct tm tm, *gmtime_res;
-
        tv.tv_sec = _time;
        tv.tv_usec = 0;
 
-       gmtime_res = gmtime_r(&(tv.tv_sec), &tm);
-       if (!gmtime_res) {
-               LOGE("gmtime_r is failed. [%d]", errno);
-               return false;
-       }
-
        ret = settimeofday(&tv, NULL);
        if (ret < 0) {
                LOGE("settimeofday is failed.[%d]", errno);
@@ -328,8 +297,6 @@ static bool __set_time(time_t _time)
        }
 
        if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
-               const char *rtc0 = default_rtc;
-               struct rtc_time _rtc_time;
                char buf[1024];
                char log_tag[ALARMMGR_LOG_TAG_SIZE] = {0,};
                char log_message[ALARMMGR_LOG_MESSAGE_SIZE] = {0,};
@@ -340,26 +307,7 @@ static bool __set_time(time_t _time)
                                        _time, timebuf);
                }
 
-               if (gfd < 0) {
-                       gfd = open(rtc0, O_RDWR);
-                       if (gfd < 0) {
-                               LOGE("Opening the /dev/rtc is failed.");
-                               return false;
-                       }
-               }
-
-               memset(&_rtc_time, 0, sizeof(_rtc_time));
-               _rtc_time.tm_sec = tm.tm_sec;
-               _rtc_time.tm_min = tm.tm_min;
-               _rtc_time.tm_hour = tm.tm_hour;
-               _rtc_time.tm_mday = tm.tm_mday;
-               _rtc_time.tm_mon = tm.tm_mon;
-               _rtc_time.tm_year = tm.tm_year;
-               _rtc_time.tm_wday = tm.tm_wday;
-               _rtc_time.tm_yday = tm.tm_yday;
-               _rtc_time.tm_isdst = tm.tm_isdst;
-
-               ret = ioctl(gfd, RTC_SET_TIME, &_rtc_time);
+               ret = SET_SYSTIME_RTC(_time);
                if (ret == -1) {
                        LOGE("ALARM_SET_RTC ioctl is failed. errno = %s", strerror_r(errno, buf, sizeof(buf)));
                        strncpy(log_tag, "FAIL: SET RTC", sizeof(log_tag) - 1);
@@ -443,6 +391,8 @@ static void __alarm_add_to_list(__alarm_info_t *__alarm_info)
                if (!_save_alarms(__alarm_info))
                        LOGE("Saving alarm_id(%d) in DB is failed.", __alarm_info->alarm_id);
        }
+
+       DO_AFTER_ALARM_CREATE(__alarm_info->pid, __alarm_info->app_unique_name);
 }
 
 static bool __check_bundle_for_update(const gchar *b_data, uid_t uid)
@@ -1649,6 +1599,8 @@ void _alarm_expired()
 
                _save_alarm_info_log("EXPIRED", __alarm_info);
 
+               DO_AFTER_ALARM_EXPIRE(__alarm_info->pid, __alarm_info->app_unique_name);
+
                if (__alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE) {
                        __alarm_remove_from_list(__alarm_info->uid, __alarm_info->alarm_id, NULL);
                } else {
@@ -1794,6 +1746,52 @@ static int __on_app_disable_cb(uid_t target_uid, int req_id,
        return 0;
 }
 
+static int __on_app_installed(uid_t target_uid, int req_id, const char *pkg_type,
+                               const char *pkgid, const char *key, const char *val,
+                               const void *pmsg, void *user_data)
+{
+       GSList *gs_iter = NULL;
+       __alarm_info_t *entry = NULL;
+       bool is_restored = false;
+
+       if (GET_POWER_SAVING_MODE() == -1)
+               return ALARMMGR_RESULT_SUCCESS;
+
+       if ((key && strncmp(key, "end", 3) != 0) || (val && strncmp(val, "ok", 2) != 0))
+               return ALARMMGR_RESULT_SUCCESS;
+
+       if (g_slist_length(g_disabled_alarm_list) == 0)
+               return ALARMMGR_RESULT_SUCCESS;
+
+       for (gs_iter = g_disabled_alarm_list; gs_iter != NULL; ) {
+               entry = gs_iter->data;
+
+               const char *caller_pkgid = entry->caller_pkgid;
+               const char *callee_pkgid = entry->callee_pkgid;
+
+               gs_iter = g_slist_next(gs_iter);
+               if ((caller_pkgid && strncmp(pkgid, caller_pkgid, strlen(pkgid)) == 0) ||
+                       (callee_pkgid && strncmp(pkgid, callee_pkgid, strlen(pkgid)) == 0)) {
+                       _alarm_set_next_duetime(entry);
+                       alarm_context.alarms = g_slist_append(alarm_context.alarms, entry);
+                       g_disabled_alarm_list = g_slist_remove(g_disabled_alarm_list, entry);
+
+                       if (!(entry->alarm_info.alarm_type & ALARM_TYPE_VOLATILE))
+                               _update_db_for_disabled_alarm(entry->alarm_id, false);
+                       is_restored = true;
+               }
+       }
+
+       if (is_restored) {
+               _alarm_disable_timer(alarm_context);
+               _clear_scheduled_alarm_list();
+               _alarm_schedule();
+               _rtc_set();
+       }
+
+       return ALARMMGR_RESULT_SUCCESS;
+}
+
 static int __on_app_uninstalled(uid_t target_uid, int req_id, const char *pkg_type,
                                const char *pkgid, const char *key, const char *val,
                                const void *pmsg, void *user_data)
@@ -1802,44 +1800,55 @@ static int __on_app_uninstalled(uid_t target_uid, int req_id, const char *pkg_ty
        __alarm_info_t *entry = NULL;
        alarm_info_t *alarm_info = NULL;
        bool is_deleted = false;
+       int is_power_saving_mode;
 
        SECURE_LOGD("pkg_type(%s), pkgid(%s), key(%s), value(%s)", pkg_type, pkgid, key, val);
 
-       if (strncmp(key, "end", 3) == 0 && strncmp(val, "ok", 2) == 0) {
-               for (gs_iter = alarm_context.alarms; gs_iter != NULL;) {
-                       entry = (__alarm_info_t *)gs_iter->data;
+       if ((key && strncmp(key, "end", 3) != 0) || (val && strncmp(val, "ok", 2) != 0))
+               return ALARMMGR_RESULT_SUCCESS;
 
-                       const char *caller_pkgid = entry->caller_pkgid;
-                       const char *callee_pkgid = entry->callee_pkgid;
-                       int pid = _get_pid_from_appid(entry->app_unique_name, entry->uid);
+       is_power_saving_mode = GET_POWER_SAVING_MODE();
 
-                       gs_iter = g_slist_next(gs_iter);
-                       if ((caller_pkgid && strncmp(pkgid, caller_pkgid, strlen(pkgid)) == 0) ||
-                                       (callee_pkgid && strncmp(pkgid, callee_pkgid, strlen(pkgid)) == 0)) {
-                               if (_remove_from_scheduled_alarm_list(entry->uid, entry->alarm_id))
-                                       is_deleted = true;
-
-                               alarm_info = &entry->alarm_info;
-                               if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
-                                       if (!_delete_alarms(entry->alarm_id))
-                                               SECURE_LOGE("_delete_alarms() is failed. pkgid[%s], alarm_id[%d]", pkgid, entry->alarm_id);
-                               }
+       for (gs_iter = alarm_context.alarms; gs_iter != NULL;) {
+               entry = (__alarm_info_t *)gs_iter->data;
+
+               const char *caller_pkgid = entry->caller_pkgid;
+               const char *callee_pkgid = entry->callee_pkgid;
+               int pid = _get_pid_from_appid(entry->app_unique_name, entry->uid);
 
+               gs_iter = g_slist_next(gs_iter);
+               if ((caller_pkgid && strncmp(pkgid, caller_pkgid, strlen(pkgid)) == 0) ||
+                       (callee_pkgid && strncmp(pkgid, callee_pkgid, strlen(pkgid)) == 0)) {
+                       if (_remove_from_scheduled_alarm_list(entry->uid, entry->alarm_id))
+                               is_deleted = true;
+
+                       alarm_info = &entry->alarm_info;
+                       if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
+                               if (is_power_saving_mode == 1)
+                                       _update_db_for_disabled_alarm(entry->alarm_id, true);
+                               else
+                                       _delete_alarms(entry->alarm_id);
+                       }
+
+                       if (is_power_saving_mode == 1) {
+                               LOGD("Save pkgid[%s] alarm_id[%d] duetime[%ld]", pkgid, entry->alarm_id, entry->due_time);
+                               g_disabled_alarm_list = g_slist_append(g_disabled_alarm_list, entry);
+                               alarm_context.alarms = g_slist_remove(alarm_context.alarms, entry);
+                       } else {
                                if (g_hash_table_remove(caller_appid_cache_table, GINT_TO_POINTER(pid)) == true)
                                        LOGD("Remove cachd data of pid[%d]", pid);
 
-                               SECURE_LOGD("Remove pkgid[%s], alarm_id[%d]", pkgid, entry->alarm_id);
+                               LOGD("Remove pkgid[%s], alarm_id[%d]", pkgid, entry->alarm_id);
                                alarm_context.alarms = g_slist_remove(alarm_context.alarms, entry);
                                _release_alarm_info_t(entry);
-
                        }
                }
+       }
 
-               if (is_deleted && (g_slist_length(g_scheduled_alarm_list) == 0)) {
-                       _alarm_disable_timer();
-                       _alarm_schedule();
-                       _rtc_set();
-               }
+       if (is_deleted && (g_slist_length(g_scheduled_alarm_list) == 0)) {
+               _alarm_disable_timer();
+               _alarm_schedule();
+               _rtc_set();
        }
 
        return ALARMMGR_RESULT_SUCCESS;
@@ -1985,8 +1994,6 @@ static int __check_modifiable(uid_t uid, pid_t pid, int alarm_id)
 int alarm_manager_alarm_set_rtc_time(GVariant *parameters)
 {
        if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
-               const char *rtc = default_rtc;
-               struct rtc_wkalrm rtc_wkalarm;
                int retval = 0;
                struct tm tm, *alarm_tm = NULL;
                char log_tag[ALARMMGR_LOG_TAG_SIZE] = {0,};
@@ -2013,26 +2020,7 @@ int alarm_manager_alarm_set_rtc_time(GVariant *parameters)
                alarm_tm->tm_min = min;
                alarm_tm->tm_sec = sec;
 
-               /*convert to calendar time representation*/
-               time_t rtc_time = mktime(alarm_tm);
-
-               if (gfd < 0) {
-                       gfd = open(rtc, O_RDWR);
-                       if (gfd < 0) {
-                               LOGE("RTC open failed.");
-                               return ERR_ALARM_SYSTEM_FAIL;
-                       }
-               }
-
-               rtc_wkalarm.enabled = 1;
-               rtc_wkalarm.time.tm_year = year;
-               rtc_wkalarm.time.tm_mon = mon;
-               rtc_wkalarm.time.tm_mday = day;
-               rtc_wkalarm.time.tm_hour = hour;
-               rtc_wkalarm.time.tm_min = min;
-               rtc_wkalarm.time.tm_sec = sec;
-
-               retval = ioctl(gfd, RTC_WKALM_SET, &rtc_wkalarm);
+               retval = SET_WAKEUP_RTC_WITH_DATETIME(alarm_tm);
                if (retval == -1) {
                        if (errno == ENOTTY)
                                LOGE("Alarm IRQs is not supported.");
@@ -2045,6 +2033,9 @@ int alarm_manager_alarm_set_rtc_time(GVariant *parameters)
                        strncpy(log_tag, "SET RTC", sizeof(log_tag) - 1);
                }
 
+               /*convert to calendar time representation*/
+               time_t rtc_time = mktime(alarm_tm);
+
                snprintf(log_message, sizeof(log_message), "wakeup rtc time: %ld, %s", rtc_time, ctime(&rtc_time));
                _save_module_log(log_tag, log_message);
                return return_code;
@@ -2213,6 +2204,7 @@ int alarm_manager_alarm_set_timezone(GVariant* parameters)
        }
 
        tzset();
+       DO_AFTER_TIMEZONE_SET();
 
        /* Rescheduling alarms */
        _alarm_disable_timer();
@@ -3022,6 +3014,10 @@ static void __initialize_noti()
 
        /* If the caller or callee app is uninstalled, all registered alarms will be canceled. */
        pkgmgr_client *pc = pkgmgr_client_new(PC_LISTENING);
+
+       pkgmgr_client_set_status_type(pc, PKGMGR_CLIENT_STATUS_INSTALL);
+       pkgmgr_client_listen_status(pc, __on_app_installed, NULL);
+
        pkgmgr_client_set_status_type(pc, PKGMGR_CLIENT_STATUS_UNINSTALL);
        pkgmgr_client_listen_status(pc, __on_app_uninstalled, NULL);
 
@@ -3032,6 +3028,86 @@ static void __initialize_noti()
        pkgmgr_client_listen_app_status(pc, __on_app_disable_cb, NULL);
 }
 
+static int __load_module(const char *path)
+{
+       plugin_handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
+       if (!plugin_handle) {
+               LOGE("Failed to load - %s", dlerror());
+               return -1;
+       }
+
+       if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
+               RTC_INIT = dlsym(plugin_handle, "RTC_INIT");
+               if (!RTC_INIT) {
+                       LOGE("Failed to find PLUGIN_INIT");
+                       goto out;
+               }
+
+               CLEAR_WAKEUP_RTC = dlsym(plugin_handle, "CLEAR_WAKEUP_RTC");
+               if (!CLEAR_WAKEUP_RTC) {
+                       LOGE("Failed to find CLEAR_WAKEUP_RTC");
+                       goto out;
+               }
+
+               SET_WAKEUP_RTC = dlsym(plugin_handle, "SET_WAKEUP_RTC");
+               if (!SET_WAKEUP_RTC) {
+                       LOGE("Failed to find SET_WAKEUP_RTC");
+                       goto out;
+               }
+
+               SET_WAKEUP_RTC_WITH_DATETIME = dlsym(plugin_handle,
+                               "SET_WAKEUP_RTC_WITH_DATETIME");
+               if (!SET_WAKEUP_RTC_WITH_DATETIME) {
+                       LOGE("Failed to find SET_WAKEUP_RTC_WITH_DATETIME");
+                       goto out;
+               }
+
+               SET_SYSTIME_RTC = dlsym(plugin_handle, "SET_SYSTIME_RTC");
+               if (!SET_SYSTIME_RTC) {
+                       LOGE("Failed to find SET_SYSTIME_RTC");
+                       goto out;
+               }
+       }
+
+       DO_AFTER_TIMEZONE_SET = dlsym(plugin_handle, "DO_AFTER_TIMEZONE_SET");
+       if (!DO_AFTER_TIMEZONE_SET) {
+               LOGE("Failed to find DO_AFTER_TIMEZONE_SET");
+               goto out;
+       }
+
+       DO_AFTER_ALARM_CREATE = dlsym(plugin_handle, "DO_AFTER_ALARM_CREATE");
+       if (!DO_AFTER_ALARM_CREATE) {
+               LOGE("Failed to find DO_AFTER_ALARM_CREATE");
+               goto out;
+       }
+
+       DO_AFTER_ALARM_EXPIRE = dlsym(plugin_handle, "DO_AFTER_ALARM_EXPIRE");
+       if (!DO_AFTER_ALARM_EXPIRE) {
+               LOGE("Failed to find DO_AFTER_ALARM_EXPIRE");
+               goto out;
+       }
+
+       PS_MODE_INIT = dlsym(plugin_handle, "PS_MODE_INIT");
+       if (!PS_MODE_INIT) {
+               LOGE("Failed to init PS_MODE_INIT");
+               goto out;
+       }
+
+       GET_POWER_SAVING_MODE = dlsym(plugin_handle, "GET_POWER_SAVING_MODE");
+       if (!GET_POWER_SAVING_MODE) {
+               LOGE("Failed to find IS_POWER_SAVING_MODE");
+               goto out;
+       }
+
+       return 0;
+
+out:
+       if (plugin_handle)
+               dlclose(plugin_handle);
+
+       return -1;
+}
+
 void _alarm_initialize()
 {
 #if !(GLIB_CHECK_VERSION(2, 36, 0))
@@ -3046,7 +3122,7 @@ void _alarm_initialize()
        alarm_context.timer = _initialize_timer();
        if (alarm_context.timer == -1) {
                LOGE("because _initialize_timer failed, "
-                                         "alarm-server cannot be runned.\n");
+                                       "alarm-server cannot be runned.\n");
                exit(1);
        }
 
@@ -3054,7 +3130,7 @@ void _alarm_initialize()
                /* because dbus's initialize
                 * failed, we cannot continue any more. */
                LOGE("because _initialize_dbus failed, "
-                                         "alarm-server cannot be runned.\n");
+                                       "alarm-server cannot be runned.\n");
                exit(1);
        }
 
@@ -3063,8 +3139,26 @@ void _alarm_initialize()
        __initialize_scheduled_alarm_list();
        if (_initialize_db() == false) {
                LOGE("_initialize_db failed, "
-                               "alarm cannot be stored to database.\n");
+                                       "alarm cannot be stored to database.\n");
+       }
+
+       if (__load_module(PATH_LIB_ALARMMGR_PLUGIN) != 0) {
+               LOGE("_load_module failed");
+               exit(1);
+       }
+
+       if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
+               if (RTC_INIT() != 0) {
+                       LOGE("rtc init failed");
+                       exit(1);
+               }
        }
+
+       if (PS_MODE_INIT() != 0) {
+               LOGE("power saving mode init failed");
+               exit(1);
+       }
+
        __initialize_alarm_list();
        __initialize_noti();
 
@@ -3072,6 +3166,11 @@ void _alarm_initialize()
                caller_appid_cache_table = g_hash_table_new_full(g_direct_hash,
                                g_direct_equal, NULL, __free_cached_value);
        }
+
+       if (_APPFW_FEATURE_WAKEUP_USING_RTC) {
+               tzset();
+               DO_AFTER_TIMEZONE_SET();
+       }
 }
 
 void _release_alarm_info_t(__alarm_info_t *entry)
@@ -3091,7 +3190,6 @@ void _release_alarm_info_t(__alarm_info_t *entry)
                free(entry->app_service_name_mod);
        if (entry->dst_service_name)
                free(entry->dst_service_name);
-
        if (entry->dst_service_name_mod)
                free(entry->dst_service_name_mod);
        if (entry->bundle)
diff --git a/server/plugin/CMakeLists.txt b/server/plugin/CMakeLists.txt
new file mode 100644 (file)
index 0000000..58ebbb3
--- /dev/null
@@ -0,0 +1,3 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+ADD_SUBDIRECTORY(platform)
diff --git a/server/plugin/platform/CMakeLists.txt b/server/plugin/platform/CMakeLists.txt
new file mode 100644 (file)
index 0000000..174f82c
--- /dev/null
@@ -0,0 +1,39 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(ALARM_SERVER_PLUGIN "alarm-server-plugin")
+SET(ALARM_SERVER_PLUGIN_DIR ${CMAKE_SOURCE_DIR}/server/plugin/platform)
+
+PROJECT(${ALARM_SERVER_PLUGIN} C)
+AUX_SOURCE_DIRECTORY(${ALARM_SERVER_PLUGIN_DIR}/src ALARM_SERVER_PLUGIN_SOURCES)
+
+INCLUDE_DIRECTORIES(${ALARM_SERVER_PLUGIN_DIR}/include)
+
+INCLUDE(FindPkgConfig)
+
+PKG_CHECK_MODULES(plugin_pkgs REQUIRED
+       dlog
+       glib-2.0
+)
+
+FOREACH(flag ${plugin_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Wall -Werror")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_C_FLAGS_RELEASE "-O2")
+
+ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
+ADD_DEFINITIONS("-DLOG_TAG=\"ALARM_MANAGER\"")
+
+ADD_LIBRARY(${PROJECT_NAME} SHARED ${ALARM_SERVER_PLUGIN_SOURCES})
+#SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${VERSION_MAJOR})
+#SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${VERSION})
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${plugin_pkgs_LDFLAGS})
+
+CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY)
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
+INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
diff --git a/server/plugin/platform/alarm-server-plugin.pc.in b/server/plugin/platform/alarm-server-plugin.pc.in
new file mode 100644 (file)
index 0000000..f4897f9
--- /dev/null
@@ -0,0 +1,11 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=@CMAKE_INSTALL_PREFIX@
+libdir=@LIB_INSTALL_DIR@
+includedir=@INCLUDE_INSTALL_DIR@
+
+Name: alarm-service-plugin
+Description: alarm-servce plugin library
+Version: @FULLVER@
+Requires: glib-2.0 dlog
+Libs: -L${libdir} -l@LIBRARY@
+Cflags: -I${includedir}
diff --git a/server/plugin/platform/include/plugin.h b/server/plugin/platform/include/plugin.h
new file mode 100644 (file)
index 0000000..ce10f34
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "ALARM_MANAGER"
+
+#define EXPORT __attribute__ ((visibility("default")))
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/server/plugin/platform/src/alarm-server-plugin.c b/server/plugin/platform/src/alarm-server-plugin.c
new file mode 100644 (file)
index 0000000..8b78767
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * 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 <stdbool.h>
+#include <fcntl.h>
+#include <time.h>
+#include <errno.h>
+#include <dlog.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <linux/rtc.h>
+
+#include "plugin.h"
+
+static const char default_rtc[] = "/dev/rtc";
+int gfd = -1;
+int is_psm = -1;
+
+EXPORT int CLEAR_WAKEUP_RTC(void)
+{
+       struct rtc_wkalrm rtc_wkalarm;
+
+       rtc_wkalarm.enabled = 0;
+       rtc_wkalarm.time.tm_year = 1900;
+       rtc_wkalarm.time.tm_mon = 0;
+       rtc_wkalarm.time.tm_mday = 1;
+       rtc_wkalarm.time.tm_hour = 0;
+       rtc_wkalarm.time.tm_min = 0;
+       rtc_wkalarm.time.tm_sec = 0;
+
+       return ioctl(gfd, RTC_WKALM_SET, &rtc_wkalarm);
+}
+
+EXPORT int SET_WAKEUP_RTC(time_t due_time)
+{
+       struct rtc_wkalrm rtc_wkalarm;
+       struct tm due_tm;
+
+       gmtime_r(&due_time, &due_tm);
+       LOGD("Setted RTC Alarm date/time is %d-%d-%d, %02d:%02d:%02d (UTC).",
+               due_tm.tm_mday, due_tm.tm_mon + 1, due_tm.tm_year + 1900,
+               due_tm.tm_hour, due_tm.tm_min, due_tm.tm_sec);
+
+       rtc_wkalarm.enabled = 1;
+       rtc_wkalarm.time.tm_year = due_tm.tm_year;
+       rtc_wkalarm.time.tm_mon = due_tm.tm_mon;
+       rtc_wkalarm.time.tm_mday = due_tm.tm_mday;
+       rtc_wkalarm.time.tm_hour = due_tm.tm_hour;
+       rtc_wkalarm.time.tm_min = due_tm.tm_min;
+       rtc_wkalarm.time.tm_sec = due_tm.tm_sec;
+
+       return ioctl(gfd, RTC_WKALM_SET, &rtc_wkalarm);
+}
+
+EXPORT int SET_WAKEUP_RTC_WITH_DATETIME(struct tm *tm)
+{
+       struct rtc_wkalrm rtc_wkalarm;
+
+       rtc_wkalarm.enabled = 1;
+       rtc_wkalarm.time.tm_year = tm->tm_year;
+       rtc_wkalarm.time.tm_mon = tm->tm_mon;
+       rtc_wkalarm.time.tm_mday = tm->tm_mday;
+       rtc_wkalarm.time.tm_hour = tm->tm_hour;
+       rtc_wkalarm.time.tm_min = tm->tm_min;
+       rtc_wkalarm.time.tm_sec = tm->tm_sec;
+
+       return ioctl(gfd, RTC_WKALM_SET, &rtc_wkalarm);
+}
+
+EXPORT int SET_SYSTIME_RTC(time_t new_time)
+{
+       struct tm tm, *gmtime_res;
+       struct rtc_time _rtc_time;
+
+       gmtime_res = gmtime_r(&(new_time), &tm);
+       if (!gmtime_res) {
+               LOGE("gmtime_r is failed. [%d]", errno);
+               return -1;
+       }
+
+       _rtc_time.tm_sec = tm.tm_sec;
+       _rtc_time.tm_min = tm.tm_min;
+       _rtc_time.tm_hour = tm.tm_hour;
+       _rtc_time.tm_mday = tm.tm_mday;
+       _rtc_time.tm_mon = tm.tm_mon;
+       _rtc_time.tm_year = tm.tm_year;
+       _rtc_time.tm_wday = tm.tm_wday;
+       _rtc_time.tm_yday = tm.tm_yday;
+       _rtc_time.tm_isdst = tm.tm_isdst;
+
+       return ioctl(gfd, RTC_SET_TIME, &_rtc_time);
+}
+
+EXPORT void DO_AFTER_TIMEZONE_SET(void)
+{
+       return;
+}
+
+EXPORT void DO_AFTER_ALARM_CREATE(int pid, const char *unique_name)
+{
+       return;
+}
+
+EXPORT void DO_AFTER_ALARM_EXPIRE(int pid, const char *unique_name)
+{
+       return;
+}
+
+EXPORT int GET_POWER_SAVING_MODE(void)
+{
+       return is_psm;
+}
+
+EXPORT int RTC_INIT(void)
+{
+       const char *rtc = default_rtc;
+
+       if (gfd < 0) {
+               gfd = open(rtc, O_RDWR);
+               if (gfd < 0) {
+                       LOGE("RTC open failed.");
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+EXPORT int PS_MODE_INIT(void)
+{
+       return 0;
+}