4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "sensor_handler.h"
23 #include <sensor_log.h>
24 #include <sensor_utils.h>
25 #include <sensor_types_private.h>
27 using namespace sensor;
29 sensor_handler::sensor_handler(const sensor_info &info)
32 const char *priv = sensor::utils::get_privilege(m_info.get_uri());
33 m_info.set_privilege(priv);
35 sensor_type_t type = sensor::utils::get_type(m_info.get_uri());
36 m_info.set_type(type);
38 /* TODO: temporary walkaround for sensors that require multiple privileges */
39 switch (m_info.get_type()) {
40 case EXTERNAL_EXERCISE_SENSOR:
41 case EXERCISE_STANDALONE_SENSOR:
42 m_info.add_privilege(PRIVILEGE_LOCATION_URI);
49 bool sensor_handler::has_observer(sensor_observer *ob)
51 for (auto it = m_observers.begin(); it != m_observers.end(); ++it) {
59 void sensor_handler::add_observer(sensor_observer *ob)
61 ret_if(has_observer(ob));
63 m_observers.push_back(ob);
66 void sensor_handler::remove_observer(sensor_observer *ob)
68 m_observers.remove(ob);
71 int sensor_handler::notify(const char *uri, sensor_data_t *data, int len)
73 if (observer_count() == 0)
78 msg = new(std::nothrow) ipc::message((char *)data, len);
79 retvm_if(!msg, OP_ERROR, "Failed to allocate memory");
81 for (auto it = m_observers.begin(); it != m_observers.end(); ++it)
82 (*it)->update(uri, msg);
84 if (msg->ref_count() == 0)
90 uint32_t sensor_handler::observer_count(void)
92 return m_observers.size();