Add sensor handle attribute 92/286892/9 accepted/tizen/unified/20230131.162126
authorTaeminYeom <taemin.yeom@samsung.com>
Mon, 16 Jan 2023 09:00:54 +0000 (18:00 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 27 Jan 2023 08:55:24 +0000 (08:55 +0000)
Sensor handle attribute is diffrent with sensor listener attribute.
Listener attribute is depending on each listener, not sensor.
But handle attribute can affect all listeners connecting the sensor.

When the client sends socket command "CMD_MANAGER_SET_ATTR_INT" or
"CMD_MANAGER_GET_ATTR_INT", the daemon brings and handle it.

Added libsensor API
-sensord_set_attribute_int
-sensord_get_attribute_int

Above two libsensor APIs were used to set/get listener attribute,
but now they are changed to set/get sensor handle attribute.
And sensor listener attribute setter and getter are
"sensord_listener_set/get_attribute_int".
It was patched in
commit 07725926e300 {"listener: change the names of attribute libsensor API"}

Change-Id: I4a62497b65262751275fb1f00c8d4df806d5ec71
Signed-off-by: TaeminYeom <taemin.yeom@samsung.com>
include/sensor_internal.h
src/client/sensor_internal.cpp
src/client/sensor_manager.cpp
src/client/sensor_manager.h
src/server/server_channel_handler.cpp
src/server/server_channel_handler.h
src/shared/command_types.h

index 5eb85dc..d256209 100644 (file)
@@ -445,10 +445,38 @@ int sensord_listener_set_attribute_str(int handle, int attribute, const char *va
  * @retval 0 Successful
  * @retval -EINVAL Invalid parameter
  * @retval -EPERM Operation not permitted
+ * @retval -EIO Input/Output error
  */
 int sensord_listener_get_attribute_str(int handle, int attribute, char **value, int *len);
 
 /**
+ * @brief Set the attribute to a connected sensor
+ *
+ * @param[in] handle a handle represensting a connected sensor.
+ * @param[in] attribute an attribute to change
+ * @param[in] value an attribute value
+ * @return 0 on success, otherwise a negative error value
+ * @retval 0 Successful
+ * @retval -EINVAL Invalid parameter
+ * @retval -EPERM Operation not permitted
+ * @retval -EIO Input/Output error
+ */
+int sensord_set_attribute_int(sensor_t sensor, int attribute, int value);
+
+/**
+ * @brief Get the attribute to a connected sensor
+ *
+ * @param[in] handle a handle represensting a connected sensor.
+ * @param[in] attribute an attribute to get value
+ * @param[out] value an attribute value
+ * @return 0 on success, otherwise a negative error value
+ * @retval 0 Successful
+ * @retval -EINVAL Invalid parameter
+ * @retval -EPERM Operation not permitted
+ */
+int sensord_get_attribute_int(sensor_t sensor, int attribute, int *value);
+
+/**
  * @brief Send data to sensorhub
  *
  * @param[in] handle a handle represensting a connected context sensor.
index 5274f60..f7a7674 100644 (file)
@@ -773,6 +773,44 @@ API int sensord_listener_get_attribute_str(int handle, int attribute, char **val
        return OP_SUCCESS;
 }
 
+API int sensord_set_attribute_int(sensor_t sensor, int attribute, int value)
+{
+       if (!sensor) {
+               _E("Failed to validate the parameter");
+               return -EINVAL;
+       }
+
+       int ret = manager.set_attribute(sensor, attribute, value);
+       if (ret < 0) {
+               _E("Failed to set attribute[%d, %d]", attribute, value);
+               return ret;
+       }
+
+       _D("Set attribute[%s, %d, %d]",
+               ((sensor_info *)(sensor))->get_uri().c_str(), attribute, value);
+
+       return OP_SUCCESS;
+}
+
+API int sensord_get_attribute_int(sensor_t sensor, int attribute, int* value)
+{
+       if (!sensor || !value) {
+               _E("Failed to validate the parameter");
+               return -EINVAL;
+       }
+
+       int ret = manager.get_attribute(sensor, attribute, value);
+       if (ret < 0) {
+               _E("Failed to get attribute[%d]", attribute);
+               return ret;
+       }
+
+       _D("Get attribute[%s, %d, %d]",
+               ((sensor_info *)(sensor))->get_uri().c_str(), attribute, *value);
+
+       return OP_SUCCESS;
+}
+
 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
 {
        sensor::sensor_listener *listener;
index cec6260..f590d5c 100644 (file)
@@ -114,6 +114,102 @@ bool sensor_manager::is_supported(const char *uri)
        return false;
 }
 
+int sensor_manager::set_attribute(sensor_t sensor, int attribute, int value)
+{
+       if (!sensor) {
+               _E("Failed to validate the parameter");
+               return -EINVAL;
+       }
+
+       if (!m_cmd_channel) {
+               _E("Failed to connect to server");
+               return -EIO;
+       }
+
+       ipc::message msg;
+       ipc::message reply;
+       cmd_manager_attr_int_t buf = {0, };
+
+       buf.attribute = attribute;
+       buf.value = value;
+
+       sensor_info *info = (sensor_info*)sensor;
+       memcpy(buf.sensor, info->get_uri().c_str(), info->get_uri().size());
+
+       msg.set_type(CMD_MANAGER_SET_ATTR_INT);
+       msg.enclose((char*)&buf, sizeof(buf));
+
+       bool ret = m_cmd_channel->send_sync(msg);
+       if (!ret) {
+               _E("Failed to send command to set attribute");
+               return -EIO;
+       }
+
+       ret = m_cmd_channel->read_sync(reply);
+       if (!ret) {
+               _E("Failed to read reply to set attribute");
+               return -EIO;
+       }
+
+       if (reply.header()->err < 0) {
+               _E("Failed to set attribute");
+               return reply.header()->err;
+       }
+
+       return OP_SUCCESS;
+}
+
+int sensor_manager::get_attribute(sensor_t sensor, int attribute, int *value)
+{
+       if (!sensor || !value) {
+               _E("Failed to validate the parameters");
+               return -EINVAL;
+       }
+
+       if (!m_cmd_channel) {
+               _E("Failed to connect to server");
+               return -EIO;
+       }
+
+       ipc::message msg;
+       ipc::message reply;
+       cmd_manager_attr_int_t buf = {0, };
+
+       buf.attribute = attribute;
+
+       sensor_info *info = (sensor_info*)sensor;
+       memcpy(buf.sensor, info->get_uri().c_str(), info->get_uri().size());
+
+       msg.set_type(CMD_MANAGER_GET_ATTR_INT);
+       msg.enclose((char*)&buf, sizeof(buf));
+
+       bool ret = m_cmd_channel->send_sync(msg);
+       if (!ret) {
+               _E("Failed to send command to get attribute");
+               return -EIO;
+       }
+
+       ret = m_cmd_channel->read_sync(reply);
+       if (!ret) {
+               _E("Failed to read reply to get attribute");
+               return -EIO;
+       }
+
+       if (reply.header()->err < 0) {
+               _E("Failed to get attribute");
+               return reply.header()->err;
+       }
+
+       if (!reply.header()->length || !reply.body()) {
+               _E("Failed to get attribute");
+               return -EIO;
+       }
+
+       *value = ((cmd_manager_attr_int_t *)reply.body())->value;
+
+       return OP_SUCCESS;
+}
+
 int sensor_manager::add_sensor(sensor_info &info)
 {
        retv_if(is_supported(info.get_uri().c_str()), OP_ERROR);
index c335e0a..4344561 100644 (file)
@@ -47,6 +47,9 @@ public:
        bool is_supported(sensor_t sensor);
        bool is_supported(const char *uri);
 
+       int set_attribute(sensor_t sensor, int attribute, int value);
+       int get_attribute(sensor_t sensor, int attribute, int *value);
+
        /* sensor provider */
        int add_sensor(sensor_info &info);
        int add_sensor(sensor_provider *provider);
index 2231bf9..fd08af1 100644 (file)
@@ -30,6 +30,8 @@
 #include "permission_checker.h"
 #include "application_sensor_handler.h"
 
+#define CONVERT_ATTR_TYPE(attr) ((attr) >> 8)
+
 using namespace sensor;
 using namespace ipc;
 
@@ -90,6 +92,10 @@ void server_channel_handler::read(channel *ch, message &msg)
                err = manager_connect(ch, msg); break;
        case CMD_MANAGER_SENSOR_LIST:
                err = manager_get_sensor_list(ch, msg); break;
+       case CMD_MANAGER_SET_ATTR_INT:
+               err = manager_set_attr_int(ch, msg); break;
+       case CMD_MANAGER_GET_ATTR_INT:
+               err = manager_get_attr_int(ch, msg); break;
        case CMD_LISTENER_CONNECT:
                err = listener_connect(ch, msg); break;
        case CMD_LISTENER_START:
@@ -147,6 +153,87 @@ int server_channel_handler::manager_get_sensor_list(channel *ch, message &msg)
        return OP_SUCCESS;
 }
 
+int server_channel_handler::manager_preprocess_attr(channel *ch, cmd_manager_attr_int_t &buf, sensor_handler **sensor)
+{
+       if (!ch || !sensor)
+               return -EINVAL;
+
+       sensor_handler *find_sensor = m_manager->get_sensor(buf.sensor);
+       if (!find_sensor) {
+               _E("Failed to find sensor");
+               return OP_ERROR;
+       }
+
+       sensor_info info = find_sensor->get_sensor_info();
+       sensor_type_t type = (sensor_type_t) CONVERT_ATTR_TYPE(buf.attribute);
+       if (type != info.get_type()) {
+               _E("Invalid attribute with sensor type");
+               return -EINVAL;
+       }
+
+       std::string privilege = info.get_privilege();
+       int ret = has_privileges(ch->get_fd(), privilege);
+       if (!ret) {
+               _E("Permission denied[%s]", privilege.c_str());
+               return -EPERM;
+       }
+       *sensor = find_sensor;
+
+       return OP_SUCCESS;
+}
+
+int server_channel_handler::manager_set_attr_int(channel *ch, message &msg)
+{
+       cmd_manager_attr_int_t buf;
+       msg.disclose((char *)&buf, sizeof(buf));
+
+       sensor_handler *sensor;
+       int ret = manager_preprocess_attr(ch, buf, &sensor);
+       if (ret < 0)
+               return ret;
+
+       ret = sensor->set_attribute(NULL, buf.attribute, buf.value);
+       if (ret < 0) {
+               _E("Failed to set attribute");
+               return ret;
+       }
+
+       return send_reply(ch, OP_SUCCESS);
+}
+
+int server_channel_handler::manager_get_attr_int(channel *ch, message &msg)
+{
+       cmd_manager_attr_int_t buf;
+       msg.disclose((char *)&buf, sizeof(buf));
+
+       sensor_handler *sensor;
+       int ret = manager_preprocess_attr(ch, buf, &sensor);
+       if (ret < 0)
+               return ret;
+
+       int value;
+       ret = sensor->get_attribute(buf.attribute, &value);
+       if (ret < 0) {
+               _E("Failed to get attribute");
+               return ret;
+       }
+
+       message reply;
+       cmd_manager_attr_int_t ret_buf;
+
+       ret_buf.attribute = buf.attribute;
+       ret_buf.value = value;
+
+       reply.enclose((char *)&ret_buf, sizeof(ret_buf));
+       reply.header()->err = OP_SUCCESS;
+       reply.set_type(CMD_MANAGER_GET_ATTR_INT);
+       ret = ch->send_sync(reply);
+       if (!ret)
+               return OP_ERROR;
+
+       return OP_SUCCESS;
+}
+
 int server_channel_handler::listener_connect(channel *ch, message &msg)
 {
        static uint32_t listener_id = 1;
index fcb1735..fc26a9e 100644 (file)
@@ -28,6 +28,7 @@
 #include "sensor_manager.h"
 #include "sensor_listener_proxy.h"
 #include "application_sensor_handler.h"
+#include "command_types.h"
 
 namespace sensor {
 
@@ -49,7 +50,10 @@ private:
        int manager_connect(ipc::channel *ch, ipc::message &msg);
        int manager_disconnect(ipc::channel *ch, ipc::message &msg);
        int manager_get_sensor_list(ipc::channel *ch, ipc::message &msg);
-
+       int manager_preprocess_attr(ipc::channel *ch, cmd_manager_attr_int_t &buf,
+                                       sensor_handler **sensor);
+       int manager_set_attr_int(ipc::channel *ch, ipc::message &msg);
+       int manager_get_attr_int(ipc::channel *ch, ipc::message &msg);
 
        int listener_connect(ipc::channel *ch, ipc::message &msg);
        int listener_disconnect(ipc::channel *ch, ipc::message &msg);
index 29aaf86..c996bf4 100644 (file)
@@ -36,6 +36,8 @@ enum cmd_type_e {
        CMD_MANAGER_SENSOR_LIST,
        CMD_MANAGER_SENSOR_ADDED,
        CMD_MANAGER_SENSOR_REMOVED,
+       CMD_MANAGER_SET_ATTR_INT,
+       CMD_MANAGER_GET_ATTR_INT,
 
        /* Listener */
        CMD_LISTENER_EVENT = 0x200,
@@ -71,6 +73,12 @@ typedef struct {
 } cmd_manager_sensor_list_t;
 
 typedef struct {
+       int attribute;
+       int value;
+       char sensor[NAME_MAX];
+} cmd_manager_attr_int_t;
+
+typedef struct {
        int listener_id;
        char sensor[NAME_MAX];
 } cmd_listener_connect_t;