sensord: fix incorrect return type
[platform/core/system/sensord.git] / src / server / external_client_manager.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2015 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 <command_common.h>
21 #include <external_client_manager.h>
22 #include <sensor_common.h>
23
24 using std::shared_ptr;
25 using std::make_shared;
26 using std::pair;
27 using std::string;
28
29 external_client_manager::external_client_manager()
30 {
31 }
32 external_client_manager::~external_client_manager()
33 {
34 }
35
36 external_client_manager& external_client_manager::get_instance(void)
37 {
38         static external_client_manager instance;
39         return instance;
40 }
41
42 int external_client_manager::create_client_record(void)
43 {
44         AUTOLOCK(m_mutex);
45
46         int client_id = 0;
47
48         shared_ptr<external_sensor_record> client_record = make_shared<external_sensor_record>();
49
50         while (m_clients.count(client_id) > 0)
51                 client_id++;
52
53         if (client_id == MAX_HANDLE) {
54                 _E("Sensor records of clients are full");
55                 return MAX_HANDLE_REACHED;
56         }
57
58         client_record->set_client_id(client_id);
59
60         m_clients.insert(pair<int, shared_ptr<external_sensor_record>>(client_id, client_record));
61
62         return client_id;
63 }
64
65 bool external_client_manager::remove_client_record(int client_id)
66 {
67         AUTOLOCK(m_mutex);
68
69         if (!m_clients.erase(client_id)) {
70                 _E("Client[%d] is not found", client_id);
71                 return false;
72         }
73
74         _I("Client record for client[%d] is removed from external client manager", client_id);
75         return true;
76 }
77
78 bool external_client_manager::has_client_record(int client_id)
79 {
80         AUTOLOCK(m_mutex);
81
82         auto it_record = m_clients.find(client_id);
83
84         return (it_record != m_clients.end());
85 }
86
87 void external_client_manager::set_client_info(int client_id, pid_t pid, const string &name)
88 {
89         AUTOLOCK(m_mutex);
90
91         auto it_record = m_clients.find(client_id);
92
93         if (it_record == m_clients.end()) {
94                 _E("Client[%d] is not found", client_id);
95                 return;
96         }
97
98         it_record->second->set_client_info(pid, name);
99
100         return;
101 }
102
103 const char* external_client_manager::get_client_info(int client_id)
104 {
105         AUTOLOCK(m_mutex);
106
107         auto it_record = m_clients.find(client_id);
108
109         if (it_record == m_clients.end()) {
110                 _D("Client[%d] is not found", client_id);
111                 return NULL;
112         }
113
114         return it_record->second->get_client_info();
115 }
116
117 bool external_client_manager::create_sensor_record(int client_id, sensor_id_t sensor)
118 {
119         AUTOLOCK(m_mutex);
120
121         auto it_record = m_clients.find(client_id);
122
123         if (it_record == m_clients.end()) {
124                 _E("Client record[%d] is not registered", client_id);
125                 return false;
126         }
127
128         return it_record->second->add_usage(sensor);
129 }
130
131 bool external_client_manager::remove_sensor_record(int client_id, sensor_id_t sensor)
132 {
133         AUTOLOCK(m_mutex);
134
135         auto it_record = m_clients.find(client_id);
136
137         if (it_record == m_clients.end()) {
138                 _E("Client[%d] is not found", client_id);
139                 return false;
140         }
141
142         if (!it_record->second->remove_usage(sensor))
143                 return false;
144
145         if (!it_record->second->has_usage())
146                 remove_client_record(client_id);
147
148         return true;
149 }
150
151 bool external_client_manager::has_sensor_record(int client_id, sensor_id_t sensor)
152 {
153         AUTOLOCK(m_mutex);
154
155         auto it_record = m_clients.find(client_id);
156
157         if (it_record == m_clients.end()) {
158                 _D("Client[%d] is not found", client_id);
159                 return false;
160         }
161
162         return it_record->second->has_usage(sensor);
163 }
164
165 bool external_client_manager::has_sensor_record(int client_id)
166 {
167         AUTOLOCK(m_mutex);
168
169         auto it_record = m_clients.find(client_id);
170
171         if (it_record == m_clients.end()) {
172                 _D("Client[%d] is not found", client_id);
173                 return false;
174         }
175
176         return it_record->second->has_usage();
177 }
178
179 bool external_client_manager::get_listener_socket(sensor_id_t sensor, csocket &sock)
180 {
181         AUTOLOCK(m_mutex);
182
183         auto it_record = m_clients.begin();
184
185         while (it_record != m_clients.end()) {
186                 if (it_record->second->has_usage(sensor)) {
187                         it_record->second->get_command_socket(sock);
188                         return true;
189                 }
190
191                 ++it_record;
192         }
193
194         return false;
195 }
196
197 bool external_client_manager::get_command_socket(int client_id, csocket &socket)
198 {
199         AUTOLOCK(m_mutex);
200
201         auto it_record = m_clients.find(client_id);
202
203         if (it_record == m_clients.end()) {
204                 _E("Client[%d] is not found", client_id);
205                 return false;
206         }
207
208         it_record->second->get_command_socket(socket);
209
210         return true;
211 }
212
213 bool external_client_manager::set_command_socket(int client_id, const csocket &socket)
214 {
215         AUTOLOCK(m_mutex);
216
217         auto it_record = m_clients.find(client_id);
218
219         if (it_record == m_clients.end()) {
220                 _E("Client[%d] is not found", client_id);
221                 return false;
222         }
223
224         it_record->second->set_command_socket(socket);
225
226         return true;
227 }