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_manager.h"
22 #include <sensor_log.h>
23 #include <sensor_info.h>
24 #include <sensor_utils.h>
25 #include <command_types.h>
26 #include <ipc_client.h>
30 #include "sensor_manager_channel_handler.h"
32 using namespace sensor;
34 sensor_manager::sensor_manager()
44 sensor_manager::~sensor_manager()
49 int sensor_manager::get_sensor(const char *uri, sensor_t *sensor)
51 if (!is_supported(uri)) {
53 _D("Not supported URI [%s]\n", uri);
57 sensor_info *info = get_info(uri);
58 retvm_if(!info, -EACCES, "There is no accessible sensor for uri[%s]", uri);
60 *sensor = (sensor_t)info;
64 int sensor_manager::get_sensors(const char *uri, sensor_t **list, int *count)
66 retv_if(!is_supported(uri), -ENODATA);
68 std::vector<sensor_info *> infos;
71 infos = get_infos(uri);
73 retvm_if(size == 0, -EACCES, "There is no accessible sensors for uri[%s]", uri);
75 *list = (sensor_t *)malloc(sizeof(sensor_info *) * size);
76 retvm_if(!*list, -ENOMEM, "Failed to allocate memory");
78 for (int i = 0; i < size; ++i)
79 *(*list + i) = infos[i];
85 bool sensor_manager::is_supported(sensor_t sensor)
87 retvm_if(!sensor, false, "Invalid sensor");
89 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
97 bool sensor_manager::is_supported(const char *uri)
99 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
102 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
103 if ((*it).get_uri() == uri)
106 std::size_t found = (*it).get_uri().find_last_of("/");
107 if (found == std::string::npos)
110 if ((*it).get_uri().substr(0, found) == uri)
117 int sensor_manager::add_sensor(sensor_info &info)
119 retv_if(is_supported(info.get_uri().c_str()), OP_ERROR);
121 m_sensors.push_back(info);
123 _I("Added sensor[%s]", info.get_uri().c_str());
128 int sensor_manager::add_sensor(sensor_provider *provider)
130 retvm_if(!provider, -EINVAL, "Invalid parameter");
131 return add_sensor(*(provider->get_sensor_info()));
134 int sensor_manager::remove_sensor(const char *uri)
136 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
137 if ((*it).get_uri() == uri) {
139 _I("Removed sensor[%s]", uri);
148 int sensor_manager::remove_sensor(sensor_provider *provider)
150 retvm_if(!provider, -EINVAL, "Invalid parameter");
151 return remove_sensor(provider->get_uri());
154 void sensor_manager::add_sensor_added_cb(sensord_added_cb cb, void *user_data)
156 m_handler->add_sensor_added_cb(cb, user_data);
159 void sensor_manager::remove_sensor_added_cb(sensord_added_cb cb)
161 m_handler->remove_sensor_added_cb(cb);
164 void sensor_manager::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data)
166 m_handler->add_sensor_removed_cb(cb, user_data);
169 void sensor_manager::remove_sensor_removed_cb(sensord_removed_cb cb)
171 m_handler->remove_sensor_removed_cb(cb);
174 bool sensor_manager::init(void)
176 m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
177 retvm_if(!m_client, false, "Failed to allocate memory");
179 m_handler = new(std::nothrow) channel_handler(this);
189 void sensor_manager::deinit(void)
200 bool sensor_manager::connect_channel(void)
204 m_cmd_channel = m_client->connect(NULL);
205 retvm_if(!m_cmd_channel, false, "Failed to connect to server");
207 m_mon_channel = m_client->connect(m_handler, &m_loop);
208 retvm_if(!m_mon_channel, false, "Failed to connect to server");
210 msg.set_type(CMD_MANAGER_CONNECT);
211 m_mon_channel->send_sync(msg);
213 m_connected.store(true);
219 bool sensor_manager::connect(void)
221 retv_if(is_connected(), true);
222 retv_if(!connect_channel(), false);
224 return get_sensors_internal();
227 void sensor_manager::disconnect(void)
229 ret_if(!is_connected());
230 m_connected.store(false);
232 m_mon_channel->disconnect();
233 delete m_mon_channel;
234 m_mon_channel = NULL;
236 m_cmd_channel->disconnect();
237 delete m_cmd_channel;
238 m_cmd_channel = NULL;
243 bool sensor_manager::is_connected(void)
245 return m_connected.load();
248 void sensor_manager::restore(void)
250 ret_if(!is_connected());
252 m_cmd_channel->disconnect();
253 delete m_cmd_channel;
254 m_cmd_channel = NULL;
256 m_connected.store(false);
257 retm_if(!connect_channel(), "Failed to restore manager");
259 _D("Restored manager");
262 void sensor_manager::decode_sensors(const char *buf, std::list<sensor_info> &infos)
268 cmd_manager_sensor_list_t *raw;
270 raw = (cmd_manager_sensor_list_t *)buf;
271 count = raw->sensor_cnt;
272 size = (const int32_t *)raw->data;
273 data = (const char *)raw->data + sizeof(int32_t);
275 for (int i = 0; i < count; ++i) {
277 info.deserialize(data, size[0]);
278 infos.push_back(info);
280 size = (const int32_t *)((const char *)data + size[0]);
281 data = (const char *)size + sizeof(int32_t);
284 _D("Sensor count : %d", count);
287 bool sensor_manager::get_sensors_internal(void)
289 retvm_if(!is_connected(), false, "Failed to get sensors");
294 char buf[MAX_BUF_SIZE];
296 msg.set_type(CMD_MANAGER_SENSOR_LIST);
298 ret = m_cmd_channel->send_sync(msg);
299 retvm_if(!ret, false, "Failed to send message");
301 ret = m_cmd_channel->read_sync(reply);
302 retvm_if(!ret, false, "Failed to receive message");
304 reply.disclose(buf, MAX_BUF_SIZE);
306 if (!m_sensors.empty())
309 decode_sensors(buf, m_sensors);
314 bool sensor_manager::has_privilege(std::string &uri)
316 retvm_if(!is_connected(), false, "Failed to get sensors");
321 cmd_has_privilege_t buf = {0, };
323 msg.set_type(CMD_HAS_PRIVILEGE);
324 memcpy(buf.sensor, uri.c_str(), uri.size());
325 msg.enclose((const char *)&buf, sizeof(buf));
327 ret = m_cmd_channel->send_sync(msg);
328 retvm_if(!ret, false, "Failed to send message");
330 ret = m_cmd_channel->read_sync(reply);
331 retvm_if(!ret, false, "Failed to receive message");
333 if (reply.header()->err == OP_SUCCESS)
336 _W("This client doesn't have the privilege for sensor[%s]", uri.c_str());
341 sensor_info *sensor_manager::get_info(const char *uri)
343 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
344 return &m_sensors.front();
346 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
347 if ((*it).get_uri() != uri)
350 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
356 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
357 std::size_t found = (*it).get_uri().find_last_of("/");
358 if (found == std::string::npos)
360 if ((*it).get_uri().substr(0, found) != uri)
363 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
370 std::vector<sensor_info *> sensor_manager::get_infos(const char *uri)
372 std::vector<sensor_info *> infos;
375 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
378 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
379 if ((*it).get_uri() != uri)
382 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
383 infos.push_back(&*it);
388 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
389 std::size_t found = (*it).get_uri().find_last_of("/");
390 if (!all && found == std::string::npos)
392 if (!all && (*it).get_uri().substr(0, found) != uri)
395 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
396 infos.push_back(&*it);