sensord: remove useless privilege and macros
[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 <message.h>
21 #include <sensor_log.h>
22 #include "sensor_handler.h"
23
24 using namespace sensor;
25
26 bool sensor_handler::has_observer(sensor_observer *ob)
27 {
28         for (auto it = m_observers.begin(); it != m_observers.end(); ++it) {
29                 if ((*it) == ob)
30                         return true;
31         }
32
33         return false;
34 }
35
36 void sensor_handler::add_observer(sensor_observer *ob)
37 {
38         ret_if(has_observer(ob));
39
40         m_observers.push_back(ob);
41 }
42
43 void sensor_handler::remove_observer(sensor_observer *ob)
44 {
45         m_observers.remove(ob);
46 }
47
48 int sensor_handler::notify(const char *uri, sensor_data_t *data, int len)
49 {
50         if (observer_count() == 0)
51                 return OP_ERROR;
52
53         ipc::message *msg;
54
55         msg = new(std::nothrow) ipc::message((char *)data, len);
56         retvm_if(!msg, OP_ERROR, "Failed to allocate memory");
57
58         for (auto it = m_observers.begin(); it != m_observers.end(); ++it)
59                 (*it)->update(uri, msg);
60
61         if (msg->ref_count() == 0)
62                 msg->unref();
63
64         return OP_SUCCESS;
65 }
66
67 uint32_t sensor_handler::observer_count(void)
68 {
69         return m_observers.size();
70 }