sensord: batch mode support 70/49870/1
authorHongkuk, Son <hongkuk.son@samsung.com>
Wed, 21 Oct 2015 05:26:47 +0000 (14:26 +0900)
committerHongkuk, Son <hongkuk.son@samsung.com>
Wed, 21 Oct 2015 05:27:26 +0000 (14:27 +0900)
Signed-off-by: Hongkuk, Son <hongkuk.son@samsung.com>
Change-Id: Id5f72577237caeb9dd814796a7b059f53e68015d

src/server/command_worker.cpp
src/shared/CMakeLists.txt
src/shared/cbatch_info_list.cpp [new file with mode: 0644]
src/shared/cbatch_info_list.h [new file with mode: 0644]
src/shared/sensor_base.cpp
src/shared/sensor_base.h

index 22c30fe..f8148de 100644 (file)
@@ -640,6 +640,13 @@ bool command_worker::cmd_set_batch(void *payload)
                goto out;
        }
 
+       if (!m_module->add_batch(m_client_id, cmd->latency)) {
+               ERR("Failed to set latency for client [%d], for sensor [0x%x] with latency [%d]",
+                       m_client_id, m_sensor_id, cmd->latency);
+               ret_value = OP_ERROR;
+               goto out;
+       }
+
        ret_value = OP_SUCCESS;
 
 out:
@@ -673,6 +680,12 @@ bool command_worker::cmd_unset_batch(void *payload)
                goto out;
        }
 
+       if (!m_module->delete_batch(m_client_id)) {
+               ERR("Failed to delete latency for client [%d]", m_client_id);
+               ret_value = OP_ERROR;
+               goto out;
+       }
+
        ret_value = OP_SUCCESS;
 
 out:
index d869101..b5d589c 100644 (file)
@@ -30,7 +30,7 @@ add_library(sensord-server SHARED
        cclient_sensor_record.cpp
        cinterval_info_list.cpp
        cwakeup_info_list.cpp
-       batch_info_list.cpp
+       cbatch_info_list.cpp
        sensor_plugin_loader.cpp
        sensor_hal.cpp
        sensor_base.cpp
@@ -66,7 +66,7 @@ install(FILES
        csensor_event_queue.h
        cinterval_info_list.h
        cwakeup_info_list.h
-       batch_info_list.h
+       cbatch_info_list.h
        sensor_plugin_loader.h
        sensor_hal.h
        sensor_base.h
diff --git a/src/shared/cbatch_info_list.cpp b/src/shared/cbatch_info_list.cpp
new file mode 100644 (file)
index 0000000..3c2681c
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * libsensord-share
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <cbatch_info_list.h>
+#include <algorithm>
+
+cbatch_info::cbatch_info(int client_id, unsigned int latency)
+{
+       this->client_id = client_id;
+       this->latency = latency;
+}
+
+bool cbatch_info_list::comp_batch_info(cbatch_info a, cbatch_info b)
+{
+       return a.latency < b.latency;
+}
+
+cbatch_info_iterator cbatch_info_list::find_if(int client_id)
+{
+       auto iter = m_list.begin();
+
+       while (iter != m_list.end()) {
+               if ((iter->client_id == client_id))
+                       break;
+
+               ++iter;
+       }
+
+       return iter;
+}
+
+bool cbatch_info_list::add_batch(int client_id, unsigned int latency)
+{
+       auto iter = find_if(client_id);
+
+       if (iter != m_list.end())
+               *iter = cbatch_info(client_id, latency);
+       else
+               m_list.push_back(cbatch_info(client_id, latency));
+
+       return true;
+}
+
+bool cbatch_info_list::delete_batch(int client_id)
+{
+       auto iter = find_if(client_id);
+
+       if (iter == m_list.end())
+               return false;
+
+       m_list.erase(iter);
+
+       return true;
+}
+
+unsigned int cbatch_info_list::get_batch(int client_id)
+{
+       auto iter = find_if(client_id);
+
+       if (iter == m_list.end())
+               return 0;
+
+       return iter->latency;
+}
+
+unsigned int cbatch_info_list::get_max(void)
+{
+       if (m_list.empty())
+               return 0;
+
+       auto iter = max_element(m_list.begin(), m_list.end(), comp_batch_info);
+
+       return iter->latency;
+}
+
diff --git a/src/shared/cbatch_info_list.h b/src/shared/cbatch_info_list.h
new file mode 100644 (file)
index 0000000..390c39d
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * libsensord-share
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef _CBATCH_INFO_LIST_CLASS_H_
+#define _CBATCH_INFO_LIST_CLASS_H_
+
+#include <list>
+
+class cbatch_info
+{
+public:
+       cbatch_info(int client_id, unsigned int latency);
+       int client_id;
+       unsigned int latency;
+};
+
+typedef std::list<cbatch_info>::iterator cbatch_info_iterator;
+
+class cbatch_info_list
+{
+private:
+       static bool comp_batch_info(cbatch_info a, cbatch_info b);
+       cbatch_info_iterator find_if(int client_id);
+
+       std::list<cbatch_info> m_list;
+
+public:
+       bool add_batch(int client_id, unsigned int latency);
+       bool delete_batch(int client_id);
+       unsigned int get_batch(int client_id);
+       unsigned int get_max(void);
+};
+#endif
index 52915e7..83815a9 100644 (file)
@@ -314,6 +314,62 @@ int sensor_base::get_wakeup(int client_id)
        return m_wakeup_info_list.is_wakeup_on();
 }
 
+bool sensor_base::add_batch(int client_id, unsigned int latency)
+{
+       unsigned int prev_max, cur_max;
+
+       AUTOLOCK(m_batch_info_list_mutex);
+
+       prev_max = m_batch_info_list.get_max();
+
+       if (!m_batch_info_list.add_batch(client_id, latency))
+               return false;
+
+       cur_max = m_batch_info_list.get_max();
+
+       if (cur_max != prev_max) {
+               INFO("Max latency for sensor[0x%x] is changed from %dms to %dms by client[%d] adding latency",
+                       get_id(), prev_max, cur_max, client_id);
+               set_batch(client_id, cur_max);
+       }
+
+       return true;
+}
+
+bool sensor_base::delete_batch(int client_id)
+{
+       unsigned int prev_max, cur_max;
+       AUTOLOCK(m_batch_info_list_mutex);
+
+       prev_max = m_batch_info_list.get_max();
+
+       if (!m_batch_info_list.delete_batch(client_id))
+               return false;
+
+       cur_max = m_batch_info_list.get_max();
+
+       if (!cur_max) {
+               INFO("No latency for sensor[0x%x] by client[%d] deleting latency, so set to default 0 ms",
+                        get_id(), client_id);
+
+               set_batch(client_id, 0);
+       } else if (cur_max != prev_max) {
+               INFO("Max latency for sensor[0x%x] is changed from %dms to %dms by client[%d] deleting latency",
+                       get_id(), prev_max, cur_max, client_id);
+
+               set_batch(client_id, cur_max);
+       }
+
+       return true;
+}
+
+unsigned int sensor_base::get_batch(int client_id)
+{
+       AUTOLOCK(m_batch_info_list_mutex);
+
+       return m_batch_info_list.get_batch(client_id);
+}
+
 void sensor_base::get_sensor_info(sensor_type_t sensor_type, sensor_info &info)
 {
        sensor_properties_s properties;
@@ -376,6 +432,11 @@ bool sensor_base::set_wakeup(int client_id, int wakeup)
        return false;
 }
 
+bool sensor_base::set_batch(int client_id, unsigned int latency)
+{
+       return false;
+}
+
 int sensor_base::send_sensorhub_data(const char* data, int data_len)
 {
        return -1;
index c4c4600..2f726f9 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <cinterval_info_list.h>
 #include <cwakeup_info_list.h>
+#include <cbatch_info_list.h>
 #include <cmutex.h>
 
 #include <common.h>
@@ -71,6 +72,10 @@ public:
        virtual bool delete_wakeup(int client_id);
        int get_wakeup(int client_id);
 
+       virtual bool add_batch(int client_id, unsigned int latency);
+       virtual bool delete_batch(int client_id);
+       unsigned int get_batch(int client_id);
+
        void get_sensor_info(sensor_type_t sensor_type, sensor_info &info);
        virtual bool get_properties(sensor_type_t sensor_type, sensor_properties_s &properties);
 
@@ -79,6 +84,7 @@ public:
 
        virtual long set_command(unsigned int cmd, long value);
        virtual bool set_wakeup(int client_id, int wakeup);
+       virtual bool set_batch(int client_id, unsigned int latency);
        virtual int send_sensorhub_data(const char* data, int data_len);
 
        virtual int get_sensor_data(unsigned int type, sensor_data_t &data);
@@ -96,8 +102,10 @@ protected:
 
        cinterval_info_list m_interval_info_list;
        cwakeup_info_list m_wakeup_info_list;
+       cbatch_info_list m_batch_info_list;
        cmutex m_interval_info_list_mutex;
        cmutex m_wakeup_info_list_mutex;
+       cmutex m_batch_info_list_mutex;
 
        cmutex m_mutex;