From: Amit Dharmapurikar Date: Tue, 11 Nov 2014 12:01:38 +0000 (+0530) Subject: Gyro sensor plugin synchronization with IIO interface support X-Git-Tag: submit/tizen/20150113.012540~107^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=84fc1f53b481b024fc2538823b74ee2831b4509f;p=platform%2Fcore%2Fsystem%2Fsensord.git Gyro sensor plugin synchronization with IIO interface support - Modified gyro plugin as per latest code - Added IIO driver interface support on RD-PQ target - Added a minor fix in sensor_hal class and modifide accel_sensor_hal.cpp accordingly Change-Id: I86b40f1f4c4bfecfda6406694c4c650751109009 Signed-off-by: Amit Dharmapurikar --- diff --git a/packaging/sensord.spec b/packaging/sensord.spec index 30202f7..079f1cc 100755 --- a/packaging/sensord.spec +++ b/packaging/sensord.spec @@ -9,7 +9,7 @@ Source1: sensord.service Source2: sensord.socket %define accel_state ON -%define gyro_state OFF +%define gyro_state ON %define proxi_state OFF %define light_state OFF %define geo_state OFF diff --git a/src/accel/accel_sensor_hal.cpp b/src/accel/accel_sensor_hal.cpp index 6b155b7..f38a0ed 100755 --- a/src/accel/accel_sensor_hal.cpp +++ b/src/accel/accel_sensor_hal.cpp @@ -47,7 +47,8 @@ using config::CConfig; #define INPUT_NAME "accelerometer_sensor" #define ACCEL_SENSORHUB_POLL_NODE_NAME "accel_poll_delay" -#define SCAN_EL_DIR "scan_elements/" +#define SCAN_EL_DIR "scan_elements/" +#define SCALE_AVAILABLE_NODE "in_accel_scale_available" #define ACCEL_RINGBUF_LEN 32 #define SEC_MSEC 1000 #define MSEC_TO_FREQ(VAL) ((SEC_MSEC) / (VAL)) @@ -94,7 +95,7 @@ accel_sensor_hal::accel_sensor_hal() m_buffer_enable_node_path = info.buffer_enable_node_path; m_buffer_length_node_path = info.buffer_length_node_path; m_available_freq_node_path = info.available_freq_node_path; - m_available_scale_node_path = info.available_scale_node_path; + m_available_scale_node_path = m_accel_dir + string(SCALE_AVAILABLE_NODE); if (!config.get(SENSOR_TYPE_ACCEL, m_model_id, ELEMENT_VENDOR, m_vendor)) { ERR("[VENDOR] is empty\n"); diff --git a/src/gyro/gyro_sensor.cpp b/src/gyro/gyro_sensor.cpp index 78b05be..d057959 100755 --- a/src/gyro/gyro_sensor.cpp +++ b/src/gyro/gyro_sensor.cpp @@ -19,10 +19,10 @@ #include #include + #include #include -#define INITIAL_VALUE -1 #define MS_TO_US 1000 #define DPS_TO_MDPS 1000 #define RAW_DATA_TO_DPS_UNIT(X) ((float)(X)/((float)DPS_TO_MDPS)) @@ -31,7 +31,7 @@ gyro_sensor::gyro_sensor() : m_sensor_hal(NULL) -, m_resolution(INITIAL_VALUE) +, m_resolution(0.0f) { m_name = string(SENSOR_NAME); @@ -42,7 +42,7 @@ gyro_sensor::gyro_sensor() gyro_sensor::~gyro_sensor() { - INFO("gyro_sensor is destroyed!"); + INFO("gyro_sensor is destroyed!\n"); } bool gyro_sensor::init() @@ -57,13 +57,14 @@ bool gyro_sensor::init() sensor_properties_t properties; if (m_sensor_hal->get_properties(properties) == false) { - ERR("sensor->get_properties() is failed!"); + ERR("sensor->get_properties() is failed!\n"); return false; } - m_resolution = properties.sensor_resolution; + m_resolution = properties.resolution; + + INFO("%s is created!\n", sensor_base::get_name()); - INFO("%s is created!", sensor_base::get_name()); return true; } @@ -74,8 +75,8 @@ sensor_type_t gyro_sensor::get_type(void) bool gyro_sensor::working(void *inst) { - gyro_sensor *sensor = (gyro_sensor *)inst; - return sensor->process_event(); + gyro_sensor *sensor = (gyro_sensor*)inst; + return sensor->process_event();; } bool gyro_sensor::process_event(void) @@ -90,6 +91,7 @@ bool gyro_sensor::process_event(void) AUTOLOCK(m_client_info_mutex); if (get_client_cnt(GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME)) { + event.sensor_id = get_id(); event.event_type = GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME; raw_to_base(event.data); push(event); @@ -100,10 +102,8 @@ bool gyro_sensor::process_event(void) bool gyro_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; } @@ -112,22 +112,20 @@ bool gyro_sensor::on_start(void) bool gyro_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 gyro_sensor::get_properties(const unsigned int type, sensor_properties_t &properties) +bool gyro_sensor::get_properties(sensor_properties_t &properties) { return m_sensor_hal->get_properties(properties); } -int gyro_sensor::get_sensor_data(const unsigned int type, sensor_data_t &data) +int gyro_sensor::get_sensor_data(unsigned int type, sensor_data_t &data) { int state; @@ -137,7 +135,7 @@ int gyro_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; } @@ -151,13 +149,13 @@ bool gyro_sensor::set_interval(unsigned long interval) AUTOLOCK(m_mutex); INFO("Polling interval is set to %dms", interval); + return m_sensor_hal->set_interval(interval); } void gyro_sensor::raw_to_base(sensor_data_t &data) { - data.data_unit_idx = SENSOR_UNIT_DEGREE_PER_SECOND; - data.values_num = 3; + data.value_count = 3; data.values[0] = data.values[0] * m_resolution; data.values[1] = data.values[1] * m_resolution; data.values[2] = data.values[2] * m_resolution; @@ -170,14 +168,14 @@ extern "C" void *create(void) try { inst = new gyro_sensor(); } catch (int err) { - ERR("Failed to create gyro_sensor class, errno : %d, errstr : %s", err, strerror(err)); + ERR("gyro_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 (gyro_sensor *)inst; + delete (gyro_sensor*)inst;; } diff --git a/src/gyro/gyro_sensor.h b/src/gyro/gyro_sensor.h index 51eb31c..81d7557 100755 --- a/src/gyro/gyro_sensor.h +++ b/src/gyro/gyro_sensor.h @@ -21,11 +21,11 @@ #define _GYRO_SENSOR_H_ #include + #include #include -class gyro_sensor : public physical_sensor -{ +class gyro_sensor : public physical_sensor { public: gyro_sensor(); virtual ~gyro_sensor(); @@ -35,18 +35,17 @@ public: static bool working(void *inst); - virtual bool on_start(void); - virtual bool on_stop(void); - virtual bool set_interval(unsigned long interval); - virtual bool get_properties(const unsigned int type, sensor_properties_t &properties); - 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; float m_resolution; + virtual bool on_start(void); + virtual bool on_stop(void); void raw_to_base(sensor_data_t &data); bool process_event(void); }; -#endif /*_GYRO_SENSOR_H_*/ \ No newline at end of file +#endif diff --git a/src/gyro/gyro_sensor_hal.cpp b/src/gyro/gyro_sensor_hal.cpp index 1dda6b9..fe04ca0 100755 --- a/src/gyro/gyro_sensor_hal.cpp +++ b/src/gyro/gyro_sensor_hal.cpp @@ -1,5 +1,5 @@ /* - * sensord + * gyro_sensor_hal * * Copyright (c) 2014 Samsung Electronics Co., Ltd. * @@ -16,99 +16,153 @@ * limitations under the License. * */ - -#include #include #include -#include #include + #include #include + #include +#include +#include +#include #include -#include using std::ifstream; using config::CConfig; -#define INITIAL_VALUE -1 -#define INITIAL_TIME 0 #define DPS_TO_MDPS 1000 -#define MIN_RANGE(RES) (-((2 << (RES))/2)) -#define MAX_RANGE(RES) (((2 << (RES))/2)-1) +#define MIN_RANGE(RES) (-((1 << (RES))/2)) +#define MAX_RANGE(RES) (((1 << (RES))/2)-1) #define RAW_DATA_TO_DPS_UNIT(X) ((float)(X)/((float)DPS_TO_MDPS)) -#define SEC_MSEC 1000 -#define MSEC_TO_FREQ(VAL) (int)((SEC_MSEC) / (VAL)) - #define SENSOR_TYPE_GYRO "GYRO" -#define ELEMENT_NAME "NAME" +#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 ELEMENT_RESOLUTION "RESOLUTION" + +#define ATTR_VALUE "value" -#define ENABLE_VAL 1 -#define DISABLE_VAL 0 -#define DEV_DIR "/dev/" -#define TRIG_PATH "trigger/current_trigger" +#define SCALE_AVAILABLE_NODE "in_anglvel_scale_available" +#define SCAN_EL_DIR "scan_elements/" +#define TRIG_SUFFIX "-trigger" +#define GYRO_RINGBUF_LEN 32 +#define SEC_MSEC 1000 +#define MSEC_TO_FREQ(VAL) ((SEC_MSEC) / (VAL)) +#define NSEC_TO_MUSEC(VAL) ((VAL) / 1000) gyro_sensor_hal::gyro_sensor_hal() -: m_x(INITIAL_VALUE) -, m_y(INITIAL_VALUE) -, m_z(INITIAL_VALUE) +: m_x(-1) +, m_y(-1) +, m_z(-1) +, m_node_handle(-1) , m_polling_interval(POLL_1HZ_MS) -, m_fired_time(INITIAL_TIME) -, m_sensorhub_supported(false) +, m_fired_time(0) { - if (!check_hw_node()) - { - ERR("check_hw_node() fail"); + + const string sensorhub_interval_node_name = "gyro_poll_delay"; + CConfig &config = CConfig::get_instance(); + + node_path_info_query query; + node_path_info info; + int input_method = IIO_METHOD; + + if (!get_model_properties(SENSOR_TYPE_GYRO, m_model_id, input_method)) { + ERR("Failed to find model_properties"); + throw ENXIO; + + } + + query.input_method = input_method; + query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name); + query.sensor_type = SENSOR_TYPE_GYRO; + query.input_event_key = "gyro_sensor"; + query.iio_enable_node_name = "gyro_enable"; + query.sensorhub_interval_node_name = sensorhub_interval_node_name; + + if (!get_node_path_info(query, info)) { + ERR("Failed to get node info"); throw ENXIO; } - CConfig &config = CConfig::get_instance(); + show_node_path_info(info); + + m_data_node = info.data_node_path; + m_interval_node = info.interval_node_path; + m_gyro_dir = info.base_dir; + m_trigger_path = info.trigger_node_path; + m_buffer_enable_node_path = info.buffer_enable_node_path; + m_buffer_length_node_path = info.buffer_length_node_path; + m_available_freq_node_path = info.available_freq_node_path; + m_available_scale_node_path = m_gyro_dir + string(SCALE_AVAILABLE_NODE); - if (!config.get(SENSOR_TYPE_GYRO, m_model_id, ELEMENT_VENDOR, m_vendor)) - { - ERR("[VENDOR] is empty"); + if (!config.get(SENSOR_TYPE_GYRO, 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_GYRO, m_model_id, ELEMENT_NAME, m_chip_name)) - { - ERR("[NAME] is empty"); + if (!config.get(SENSOR_TYPE_GYRO, 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()); + + if (input_method == IIO_METHOD) { + m_trigger_name = m_model_id + TRIG_SUFFIX; + if (!verify_iio_trigger(m_trigger_name)) { + ERR("Failed verify trigger"); + throw ENXIO; + } + string scan_dir = m_gyro_dir + SCAN_EL_DIR; + if (!get_generic_channel_names(scan_dir, string("_type"), m_generic_channel_names)) + ERR ("Failed to find any input channels"); + else { + INFO ("generic channel names:"); + for (vector ::iterator it = m_generic_channel_names.begin(); + it != m_generic_channel_names.end(); ++it) { + INFO ("%s", it->c_str()); + } + } + } long resolution; - if (!config.get(SENSOR_TYPE_GYRO, m_model_id, ELEMENT_RESOLUTION, resolution)) - { - ERR("[RESOLUTION] is empty"); + if (!config.get(SENSOR_TYPE_GYRO, m_model_id, ELEMENT_RESOLUTION, resolution)) { + ERR("[RESOLUTION] is empty\n"); throw ENXIO; } m_resolution = (int)resolution; - INFO("m_resolution = %d", m_resolution); double raw_data_unit; - if (!config.get(SENSOR_TYPE_GYRO, m_model_id, ELEMENT_RAW_DATA_UNIT, raw_data_unit)) - { - ERR("[RAW_DATA_UNIT] is empty"); + if (!config.get(SENSOR_TYPE_GYRO, m_model_id, ELEMENT_RAW_DATA_UNIT, raw_data_unit)) { + ERR("[RAW_DATA_UNIT] is empty\n"); throw ENXIO; } m_raw_data_unit = (float)(raw_data_unit); - INFO("m_raw_data_unit = %f", m_raw_data_unit); - INFO("RAW_DATA_TO_DPS_UNIT(m_raw_data_unit) = [%f]", RAW_DATA_TO_DPS_UNIT(m_raw_data_unit)); - INFO("gyro_sensor_hal is created!"); + if ((m_node_handle = open(m_data_node.c_str(), O_RDWR)) < 0) { + ERR("gyro handle open fail for gyro processor, error:%s\n", strerror(errno)); + throw ENXIO; + } + + if (setup_channels() == true) + INFO("IIO channel setup successful"); + else { + ERR("IIO channel setup failed"); + throw ENXIO; + } + + INFO("m_raw_data_unit = %f\n",m_raw_data_unit); + INFO("RAW_DATA_TO_DPS_UNIT(m_raw_data_unit) = [%f]",RAW_DATA_TO_DPS_UNIT(m_raw_data_unit)); + INFO("gyro_sensor is created!\n"); } gyro_sensor_hal::~gyro_sensor_hal() @@ -116,10 +170,11 @@ gyro_sensor_hal::~gyro_sensor_hal() enable_resource(false); if (m_data != NULL) delete []m_data; - if (m_fp_buffer > 0) - close(m_fp_buffer); - INFO("gyro_sensor_hal is destroyed!"); + close(m_node_handle); + m_node_handle = -1; + + INFO("gyro_sensor is destroyed!\n"); } string gyro_sensor_hal::get_model_id(void) @@ -132,38 +187,16 @@ sensor_type_t gyro_sensor_hal::get_type(void) return GYROSCOPE_SENSOR; } -bool gyro_sensor_hal::enable_resource(bool enable) -{ - string temp; - int enable_val; - - if(enable) - enable_val = ENABLE_VAL; - else - enable_val = DISABLE_VAL; - - temp = m_gyro_dir + string(SCAN_EL_DIR) + string(CHANNEL_NAME_X) + string(ENABLE_SUFFIX); - update_sysfs_num(temp.c_str(), enable_val); - temp = m_gyro_dir + string(SCAN_EL_DIR) + string(CHANNEL_NAME_Y) + string(ENABLE_SUFFIX); - update_sysfs_num(temp.c_str(), enable_val); - temp = m_gyro_dir + string(SCAN_EL_DIR) + string(CHANNEL_NAME_Z) + string(ENABLE_SUFFIX); - update_sysfs_num(temp.c_str(), enable_val); - temp = m_gyro_dir + string(SCAN_EL_DIR) + string(CHANNEL_NAME_TIME) + string(ENABLE_SUFFIX); - update_sysfs_num(temp.c_str(), enable_val); - setup_trigger(INPUT_TRIG_NAME, enable); - setup_buffer(enable_val); - - return true; -} - bool gyro_sensor_hal::enable(void) { AUTOLOCK(m_mutex); - enable_resource(true); + if (!enable_resource(true)) + return false; + set_interval(m_polling_interval); - m_fired_time = INITIAL_TIME; + m_fired_time = 0; INFO("Gyro sensor real starting"); return true; } @@ -172,35 +205,35 @@ bool gyro_sensor_hal::disable(void) { AUTOLOCK(m_mutex); - enable_resource(false); + if (!enable_resource(false)) + return false; + INFO("Gyro sensor real stopping"); return true; + } bool gyro_sensor_hal::set_interval(unsigned long ms_interval) { - int freq, i, approx_freq; - freq = MSEC_TO_FREQ(ms_interval); - - for (i=0; i < m_sample_freq_count; i++) - { - if (freq == m_sample_freq[i]) - { - if (update_sysfs_num(m_freq_resource.c_str(), freq, true) == 0) - { + int freq, i; + + freq = (int)(MSEC_TO_FREQ(ms_interval)); + + for (i=0; i < m_sample_freq_count; i++) { + if (freq == m_sample_freq[i]) { + if (update_sysfs_num(m_interval_node.c_str(), freq, true) == 0) { INFO("Interval is changed from %lums to %lums]", m_polling_interval, ms_interval); m_polling_interval = ms_interval; return true; } - else - { + else { ERR("Failed to set data %lu\n", ms_interval); return false; } } } - INFO("The interval not supported: %lu\n", ms_interval); + DBG("The interval not supported: %lu\n", ms_interval); ERR("Failed to set data %lu\n", ms_interval); return false; } @@ -212,23 +245,22 @@ bool gyro_sensor_hal::update_value(bool wait) ssize_t read_size; const int TIMEOUT = 1000; - pfd.fd = m_fp_buffer; + pfd.fd = m_node_handle; pfd.events = POLLIN; if (wait) poll(&pfd, 1, TIMEOUT); else poll(&pfd, 1, 0); - read_size = read(m_fp_buffer, m_data, GYRO_RINGBUF_LEN * m_scan_size); - - if (read_size <= 0) - { - ERR("No gyro data available to read\n"); + read_size = read(m_node_handle, m_data, GYRO_RINGBUF_LEN * m_scan_size); + if (read_size <= 0) { + ERR("Gyro:No data available\n"); return false; } - - for (i = 0; i < (read_size / m_scan_size); i++) - decode_data(); + else { + for (i = 0; i < (read_size / m_scan_size); i++) + decode_data(); + } return true; } @@ -241,25 +273,11 @@ bool gyro_sensor_hal::is_data_ready(bool wait) int gyro_sensor_hal::get_sensor_data(sensor_data_t &data) { - const int chance = 3; - int retry = 0; - - while ((m_fired_time == INITIAL_TIME) && (retry++ < chance)) - { - INFO("Try usleep for getting a valid BASE DATA value"); - usleep(m_polling_interval * MS_TO_SEC); - } - - if (m_fired_time == INITIAL_TIME) - { - ERR("get_sensor_data failed"); - return -1; - } + AUTOLOCK(m_value_mutex); - data.data_accuracy = SENSOR_ACCURACY_GOOD; - data.data_unit_idx = SENSOR_UNIT_VENDOR_UNIT; + data.accuracy = SENSOR_ACCURACY_GOOD; data.timestamp = m_fired_time ; - data.values_num = 3; + data.value_count = 3; data.values[0] = m_x; data.values[1] = m_y; data.values[2] = m_z; @@ -269,126 +287,29 @@ int gyro_sensor_hal::get_sensor_data(sensor_data_t &data) bool gyro_sensor_hal::get_properties(sensor_properties_t &properties) { - properties.sensor_unit_idx = SENSOR_UNIT_DEGREE_PER_SECOND; - properties.sensor_min_range = MIN_RANGE(m_resolution) * RAW_DATA_TO_DPS_UNIT(m_raw_data_unit); - properties.sensor_max_range = MAX_RANGE(m_resolution) * RAW_DATA_TO_DPS_UNIT(m_raw_data_unit); - 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 = RAW_DATA_TO_DPS_UNIT(m_raw_data_unit); + properties.name = m_chip_name; + properties.vendor = m_vendor; + properties.min_range = MIN_RANGE(m_resolution)* RAW_DATA_TO_DPS_UNIT(m_raw_data_unit); + properties.max_range = MAX_RANGE(m_resolution)* RAW_DATA_TO_DPS_UNIT(m_raw_data_unit); + properties.min_interval = 1; + properties.resolution = RAW_DATA_TO_DPS_UNIT(m_raw_data_unit); + properties.fifo_count = 0; + properties.max_batch_count = 0; return true; -} -bool gyro_sensor_hal::is_sensorhub_supported(void) -{ - return false; } -bool gyro_sensor_hal::check_hw_node(void) +bool gyro_sensor_hal::add_gyro_channels_to_array(void) { - string name_node; - string hw_name; - string file_name; - string temp; - DIR *main_dir = NULL; - struct dirent *dir_entry = NULL; - bool find_node = false; - bool find_trigger = 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; - } - - m_channels = (struct channel_parameters*) malloc(sizeof(struct channel_parameters) * NO_OF_CHANNELS); - - while (!(find_node && find_trigger)) - { - 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_GYRO, hw_name) == true) - { - m_gyro_dir = string(IIO_DIR) + string(dir_entry->d_name) + string("/"); - m_buffer_access = string(DEV_DIR) + string(dir_entry->d_name); - m_name = m_model_id = hw_name; - find_node = true; - INFO("m_gyro_dir:%s\n", m_gyro_dir.c_str()); - INFO("m_buffer_access:%s\n", m_buffer_access.c_str()); - INFO("m_name:%s\n", m_name.c_str()); - } - } - - if (strncmp(dir_entry->d_name, IIO_TRIG_BASE_NAME, IIO_TRIG_STR_LEN) == 0) - { - if (hw_name == string(INPUT_TRIG_NAME)) - { - m_gyro_trig_dir = string(IIO_DIR) + string(dir_entry->d_name) + string("/"); - find_trigger = true; - DBG("m_gyro_trig_dir:%s\n", m_gyro_trig_dir.c_str()); - } - } - - if (find_node && find_trigger) - break; - } - } - - closedir(main_dir); - - if (find_node && find_trigger) - { - if (setup_channels() == true) - INFO("IIO channel setup successful"); - else - { - ERR("IIO channel setup failed"); + int i = 0; + m_channels = (struct channel_parameters*) malloc(sizeof(struct channel_parameters) * m_generic_channel_names.size()); + for (vector ::iterator it = m_generic_channel_names.begin(); + it != m_generic_channel_names.end(); ++it) { + if (add_channel_to_array(m_gyro_dir.c_str(), it->c_str() , &m_channels[i++]) < 0) { + ERR("Failed to add channel %s to channel array", it->c_str()); return false; } } - return (find_node && find_trigger); -} - -bool gyro_sensor_hal::add_gyro_channels_to_array(void) -{ - if (add_channel_to_array(m_gyro_dir.c_str(), CHANNEL_NAME_X, &m_channels[0]) < 0) - { - ERR("Failed to add %s to channel array", CHANNEL_NAME_X); - return false; - } - if (add_channel_to_array(m_gyro_dir.c_str(), CHANNEL_NAME_Y, &m_channels[1]) < 0) - { - ERR("Failed to add %s to channel array", CHANNEL_NAME_Y); - return false; - } - if (add_channel_to_array(m_gyro_dir.c_str(), CHANNEL_NAME_Z, &m_channels[2]) < 0) - { - ERR("Failed to add %s to channel array", CHANNEL_NAME_Z); - return false; - } - if (add_channel_to_array(m_gyro_dir.c_str(), CHANNEL_NAME_TIME, &m_channels[3]) < 0) - { - ERR("Failed to add channel time_stamp to channel array"); - return false; - } return true; } @@ -396,44 +317,34 @@ bool gyro_sensor_hal::setup_channels(void) { int freq, i; double sf; - string temp; enable_resource(true); - if (!add_gyro_channels_to_array()) + if (!add_gyro_channels_to_array()) { + ERR("Failed to add channels to array!"); return false; + } - sort_channels_by_index(m_channels, NO_OF_CHANNELS); + INFO("Sorting channels by index"); + sort_channels_by_index(m_channels, m_generic_channel_names.size()); + INFO("Sorting channels by index completed"); - m_scan_size = get_channel_array_size(m_channels, NO_OF_CHANNELS); - if (m_scan_size == 0) - { + m_scan_size = get_channel_array_size(m_channels, m_generic_channel_names.size()); + if (m_scan_size == 0) { ERR("Channel array size is zero"); return false; } m_data = new (std::nothrow) char[m_scan_size * GYRO_RINGBUF_LEN]; - if (m_data == NULL) - { + if (m_data == NULL) { ERR("Couldn't create data buffer\n"); return false; } - m_fp_buffer = open(m_buffer_access.c_str(), O_RDONLY | O_NONBLOCK); - if (m_fp_buffer == -1) - { - ERR("Failed to open ring buffer(%s)\n", m_buffer_access.c_str()); - return false; - } - - m_freq_resource = m_gyro_dir + string(GYRO_FREQ); - temp = m_gyro_dir + string(GYRO_FREQ_AVLBL); - FILE *fp = NULL; - fp = fopen(temp.c_str(), "r"); - if (!fp) - { - ERR("Fail to open available frequencies file:%s\n", temp.c_str()); + fp = fopen(m_available_freq_node_path.c_str(), "r"); + if (!fp) { + ERR("Fail to open available frequencies file:%s\n", m_available_freq_node_path.c_str()); return false; } @@ -441,15 +352,15 @@ bool gyro_sensor_hal::setup_channels(void) m_sample_freq[i] = 0; i = 0; + while (fscanf(fp, "%d", &freq) > 0) m_sample_freq[i++] = freq; + m_sample_freq_count = i; - temp = m_gyro_dir + string(GYRO_SCALE_AVLBL); - fp = fopen(temp.c_str(), "r"); - if (!fp) - { - ERR("Fail to open available scale factors file:%s\n", temp.c_str()); + fp = fopen(m_available_scale_node_path.c_str(), "r"); + if (!fp) { + ERR("Fail to open available scale factors file:%s\n", m_available_scale_node_path.c_str()); return false; } @@ -457,8 +368,10 @@ bool gyro_sensor_hal::setup_channels(void) m_scale_factor[i] = 0; i = 0; + while (fscanf(fp, "%lf", &sf) > 0) m_scale_factor[i++] = sf; + m_scale_factor_count = i; return true; @@ -476,20 +389,17 @@ void gyro_sensor_hal::decode_data(void) if ((val >> m_channels[3].valid_bits) & 1) val = (val & m_channels[3].mask) | ~m_channels[3].mask; - m_fired_time = (unsigned long long int)(val); - DBG("m_x = %d, m_y = %d, m_z = %d, time = %lluus", m_x, m_y, m_z, m_fired_time); + m_fired_time = (unsigned long long int)(NSEC_TO_MUSEC(val)); + INFO("m_x = %d, m_y = %d, m_z = %d, time = %lluus", m_x, m_y, m_z, m_fired_time); } -bool gyro_sensor_hal::setup_trigger(char* trig_name, bool verify) +bool gyro_sensor_hal::setup_trigger(const char* trig_name, bool verify) { - string temp; - int ret; + int ret = 0; - temp = m_gyro_dir + string(TRIG_PATH); - update_sysfs_string(temp.c_str(), trig_name, verify); - if (ret < 0) - { - ERR("failed to write to current_trigger\n"); + ret = update_sysfs_string(m_trigger_path.c_str(), trig_name); + if (ret < 0) { + ERR("failed to write to current_trigger,%s,%s\n", m_trigger_path.c_str(), trig_name); return false; } INFO("current_trigger setup successfully\n"); @@ -498,31 +408,42 @@ bool gyro_sensor_hal::setup_trigger(char* trig_name, bool verify) bool gyro_sensor_hal::setup_buffer(int enable) { - string temp; int ret; - temp = m_gyro_dir + string(BUFFER_LEN); - INFO("Buffer Length Setup: %s", temp.c_str()); - ret = update_sysfs_num(temp.c_str(), GYRO_RINGBUF_LEN, true); - if (ret < 0) - { + ret = update_sysfs_num(m_buffer_length_node_path.c_str(), GYRO_RINGBUF_LEN, true); + if (ret < 0) { ERR("failed to write to buffer/length\n"); return false; } INFO("buffer/length setup successfully\n"); - temp = m_gyro_dir + string(BUFFER_EN); - INFO("Buffer Enable: %s", temp.c_str()); - ret = update_sysfs_num(temp.c_str(), enable, true); - if (ret < 0) - { + ret = update_sysfs_num(m_buffer_enable_node_path.c_str(), enable, true); + if (ret < 0) { ERR("failed to write to buffer/enable\n"); return false; } + if (enable) INFO("buffer enabled\n"); else INFO("buffer disabled\n"); + return true; +} + +bool gyro_sensor_hal::enable_resource(bool enable) +{ + string temp; + if(enable) + setup_trigger(m_trigger_name.c_str(), enable); + else + setup_trigger("NULL", enable); + for (vector ::iterator it = m_generic_channel_names.begin(); + it != m_generic_channel_names.end(); ++it) { + temp = m_gyro_dir + string(SCAN_EL_DIR) + *it + string("_en"); + if (update_sysfs_num(temp.c_str(), enable) < 0) + return false; + } + setup_buffer(enable); return true; } @@ -530,20 +451,17 @@ extern "C" void *create(void) { gyro_sensor_hal *inst; - try - { + try { inst = new gyro_sensor_hal(); - } - catch (int err) - { - ERR("Failed to create gyro_sensor_hal class, errno : %d, errstr : %s", err, strerror(err)); + } catch (int err) { + ERR("gyro_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 (gyro_sensor_hal *)inst; + delete (gyro_sensor_hal*)inst; } diff --git a/src/gyro/gyro_sensor_hal.h b/src/gyro/gyro_sensor_hal.h index 4865d6f..f2e63a6 100755 --- a/src/gyro/gyro_sensor_hal.h +++ b/src/gyro/gyro_sensor_hal.h @@ -1,5 +1,5 @@ /* - * sensord + * gyro_sensor_hal * * Copyright (c) 2014 Samsung Electronics Co., Ltd. * @@ -24,38 +24,9 @@ #include #include -#define INPUT_DEV_NAME "lsm330dlc-gyro" -#define INPUT_TRIG_NAME "lsm330dlc-gyro-trigger" - -#define IIO_DIR "/sys/bus/iio/devices/" -#define GYRO_FREQ "sampling_frequency" -#define GYRO_FREQ_AVLBL "sampling_frequency_available" -#define GYRO_SCALE_AVLBL "in_anglvel_scale_available" -#define GYRO_X_SCALE "in_anglvel_x_scale" -#define GYRO_Y_SCALE "in_anglvel_y_scale" -#define GYRO_Z_SCALE "in_anglvel_z_scale" - -#define NO_OF_CHANNELS 4 #define MAX_FREQ_COUNT 16 #define MAX_SCALING_COUNT 16 -#define CHANNEL_NAME_X "in_anglvel_x" -#define CHANNEL_NAME_Y "in_anglvel_y" -#define CHANNEL_NAME_Z "in_anglvel_z" -#define CHANNEL_NAME_TIME "in_timestamp" -#define ENABLE_SUFFIX "_en" -#define NAME_NODE "/name" -#define BUFFER_EN "buffer/enable" -#define BUFFER_LEN "buffer/length" -#define SCAN_EL_DIR "scan_elements/" - -#define IIO_DEV_BASE_NAME "iio:device" -#define IIO_TRIG_BASE_NAME "trigger" -#define IIO_DEV_STR_LEN 10 -#define IIO_TRIG_STR_LEN 7 - -#define GYRO_RINGBUF_LEN 32 - using std::string; class gyro_sensor_hal : public sensor_hal @@ -67,55 +38,58 @@ public: sensor_type_t get_type(void); bool enable(void); bool disable(void); - bool set_interval(unsigned long val); + bool set_interval(unsigned long ms_interval); 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: int m_x; int m_y; int m_z; + int m_node_handle; unsigned long m_polling_interval; unsigned long long m_fired_time; - bool m_sensorhub_supported; + + int m_scale_factor_count; + int m_sample_freq_count; + int m_sample_freq[MAX_FREQ_COUNT]; + double m_scale_factor[MAX_SCALING_COUNT]; + char *m_data; + int m_scan_size; + struct channel_parameters *m_channels; + + string m_trigger_name; + string m_trigger_path; + string m_buffer_enable_node_path; + string m_buffer_length_node_path; + string m_available_freq_node_path; + string m_available_scale_node_path; + string m_gyro_dir; + vector m_generic_channel_names; string m_model_id; - string m_name; string m_vendor; string m_chip_name; int m_resolution; float m_raw_data_unit; - string m_polling_resource; + string m_data_node; + string m_interval_node; - string m_gyro_dir; - string m_gyro_trig_dir; - string m_buffer_access; - string m_freq_resource; - - int m_scale_factor_count; - int m_sample_freq_count; - int m_sample_freq[MAX_FREQ_COUNT]; - double m_scale_factor[MAX_SCALING_COUNT]; - - int m_fp_buffer; - char *m_data; - int m_scan_size; - struct channel_parameters *m_channels; + bool m_sensorhub_controlled; cmutex m_value_mutex; - bool enable_resource(bool enable); bool update_value(bool wait); - bool is_sensorhub_supported(void); - + bool setup_trigger(const char* trig_name, bool verify); + bool setup_buffer(int enable); + bool enable_resource(bool enable); bool add_gyro_channels_to_array(void); bool setup_channels(void); - bool setup_buffer(int enable); bool setup_trigger(char* trig_name, bool verify); void decode_data(void); + }; -#endif /*_GYRO_SENSOR_HAL_H_*/ +#endif /*_GYRO_SENSOR_HAL_CLASS_H_*/ diff --git a/src/shared/sensor_hal.cpp b/src/shared/sensor_hal.cpp index 8b64ff9..e036203 100755 --- a/src/shared/sensor_hal.cpp +++ b/src/shared/sensor_hal.cpp @@ -151,7 +151,6 @@ bool sensor_hal::get_iio_node_info(const string &key, const string& enable_node_ info.buffer_length_node_path = base_dir + string("buffer/length"); info.trigger_node_path = base_dir + string("trigger/current_trigger"); info.available_freq_node_path = base_dir + string("sampling_frequency_available"); - info.available_scale_node_path = base_dir + string("in_accel_scale_available"); return true; } diff --git a/src/shared/sensor_hal.h b/src/shared/sensor_hal.h index 3d62cbd..261608e 100755 --- a/src/shared/sensor_hal.h +++ b/src/shared/sensor_hal.h @@ -47,7 +47,6 @@ typedef struct { string buffer_length_node_path; string trigger_node_path; string available_freq_node_path; - string available_scale_node_path; string base_dir; } node_path_info;