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::set_attribute(sensor_t sensor, int attribute, int value)
120 _E("Failed to validate the parameter");
124 if (!m_cmd_channel) {
125 _E("Failed to connect to server");
131 cmd_manager_attr_int_t buf = {0, };
133 buf.attribute = attribute;
136 sensor_info *info = (sensor_info*)sensor;
137 memcpy(buf.sensor, info->get_uri().c_str(), info->get_uri().size());
139 msg.set_type(CMD_MANAGER_SET_ATTR_INT);
140 msg.enclose((char*)&buf, sizeof(buf));
142 bool ret = m_cmd_channel->send_sync(msg);
144 _E("Failed to send command to set attribute");
148 ret = m_cmd_channel->read_sync(reply);
150 _E("Failed to read reply to set attribute");
154 if (reply.header()->err < 0) {
155 _E("Failed to set attribute");
156 return reply.header()->err;
162 int sensor_manager::get_attribute(sensor_t sensor, int attribute, int *value)
164 if (!sensor || !value) {
165 _E("Failed to validate the parameters");
169 if (!m_cmd_channel) {
170 _E("Failed to connect to server");
176 cmd_manager_attr_int_t buf = {0, };
178 buf.attribute = attribute;
180 sensor_info *info = (sensor_info*)sensor;
181 memcpy(buf.sensor, info->get_uri().c_str(), info->get_uri().size());
183 msg.set_type(CMD_MANAGER_GET_ATTR_INT);
184 msg.enclose((char*)&buf, sizeof(buf));
186 bool ret = m_cmd_channel->send_sync(msg);
188 _E("Failed to send command to get attribute");
192 ret = m_cmd_channel->read_sync(reply);
194 _E("Failed to read reply to get attribute");
198 if (reply.header()->err < 0) {
199 _E("Failed to get attribute");
200 return reply.header()->err;
203 if (!reply.header()->length || !reply.body()) {
204 _E("Failed to get attribute");
208 *value = ((cmd_manager_attr_int_t *)reply.body())->value;
213 int sensor_manager::add_sensor(sensor_info &info)
215 retv_if(is_supported(info.get_uri().c_str()), OP_ERROR);
217 m_sensors.push_back(info);
219 _I("Added sensor[%s]", info.get_uri().c_str());
224 int sensor_manager::add_sensor(sensor_provider *provider)
226 retvm_if(!provider, -EINVAL, "Invalid parameter");
227 return add_sensor(*(provider->get_sensor_info()));
230 int sensor_manager::remove_sensor(const char *uri)
232 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
233 if ((*it).get_uri() == uri) {
235 _I("Removed sensor[%s]", uri);
244 int sensor_manager::remove_sensor(sensor_provider *provider)
246 retvm_if(!provider, -EINVAL, "Invalid parameter");
247 return remove_sensor(provider->get_uri());
250 void sensor_manager::add_sensor_added_cb(sensord_added_cb cb, void *user_data)
252 m_handler->add_sensor_added_cb(cb, user_data);
255 void sensor_manager::remove_sensor_added_cb(sensord_added_cb cb)
257 m_handler->remove_sensor_added_cb(cb);
260 void sensor_manager::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data)
262 m_handler->add_sensor_removed_cb(cb, user_data);
265 void sensor_manager::remove_sensor_removed_cb(sensord_removed_cb cb)
267 m_handler->remove_sensor_removed_cb(cb);
270 bool sensor_manager::init(void)
272 m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
273 retvm_if(!m_client, false, "Failed to allocate memory");
275 m_handler = new(std::nothrow) channel_handler(this);
285 void sensor_manager::deinit(void)
296 bool sensor_manager::connect_channel(void)
300 m_cmd_channel = m_client->connect(NULL);
301 retvm_if(!m_cmd_channel, false, "Failed to connect to server");
303 m_mon_channel = m_client->connect(m_handler, &m_loop);
304 retvm_if(!m_mon_channel, false, "Failed to connect to server");
306 msg.set_type(CMD_MANAGER_CONNECT);
307 m_mon_channel->send_sync(msg);
309 m_connected.store(true);
315 bool sensor_manager::connect(void)
317 retv_if(is_connected(), true);
318 retv_if(!connect_channel(), false);
320 return get_sensors_internal();
323 void sensor_manager::disconnect(void)
325 ret_if(!is_connected());
326 m_connected.store(false);
328 m_mon_channel->disconnect();
329 delete m_mon_channel;
330 m_mon_channel = NULL;
332 m_cmd_channel->disconnect();
333 delete m_cmd_channel;
334 m_cmd_channel = NULL;
339 bool sensor_manager::is_connected(void)
341 return m_connected.load();
344 void sensor_manager::restore(void)
346 ret_if(!is_connected());
348 m_cmd_channel->disconnect();
349 delete m_cmd_channel;
350 m_cmd_channel = NULL;
352 m_connected.store(false);
353 retm_if(!connect_channel(), "Failed to restore manager");
355 _D("Restored manager");
358 void sensor_manager::decode_sensors(const char *buf, std::list<sensor_info> &infos)
364 cmd_manager_sensor_list_t *raw;
366 raw = (cmd_manager_sensor_list_t *)buf;
367 count = raw->sensor_cnt;
368 size = (const int32_t *)raw->data;
369 data = (const char *)raw->data + sizeof(int32_t);
371 for (int i = 0; i < count; ++i) {
373 info.deserialize(data, size[0]);
374 infos.push_back(info);
376 size = (const int32_t *)((const char *)data + size[0]);
377 data = (const char *)size + sizeof(int32_t);
380 _D("Sensor count : %d", count);
383 bool sensor_manager::get_sensors_internal(void)
385 retvm_if(!is_connected(), false, "Failed to get sensors");
390 char buf[MAX_BUF_SIZE];
392 msg.set_type(CMD_MANAGER_SENSOR_LIST);
394 ret = m_cmd_channel->send_sync(msg);
395 retvm_if(!ret, false, "Failed to send message");
397 ret = m_cmd_channel->read_sync(reply);
398 retvm_if(!ret, false, "Failed to receive message");
400 reply.disclose(buf, MAX_BUF_SIZE);
402 if (!m_sensors.empty())
405 decode_sensors(buf, m_sensors);
410 bool sensor_manager::has_privilege(std::string &uri)
412 retvm_if(!is_connected(), false, "Failed to get sensors");
417 cmd_has_privilege_t buf = {0, };
419 msg.set_type(CMD_HAS_PRIVILEGE);
420 memcpy(buf.sensor, uri.c_str(), uri.size());
421 msg.enclose((const char *)&buf, sizeof(buf));
423 ret = m_cmd_channel->send_sync(msg);
424 retvm_if(!ret, false, "Failed to send message");
426 ret = m_cmd_channel->read_sync(reply);
427 retvm_if(!ret, false, "Failed to receive message");
429 if (reply.header()->err == OP_SUCCESS)
432 _W("This client doesn't have the privilege for sensor[%s]", uri.c_str());
437 sensor_info *sensor_manager::get_info(const char *uri)
439 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
440 return &m_sensors.front();
442 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
443 if ((*it).get_uri() != uri)
446 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
452 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
453 std::size_t found = (*it).get_uri().find_last_of("/");
454 if (found == std::string::npos)
456 if ((*it).get_uri().substr(0, found) != uri)
459 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
466 std::vector<sensor_info *> sensor_manager::get_infos(const char *uri)
468 std::vector<sensor_info *> infos;
471 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
474 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
475 if ((*it).get_uri() != uri)
478 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
479 infos.push_back(&*it);
484 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
485 std::size_t found = (*it).get_uri().find_last_of("/");
486 if (!all && found == std::string::npos)
488 if (!all && (*it).get_uri().substr(0, found) != uri)
491 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
492 infos.push_back(&*it);