#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"
static struct _cloud_account {
const char *access_token;
const char *user_id;
-} cloud_account;
+} *cloud_account;
static struct _update_info {
char *new_version;
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;
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)
}
#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");
update_info->package_size = 0;
update_info->updatable = 0;
- register_account_info_message_port();
- request_cloud_accout();
+ get_cloud_accout();
init_update_alarm();
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);
}
_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.");