Coverity issues Fix
[platform/core/system/sensord.git] / src / client / sensor_manager_channel_handler.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "sensor_manager_channel_handler.h"
21
22 #include <sensor_log.h>
23 #include <command_types.h>
24 #include "sensor_manager.h"
25
26 using namespace sensor;
27
28 sensor_manager::channel_handler::channel_handler(sensor_manager *manager)
29 : m_manager(manager)
30 {
31 }
32
33 void sensor_manager::channel_handler::connected(ipc::channel *ch)
34 {
35 }
36
37 void sensor_manager::channel_handler::disconnected(ipc::channel *ch)
38 {
39         /* If channel->disconnect() is not explicitly called, it will be restored */
40         m_manager->restore();
41 }
42
43 void sensor_manager::channel_handler::read(ipc::channel *ch, ipc::message &msg)
44 {
45         switch (msg.header()->type) {
46         case CMD_MANAGER_SENSOR_ADDED:
47                 on_sensor_added(ch, msg);
48                 break;
49         case CMD_MANAGER_SENSOR_REMOVED:
50                 on_sensor_removed(ch, msg);
51                 break;
52         }
53 }
54
55 void sensor_manager::channel_handler::read_complete(ipc::channel *ch)
56 {
57 }
58
59 void sensor_manager::channel_handler::error_caught(ipc::channel *ch, int error)
60 {
61 }
62
63 void sensor_manager::channel_handler::on_sensor_added(ipc::channel *ch, ipc::message &msg)
64 {
65         ret_if(msg.header()->err < OP_SUCCESS);
66
67         sensor_info info;
68         info.clear();
69         info.deserialize(msg.body(), msg.size());
70
71         m_manager->add_sensor(info);
72
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);
76                 ++it;
77         }
78 }
79
80 void sensor_manager::channel_handler::on_sensor_removed(ipc::channel *ch, ipc::message &msg)
81 {
82         ret_if(msg.header()->err < 0);
83         char uri[NAME_MAX] = {0, };
84
85         msg.disclose(uri);
86         m_manager->remove_sensor(uri);
87
88         auto it = m_sensor_removed_callbacks.begin();
89         while (it != m_sensor_removed_callbacks.end()) {
90                 it->first(uri, it->second);
91                 ++it;
92         }
93 }
94
95 void sensor_manager::channel_handler::add_sensor_added_cb(sensord_added_cb cb, void *user_data)
96 {
97         m_sensor_added_callbacks.emplace(cb, user_data);
98 }
99
100 void sensor_manager::channel_handler::remove_sensor_added_cb(sensord_added_cb cb)
101 {
102         m_sensor_added_callbacks.erase(cb);
103 }
104
105 void sensor_manager::channel_handler::add_sensor_removed_cb(sensord_removed_cb cb, void *user_data)
106 {
107         m_sensor_removed_callbacks.emplace(cb, user_data);
108 }
109
110 void sensor_manager::channel_handler::remove_sensor_removed_cb(sensord_removed_cb cb)
111 {
112         m_sensor_removed_callbacks.erase(cb);
113 }