4 * Copyright (c) 2013 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 <command_common.h>
21 #include <client_info_manager.h>
22 #include <sensor_log.h>
28 client_info_manager::client_info_manager()
32 client_info_manager::~client_info_manager()
37 client_info_manager& client_info_manager::get_instance()
39 static client_info_manager inst;
43 bool client_info_manager::get_registered_events(int client_id, sensor_id_t sensor_id, event_type_vector &event_vec)
47 auto it_record = m_clients.find(client_id);
49 if (it_record == m_clients.end()) {
50 _E("Client[%d] is not found", client_id);
54 if(!it_record->second.get_registered_events(sensor_id, event_vec))
60 bool client_info_manager::register_event(int client_id, sensor_id_t sensor_id, unsigned int event_type)
64 auto it_record = m_clients.find(client_id);
66 if (it_record == m_clients.end()) {
67 _E("Client[%d] is not found", client_id);
71 if(!it_record->second.register_event(sensor_id, event_type))
77 bool client_info_manager::unregister_event(int client_id, sensor_id_t sensor_id, unsigned int event_type)
81 auto it_record = m_clients.find(client_id);
83 if (it_record == m_clients.end()) {
84 _E("Client[%d] is not found", client_id);
88 if(!it_record->second.unregister_event(sensor_id, event_type))
94 bool client_info_manager::set_batch(int client_id, sensor_id_t sensor_id, unsigned int interval, unsigned latency)
98 auto it_record = m_clients.find(client_id);
100 if (it_record == m_clients.end()) {
101 _E("Client[%d] is not found", client_id);
105 return it_record->second.set_batch(sensor_id, interval, latency);
108 bool client_info_manager::get_batch(int client_id, sensor_id_t sensor_id, unsigned int &interval, unsigned int &latency)
112 auto it_record = m_clients.find(client_id);
114 if (it_record == m_clients.end()) {
115 _E("Client[%d] is not found", client_id);
119 return it_record->second.get_batch(sensor_id, interval, latency);
122 bool client_info_manager::set_option(int client_id, sensor_id_t sensor_id, int option)
126 auto it_record = m_clients.find(client_id);
128 if (it_record == m_clients.end()) {
129 _E("Client[%d] is not found", client_id);
133 if(!it_record->second.set_option(sensor_id, option))
139 bool client_info_manager::set_start(int client_id, sensor_id_t sensor_id, bool start)
143 auto it_record = m_clients.find(client_id);
145 if (it_record == m_clients.end()) {
146 _E("Client[%d] is not found", client_id);
150 if(!it_record->second.set_start(sensor_id, start))
156 bool client_info_manager::is_started(int client_id, sensor_id_t sensor_id)
160 auto it_record = m_clients.find(client_id);
162 if (it_record == m_clients.end()) {
163 _E("Client[%d] is not found", client_id);
167 return it_record->second.is_started(sensor_id);
170 int client_info_manager::create_client_record(void)
176 client_sensor_record client_record;
178 while (m_clients.count(client_id) > 0)
181 if (client_id == MAX_HANDLE) {
182 _E("Sensor records of clients are full");
183 return MAX_HANDLE_REACHED;
186 client_record.set_client_id(client_id);
188 m_clients.insert(pair<int, client_sensor_record> (client_id, client_record));
193 bool client_info_manager::remove_client_record(int client_id)
197 auto it_record = m_clients.find(client_id);
199 if (it_record == m_clients.end()) {
200 _E("Client[%d] is not found", client_id);
204 m_clients.erase(it_record);
206 _I("Client record for client[%d] is removed from client info manager", client_id);
211 bool client_info_manager::has_client_record(int client_id)
215 auto it_record = m_clients.find(client_id);
217 return (it_record != m_clients.end());
220 void client_info_manager::set_client_info(int client_id, pid_t pid, const string &name)
224 auto it_record = m_clients.find(client_id);
226 if (it_record == m_clients.end()) {
227 _E("Client[%d] is not found", client_id);
231 it_record->second.set_client_info(pid, name);
236 const char* client_info_manager::get_client_info(int client_id)
240 auto it_record = m_clients.find(client_id);
242 if (it_record == m_clients.end()) {
243 _D("Client[%d] is not found", client_id);
247 return it_record->second.get_client_info();
250 bool client_info_manager::set_permission(int client_id, int permission)
254 auto it_record = m_clients.find(client_id);
256 if (it_record == m_clients.end()) {
257 _D("Client[%d] is not found", client_id);
261 it_record->second.set_permission(permission);
265 bool client_info_manager::get_permission(int client_id, int &permission)
269 auto it_record = m_clients.find(client_id);
271 if (it_record == m_clients.end()) {
272 _D("Client[%d] is not found", client_id);
276 permission = it_record->second.get_permission();
280 bool client_info_manager::create_sensor_record(int client_id, sensor_id_t sensor_id)
284 auto it_record = m_clients.find(client_id);
286 if (it_record == m_clients.end()) {
287 _E("Client record[%d] is not registered", client_id);
291 it_record->second.add_sensor_usage(sensor_id);
296 bool client_info_manager::remove_sensor_record(int client_id, sensor_id_t sensor_id)
300 auto it_record = m_clients.find(client_id);
302 if (it_record == m_clients.end()) {
303 _E("Client[%d] is not found", client_id);
307 if(!it_record->second.remove_sensor_usage(sensor_id))
310 if(!it_record->second.has_sensor_usage())
311 remove_client_record(client_id);
316 bool client_info_manager::has_sensor_record(int client_id, sensor_id_t sensor_id)
320 auto it_record = m_clients.find(client_id);
322 if (it_record == m_clients.end()) {
323 _D("Client[%d] is not found", client_id);
327 if(!it_record->second.has_sensor_usage(sensor_id))
333 bool client_info_manager::has_sensor_record(int client_id)
337 auto it_record = m_clients.find(client_id);
339 if (it_record == m_clients.end()) {
340 _D("Client[%d] is not found", client_id);
344 if(!it_record->second.has_sensor_usage())
350 bool client_info_manager::get_listener_ids(sensor_id_t sensor_id, unsigned int event_type, client_id_vec &id_vec)
354 auto it_record = m_clients.begin();
356 while (it_record != m_clients.end()) {
357 if(it_record->second.is_listening_event(sensor_id, event_type))
358 id_vec.push_back(it_record->first);
366 bool client_info_manager::get_event_socket(int client_id, csocket &socket)
370 auto it_record = m_clients.find(client_id);
372 if (it_record == m_clients.end()) {
373 _E("Client[%d] is not found", client_id);
377 it_record->second.get_event_socket(socket);
382 bool client_info_manager::set_event_socket(int client_id, const csocket &socket)
386 auto it_record = m_clients.find(client_id);
388 if (it_record == m_clients.end()) {
389 _E("Client[%d] is not found", client_id);
393 it_record->second.set_event_socket(socket);