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_channel_handler.h"
22 #include <sensor_log.h>
23 #include <command_types.h>
24 #include "sensor_manager.h"
26 using namespace sensor;
28 sensor_manager::channel_handler::channel_handler(sensor_manager *manager)
33 void sensor_manager::channel_handler::connected(ipc::channel *ch)
37 void sensor_manager::channel_handler::disconnected(ipc::channel *ch)
39 /* If channel->disconnect() is not explicitly called, it will be restored */
43 void sensor_manager::channel_handler::read(ipc::channel *ch, ipc::message &msg)
45 switch (msg.header()->type) {
46 case CMD_MANAGER_SENSOR_ADDED:
47 on_sensor_added(ch, msg);
49 case CMD_MANAGER_SENSOR_REMOVED:
50 on_sensor_removed(ch, msg);
55 void sensor_manager::channel_handler::read_complete(ipc::channel *ch)
59 void sensor_manager::channel_handler::error_caught(ipc::channel *ch, int error)
63 void sensor_manager::channel_handler::on_sensor_added(ipc::channel *ch, ipc::message &msg)
65 ret_if(msg.header()->err < OP_SUCCESS);
69 info.deserialize(msg.body(), msg.size());
71 m_manager->add_sensor(info);
73 auto it = m_sensor_added_callbacks.begin();
74 while (it != m_sensor_added_callbacks.end()) {
75 it->first(info.get_uri().c_str(), it->second);
80 void sensor_manager::channel_handler::on_sensor_removed(ipc::channel *ch, ipc::message &msg)
82 ret_if(msg.header()->err < 0);
83 char uri[NAME_MAX] = {0, };
86 m_manager->remove_sensor(uri);
88 auto it = m_sensor_removed_callbacks.begin();
89 while (it != m_sensor_removed_callbacks.end()) {
90 it->first(uri, it->second);
95 void sensor_manager::channel_handler::add_sensor_added_cb(sensord_added_cb cb, void *user_data)
97 m_sensor_added_callbacks.emplace(cb, user_data);
100 void sensor_manager::channel_handler::remove_sensor_added_cb(sensord_added_cb cb)
102 m_sensor_added_callbacks.erase(cb);
105 void sensor_manager::channel_handler::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data)
107 m_sensor_removed_callbacks.emplace(cb, user_data);
110 void sensor_manager::channel_handler::remove_sensor_removed_cb(sensord_removed_cb cb)
112 m_sensor_removed_callbacks.erase(cb);