From a25bb895e45fb31673edb2bb5752da75c5accad6 Mon Sep 17 00:00:00 2001 From: Boram Bae Date: Tue, 21 Jan 2020 14:19:06 +0900 Subject: [PATCH] Update for attribute changed callback and attribute getter * Do not update client attribute cache when calling attribute changed callback * Fix a bug related sensord_attribute_{int/str}_get * Fix build warrning related to type conversion * Notify all client(including self) that following attributes are changed SENSORD_ATTRIBUTE_INTERVAL SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY SENSORD_ATTRIBUTE_PAUSE_POLICY SENSORD_ATTRIBUTE_AXIS_ORIENTATION ALL_CUSTOM_ATTRIBUE_INT ALL_ATTRIBUTE_STR * Refactor some codes * Add more internal tests Change-Id: I0e7fb63cd9a44ce2306299afcb791fbac488afda Signed-off-by: Boram Bae --- src/client/sensor_internal.cpp | 6 +- src/client/sensor_listener.cpp | 108 +++++++++++++-------------- src/sensorctl/testcase/sensor_listener.cpp | 114 ++++++++++++++++++++++++++++- src/server/application_sensor_handler.cpp | 17 ++++- src/server/application_sensor_handler.h | 4 +- src/server/external_sensor_handler.cpp | 25 ++++++- src/server/external_sensor_handler.h | 4 + src/server/fusion_sensor_handler.cpp | 22 +++++- src/server/fusion_sensor_handler.h | 4 + src/server/physical_sensor_handler.cpp | 24 ++++-- src/server/physical_sensor_handler.h | 6 +- src/server/sensor_handler.cpp | 75 ++++++++++++++++--- src/server/sensor_handler.h | 18 ++++- src/server/sensor_listener_proxy.cpp | 82 +++++++++++++++++---- src/server/sensor_listener_proxy.h | 28 ++++--- src/server/server_channel_handler.cpp | 85 ++++++++++++--------- 16 files changed, 465 insertions(+), 157 deletions(-) diff --git a/src/client/sensor_internal.cpp b/src/client/sensor_internal.cpp index 8cf5de1..a34bd97 100644 --- a/src/client/sensor_internal.cpp +++ b/src/client/sensor_internal.cpp @@ -36,7 +36,7 @@ #include "sensor_reader.h" -#define CONVERT_OPTION_PAUSE_POLICY(option) ((option) ^ 0b11) +#define CONVERT_OPTION_TO_PAUSE_POLICY(option) ((option) ^ 0b11) #define MAX_LISTENER 100 using namespace sensor; @@ -575,7 +575,7 @@ API bool sensord_start(int handle, int option) listener = it->second; - pause = CONVERT_OPTION_PAUSE_POLICY(option); + pause = CONVERT_OPTION_TO_PAUSE_POLICY(option); prev_pause = listener->get_pause_policy(); if (listener->set_attribute(SENSORD_ATTRIBUTE_PAUSE_POLICY, pause) < 0) { @@ -678,7 +678,7 @@ API bool sensord_set_option(int handle, int option) listener = it->second; - pause = CONVERT_OPTION_PAUSE_POLICY(option); + pause = CONVERT_OPTION_TO_PAUSE_POLICY(option); if (listener->set_attribute(SENSORD_ATTRIBUTE_PAUSE_POLICY, pause) < 0) { _E("Failed to set option[%d(%d)] to listener", option, pause); diff --git a/src/client/sensor_listener.cpp b/src/client/sensor_listener.cpp index 612c1ad..ead43f6 100644 --- a/src/client/sensor_listener.cpp +++ b/src/client/sensor_listener.cpp @@ -62,17 +62,11 @@ public: if (m_listener->get_attribute_int_changed_handler()) { m_listener->get_attribute_int_changed_handler()->read(ch, msg); } - - cmd_listener_attr_int_t* buf = (cmd_listener_attr_int_t*) msg.body(); - m_listener->update_attribute(buf->attribute, buf->value); } break; case CMD_LISTENER_SET_ATTR_STR: { if (m_listener->get_attribute_str_changed_handler()) { m_listener->get_attribute_str_changed_handler()->read(ch, msg); } - - cmd_listener_attr_str_t* buf = (cmd_listener_attr_str_t*) msg.body(); - m_listener->update_attribute(buf->attribute, buf->value, buf->len); } break; default: _W("Invalid command message"); @@ -200,11 +194,11 @@ void sensor_listener::restore(void) auto interval = m_attributes_int.find(SENSORD_ATTRIBUTE_INTERVAL); if (interval != m_attributes_int.end()) - set_interval( m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL]); + set_interval(m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL]); auto latency = m_attributes_int.find(SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY); if (latency != m_attributes_int.end()) - set_max_batch_latency( m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY]); + set_max_batch_latency(m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY]); _D("Restored listener[%d]", get_id()); } @@ -412,7 +406,7 @@ int sensor_listener::get_interval(void) auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_INTERVAL); retv_if(it == m_attributes_int.end(), -1); - return m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL]; + return m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL]; } int sensor_listener::get_max_batch_latency(void) @@ -420,7 +414,7 @@ int sensor_listener::get_max_batch_latency(void) auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY); retv_if(it == m_attributes_int.end(), -1); - return m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY]; + return m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY]; } int sensor_listener::get_pause_policy(void) @@ -428,7 +422,7 @@ int sensor_listener::get_pause_policy(void) auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_PAUSE_POLICY); retv_if(it == m_attributes_int.end(), -1); - return m_attributes_int[SENSORD_ATTRIBUTE_PAUSE_POLICY]; + return m_attributes_int[SENSORD_ATTRIBUTE_PAUSE_POLICY]; } int sensor_listener::get_passive_mode(void) @@ -436,7 +430,7 @@ int sensor_listener::get_passive_mode(void) auto it = m_attributes_int.find(SENSORD_ATTRIBUTE_PASSIVE_MODE); retv_if(it == m_attributes_int.end(), -1); - return m_attributes_int[SENSORD_ATTRIBUTE_PASSIVE_MODE]; + return m_attributes_int[SENSORD_ATTRIBUTE_PASSIVE_MODE]; } int sensor_listener::set_interval(unsigned int interval) @@ -455,7 +449,7 @@ int sensor_listener::set_interval(unsigned int interval) /* If it is not started, store the value only */ if (!m_started.load()) { - m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL] = _interval; + m_attributes_int[SENSORD_ATTRIBUTE_INTERVAL] = _interval; return OP_SUCCESS; } @@ -468,7 +462,7 @@ int sensor_listener::set_max_batch_latency(unsigned int max_batch_latency) /* If it is not started, store the value only */ if (!m_started.load()) { - m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY] = max_batch_latency; + m_attributes_int[SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY] = max_batch_latency; return OP_SUCCESS; } @@ -516,37 +510,37 @@ int sensor_listener::set_attribute(int attribute, int value) int sensor_listener::get_attribute(int attribute, int* value) { - auto it = m_attributes_int.find(attribute); - if (it == m_attributes_int.end()) { - ipc::message msg; - ipc::message reply; - cmd_listener_attr_int_t buf; + ipc::message msg; + ipc::message reply; + cmd_listener_attr_int_t buf; - buf.listener_id = m_id; - buf.attribute = attribute; + retvm_if(!m_cmd_channel, -EIO, "Failed to connect to server"); - msg.set_type(CMD_LISTENER_GET_ATTR_INT); - msg.enclose((char *)&buf, sizeof(buf)); - m_cmd_channel->send_sync(msg); + buf.listener_id = m_id; + buf.attribute = attribute; + msg.set_type(CMD_LISTENER_GET_ATTR_INT); + msg.enclose((char *)&buf, sizeof(buf)); - m_cmd_channel->read_sync(reply); + m_cmd_channel->send_sync(msg); + m_cmd_channel->read_sync(reply); - if (reply.header()->err < 0) { - return reply.header()->err; - } - if (reply.header()->length && reply.body()) { - update_attribute(attribute, ((cmd_listener_attr_int_t *)reply.body())->value); - } + if (reply.header()->err < 0) { + return reply.header()->err; } - *value = m_attributes_int[attribute]; - return OP_SUCCESS; + if (reply.header()->length && reply.body()) { + *value = ((cmd_listener_attr_int_t *)reply.body())->value; + return OP_SUCCESS; + } + + return OP_ERROR; } void sensor_listener::update_attribute(int attribute, int value) { + AUTOLOCK(lock); m_attributes_int[attribute] = value; - _I("listener[%d]'s attribues(int) size : %d", get_id(), m_attributes_int.size()); + _I("Update_attribute(int) listener[%d] attribute[%d] value[%d] attributes size[%d]", get_id(), attribute, value, m_attributes_int.size()); } int sensor_listener::set_attribute(int attribute, const char *value, int len) @@ -590,42 +584,40 @@ int sensor_listener::set_attribute(int attribute, const char *value, int len) int sensor_listener::get_attribute(int attribute, char **value, int* len) { - auto it = m_attributes_str.find(attribute); - if (it == m_attributes_str.end()) { - ipc::message msg; - ipc::message reply; - cmd_listener_attr_str_t buf; - - buf.listener_id = m_id; - buf.attribute = attribute; + ipc::message msg; + ipc::message reply; + cmd_listener_attr_str_t buf; - msg.set_type(CMD_LISTENER_GET_ATTR_STR); - msg.enclose((char *)&buf, sizeof(buf)); - m_cmd_channel->send_sync(msg); + buf.listener_id = m_id; + buf.attribute = attribute; - m_cmd_channel->read_sync(reply); - if (reply.header()->err < 0) { - return reply.header()->err; - } + msg.set_type(CMD_LISTENER_GET_ATTR_STR); + msg.enclose((char *)&buf, sizeof(buf)); + m_cmd_channel->send_sync(msg); - if (reply.header()->length && reply.body()) { - cmd_listener_attr_str_t * recv_buf = (cmd_listener_attr_str_t *)reply.body(); - update_attribute(attribute, (char *)recv_buf->value, recv_buf->len); - } + m_cmd_channel->read_sync(reply); + if (reply.header()->err < 0) { + return reply.header()->err; } - *len = m_attributes_str[attribute].size(); - *value = (char *) malloc(*len); - std::copy(m_attributes_str[attribute].begin(), m_attributes_str[attribute].end(), *value); + if (reply.header()->length && reply.body()) { + cmd_listener_attr_str_t * recv_buf = (cmd_listener_attr_str_t *)reply.body(); + char* p = (char *)recv_buf->value; + *len = recv_buf->len; + *value = (char *) malloc(*len); + std::copy(p, p + recv_buf->len, *value); + return OP_SUCCESS; + } - return OP_SUCCESS; + return OP_ERROR; } void sensor_listener::update_attribute(int attribute, const char *value, int len) { + AUTOLOCK(lock); m_attributes_str[attribute].clear(); m_attributes_str[attribute].insert(m_attributes_str[attribute].begin(), value, value + len); - _I("listener[%d]'s attribues(str) size : %d", get_id(), m_attributes_str.size()); + _I("Update_attribute(str) listener[%d] attribute[%d] value[%s] attributes size[%zu]", get_id(), attribute, value, m_attributes_int.size()); } int sensor_listener::get_sensor_data(sensor_data_t *data) diff --git a/src/sensorctl/testcase/sensor_listener.cpp b/src/sensorctl/testcase/sensor_listener.cpp index 70050b0..517d0cc 100644 --- a/src/sensorctl/testcase/sensor_listener.cpp +++ b/src/sensorctl/testcase/sensor_listener.cpp @@ -278,6 +278,36 @@ TESTCASE(sensor_listener, set_get_attribute_int_2) return true; } +TESTCASE(sensor_listener, set_get_attribute_int_3) +{ + int err = 0; + bool ret = true; + int handle = 0; + sensor_t sensor = NULL; + int attr = 20; + int value = -1; + + err = sensord_get_default_sensor(ACCELEROMETER_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensor); + + err = sensord_set_attribute_int(handle, attr, 1); + ASSERT_EQ(err, 0); + + for (int i = 0 ; i < 10; i ++) { + err = sensord_get_attribute_int(handle, attr, &value); + ASSERT_EQ(err, 0); + } + + ASSERT_EQ(value, 1); + + ret = sensord_disconnect(handle); + ASSERT_TRUE(ret); + + return true; +} + TESTCASE(sensor_listener, get_attribute_int_1) { int err = 0; @@ -386,6 +416,37 @@ TESTCASE(sensor_listener, set_get_attribute_string_2) return true; } +TESTCASE(sensor_listener, set_get_attribute_string_3) +{ + int err = 0; + bool ret = true; + int handle = 0; + char *value = NULL; + int len = 0; + sensor_t sensor = NULL; + int attr = 1; + + err = sensord_get_default_sensor(ACCELEROMETER_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensor); + err = sensord_set_attribute_str(handle, attr, TEST_STRING, TEST_STRING_LEN); + ASSERT_EQ(err, 0); + + for (int i = 0; i < 10; i++) { + err = sensord_get_attribute_str(handle, attr, &value, &len); + ASSERT_EQ(err, 0); + ASSERT_EQ(len, TEST_STRING_LEN); + ASSERT_EQ(strncmp(value, TEST_STRING, len), 0); + } + + ret = sensord_disconnect(handle); + ASSERT_TRUE(ret); + + free(value); + return true; +} + TESTCASE(sensor_listener, set_get_get_attribute_string_1) { int err; @@ -627,7 +688,7 @@ TESTCASE(skip_sensor_listener, register_attribute_str_changed) ret = sensord_stop(handle); ASSERT_TRUE(ret); - ret = sensord_unregister_attribute_int_changed_cb(handle); + ret = sensord_unregister_attribute_str_changed_cb(handle); ASSERT_TRUE(ret); ret = sensord_disconnect(handle); @@ -687,4 +748,53 @@ TESTCASE(skip_sensor_listener, attribute_str_changer) ASSERT_TRUE(ret); return true; -} \ No newline at end of file +} + +TESTCASE(skip_sensor_listener, light_sensor_set_attribute_int_1) +{ + int err = 0; + bool ret = true; + int handle = 0; + sensor_t sensor = NULL; + int attr = 2; + int value = 0; + + err = sensord_get_default_sensor(LIGHT_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensor); + err = sensord_set_attribute_int(handle, attr, 2); + ASSERT_EQ(err, 0); + + err = sensord_get_attribute_int(handle, attr, &value); + ASSERT_EQ(err, 0); + ASSERT_EQ(value, 2); + + ret = sensord_disconnect(handle); + ASSERT_TRUE(ret); + + return true; +} + +TESTCASE(skip_sensor_listener, light_sensor_get_attribute_int_1) +{ + int err = 0; + bool ret = true; + int handle = 0; + sensor_t sensor = NULL; + int attr = 2; + int value = 0; + + err = sensord_get_default_sensor(LIGHT_SENSOR, &sensor); + ASSERT_EQ(err, 0); + + handle = sensord_connect(sensor); + err = sensord_get_attribute_int(handle, attr, &value); + ASSERT_EQ(err, 0); + ASSERT_EQ(value, 2); + + ret = sensord_disconnect(handle); + ASSERT_TRUE(ret); + + return true; +} diff --git a/src/server/application_sensor_handler.cpp b/src/server/application_sensor_handler.cpp index fe1aac3..2bf1a07 100644 --- a/src/server/application_sensor_handler.cpp +++ b/src/server/application_sensor_handler.cpp @@ -30,7 +30,6 @@ application_sensor_handler::application_sensor_handler(const sensor_info &info, : sensor_handler(info) , m_ch(ch) , m_started(false) -, m_prev_interval(0) { } @@ -121,15 +120,25 @@ int application_sensor_handler::set_interval(sensor_observer *ob, int32_t interv msg.enclose((const char *)&buf, sizeof(cmd_provider_attr_int_t)); m_ch->send_sync(msg); - m_prev_interval = cur_interval; - - _I("Set interval[%d] to sensor[%s]", cur_interval, m_info.get_uri().c_str()); + update_prev_interval(cur_interval); + return OP_SUCCESS; +} +int application_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval) +{ + interval = m_prev_interval; return OP_SUCCESS; } int application_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency) { + update_prev_latency(latency); + return OP_SUCCESS; +} + +int application_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency) +{ + latency = m_prev_latency; return OP_SUCCESS; } diff --git a/src/server/application_sensor_handler.h b/src/server/application_sensor_handler.h index 8560451..5159bd5 100644 --- a/src/server/application_sensor_handler.h +++ b/src/server/application_sensor_handler.h @@ -45,7 +45,9 @@ public: int stop(sensor_observer *ob); int set_interval(sensor_observer *ob, int32_t interval); + int get_interval(sensor_observer *ob, int32_t& interval); int set_batch_latency(sensor_observer *ob, int32_t latency); + int get_batch_latency(sensor_observer *ob, int32_t &latency); int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); int flush(sensor_observer *ob); @@ -54,12 +56,12 @@ public: private: ipc::channel *m_ch; std::atomic m_started; - int32_t m_prev_interval; int get_min_interval(void); std::vector m_required_sensors; std::unordered_map m_interval_map; + }; } diff --git a/src/server/external_sensor_handler.cpp b/src/server/external_sensor_handler.cpp index e0f74bf..8fd29ec 100644 --- a/src/server/external_sensor_handler.cpp +++ b/src/server/external_sensor_handler.cpp @@ -150,15 +150,22 @@ int external_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) retv_if(m_policy <= OP_ERROR, m_policy); } - m_interval_map[ob] = interval; + m_interval_map[ob] = _interval; + int ret = OP_SUCCESS; if (m_policy == OP_DEFAULT && observer_count() > 0) { _interval = get_min_interval(); - return m_sensor->set_interval(ob, _interval); + ret = m_sensor->set_interval(ob, _interval); } - _I("Set interval[%d] to sensor[%s]", _interval, m_info.get_uri().c_str()); + update_prev_interval(_interval); + return ret; +} +int external_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval) +{ + retv_if(!m_sensor, -EINVAL); + interval = m_prev_interval; return OP_SUCCESS; } @@ -192,10 +199,20 @@ int external_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t late m_batch_latency_map[ob] = _latency; + int ret = OP_SUCCESS; if (m_policy == OP_DEFAULT && observer_count() > 0) { _latency = get_min_batch_latency(); - return m_sensor->set_batch_latency(ob, latency); + ret = m_sensor->set_batch_latency(ob, latency); } + + update_prev_latency(_latency); + return ret; +} + +int external_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency) +{ + retv_if(!m_sensor, -EINVAL); + latency = m_prev_latency; return OP_SUCCESS; } diff --git a/src/server/external_sensor_handler.h b/src/server/external_sensor_handler.h index 753a80d..c77a064 100644 --- a/src/server/external_sensor_handler.h +++ b/src/server/external_sensor_handler.h @@ -42,7 +42,11 @@ public: int stop(sensor_observer *ob); int set_interval(sensor_observer *ob, int32_t interval); + int get_interval(sensor_observer *ob, int32_t& interval); + int set_batch_latency(sensor_observer *ob, int32_t latency); + int get_batch_latency(sensor_observer *ob, int32_t &latency); + int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); int flush(sensor_observer *ob); diff --git a/src/server/fusion_sensor_handler.cpp b/src/server/fusion_sensor_handler.cpp index 1ede44c..4a85de3 100644 --- a/src/server/fusion_sensor_handler.cpp +++ b/src/server/fusion_sensor_handler.cpp @@ -139,16 +139,24 @@ int fusion_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) policy = m_sensor->set_interval(ob, _interval); retv_if(policy <= OP_ERROR, policy); - m_interval_map[ob] = interval; + m_interval_map[ob] = _interval; if (policy == OP_DEFAULT) _interval = get_min_interval(); - _I("Set interval[%d] to sensor[%s]", _interval, m_info.get_uri().c_str()); + update_prev_interval(_interval); return set_interval_internal(_interval); } +int fusion_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval) +{ + retv_if(!m_sensor, -EINVAL); + + interval = m_prev_interval; + return OP_SUCCESS; +} + int fusion_sensor_handler::get_min_batch_latency(void) { int batch_latency; @@ -183,9 +191,19 @@ int fusion_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latenc if (policy == OP_DEFAULT) _latency = get_min_batch_latency(); + update_prev_latency(_latency); + return set_batch_latency_internal(_latency); } +int fusion_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency) +{ + retv_if(!m_sensor, -EINVAL); + + latency = m_prev_latency; + return OP_SUCCESS; +} + int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value) { retv_if(!m_sensor, -EINVAL); diff --git a/src/server/fusion_sensor_handler.h b/src/server/fusion_sensor_handler.h index e5d6db3..cdccf1d 100644 --- a/src/server/fusion_sensor_handler.h +++ b/src/server/fusion_sensor_handler.h @@ -59,7 +59,11 @@ public: int stop(sensor_observer *ob); int set_interval(sensor_observer *ob, int32_t interval); + int get_interval(sensor_observer *ob, int32_t& interval); + int set_batch_latency(sensor_observer *ob, int32_t latency); + int get_batch_latency(sensor_observer *ob, int32_t &latency); + int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); int flush(sensor_observer *ob); diff --git a/src/server/physical_sensor_handler.cpp b/src/server/physical_sensor_handler.cpp index da82314..d32efe8 100644 --- a/src/server/physical_sensor_handler.cpp +++ b/src/server/physical_sensor_handler.cpp @@ -33,8 +33,6 @@ physical_sensor_handler::physical_sensor_handler(const sensor_info &info, , m_device(device) , m_sensor(sensor) , m_hal_id(hal_id) -, m_prev_interval(0) -, m_prev_latency(0) { } @@ -179,13 +177,18 @@ int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval) ret = m_device->set_interval(m_hal_id, cur_interval); - m_prev_interval = cur_interval; - - _I("Set interval[%d] to sensor[%s]", cur_interval, m_info.get_uri().c_str()); + update_prev_interval(cur_interval); return (ret ? OP_SUCCESS : OP_ERROR); } +int physical_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval) +{ + retv_if(!m_device, -EINVAL); + interval = m_prev_interval; + return OP_SUCCESS; +} + int physical_sensor_handler::get_min_batch_latency(void) { int batch_latency; @@ -224,13 +227,18 @@ int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t late ret = m_device->set_batch_latency(m_hal_id, cur_latency); - m_prev_latency = cur_latency; - - _I("Set batch latency[%d] to sensor[%s]", cur_latency, m_info.get_uri().c_str()); + update_prev_latency(cur_latency); return (ret ? OP_SUCCESS : OP_ERROR); } +int physical_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency) +{ + retv_if(!m_device, -EINVAL); + latency = m_prev_latency; + return OP_SUCCESS; +} + int physical_sensor_handler::delete_batch_latency(sensor_observer *ob) { bool ret = false; diff --git a/src/server/physical_sensor_handler.h b/src/server/physical_sensor_handler.h index 608f854..0fca0fe 100644 --- a/src/server/physical_sensor_handler.h +++ b/src/server/physical_sensor_handler.h @@ -49,7 +49,11 @@ public: int stop(sensor_observer *ob); int set_interval(sensor_observer *ob, int32_t interval); + int get_interval(sensor_observer *ob, int32_t& interval); + int set_batch_latency(sensor_observer *ob, int32_t latency); + int get_batch_latency(sensor_observer *ob, int32_t &latency); + int delete_batch_latency(sensor_observer *ob); int set_attribute(sensor_observer *ob, int32_t attr, int32_t value); int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len); @@ -63,8 +67,6 @@ private: sensor_device *m_device; physical_sensor *m_sensor; uint32_t m_hal_id; - int32_t m_prev_interval; - int32_t m_prev_latency; std::unordered_map m_interval_map; std::unordered_map m_batch_latency_map; diff --git a/src/server/sensor_handler.cpp b/src/server/sensor_handler.cpp index 33c3e53..d6ddc5f 100644 --- a/src/server/sensor_handler.cpp +++ b/src/server/sensor_handler.cpp @@ -30,6 +30,9 @@ using namespace sensor; sensor_handler::sensor_handler(const sensor_info &info) : m_info(info) +, m_prev_interval(0) +, m_prev_latency(0) +, m_need_to_notify_attribute_changed(false) { const char *priv = sensor::utils::get_privilege(m_info.get_uri()); m_info.set_privilege(priv); @@ -125,7 +128,7 @@ int sensor_handler::get_cache(sensor_data_t **data, int *len) return 0; } -bool sensor_handler::notify_attribute_changed(uint32_t id, int attribute, int value) +bool sensor_handler::notify_attribute_changed(uint32_t id, int32_t attribute, int32_t value) { if (observer_count() == 0) return OP_ERROR; @@ -153,7 +156,7 @@ bool sensor_handler::notify_attribute_changed(uint32_t id, int attribute, int va return OP_SUCCESS; } -bool sensor_handler::notify_attribute_changed(uint32_t id, int attribute, const char *value, int len) +bool sensor_handler::notify_attribute_changed(uint32_t id, int32_t attribute, const char *value, int len) { if (observer_count() == 0) return OP_ERROR; @@ -179,7 +182,7 @@ bool sensor_handler::notify_attribute_changed(uint32_t id, int attribute, const sensor_listener_proxy *proxy = NULL; for (auto it = m_observers.begin(); it != m_observers.end(); ++it) { proxy = dynamic_cast(*it); - if (proxy && proxy->get_id() != id) { + if (proxy) { proxy->on_attribute_changed(msg); } } @@ -205,8 +208,18 @@ int sensor_handler::get_attribute(int32_t attr, int32_t* value) void sensor_handler::update_attribute(int32_t attr, int32_t value) { - m_attributes_int[attr] = value; - _I("[%s] attributes(int) size : %d", m_info.get_uri().c_str(), m_attributes_int.size()); + auto it = m_attributes_int.find(attr); + if(it != m_attributes_int.end()) { + if (it->second != value) { + set_need_to_notify_attribute_changed(true); + } + } else { + set_need_to_notify_attribute_changed(true); + } + if (need_to_notify_attribute_changed()) { + m_attributes_int[attr] = value; + _I("[%s] attributes(int) attr[%d] value[%d] attributes size[%zu]", m_info.get_uri().c_str(), attr, value, m_attributes_int.size()); + } } int sensor_handler::get_attribute(int32_t attr, char **value, int *len) @@ -223,8 +236,52 @@ int sensor_handler::get_attribute(int32_t attr, char **value, int *len) void sensor_handler::update_attribute(int32_t attr, const char *value, int len) { - m_attributes_str[attr].clear(); - m_attributes_str[attr].insert(m_attributes_str[attr].begin(), value, value + len); - _I("[%s] attributes(int) size : %d", m_info.get_uri().c_str(), m_attributes_int.size()); + auto it = m_attributes_str.find(attr); + if(it != m_attributes_str.end()) { + if (it->second.size() != (size_t)len) { + set_need_to_notify_attribute_changed(true); + } else { + for(int i = 0 ; i < len; i++) { + if (value[i] != it->second[i]) { + set_need_to_notify_attribute_changed(true); + break; + } + } + } + } else { + set_need_to_notify_attribute_changed(true); + } + if (need_to_notify_attribute_changed()) { + m_attributes_str[attr].clear(); + m_attributes_str[attr].insert(m_attributes_str[attr].begin(), value, value + len); + _I("[%s] attributes(int) attr[%d] value[%s] attributes size[%zu]", m_info.get_uri().c_str(), attr, value, m_attributes_str.size()); + } +} -} \ No newline at end of file +bool sensor_handler::need_to_notify_attribute_changed() +{ + return m_need_to_notify_attribute_changed; +} + +void sensor_handler::set_need_to_notify_attribute_changed(bool value) +{ + m_need_to_notify_attribute_changed = value; +} + +void sensor_handler::update_prev_interval(int32_t interval) +{ + if (m_prev_interval != interval) { + m_prev_interval = interval; + set_need_to_notify_attribute_changed(true); + _I("Set interval[%d] to sensor[%s]", m_prev_interval, m_info.get_uri().c_str()); + } +} + +void sensor_handler::update_prev_latency(int32_t latency) +{ + if (m_prev_latency != latency) { + m_prev_latency = latency; + set_need_to_notify_attribute_changed(true); + _I("Set interval[%d] to sensor[%s]", m_prev_latency, m_info.get_uri().c_str()); + } +} diff --git a/src/server/sensor_handler.h b/src/server/sensor_handler.h index e7b3168..34a603c 100644 --- a/src/server/sensor_handler.h +++ b/src/server/sensor_handler.h @@ -48,7 +48,11 @@ public: virtual int stop(sensor_observer *ob) = 0; virtual int set_interval(sensor_observer *ob, int32_t interval) = 0; + virtual int get_interval(sensor_observer *ob, int32_t &interval) = 0; + virtual int set_batch_latency(sensor_observer *ob, int32_t latency) = 0; + virtual int get_batch_latency(sensor_observer *ob, int32_t &latency) = 0; + virtual int delete_batch_latency(sensor_observer *ob); virtual int set_attribute(sensor_observer *ob, int32_t attr, int32_t value) = 0; virtual int get_attribute(int32_t attr, int32_t* value); @@ -61,14 +65,22 @@ public: void set_cache(sensor_data_t *data, int size); int get_cache(sensor_data_t **data, int *len); - bool notify_attribute_changed(uint32_t id, int attribute, int value); - bool notify_attribute_changed(uint32_t id, int attribute, const char *value, int len); - + bool notify_attribute_changed(uint32_t id, int32_t attribute, int32_t value); + bool notify_attribute_changed(uint32_t id, int32_t attribute, const char *value, int len); + bool need_to_notify_attribute_changed(); + void set_need_to_notify_attribute_changed(bool value); protected: + void update_prev_interval(int32_t interval); + void update_prev_latency(int32_t latency); + sensor_info m_info; + int32_t m_prev_interval; + int32_t m_prev_latency; + std::map m_attributes_int; std::map> m_attributes_str; + bool m_need_to_notify_attribute_changed; private: std::list m_observers; diff --git a/src/server/sensor_listener_proxy.cpp b/src/server/sensor_listener_proxy.cpp index 0862774..d47f362 100644 --- a/src/server/sensor_listener_proxy.cpp +++ b/src/server/sensor_listener_proxy.cpp @@ -41,6 +41,7 @@ sensor_listener_proxy::sensor_listener_proxy(uint32_t id, , m_pause_policy(SENSORD_PAUSE_ALL) , m_axis_orientation(SENSORD_AXIS_DISPLAY_ORIENTED) , m_last_accuracy(SENSOR_ACCURACY_UNDEFINED) +, m_need_to_notify_attribute_changed(false) { sensor_policy_monitor::get_instance().add_listener(this); } @@ -151,24 +152,49 @@ int sensor_listener_proxy::stop(bool policy) return OP_SUCCESS; } -int sensor_listener_proxy::set_interval(unsigned int interval) +int sensor_listener_proxy::set_interval(int32_t interval) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); _D("Listener[%d] try to set interval[%d]", get_id(), interval); - return sensor->set_interval(this, interval); + int ret = sensor->set_interval(this, interval); + apply_sensor_handler_need_to_notify_attribute_changed(sensor); + + return ret; +} + +int sensor_listener_proxy::get_interval(int32_t& interval) +{ + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + + _D("Listener[%d] try to get interval[%d]", get_id()); + + return sensor->get_interval(this, interval); } -int sensor_listener_proxy::set_max_batch_latency(unsigned int max_batch_latency) +int sensor_listener_proxy::set_max_batch_latency(int32_t max_batch_latency) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); _D("Listener[%d] try to set max batch latency[%d]", get_id(), max_batch_latency); + int ret = sensor->set_batch_latency(this, max_batch_latency); + apply_sensor_handler_need_to_notify_attribute_changed(sensor); - return sensor->set_batch_latency(this, max_batch_latency); + return ret; +} + +int sensor_listener_proxy::get_max_batch_latency(int32_t& max_batch_latency) +{ + sensor_handler *sensor = m_manager->get_sensor(m_uri); + retv_if(!sensor, -EINVAL); + + _D("Listener[%d] try to get max batch latency[%d]", get_id()); + + return sensor->get_batch_latency(this, max_batch_latency); } int sensor_listener_proxy::delete_batch_latency(void) @@ -188,7 +214,7 @@ int sensor_listener_proxy::set_passive_mode(bool passive) return OP_SUCCESS; } -int sensor_listener_proxy::set_attribute(int attribute, int value) +int sensor_listener_proxy::set_attribute(int32_t attribute, int32_t value) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); @@ -196,19 +222,28 @@ int sensor_listener_proxy::set_attribute(int attribute, int value) _D("Listener[%d] try to set attribute[%d, %d]", get_id(), attribute, value); if (attribute == SENSORD_ATTRIBUTE_PAUSE_POLICY) { - m_pause_policy = value; + if (m_pause_policy != value) { + m_pause_policy = value; + set_need_to_notify_attribute_changed(true); + } return OP_SUCCESS; } else if (attribute == SENSORD_ATTRIBUTE_AXIS_ORIENTATION) { - m_axis_orientation = value; + if (m_axis_orientation != value) { + m_axis_orientation = value; + set_need_to_notify_attribute_changed(true); + } return OP_SUCCESS; } else if (attribute == SENSORD_ATTRIBUTE_FLUSH) { return flush(); } - return sensor->set_attribute(this, attribute, value); + int ret = sensor->set_attribute(this, attribute, value); + apply_sensor_handler_need_to_notify_attribute_changed(sensor); + + return ret; } -int sensor_listener_proxy::get_attribute(int attribute, int *value) +int sensor_listener_proxy::get_attribute(int32_t attribute, int32_t *value) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); @@ -228,17 +263,20 @@ int sensor_listener_proxy::get_attribute(int attribute, int *value) return sensor->get_attribute(attribute, value); } -int sensor_listener_proxy::set_attribute(int attribute, const char *value, int len) +int sensor_listener_proxy::set_attribute(int32_t attribute, const char *value, int len) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); _D("Listener[%d] try to set string attribute[%d], len[%d]", get_id(), attribute, len); - return sensor->set_attribute(this, attribute, value, len); + int ret = sensor->set_attribute(this, attribute, value, len); + apply_sensor_handler_need_to_notify_attribute_changed(sensor); + + return ret; } -int sensor_listener_proxy::get_attribute(int attribute, char **value, int *len) +int sensor_listener_proxy::get_attribute(int32_t attribute, char **value, int *len) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); @@ -288,7 +326,7 @@ void sensor_listener_proxy::on_policy_changed(int policy, int value) start(true); } -bool sensor_listener_proxy::notify_attribute_changed(int attribute, int value) +bool sensor_listener_proxy::notify_attribute_changed(int32_t attribute, int32_t value) { sensor_handler *sensor = m_manager->get_sensor(m_uri); retv_if(!sensor, -EINVAL); @@ -302,4 +340,20 @@ bool sensor_listener_proxy::notify_attribute_changed(int attribute, const char * retv_if(!sensor, -EINVAL); return sensor->notify_attribute_changed(m_id, attribute, value, len); -} \ No newline at end of file +} + +bool sensor_listener_proxy::need_to_notify_attribute_changed() +{ + return m_need_to_notify_attribute_changed; +} + +void sensor_listener_proxy::set_need_to_notify_attribute_changed(bool value) +{ + m_need_to_notify_attribute_changed = value; +} + +void sensor_listener_proxy::apply_sensor_handler_need_to_notify_attribute_changed(sensor_handler *handler) +{ + set_need_to_notify_attribute_changed(handler->need_to_notify_attribute_changed()); + handler->set_need_to_notify_attribute_changed(false); +} diff --git a/src/server/sensor_listener_proxy.h b/src/server/sensor_listener_proxy.h index 084ffe2..526e543 100644 --- a/src/server/sensor_listener_proxy.h +++ b/src/server/sensor_listener_proxy.h @@ -44,26 +44,31 @@ public: int start(bool policy = false); int stop(bool policy = false); - int set_interval(unsigned int interval); - int set_max_batch_latency(unsigned int max_batch_latency); + int set_interval(int32_t interval); + int get_interval(int32_t& interval); + int set_max_batch_latency(int32_t max_batch_latency); + int get_max_batch_latency(int32_t& max_batch_latency); int delete_batch_latency(void); int set_passive_mode(bool passive); - int set_attribute(int attribute, int value); - int get_attribute(int attribute, int *value); - int set_attribute(int attribute, const char *value, int len); - int get_attribute(int attribute, char **value, int *len); + int set_attribute(int32_t attribute, int32_t value); + int get_attribute(int32_t attribute, int32_t *value); + int set_attribute(int32_t attribute, const char *value, int len); + int get_attribute(int32_t attribute, char **value, int *len); int flush(void); int get_data(sensor_data_t **data, int *len); std::string get_required_privileges(void); /* sensor_policy_listener interface */ void on_policy_changed(int policy, int value); - bool notify_attribute_changed(int attribute, int value); - bool notify_attribute_changed(int attribute, const char *value, int len); + bool notify_attribute_changed(int32_t attribute, int32_t value); + bool notify_attribute_changed(int32_t attribute, const char *value, int len); + bool need_to_notify_attribute_changed(); + void set_need_to_notify_attribute_changed(bool value); private: void update_event(std::shared_ptr msg); void update_accuracy(std::shared_ptr msg); + void apply_sensor_handler_need_to_notify_attribute_changed(sensor_handler* handler); uint32_t m_id; std::string m_uri; @@ -73,9 +78,10 @@ private: bool m_started; bool m_passive; - int m_pause_policy; - int m_axis_orientation; - int m_last_accuracy; + int32_t m_pause_policy; + int32_t m_axis_orientation; + int32_t m_last_accuracy; + bool m_need_to_notify_attribute_changed; }; } diff --git a/src/server/server_channel_handler.cpp b/src/server/server_channel_handler.cpp index 0058c17..1be2686 100644 --- a/src/server/server_channel_handler.cpp +++ b/src/server/server_channel_handler.cpp @@ -235,9 +235,6 @@ int server_channel_handler::listener_set_attr_int(channel *ch, message &msg) case SENSORD_ATTRIBUTE_AXIS_ORIENTATION: default: ret = m_listeners[id]->set_attribute(buf.attribute, buf.value); - if (ret == OP_SUCCESS) { - need_notify = true; - } } /* TODO : check return value */ if (ret < 0) @@ -245,8 +242,9 @@ int server_channel_handler::listener_set_attr_int(channel *ch, message &msg) ret = send_reply(ch, OP_SUCCESS); - if (need_notify) { + if (m_listeners[id]->need_to_notify_attribute_changed()) { m_listeners[id]->notify_attribute_changed(buf.attribute, buf.value); + m_listeners[id]->set_need_to_notify_attribute_changed(false); } return ret; @@ -282,7 +280,11 @@ int server_channel_handler::listener_set_attr_str(channel *ch, message &msg) } ret = send_reply(ch, OP_SUCCESS); - m_listeners[id]->notify_attribute_changed(buf->attribute, buf->value, buf->len); + + if (m_listeners[id]->need_to_notify_attribute_changed()) { + m_listeners[id]->notify_attribute_changed(buf->attribute, buf->value, buf->len); + m_listeners[id]->set_need_to_notify_attribute_changed(false); + } delete [] buf; return ret; @@ -293,7 +295,8 @@ int server_channel_handler::listener_get_attr_int(ipc::channel *ch, ipc::message cmd_listener_attr_int_t buf; msg.disclose((char *)&buf); uint32_t id = buf.listener_id; - + int attr = buf.attribute; + int value = 0; int ret = OP_SUCCESS; auto it = m_listeners.find(id); @@ -302,9 +305,11 @@ int server_channel_handler::listener_get_attr_int(ipc::channel *ch, ipc::message -EACCES, "Permission denied[%d, %s]", id, m_listeners[id]->get_required_privileges().c_str()); - switch (buf.attribute) { + switch (attr) { case SENSORD_ATTRIBUTE_INTERVAL: + ret = m_listeners[id]->get_interval(value); break; case SENSORD_ATTRIBUTE_MAX_BATCH_LATENCY: + ret = m_listeners[id]->get_max_batch_latency(value); break; case SENSORD_ATTRIBUTE_PASSIVE_MODE: // TODO : Are these features required? ret = OP_ERROR; @@ -312,19 +317,26 @@ int server_channel_handler::listener_get_attr_int(ipc::channel *ch, ipc::message case SENSORD_ATTRIBUTE_PAUSE_POLICY: case SENSORD_ATTRIBUTE_AXIS_ORIENTATION: default: - ret = m_listeners[id]->get_attribute(buf.attribute, &buf.value); + ret = m_listeners[id]->get_attribute(attr, &value); } - if (ret == OP_SUCCESS) { - message reply; - reply.enclose((char *)&buf, sizeof(buf)); - reply.set_type(CMD_LISTENER_GET_ATTR_INT); - ret = ch->send_sync(reply); - } else { - ret = send_reply(ch, OP_ERROR); + if (ret != OP_SUCCESS) { + _E("Failed to listener_get_attr_int"); + return ret; } + message reply; + cmd_listener_attr_int_t ret_buf; - return ret; + ret_buf.listener_id = id; + ret_buf.attribute = attr; + ret_buf.value = value; + + reply.enclose((char *)&ret_buf, sizeof(ret_buf)); + reply.header()->err = OP_SUCCESS; + reply.set_type(CMD_LISTENER_GET_ATTR_INT); + ret = ch->send_sync(reply); + + return OP_SUCCESS; } int server_channel_handler::listener_get_attr_str(ipc::channel *ch, ipc::message &msg) @@ -355,28 +367,29 @@ int server_channel_handler::listener_get_attr_str(ipc::channel *ch, ipc::message int len = 0; int ret = m_listeners[id]->get_attribute(attr, &value, &len); - if (ret == OP_SUCCESS) { - cmd_listener_attr_str_t *reply_buf; - size_t size = sizeof(cmd_listener_attr_str_t) + len; - reply_buf = (cmd_listener_attr_str_t *) new(std::nothrow) char[size]; - retvm_if(!reply_buf, -ENOMEM, "Failed to allocate memory"); - - reply_buf->attribute = attr; - memcpy(reply_buf->value, value, len); - reply_buf->len = len; - delete [] value; - - ipc::message reply; - reply.enclose((char *)reply_buf, size); - reply.set_type(CMD_LISTENER_GET_ATTR_STR); - - ret = ch->send_sync(reply); - delete [] reply_buf; - } else { - ret = send_reply(ch, OP_ERROR); + if (ret != OP_SUCCESS) { + _E("Failed to listener_get_attr_str"); + return ret; } - return ret; + cmd_listener_attr_str_t *reply_buf; + size_t size = sizeof(cmd_listener_attr_str_t) + len; + reply_buf = (cmd_listener_attr_str_t *) new(std::nothrow) char[size]; + retvm_if(!reply_buf, -ENOMEM, "Failed to allocate memory"); + + reply_buf->attribute = attr; + memcpy(reply_buf->value, value, len); + reply_buf->len = len; + delete [] value; + + ipc::message reply; + reply.enclose((char *)reply_buf, size); + reply.set_type(CMD_LISTENER_GET_ATTR_STR); + + ret = ch->send_sync(reply); + delete [] reply_buf; + + return OP_SUCCESS; } int server_channel_handler::listener_get_data(channel *ch, message &msg) -- 2.7.4