From fec0e8f54ebe88fb558f51566cdbb893dd2caeec Mon Sep 17 00:00:00 2001 From: Vibhor Gaur Date: Fri, 21 Nov 2014 15:35:04 +0530 Subject: [PATCH] Synchronizing light sensor plugin with addition of IIO interface support Change-Id: Icd70f7833feef03ce6d9daf7dd4aabf721d5d146 --- packaging/sensord.spec | 3 +- src/light/light_sensor.cpp | 8 +- src/light/light_sensor_hal.cpp | 192 +++++++++++++++++++---------------------- src/light/light_sensor_hal.h | 39 +++++---- 4 files changed, 114 insertions(+), 128 deletions(-) diff --git a/packaging/sensord.spec b/packaging/sensord.spec index eb9d4c2..c7e8946 100755 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -11,7 +11,7 @@ Source2: sensord.socket %define accel_state ON %define gyro_state ON %define proxi_state ON -%define light_state OFF +%define light_state ON %define geo_state ON %define pressure_state OFF %define temperature_state OFF @@ -144,6 +144,7 @@ systemctl daemon-reload /usr/bin/gyro /usr/bin/proxi /usr/bin/pressure + %license LICENSE.APLv2 %{_datadir}/license/test %endif diff --git a/src/light/light_sensor.cpp b/src/light/light_sensor.cpp index 4c8481d..af222e1 100755 --- a/src/light/light_sensor.cpp +++ b/src/light/light_sensor.cpp @@ -159,10 +159,9 @@ bool light_sensor::get_properties(const unsigned int type, sensor_properties_t & return 0; if (type == LIGHT_BASE_DATA_SET) { - properties.sensor_unit_idx = SENSOR_UNIT_LEVEL; - properties.sensor_min_range = 0; - properties.sensor_max_range = sizeof(m_light_level) / sizeof(m_light_level[0]) - 1; - properties.sensor_resolution = 1; + properties.min_range = 0; + properties.max_range = sizeof(m_light_level) / sizeof(m_light_level[0]) - 1; + properties.resolution = 1; return 0; } @@ -198,7 +197,6 @@ bool light_sensor::set_interval(unsigned long interval) void light_sensor::raw_to_level(sensor_data_t &data) { - data.data_unit_idx = SENSOR_UNIT_LEVEL; data.values[0] = (int) adc_to_light_level((int)data.values[0]); data.values_num = 1; } diff --git a/src/light/light_sensor_hal.cpp b/src/light/light_sensor_hal.cpp index b429ffc..6498728 100755 --- a/src/light/light_sensor_hal.cpp +++ b/src/light/light_sensor_hal.cpp @@ -1,5 +1,5 @@ /* - * sensord + * light_sensor_hal * * Copyright (c) 2014 Samsung Electronics Co., Ltd. * @@ -16,65 +16,106 @@ * limitations under the License. * */ - #include #include #include -#include #include + #include #include + #include +#include #include using std::ifstream; using config::csensor_config; -#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" #define ELEMENT_VENDOR "VENDOR" #define ELEMENT_RAW_DATA_UNIT "RAW_DATA_UNIT" #define ELEMENT_RESOLUTION "RESOLUTION" #define ATTR_VALUE "value" +#define INITIAL_TIME -1 +#define BIAS 1 +#define INVALID_VALUE -1 +#define INITIAL_VALUE -1 light_sensor_hal::light_sensor_hal() -: m_adc(INITIAL_VALUE) -, m_sensorhub_supported(false) +: m_polling_interval(POLL_1HZ_MS) +, m_adc(INVALID_VALUE) +, m_fired_time(INITIAL_TIME) +, m_node_handle(-1) { - if (!check_hw_node()) - { - ERR("check_hw_node() fail"); + const string sensorhub_interval_node_name = "light_poll_delay"; + csensor_config &config = csensor_config::get_instance(); + + node_path_info_query query; + node_path_info info; + int input_method = IIO_METHOD; + + if (!get_model_properties(SENSOR_TYPE_LIGHT, m_model_id, input_method)) { + ERR("Failed to find model_properties"); throw ENXIO; + } - csensor_config &config = csensor_config::get_instance(); + query.input_method = IIO_METHOD; + query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name); + query.sensor_type = SENSOR_TYPE_LIGHT; + query.input_event_key = "light_sensor"; + query.iio_enable_node_name = "light_enable"; + query.sensorhub_interval_node_name = sensorhub_interval_node_name; - if (!config.get(SENSOR_TYPE_LIGHT, 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; + } + + show_node_path_info(info); + + m_data_node = info.data_node_path; + m_enable_node = info.enable_node_path; + m_interval_node = info.interval_node_path; + + if(input_method == IIO_METHOD) { + m_light_dir=info.base_dir; + m_light_node = m_light_dir + string(ILL_CLEAR_NODE); + + INFO("m_light_node = %s", m_light_node.c_str()); + INFO("m_light_dir = %s", m_light_dir.c_str()); + } + + if (!config.get(SENSOR_TYPE_LIGHT, 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_LIGHT, m_model_id, ELEMENT_NAME, m_chip_name)) - { - ERR("[NAME] is empty"); + if (!config.get(SENSOR_TYPE_LIGHT, 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("light_sensor_hal is created!"); + INFO("m_chip_name = %s\n",m_chip_name.c_str()); + + if ((m_node_handle = open(m_light_node.c_str(),O_RDWR)) < 0) { + ERR("Failed to open handle(%d)", m_node_handle); + throw ENXIO; + } + + INFO("light_sensor_hal is created!\n"); + } light_sensor_hal::~light_sensor_hal() { - INFO("light_sensor_hal is destroyed!"); + close(m_node_handle); + m_node_handle = -1; + + INFO("light_sensor_hal is destroyed!\n"); } string light_sensor_hal::get_model_id(void) @@ -82,6 +123,7 @@ string light_sensor_hal::get_model_id(void) return m_model_id; } + sensor_type_t light_sensor_hal::get_type(void) { return LIGHT_SENSOR; @@ -89,26 +131,28 @@ sensor_type_t light_sensor_hal::get_type(void) bool light_sensor_hal::enable(void) { - INFO("Resource already enabled. Enable not supported."); + m_fired_time = INITIAL_TIME; + INFO("Light sensor real starting"); return true; } bool light_sensor_hal::disable(void) { - INFO("Disable not supported."); + INFO("Light sensor real stopping"); return true; } bool light_sensor_hal::set_interval(unsigned long val) { - INFO("Polling not supported. Polling interval cannot be changed."); return true; } -bool light_sensor_hal::update_value(void) + +bool light_sensor_hal::update_value(bool wait) { + unsigned short int adc = INITIAL_VALUE; - if (!read_node_value(m_clear_raw_node, adc)) + if (!read_node_value(m_light_node, adc)) { INFO("Read Value Failed. clear val: %d", adc); return false; @@ -118,99 +162,41 @@ bool light_sensor_hal::update_value(void) m_adc = (int)adc; return true; + } bool light_sensor_hal::is_data_ready(bool wait) { bool ret; - ret = update_value(); + ret = update_value(wait); return ret; } int light_sensor_hal::get_sensor_data(sensor_data_t &data) { AUTOLOCK(m_value_mutex); - data.data_accuracy = SENSOR_ACCURACY_GOOD; - data.data_unit_idx = SENSOR_UNIT_LUX; - data.timestamp = INITIAL_TIME; - data.values_num = NO_OF_DATA_VAL; + data.accuracy = SENSOR_ACCURACY_GOOD; + data.timestamp = m_fired_time ; + data.value_count = 1; data.values[0] = (float) m_adc; return 0; } + bool light_sensor_hal::get_properties(sensor_properties_t &properties) { - properties.sensor_unit_idx = SENSOR_UNIT_LUX; - properties.sensor_min_range = 0; - properties.sensor_max_range = 65536; - 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.0f; + properties.name = m_chip_name; + properties.vendor = m_vendor; + properties.min_range = 0; + properties.max_range = 65536; + properties.min_interval = 1; + properties.resolution = 1; + properties.fifo_count = 0; + properties.max_batch_count = 0; return true; } -bool light_sensor_hal::is_sensorhub_supported(void) -{ - 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; - - INFO("======================start check_hw_node============================="); - - m_sensorhub_supported = is_sensorhub_supported(); - main_dir = opendir(IIO_DIR); - - if (!main_dir) - { - ERR("Directory open failed to collect data"); - 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_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; - break; - } - } - } - } - closedir(main_dir); - - return find_node; -} - extern "C" void *create(void) { light_sensor_hal *inst; @@ -218,14 +204,14 @@ extern "C" void *create(void) try { inst = new light_sensor_hal(); } catch (int err) { - ERR("Failed to create light_sensor_hal class, errno : %d, errstr : %s", err, strerror(err)); + ERR("light_sensor_hal 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 (light_sensor_hal *)inst; + delete (light_sensor_hal*)inst; } diff --git a/src/light/light_sensor_hal.h b/src/light/light_sensor_hal.h index 30c53f2..ec35104 100755 --- a/src/light/light_sensor_hal.h +++ b/src/light/light_sensor_hal.h @@ -1,5 +1,5 @@ /* - * sensord + * light_sensor_hal * * Copyright (c) 2014 Samsung Electronics Co., Ltd. * @@ -19,17 +19,10 @@ #ifndef _LIGHT_SENSOR_HAL_H_ #define _LIGHT_SENSOR_HAL_H_ - +#define ILL_CLEAR_NODE "in_illuminance_clear_raw" #include #include -#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; class light_sensor_hal : public sensor_hal @@ -45,21 +38,29 @@ public: 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); - private: - int m_adc; - bool m_sensorhub_supported; - string m_model_id; - string m_name; string m_vendor; string m_chip_name; - cmutex m_value_mutex; + unsigned long m_polling_interval; + + int m_adc; + + unsigned long long m_fired_time; + int m_node_handle; + + string m_enable_node; + string m_data_node; + string m_interval_node; string m_clear_raw_node; + string m_light_dir; + string m_light_node; + + bool m_sensorhub_controlled; + + cmutex m_value_mutex; - bool update_value(void); - bool is_sensorhub_supported(void); + bool update_value(bool wait); }; -#endif /*_LIGHT_SENSOR_HAL_H_*/ +#endif /*_GYRO_SENSOR_HAL_CLASS_H_*/ -- 2.7.4