sensor-hal-tm1: sync with sensor-hal-tw1 HAL
[platform/adaptation/tm1/sensor-hal-tm1.git] / src / proxi / proxi_device.cpp
similarity index 65%
rename from src/proxi/proxi.cpp
rename to src/proxi/proxi_device.cpp
index aa930f2..211db23 100644 (file)
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+
 #include <linux/input.h>
+#include <sys/ioctl.h>
+#include <poll.h>
+
 #include <util.h>
+#include <sensor_common.h>
 #include <sensor_log.h>
 
-#include "proxi.h"
+#include "proxi_device.h"
 
 #define MODEL_NAME "IMS1911"
 #define VENDOR "ITM"
 #define MIN_INTERVAL 1
 #define MAX_BATCH_COUNT 0
 
-#define SENSORHUB_PROXIMITY_ENABLE_BIT 7
+#define SENSOR_NAME "SENSOR_PROXIMITY"
+#define SENSOR_TYPE_PROXI              "PROXI"
+
+#define INPUT_NAME     "proximity_sensor"
+#define PROXI_SENSORHUB_POLL_NODE_NAME "prox_poll_delay"
 
 #define RAW_DATA_TO_DISTANCE(x) ((x) * 5)
 
-static const sensor_info_t sensor_info = {
+static sensor_info_t sensor_info = {
        id: 0x1,
-       name: "Proximity Sensor",
+       name: SENSOR_NAME,
        type: SENSOR_DEVICE_PROXIMITY,
-       event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
+       event_type: (SENSOR_DEVICE_PROXIMITY << SENSOR_EVENT_SHIFT) | RAW_DATA_EVENT,
        model_name: MODEL_NAME,
        vendor: VENDOR,
        min_range: MIN_RANGE,
@@ -52,41 +61,51 @@ static const sensor_info_t sensor_info = {
        wakeup_supported: false
 };
 
-std::vector<uint32_t> proxi_device::event_ids;
-
 proxi_device::proxi_device()
 : m_node_handle(-1)
-, m_state(-1)
+, m_state(PROXIMITY_NODE_STATE_FAR)
 , m_fired_time(0)
 , m_sensorhub_controlled(false)
 {
-       const std::string sensorhub_interval_node_name = "prox_poll_delay";
+       const std::string sensorhub_interval_node_name = PROXI_SENSORHUB_POLL_NODE_NAME;
 
        node_info_query query;
        node_info info;
 
        query.sensorhub_controlled = m_sensorhub_controlled = util::is_sensorhub_controlled(sensorhub_interval_node_name);
-       query.sensor_type = "PROXI";
-       query.key = "proximity_sensor";
+       query.sensor_type = SENSOR_TYPE_PROXI;
+       query.key = INPUT_NAME;
        query.iio_enable_node_name = "proximity_enable";
        query.sensorhub_interval_node_name = sensorhub_interval_node_name;
 
        if (!util::get_node_info(query, info)) {
-               ERR("Failed to get node info");
+               _E("Failed to get node info");
                throw ENXIO;
        }
 
        util::show_node_info(info);
 
+       m_method = info.method;
        m_data_node = info.data_node_path;
        m_enable_node = info.enable_node_path;
 
-       if ((m_node_handle = open(m_data_node.c_str(), O_RDONLY)) < 0) {
+       m_node_handle = open(m_data_node.c_str(), O_RDONLY);
+
+       if (m_node_handle < 0) {
                _ERRNO(errno, _E, "proxi handle open fail for proxi device");
                throw ENXIO;
        }
 
-       INFO("proxi_device is created!");
+       if (m_method == INPUT_EVENT_METHOD) {
+               if (!util::set_monotonic_clock(m_node_handle))
+                       throw ENXIO;
+
+               update_value = [=]() {
+                       return this->update_value_input_event();
+               };
+       }
+
+       _I("Proxi_sensor_hal is created!");
 }
 
 proxi_device::~proxi_device()
@@ -94,10 +113,10 @@ proxi_device::~proxi_device()
        close(m_node_handle);
        m_node_handle = -1;
 
-       INFO("proxi_device is destroyed!");
+       _I("Proxi_sensor_hal is destroyed!");
 }
 
-int proxi_device::get_poll_fd()
+int proxi_device::get_poll_fd(void)
 {
        return m_node_handle;
 }
@@ -114,7 +133,7 @@ bool proxi_device::enable(uint32_t id)
        util::set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT);
 
        m_fired_time = 0;
-       INFO("Enable proximity sensor");
+       INFO("Enable proxi sensor");
        return true;
 }
 
@@ -122,37 +141,40 @@ bool proxi_device::disable(uint32_t id)
 {
        util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT);
 
-       INFO("Disable proximity sensor");
+       INFO("Disable proxi sensor");
        return true;
 }
 
 bool proxi_device::update_value_input_event(void)
 {
        struct input_event proxi_event;
-       DBG("proxi event detection!");
+       _I("proxi event detection!");
 
        int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
 
        if (len == -1) {
-               _ERRNO(errno, _D, "Failed to read from m_node_handle[%d]", m_node_handle);
+               _ERRNO(errno, _E, "Failed to read from m_node_handle");
                return false;
        }
 
-       if ((proxi_event.type == EV_ABS) && (proxi_event.code == ABS_DISTANCE)) {
-               m_state = proxi_event.value;
-               m_fired_time = util::get_timestamp(&proxi_event.time);
-
-               DBG("m_state = %d, time = %lluus", m_state, m_fired_time);
+       _D("read event,  len : %d , type : %x , code : %x , value : %x", len, proxi_event.type, proxi_event.code, proxi_event.value);
+       if ((proxi_event.type != EV_ABS) || (proxi_event.code != ABS_DISTANCE))
+               return false;
 
-               return true;
+       if (proxi_event.value != PROXIMITY_NODE_STATE_FAR && proxi_event.value != PROXIMITY_NODE_STATE_NEAR) {
+               _E("PROXIMITY_STATE Unknown: %d",proxi_event.value);
+               return false;
        }
 
-       return false;
+       m_state = proxi_event.value;
+       m_fired_time = util::get_timestamp(&proxi_event.time);
+
+       return true;
 }
 
 int proxi_device::read_fd(uint32_t **ids)
 {
-       if (!update_value_input_event()) {
+       if (!update_value()) {
                DBG("Failed to update value");
                return false;
        }
@@ -170,6 +192,7 @@ int proxi_device::get_data(uint32_t id, sensor_data_t **data, int *length)
        int remains = 1;
        sensor_data_t *sensor_data;
        sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
+       retvm_if(!sensor_data, -ENOMEM, "Memory allocation failed");
 
        sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
        sensor_data->timestamp = m_fired_time;
@@ -181,4 +204,3 @@ int proxi_device::get_data(uint32_t id, sensor_data_t **data, int *length)
 
        return --remains;
 }
-