Updating event representation for rotation vector virtual sensor
[platform/core/system/sensord.git] / src / server / command_worker.cpp
1 /*
2  * sensord
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_worker.h>
21 #include <sensor_plugin_loader.h>
22 #include <sensor_info.h>
23 #include <sensor_accel.h>
24 #include <sensor_gyro.h>
25 #include <sensor_geomag.h>
26 #include <sensor_orientation.h>
27 #include <sensor_linear_accel.h>
28 #include <sensor_gravity.h>
29 #include <thread>
30 #include <string>
31 #include <utility>
32 #include <permission_checker.h>
33 #include <set>
34
35 using std::string;
36 using std::make_pair;
37 using std::set;
38
39 command_worker::cmd_handler_t command_worker::m_cmd_handlers[];
40 sensor_raw_data_map command_worker::m_sensor_raw_data_map;
41 cpacket command_worker::m_sensor_list;
42
43 set<unsigned int> priority_list;
44
45 command_worker::command_worker(const csocket& socket)
46 : m_client_id(CLIENT_ID_INVALID)
47 , m_permission(SENSOR_PERMISSION_NONE)
48 , m_socket(socket)
49 , m_module(NULL)
50 {
51         static bool init = false;
52
53         if (!init) {
54                 init_cmd_handlers();
55                 make_sensor_raw_data_map();
56
57                 init = true;
58         }
59
60         m_worker.set_context(this);
61         m_worker.set_working(working);
62         m_worker.set_stopped(stopped);
63 }
64
65 command_worker::~command_worker()
66 {
67         m_socket.close();
68 }
69
70
71 bool command_worker::start(void)
72 {
73         return m_worker.start();
74 }
75
76 void command_worker::init_cmd_handlers(void)
77 {
78         m_cmd_handlers[CMD_GET_ID]                              = &command_worker::cmd_get_id;
79         m_cmd_handlers[CMD_GET_SENSOR_LIST]             = &command_worker::cmd_get_sensor_list;
80         m_cmd_handlers[CMD_HELLO]                               = &command_worker::cmd_hello;
81         m_cmd_handlers[CMD_BYEBYE]                              = &command_worker::cmd_byebye;
82         m_cmd_handlers[CMD_START]                               = &command_worker::cmd_start;
83         m_cmd_handlers[CMD_STOP]                                = &command_worker::cmd_stop;
84         m_cmd_handlers[CMD_REG]                                 = &command_worker::cmd_register_event;
85         m_cmd_handlers[CMD_UNREG]                               = &command_worker::cmd_unregister_event;
86         m_cmd_handlers[CMD_SET_OPTION]                  = &command_worker::cmd_set_option;
87         m_cmd_handlers[CMD_SET_INTERVAL]                = &command_worker::cmd_set_interval;
88         m_cmd_handlers[CMD_UNSET_INTERVAL]              = &command_worker::cmd_unset_interval;
89         m_cmd_handlers[CMD_SET_COMMAND]                 = &command_worker::cmd_set_command;
90         m_cmd_handlers[CMD_GET_DATA]                    = &command_worker::cmd_get_data;
91         m_cmd_handlers[CMD_SEND_SENSORHUB_DATA] = &command_worker::cmd_send_sensorhub_data;
92 }
93
94 void command_worker::get_sensor_list(int permissions, cpacket &sensor_list)
95 {
96         const int PERMISSION_COUNT = sizeof(permissions) * 8;
97         vector<raw_data_t *> sensor_raw_vec;
98         size_t total_raw_data_size = 0;
99
100         for (int i = 0; i < PERMISSION_COUNT; ++i) {
101                 int perm = (permissions & (1 << i));
102
103                 if (perm) {
104                         auto range = m_sensor_raw_data_map.equal_range(perm);
105
106                         sensor_raw_data_map::iterator it_raw_data;
107
108                         for (it_raw_data = range.first; it_raw_data != range.second; ++it_raw_data) {
109                                 total_raw_data_size += it_raw_data->second.size();
110                                 sensor_raw_vec.push_back(&(it_raw_data->second));
111                         }
112                 }
113         }
114
115         int sensor_cnt;
116
117         sensor_cnt = sensor_raw_vec.size();
118
119         sensor_list.set_payload_size(sizeof(cmd_get_sensor_list_done_t) + (sizeof(size_t) * sensor_cnt) + total_raw_data_size);
120         sensor_list.set_cmd(CMD_GET_SENSOR_LIST);
121
122         cmd_get_sensor_list_done_t *cmd_get_sensor_list_done;
123
124         cmd_get_sensor_list_done = (cmd_get_sensor_list_done_t*)sensor_list.data();
125         cmd_get_sensor_list_done->sensor_cnt = sensor_cnt;
126         size_t* size_field = (size_t *) cmd_get_sensor_list_done->data;
127
128
129         for (int i = 0; i < sensor_cnt; ++i)
130                 size_field[i] = sensor_raw_vec[i]->size();
131
132         char* raw_data_field = cmd_get_sensor_list_done->data + (sizeof(size_t) * sensor_cnt);
133
134         int idx = 0;
135         for (int i = 0; i < sensor_cnt; ++i) {
136                 copy(sensor_raw_vec[i]->begin(), sensor_raw_vec[i]->end(), raw_data_field + idx);
137                 idx += sensor_raw_vec[i]->size();
138         }
139
140 }
141
142 void command_worker::make_sensor_raw_data_map(void)
143 {
144         vector<sensor_base *> sensors;
145         sensor_info info;
146         int permission;
147
148         sensors = sensor_plugin_loader::get_instance().get_sensors(ALL_SENSOR);
149
150         auto it_sensor = sensors.begin();
151
152         while (it_sensor != sensors.end()) {
153                 (*it_sensor)->get_sensor_info(info);
154                 permission = (*it_sensor)->get_permission();
155
156                 sensor_raw_data_map::iterator it_sensor_raw_data;
157                 it_sensor_raw_data = m_sensor_raw_data_map.insert(std::make_pair(permission, raw_data_t()));
158
159                 info.get_raw_data(it_sensor_raw_data->second);
160                 info.clear();
161                 ++it_sensor;
162         }
163 }
164
165 bool command_worker::working(void *ctx)
166 {
167         bool ret;
168         command_worker *inst = (command_worker *)ctx;
169
170         packet_header header;
171         char *payload;
172
173         if (inst->m_socket.recv(&header, sizeof(header)) <= 0) {
174                 string info;
175                 inst->get_info(info);
176                 DBG("%s failed to receive header", info.c_str());
177                 return false;
178         }
179
180         if (header.size > 0) {
181
182                 payload = new(std::nothrow) char[header.size];
183                 retvm_if(!payload, false, "Failed to allocate memory");
184
185                 if (inst->m_socket.recv(payload, header.size) <= 0) {
186                         string info;
187                         inst->get_info(info);
188                         DBG("%s failed to receive data of packet", info.c_str());
189                         delete[] payload;
190                         return false;
191                 }
192         } else {
193                 payload = NULL;
194         }
195
196         ret = inst->dispatch_command(header.cmd, payload);
197
198         if (payload)
199                 delete[] payload;
200
201         return ret;
202 }
203
204
205 bool command_worker::stopped(void *ctx)
206 {
207         string info;
208         event_type_vector event_vec;
209         command_worker *inst = (command_worker *)ctx;
210
211         inst->get_info(info);
212         INFO("%s is stopped", info.c_str());
213
214         if ((inst->m_module) && (inst->m_client_id != CLIENT_ID_INVALID)) {
215
216                 get_client_info_manager().get_registered_events(inst->m_client_id, inst->m_module->get_id(), event_vec);
217
218                 auto it_event = event_vec.begin();
219
220                 while (it_event != event_vec.end()) {
221                         WARN("Does not unregister event[0x%x] before connection broken for [%s]!!", *it_event, inst->m_module->get_name());
222                         if (!inst->m_module->delete_client(*it_event))
223                                 ERR("Unregistering event[0x%x] failed", *it_event);
224
225                         ++it_event;
226                 }
227
228                 if (get_client_info_manager().is_started(inst->m_client_id, inst->m_module->get_id())) {
229                         WARN("Does not receive cmd_stop before connection broken for [%s]!!", inst->m_module->get_name());
230                         inst->m_module->delete_interval(inst->m_client_id, false);
231                         inst->m_module->stop();
232                 }
233
234                 if (inst->m_module->get_id()) {
235                         if (get_client_info_manager().has_sensor_record(inst->m_client_id, inst->m_module->get_id())) {
236                                 INFO("Removing sensor[0x%x] record for client_id[%d]", inst->m_module->get_id(), inst->m_client_id);
237                                 get_client_info_manager().remove_sensor_record(inst->m_client_id, inst->m_module->get_id());
238                         }
239                 }
240         }
241
242         delete inst;
243         return true;
244 }
245
246 bool command_worker::dispatch_command(int cmd, void* payload)
247 {
248         int ret = false;
249
250         if (!(cmd > 0 && cmd < CMD_CNT)) {
251                 ERR("Unknown command: %d", cmd);
252         } else {
253                 cmd_handler_t cmd_handler;
254                 cmd_handler = command_worker::m_cmd_handlers[cmd];
255                 if (cmd_handler)
256                         ret = (this->*cmd_handler)(payload);
257         }
258
259         return ret;
260 }
261
262 bool command_worker::send_cmd_done(long value)
263 {
264         cpacket* ret_packet;
265         cmd_done_t *cmd_done;
266
267         ret_packet = new(std::nothrow) cpacket(sizeof(cmd_done_t));
268         retvm_if(!ret_packet, false, "Failed to allocate memory");
269
270         ret_packet->set_cmd(CMD_DONE);
271
272         cmd_done = (cmd_done_t*)ret_packet->data();
273         cmd_done->value = value;
274
275         if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) {
276                 ERR("Failed to send a cmd_done to client_id [%d] with value [%ld]", m_client_id, value);
277                 delete ret_packet;
278                 return false;
279         }
280
281         delete ret_packet;
282         return true;
283
284 }
285
286
287 bool command_worker::send_cmd_get_id_done(int client_id)
288 {
289         cpacket* ret_packet;
290         cmd_get_id_done_t *cmd_get_id_done;
291
292         ret_packet = new(std::nothrow) cpacket(sizeof(cmd_get_id_done_t));
293         retvm_if(!ret_packet, false, "Failed to allocate memory");
294
295         ret_packet->set_cmd(CMD_GET_ID);
296
297         cmd_get_id_done = (cmd_get_id_done_t*)ret_packet->data();
298         cmd_get_id_done->client_id = client_id;
299
300         if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) {
301                 ERR("Failed to send a cmd_get_id_done with client_id [%d]", client_id);
302                 delete ret_packet;
303                 return false;
304         }
305
306         delete ret_packet;
307         return true;
308 }
309
310 bool command_worker::send_cmd_get_data_done(int state, sensor_data_t *data)
311 {
312
313         cpacket* ret_packet;
314         cmd_get_data_done_t *cmd_get_data_done;
315
316         ret_packet = new(std::nothrow) cpacket(sizeof(cmd_get_data_done_t));
317         retvm_if(!ret_packet, false, "Failed to allocate memory");
318
319         ret_packet->set_cmd(CMD_GET_DATA);
320
321         cmd_get_data_done = (cmd_get_data_done_t*)ret_packet->data();
322         cmd_get_data_done->state = state;
323
324         memcpy(&cmd_get_data_done->base_data , data, sizeof(sensor_data_t));
325
326         if (m_socket.send(ret_packet->packet(), ret_packet->size()) <= 0) {
327                 ERR("Failed to send a cmd_get_data_done");
328                 delete ret_packet;
329                 return false;
330         }
331
332         delete ret_packet;
333         return true;
334 }
335
336
337 bool command_worker::send_cmd_get_sensor_list_done(void)
338 {
339         cpacket sensor_list;
340
341         int permission = get_permission();
342
343         INFO("permission = 0x%x", permission);
344
345         get_sensor_list(permission, sensor_list);
346
347         if (m_socket.send(sensor_list.packet(), sensor_list.size()) <= 0) {
348                 ERR("Failed to send a cmd_get_sensor_list_done");
349                 return false;
350         }
351
352         return true;
353 }
354
355 bool command_worker::cmd_get_id(void *payload)
356 {
357         cmd_get_id_t *cmd;
358         int client_id;
359
360         DBG("CMD_GET_ID Handler invoked\n");
361         cmd = (cmd_get_id_t*)payload;
362
363         client_id = get_client_info_manager().create_client_record();
364         get_client_info_manager().set_client_info(client_id, cmd->pid);
365
366         m_permission = get_permission();
367         get_client_info_manager().set_permission(client_id, m_permission);
368
369         INFO("New client id [%d] created", client_id);
370
371         if (!send_cmd_get_id_done(client_id))
372                 ERR("Failed to send cmd_done to a client");
373
374         return true;
375 }
376
377
378 bool command_worker::cmd_get_sensor_list(void *payload)
379 {
380         DBG("CMD_GET_SENSOR_LIST Handler invoked\n");
381
382         if (!send_cmd_get_sensor_list_done())
383                 ERR("Failed to send cmd_get_sensor_list_done to a client");
384
385         return true;
386 }
387
388 bool command_worker::cmd_hello(void *payload)
389 {
390         cmd_hello_t *cmd;
391         long ret_value = OP_ERROR;
392
393         DBG("CMD_HELLO Handler invoked\n");
394         cmd = (cmd_hello_t*)payload;
395
396         m_client_id = cmd->client_id;
397
398         if (m_permission == SENSOR_PERMISSION_NONE)
399                 get_client_info_manager().get_permission(m_client_id, m_permission);
400
401         m_module = (sensor_base *)sensor_plugin_loader::get_instance().get_sensor(cmd->sensor);
402
403         if (!m_module) {
404                 ERR("Sensor type[%d] is not supported", cmd->sensor);
405                 if (!get_client_info_manager().has_sensor_record(m_client_id))
406                         get_client_info_manager().remove_client_record(m_client_id);
407
408                 ret_value = OP_ERROR;
409                 goto out;
410         }
411
412         if (!is_permission_allowed()) {
413                 ERR("Permission denied to connect sensor[0x%x] for client [%d]", m_module->get_id(), m_client_id);
414                 ret_value = OP_ERROR;
415                 goto out;
416         }
417
418         DBG("Hello sensor [0x%x], client id [%d]", m_module->get_id(), m_client_id);
419         get_client_info_manager().create_sensor_record(m_client_id, m_module->get_id());
420         INFO("New sensor record created for sensor [0x%x], sensor name [%s] on client id [%d]\n", m_module->get_id(), m_module->get_name(), m_client_id);
421         ret_value = OP_SUCCESS;
422 out:
423         if (!send_cmd_done(ret_value))
424                 ERR("Failed to send cmd_done to a client");
425
426         return true;
427 }
428
429 bool command_worker::cmd_byebye(void *payload)
430 {
431         long ret_value = OP_ERROR;
432
433         if (!is_permission_allowed()) {
434                 ERR("Permission denied to stop sensor[0x%x] for client [%d]", m_module? m_module->get_id() : -1, m_client_id);
435                 ret_value = OP_ERROR;
436                 goto out;
437         }
438
439         DBG("CMD_BYEBYE for client [%d], sensor [0x%x]", m_client_id, m_module->get_id());
440
441         if (!get_client_info_manager().remove_sensor_record(m_client_id, m_module->get_id())) {
442                 ERR("Error removing sensor_record for client [%d]", m_client_id);
443                 ret_value = OP_ERROR;
444                 goto out;
445         }
446
447         m_client_id = CLIENT_ID_INVALID;
448         ret_value = OP_SUCCESS;
449
450 out:
451         if (!send_cmd_done(ret_value))
452                 ERR("Failed to send cmd_done to a client");
453
454         if (ret_value == OP_SUCCESS)
455                 return false;
456
457         return true;
458 }
459
460 bool command_worker::cmd_start(void *payload)
461 {
462         long ret_value = OP_ERROR;
463
464         if (!is_permission_allowed()) {
465                 ERR("Permission denied to start sensor[0x%x] for client [%d]", m_module? m_module->get_id() : -1, m_client_id);
466                 ret_value = OP_ERROR;
467                 goto out;
468         }
469
470         DBG("START Sensor [0x%x], called from client [%d]", m_module->get_id(), m_client_id);
471
472         if (m_module->start()) {
473                 get_client_info_manager().set_start(m_client_id, m_module->get_id(), true);
474 /*
475  *      Rotation could be changed even LCD is off by pop sync rotation
476  *      and a client listening rotation event with always-on option.
477  *      To reflect the last rotation state, request it to event dispatcher.
478  */
479                 get_event_dispathcher().request_last_event(m_client_id, m_module->get_id());
480                 ret_value = OP_SUCCESS;
481         } else {
482                 ERR("Failed to start sensor [0x%x] for client [%d]", m_module->get_id(), m_client_id);
483                 ret_value = OP_ERROR;
484         }
485
486 out:
487         if (!send_cmd_done(ret_value))
488                 ERR("Failed to send cmd_done to a client");
489
490         return true;
491 }
492
493 bool command_worker::cmd_stop(void *payload)
494 {
495         long ret_value = OP_ERROR;
496
497         if (!is_permission_allowed()) {
498                 ERR("Permission denied to stop sensor[0x%x] for client [%d]", m_module? m_module->get_id() : -1, m_client_id);
499                 ret_value = OP_ERROR;
500                 goto out;
501         }
502
503         DBG("STOP Sensor [0x%x], called from client [%d]", m_module->get_id(), m_client_id);
504
505         if (m_module->stop()) {
506                 get_client_info_manager().set_start(m_client_id, m_module->get_id(), false);
507                 ret_value = OP_SUCCESS;
508         } else {
509                 ERR("Failed to stop sensor [0x%x] for client [%d]", m_module->get_id(), m_client_id);
510                 ret_value = OP_ERROR;
511         }
512
513 out:
514         if (!send_cmd_done(ret_value))
515                 ERR("Failed to send cmd_done to a client");
516
517         return true;
518 }
519
520 bool command_worker::cmd_register_event(void *payload)
521 {
522         cmd_reg_t *cmd;
523         long ret_value = OP_ERROR;
524
525         cmd = (cmd_reg_t*)payload;
526
527         if (!is_permission_allowed()) {
528                 ERR("Permission denied to register event [0x%x] for client [%d] to client info manager",
529                         cmd->event_type, m_client_id);
530                 ret_value = OP_ERROR;
531                 goto out;
532         }
533
534         if (!get_client_info_manager().register_event(m_client_id, m_module? m_module->get_id() : -1, cmd->event_type)) {
535                 INFO("Failed to register event [0x%x] for client [%d] to client info manager",
536                         cmd->event_type, m_client_id);
537                 ret_value = OP_ERROR;
538                 goto out;
539         }
540
541         insert_priority_list(cmd->event_type);
542         m_module->add_client(cmd->event_type);
543
544         ret_value = OP_SUCCESS;
545         DBG("Registering Event [0x%x] is done for client [%d]", cmd->event_type, m_client_id);
546
547 out:
548         if (!send_cmd_done(ret_value))
549                 ERR("Failed to send cmd_done to a client");
550
551         return true;
552 }
553
554 bool command_worker::cmd_unregister_event(void *payload)
555 {
556         cmd_unreg_t *cmd;
557         long ret_value = OP_ERROR;
558
559         cmd = (cmd_unreg_t*)payload;
560
561         if (!is_permission_allowed()) {
562                 ERR("Permission denied to unregister event [0x%x] for client [%d] to client info manager",
563                         cmd->event_type, m_client_id);
564                 ret_value = OP_ERROR;
565                 goto out;
566         }
567
568         if (!get_client_info_manager().unregister_event(m_client_id, m_module->get_id(), cmd->event_type)) {
569                 ERR("Failed to unregister event [0x%x] for client [%d] from client info manager",
570                         cmd->event_type, m_client_id);
571                 ret_value = OP_ERROR;
572                 goto out;
573         }
574
575         if (!m_module->delete_client(cmd->event_type)) {
576                 ERR("Failed to unregister event [0x%x] for client [%d]",
577                         cmd->event_type, m_client_id);
578                 ret_value = OP_ERROR;
579                 goto out;
580         }
581
582         ret_value = OP_SUCCESS;
583         DBG("Unregistering Event [0x%x] is done for client [%d]",
584                 cmd->event_type, m_client_id);
585
586 out:
587         if (!send_cmd_done(ret_value))
588                 ERR("Failed to send cmd_done to a client");
589
590         return true;
591 }
592
593 bool command_worker::cmd_set_interval(void *payload)
594 {
595         cmd_set_interval_t *cmd;
596         long ret_value = OP_ERROR;
597
598         cmd = (cmd_set_interval_t*)payload;
599
600         if (!is_permission_allowed()) {
601                 ERR("Permission denied to register interval for client [%d], for sensor [0x%x] with interval [%d] to client info manager",
602                         m_client_id, m_module? m_module->get_id() : -1, cmd->interval);
603                 ret_value = OP_ERROR;
604                 goto out;
605         }
606
607         if (!get_client_info_manager().set_interval(m_client_id, m_module->get_id(), cmd->interval)) {
608                 ERR("Failed to register interval for client [%d], for sensor [0x%x] with interval [%d] to client info manager",
609                         m_client_id, m_module->get_id(), cmd->interval);
610                 ret_value = OP_ERROR;
611                 goto out;
612         }
613
614         if (!m_module->add_interval(m_client_id, cmd->interval, false)) {
615                 ERR("Failed to set interval for client [%d], for sensor [0x%x] with interval [%d]",
616                         m_client_id, m_module->get_id(), cmd->interval);
617                 ret_value = OP_ERROR;
618                 goto out;
619         }
620
621         ret_value = OP_SUCCESS;
622
623 out:
624         if (!send_cmd_done(ret_value))
625                 ERR("Failed to send cmd_done to a client");
626
627         return true;
628 }
629
630 bool command_worker::cmd_unset_interval(void *payload)
631 {
632         long ret_value = OP_ERROR;
633
634         if (!is_permission_allowed()) {
635                 ERR("Permission denied to unregister interval for client [%d], for sensor [0x%x] to client info manager",
636                         m_client_id, m_module? m_module->get_id() : -1);
637                 ret_value = OP_ERROR;
638                 goto out;
639         }
640
641         if (!get_client_info_manager().set_interval(m_client_id, m_module->get_id(), 0)) {
642                 ERR("Failed to unregister interval for client [%d], for sensor [0x%x] to client info manager",
643                         m_client_id, m_module->get_id());
644                 ret_value = OP_ERROR;
645                 goto out;
646         }
647
648         if (!m_module->delete_interval(m_client_id, false)) {
649                 ERR("Failed to delete interval for client [%d]", m_client_id);
650                 ret_value = OP_ERROR;
651                 goto out;
652         }
653
654         ret_value = OP_SUCCESS;
655
656 out:
657         if (!send_cmd_done(ret_value))
658                 ERR("Failed to send cmd_done to a client");
659
660         return true;
661 }
662
663 bool command_worker::cmd_set_option(void *payload)
664 {
665         cmd_set_option_t *cmd;
666         long ret_value = OP_ERROR;
667
668         cmd = (cmd_set_option_t*)payload;
669
670         if (!is_permission_allowed()) {
671                 ERR("Permission denied to set interval for client [%d], for sensor [0x%x] with option [%d] to client info manager",
672                         m_client_id, m_module? m_module->get_id() : -1, cmd->option);
673                 ret_value = OP_ERROR;
674                 goto out;
675         }
676
677         if (!get_client_info_manager().set_option(m_client_id, m_module->get_id(), cmd->option)) {
678                 ERR("Failed to set option for client [%d], for sensor [0x%x] with option [%d] to client info manager",
679                         m_client_id, m_module->get_id(), cmd->option);
680                 ret_value = OP_ERROR;
681                 goto out;
682         }
683
684         ret_value = OP_SUCCESS;
685 out:
686         if (!send_cmd_done(ret_value))
687                 ERR("Failed to send cmd_done to a client");
688
689         return true;
690 }
691
692 bool command_worker::cmd_set_command(void *payload)
693 {
694         cmd_set_command_t *cmd;
695         long ret_value = OP_ERROR;
696
697         DBG("CMD_SET_COMMAND  Handler invoked\n");
698
699         cmd = (cmd_set_command_t*)payload;
700
701         if (!is_permission_allowed()) {
702                 ERR("Permission denied to set command for client [%d], for sensor [0x%x] with cmd [%d]",
703                         m_client_id, m_module? m_module->get_id() : -1, cmd->cmd);
704                 ret_value = OP_ERROR;
705                 goto out;
706         }
707
708         ret_value = m_module->set_command(cmd->cmd, cmd->value);
709
710 out:
711         if (!send_cmd_done(ret_value))
712                 ERR("Failed to send cmd_done to a client");
713
714         return true;
715 }
716
717 bool command_worker::cmd_get_data(void *payload)
718 {
719         const int GET_DATA_MIN_INTERVAL = 10;
720         cmd_get_data_t *cmd;
721         int state = OP_ERROR;
722         bool adjusted = false;
723
724         sensor_data_t data;
725
726         DBG("CMD_GET_VALUE Handler invoked\n");
727
728         cmd = (cmd_get_data_t*)payload;
729
730         if (!is_permission_allowed()) {
731                 ERR("Permission denied to get data for client [%d], for sensor [0x%x]",
732                         m_client_id, m_module? m_module->get_id() : -1);
733                 state = OP_ERROR;
734                 goto out;
735         }
736
737         state = m_module->get_sensor_data(cmd->type, data);
738
739         // In case of not getting sensor data, wait short time and retry again
740         // 1. changing interval to be less than 10ms
741         // 2. In case of first time, wait for INIT_WAIT_TIME
742         // 3. at another time, wait for WAIT_TIME
743         // 4. retrying to get data
744         // 5. repeat 2 ~ 4 operations RETRY_CNT times
745         // 6. reverting back to original interval
746         if (!state && !data.timestamp) {
747                 const int RETRY_CNT     = 3;
748                 const unsigned long long INIT_WAIT_TIME = 20000; //20ms
749                 const unsigned long WAIT_TIME = 100000; //100ms
750                 int retry = 0;
751
752                 unsigned int interval = m_module->get_interval(m_client_id, false);
753
754                 if (interval > GET_DATA_MIN_INTERVAL) {
755                         m_module->add_interval(m_client_id, GET_DATA_MIN_INTERVAL, false);
756                         adjusted = true;
757                 }
758
759                 while (!state && !data.timestamp && (retry++ < RETRY_CNT)) {
760                         INFO("Wait sensor[0x%x] data updated for client [%d] #%d", m_module->get_id(), m_client_id, retry);
761                         usleep((retry == 1) ? INIT_WAIT_TIME : WAIT_TIME);
762                         state = m_module->get_sensor_data(cmd->type, data);
763                 }
764
765                 if (adjusted)
766                         m_module->add_interval(m_client_id, interval, false);
767         }
768
769         if (!data.timestamp)
770                 state = OP_ERROR;
771
772         if (state) {
773                 ERR("Failed to get data for client [%d], for sensor [0x%x]",
774                         m_client_id, m_module->get_id());
775         }
776
777 out:
778         send_cmd_get_data_done(state, &data);
779
780         return true;
781 }
782
783 bool command_worker::cmd_send_sensorhub_data(void *payload)
784 {
785         cmd_send_sensorhub_data_t *cmd;
786         long ret_value = OP_ERROR;
787
788         DBG("CMD_SEND_SENSORHUB_DATA Handler invoked");
789
790         cmd = (cmd_send_sensorhub_data_t*)payload;
791
792         if (!is_permission_allowed()) {
793                 ERR("Permission denied to send sensorhub_data for client [%d], for sensor [0x%x]",
794                         m_client_id, m_module? m_module->get_id() : -1);
795                 ret_value = OP_ERROR;
796                 goto out;
797         }
798
799         ret_value = m_module->send_sensorhub_data(cmd->data, cmd->data_len);
800
801 out:
802         if (!send_cmd_done(ret_value))
803                 ERR("Failed to send cmd_done to a client");
804
805         return true;
806 }
807
808 void command_worker::get_info(string &info)
809 {
810         const char *client_info = NULL;
811         const char *sensor_info = NULL;
812
813         if (m_client_id != CLIENT_ID_INVALID)
814                 client_info = get_client_info_manager().get_client_info(m_client_id);
815
816         if (m_module)
817                 sensor_info = m_module->get_name();
818
819         info = string("Command worker for ") + (client_info ? client_info : "Unknown") + "'s "
820                 + (sensor_info ? sensor_info : "Unknown");
821 }
822
823 int command_worker::get_permission(void)
824 {
825         return permission_checker::get_instance().get_permission(m_socket.get_socket_fd());
826 }
827
828 bool command_worker::is_permission_allowed(void)
829 {
830         if (!m_module)
831                 return false;
832
833         if (m_module->get_permission() & m_permission)
834                 return true;
835
836         return false;
837 }
838
839
840 cclient_info_manager& command_worker::get_client_info_manager(void)
841 {
842         return cclient_info_manager::get_instance();
843 }
844
845 csensor_event_dispatcher& command_worker::get_event_dispathcher(void)
846 {
847         return csensor_event_dispatcher::get_instance();
848 }
849
850 void insert_priority_list(unsigned int event_type)
851 {
852         if (event_type == ORIENTATION_RAW_DATA_EVENT ||
853                         event_type == LINEAR_ACCEL_RAW_DATA_EVENT ||
854                         event_type == GRAVITY_RAW_DATA_EVENT ||
855                         event_type == ROTATION_VECTOR_RAW_DATA_EVENT) {
856                 priority_list.insert(ACCELEROMETER_RAW_DATA_EVENT);
857                 priority_list.insert(GYROSCOPE_RAW_DATA_EVENT);
858                 priority_list.insert(GEOMAGNETIC_RAW_DATA_EVENT);
859         }
860
861         if (event_type == GEOMAGNETIC_RV_RAW_DATA_EVENT) {
862                 priority_list.insert(ACCELEROMETER_RAW_DATA_EVENT);
863                 priority_list.insert(GEOMAGNETIC_RAW_DATA_EVENT);
864         }
865
866         if (event_type == GAMING_RV_RAW_DATA_EVENT) {
867                 priority_list.insert(ACCELEROMETER_RAW_DATA_EVENT);
868                 priority_list.insert(GYROSCOPE_RAW_DATA_EVENT);
869         }
870 }