sensord: fix the problem that attribute is not restored properly 86/89186/1
authorkibak.yoon <kibak.yoon@samsung.com>
Wed, 31 Aug 2016 02:45:10 +0000 (11:45 +0900)
committerkibak.yoon <kibak.yoon@samsung.com>
Thu, 22 Sep 2016 13:08:20 +0000 (22:08 +0900)
if command including "0", e.g., command is "-63, 23, 50, 0, 0, 0, 0", is
sent to sensord, just "-63, 23, 50" command is stored to cache.
because std::string recognize 4th character as NULL termination.
so value and value_len should be stored to cache.

Change-Id: Ib04afa1f9824a679b546ce9c284b91d389172a82
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.cpp
src/client/sensor_handle_info.h

index 171cb88..b01b607 100644 (file)
@@ -117,6 +117,8 @@ void clean_up(void)
                sensord_disconnect(*it_handle);
                ++it_handle;
        }
+
+       sensor_event_listener::get_instance().clear();
 }
 
 static int get_power_save_state(void)
@@ -191,8 +193,8 @@ bool restore_attributes(int client_id, sensor_id_t sensor, command_channel *cmd_
 
                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();
+                       const char *value = it->second->get();
+                       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());
@@ -1169,7 +1171,7 @@ 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);
+       sensor_client_info::get_instance().set_attribute(handle, attribute, value, value_len);
 
        return OP_SUCCESS;
 }
index 54a6d7e..d027d8a 100644 (file)
@@ -682,7 +682,7 @@ bool sensor_client_info::set_attribute(int handle, int attribute, int value)
        return true;
 }
 
-bool sensor_client_info::set_attribute(int handle, int attribute, std::string value)
+bool sensor_client_info::set_attribute(int handle, int attribute, const char *value, int value_len)
 {
        AUTOLOCK(m_handle_info_lock);
 
@@ -693,13 +693,29 @@ bool sensor_client_info::set_attribute(int handle, int attribute, std::string va
                return false;
        }
 
-       it_handle->second.attributes_str[attribute] = value;
+       auto it_attribute = it_handle->second.attributes_str.find(attribute);
+
+       if (it_attribute != it_handle->second.attributes_str.end()) {
+               it_attribute->second->set(value, value_len);
+               return true;
+       }
+
+       attribute_info *info = new(std::nothrow) attribute_info();
+       retvm_if(!info, false, "Failed to allocate memory");
+
+       info->set(value, value_len);
+       it_handle->second.attributes_str[attribute] = info;
 
        return true;
 }
 
 void sensor_client_info::clear(void)
 {
+       auto it_handle = m_sensor_handle_infos.begin();
+
+       while (it_handle != m_sensor_handle_infos.end())
+               it_handle->second.clear();
+
        close_command_channel();
        m_sensor_handle_infos.clear();
        m_command_channels.clear();
index ab3a384..905d682 100644 (file)
@@ -66,7 +66,7 @@ public:
        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_attribute(int handle, int attribute, const char *value, int value_len);
 
        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);
index f1e9c65..9f2d562 100644 (file)
@@ -25,6 +25,45 @@ using std::pair;
 
 unsigned long long sensor_handle_info::m_event_id = 0;
 
+attribute_info::attribute_info()
+: m_attr(NULL)
+, m_len(0)
+{
+}
+
+attribute_info::~attribute_info()
+{
+       if (m_attr) {
+               delete m_attr;
+               m_attr = NULL;
+       }
+       m_len = 0;
+}
+
+bool attribute_info::set(const char *value, unsigned int len)
+{
+       if (m_attr)
+               delete m_attr;
+
+       m_attr = new(std::nothrow) char[len];
+       retvm_if(!m_attr, false, "Failed to allocate memory");
+
+       memcpy(m_attr, value, len);
+       m_len = len;
+
+       return true;
+}
+
+char *attribute_info::get(void)
+{
+       return m_attr;
+}
+
+unsigned int attribute_info::size(void)
+{
+       return m_len;
+}
+
 sensor_handle_info::sensor_handle_info()
 : m_handle(0)
 , m_sensor_id(UNKNOWN_SENSOR)
@@ -101,6 +140,17 @@ bool sensor_handle_info::delete_reg_event_info(unsigned int event_type)
        return true;
 }
 
+void sensor_handle_info::clear(void)
+{
+       sensor_attribute_str_map::iterator it_attribute;
+
+       for (it_attribute = attributes_str.begin(); it_attribute != attributes_str.end(); ++it_attribute)
+               delete it_attribute->second;
+
+       attributes_int.clear();
+       attributes_str.clear();
+}
+
 void sensor_handle_info::clear_all_events(void)
 {
        m_reg_event_infos.clear();
index b62f646..70cf5c9 100644 (file)
 #include <string>
 #include <map>
 
+class attribute_info {
+public:
+       attribute_info();
+       ~attribute_info();
+
+       char *get(void);
+       bool set(const char *value, unsigned int len);
+
+       unsigned int size(void);
+
+private:
+       char *m_attr;
+       unsigned int m_len;
+};
+
 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;
+typedef std::map<int, attribute_info *> sensor_attribute_str_map;
 
 class sensor_handle_info {
 public:
@@ -47,6 +62,7 @@ public:
        void get_batch(unsigned int &interval, unsigned int &latency);
        unsigned int get_reg_event_count(void);
 
+       void clear(void);
        void clear_all_events(void);
        static unsigned long long renew_event_id(void);