Merge branch 'devel/tizen' into tizen
[platform/core/system/sensord.git] / src / server / sensor_handler.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "sensor_handler.h"
21
22 #include <message.h>
23 #include <sensor_log.h>
24 #include <sensor_utils.h>
25
26 using namespace sensor;
27
28 sensor_handler::sensor_handler(const sensor_info &info)
29 : m_info(info)
30 {
31         const char *priv = sensor::utils::get_privilege(m_info.get_uri());
32         m_info.set_privilege(priv);
33
34         sensor_type_t type = sensor::utils::get_type(m_info.get_uri());
35         m_info.set_type(type);
36 }
37
38 bool sensor_handler::has_observer(sensor_observer *ob)
39 {
40         for (auto it = m_observers.begin(); it != m_observers.end(); ++it) {
41                 if ((*it) == ob)
42                         return true;
43         }
44
45         return false;
46 }
47
48 void sensor_handler::add_observer(sensor_observer *ob)
49 {
50         ret_if(has_observer(ob));
51
52         m_observers.push_back(ob);
53 }
54
55 void sensor_handler::remove_observer(sensor_observer *ob)
56 {
57         m_observers.remove(ob);
58 }
59
60 int sensor_handler::notify(const char *uri, sensor_data_t *data, int len)
61 {
62         if (observer_count() == 0)
63                 return OP_ERROR;
64
65         ipc::message *msg;
66
67         msg = new(std::nothrow) ipc::message((char *)data, len);
68         retvm_if(!msg, OP_ERROR, "Failed to allocate memory");
69
70         for (auto it = m_observers.begin(); it != m_observers.end(); ++it)
71                 (*it)->update(uri, msg);
72
73         if (msg->ref_count() == 0)
74                 msg->unref();
75
76         return OP_SUCCESS;
77 }
78
79 uint32_t sensor_handler::observer_count(void)
80 {
81         return m_observers.size();
82 }