From da9123180996923a1449f47d5a4965ca4780ffc2 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 8 Jun 2016 20:18:58 +0900 Subject: [PATCH] sensord: restore attributes after sensor daemon is restarted sensor attributes also has to restored when daemon is restarted, like listener, interval, batch latency. Change-Id: Id9bfb37fc9ab9b71caca08639f2106710a65741f Signed-off-by: kibak.yoon --- src/client/client.cpp | 42 +++++++++++++++++++++++++++++++++++++++ src/client/sensor_client_info.cpp | 32 +++++++++++++++++++++++++++++ src/client/sensor_client_info.h | 24 +++++++--------------- src/client/sensor_handle_info.h | 6 ++++++ 4 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index 769aa9d..e26ba85 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -172,6 +172,40 @@ static void power_save_state_cb(keynode_t *node, void *data) } } +bool restore_attributes(int client_id, sensor_id_t sensor, command_channel *cmd_channel) +{ + sensor_handle_info_map handle_infos; + + sensor_client_info::get_instance().get_sensor_handle_info(sensor, handle_infos); + + for (auto it_handles = handle_infos.begin(); it_handles != handle_infos.end(); ++it_handles) { + sensor_handle_info info = it_handles->second; + + for (auto it = info.attributes_int.begin(); it != info.attributes_int.end(); ++it) { + int attribute = it->first; + int value = it->second; + if (!cmd_channel->cmd_set_attribute_int(attribute, value)) { + _E("Failed to send cmd_set_attribute_int(%d, %d) for %s", + client_id, value, get_client_name()); + return false; + } + } + + for (auto it = info.attributes_str.begin(); it != info.attributes_str.end(); ++it) { + int attribute = it->first; + const char *value = it->second.c_str(); + int value_len = it->second.size(); + if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) { + _E("Failed to send cmd_set_attribute_str(%d, %d, %s) for %s", + client_id, value_len, value, get_client_name()); + return false; + } + } + } + + return true; +} + void restore_session(void) { AUTOLOCK(lock); @@ -233,6 +267,10 @@ void restore_session(void) goto FAILED; } + if (!restore_attributes(client_id, *it_sensor, cmd_channel)) { + _E("Failed to restore attributes(%s) for %s", get_sensor_name(*it_sensor), get_client_name()); + goto FAILED; + } ++it_sensor; } @@ -1074,6 +1112,8 @@ static int change_attribute_int(int handle, int attribute, int value) return -EPERM; } + sensor_client_info::get_instance().set_attribute(handle, attribute, value); + return OP_SUCCESS; } @@ -1130,6 +1170,8 @@ API int sensord_set_attribute_str(int handle, int attribute, const char *value, return -EPERM; } + sensor_client_info::get_instance().set_attribute(handle, attribute, value); + return OP_SUCCESS; } diff --git a/src/client/sensor_client_info.cpp b/src/client/sensor_client_info.cpp index 08b5a9d..54a6d7e 100644 --- a/src/client/sensor_client_info.cpp +++ b/src/client/sensor_client_info.cpp @@ -666,6 +666,38 @@ void sensor_client_info::set_pause_policy(sensor_id_t sensor, int pause_policy) } } +bool sensor_client_info::set_attribute(int handle, int attribute, int value) +{ + AUTOLOCK(m_handle_info_lock); + + auto it_handle = m_sensor_handle_infos.find(handle); + + if (it_handle == m_sensor_handle_infos.end()) { + _E("Handle[%d] is not found for client %s", handle, get_client_name()); + return false; + } + + it_handle->second.attributes_int[attribute] = value; + + return true; +} + +bool sensor_client_info::set_attribute(int handle, int attribute, std::string value) +{ + AUTOLOCK(m_handle_info_lock); + + auto it_handle = m_sensor_handle_infos.find(handle); + + if (it_handle == m_sensor_handle_infos.end()) { + _E("Handle[%d] is not found for client %s", handle, get_client_name()); + return false; + } + + it_handle->second.attributes_str[attribute] = value; + + return true; +} + void sensor_client_info::clear(void) { close_command_channel(); diff --git a/src/client/sensor_client_info.h b/src/client/sensor_client_info.h index 7c46323..ab3a384 100644 --- a/src/client/sensor_client_info.h +++ b/src/client/sensor_client_info.h @@ -29,27 +29,14 @@ #include #include #include -#include -#include -#include -#include #include #include #include -using std::unordered_map; -using std::vector; -using std::string; -using std::queue; -using std::mutex; -using std::lock_guard; -using std::unique_lock; -using std::condition_variable; - -typedef vector handle_vector; -typedef vector sensor_id_vector; -typedef unordered_map sensor_handle_info_map; -typedef unordered_map sensor_command_channel_map; +typedef std::vector handle_vector; +typedef std::vector sensor_id_vector; +typedef std::unordered_map sensor_handle_info_map; +typedef std::unordered_map sensor_command_channel_map; typedef struct sensor_rep { bool active; @@ -78,6 +65,9 @@ public: bool get_passive_mode(int handle); bool set_passive_mode(int handle, bool passive); + bool set_attribute(int handle, int attribute, int value); + bool set_attribute(int handle, int attribute, std::string value); + bool set_sensor_pause_policy(int handle, int pause_policy); bool set_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency); bool set_accuracy(int handle, int accuracy); diff --git a/src/client/sensor_handle_info.h b/src/client/sensor_handle_info.h index c74c3a5..b62f646 100644 --- a/src/client/sensor_handle_info.h +++ b/src/client/sensor_handle_info.h @@ -25,8 +25,12 @@ #include #include #include +#include +#include typedef std::unordered_map event_info_map; +typedef std::map sensor_attribute_int_map; +typedef std::map sensor_attribute_str_map; class sensor_handle_info { public: @@ -59,6 +63,8 @@ public: sensor_accuracy_changed_cb_t m_accuracy_cb; void *m_accuracy_user_data; bool m_passive; + sensor_attribute_int_map attributes_int; + sensor_attribute_str_map attributes_str; private: event_info_map m_reg_event_infos; -- 2.7.4