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