Synchronizing proxi sensor plugin with addition of IIO interface support 52/30552/1
authorAmit Dharmapurikar <amit.vd@samsung.com>
Thu, 20 Nov 2014 04:59:04 +0000 (10:29 +0530)
committerAmit Dharmapurikar <amit.vd@samsung.com>
Thu, 20 Nov 2014 04:59:04 +0000 (10:29 +0530)
Change-Id: Ic505aab62f6a42572a6cf441a9eb18b196ccb524
Signed-off-by: Amit Dharmapurikar <amit.vd@samsung.com>
packaging/sensord.spec
src/proxi/CMakeLists.txt
src/proxi/proxi_sensor.cpp
src/proxi/proxi_sensor.h
src/proxi/proxi_sensor_hal.cpp
src/proxi/proxi_sensor_hal.h

index 7420325..eb9d4c2 100755 (executable)
@@ -10,7 +10,7 @@ Source2:    sensord.socket
 
 %define accel_state ON
 %define gyro_state ON
-%define proxi_state OFF
+%define proxi_state ON
 %define light_state OFF
 %define geo_state ON
 %define pressure_state OFF
index 6a52735..f679e41 100755 (executable)
@@ -15,7 +15,7 @@ include_directories(${CMAKE_SOURCE_DIR}/src/libsensord)
 
 include(FindPkgConfig)
 pkg_check_modules(rpkgs REQUIRED vconf)
-add_definitions(${rpkgs_CFLAGS} -DUSE_ONLY_ONE_MODULE)
+add_definitions(${rpkgs_CFLAGS} -DUSE_ONLY_ONE_MODULE -DUSE_LCD_TYPE_CHECK)
 
 set(PROJECT_MAJOR_VERSION "0")
 set(PROJECT_MINOR_VERSION "0")
@@ -43,7 +43,7 @@ add_library(${SENSOR_NAME} SHARED
 )
 
 add_library(${SENSOR_HAL_NAME} SHARED
-       proxi_sensor_hal.cpp
+        proxi_sensor_hal.cpp
 )
 
 target_link_libraries(${SENSOR_NAME} ${rpkgs_LDFLAGS} ${GLES_LDFLAGS} "-lm")
index 4799da4..c55bc74 100755 (executable)
 
 #include <common.h>
 #include <sf_common.h>
+
 #include <proxi_sensor.h>
 #include <sensor_plugin_loader.h>
 
+
 #define SENSOR_NAME "PROXI_SENSOR"
 
 proxi_sensor::proxi_sensor()
@@ -39,7 +41,7 @@ proxi_sensor::proxi_sensor()
 
 proxi_sensor::~proxi_sensor()
 {
-       INFO("proxi_sensor is destroyed!");
+       INFO("proxi_sensor is destroyed!\n");
 }
 
 bool proxi_sensor::init()
@@ -51,7 +53,7 @@ bool proxi_sensor::init()
                return false;
        }
 
-       INFO("%s is created!", sensor_base::get_name());
+       INFO("%s is created!\n", sensor_base::get_name());
        return true;
 }
 
@@ -62,7 +64,7 @@ sensor_type_t proxi_sensor::get_type(void)
 
 bool proxi_sensor::working(void *inst)
 {
-       proxi_sensor *sensor = (proxi_sensor *)inst;
+       proxi_sensor *sensor = (proxi_sensor*)inst;
        return sensor->process_event();
 }
 
@@ -79,6 +81,7 @@ bool proxi_sensor::process_event(void)
        AUTOLOCK(m_client_info_mutex);
        AUTOLOCK(m_mutex);
 
+       event.sensor_id = get_id();
        if (get_client_cnt(PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME)) {
                event.event_type = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
                raw_to_base(event.data);
@@ -103,10 +106,8 @@ bool proxi_sensor::process_event(void)
 
 bool proxi_sensor::on_start(void)
 {
-       AUTOLOCK(m_mutex);
-
        if (!m_sensor_hal->enable()) {
-               ERR("m_sensor_hal start fail");
+               ERR("m_sensor_hal start fail\n");
                return false;
        }
 
@@ -115,22 +116,25 @@ bool proxi_sensor::on_start(void)
 
 bool proxi_sensor::on_stop(void)
 {
-       AUTOLOCK(m_mutex);
-
        if (!m_sensor_hal->disable()) {
-               ERR("m_sensor_hal stop fail");
+               ERR("m_sensor_hal stop fail\n");
                return false;
        }
 
        return stop_poll();
 }
 
-bool proxi_sensor::get_properties(const unsigned int type, sensor_properties_t &properties)
+bool proxi_sensor::get_properties(sensor_properties_t &properties)
 {
-       return m_sensor_hal->get_properties(properties);
+       m_sensor_hal->get_properties(properties);
+
+       properties.min_range = properties.min_range * 5;
+       properties.max_range = properties.max_range * 5;
+
+       return true;
 }
 
-int proxi_sensor::get_sensor_data(const unsigned int type, sensor_data_t &data)
+int proxi_sensor::get_sensor_data(unsigned int type, sensor_data_t &data)
 {
        int state;
 
@@ -140,7 +144,7 @@ int proxi_sensor::get_sensor_data(const unsigned int type, sensor_data_t &data)
        state = m_sensor_hal->get_sensor_data(data);
 
        if (state < 0) {
-               ERR("m_sensor_hal get struct_data fail");
+               ERR("m_sensor_hal get struct_data fail\n");
                return -1;
        }
 
@@ -159,7 +163,7 @@ void proxi_sensor::raw_to_base(sensor_data_t &data)
 
 void proxi_sensor::raw_to_state(sensor_data_t &data)
 {
-       data.values_num = 1;
+       data.value_count = 1;
 }
 
 extern "C" void *create(void)
@@ -169,14 +173,14 @@ extern "C" void *create(void)
        try {
                inst = new proxi_sensor();
        } catch (int err) {
-               ERR("Failed to create proxi_sensor class, errno : %d, errstr : %s", err, strerror(err));
+               ERR("proxi_sensor class create fail , errno : %d , errstr : %s\n", err, strerror(err));
                return NULL;
        }
 
-       return (void *)inst;
+       return (void*)inst;
 }
 
 extern "C" void destroy(void *inst)
 {
-       delete (proxi_sensor *)inst;
+       delete (proxi_sensor*)inst;
 }
index 5b8a411..b61da04 100755 (executable)
 #define _PROXI_SENSOR_H_
 
 #include <sensor_common.h>
+
 #include <physical_sensor.h>
 #include <sensor_hal.h>
 
-class proxi_sensor : public physical_sensor
-{
+class proxi_sensor : public physical_sensor {
 public:
        proxi_sensor();
        virtual ~proxi_sensor();
 
-       virtual bool init();
-       virtual sensor_type_t get_type(void);
+       bool init();
+       sensor_type_t get_type(void);
 
        static bool working(void *inst);
 
-       virtual bool on_start(void);
-       virtual bool on_stop(void);
-
-       virtual bool get_properties(const unsigned int type, sensor_properties_t &properties);
-       virtual int get_sensor_data(const unsigned int type, sensor_data_t &data);
+       virtual bool get_properties(sensor_properties_t &properties);
+       int get_sensor_data(unsigned int type, sensor_data_t &data);
 private:
        sensor_hal *m_sensor_hal;
-       cmutex m_value_mutex;
 
        int m_state;
 
+       cmutex m_value_mutex;
+
+
+       virtual bool on_start(void);
+       virtual bool on_stop(void);
+
        void raw_to_base(sensor_data_t &data);
        void raw_to_state(sensor_data_t &data);
        bool process_event(void);
 };
-#endif /*_PROXI_SENSOR_H_*/
+
+#endif
index c1fb0d7..1701100 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * sensord
+ * proxi_sensor_hal
  *
  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  *
  * limitations under the License.
  *
  */
-
-#include <fstream>
 #include <fcntl.h>
 #include <sys/stat.h>
-#include <sys/ioctl.h>
 #include <dirent.h>
+
 #include <linux/input.h>
 #include <cconfig.h>
+
 #include <proxi_sensor_hal.h>
+#include <sys/ioctl.h>
+#include <fstream>
+#include <cconfig.h>
 #include <iio_common.h>
 
-using std::ifstream;
-using config::CConfig;
+#define NO_FLAG                        0
+#define PROXIMITY_TYPE 8
 
-#define INITIAL_VALUE -1
-#define INITIAL_TIME 0
+#define EVENT_DIR              "events/"
+#define EVENT_EN_NODE  "in_proximity_thresh_either_en"
 
 #define SENSOR_TYPE_PROXI              "PROXI"
-#define ELEMENT_NAME                   "NAME"
+#define ELEMENT_NAME                   "NAME"
 #define ELEMENT_VENDOR                 "VENDOR"
-#define ATTR_VALUE                             "value"
+#define ATTR_VALUE                             "value"
 
-#define INPUT_NAME     "proximity_sensor"
+#define PROXI_CODE     0x0019
 
-#define NO_FLAG                        0
-#define ENABLE_VAL             true
-#define DISABLE_VAL            false
+using std::ifstream;
+using config::CConfig;
 
 proxi_sensor_hal::proxi_sensor_hal()
 : m_state(PROXIMITY_STATE_FAR)
-, m_node_handle(INITIAL_VALUE)
-, m_fired_time(INITIAL_TIME)
-, m_sensorhub_supported(false)
+, m_fired_time(0)
+, m_node_handle(-1)
 {
-       int fd, ret;
+       const string sensorhub_interval_node_name = "prox_poll_delay";
+       CConfig &config = CConfig::get_instance();
 
-       if (!check_hw_node())
-       {
-               ERR("check_hw_node() fail");
+       node_path_info_query query;
+       node_path_info info;
+       int input_method = IIO_METHOD;
+
+       if (!get_model_properties(SENSOR_TYPE_PROXI, m_model_id, input_method)) {
+               ERR("Failed to find model_properties");
                throw ENXIO;
+
        }
 
-       CConfig &config = CConfig::get_instance();
+       query.input_method = input_method;
+       query.sensorhub_controlled = m_sensorhub_controlled = false;
+       query.sensor_type = SENSOR_TYPE_PROXI;
+       query.input_event_key = "proximity_sensor";
+       query.iio_enable_node_name = "proximity_enable";
+       query.sensorhub_interval_node_name = sensorhub_interval_node_name;
 
-       if (!config.get(SENSOR_TYPE_PROXI, m_model_id, ELEMENT_VENDOR, m_vendor))
-       {
-               ERR("[VENDOR] is empty");
+       if (!get_node_path_info(query, info)) {
+               ERR("Failed to get node info");
+               throw ENXIO;
+       }
+
+       m_data_node = info.data_node_path;
+       m_enable_node = info.base_dir + string(EVENT_DIR) + string(EVENT_EN_NODE);
+
+       INFO("data node: %s",m_data_node.c_str());
+       INFO("enable node: %s",m_enable_node.c_str());
+
+       if (!config.get(SENSOR_TYPE_PROXI, m_model_id, ELEMENT_VENDOR, m_vendor)) {
+               ERR("[VENDOR] is empty\n");
                throw ENXIO;
        }
 
        INFO("m_vendor = %s", m_vendor.c_str());
 
-       if (!config.get(SENSOR_TYPE_PROXI, m_model_id, ELEMENT_NAME, m_chip_name))
-       {
-               ERR("[NAME] is empty");
+       if (!config.get(SENSOR_TYPE_PROXI, m_model_id, ELEMENT_NAME, m_chip_name)) {
+               ERR("[NAME] is empty\n");
                throw ENXIO;
        }
 
-       INFO("m_chip_name = %s", m_chip_name.c_str());
+       INFO("m_chip_name = %s\n",m_chip_name.c_str());
 
-       fd = open(m_event_resource.c_str(), NO_FLAG);
-       if (fd == -1)
-       {
+       int fd, ret;
+       fd = open(m_data_node.c_str(), NO_FLAG);
+       if (fd == -1) {
                ERR("Could not open event resource");
                throw ENXIO;
        }
 
-       ret = ioctl(fd, IOCTL_IIO_EVENT_FD, &m_event_fd);
+       ret = ioctl(fd, IOCTL_IIO_EVENT_FD, &m_node_handle);
 
        close(fd);
 
-       if ((ret == -1) || (m_event_fd == -1))
-       {
+       if ((ret == -1) || (m_node_handle == -1)) {
                ERR("Failed to retrieve event fd");
                throw ENXIO;
        }
 
-       INFO("proxi_sensor_hal is created!");
+       INFO("Proxi_sensor_hal is created!\n");
+
 }
 
 proxi_sensor_hal::~proxi_sensor_hal()
 {
-       close(m_event_fd);
-       INFO("proxi_sensor_hal is destroyed!");
+       close(m_node_handle);
+       m_node_handle = -1;
+
+       INFO("Proxi_sensor_hal is destroyed!\n");
 }
 
 string proxi_sensor_hal::get_model_id(void)
@@ -114,7 +135,7 @@ sensor_type_t proxi_sensor_hal::get_type(void)
 
 bool proxi_sensor_hal::enable_resource(bool enable)
 {
-       update_sysfs_num(m_enable_resource.c_str(), enable);
+       update_sysfs_num(m_enable_node.c_str(), enable);
        return true;
 }
 
@@ -122,10 +143,10 @@ bool proxi_sensor_hal::enable(void)
 {
        AUTOLOCK(m_mutex);
 
-       enable_resource(ENABLE_VAL);
+       enable_resource(true);
 
        m_fired_time = 0;
-       INFO("Proximity sensor real starting");
+       INFO("Proxi sensor real starting");
        return true;
 }
 
@@ -133,9 +154,9 @@ bool proxi_sensor_hal::disable(void)
 {
        AUTOLOCK(m_mutex);
 
-       enable_resource(DISABLE_VAL);
+       enable_resource(true);
 
-       INFO("Proximity sensor real stopping");
+       INFO("Proxi sensor real stopping");
        return true;
 }
 
@@ -146,71 +167,60 @@ bool proxi_sensor_hal::update_value(bool wait)
 
        FD_ZERO(&readfds);
        FD_ZERO(&exceptfds);
-       FD_SET(m_event_fd, &readfds);
-       FD_SET(m_event_fd, &exceptfds);
+       FD_SET(m_node_handle, &readfds);
+       FD_SET(m_node_handle, &exceptfds);
 
        int ret;
-       ret = select(m_event_fd + 1, &readfds, NULL, &exceptfds, NULL);
+       ret = select(m_node_handle + 1, &readfds, NULL, &exceptfds, NULL);
 
-       if (ret == -1)
-       {
-               ERR("select error:%s m_event_fd:d", strerror(errno), m_event_fd);
+       if (ret == -1) {
+               ERR("select error:%s m_node_handle:%d", strerror(errno), m_node_handle);
                return false;
        }
-       else if (!ret)
-       {
+       else if (!ret) {
                DBG("select timeout");
                return false;
        }
 
-       if (FD_ISSET(m_event_fd, &exceptfds))
-       {
+       if (FD_ISSET(m_node_handle, &exceptfds)) {
                ERR("select exception occurred!");
                return false;
        }
 
-       if (FD_ISSET(m_event_fd, &readfds))
-       {
+       if (FD_ISSET(m_node_handle, &readfds)) {
                INFO("proximity event detection!");
-               int len = read(m_event_fd, &proxi_event, sizeof(proxi_event));
+               int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
 
-               if (len == -1)
-               {
-                       DBG("Error in read(m_event_fd):%s.", strerror(errno));
+               if (len == -1) {
+                       DBG("Error in read(m_node_handle):%s.", strerror(errno));
                        return false;
                }
 
                ull_bytes_t ev_data;
                ev_data.num = proxi_event.event_id;
-               if (ev_data.bytes[CH_TYPE] == PROXIMITY_TYPE)
-               {
+               if (ev_data.bytes[CH_TYPE] == PROXIMITY_TYPE) {
                        AUTOLOCK(m_value_mutex);
                        int temp;
                        temp = GET_DIR_VAL(ev_data.bytes[DIRECTION]);
-                       if (temp == PROXIMITY_NODE_STATE_FAR)
-                       {
+                       if (temp == PROXIMITY_NODE_STATE_FAR) {
                                INFO("PROXIMITY_STATE_FAR state occurred");
                                m_state = PROXIMITY_STATE_FAR;
                        }
-                       else if (temp == PROXIMITY_NODE_STATE_NEAR)
-                       {
+                       else if (temp == PROXIMITY_NODE_STATE_NEAR) {
                                INFO("PROXIMITY_STATE_NEAR state occurred");
                                m_state = PROXIMITY_STATE_NEAR;
                        }
-                       else
-                       {
+                       else {
                                ERR("PROXIMITY_STATE Unknown: %d", proxi_event.event_id);
                                return false;
                        }
                }
                m_fired_time = proxi_event.timestamp;
        }
-       else
-       {
+       else {
                ERR("No proximity event data available to read");
                return false;
        }
-
        return true;
 }
 
@@ -224,110 +234,42 @@ bool proxi_sensor_hal::is_data_ready(bool wait)
 int proxi_sensor_hal::get_sensor_data(sensor_data_t &data)
 {
        AUTOLOCK(m_value_mutex);
-       data.data_accuracy = SENSOR_ACCURACY_UNDEFINED;
-       data.data_unit_idx = SENSOR_UNIT_STATE_ON_OFF;
+       data.accuracy = SENSOR_ACCURACY_UNDEFINED;
        data.timestamp = m_fired_time;
-       data.values_num = 1;
-       data.values[0] = (float)(m_state);
+       data.value_count = 1;
+       data.values[0] = m_state;
+
        return 0;
 }
 
 bool proxi_sensor_hal::get_properties(sensor_properties_t &properties)
 {
-       properties.sensor_unit_idx = SENSOR_UNIT_STATE_ON_OFF;
-       properties.sensor_min_range = 0;
-       properties.sensor_max_range = 1;
-       snprintf(properties.sensor_name,   sizeof(properties.sensor_name), "%s", m_chip_name.c_str());
-       snprintf(properties.sensor_vendor, sizeof(properties.sensor_vendor), "%s", m_vendor.c_str());
-       properties.sensor_resolution = 1;
+       properties.name = m_chip_name;
+       properties.vendor = m_vendor;
+       properties.min_range = 0;
+       properties.max_range = 1;
+       properties.min_interval = 1;
+       properties.resolution = 1;
+       properties.fifo_count = 0;
+       properties.max_batch_count = 0;
        return true;
 }
 
-bool proxi_sensor_hal::is_sensorhub_supported(void)
-{
-       return false;
-}
-
-bool proxi_sensor_hal::check_hw_node(void)
-{
-       string name_node;
-       string hw_name;
-       string file_name;
-       DIR *main_dir = NULL;
-       struct dirent *dir_entry = NULL;
-       bool find_node = false;
-
-       INFO("======================start check_hw_node=============================");
-
-       m_sensorhub_supported = is_sensorhub_supported();
-       main_dir = opendir(IIO_DIR);
-
-       if (!main_dir)
-       {
-               ERR("Could not open IIO directory\n");
-               return false;
-       }
-
-       while (!find_node)
-       {
-               dir_entry = readdir(main_dir);
-               if(dir_entry == NULL)
-                       break;
-
-               if ((strncasecmp(dir_entry->d_name , ".", 1 ) != 0) && (strncasecmp(dir_entry->d_name , "..", 2 ) != 0) && (dir_entry->d_ino != 0))
-               {
-                       file_name = string(IIO_DIR) + string(dir_entry->d_name) + string(NAME_NODE);
-
-                       ifstream infile(file_name.c_str());
-
-                       if (!infile)
-                               continue;
-
-                       infile >> hw_name;
-
-                       if (strncmp(dir_entry->d_name, IIO_DEV_BASE_NAME, IIO_DEV_STR_LEN) == 0)
-                       {
-                               if (CConfig::get_instance().is_supported(SENSOR_TYPE_PROXI, hw_name) == true)
-                               {
-                                       m_name = m_model_id = hw_name;
-                                       m_proxi_dir = string(dir_entry->d_name);
-                                       m_enable_resource = string(IIO_DIR) + m_proxi_dir + string(EVENT_DIR) + string(EVENT_EN_NODE);
-                                       m_event_resource = string(DEV_DIR) + m_proxi_dir;
-
-                                       INFO("m_enable_resource = %s", m_enable_resource.c_str());
-                                       INFO("m_model_id = %s", m_model_id.c_str());
-                                       INFO("m_proxi_dir = %s", m_proxi_dir.c_str());
-                                       INFO("m_event_resource = %s", m_event_resource.c_str());
-
-                                       find_node = true;
-                                       break;
-                               }
-                       }
-               }
-       }
-
-       closedir(main_dir);
-       return find_node;
-}
-
 extern "C" void *create(void)
 {
        proxi_sensor_hal *inst;
 
-       try
-       {
+       try {
                inst = new proxi_sensor_hal();
-       }
-       catch (int err)
-       {
-               ERR("Failed to create proxi_sensor_hal class, errno : %d, errstr : %s", err, strerror(err));
+       } catch (int err) {
+               ERR("proxi_sensor class create fail , errno : %d , errstr : %s\n", err, strerror(err));
                return NULL;
        }
 
-       return (void *)inst;
+       return (void*)inst;
 }
 
 extern "C" void destroy(void *inst)
 {
-       delete (proxi_sensor_hal *)inst;
+       delete (proxi_sensor_hal*)inst;
 }
index bd273fa..6113397 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * sensord
+ * proxi_sensor_hal
  *
  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  *
 #include <sensor_hal.h>
 #include <string>
 
-#define IIO_DIR                        "/sys/bus/iio/devices/"
-#define NAME_NODE              "/name"
-#define EVENT_DIR              "/events"
-#define EVENT_EN_NODE  "/in_proximity_thresh_either_en"
-#define DEV_DIR                        "/dev/"
-
-#define IIO_DEV_BASE_NAME      "iio:device"
-#define IIO_DEV_STR_LEN                10
-
-#define PROXIMITY_NODE_STATE_NEAR      1
-#define PROXIMITY_NODE_STATE_FAR       2
-#define PROXIMITY_TYPE                         8
-
 using std::string;
 
 class proxi_sensor_hal : public sensor_hal
 {
 public:
+       enum proxi_node_state_event_t { //changed as per IIO definitions
+               PROXIMITY_NODE_STATE_NEAR = 1,
+               PROXIMITY_NODE_STATE_FAR = 2,
+               PROXIMITY_NODE_STATE_UNKNOWN = 0,
+       };
+
        proxi_sensor_hal();
        virtual ~proxi_sensor_hal();
        string get_model_id(void);
@@ -49,31 +42,24 @@ public:
        bool disable(void);
        bool is_data_ready(bool wait);
        virtual int get_sensor_data(sensor_data_t &data);
-       bool get_properties(sensor_properties_t &properties);
-       bool check_hw_node(void);
-
+       virtual bool get_properties(sensor_properties_t &properties);
 private:
-       unsigned int m_state;
-       int m_node_handle;
-       unsigned long long m_fired_time;
-       bool m_sensorhub_supported;
-
        string m_model_id;
-       string m_name;
        string m_vendor;
        string m_chip_name;
 
-       string m_proxi_dir;
+       string m_enable_node;
+       string m_data_node;
 
-       string m_enable_resource;
-       string m_event_resource;
+       unsigned int m_state;
 
-       cmutex m_value_mutex;
+       unsigned long long m_fired_time;
 
-       int m_event_fd;
+       int m_node_handle;
+       bool m_sensorhub_controlled;
+       cmutex m_value_mutex;
 
-       bool enable_resource(bool enable);
        bool update_value(bool wait);
-       bool is_sensorhub_supported(void);
+       bool enable_resource(bool enable);
 };
 #endif /*_PROXI_SENSOR_HAL_H_*/