plugin: Get the cloud account through the key-manager 70/190070/5
authorSunmin Lee <sunm.lee@samsung.com>
Fri, 21 Sep 2018 10:00:27 +0000 (19:00 +0900)
committerSunmin Lee <sunm.lee@samsung.com>
Wed, 17 Oct 2018 00:40:23 +0000 (09:40 +0900)
Now the Smartthings-thing agent offers the account information
through the key-manager.

Change-Id: I4f4e452435f05f41c857c0d3ec1d1f563edb9b0f
Signed-off-by: Sunmin Lee <sunm.lee@samsung.com>
packaging/update-control.spec
src/plugin/CMakeLists.txt
src/plugin/plugin.c

index e34458c21095420f6fbb7ec0673e13fece2361b4..cbcd6ef54b68de8bde0782594e6165a55d7b767b 100644 (file)
@@ -10,14 +10,12 @@ BuildRequires:  cmake
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(capi-appfw-app-common)
-BuildRequires:  pkgconfig(capi-appfw-app-control)
 BuildRequires:  pkgconfig(libcurl)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(gio-2.0)
-BuildRequires:  pkgconfig(message-port)
-BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(alarm-service)
 BuildRequires:  pkgconfig(libtzplatform-config)
+BuildRequires:  pkgconfig(key-manager)
 
 %description
 An Update Control library in Tizen C API
index a9aee30ad2704eab3c21ff719cb7baa9b4892129..dd6ed043b39f487d5dd51031be4ff21c6e35080a 100644 (file)
@@ -15,15 +15,14 @@ SET(SOURCES
 #MESSAGE("INCLUDE dir: ${CMAKE_SOURCE_DIR}/src/plugin")
 
 SET(requires
-       capi-appfw-app-control
        capi-appfw-app-common
        capi-system-info
-       message-port
        dlog
        gio-2.0
        alarm-service
        libcurl
        libtzplatform-config
+       key-manager
 )
 
 INCLUDE(FindPkgConfig)
index b00ea97b76a38996b70ecd5408983d75dd60dc5f..a32276435754b5e47653ee851bab6d76de46d097 100644 (file)
 #include <stdlib.h>
 #include <update_control.h>
 #include <app_common.h>
-#include <app_control.h>
-#include <message_port.h>
 #include <alarm.h>
 #include <system_info.h>
 #include <tzplatform_config.h>
+#include <ckmc/ckmc-manager.h>
 #include "plugin.h"
 #include "http_util.h"
 
 #undef API
 #define API __attribute__ ((visibility("default")))
 
-/* IPC with Smartthings-thing for ST cloud account info */
-#define ST_APP_THING_SERVICE_APP_ID   "com.samsung.tizen.smartthings-thing"
-#define ST_APP_OPERATION_UPDATE_AGENT "http://tizen.org/appcontrol/operation/update_agent"
-#define ST_SVC_KEY_COMMAND            "command"
-#define ST_SVC_CMD_GET_ACCOUNT_INFO   "get_account_info"
-
-#define ST_SVC_EVT_SET_ACCOUNT_INFO                 "evt_set_account_info"
-#define ST_SVC_KEY_ACCESS_TOKEN                     "access_token"
-#define ST_SVC_KEY_USER_ID                          "user_id"
-#define ST_UPDATE_AGENT_APP_MSG_PORT_NAME_SUFFIX    ".update_agent"
+/* Key manager information for ST cloud account info */
+#define ST_APP_THING_SERVICE_APP_ID          "com.samsung.tizen.smartthings-thing"
+#define DM_KEY_MANAGER_ALIAS_ACCESS_TOKEN    "access_token"
+#define DM_KEY_MANAGER_ALIAS_USER_ID         "user_id"
 
 /* Update dbus */
 #define UPDATE_BUS_NAME         "org.tizen.system.update"
@@ -59,7 +52,7 @@
 static struct _cloud_account {
        const char *access_token;
        const char *user_id;
-} cloud_account;
+} *cloud_account;
 
 static struct _update_info {
        char *new_version;
@@ -68,7 +61,6 @@ static struct _update_info {
        int package_size;
 } *update_info;
 
-static int msg_port_id = -1;
 static alarm_id_t reserved_update_alarm_id = -1;
 static bool package_downloaded = false;
 
@@ -80,73 +72,55 @@ static alarm_id_t polling_alarm_id = -1;
 static bool debug_mode = false;
 #endif
 
-static void message_port_cb(int local_port_id,
-               const char *remote_app_id,
-               const char *remote_port,
-               bool trusted_remote_port,
-               bundle *message,
-               void *user_data)
+static int get_keymanager_data(const char *alias, const char *password,
+               char **data)
 {
-       _D("message port callback called");
-
-       char *cmd = NULL;
-       char *access_token = NULL;
-       char *user_id = NULL;
-
-       bundle_get_str(message, ST_SVC_KEY_COMMAND, &cmd);
-
-       if (STR_EQ(cmd, ST_SVC_EVT_SET_ACCOUNT_INFO)) {
-               bundle_get_str(message, ST_SVC_KEY_ACCESS_TOKEN, &access_token);
-               bundle_get_str(message, ST_SVC_KEY_USER_ID, &user_id);
-               _D("Access Token: [%s], User ID: [%s]", access_token, user_id);
-               if (access_token && !STR_EMPTY(access_token))
-                       cloud_account.access_token = strdup(access_token);
-               if (user_id && !STR_EMPTY(user_id))
-                       cloud_account.user_id = strdup(user_id);
-       } else {
-               _D("No matching command");
-       }
-}
-
-static void register_account_info_message_port(void)
-{
-       _D("_register_trusted_message_port() called");
-
-       char *app_id = NULL;
-       char port_name[128];
+       _D("Get alias [%s]", alias);
 
-       app_get_id(&app_id);
-       snprintf(port_name, sizeof(port_name), "%s%s", app_id,
-                       ST_UPDATE_AGENT_APP_MSG_PORT_NAME_SUFFIX);
-       free(app_id);
-
-       msg_port_id = message_port_register_trusted_local_port(port_name, message_port_cb, NULL);
-       if (msg_port_id < 0)
-               _E("Port register error: %d", msg_port_id);
-}
+       int ret = 0;
+       char *full_alias = NULL;
+       ckmc_raw_buffer_s *raw_buf = NULL;
 
-static void request_cloud_accout(void)
-{
-       _D("Request cloud account info");
+       ckmc_alias_new(ST_APP_THING_SERVICE_APP_ID, alias, &full_alias);
+       retvm_if(!full_alias, -1, "Failed to ckmc_alias_new");
 
-       app_control_h app_control = NULL;
-       int ret = 0;
+       ret = ckmc_get_data(full_alias, password, &raw_buf);
+       if (ret != 0) {
+               if (ret != CKMC_ERROR_DB_ALIAS_UNKNOWN)
+                       _E("Failed to ckmc_get_data [%d]", ret);
+               ret = -1;
+               goto out;
+       }
 
-       ret = app_control_create(&app_control);
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               _E("app_control_create failed [%d]", ret);
-               return;
+       *data = strndup((const char *)raw_buf->data, raw_buf->size);
+       if (!*data) {
+               _E("Failed to strndup");
+               ret = -1;
        }
 
-       app_control_set_operation(app_control, ST_APP_OPERATION_UPDATE_AGENT);
-       app_control_set_app_id(app_control, ST_APP_THING_SERVICE_APP_ID);
-       app_control_add_extra_data(app_control, ST_SVC_KEY_COMMAND, ST_SVC_CMD_GET_ACCOUNT_INFO);
+out:
+       if (raw_buf)
+               ckmc_buffer_free(raw_buf);
+       if (full_alias)
+               free(full_alias);
 
-       ret = app_control_send_launch_request(app_control, NULL, NULL);
-       if (ret != APP_CONTROL_ERROR_NONE)
-               _E("app_control_send_launch_request failed [%d]", ret);
+       return ret;
+}
 
-       app_control_destroy(app_control);
+static void get_cloud_accout(void)
+{
+       _D("Get cloud account through the key-manager");
+
+       get_keymanager_data(DM_KEY_MANAGER_ALIAS_ACCESS_TOKEN, NULL,
+                       (char **)&cloud_account->access_token);
+       get_keymanager_data(DM_KEY_MANAGER_ALIAS_USER_ID, NULL,
+                       (char **)&cloud_account->user_id);
+
+       if (!cloud_account->access_token || !cloud_account->user_id)
+               _E("Failed to get cloud account");
+       else
+               _D("Access Token: [%s], User ID: [%s]",
+                               cloud_account->access_token, cloud_account->user_id);
 }
 
 static int reservation_alarm_cb(alarm_id_t alarm_id, void *data)
@@ -486,6 +460,15 @@ API int update_control_initialize(void)
        }
 #endif
 
+       cloud_account = (struct _cloud_account *)malloc(sizeof(*cloud_account));
+       if (!cloud_account) {
+               _E("Failed to malloc");
+               return UPDATE_CONTROL_ERROR_OUT_OF_MEMORY;
+       }
+
+       cloud_account->access_token = NULL;
+       cloud_account->user_id = NULL;
+
        update_info = (struct _update_info *)malloc(sizeof(*update_info));
        if (!update_info) {
                _E("Failed to malloc");
@@ -497,8 +480,7 @@ API int update_control_initialize(void)
        update_info->package_size = 0;
        update_info->updatable = 0;
 
-       register_account_info_message_port();
-       request_cloud_accout();
+       get_cloud_accout();
 
        init_update_alarm();
 
@@ -528,9 +510,6 @@ API int update_control_deinitialize(void)
                free(update_info->package_uri);
        free(update_info);
 
-       if ((ret = message_port_unregister_trusted_local_port(msg_port_id)) < 0)
-               _E("Failed to message_port_unregister_trusted_local_port: %d", ret);
-
        if (reserved_update_alarm_id != -1) {
                if ((ret = alarmmgr_remove_alarm(reserved_update_alarm_id)) < 0)
                        _E("Failed to alarmmgr_remove_alarm: %d", ret);
@@ -585,20 +564,20 @@ API int update_control_check_new_version(void)
        }
        _D("Device info: Device Type [%s], Version [%s]", device_type, version);
 
-       if (!cloud_account.access_token || STR_EMPTY(cloud_account.access_token)
-                       || !cloud_account.user_id || STR_EMPTY(cloud_account.user_id)) {
+       if (!cloud_account->access_token || STR_EMPTY(cloud_account->access_token)
+                       || !cloud_account->user_id || STR_EMPTY(cloud_account->user_id)) {
                _D("The cloud account info is not set so request it. Try again later");
-               request_cloud_accout();
+               get_cloud_accout();
                /* Wait for response from smartthings-thing */
                ret = UPDATE_CONTROL_ERROR_CONNECTION_REFUSED;
                goto exit;
        } else {
                _D("The cloud account is not empty: token = [%s], id = [%s]",
-                               cloud_account.access_token, cloud_account.user_id);
+                               cloud_account->access_token, cloud_account->user_id);
        }
 
-       http_util_check_request_to_fota(cloud_account.user_id,
-                       cloud_account.access_token, device_type, version,
+       http_util_check_request_to_fota(cloud_account->user_id,
+                       cloud_account->access_token, device_type, version,
                        &res_header, &res_body);
        if (!res_body || STR_EMPTY(res_body)) {
                _E("No response for check request.");