Merge "sensord: delete batch latency/attribute when client is terminated unexpectedly...
[platform/core/system/sensord.git] / src / server / client_info_manager.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 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_log.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
32 client_info_manager::~client_info_manager()
33 {
34         AUTOLOCK(m_mutex);
35
36         m_clients.clear();
37 }
38
39 client_info_manager& client_info_manager::get_instance(void)
40 {
41         static client_info_manager inst;
42         return inst;
43 }
44
45 bool client_info_manager::get_registered_events(int client_id, sensor_id_t sensor_id, event_type_vector &event_vec)
46 {
47         AUTOLOCK(m_mutex);
48
49         retvm_if(m_clients.empty(), false, "client list is empty");
50
51         auto it_record = m_clients.find(client_id);
52
53         if (it_record == m_clients.end()) {
54                 _E("Client[%d] is not found", client_id);
55                 return false;
56         }
57
58         if (!it_record->second.get_registered_events(sensor_id, event_vec))
59                 return false;
60
61         return true;
62 }
63
64 bool client_info_manager::register_event(int client_id, sensor_id_t sensor_id, unsigned int event_type)
65 {
66         AUTOLOCK(m_mutex);
67
68         retvm_if(m_clients.empty(), false, "client list is empty");
69
70         auto it_record = m_clients.find(client_id);
71
72         if (it_record == m_clients.end()) {
73                 _E("Client[%d] is not found", client_id);
74                 return false;
75         }
76
77         if (!it_record->second.register_event(sensor_id, event_type))
78                 return false;
79
80         return true;
81 }
82
83 bool client_info_manager::unregister_event(int client_id, sensor_id_t sensor_id, unsigned int event_type)
84 {
85         AUTOLOCK(m_mutex);
86
87         retvm_if(m_clients.empty(), false, "client list is empty");
88
89         auto it_record = m_clients.find(client_id);
90
91         if (it_record == m_clients.end()) {
92                 _E("Client[%d] is not found", client_id);
93                 return false;
94         }
95
96         if (!it_record->second.unregister_event(sensor_id, event_type))
97                 return false;
98
99         return true;
100 }
101
102 bool client_info_manager::set_batch(int client_id, sensor_id_t sensor_id, unsigned int interval, unsigned latency)
103 {
104         AUTOLOCK(m_mutex);
105
106         retvm_if(m_clients.empty(), false, "client list is empty");
107
108         auto it_record = m_clients.find(client_id);
109
110         if (it_record == m_clients.end()) {
111                 _E("Client[%d] is not found", client_id);
112                 return false;
113         }
114
115         return it_record->second.set_batch(sensor_id, interval, latency);
116 }
117
118 bool client_info_manager::get_batch(int client_id, sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency)
119 {
120         AUTOLOCK(m_mutex);
121
122         retvm_if(m_clients.empty(), false, "client list is empty");
123
124         auto it_record = m_clients.find(client_id);
125
126         if (it_record == m_clients.end()) {
127                 _E("Client[%d] is not found", client_id);
128                 return false;
129         }
130
131         return it_record->second.get_batch(sensor_id, interval, latency);
132 }
133
134 bool client_info_manager::set_pause_policy(int client_id, sensor_id_t sensor_id, int pause_policy)
135 {
136         AUTOLOCK(m_mutex);
137
138         retvm_if(m_clients.empty(), false, "client list is empty");
139
140         auto it_record = m_clients.find(client_id);
141
142         if (it_record == m_clients.end()) {
143                 _E("Client[%d] is not found", client_id);
144                 return false;
145         }
146
147         if (!it_record->second.set_pause_policy(sensor_id, pause_policy))
148                 return false;
149
150         return true;
151 }
152
153 bool client_info_manager::set_start(int client_id, sensor_id_t sensor_id, bool start)
154 {
155         AUTOLOCK(m_mutex);
156
157         retvm_if(m_clients.empty(), false, "client list is empty");
158
159         auto it_record = m_clients.find(client_id);
160
161         if (it_record == m_clients.end()) {
162                 _E("Client[%d] is not found", client_id);
163                 return false;
164         }
165
166         if (!it_record->second.set_start(sensor_id, start))
167                 return false;
168
169         return true;
170 }
171
172 bool client_info_manager::is_started(int client_id, sensor_id_t sensor_id)
173 {
174         AUTOLOCK(m_mutex);
175
176         retvm_if(m_clients.empty(), false, "client list is empty");
177
178         auto it_record = m_clients.find(client_id);
179
180         if (it_record == m_clients.end()) {
181                 _E("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                 _E("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 bool client_info_manager::remove_client_record(int client_id)
212 {
213         AUTOLOCK(m_mutex);
214
215         retvm_if(m_clients.empty(), false, "client list is empty");
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         m_clients.erase(it_record);
225
226         _I("Client record for client[%d] is removed from client info manager", client_id);
227
228         return true;
229 }
230
231 bool client_info_manager::has_client_record(int client_id)
232 {
233         AUTOLOCK(m_mutex);
234
235         retvm_if(m_clients.empty(), false, "client list is empty");
236
237         auto it_record = m_clients.find(client_id);
238
239         return (it_record != m_clients.end());
240 }
241
242 void client_info_manager::set_client_info(int client_id, pid_t pid, const string &name)
243 {
244         AUTOLOCK(m_mutex);
245
246         auto it_record = m_clients.find(client_id);
247
248         if (it_record == m_clients.end()) {
249                 _E("Client[%d] is not found", client_id);
250                 return;
251         }
252
253         it_record->second.set_client_info(pid, name);
254
255         return;
256 }
257
258 const char* client_info_manager::get_client_info(int client_id)
259 {
260         AUTOLOCK(m_mutex);
261
262         retvm_if(m_clients.empty(), NULL, "client list is empty");
263
264         auto it_record = m_clients.find(client_id);
265
266         if (it_record == m_clients.end()) {
267                 _D("Client[%d] is not found", client_id);
268                 return NULL;
269         }
270
271         return it_record->second.get_client_info();
272 }
273
274 bool client_info_manager::set_permission(int client_id, int permission)
275 {
276         AUTOLOCK(m_mutex);
277
278         retvm_if(m_clients.empty(), false, "client list is empty");
279
280         auto it_record = m_clients.find(client_id);
281
282         if (it_record == m_clients.end()) {
283                 _D("Client[%d] is not found", client_id);
284                 return false;
285         }
286
287         it_record->second.set_permission(permission);
288         return true;
289 }
290
291 bool client_info_manager::get_permission(int client_id, int &permission)
292 {
293         AUTOLOCK(m_mutex);
294
295         retvm_if(m_clients.empty(), false, "client list is empty");
296
297         auto it_record = m_clients.find(client_id);
298
299         if (it_record == m_clients.end()) {
300                 _D("Client[%d] is not found", client_id);
301                 return false;
302         }
303
304         permission = it_record->second.get_permission();
305         return true;
306 }
307
308 bool client_info_manager::create_sensor_record(int client_id, sensor_id_t sensor_id)
309 {
310         AUTOLOCK(m_mutex);
311
312         retvm_if(m_clients.empty(), false, "client list is empty");
313
314         auto it_record = m_clients.find(client_id);
315
316         if (it_record == m_clients.end()) {
317                 _E("Client record[%d] is not registered", client_id);
318                 return false;
319         }
320
321         it_record->second.add_sensor_usage(sensor_id);
322
323         return true;
324 }
325
326 bool client_info_manager::remove_sensor_record(int client_id, sensor_id_t sensor_id)
327 {
328         AUTOLOCK(m_mutex);
329
330         retvm_if(m_clients.empty(), false, "client list is empty");
331
332         auto it_record = m_clients.find(client_id);
333
334         if (it_record == m_clients.end()) {
335                 _E("Client[%d] is not found", client_id);
336                 return false;
337         }
338
339         if (!it_record->second.remove_sensor_usage(sensor_id))
340                 return false;
341
342         if (!it_record->second.has_sensor_usage())
343                 remove_client_record(client_id);
344
345         return true;
346 }
347
348 bool client_info_manager::has_sensor_record(int client_id, sensor_id_t sensor_id)
349 {
350         AUTOLOCK(m_mutex);
351
352         retvm_if(m_clients.empty(), false, "client list is empty");
353
354         auto it_record = m_clients.find(client_id);
355
356         if (it_record == m_clients.end()) {
357                 _D("Client[%d] is not found", client_id);
358                 return false;
359         }
360
361         if (!it_record->second.has_sensor_usage(sensor_id))
362                 return false;
363
364         return true;
365 }
366
367 bool client_info_manager::has_sensor_record(int client_id)
368 {
369         AUTOLOCK(m_mutex);
370
371         retvm_if(m_clients.empty(), false, "client list is empty");
372
373         auto it_record = m_clients.find(client_id);
374
375         if (it_record == m_clients.end()) {
376                 _D("Client[%d] is not found", client_id);
377                 return false;
378         }
379
380         if (!it_record->second.has_sensor_usage())
381                 return false;
382
383         return true;
384 }
385
386 bool client_info_manager::get_listener_ids(sensor_id_t sensor_id, unsigned int event_type, client_id_vec &id_vec)
387 {
388         AUTOLOCK(m_mutex);
389
390         retvm_if(m_clients.empty(), false, "client list is empty");
391
392         auto it_record = m_clients.begin();
393
394         while (it_record != m_clients.end()) {
395                 if (it_record->second.is_listening_event(sensor_id, event_type))
396                         id_vec.push_back(it_record->first);
397
398                 ++it_record;
399         }
400
401         return true;
402 }
403
404 bool client_info_manager::get_event_socket(int client_id, csocket &socket)
405 {
406         AUTOLOCK(m_mutex);
407
408         retvm_if(m_clients.empty(), false, "client list is empty");
409
410         auto it_record = m_clients.find(client_id);
411
412         if (it_record == m_clients.end()) {
413                 _E("Client[%d] is not found", client_id);
414                 return false;
415         }
416
417         it_record->second.get_event_socket(socket);
418
419         return true;
420 }
421
422 bool client_info_manager::set_event_socket(int client_id, const csocket &socket)
423 {
424         AUTOLOCK(m_mutex);
425
426         retvm_if(m_clients.empty(), false, "client list is empty");
427
428         auto it_record = m_clients.find(client_id);
429
430         if (it_record == m_clients.end()) {
431                 _E("Client[%d] is not found", client_id);
432                 return false;
433         }
434
435         it_record->second.set_event_socket(socket);
436
437         return true;
438 }