sensord: clean up sf_common.h/sensor_common.h/sensor_logs.h
[platform/core/system/sensord.git] / src / server / client_info_manager.cpp
1 /*
2  * libsensord-share
3  *
4  * Copyright (c) 2014 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 <client_info_manager.h>
22 #include <sensor_logs.h>
23 #include <csocket.h>
24
25 using std::pair;
26 using std::string;
27
28 client_info_manager::client_info_manager()
29 {
30 }
31 client_info_manager::~client_info_manager()
32 {
33         m_clients.clear();
34 }
35
36 client_info_manager& client_info_manager::get_instance()
37 {
38         static client_info_manager inst;
39         return inst;
40 }
41
42 bool client_info_manager::get_registered_events(int client_id, sensor_id_t sensor_id, event_type_vector &event_vec)
43 {
44         AUTOLOCK(m_mutex);
45
46         auto it_record = m_clients.find(client_id);
47
48         if (it_record == m_clients.end()) {
49                 ERR("Client[%d] is not found", client_id);
50                 return false;
51         }
52
53         if(!it_record->second.get_registered_events(sensor_id, event_vec))
54                 return false;
55
56         return true;
57 }
58
59
60 bool client_info_manager::register_event(int client_id, sensor_id_t sensor_id, unsigned int event_type)
61 {
62         AUTOLOCK(m_mutex);
63
64         auto it_record = m_clients.find(client_id);
65
66         if (it_record == m_clients.end()) {
67                 ERR("Client[%d] is not found", client_id);
68                 return false;
69         }
70
71         if(!it_record->second.register_event(sensor_id, event_type))
72                 return false;
73
74         return true;
75 }
76
77 bool client_info_manager::unregister_event(int client_id, sensor_id_t sensor_id, unsigned int event_type)
78 {
79         AUTOLOCK(m_mutex);
80
81         auto it_record = m_clients.find(client_id);
82
83         if (it_record == m_clients.end()) {
84                 ERR("Client[%d] is not found", client_id);
85                 return false;
86         }
87
88         if(!it_record->second.unregister_event(sensor_id, event_type))
89                 return false;
90
91         return true;
92 }
93
94 bool client_info_manager::set_batch(int client_id, sensor_id_t sensor_id, unsigned int interval, unsigned latency)
95 {
96         AUTOLOCK(m_mutex);
97
98         auto it_record = m_clients.find(client_id);
99
100         if (it_record == m_clients.end()) {
101                 ERR("Client[%d] is not found", client_id);
102                 return false;
103         }
104
105         return it_record->second.set_batch(sensor_id, interval, latency);
106 }
107
108 bool client_info_manager::get_batch(int client_id, sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency)
109 {
110         AUTOLOCK(m_mutex);
111
112         auto it_record = m_clients.find(client_id);
113
114         if (it_record == m_clients.end()) {
115                 ERR("Client[%d] is not found", client_id);
116                 return false;
117         }
118
119         return it_record->second.get_batch(sensor_id, interval, latency);
120 }
121
122 bool client_info_manager::set_option(int client_id, sensor_id_t sensor_id, int option)
123 {
124         AUTOLOCK(m_mutex);
125
126         auto it_record = m_clients.find(client_id);
127
128         if (it_record == m_clients.end()) {
129                 ERR("Client[%d] is not found", client_id);
130                 return false;
131         }
132
133         if(!it_record->second.set_option(sensor_id, option))
134                 return false;
135
136         return true;
137 }
138
139 bool client_info_manager::set_wakeup(int client_id, sensor_id_t sensor_id, int wakeup)
140 {
141         AUTOLOCK(m_mutex);
142
143         auto it_record = m_clients.find(client_id);
144
145         if (it_record == m_clients.end()) {
146                 ERR("Client[%d] is not found", client_id);
147                 return false;
148         }
149
150         if(!it_record->second.set_wakeup(sensor_id, wakeup))
151                 return false;
152
153         return true;
154 }
155
156 bool client_info_manager::set_start(int client_id, sensor_id_t sensor_id, bool start)
157 {
158         AUTOLOCK(m_mutex);
159
160         auto it_record = m_clients.find(client_id);
161
162         if (it_record == m_clients.end()) {
163                 ERR("Client[%d] is not found", client_id);
164                 return false;
165         }
166
167         if(!it_record->second.set_start(sensor_id, start))
168                 return false;
169
170         return true;
171
172 }
173
174 bool client_info_manager::is_started(int client_id, sensor_id_t sensor_id)
175 {
176         AUTOLOCK(m_mutex);
177
178         auto it_record = m_clients.find(client_id);
179
180         if (it_record == m_clients.end()) {
181                 ERR("Client[%d] is not found", client_id);
182                 return false;
183         }
184
185         return it_record->second.is_started(sensor_id);
186 }
187
188 int client_info_manager::create_client_record(void)
189 {
190         AUTOLOCK(m_mutex);
191
192         int client_id = 0;
193
194         client_sensor_record client_record;
195
196         while (m_clients.count(client_id) > 0)
197                 client_id++;
198
199         if (client_id == MAX_HANDLE) {
200                 ERR("Sensor records of clients are full");
201                 return MAX_HANDLE_REACHED;
202         }
203
204         client_record.set_client_id(client_id);
205
206         m_clients.insert(pair<int,client_sensor_record> (client_id, client_record));
207
208         return client_id;
209 }
210
211
212 bool client_info_manager::remove_client_record(int client_id)
213 {
214         AUTOLOCK(m_mutex);
215
216         auto it_record = m_clients.find(client_id);
217
218         if (it_record == m_clients.end()) {
219                 ERR("Client[%d] is not found", client_id);
220                 return false;
221         }
222
223         m_clients.erase(it_record);
224
225         INFO("Client record for client[%d] is removed from client info manager", client_id);
226
227         return true;
228 }
229
230
231 bool client_info_manager::has_client_record(int client_id)
232 {
233         AUTOLOCK(m_mutex);
234
235         auto it_record = m_clients.find(client_id);
236
237         return (it_record != m_clients.end());
238 }
239
240
241 void client_info_manager::set_client_info(int client_id, pid_t pid, const string &name)
242 {
243         AUTOLOCK(m_mutex);
244
245         auto it_record = m_clients.find(client_id);
246
247         if (it_record == m_clients.end()) {
248                 ERR("Client[%d] is not found", client_id);
249                 return;
250         }
251
252         it_record->second.set_client_info(pid, name);
253
254         return;
255 }
256
257 const char* client_info_manager::get_client_info(int client_id)
258 {
259         AUTOLOCK(m_mutex);
260
261         auto it_record = m_clients.find(client_id);
262
263         if (it_record == m_clients.end()) {
264                 DBG("Client[%d] is not found", client_id);
265                 return NULL;
266         }
267
268         return it_record->second.get_client_info();
269 }
270
271 bool client_info_manager::set_permission(int client_id, int permission)
272 {
273         AUTOLOCK(m_mutex);
274
275         auto it_record = m_clients.find(client_id);
276
277         if (it_record == m_clients.end()) {
278                 DBG("Client[%d] is not found", client_id);
279                 return false;
280         }
281
282         it_record->second.set_permission(permission);
283         return true;
284 }
285
286 bool client_info_manager::get_permission(int client_id, int &permission)
287 {
288         AUTOLOCK(m_mutex);
289
290         auto it_record = m_clients.find(client_id);
291
292         if (it_record == m_clients.end()) {
293                 DBG("Client[%d] is not found", client_id);
294                 return false;
295         }
296
297         permission = it_record->second.get_permission();
298         return true;
299 }
300
301 bool client_info_manager::create_sensor_record(int client_id, sensor_id_t sensor_id)
302 {
303         AUTOLOCK(m_mutex);
304
305         auto it_record = m_clients.find(client_id);
306
307         if (it_record == m_clients.end()) {
308                 ERR("Client record[%d] is not registered", client_id);
309                 return false;
310         }
311
312         it_record->second.add_sensor_usage(sensor_id);
313
314         return true;
315 }
316
317 bool client_info_manager::remove_sensor_record(int client_id, sensor_id_t sensor_id)
318 {
319         AUTOLOCK(m_mutex);
320
321         auto it_record = m_clients.find(client_id);
322
323         if (it_record == m_clients.end()) {
324                 ERR("Client[%d] is not found", client_id);
325                 return false;
326         }
327
328         if(!it_record->second.remove_sensor_usage(sensor_id))
329                 return false;
330
331         if(!it_record->second.has_sensor_usage())
332                 remove_client_record(client_id);
333
334         return true;
335 }
336
337
338 bool client_info_manager::has_sensor_record(int client_id, sensor_id_t sensor_id)
339 {
340         AUTOLOCK(m_mutex);
341
342         auto it_record = m_clients.find(client_id);
343
344         if (it_record == m_clients.end()) {
345                 DBG("Client[%d] is not found", client_id);
346                 return false;
347         }
348
349         if(!it_record->second.has_sensor_usage(sensor_id))
350                 return false;
351
352         return true;
353 }
354
355 bool client_info_manager::has_sensor_record(int client_id)
356 {
357         AUTOLOCK(m_mutex);
358
359         auto it_record = m_clients.find(client_id);
360
361         if (it_record == m_clients.end()) {
362                 DBG("Client[%d] is not found", client_id);
363                 return false;
364         }
365
366         if(!it_record->second.has_sensor_usage())
367                 return false;
368
369         return true;
370 }
371
372 bool client_info_manager::get_listener_ids(sensor_id_t sensor_id, unsigned int event_type, client_id_vec &id_vec)
373 {
374         AUTOLOCK(m_mutex);
375
376         auto it_record = m_clients.begin();
377
378         while (it_record != m_clients.end()) {
379                 if(it_record->second.is_listening_event(sensor_id, event_type))
380                         id_vec.push_back(it_record->first);
381
382                 ++it_record;
383         }
384
385         return true;
386 }
387
388 bool client_info_manager::get_event_socket(int client_id, csocket &socket)
389 {
390         AUTOLOCK(m_mutex);
391
392         auto it_record = m_clients.find(client_id);
393
394         if (it_record == m_clients.end()) {
395                 ERR("Client[%d] is not found", client_id);
396                 return false;
397         }
398
399         it_record->second.get_event_socket(socket);
400
401         return true;
402 }
403
404 bool client_info_manager::set_event_socket(int client_id, const csocket &socket)
405 {
406
407         AUTOLOCK(m_mutex);
408
409         auto it_record = m_clients.find(client_id);
410
411         if (it_record == m_clients.end()) {
412                 ERR("Client[%d] is not found", client_id);
413                 return false;
414         }
415
416         it_record->second.set_event_socket(socket);
417
418         return true;
419 }