Adding AK8975 geo-sensor info in sensors.xml.in required by geo-plugin
[platform/core/system/sensord.git] / src / shared / csensor_event_dispatcher.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 <csensor_event_dispatcher.h>
21 #include <sensor_plugin_loader.h>
22 #include <common.h>
23 #include <sf_common.h>
24 #include <vconf.h>
25 #include <thread>
26
27 using std::thread;
28
29 #define MAX_PENDING_CONNECTION 32
30
31 csensor_event_dispatcher::csensor_event_dispatcher()
32 : m_lcd_on(true)
33 {
34         m_sensor_fusion = sensor_plugin_loader::get_instance().get_fusion();
35 }
36
37 csensor_event_dispatcher::~csensor_event_dispatcher() { }
38
39 bool csensor_event_dispatcher::run(void)
40 {
41         INFO("Starting Event Dispatcher");
42
43         if (!m_accept_socket.create(SOCK_SEQPACKET)) {
44                 ERR("Listener Socket Creation failed in Server");
45                 return false;
46         }
47
48         if (!m_accept_socket.bind(EVENT_CHANNEL_PATH)) {
49                 ERR("Listener Socket Binding failed in Server");
50                 m_accept_socket.close();
51                 return false;
52         }
53
54         if (!m_accept_socket.listen(MAX_PENDING_CONNECTION)) {
55                 ERR("Socket Listen failed in Server");
56                 return false;
57         }
58
59         thread accepter(&csensor_event_dispatcher::accept_connections, this);
60         accepter.detach();
61
62         thread dispatcher(&csensor_event_dispatcher::dispatch_event, this);
63         dispatcher.detach();
64
65         return true;
66 }
67
68 void csensor_event_dispatcher::accept_event_channel(csocket client_socket)
69 {
70         int client_id;
71         event_channel_ready_t event_channel_ready;
72         cclient_info_manager &client_info_manager = get_client_info_manager();
73         client_socket.set_connection_mode();
74
75         if (client_socket.recv(&client_id, sizeof(client_id)) <= 0) {
76                 ERR("Failed to receive client id on socket fd[%d]", client_socket.get_socket_fd());
77                 return;
78         }
79
80         client_socket.set_transfer_mode();
81         AUTOLOCK(m_mutex);
82
83         if (!get_client_info_manager().set_event_socket(client_id, client_socket)) {
84                 ERR("Failed to store event socket[%d] for %s", client_socket.get_socket_fd(),
85                         client_info_manager.get_client_info(client_id));
86                 return;
87         }
88
89         event_channel_ready.magic = EVENT_CHANNEL_MAGIC;
90         event_channel_ready.client_id = client_id;
91
92         INFO("Event channel is accepted for %s on socket[%d]",
93                  client_info_manager.get_client_info(client_id), client_socket.get_socket_fd());
94
95         if (client_socket.send(&event_channel_ready, sizeof(event_channel_ready)) <= 0) {
96                 ERR("Failed to send event_channel_ready packet to %s on socket fd[%d]",
97                         client_info_manager.get_client_info(client_id), client_socket.get_socket_fd());
98                 return;
99         }
100 }
101
102 void csensor_event_dispatcher::accept_connections(void)
103 {
104         INFO("Event channel acceptor is started.");
105
106         while (true) {
107                 csocket client_socket;
108
109                 if (!m_accept_socket.accept(client_socket)) {
110                         ERR("Accepting socket failed in Server");
111                         continue;
112                 }
113
114                 INFO("New client connected (socket_fd : %d)", client_socket.get_socket_fd());
115                 thread event_channel_creator(&csensor_event_dispatcher::accept_event_channel, this, client_socket);
116                 event_channel_creator.detach();
117         }
118 }
119
120 void csensor_event_dispatcher::dispatch_event(void)
121 {
122         const int MAX_EVENT_PER_SENSOR = 16;
123         const int MAX_SENSOR_EVENT = 1 + (sensor_plugin_loader::get_instance().get_virtual_sensors().size()
124                                                                         * MAX_EVENT_PER_SENSOR);
125         const int MAX_SYNTH_PER_SENSOR = 5;
126         INFO("Event Dispatcher started");
127         m_lcd_on = is_lcd_on();
128
129         if (vconf_notify_key_changed(VCONFKEY_PM_STATE, situation_watcher, this) != 0)
130                 ERR("Fail to set notify callback for %s", VCONFKEY_PM_STATE);
131
132         while (true) {
133                 bool is_hub_event = false;
134                 event_situation situation;
135                 void *seed_event = get_event_queue().pop();
136                 unsigned int event_type = *((unsigned int *)(seed_event));
137
138                 if (is_sensorhub_event(event_type))
139                         is_hub_event = true;
140
141                 if (m_lcd_on)
142                         situation = SITUATION_LCD_ON;
143                 else
144                         situation = SITUATION_LCD_OFF;
145
146                 if (is_hub_event) {
147                         sensorhub_event_t *sensorhub_event = (sensorhub_event_t *)seed_event;
148                         sensorhub_event->situation = situation;
149                         send_sensor_events(sensorhub_event, 1, true, situation);
150                 } else {
151                         sensor_event_t sensor_events[MAX_SENSOR_EVENT];
152                         unsigned int event_cnt = 0;
153                         sensor_events[event_cnt++] = *((sensor_event_t *)seed_event);
154
155                         if (m_sensor_fusion) {
156                                 if (m_sensor_fusion->is_started())
157                                         m_sensor_fusion->fuse(*((sensor_event_t *)seed_event));
158                         }
159
160                         virtual_sensors v_sensors = get_active_virtual_sensors();
161                         virtual_sensors::iterator it_v_sensor;
162                         it_v_sensor = v_sensors.begin();
163                         vector<sensor_event_t> v_sensor_events;
164                         v_sensor_events.reserve(MAX_SYNTH_PER_SENSOR);
165
166                         while (it_v_sensor != v_sensors.end()) {
167                                 int synthesized_cnt;
168                                 v_sensor_events.clear();
169                                 (*it_v_sensor)->synthesize(*((sensor_event_t *)seed_event), v_sensor_events);
170                                 synthesized_cnt = v_sensor_events.size();
171
172                                 for (int i = 0; i < synthesized_cnt; ++i)
173                                         sensor_events[event_cnt++] = v_sensor_events[i];
174
175                                 ++it_v_sensor;
176                         }
177
178                         sort_sensor_events(sensor_events, event_cnt);
179
180                         for (int i = 0; i < event_cnt; ++i) {
181                                 sensor_events[i].situation = situation;
182
183                                 if (is_record_event(sensor_events[i].event_type))
184                                         put_last_event(sensor_events[i].event_type, sensor_events[i]);
185                         }
186
187                         send_sensor_events(sensor_events, event_cnt, false, situation);
188                 }
189
190                 if (is_hub_event)
191                         delete (sensorhub_event_t *)seed_event;
192                 else
193                         delete (sensor_event_t *)seed_event;
194         }
195 }
196
197 void csensor_event_dispatcher::send_sensor_events(void *events, int event_cnt, bool is_hub_event, event_situation situation)
198 {
199         sensor_event_t *sensor_events;
200         sensorhub_event_t *sensor_hub_events;
201         cclient_info_manager &client_info_manager = get_client_info_manager();
202
203         if (is_hub_event)
204                 sensor_hub_events = (sensorhub_event_t *)events;
205         else
206                 sensor_events = (sensor_event_t *)events;
207
208         for (int i = 0; i < event_cnt; ++i) {
209                 client_id_vec id_vec;
210                 unsigned int event_type;
211
212                 if (is_hub_event)
213                         event_type = sensor_hub_events[i].event_type;
214                 else
215                         event_type = sensor_events[i].event_type;
216
217                 client_info_manager.get_listener_ids(event_type, situation, id_vec);
218                 client_id_vec::iterator it_client_id;
219                 it_client_id = id_vec.begin();
220
221                 while (it_client_id != id_vec.end()) {
222                         csocket client_socket;
223                         client_info_manager.get_event_socket(*it_client_id, client_socket);
224                         bool ret;
225
226                         if (is_hub_event)
227                                 ret = (client_socket.send(sensor_hub_events + i, sizeof(sensorhub_event_t)) > 0);
228                         else
229                                 ret = (client_socket.send(sensor_events + i, sizeof(sensor_event_t)) > 0);
230
231                         if (ret)
232                                 DBG("Event[0x%x] sent to %s on socket[%d]", event_type, client_info_manager.get_client_info(*it_client_id), client_socket.get_socket_fd());
233                         else
234                                 ERR("Failed to send event[0x%x] to %s on socket[%d]", event_type, client_info_manager.get_client_info(*it_client_id), client_socket.get_socket_fd());
235
236                         ++it_client_id;
237                 }
238         }
239 }
240
241 bool csensor_event_dispatcher::is_lcd_on(void)
242 {
243         int lcd_state;
244
245         if (vconf_get_int(VCONFKEY_PM_STATE, &lcd_state) != 0) {
246                 ERR("Can't get the value of VCONFKEY_PM_STATE");
247                 return true;
248         }
249
250         if (lcd_state == VCONFKEY_PM_STATE_LCDOFF)
251                 return false;
252
253         return true;
254 }
255
256 cclient_info_manager &csensor_event_dispatcher::get_client_info_manager(void)
257 {
258         return cclient_info_manager::get_instance();
259 }
260
261 csensor_event_queue &csensor_event_dispatcher::get_event_queue(void)
262 {
263         return csensor_event_queue::get_instance();
264 }
265
266 bool csensor_event_dispatcher::is_record_event(unsigned int event_type)
267 {
268         switch (event_type) {
269         case ACCELEROMETER_EVENT_ROTATION_CHECK:
270                 return true;
271                 break;
272         default:
273                 break;
274         }
275
276         return false;
277 }
278
279 void csensor_event_dispatcher::put_last_event(unsigned int event_type, const sensor_event_t &event)
280 {
281         AUTOLOCK(m_last_events_mutex);
282         m_last_events[event_type] = event;
283 }
284
285 bool csensor_event_dispatcher::get_last_event(unsigned int event_type, sensor_event_t &event)
286 {
287         AUTOLOCK(m_last_events_mutex);
288         event_type_last_event_map::iterator it_event;
289         it_event = m_last_events.find(event_type);
290
291         if (it_event == m_last_events.end())
292                 return false;
293
294         event = it_event->second;
295         return true;
296 }
297
298 bool csensor_event_dispatcher::has_active_virtual_sensor(virtual_sensor *sensor)
299 {
300         AUTOLOCK(m_active_virtual_sensors_mutex);
301         virtual_sensors::iterator it_v_sensor;
302         it_v_sensor = find(m_active_virtual_sensors.begin(), m_active_virtual_sensors.end(), sensor);
303         return (it_v_sensor != m_active_virtual_sensors.end());
304 }
305
306 virtual_sensors csensor_event_dispatcher::get_active_virtual_sensors(void)
307 {
308         AUTOLOCK(m_active_virtual_sensors_mutex);
309         return m_active_virtual_sensors;
310 }
311
312 bool csensor_event_dispatcher::compare_by_timestamp(const sensor_event_t &a, const sensor_event_t &b)
313 {
314         return a.data.timestamp < b.data.timestamp;
315 }
316
317 void csensor_event_dispatcher::sort_sensor_events(sensor_event_t *events, unsigned int cnt)
318 {
319         std::sort(events, events + cnt, compare_by_timestamp);
320 }
321
322 void csensor_event_dispatcher::request_last_event(int client_id, const sensor_type_t sensor)
323 {
324         cclient_info_manager &client_info_manager = get_client_info_manager();
325         event_type_vector event_vec;
326         csocket client_socket;
327
328         if (client_info_manager.get_registered_events(client_id, sensor, event_vec)) {
329                 client_info_manager.get_event_socket(client_id, client_socket);
330                 event_type_vector::iterator it_event;
331                 it_event = event_vec.begin();
332
333                 while (it_event != event_vec.end()) {
334                         sensor_event_t event;
335
336                         if (is_record_event(*it_event) && get_last_event(*it_event, event)) {
337                                 if (client_socket.send(&event, sizeof(event)) > 0)
338                                         INFO("Send the last event[0x%x] to %s on socket[%d]", event.event_type,
339                                                 client_info_manager.get_client_info(client_id), client_socket.get_socket_fd());
340                                 else
341                                         ERR("Failed to send event[0x%x] to %s on socket[%d]", event.event_type,
342                                                 client_info_manager.get_client_info(client_id), client_socket.get_socket_fd());
343                         }
344
345                         ++it_event;
346                 }
347         }
348 }
349
350 bool csensor_event_dispatcher::add_active_virtual_sensor(virtual_sensor *sensor)
351 {
352         AUTOLOCK(m_active_virtual_sensors_mutex);
353
354         if (has_active_virtual_sensor(sensor)) {
355                 ERR("[%s] sensor is already added on active virtual sensors", sensor->get_name());
356                 return false;
357         }
358
359         m_active_virtual_sensors.push_back(sensor);
360         return true;
361 }
362
363 bool csensor_event_dispatcher::delete_active_virtual_sensor(virtual_sensor *sensor)
364 {
365         AUTOLOCK(m_active_virtual_sensors_mutex);
366         virtual_sensors::iterator it_v_sensor;
367         it_v_sensor = find(m_active_virtual_sensors.begin(), m_active_virtual_sensors.end(), sensor);
368
369         if (it_v_sensor == m_active_virtual_sensors.end()) {
370                 ERR("Fail to delete non-existent [%s] sensor on active virtual sensors", sensor->get_name());
371                 return false;
372         }
373
374         m_active_virtual_sensors.erase(it_v_sensor);
375         return true;
376 }
377
378 void csensor_event_dispatcher::situation_watcher(keynode_t *node, void *user_data)
379 {
380         csensor_event_dispatcher *dispatcher = (csensor_event_dispatcher *) user_data;
381
382         if (!strcmp(vconf_keynode_get_name(node), VCONFKEY_PM_STATE))
383                 dispatcher->m_lcd_on = dispatcher->is_lcd_on();
384 }