Prioritizing event queue for virtual sensors 99/28599/11
authorVibhor Gaur <vibhor.gaur@samsung.com>
Fri, 10 Oct 2014 09:37:16 +0000 (15:07 +0530)
committerVibhor Gaur <vibhor.gaur@samsung.com>
Thu, 16 Oct 2014 08:14:51 +0000 (13:44 +0530)
-Prioritizing events coming from accelerometer,gyroscope and geomagnetic sensors in the event queue for orientation,gravity and linear acceleration virtual sensors.

Change-Id: I763b765190e5fd0a38897d874a3fccec413592e5

src/shared/csensor_event_queue.h

index 890e179..85e6ea9 100755 (executable)
 #include <queue>
 #include <mutex>
 #include <condition_variable>
+#include <sensor_accel.h>
+#include <sensor_gyro.h>
+#include <sensor_geomag.h>
+#include <sensor_orientation.h>
+#include <sensor_linear_accel.h>
+#include <sensor_gravity.h>
+#include <set>
+#include <algorithm>
 
+using namespace std;
 using std::queue;
+using std::set;
 using std::mutex;
 using std::lock_guard;
 using std::unique_lock;
 using std::condition_variable;
 
+static set<unsigned int> priority_list;
+
 class csensor_event_queue
 {
 private:
        static const unsigned int QUEUE_FULL_SIZE = 1000;
 
-       queue<void * > m_queue;
+       class compare {
+       public:
+               bool operator() (void* &v1,void *&v2) {
+                       sensor_event_t *e1 = (sensor_event_t *)v1;
+                       sensor_event_t *e2 = (sensor_event_t *)v2;
+                       bool prioritize_e1 = true;
+                       bool prioritize_e2 = true;
+                       /*Since priority_list is a set it will insert an event only if it is not present in the set*/
+                       if (e2->event_type == GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME || e2->event_type == LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME || e2->event_type == ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME) {
+                               priority_list.insert(ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME);
+                               priority_list.insert(GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME);
+                               priority_list.insert(GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME);
+                       }
+
+                       if (priority_list.empty())
+                               return (e2->data.timestamp < e1->data.timestamp);
+
+                       set<unsigned int>::iterator iter_e1 = priority_list.find(e1->event_type);
+                       set<unsigned int>::iterator iter_e2 = priority_list.find(e2->event_type);
+
+                       if (iter_e1 == priority_list.end())
+                               prioritize_e1 = false;
+
+                       if (iter_e2 == priority_list.end())
+                               prioritize_e2 = false;
+
+                       if (prioritize_e2) {
+                               if (!prioritize_e1)
+                                       return true;
+                               else {
+                                       if (e2->data.timestamp <= e1->data.timestamp)
+                                               return true;
+                                       return false;
+                               }
+                       }
+                       else {
+                               if (prioritize_e1)
+                                       return false;
+                               else if (e2->data.timestamp <= e1->data.timestamp)
+                                       return true;
+                               else
+                                       return false;
+                       }
+               }
+       };
+
+       std::priority_queue<void*, std::vector<void*>, compare> m_queue;
+
        mutex m_mutex;
        condition_variable m_cond_var;