Merge "Adding pressure sensor plugin code modified for compatibility with IIO driver...
authorKibak Yoon <kibak.yoon@samsung.com>
Mon, 22 Sep 2014 04:55:27 +0000 (21:55 -0700)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Mon, 22 Sep 2014 04:55:27 +0000 (21:55 -0700)
src/light/light_sensor_hal.cpp
src/light/light_sensor_hal.h

index 06045db..6beb3c1 100755 (executable)
 using std::ifstream;
 using config::CConfig;
 
-#define NODE_NAME "name"
-#define NODE_INPUT "input"
-#define NODE_ENABLE "enable"
-#define NODE_POLL_DELAY "poll_delay"
-#define NODE_LIGHT_POLL_DELAY "light_poll_delay"
-#define SENSOR_NODE "/sys/class/sensors/"
-#define SENSORHUB_NODE "/sys/class/sensors/ssp_sensor/"
-#define INPUT_DEVICE_NODE "/sys/class/input/"
-#define DEV_INPUT_NODE "/dev/input/event/"
-
-#define BIAS   1
-#define INITIAL_VALUE -1
-#define INITIAL_TIME 0
+#define BIAS                           1
+#define INITIAL_VALUE          -1
+#define INITIAL_TIME           0
+#define NO_OF_DATA_VAL         1
 
 #define SENSOR_TYPE_LIGHT              "LIGHT"
 #define ELEMENT_NAME                   "NAME"
@@ -50,56 +41,38 @@ using config::CConfig;
 #define ELEMENT_RESOLUTION             "RESOLUTION"
 #define ATTR_VALUE                             "value"
 
-#define INPUT_NAME     "light_sensor"
-
 light_sensor_hal::light_sensor_hal()
 : m_adc(INITIAL_VALUE)
-, m_node_handle(INITIAL_VALUE)
-, m_polling_interval(POLL_1HZ_MS)
-, m_fired_time(INITIAL_TIME)
 , m_sensorhub_supported(false)
 {
-       if (!check_hw_node()) {
+       if (!check_hw_node())
+       {
                ERR("check_hw_node() fail");
                throw ENXIO;
        }
 
        CConfig &config = CConfig::get_instance();
 
-       if (!config.get(SENSOR_TYPE_LIGHT, m_model_id, ELEMENT_VENDOR, m_vendor)) {
+       if (!config.get(SENSOR_TYPE_LIGHT, m_model_id, ELEMENT_VENDOR, m_vendor))
+       {
                ERR("[VENDOR] is empty");
                throw ENXIO;
        }
 
        INFO("m_vendor = %s", m_vendor.c_str());
 
-       if (!config.get(SENSOR_TYPE_LIGHT, m_model_id, ELEMENT_NAME, m_chip_name)) {
+       if (!config.get(SENSOR_TYPE_LIGHT, m_model_id, ELEMENT_NAME, m_chip_name))
+       {
                ERR("[NAME] is empty");
                throw ENXIO;
        }
 
        INFO("m_chip_name = %s", m_chip_name.c_str());
-
-       if ((m_node_handle = open(m_resource.c_str(), O_RDWR)) < 0) {
-               ERR("Failed to open handle(%d)", m_node_handle);
-               throw ENXIO;
-       }
-
-       int clockId = CLOCK_MONOTONIC;
-
-       if (ioctl(m_node_handle, EVIOCSCLOCKID, &clockId) != 0) {
-               ERR("Fail to set monotonic timestamp for %s", m_resource.c_str());
-               throw ENXIO;
-       }
-
        INFO("light_sensor_hal is created!");
 }
 
 light_sensor_hal::~light_sensor_hal()
 {
-       close(m_node_handle);
-       m_node_handle = INITIAL_VALUE;
-
        INFO("light_sensor_hal is destroyed!");
 }
 
@@ -113,174 +86,35 @@ sensor_type_t light_sensor_hal::get_type(void)
        return LIGHT_SENSOR;
 }
 
-bool light_sensor_hal::enable_resource(string &resource_node, bool enable)
-{
-       int prev_status, status;
-       FILE *fp = NULL;
-       fp = fopen(resource_node.c_str(), "r");
-
-       if (!fp) {
-               ERR("Fail to open a resource file: %s", resource_node.c_str());
-               return false;
-       }
-
-       if (fscanf(fp, "%d", &prev_status) < 0) {
-               ERR("Failed to get data from %s", resource_node.c_str());
-               fclose(fp);
-               return false;
-       }
-
-       fclose(fp);
-
-       if (enable) {
-               if (m_sensorhub_supported)
-                       status = prev_status | (1 << SENSORHUB_LIGHT_ENABLE_BIT);
-               else
-                       status = 1;
-       } else {
-               if (m_sensorhub_supported)
-                       status = prev_status ^ (1 << SENSORHUB_LIGHT_ENABLE_BIT);
-               else
-                       status = 0;
-       }
-
-       fp = fopen(resource_node.c_str(), "w");
-
-       if (!fp) {
-               ERR("Failed to open a resource file: %s", resource_node.c_str());
-               return false;
-       }
-
-       if (fprintf(fp, "%d", status) < 0) {
-               ERR("Failed to enable a resource file: %s", resource_node.c_str());
-               fclose(fp);
-               return false;
-       }
-
-       if (fp)
-               fclose(fp);
-
-       return true;
-}
-
 bool light_sensor_hal::enable(void)
 {
-       AUTOLOCK(m_mutex);
-
-       enable_resource(m_enable_resource, true);
-       set_interval(m_polling_interval);
-
-       m_fired_time = 0;
-       INFO("Light sensor real starting");
+       INFO("Resource already enabled. Enable not supported.");
        return true;
 }
 
 bool light_sensor_hal::disable(void)
 {
-       AUTOLOCK(m_mutex);
-
-       enable_resource(m_enable_resource, false);
-       INFO("Light sensor real stopping");
+       INFO("Disable not supported.");
        return true;
 }
 
 bool light_sensor_hal::set_interval(unsigned long val)
 {
-       unsigned long long polling_interval_ns;
-       FILE *fp = NULL;
-
-       AUTOLOCK(m_mutex);
-
-       polling_interval_ns = ((unsigned long long)(val) * MS_TO_SEC * MS_TO_SEC);
-       fp = fopen(m_polling_resource.c_str(), "w");
-
-       if (!fp) {
-               ERR("Failed to open a resource file: %s", m_polling_resource.c_str());
-               return false;
-       }
-
-       if (fprintf(fp, "%llu", polling_interval_ns) < 0) {
-               ERR("Failed to set data %llu", polling_interval_ns);
-               fclose(fp);
-               return false;
-       }
-
-       if (fp)
-               fclose(fp);
-
-       INFO("Interval is changed from %dms to %dms]", m_polling_interval, val);
-       m_polling_interval = val;
+       INFO("Polling not supported. Polling interval cannot be changed.");
        return true;
 }
 
-bool light_sensor_hal::update_value(bool wait)
+bool light_sensor_hal::update_value(void)
 {
-       const int TIMEOUT = 1;
-       struct timeval tv;
-       fd_set readfds, exceptfds;
-       int adc = INITIAL_VALUE;
-
-       FD_ZERO(&readfds);
-       FD_ZERO(&exceptfds);
-       FD_SET(m_node_handle, &readfds);
-       FD_SET(m_node_handle, &exceptfds);
-
-       if (wait) {
-               tv.tv_sec = TIMEOUT;
-               tv.tv_usec = 0;
-       } else {
-               tv.tv_sec = 0;
-               tv.tv_usec = 0;
-       }
-
-       int ret;
-       ret = select(m_node_handle + 1, &readfds, NULL, &exceptfds, &tv);
-
-       if (ret == -1) {
-               ERR("select error:%s m_node_handle:%d", strerror(errno), m_node_handle);
-               return false;
-       } else if (!ret) {
-               DBG("select timeout: %d seconds elapsed", tv.tv_sec);
-               return false;
-       }
-
-       if (FD_ISSET(m_node_handle, &exceptfds)) {
-               ERR("select exception occurred!");
-               return false;
-       }
-
-       if (FD_ISSET(m_node_handle, &readfds)) {
-               struct input_event light_event;
-               DBG("light event detection!");
-
-               int len;
-               len = read(m_node_handle, &light_event, sizeof(light_event));
-
-               if (len == -1) {
-                       DBG("read(m_node_handle) is error:%s.", strerror(errno));
-                       return false;
-               }
-
-               if (light_event.type == EV_ABS && light_event.code == ABS_MISC) {
-                       adc = light_event.value;
-               } else if (light_event.type == EV_REL && light_event.code == REL_RX) {
-                       adc = light_event.value - BIAS;
-               } else {
-                       DBG("light input event[type = %d, code = %d] is unknown.", light_event.type, light_event.code);
-                       return false;
-               }
-
-               DBG("read event, len : %d, type : %x, code : %x, value : %x",
-                       len, light_event.type, light_event.code, light_event.value);
-               DBG("update_value, adc : %d", adc);
-
-               AUTOLOCK(m_value_mutex);
-               m_adc = adc;
-               m_fired_time = get_timestamp(&light_event.time);
-       } else {
-               ERR("select nothing to read!!!");
+       unsigned short int adc = INITIAL_VALUE;
+       if (!read_node_value<unsigned short int>(m_clear_raw_node, adc))
+       {
+               INFO("Read Value Failed. clear val: %d", adc);
                return false;
        }
+       INFO("Read Value success. Light Sensor clear val: %d", adc);
+       AUTOLOCK(m_value_mutex);
+       m_adc = (int)adc;
 
        return true;
 }
@@ -288,30 +122,17 @@ bool light_sensor_hal::update_value(bool wait)
 bool light_sensor_hal::is_data_ready(bool wait)
 {
        bool ret;
-       ret = update_value(wait);
+       ret = update_value();
        return ret;
 }
 
 int light_sensor_hal::get_sensor_data(sensor_data_t &data)
 {
-       const int chance = 3;
-       int retry = 0;
-
-       while ((m_fired_time == 0) && (retry++ < chance)) {
-               INFO("Try usleep for getting a valid BASE DATA value");
-               usleep(m_polling_interval * MS_TO_SEC);
-       }
-
-       if (m_fired_time == 0) {
-               ERR("get_sensor_data failed");
-               return -1;
-       }
-
        AUTOLOCK(m_value_mutex);
        data.data_accuracy = SENSOR_ACCURACY_GOOD;
        data.data_unit_idx = SENSOR_UNIT_LUX;
-       data.timestamp = m_fired_time ;
-       data.values_num = 1;
+       data.timestamp = INITIAL_TIME;
+       data.values_num = NO_OF_DATA_VAL;
        data.values[0] = (float) m_adc;
 
        return 0;
@@ -330,23 +151,14 @@ bool light_sensor_hal::get_properties(sensor_properties_t &properties)
 
 bool light_sensor_hal::is_sensorhub_supported(void)
 {
-       DIR *main_dir = NULL;
-       main_dir = opendir(SENSORHUB_NODE);
-
-       if (!main_dir) {
-               INFO("Sensor Hub is not supported");
-               return false;
-       }
-
-       INFO("It supports sensor hub");
-       closedir(main_dir);
-       return true;
+       return false;
 }
 
 bool light_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;
@@ -354,86 +166,46 @@ bool light_sensor_hal::check_hw_node(void)
        INFO("======================start check_hw_node=============================");
 
        m_sensorhub_supported = is_sensorhub_supported();
-       main_dir = opendir(SENSOR_NODE);
+       main_dir = opendir(IIO_DIR);
 
-       if (!main_dir) {
+       if (!main_dir)
+       {
                ERR("Directory open failed to collect data");
                return false;
        }
 
-       while ((!find_node) && (dir_entry = readdir(main_dir))) {
-               if ((strncasecmp(dir_entry->d_name , ".", 1 ) != 0) && (strncasecmp(dir_entry->d_name , "..", 2 ) != 0) && (dir_entry->d_ino != 0)) {
-                       name_node = string(SENSOR_NODE) + string(dir_entry->d_name) + string("/") + string(NODE_NAME);
+       while (!find_node)
+       {
+               dir_entry = readdir(main_dir);
+               if(dir_entry == NULL)
+                       break;
 
-                       ifstream infile(name_node.c_str());
+               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 (CConfig::get_instance().is_supported(SENSOR_TYPE_LIGHT, hw_name) == true) {
-                               m_name = m_model_id = hw_name;
-                               INFO("m_model_id = %s", m_model_id.c_str());
-                               find_node = true;
-                               break;
-                       }
-               }
-       }
-
-       closedir(main_dir);
-
-       if (find_node) {
-               main_dir = opendir(INPUT_DEVICE_NODE);
-
-               if (!main_dir) {
-                       ERR("Directory open failed to collect data");
-                       return false;
-               }
-
-               find_node = false;
-
-               while ((!find_node) && (dir_entry = readdir(main_dir))) {
-                       if (strncasecmp(dir_entry->d_name, NODE_INPUT, 5) == 0) {
-                               name_node = string(INPUT_DEVICE_NODE) + string(dir_entry->d_name) + string("/") + string(NODE_NAME);
-                               ifstream infile(name_node.c_str());
-
-                               if (!infile)
-                                       continue;
-
-                               infile >> hw_name;
-
-                               if (hw_name == string(INPUT_NAME)) {
-                                       INFO("name_node = %s", name_node.c_str());
-                                       DBG("Find H/W  for light_sensor");
-
+                       if (strncmp(dir_entry->d_name, IIO_DEV_BASE_NAME, IIO_DEV_STR_LEN) == 0)
+                       {
+                               if (CConfig::get_instance().is_supported(SENSOR_TYPE_LIGHT, hw_name) == true)
+                               {
+                                       m_name = m_model_id = hw_name;
+                                       m_clear_raw_node = string(IIO_DIR) + string(dir_entry->d_name) + string(ILL_CLEAR_NODE);
+                                       INFO("m_model_id = %s", m_model_id.c_str());
+                                       INFO("m_clear_raw_node = %s", m_clear_raw_node.c_str());
                                        find_node = true;
-                                       string dir_name;
-                                       dir_name = string(dir_entry->d_name);
-                                       unsigned found = dir_name.find_first_not_of(NODE_INPUT);
-                                       m_resource = string(DEV_INPUT_NODE) + dir_name.substr(found);
-
-                                       if (m_sensorhub_supported) {
-                                               m_enable_resource = string(SENSORHUB_NODE) + string(NODE_ENABLE);
-                                               m_polling_resource = string(SENSORHUB_NODE) + string(NODE_LIGHT_POLL_DELAY);
-                                       } else {
-                                               m_enable_resource = string(INPUT_DEVICE_NODE) + string(dir_entry->d_name) + string("/") + string(NODE_ENABLE);
-                                               m_polling_resource = string(INPUT_DEVICE_NODE) + string(dir_entry->d_name) + string("/") + string(NODE_POLL_DELAY);
-                                       }
-
                                        break;
                                }
                        }
                }
-
-               closedir(main_dir);
-       }
-
-       if (find_node) {
-               INFO("m_resource = %s", m_resource.c_str());
-               INFO("m_enable_resource = %s", m_enable_resource.c_str());
-               INFO("m_polling_resource = %s", m_polling_resource.c_str());
        }
+       closedir(main_dir);
 
        return find_node;
 }
index 2cba537..4c39805 100755 (executable)
 
 #include <sensor_hal.h>
 #include <string>
+#include <fstream>
+
+#define IIO_DIR                                "/sys/bus/iio/devices/"
+#define NAME_NODE                      "/name"
+#define ILL_CLEAR_NODE         "/in_illuminance_clear_raw"
+
+#define IIO_DEV_BASE_NAME      "iio:device"
+#define IIO_DEV_STR_LEN                10
 
 using std::string;
+using std::ifstream;
 
 class light_sensor_hal : public sensor_hal
 {
@@ -42,9 +51,6 @@ public:
 
 private:
        int m_adc;
-       int m_node_handle;
-       unsigned long m_polling_interval;
-       unsigned long long m_fired_time;
        bool m_sensorhub_supported;
 
        string m_model_id;
@@ -52,14 +58,26 @@ private:
        string m_vendor;
        string m_chip_name;
 
-       string m_resource;
-       string m_enable_resource;
-       string m_polling_resource;
-
        cmutex m_value_mutex;
+       string m_clear_raw_node;
 
-       bool enable_resource(string &resource_node, bool enable);
-       bool update_value(bool wait);
+       bool update_value(void);
        bool is_sensorhub_supported(void);
+
+       template <typename value_t>
+       bool read_node_value(string node_path, value_t &value)
+       {
+               ifstream handle;
+               handle.open(node_path.c_str());
+               if (!handle)
+               {
+                       ERR("Failed to open handle(%s)", node_path.c_str());
+                       return false;
+               }
+               handle >> value;
+               handle.close();
+
+               return true;
+       }
 };
-#endif /*_LIGHT_SENSOR_HAL_H_*/
\ No newline at end of file
+#endif /*_LIGHT_SENSOR_HAL_H_*/