Updating Gravity Virtual Sensor Code 94/30594/1
authorRamasamy <ram.kannan@samsung.com>
Fri, 21 Nov 2014 04:09:34 +0000 (09:39 +0530)
committerRamasamy <ram.kannan@samsung.com>
Fri, 21 Nov 2014 04:09:55 +0000 (09:39 +0530)
- Updating Gravity sensor after testing on SDK
- Updating based on latest public code
- Updating for new XML based configuration file

Change-Id: I2000b0ce9a5a10525d54c3d6977075cd1f70e6d0

packaging/sensord.spec
src/gravity/gravity_sensor.cpp
src/gravity/gravity_sensor.h

index 78f5b98..bfa8fa4 100755 (executable)
@@ -16,7 +16,7 @@ Source2:    sensord.socket
 %define pressure_state OFF
 %define temperature_state OFF
 %define orientation_state ON
-%define gravity_state OFF
+%define gravity_state ON
 %define linear_accel_state OFF
 
 %define build_test_suite OFF
index 5d574a0..08b70c5 100755 (executable)
 #include <dlfcn.h>
 #include <common.h>
 #include <sf_common.h>
-#include <sensor_base.h>
 #include <gravity_sensor.h>
 #include <sensor_plugin_loader.h>
+#include <cvirtual_sensor_config.h>
 
 #define INITIAL_VALUE -1
-#define INITIAL_TIME 0
 #define GRAVITY 9.80665
 
+#define DEG2RAD (M_PI/180)
+
 #define SENSOR_NAME "GRAVITY_SENSOR"
+#define SENSOR_TYPE_GRAVITY            "GRAVITY"
+
+#define MS_TO_US 1000
+
+#define ELEMENT_NAME                                                                                   "NAME"
+#define ELEMENT_VENDOR                                                                                 "VENDOR"
+#define ELEMENT_RAW_DATA_UNIT                                                                  "RAW_DATA_UNIT"
+#define ELEMENT_DEFAULT_SAMPLING_TIME                                                  "DEFAULT_SAMPLING_TIME"
+#define ELEMENT_GRAVITY_SIGN_COMPENSATION                                              "GRAVITY_SIGN_COMPENSATION"
 
 gravity_sensor::gravity_sensor()
 : m_orientation_sensor(NULL)
 , m_x(INITIAL_VALUE)
 , m_y(INITIAL_VALUE)
 , m_z(INITIAL_VALUE)
-, m_timestamp(INITIAL_TIME)
 {
-       m_name = string(SENSOR_NAME);
+       cvirtual_sensor_config &config = cvirtual_sensor_config::get_instance();
 
+       m_name = string(SENSOR_NAME);
+       m_timestamp = get_timestamp();
        register_supported_event(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME);
+
+       if (!config.get(SENSOR_TYPE_GRAVITY, ELEMENT_VENDOR, m_vendor)) {
+               ERR("[VENDOR] is empty\n");
+               throw ENXIO;
+       }
+
+       INFO("m_vendor = %s", m_vendor.c_str());
+
+       if (!config.get(SENSOR_TYPE_GRAVITY, ELEMENT_RAW_DATA_UNIT, m_raw_data_unit)) {
+               ERR("[RAW_DATA_UNIT] is empty\n");
+               throw ENXIO;
+       }
+
+       INFO("m_raw_data_unit = %s", m_raw_data_unit.c_str());
+
+       if (!config.get(SENSOR_TYPE_GRAVITY, ELEMENT_DEFAULT_SAMPLING_TIME, &m_default_sampling_time)) {
+               ERR("[DEFAULT_SAMPLING_TIME] is empty\n");
+               throw ENXIO;
+       }
+
+       INFO("m_default_sampling_time = %d", m_default_sampling_time);
+
+       if (!config.get(SENSOR_TYPE_GRAVITY, ELEMENT_GRAVITY_SIGN_COMPENSATION, m_gravity_sign_compensation, 3)) {
+               ERR("[GRAVITY_SIGN_COMPENSATION] is empty\n");
+               throw ENXIO;
+       }
+
+       INFO("m_gravity_sign_compensation = (%d, %d, %d)", m_gravity_sign_compensation[0], m_gravity_sign_compensation[1], m_gravity_sign_compensation[2]);
+
+       m_interval = m_default_sampling_time * MS_TO_US;
 }
 
 gravity_sensor::~gravity_sensor()
 {
-       INFO("gravity_sensor is destroyed!");
+       INFO("gravity_sensor is destroyed!\n");
 }
 
 bool gravity_sensor::init()
@@ -77,6 +118,7 @@ bool gravity_sensor::on_start(void)
        AUTOLOCK(m_mutex);
 
        m_orientation_sensor->add_client(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME);
+       m_orientation_sensor->add_interval((int)this, (m_interval/MS_TO_US), true);
        m_orientation_sensor->start();
 
        activate();
@@ -88,6 +130,7 @@ bool gravity_sensor::on_stop(void)
        AUTOLOCK(m_mutex);
 
        m_orientation_sensor->delete_client(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME);
+       m_orientation_sensor->delete_interval((int)this, true);
        m_orientation_sensor->stop();
 
        deactivate();
@@ -113,24 +156,28 @@ bool gravity_sensor::delete_interval(int client_id)
 void gravity_sensor::synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs)
 {
        sensor_event_t gravity_event;
-       vector<sensor_event_t> orientation_event;
-       ((virtual_sensor *)m_orientation_sensor)->synthesize(event, orientation_event);
-
-       if (!orientation_event.empty()) {
-               AUTOLOCK(m_mutex);
-
-               m_timestamp = orientation_event[0].data.timestamp;
-               gravity_event.data.values[0] = GRAVITY * sin(orientation_event[0].data.values[1]);
-               gravity_event.data.values[1] = GRAVITY * sin(orientation_event[0].data.values[0]);
-               gravity_event.data.values[2] = GRAVITY * cos(orientation_event[0].data.values[1] *
-                                                                               orientation_event[0].data.values[0]);
-               gravity_event.data.values_num = 3;
+
+       const float MIN_DELIVERY_DIFF_FACTOR = 0.75f;
+       unsigned long long diff_time;
+
+       if (event.event_type == ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME) {
+               diff_time = event.data.timestamp - m_timestamp;
+
+               if (m_timestamp && (diff_time < m_interval * MIN_DELIVERY_DIFF_FACTOR))
+                       return;
+
+               gravity_event.sensor_id = get_id();
+               gravity_event.event_type = GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME;
+               m_timestamp = get_timestamp();
+               gravity_event.data.values[0] = m_gravity_sign_compensation[0] * GRAVITY * sin(event.data.values[2] * DEG2RAD);
+               gravity_event.data.values[1] = m_gravity_sign_compensation[1] * GRAVITY * sin(event.data.values[1] * DEG2RAD);
+               gravity_event.data.values[2] = m_gravity_sign_compensation[2] * GRAVITY * cos(event.data.values[2] * DEG2RAD) *
+                                                                               cos(event.data.values[1] * DEG2RAD);
+               gravity_event.data.value_count = 3;
                gravity_event.data.timestamp = m_timestamp;
-               gravity_event.data.data_accuracy = SENSOR_ACCURACY_GOOD;
-               gravity_event.data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
+               gravity_event.data.accuracy = SENSOR_ACCURACY_GOOD;
 
-               outs.push_back(gravity_event);
-               return;
+               push(gravity_event);
        }
 }
 
@@ -143,25 +190,24 @@ int gravity_sensor::get_sensor_data(const unsigned int event_type, sensor_data_t
 
        m_orientation_sensor->get_sensor_data(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME, orientation_data);
 
-       data.data_accuracy = SENSOR_ACCURACY_GOOD;
-       data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
-       data.timestamp = orientation_data.timestamp;
-       data.values[0] = GRAVITY * sin(orientation_data.values[1]);
-       data.values[1] = GRAVITY * sin(orientation_data.values[0]);
-       data.values[2] = GRAVITY * cos(orientation_data.values[1] * orientation_data.values[0]);
-       data.values_num = 3;
+       data.accuracy = SENSOR_ACCURACY_GOOD;
+       data.timestamp = get_timestamp();
+       data.values[0] = m_gravity_sign_compensation[0] * GRAVITY * sin(orientation_data.values[2] * DEG2RAD);
+       data.values[1] = m_gravity_sign_compensation[1] * GRAVITY * sin(orientation_data.values[1] * DEG2RAD);
+       data.values[2] = m_gravity_sign_compensation[2] * GRAVITY * cos(orientation_data.values[2] * DEG2RAD) *
+                                               cos(orientation_data.values[1] * DEG2RAD);
+       data.value_count = 3;
 
        return 0;
 }
 
-bool gravity_sensor::get_properties(const unsigned int type, sensor_properties_t &properties)
+bool gravity_sensor::get_properties(sensor_properties_t &properties)
 {
-       properties.sensor_unit_idx = SENSOR_UNIT_DEGREE;
-       properties.sensor_min_range = -GRAVITY;
-       properties.sensor_max_range = GRAVITY;
-       properties.sensor_resolution = 1;
-       strncpy(properties.sensor_vendor, "Samsung", MAX_KEY_LENGTH);
-       strncpy(properties.sensor_name, SENSOR_NAME, MAX_KEY_LENGTH);
+       properties.min_range = -GRAVITY;
+       properties.max_range = GRAVITY;
+       properties.resolution = 1;
+       properties.vendor = m_vendor;
+       properties.name = SENSOR_NAME;
 
        return true;
 }
index 33e1eaa..d7aeacb 100755 (executable)
 #ifndef _GRAVITY_SENSOR_H_
 #define _GRAVITY_SENSOR_H_
 
-#include <sensor.h>
+#include <sensor_internal.h>
 #include <virtual_sensor.h>
 #include <string>
 
 using std::string;
 
-class gravity_sensor : public virtual_sensor
-{
+class gravity_sensor : public virtual_sensor {
 public:
        gravity_sensor();
        virtual ~gravity_sensor();
@@ -35,25 +34,30 @@ public:
        bool init();
        sensor_type_t get_type(void);
 
-       static bool working(void *inst);
-
-       bool on_start(void);
-       bool on_stop(void);
-
        void synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs);
 
        bool add_interval(int client_id, unsigned int interval);
        bool delete_interval(int client_id);
 
        int get_sensor_data(const unsigned int event_type, sensor_data_t &data);
-       bool get_properties(const unsigned int type, sensor_properties_t &properties);
+       bool get_properties(sensor_properties_t &properties);
 private:
        sensor_base *m_orientation_sensor;
+       cmutex m_value_mutex;
 
        float m_x;
        float m_y;
        float m_z;
        unsigned long long m_timestamp;
+       unsigned int m_interval;
+
+       string m_vendor;
+       string m_raw_data_unit;
+       int m_default_sampling_time;
+       int m_gravity_sign_compensation[3];
+
+       bool on_start(void);
+       bool on_stop(void);
 };
 
-#endif /*_GRAVITY_SENSOR_H_*/
+#endif