sensord: restore attributes after sensor daemon is restarted 01/75601/1
authorkibak.yoon <kibak.yoon@samsung.com>
Wed, 8 Jun 2016 11:18:58 +0000 (20:18 +0900)
committerkibak.yoon <kibak.yoon@samsung.com>
Mon, 20 Jun 2016 13:30:40 +0000 (22:30 +0900)
sensor attributes also has to restored when daemon is restarted,
like listener, interval, batch latency.

Change-Id: Id9bfb37fc9ab9b71caca08639f2106710a65741f
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
src/client/client.cpp
src/client/sensor_client_info.cpp
src/client/sensor_client_info.h
src/client/sensor_handle_info.h

index 769aa9d..e26ba85 100644 (file)
@@ -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;
 }
 
index 08b5a9d..54a6d7e 100644 (file)
@@ -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();
index 7c46323..ab3a384 100644 (file)
 #include <sstream>
 #include <unordered_map>
 #include <vector>
-#include <string>
-#include <queue>
-#include <mutex>
-#include <condition_variable>
 #include <cmutex.h>
 #include <sensor_handle_info.h>
 #include <command_channel.h>
 
-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<unsigned int> handle_vector;
-typedef vector<sensor_id_t> sensor_id_vector;
-typedef unordered_map<int, sensor_handle_info> sensor_handle_info_map;
-typedef unordered_map<sensor_id_t, command_channel *> sensor_command_channel_map;
+typedef std::vector<unsigned int> handle_vector;
+typedef std::vector<sensor_id_t> sensor_id_vector;
+typedef std::unordered_map<int, sensor_handle_info> sensor_handle_info_map;
+typedef std::unordered_map<sensor_id_t, command_channel *> 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);
index c74c3a5..b62f646 100644 (file)
 #include <sensor_log.h>
 #include <string.h>
 #include <unordered_map>
+#include <string>
+#include <map>
 
 typedef std::unordered_map<unsigned int, reg_event_info> event_info_map;
+typedef std::map<int, int> sensor_attribute_int_map;
+typedef std::map<int, std::string> 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;