sensord: implement sensor_handler class 98/123598/1
authorkibak.yoon <kibak.yoon@samsung.com>
Thu, 6 Apr 2017 07:45:06 +0000 (16:45 +0900)
committerkibak.yoon <kibak.yoon@samsung.com>
Thu, 6 Apr 2017 07:47:01 +0000 (16:47 +0900)
- sensor_handler has:
  - a common logic of publisher
  - sensor control interface

Change-Id: Ifbc32db62b96996d01041f6b7855b07ae07115b0
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
src/server/sensor_handler.cpp [new file with mode: 0644]
src/server/sensor_handler.h [new file with mode: 0644]

diff --git a/src/server/sensor_handler.cpp b/src/server/sensor_handler.cpp
new file mode 100644 (file)
index 0000000..e567c22
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 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 <message.h>
+#include <sensor_log.h>
+#include "sensor_handler.h"
+
+using namespace sensor;
+
+bool sensor_handler::has_observer(sensor_observer *ob)
+{
+       for (auto it = m_observers.begin(); it != m_observers.end(); ++it) {
+               if ((*it) == ob)
+                       return true;
+       }
+
+       return false;
+}
+
+void sensor_handler::add_observer(sensor_observer *ob)
+{
+       ret_if(has_observer(ob));
+
+       m_observers.push_back(ob);
+}
+
+void sensor_handler::remove_observer(sensor_observer *ob)
+{
+       m_observers.remove(ob);
+}
+
+int sensor_handler::notify(const char *uri, sensor_data_t *data, int len)
+{
+       if (observer_count() == 0)
+               return OP_ERROR;
+
+       ipc::message *msg;
+
+       msg = new(std::nothrow) ipc::message((char *)data, len);
+       retvm_if(!msg, OP_ERROR, "Failed to allocate memory");
+
+       for (auto it = m_observers.begin(); it != m_observers.end(); ++it)
+               (*it)->update(uri, msg);
+
+       if (msg->ref_count() == 0)
+               msg->unref();
+
+       return OP_SUCCESS;
+}
+
+uint32_t sensor_handler::observer_count(void)
+{
+       return m_observers.size();
+}
diff --git a/src/server/sensor_handler.h b/src/server/sensor_handler.h
new file mode 100644 (file)
index 0000000..68672ab
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 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 __SENSOR_HANDLER_H__
+#define __SENSOR_HANDLER_H__
+
+#include <sensor_observer.h>
+#include <sensor_publisher.h>
+#include <sensor_types.h>
+#include <sensor_info.h>
+#include <list>
+
+namespace sensor {
+
+class sensor_handler : public sensor_publisher {
+public:
+       virtual ~sensor_handler() {}
+
+       /* publisher */
+       bool has_observer(sensor_observer *ob);
+       void add_observer(sensor_observer *ob);
+       void remove_observer(sensor_observer *ob);
+       int notify(const char *type, sensor_data_t *data, int len);
+       uint32_t observer_count(void);
+
+       virtual const sensor_info &get_sensor_info(void) = 0;
+
+       virtual int start(sensor_observer *ob) = 0;
+       virtual int stop(sensor_observer *ob) = 0;
+
+       virtual int set_interval(sensor_observer *ob, int32_t interval) = 0;
+       virtual int set_batch_latency(sensor_observer *ob, int32_t latency) = 0;
+       virtual int set_attribute(sensor_observer *ob, int32_t attr, int32_t value) = 0;
+       virtual int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len) = 0;
+       virtual int get_data(sensor_data_t **data, int *len) = 0;
+       virtual int flush(void) = 0;
+
+private:
+       std::list<sensor_observer *> m_observers;
+};
+
+}
+
+#endif /* __SENSOR_HANDLER_H__ */