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