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)) {
56 sensor_info *info = get_info(uri);
57 retv_if(!info, -EACCES);
59 *sensor = (sensor_t)info;
63 int sensor_manager::get_sensors(const char *uri, sensor_t **list, int *count)
65 retv_if(!is_supported(uri), -ENODATA);
67 std::vector<sensor_info *> infos;
70 infos = get_infos(uri);
72 retv_if(size == 0, -EACCES);
74 *list = (sensor_t *)malloc(sizeof(sensor_info *) * size);
75 retvm_if(!*list, -ENOMEM, "Failed to allocate memory");
77 for (int i = 0; i < size; ++i)
78 *(*list + i) = infos[i];
84 bool sensor_manager::is_supported(sensor_t sensor)
86 retvm_if(!sensor, false, "Invalid sensor");
88 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
96 bool sensor_manager::is_supported(const char *uri)
98 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
101 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
102 if ((*it).get_uri() == uri)
105 std::size_t found = (*it).get_uri().find_last_of("/");
106 if (found == std::string::npos)
109 if ((*it).get_uri().substr(0, found) == uri)
116 int sensor_manager::add_sensor(sensor_info &info)
118 retv_if(is_supported(info.get_uri().c_str()), OP_ERROR);
120 m_sensors.push_back(info);
122 _I("Added sensor[%s]", info.get_uri().c_str());
127 int sensor_manager::add_sensor(sensor_provider *provider)
129 retvm_if(!provider, -EINVAL, "Invalid parameter");
130 return add_sensor(*(provider->get_sensor_info()));
133 int sensor_manager::remove_sensor(const char *uri)
135 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
136 if ((*it).get_uri() == uri) {
137 _I("Removing sensor[%s]", (*it).get_uri().c_str());
138 it = m_sensors.erase(it);
147 int sensor_manager::remove_sensor(sensor_provider *provider)
149 retvm_if(!provider, -EINVAL, "Invalid parameter");
150 return remove_sensor(provider->get_uri());
153 void sensor_manager::add_sensor_added_cb(sensord_added_cb cb, void *user_data)
155 m_handler->add_sensor_added_cb(cb, user_data);
158 void sensor_manager::remove_sensor_added_cb(sensord_added_cb cb)
160 m_handler->remove_sensor_added_cb(cb);
163 void sensor_manager::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data)
165 m_handler->add_sensor_removed_cb(cb, user_data);
168 void sensor_manager::remove_sensor_removed_cb(sensord_removed_cb cb)
170 m_handler->remove_sensor_removed_cb(cb);
173 bool sensor_manager::init(void)
175 m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH);
176 retvm_if(!m_client, false, "Failed to allocate memory");
178 m_handler = new(std::nothrow) channel_handler(this);
188 void sensor_manager::deinit(void)
199 bool sensor_manager::connect_channel(void)
203 m_cmd_channel = m_client->connect(NULL);
204 retvm_if(!m_cmd_channel, false, "Failed to connect to server");
206 m_mon_channel = m_client->connect(m_handler, &m_loop);
207 retvm_if(!m_mon_channel, false, "Failed to connect to server");
209 msg.set_type(CMD_MANAGER_CONNECT);
210 m_mon_channel->send_sync(&msg);
212 m_connected.store(true);
218 bool sensor_manager::connect(void)
220 retv_if(is_connected(), true);
221 retv_if(!connect_channel(), false);
223 return get_sensors_internal();
226 void sensor_manager::disconnect(void)
228 ret_if(!is_connected());
229 m_connected.store(false);
231 m_mon_channel->disconnect();
232 delete m_mon_channel;
233 m_mon_channel = NULL;
235 m_cmd_channel->disconnect();
236 delete m_cmd_channel;
237 m_cmd_channel = NULL;
242 bool sensor_manager::is_connected(void)
244 return m_connected.load();
247 void sensor_manager::restore(void)
249 ret_if(!is_connected());
251 m_connected.store(false);
252 retm_if(!connect_channel(), "Failed to restore manager");
254 _D("Restored manager");
257 void sensor_manager::decode_sensors(const char *buf, std::vector<sensor_info> &infos)
263 cmd_manager_sensor_list_t *raw;
265 raw = (cmd_manager_sensor_list_t *)buf;
266 count = raw->sensor_cnt;
267 size = (const int32_t *)raw->data;
268 data = (const char *)raw->data + sizeof(int32_t);
270 for (int i = 0; i < count; ++i) {
272 info.deserialize(data, size[0]);
273 infos.push_back(info);
275 size = (const int32_t *)((const char *)data + size[0]);
276 data = (const char *)size + sizeof(int32_t);
279 _D("Sensor count : %d", count);
282 bool sensor_manager::get_sensors_internal(void)
284 retvm_if(!is_connected(), false, "Failed to get sensors");
289 char buf[MAX_BUF_SIZE];
291 msg.set_type(CMD_MANAGER_SENSOR_LIST);
293 ret = m_cmd_channel->send_sync(&msg);
294 retvm_if(!ret, false, "Failed to send message");
296 ret = m_cmd_channel->read_sync(reply);
297 retvm_if(!ret, false, "Failed to receive message");
301 decode_sensors(buf, m_sensors);
306 bool sensor_manager::has_privilege(std::string &uri)
308 retvm_if(!is_connected(), false, "Failed to get sensors");
313 cmd_has_privilege_t buf = {0, };
315 msg.set_type(CMD_HAS_PRIVILEGE);
316 memcpy(buf.sensor, uri.c_str(), uri.size());
317 msg.enclose((const char *)&buf, sizeof(buf));
319 ret = m_cmd_channel->send_sync(&msg);
320 retvm_if(!ret, false, "Failed to send message");
322 ret = m_cmd_channel->read_sync(reply);
323 retvm_if(!ret, false, "Failed to receive message");
325 if (reply.header()->err == OP_SUCCESS)
328 _W("This client doesn't have the privilege for sensor[%s]", uri.c_str());
333 sensor_info *sensor_manager::get_info(const char *uri)
335 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
336 return &m_sensors[0];
338 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
339 if ((*it).get_uri() != uri)
342 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
348 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
349 std::size_t found = (*it).get_uri().find_last_of("/");
350 if (found == std::string::npos)
352 if ((*it).get_uri().substr(0, found) != uri)
355 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
362 std::vector<sensor_info *> sensor_manager::get_infos(const char *uri)
364 std::vector<sensor_info *> infos;
367 if (strncmp(uri, utils::get_uri(ALL_SENSOR), strlen(utils::get_uri(ALL_SENSOR))) == 0)
370 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
371 if ((*it).get_uri() != uri)
374 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
375 infos.push_back(&*it);
380 for (auto it = m_sensors.begin(); it != m_sensors.end(); ++it) {
381 std::size_t found = (*it).get_uri().find_last_of("/");
382 if (!all && found == std::string::npos)
384 if (!all && (*it).get_uri().substr(0, found) != uri)
387 if ((*it).get_privilege().empty() || has_privilege((*it).get_uri()))
388 infos.push_back(&*it);