Added a dbus property 'TimeUpdated'
[platform/upstream/connman.git] / src / clock.c
old mode 100644 (file)
new mode 100755 (executable)
index 2d3d4c0..40729b2
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
 
 #include "connman.h"
 
-enum time_updates {
-       TIME_UPDATES_UNKNOWN = 0,
-       TIME_UPDATES_MANUAL  = 1,
-       TIME_UPDATES_AUTO    = 2,
-};
-
 enum timezone_updates {
        TIMEZONE_UPDATES_UNKNOWN = 0,
        TIMEZONE_UPDATES_MANUAL  = 1,
@@ -45,7 +39,9 @@ static enum time_updates time_updates_config = TIME_UPDATES_AUTO;
 static enum timezone_updates timezone_updates_config = TIMEZONE_UPDATES_AUTO;
 
 static char *timezone_config = NULL;
-static char **timeservers_config = NULL;
+#if defined TIZEN_EXT
+static bool time_updated = false;
+#endif
 
 static const char *time_updates2string(enum time_updates value)
 {
@@ -95,17 +91,84 @@ static enum timezone_updates string2timezone_updates(const char *value)
         return TIMEZONE_UPDATES_UNKNOWN;
 }
 
+static void clock_properties_load(void)
+{
+       GKeyFile *keyfile;
+       char *str;
+       enum time_updates time_value;
+       enum timezone_updates timezone_value;
+
+       keyfile = __connman_storage_load_global();
+       if (!keyfile)
+               return;
+
+       str = g_key_file_get_string(keyfile, "global", "TimeUpdates", NULL);
+
+       time_value = string2time_updates(str);
+       if (time_value != TIME_UPDATES_UNKNOWN)
+               time_updates_config = time_value;
+
+       g_free(str);
+
+       str = g_key_file_get_string(keyfile, "global", "TimezoneUpdates",
+                       NULL);
+
+       timezone_value = string2timezone_updates(str);
+       if (timezone_value != TIMEZONE_UPDATES_UNKNOWN)
+               timezone_updates_config = timezone_value;
+
+       g_free(str);
+
+       g_key_file_free(keyfile);
+}
+
+static void clock_properties_save(void)
+{
+       GKeyFile *keyfile;
+       const char *str;
+
+       keyfile = __connman_storage_load_global();
+       if (!keyfile)
+               keyfile = g_key_file_new();
+
+       str = time_updates2string(time_updates_config);
+       if (str)
+               g_key_file_set_string(keyfile, "global", "TimeUpdates", str);
+       else
+               g_key_file_remove_key(keyfile, "global", "TimeUpdates", NULL);
+
+       str = timezone_updates2string(timezone_updates_config);
+       if (str)
+               g_key_file_set_string(keyfile, "global", "TimezoneUpdates",
+                               str);
+       else
+               g_key_file_remove_key(keyfile, "global", "TimezoneUpdates",
+                               NULL);
+
+       __connman_storage_save_global(keyfile);
+
+       g_key_file_free(keyfile);
+}
+
+enum time_updates __connman_clock_timeupdates(void)
+{
+       return time_updates_config;
+}
+
 static void append_timeservers(DBusMessageIter *iter, void *user_data)
 {
        int i;
+       char **timeservers = __connman_timeserver_system_get();
 
-       if (timeservers_config == NULL)
+       if (!timeservers)
                return;
 
-       for (i = 0; timeservers_config[i] != NULL; i++) {
+       for (i = 0; timeservers[i]; i++) {
                dbus_message_iter_append_basic(iter,
-                               DBUS_TYPE_STRING, &timeservers_config[i]);
+                               DBUS_TYPE_STRING, &timeservers[i]);
        }
+
+       g_strfreev(timeservers);
 }
 
 static DBusMessage *get_properties(DBusConnection *conn,
@@ -115,17 +178,26 @@ static DBusMessage *get_properties(DBusConnection *conn,
        DBusMessageIter array, dict;
        struct timeval tv;
        const char *str;
+#if defined TIZEN_EXT
+       dbus_bool_t val = time_updated;
+#endif
 
        DBG("conn %p", conn);
 
        reply = dbus_message_new_method_return(msg);
-       if (reply == NULL)
+       if (!reply)
                return NULL;
 
        dbus_message_iter_init_append(reply, &array);
 
        connman_dbus_dict_open(&array, &dict);
 
+#if defined TIZEN_EXT
+       connman_dbus_dict_append_basic(&dict, "TimeUpdated",
+                                               DBUS_TYPE_BOOLEAN,
+                                               &val);
+#endif
+
        if (gettimeofday(&tv, NULL) == 0) {
                dbus_uint64_t val = tv.tv_sec;
 
@@ -134,16 +206,16 @@ static DBusMessage *get_properties(DBusConnection *conn,
        }
 
        str = time_updates2string(time_updates_config);
-       if (str != NULL)
+       if (str)
                connman_dbus_dict_append_basic(&dict, "TimeUpdates",
                                                DBUS_TYPE_STRING, &str);
 
-       if (timezone_config != NULL)
+       if (timezone_config)
                connman_dbus_dict_append_basic(&dict, "Timezone",
                                        DBUS_TYPE_STRING, &timezone_config);
 
        str = timezone_updates2string(timezone_updates_config);
-       if (str != NULL)
+       if (str)
                connman_dbus_dict_append_basic(&dict, "TimezoneUpdates",
                                                DBUS_TYPE_STRING, &str);
 
@@ -164,16 +236,28 @@ static DBusMessage *set_property(DBusConnection *conn,
 
        DBG("conn %p", conn);
 
-       if (dbus_message_iter_init(msg, &iter) == FALSE)
+       if (!dbus_message_iter_init(msg, &iter))
+               return __connman_error_invalid_arguments(msg);
+
+       if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
                return __connman_error_invalid_arguments(msg);
 
        dbus_message_iter_get_basic(&iter, &name);
        dbus_message_iter_next(&iter);
+
+       if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
+               return __connman_error_invalid_arguments(msg);
+
        dbus_message_iter_recurse(&iter, &value);
 
        type = dbus_message_iter_get_arg_type(&value);
 
-       if (g_str_equal(name, "Time") == TRUE) {
+       if (g_str_equal(name, "Time")) {
+#if defined TIZEN_EXT
+               /* Tizen updates time (ntp) by system service */
+
+               return __connman_error_permission_denied(msg);
+#else
                struct timeval tv;
                dbus_uint64_t newval;
 
@@ -190,7 +274,12 @@ static DBusMessage *set_property(DBusConnection *conn,
 
                if (settimeofday(&tv, NULL) < 0)
                        return __connman_error_invalid_arguments(msg);
-       } else if (g_str_equal(name, "TimeUpdates") == TRUE) {
+
+               connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
+                               CONNMAN_CLOCK_INTERFACE, "Time",
+                               DBUS_TYPE_UINT64, &newval);
+#endif
+       } else if (g_str_equal(name, "TimeUpdates")) {
                const char *strval;
                enum time_updates newval;
 
@@ -208,10 +297,24 @@ static DBusMessage *set_property(DBusConnection *conn,
 
                time_updates_config = newval;
 
+               clock_properties_save();
                connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
                                CONNMAN_CLOCK_INTERFACE, "TimeUpdates",
                                DBUS_TYPE_STRING, &strval);
-       } else if (g_str_equal(name, "TimezoneUpdates") == TRUE) {
+       } else if (g_str_equal(name, "Timezone")) {
+               const char *strval;
+
+               if (type != DBUS_TYPE_STRING)
+                       return __connman_error_invalid_arguments(msg);
+
+               if (timezone_updates_config != TIMEZONE_UPDATES_MANUAL)
+                       return __connman_error_permission_denied(msg);
+
+               dbus_message_iter_get_basic(&value, &strval);
+
+               if (__connman_timezone_change(strval) < 0)
+                       return __connman_error_invalid_arguments(msg);
+       } else if (g_str_equal(name, "TimezoneUpdates")) {
                const char *strval;
                enum timezone_updates newval;
 
@@ -229,42 +332,50 @@ static DBusMessage *set_property(DBusConnection *conn,
 
                timezone_updates_config = newval;
 
+               clock_properties_save();
                connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
                                CONNMAN_CLOCK_INTERFACE, "TimezoneUpdates",
                                DBUS_TYPE_STRING, &strval);
-       } else if (g_str_equal(name, "Timeservers") == TRUE) {
+       } else if (g_str_equal(name, "Timeservers")) {
                DBusMessageIter entry;
-               GString *str;
+               char **str = NULL;
+               GSList *list = NULL;
+               int count = 0;
 
                if (type != DBUS_TYPE_ARRAY)
                        return __connman_error_invalid_arguments(msg);
 
-               str = g_string_new(NULL);
-               if (str == NULL)
-                       return __connman_error_invalid_arguments(msg);
-
                dbus_message_iter_recurse(&value, &entry);
 
                while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
                        const char *val;
+                       GSList *new_head;
 
                        dbus_message_iter_get_basic(&entry, &val);
-                       dbus_message_iter_next(&entry);
 
-                       if (str->len > 0)
-                               g_string_append_printf(str, " %s", val);
-                       else
-                               g_string_append(str, val);
+                       new_head = __connman_timeserver_add_list(list, val);
+                       if (list != new_head) {
+                               count++;
+                               list = new_head;
+                       }
+
+                       dbus_message_iter_next(&entry);
                }
 
-               g_strfreev(timeservers_config);
+               if (list) {
+                       str = g_new0(char *, count+1);
 
-               if (str->len > 0)
-                       timeservers_config = g_strsplit_set(str->str, " ", 0);
-               else
-                       timeservers_config = NULL;
+                       while (list) {
+                               count--;
+                               str[count] = list->data;
+                               list = g_slist_delete_link(list, list);
+                       };
+               }
+
+               __connman_timeserver_system_set(str);
 
-               g_string_free(str, TRUE);
+               if (str)
+                       g_strfreev(str);
 
                connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
                                CONNMAN_CLOCK_INTERFACE, "Timeservers",
@@ -275,27 +386,56 @@ static DBusMessage *set_property(DBusConnection *conn,
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
-static GDBusMethodTable clock_methods[] = {
-       { "GetProperties", "",   "a{sv}", get_properties },
-       { "SetProperty",   "sv", "",      set_property   },
+static const GDBusMethodTable clock_methods[] = {
+       { GDBUS_METHOD("GetProperties",
+                       NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
+                       get_properties) },
+       { GDBUS_METHOD("SetProperty",
+                       GDBUS_ARGS({ "name", "s" }, { "value", "v" }), NULL,
+                       set_property)   },
        { },
 };
 
-static GDBusSignalTable clock_signals[] = {
-       { "PropertyChanged", "sv" },
+static const GDBusSignalTable clock_signals[] = {
+       { GDBUS_SIGNAL("PropertyChanged",
+                       GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
        { },
 };
 
 static DBusConnection *connection = NULL;
 
+#if defined TIZEN_EXT
+void __connman_clock_set_time_updated(bool updated)
+{
+       time_updated = updated;
+}
+#endif
+
+void __connman_clock_update_timezone(void)
+{
+       DBG("");
+
+       g_free(timezone_config);
+       timezone_config = __connman_timezone_lookup();
+
+       if (!timezone_config)
+               return;
+
+       connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
+                               CONNMAN_CLOCK_INTERFACE, "Timezone",
+                               DBUS_TYPE_STRING, &timezone_config);
+}
+
 int __connman_clock_init(void)
 {
        DBG("");
 
        connection = connman_dbus_get_connection();
-       if (connection == NULL)
+       if (!connection)
                return -1;
 
+       __connman_timezone_init();
+
        timezone_config = __connman_timezone_lookup();
 
        g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
@@ -303,6 +443,7 @@ int __connman_clock_init(void)
                                                clock_methods, clock_signals,
                                                NULL, NULL, NULL);
 
+       clock_properties_load();
        return 0;
 }
 
@@ -310,7 +451,7 @@ void __connman_clock_cleanup(void)
 {
        DBG("");
 
-       if (connection == NULL)
+       if (!connection)
                return;
 
        g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
@@ -318,6 +459,7 @@ void __connman_clock_cleanup(void)
 
        dbus_connection_unref(connection);
 
+       __connman_timezone_cleanup();
+
        g_free(timezone_config);
-       g_strfreev(timeservers_config);
 }