Fix - Coding rule
[platform/core/uifw/voice-control.git] / common / vc_command.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <cynara-client.h>
18 #include <cynara-error.h>
19 #include <cynara-session.h>
20 #include <libintl.h>
21 #include <stdlib.h>
22 #include <system_info.h>
23
24 #include "vc_command.h"
25 #include "vc_main.h"
26 #include "voice_control_command.h"
27 #include "voice_control_command_expand.h"
28 #include "voice_control_common.h"
29 #include "voice_control_key_defines.h"
30
31 static int g_feature_enabled = -1;
32
33 static int g_privilege_allowed = 1; /* Always True */
34 static cynara *p_cynara = NULL;
35
36 static int __vc_cmd_get_feature_enabled()
37 {
38         if (0 == g_feature_enabled) {
39                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Voice control feature NOT supported");
40                 return VC_ERROR_NOT_SUPPORTED;
41         } else if (-1 == g_feature_enabled) {
42                 bool vc_supported = false;
43                 bool mic_supported = false;
44                 if (0 == system_info_get_platform_bool(VC_FEATURE_PATH, &vc_supported)) {
45                         if (0 == system_info_get_platform_bool(VC_MIC_FEATURE_PATH, &mic_supported)) {
46                                 if (false == vc_supported || false == mic_supported) {
47                                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Voice control feature NOT supported");
48                                         g_feature_enabled = 0;
49                                         return VC_ERROR_NOT_SUPPORTED;
50                                 }
51
52                                 g_feature_enabled = 1;
53                         } else {
54                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get feature value");
55                                 return VC_ERROR_NOT_SUPPORTED;
56                         }
57                 } else {
58                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get feature value");
59                         return VC_ERROR_NOT_SUPPORTED;
60                 }
61         }
62
63         return 0;
64 }
65
66 static int __check_privilege_initialize()
67 {
68         int ret = cynara_initialize(&p_cynara, NULL);
69         if (CYNARA_API_SUCCESS != ret)
70                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] fail to initialize");
71         
72         return ret == CYNARA_API_SUCCESS;
73 }
74
75 static int __check_privilege(const char* uid, const char * privilege)
76 {
77         FILE *fp = NULL;
78         char smack_label[1024] = "/proc/self/attr/current";
79
80         if (!p_cynara) {
81             return false;
82         }
83
84         fp = fopen(smack_label, "r");
85         if (fp != NULL) {
86             if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
87                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] fail to fread");
88
89             fclose(fp);
90         }
91
92         pid_t pid = getpid();
93         char *session = cynara_session_from_pid(pid);
94         int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
95         SLOG(LOG_DEBUG, TAG_VCCMD, "[Client]cynara_check returned %d(%s)", ret, (CYNARA_API_ACCESS_ALLOWED == ret) ? "Allowed" : "Denied");
96         if (session)
97             free(session);
98
99         if (ret != CYNARA_API_ACCESS_ALLOWED)
100             return false;
101         return true;
102 }
103
104 static void __check_privilege_deinitialize()
105 {
106         if (p_cynara)
107                 cynara_finish(p_cynara);
108         p_cynara = NULL;
109 }
110
111 static int __vc_cmd_check_privilege()
112 {
113         char uid[16];
114
115         if (0 == g_privilege_allowed) {
116                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Permission is denied");
117                 return VC_ERROR_PERMISSION_DENIED;
118         } else if (-1 == g_privilege_allowed) {
119                 if (false == __check_privilege_initialize()) {
120                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] privilege initialize is failed");
121                         return VC_ERROR_PERMISSION_DENIED;
122                 }
123                 snprintf(uid, 16, "%d", getuid());
124                 if (false == __check_privilege(uid, VC_PRIVILEGE)) {
125                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Permission is denied");
126                         g_privilege_allowed = 0;
127                         __check_privilege_deinitialize();
128                         return VC_ERROR_PERMISSION_DENIED;
129                 }
130                 __check_privilege_deinitialize();
131         }
132
133         g_privilege_allowed = 1;
134
135         return VC_ERROR_NONE;
136 }
137
138 int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list)
139 {
140         if (0 != __vc_cmd_get_feature_enabled()) {
141                 return VC_ERROR_NOT_SUPPORTED;
142         }
143         if (0 != __vc_cmd_check_privilege()) {
144                 return VC_ERROR_PERMISSION_DENIED;
145         }
146
147         if (NULL == vc_cmd_list) {
148                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
149                 return VC_ERROR_INVALID_PARAMETER;
150         }
151
152         vc_cmd_list_s* list = (vc_cmd_list_s*)calloc(1, sizeof(vc_cmd_list_s));
153
154         if (NULL == list) {
155                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not enough memory");
156                 return VC_ERROR_OUT_OF_MEMORY;
157         }
158
159         list->index = -1;
160         list->list = NULL;
161
162         *vc_cmd_list = (vc_cmd_list_h)list;
163
164         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", *vc_cmd_list);
165
166         return VC_ERROR_NONE;
167 }
168
169 int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command)
170 {
171         if (0 != __vc_cmd_get_feature_enabled()) {
172                 return VC_ERROR_NOT_SUPPORTED;
173         }
174         if (0 != __vc_cmd_check_privilege()) {
175                 return VC_ERROR_PERMISSION_DENIED;
176         }
177
178         if (NULL == vc_cmd_list) {
179                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
180                 return VC_ERROR_INVALID_PARAMETER;
181         }
182
183         vc_cmd_list_remove_all(vc_cmd_list, release_command);
184
185         vc_cmd_list_s* list = NULL;
186         list = (vc_cmd_list_s*)vc_cmd_list;
187
188         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", list);
189
190         if (NULL != list) {
191                 free(list);
192                 list = NULL;
193         }
194
195         return VC_ERROR_NONE;
196 }
197
198 int vc_cmd_list_get_count(vc_cmd_list_h vc_cmd_list, int* count)
199 {
200         if (0 != __vc_cmd_get_feature_enabled()) {
201                 return VC_ERROR_NOT_SUPPORTED;
202         }
203         if (0 != __vc_cmd_check_privilege()) {
204                 return VC_ERROR_PERMISSION_DENIED;
205         }
206
207         if (NULL == vc_cmd_list || NULL == count) {
208                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Get command count : Input parameter is NULL");
209                 return VC_ERROR_INVALID_PARAMETER;
210         }
211
212         vc_cmd_list_s* list = NULL;
213         list = (vc_cmd_list_s*)vc_cmd_list;
214
215         *count = g_slist_length(list->list);
216
217         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p), count(%d)", list, *count);
218
219         return VC_ERROR_NONE;
220 }
221
222 int vc_cmd_list_add(vc_cmd_list_h vc_cmd_list, vc_cmd_h vc_command)
223 {
224         if (0 != __vc_cmd_get_feature_enabled()) {
225                 return VC_ERROR_NOT_SUPPORTED;
226         }
227         if (0 != __vc_cmd_check_privilege()) {
228                 return VC_ERROR_PERMISSION_DENIED;
229         }
230
231         if (NULL == vc_cmd_list || NULL == vc_command) {
232                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
233                 return VC_ERROR_INVALID_PARAMETER;
234         }
235
236         vc_cmd_list_s* list = NULL;
237         list = (vc_cmd_list_s*)vc_cmd_list;
238
239         vc_cmd_s* cmd = NULL;
240         cmd = (vc_cmd_s*)vc_command;
241
242         list->list = g_slist_append(list->list, cmd);
243
244         if (1 == g_slist_length(list->list)) {
245                 list->index = 0;
246         }
247
248         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p), command(%p)", list, cmd);
249
250         return VC_ERROR_NONE;
251 }
252
253 int vc_cmd_list_remove(vc_cmd_list_h vc_cmd_list, vc_cmd_h vc_command)
254 {
255         if (0 != __vc_cmd_get_feature_enabled()) {
256                 return VC_ERROR_NOT_SUPPORTED;
257         }
258         if (0 != __vc_cmd_check_privilege()) {
259                 return VC_ERROR_PERMISSION_DENIED;
260         }
261
262         if (NULL == vc_cmd_list || NULL == vc_command) {
263                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
264                 return VC_ERROR_INVALID_PARAMETER;
265         }
266
267         vc_cmd_list_s* list = NULL;
268         list = (vc_cmd_list_s*)vc_cmd_list;
269
270         vc_cmd_s* cmd = NULL;
271         cmd = (vc_cmd_s*)vc_command;
272
273         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p), command(%p)", list, cmd);
274
275         vc_cmd_s* temp_cmd = NULL;
276         GSList *iter = NULL;
277
278         iter = g_slist_nth(list->list, 0);
279
280         while (NULL != iter) {
281                 temp_cmd = iter->data;
282
283                 if (NULL != temp_cmd && cmd == temp_cmd) {
284                         list->list = g_slist_remove(list->list, temp_cmd);
285                         /*
286                         if (true == release_command) {
287                                 SLOG(LOG_DEBUG, TAG_VCCMD, "Release command data");
288                                 if (NULL != temp_cmd->command)          free(temp_cmd->command);
289                                 if (NULL != temp_cmd->parameter)        free(temp_cmd->parameter);
290                                 free(temp_cmd);
291                                 temp_cmd = NULL;
292                         }
293                         */
294                 }
295
296                 iter = g_slist_next(iter);
297         }
298
299         int count = g_slist_length(list->list);
300
301         if (0 == count) {
302                 list->index = -1;
303         } else if (list->index == count) {
304                 list->index = count - 1;
305         }
306
307         return VC_ERROR_NONE;
308 }
309
310 int vc_cmd_list_remove_all(vc_cmd_list_h vc_cmd_list, bool release_command)
311 {
312         if (0 != __vc_cmd_get_feature_enabled()) {
313                 return VC_ERROR_NOT_SUPPORTED;
314         }
315         if (0 != __vc_cmd_check_privilege()) {
316                 return VC_ERROR_PERMISSION_DENIED;
317         }
318
319         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Destroy all command");
320
321         if (NULL == vc_cmd_list) {
322                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
323                 return VC_ERROR_INVALID_PARAMETER;
324         }
325
326         vc_cmd_list_s* list = NULL;
327         list = (vc_cmd_list_s*)vc_cmd_list;
328
329         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list (%p), release command (%s)"
330                  , list, release_command ? "true" : "false");
331
332         int count = g_slist_length(list->list);
333
334         int i ;
335         vc_cmd_s *temp_cmd;
336
337         for (i = 0; i < count ; i++) {
338                 temp_cmd = g_slist_nth_data(list->list, 0);
339
340                 if (NULL != temp_cmd) {
341                         list->list = g_slist_remove(list->list, temp_cmd);
342
343                         if (true == release_command) {
344                                 SLOG(LOG_DEBUG, TAG_VCCMD, "Free command(%p)", temp_cmd);
345                                 if (NULL != temp_cmd->command)          free(temp_cmd->command);
346                                 if (NULL != temp_cmd->parameter)        free(temp_cmd->parameter);
347                                 free(temp_cmd);
348                                 temp_cmd = NULL;
349                         }
350                 }
351         }
352
353         list->index = -1;
354
355         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
356         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
357
358         return VC_ERROR_NONE;
359 }
360
361 int vc_cmd_list_foreach_commands(vc_cmd_list_h vc_cmd_list, vc_cmd_list_cb callback, void* user_data)
362 {
363         if (0 != __vc_cmd_get_feature_enabled()) {
364                 return VC_ERROR_NOT_SUPPORTED;
365         }
366         if (0 != __vc_cmd_check_privilege()) {
367                 return VC_ERROR_PERMISSION_DENIED;
368         }
369
370         if (NULL == vc_cmd_list) {
371                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
372                 return VC_ERROR_INVALID_PARAMETER;
373         }
374
375         vc_cmd_list_s* list = NULL;
376         list = (vc_cmd_list_s*)vc_cmd_list;
377
378         int count = g_slist_length(list->list);
379         int i ;
380
381         GSList *iter = NULL;
382         vc_cmd_s *temp_cmd;
383
384         iter = g_slist_nth(list->list, 0);
385
386         for (i = 0; i < count; i++) {
387                 if (NULL == iter) {
388                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] No command in list");
389                         return VC_ERROR_OPERATION_FAILED;
390                 }
391
392                 temp_cmd = iter->data;
393
394                 if (NULL != temp_cmd) {
395                         if (false == callback((vc_cmd_h)temp_cmd, user_data)) {
396                                 break;
397                         }
398
399                 }
400                 iter = g_slist_next(iter);
401         }
402
403         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Foreach commands Done");
404
405         return VC_ERROR_NONE;
406 }
407
408 int vc_cmd_list_filter_by_type(vc_cmd_list_h original, int type, vc_cmd_list_h* filtered)
409 {
410         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Filter by type");
411
412         if (0 != __vc_cmd_get_feature_enabled()) {
413                 return VC_ERROR_NOT_SUPPORTED;
414         }
415         if (0 != __vc_cmd_check_privilege()) {
416                 return VC_ERROR_PERMISSION_DENIED;
417         }
418
419         if (NULL == original) {
420                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
421                 return VC_ERROR_INVALID_PARAMETER;
422         }
423
424         if (VC_COMMAND_TYPE_NONE >= type || VC_COMMAND_TYPE_EXCLUSIVE < type) {
425                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid type");
426                 return VC_ERROR_INVALID_PARAMETER;
427         }
428
429         vc_cmd_list_s* list = NULL;
430         list = (vc_cmd_list_s*)original;
431
432         vc_cmd_list_h temp_list;
433         if (0 != vc_cmd_list_create(&temp_list)) {
434                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to list create");
435                 return VC_ERROR_OPERATION_FAILED;
436         }
437
438         int count = g_slist_length(list->list);
439         int i;
440
441         GSList *iter = NULL;
442         vc_cmd_s *iter_cmd;
443
444         iter = g_slist_nth(list->list, 0);
445
446         for (i = 0; i < count; i++) {
447                 if (NULL == iter) {
448                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] No command in list");
449                         return VC_ERROR_OPERATION_FAILED;
450                 }
451                 if (NULL != iter->data) {
452                         iter_cmd = iter->data;
453
454                         if (NULL != iter_cmd) {
455                                 int iter_type;
456                                 if (0 != vc_cmd_get_type((vc_cmd_h)iter_cmd, &iter_type)) {
457                                         SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get command type");
458                                         continue;
459                                 }
460
461                                 if (iter_type == type) {
462                                         vc_cmd_h temp_cmd;
463                                         if (0 != vc_cmd_create(&temp_cmd)) {
464                                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to create cmd");
465                                                 continue;
466                                         }
467
468                                         memcpy(temp_cmd, iter_cmd, sizeof(vc_cmd_s));
469                                         if (NULL != iter_cmd->command) {
470                                                 ((vc_cmd_s*)temp_cmd)->command = strdup(iter_cmd->command);
471                                         }
472                                         if (NULL != iter_cmd->parameter) {
473                                                 ((vc_cmd_s*)temp_cmd)->parameter = strdup(iter_cmd->parameter);
474                                         }
475
476                                         if (0 != vc_cmd_list_add(temp_list, temp_cmd)) {
477                                                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to cmd list add");
478                                                 vc_cmd_destroy(temp_cmd);
479                                                 continue;
480                                         }
481                                 }
482                         }
483                 }
484                 iter = g_slist_next(iter);
485         }
486
487         count = 0;
488         if (0 != vc_cmd_list_get_count(temp_list, &count)) {
489                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Fail to get count");
490         } else {
491                 SLOG(LOG_DEBUG, TAG_VCCMD, "Filtering result : (%d) command", count);
492         }
493
494         *filtered = temp_list;
495
496         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
497
498         return VC_ERROR_NONE;
499 }
500
501 int vc_cmd_list_first(vc_cmd_list_h vc_cmd_list)
502 {
503         if (0 != __vc_cmd_get_feature_enabled()) {
504                 return VC_ERROR_NOT_SUPPORTED;
505         }
506         if (0 != __vc_cmd_check_privilege()) {
507                 return VC_ERROR_PERMISSION_DENIED;
508         }
509
510         if (NULL == vc_cmd_list) {
511                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
512                 return VC_ERROR_INVALID_PARAMETER;
513         }
514
515         vc_cmd_list_s* list = NULL;
516         list = (vc_cmd_list_s*)vc_cmd_list;
517
518         if (0 == g_slist_length(list->list)) {
519                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] List is empty");
520                 return VC_ERROR_EMPTY;
521         }
522
523         list->index = 0;
524
525         return VC_ERROR_NONE;
526 }
527
528 int vc_cmd_list_last(vc_cmd_list_h vc_cmd_list)
529 {
530         if (0 != __vc_cmd_get_feature_enabled()) {
531                 return VC_ERROR_NOT_SUPPORTED;
532         }
533         if (0 != __vc_cmd_check_privilege()) {
534                 return VC_ERROR_PERMISSION_DENIED;
535         }
536
537         if (NULL == vc_cmd_list) {
538                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
539                 return VC_ERROR_INVALID_PARAMETER;
540         }
541
542         vc_cmd_list_s* list = NULL;
543         list = (vc_cmd_list_s*)vc_cmd_list;
544
545         int count = g_slist_length(list->list);
546
547         if (0 == count) {
548                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] List is empty");
549                 return VC_ERROR_EMPTY;
550         } else {
551                 list->index = count - 1;
552                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
553         }
554
555         return VC_ERROR_NONE;
556 }
557
558 int vc_cmd_list_next(vc_cmd_list_h vc_cmd_list)
559 {
560         if (0 != __vc_cmd_get_feature_enabled()) {
561                 return VC_ERROR_NOT_SUPPORTED;
562         }
563         if (0 != __vc_cmd_check_privilege()) {
564                 return VC_ERROR_PERMISSION_DENIED;
565         }
566
567         if (NULL == vc_cmd_list) {
568                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
569                 return VC_ERROR_INVALID_PARAMETER;
570         }
571
572         vc_cmd_list_s* list = NULL;
573         list = (vc_cmd_list_s*)vc_cmd_list;
574
575         int count = g_slist_length(list->list);
576
577         if (list->index < count - 1) {
578                 list->index = list->index + 1;
579                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
580         } else {
581                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
582                 return VC_ERROR_ITERATION_END;
583         }
584
585         return VC_ERROR_NONE;
586 }
587
588 int vc_cmd_list_prev(vc_cmd_list_h vc_cmd_list)
589 {
590         if (0 != __vc_cmd_get_feature_enabled()) {
591                 return VC_ERROR_NOT_SUPPORTED;
592         }
593         if (0 != __vc_cmd_check_privilege()) {
594                 return VC_ERROR_PERMISSION_DENIED;
595         }
596
597         if (NULL == vc_cmd_list) {
598                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
599                 return VC_ERROR_INVALID_PARAMETER;
600         }
601
602         vc_cmd_list_s* list = NULL;
603         list = (vc_cmd_list_s*)vc_cmd_list;
604
605         if (list->index > 0) {
606                 list->index = list->index - 1;
607                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
608         } else {
609                 SLOG(LOG_DEBUG, TAG_VCCMD, "[DEBUG] List index : %d", list->index);
610                 return VC_ERROR_ITERATION_END;
611         }
612
613         return VC_ERROR_NONE;
614 }
615
616 int vc_cmd_list_get_current(vc_cmd_list_h vc_cmd_list, vc_cmd_h* vc_command)
617 {
618         if (0 != __vc_cmd_get_feature_enabled()) {
619                 return VC_ERROR_NOT_SUPPORTED;
620         }
621         if (0 != __vc_cmd_check_privilege()) {
622                 return VC_ERROR_PERMISSION_DENIED;
623         }
624
625         if (NULL == vc_cmd_list || NULL == vc_command) {
626                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
627                 return VC_ERROR_INVALID_PARAMETER;
628         }
629
630         vc_cmd_list_s* list = NULL;
631         list = (vc_cmd_list_s*)vc_cmd_list;
632
633         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list (%p), index (%d)", list, list->index);
634
635         if (0 == g_slist_length(list->list)) {
636                 SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list is empty");
637                 *vc_command = NULL;
638                 return VC_ERROR_EMPTY;
639         }
640
641         vc_cmd_s *temp_cmd = NULL;
642         temp_cmd = g_slist_nth_data(list->list, list->index);
643
644         *vc_command = (vc_cmd_h)temp_cmd;
645
646         SLOG(LOG_DEBUG, TAG_VCCMD, "[List] Get current command (%p)", *vc_command);
647
648         return VC_ERROR_NONE;
649 }
650
651
652 int vc_cmd_create(vc_cmd_h* vc_command)
653 {
654         if (0 != __vc_cmd_get_feature_enabled()) {
655                 return VC_ERROR_NOT_SUPPORTED;
656         }
657         if (0 != __vc_cmd_check_privilege()) {
658                 return VC_ERROR_PERMISSION_DENIED;
659         }
660
661         if (NULL == vc_command) {
662                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
663                 return VC_ERROR_INVALID_PARAMETER;
664         }
665
666         vc_cmd_s* command = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
667
668         if (NULL == command) {
669                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Not enough memory");
670                 return VC_ERROR_OUT_OF_MEMORY;
671         }
672
673         command->pid = 0;
674         command->id = 0;
675         command->index = 0;
676         command->type = VC_COMMAND_TYPE_NONE;
677         command->format = VC_CMD_FORMAT_FIXED;
678         command->command = NULL;
679         command->parameter = NULL;
680         command->domain = 0;
681         command->key = VC_KEY_NONE;
682         command->modifier = VC_MODIFIER_NONE;
683
684         *vc_command = (vc_cmd_h)command;
685
686         SLOG(LOG_DEBUG, TAG_VCCMD, "[Create command][%p]", *vc_command);
687
688         return VC_ERROR_NONE;
689 }
690
691 int vc_cmd_destroy(vc_cmd_h vc_command)
692 {
693         if (0 != __vc_cmd_get_feature_enabled()) {
694                 return VC_ERROR_NOT_SUPPORTED;
695         }
696         if (0 != __vc_cmd_check_privilege()) {
697                 return VC_ERROR_PERMISSION_DENIED;
698         }
699
700         if (NULL == vc_command) {
701                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Input parameter is NULL");
702                 return VC_ERROR_INVALID_PARAMETER;
703         }
704
705         vc_cmd_s* command = NULL;
706         command = (vc_cmd_s*)vc_command;
707
708         SLOG(LOG_DEBUG, TAG_VCCMD, "[Destroy command][%p]", command);
709
710         if (NULL != command) {
711                 if (NULL != command->command)   free(command->command);
712                 if (NULL != command->parameter) free(command->parameter);
713                 free(command);
714                 command = NULL;
715         }
716
717         return VC_ERROR_NONE;
718 }
719
720 int vc_cmd_set_id(vc_cmd_h vc_command, int id)
721 {
722         if (0 != __vc_cmd_get_feature_enabled()) {
723                 return VC_ERROR_NOT_SUPPORTED;
724         }
725         if (0 != __vc_cmd_check_privilege()) {
726                 return VC_ERROR_PERMISSION_DENIED;
727         }
728
729         if (NULL == vc_command) {
730                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
731                 return VC_ERROR_INVALID_PARAMETER;
732         }
733
734         vc_cmd_s* cmd = NULL;
735         cmd = (vc_cmd_s*)vc_command;
736
737         if (NULL != cmd) {
738                 cmd->id = id;
739                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Set id][%p] id(%d)", vc_command, cmd->id);
740         }
741
742         return 0;
743 }
744
745 int vc_cmd_get_id(vc_cmd_h vc_command, int* id)
746 {
747         if (0 != __vc_cmd_get_feature_enabled()) {
748                 return VC_ERROR_NOT_SUPPORTED;
749         }
750         if (0 != __vc_cmd_check_privilege()) {
751                 return VC_ERROR_PERMISSION_DENIED;
752         }
753
754         if (NULL == vc_command || NULL == id) {
755                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
756                 return VC_ERROR_INVALID_PARAMETER;
757         }
758
759         vc_cmd_s* cmd = NULL;
760         cmd = (vc_cmd_s*)vc_command;
761
762         if (NULL != cmd) {
763                 *id = cmd->id;
764                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Get id][%p] id(%d)", vc_command, *id);
765         }
766
767         return 0;
768 }
769
770 int vc_cmd_set_command(vc_cmd_h vc_command, const char* command)
771 {
772         if (0 != __vc_cmd_get_feature_enabled()) {
773                 return VC_ERROR_NOT_SUPPORTED;
774         }
775         if (0 != __vc_cmd_check_privilege()) {
776                 return VC_ERROR_PERMISSION_DENIED;
777         }
778
779         if (NULL == vc_command) {
780                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
781                 return VC_ERROR_INVALID_PARAMETER;
782         }
783
784         vc_cmd_s* cmd = NULL;
785         cmd = (vc_cmd_s*)vc_command;
786
787         if (NULL != cmd->command) {
788                 free(cmd->command);
789         }
790
791         cmd->command = NULL;
792
793         if (NULL != command) {
794                 cmd->command = strdup(command);
795         }
796
797         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set command][%p] Command(%s)", vc_command, cmd->command);
798
799         return 0;
800 }
801
802 int vc_cmd_get_command(vc_cmd_h vc_command, char** command)
803 {
804         if (0 != __vc_cmd_get_feature_enabled()) {
805                 return VC_ERROR_NOT_SUPPORTED;
806         }
807         if (0 != __vc_cmd_check_privilege()) {
808                 return VC_ERROR_PERMISSION_DENIED;
809         }
810
811         if (NULL == vc_command || NULL == command) {
812                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
813                 return VC_ERROR_INVALID_PARAMETER;
814         }
815
816         vc_cmd_s* cmd = NULL;
817         cmd = (vc_cmd_s*)vc_command;
818
819         if (NULL != cmd->command) {
820                 *command = strdup(gettext(cmd->command));
821         }
822
823         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get command][%p] Command(%s)", vc_command, *command);
824
825         return 0;
826 }
827
828 int vc_cmd_set_unfixed_command(vc_cmd_h vc_command, const char* command)
829 {
830         if (0 != __vc_cmd_get_feature_enabled()) {
831                 return VC_ERROR_NOT_SUPPORTED;
832         }
833         if (0 != __vc_cmd_check_privilege()) {
834                 return VC_ERROR_PERMISSION_DENIED;
835         }
836
837         if (NULL == vc_command) {
838                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
839                 return VC_ERROR_INVALID_PARAMETER;
840         }
841
842         vc_cmd_s* cmd = NULL;
843         cmd = (vc_cmd_s*)vc_command;
844
845         if (NULL != cmd->parameter) {
846                 free(cmd->parameter);
847         }
848
849         cmd->parameter = NULL;
850
851         if (NULL != command) {
852                 cmd->parameter = strdup(command);
853                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Set parameter][%p] parameter(%s)", vc_command, cmd->parameter);
854         }
855
856         return 0;
857 }
858
859 int vc_cmd_get_unfixed_command(vc_cmd_h vc_command, char** command)
860 {
861         if (0 != __vc_cmd_get_feature_enabled()) {
862                 return VC_ERROR_NOT_SUPPORTED;
863         }
864         if (0 != __vc_cmd_check_privilege()) {
865                 return VC_ERROR_PERMISSION_DENIED;
866         }
867
868         if (NULL == vc_command || NULL == command) {
869                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid handle ");
870                 return VC_ERROR_INVALID_PARAMETER;
871         }
872
873         vc_cmd_s* cmd = NULL;
874         cmd = (vc_cmd_s*)vc_command;
875
876         if (NULL != cmd->parameter) {
877                 *command = strdup(gettext(cmd->parameter));
878                 SLOG(LOG_DEBUG, TAG_VCCMD, "[Get nonfixed command][%p] nonfixed command(%s)", vc_command, *command);
879         }
880
881         return 0;
882 }
883
884 int vc_cmd_set_type(vc_cmd_h vc_command, int type)
885 {
886         if (0 != __vc_cmd_get_feature_enabled()) {
887                 return VC_ERROR_NOT_SUPPORTED;
888         }
889         if (0 != __vc_cmd_check_privilege()) {
890                 return VC_ERROR_PERMISSION_DENIED;
891         }
892
893         if (NULL == vc_command) {
894                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
895                 return VC_ERROR_INVALID_PARAMETER;
896         }
897
898         vc_cmd_s* cmd = NULL;
899         cmd = (vc_cmd_s*)vc_command;
900
901         cmd->type = type;
902
903         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set type][%p] type(%d)", vc_command, cmd->type);
904
905         return 0;
906 }
907
908 int vc_cmd_get_type(vc_cmd_h vc_command, int* type)
909 {
910         if (0 != __vc_cmd_get_feature_enabled()) {
911                 return VC_ERROR_NOT_SUPPORTED;
912         }
913         if (0 != __vc_cmd_check_privilege()) {
914                 return VC_ERROR_PERMISSION_DENIED;
915         }
916
917         if (NULL == vc_command || NULL == type) {
918                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
919                 return VC_ERROR_INVALID_PARAMETER;
920         }
921
922         vc_cmd_s* cmd = NULL;
923         cmd = (vc_cmd_s*)vc_command;
924
925         *type = cmd->type;
926
927         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get type][%p] type(%d)", vc_command, *type);
928
929         return 0;
930 }
931
932 int vc_cmd_set_format(vc_cmd_h vc_command, vc_cmd_format_e format)
933 {
934         if (0 != __vc_cmd_get_feature_enabled()) {
935                 return VC_ERROR_NOT_SUPPORTED;
936         }
937         if (0 != __vc_cmd_check_privilege()) {
938                 return VC_ERROR_PERMISSION_DENIED;
939         }
940
941         if (NULL == vc_command) {
942                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
943                 return VC_ERROR_INVALID_PARAMETER;
944         }
945
946         vc_cmd_s* cmd = NULL;
947         cmd = (vc_cmd_s*)vc_command;
948
949         cmd->format = format;
950
951         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set format][%p] format(%d)", vc_command, format);
952
953         return 0;
954 }
955
956 int vc_cmd_get_format(vc_cmd_h vc_command, vc_cmd_format_e* format)
957 {
958         if (0 != __vc_cmd_get_feature_enabled()) {
959                 return VC_ERROR_NOT_SUPPORTED;
960         }
961         if (0 != __vc_cmd_check_privilege()) {
962                 return VC_ERROR_PERMISSION_DENIED;
963         }
964
965         if (NULL == vc_command || NULL == format) {
966                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
967                 return VC_ERROR_INVALID_PARAMETER;
968         }
969
970         vc_cmd_s* cmd = NULL;
971         cmd = (vc_cmd_s*)vc_command;
972
973         *format = cmd->format;
974
975         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get format][%p] format(%d)", vc_command, *format);
976
977         return 0;
978 }
979
980 int vc_cmd_set_pid(vc_cmd_h vc_command, int pid)
981 {
982         if (0 != __vc_cmd_get_feature_enabled()) {
983                 return VC_ERROR_NOT_SUPPORTED;
984         }
985         if (0 != __vc_cmd_check_privilege()) {
986                 return VC_ERROR_PERMISSION_DENIED;
987         }
988
989         if (NULL == vc_command) {
990                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
991                 return VC_ERROR_INVALID_PARAMETER;
992         }
993
994         vc_cmd_s* cmd = NULL;
995         cmd = (vc_cmd_s*)vc_command;
996
997         cmd->pid = pid;
998
999         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set pid][%p] pid(%d)", vc_command, cmd->pid);
1000
1001         return 0;
1002 }
1003
1004 int vc_cmd_get_pid(vc_cmd_h vc_command, int* pid)
1005 {
1006         if (0 != __vc_cmd_get_feature_enabled()) {
1007                 return VC_ERROR_NOT_SUPPORTED;
1008         }
1009         if (0 != __vc_cmd_check_privilege()) {
1010                 return VC_ERROR_PERMISSION_DENIED;
1011         }
1012
1013         if (NULL == vc_command || NULL == pid) {
1014                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1015                 return VC_ERROR_INVALID_PARAMETER;
1016         }
1017
1018         vc_cmd_s* cmd = NULL;
1019         cmd = (vc_cmd_s*)vc_command;
1020
1021         *pid = cmd->pid;
1022
1023         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get pid][%p] pid(%d)", vc_command, *pid);
1024
1025         return 0;
1026 }
1027
1028 int vc_cmd_set_domain(vc_cmd_h vc_command, int domain)
1029 {
1030         if (0 != __vc_cmd_get_feature_enabled()) {
1031                 return VC_ERROR_NOT_SUPPORTED;
1032         }
1033         if (0 != __vc_cmd_check_privilege()) {
1034                 return VC_ERROR_PERMISSION_DENIED;
1035         }
1036
1037         if (NULL == vc_command) {
1038                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1039                 return VC_ERROR_INVALID_PARAMETER;
1040         }
1041
1042         vc_cmd_s* cmd = NULL;
1043         cmd = (vc_cmd_s*)vc_command;
1044
1045         cmd->domain = domain;
1046
1047         SLOG(LOG_DEBUG, TAG_VCCMD, "[Set domain] domain : %d", domain);
1048
1049         return 0;
1050 }
1051
1052 int vc_cmd_get_domain(vc_cmd_h vc_command, int* domain)
1053 {
1054         if (0 != __vc_cmd_get_feature_enabled()) {
1055                 return VC_ERROR_NOT_SUPPORTED;
1056         }
1057         if (0 != __vc_cmd_check_privilege()) {
1058                 return VC_ERROR_PERMISSION_DENIED;
1059         }
1060
1061         if (NULL == vc_command || NULL == domain) {
1062                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1063                 return VC_ERROR_INVALID_PARAMETER;
1064         }
1065
1066         vc_cmd_s* cmd = NULL;
1067         cmd = (vc_cmd_s*)vc_command;
1068
1069         *domain = cmd->domain;
1070
1071         SLOG(LOG_DEBUG, TAG_VCCMD, "[Get domain] domain : %d", *domain);
1072
1073         return 0;
1074 }
1075
1076 /**
1077 * @brief Sets key value of command.
1078 *
1079 * @param[in] vc_command Command handle
1080 * @param[in] key key value
1081 * @param[in] modifier modifier value
1082 *
1083 * @return 0 on success, otherwise a negative error value
1084 * @retval #VC_ERROR_NONE Successful
1085 * @retval #VC_ERROR_INVALID_PARAMETER Invalid parameter
1086 *
1087 * @see vc_cmd_get_result_key()
1088 */
1089 int vc_cmd_set_result_key(vc_cmd_h vc_command, int key, int modifier)
1090 {
1091         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Set result key");
1092
1093         if (NULL == vc_command) {
1094                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1095                 return VC_ERROR_INVALID_PARAMETER;
1096         }
1097
1098         vc_cmd_s* cmd = NULL;
1099         cmd = (vc_cmd_s*)vc_command;
1100
1101         SLOG(LOG_DEBUG, TAG_VCCMD, "key : %d, modifier : %d", key, modifier);
1102
1103         cmd->key = key;
1104         cmd->modifier = modifier;
1105
1106         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
1107         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
1108
1109         return 0;
1110 }
1111
1112 /**
1113 * @brief Gets key value of command.
1114 *
1115 * @param[in] vc_command Command handle
1116 * @param[out] key key value
1117 * @param[out] modifier modifier value
1118 *
1119 * @return 0 on success, otherwise a negative error value
1120 * @retval #VC_ERROR_NONE Successful
1121 * @retval #VC_ERROR_INVALID_PARAMETER Invalid parameter
1122 *
1123 * @see vc_cmd_add_result_key()
1124 */
1125 int vc_cmd_get_result_key(vc_cmd_h vc_command, int* key, int* modifier)
1126 {
1127         SLOG(LOG_DEBUG, TAG_VCCMD, "===== Get result key");
1128
1129         if (NULL == vc_command || NULL == key || NULL == modifier) {
1130                 SLOG(LOG_ERROR, TAG_VCCMD, "[ERROR] Invalid parameter ");
1131                 return VC_ERROR_INVALID_PARAMETER;
1132         }
1133
1134         vc_cmd_s* cmd = NULL;
1135         cmd = (vc_cmd_s*)vc_command;
1136
1137         *key = cmd->key;
1138         *modifier = cmd->modifier;
1139
1140         SLOG(LOG_DEBUG, TAG_VCCMD, "=====");
1141         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
1142
1143         return 0;
1144 }
1145
1146 int vc_cmd_print_list(vc_cmd_list_h vc_cmd_list)
1147 {
1148         if (NULL == vc_cmd_list) {
1149                 return -1;
1150         }
1151
1152         vc_cmd_list_s* list = NULL;
1153         list = (vc_cmd_list_s*)vc_cmd_list;
1154
1155         SLOG(LOG_DEBUG, TAG_VCCMD, "=== Command List ===");
1156         SLOG(LOG_DEBUG, TAG_VCCMD, "[List][%p]", list);
1157
1158         int count = g_slist_length(list->list);
1159
1160         int i;
1161         vc_cmd_s *temp_cmd = NULL;
1162
1163         for (i = 0; i < count ; i++) {
1164                 temp_cmd = g_slist_nth_data(list->list, i);
1165
1166                 if (NULL != temp_cmd) {
1167                         SLOG(LOG_DEBUG, TAG_VCCMD, "  [%d][%p] PID(%d) ID(%d) Type(%d) Format(%d) Domain(%d) Command(%s) Param(%s)",
1168                                  i, temp_cmd, temp_cmd->pid, temp_cmd->index, temp_cmd->type, temp_cmd->format, temp_cmd->domain, temp_cmd->command, temp_cmd->parameter);
1169                 }
1170         }
1171
1172         SLOG(LOG_DEBUG, TAG_VCCMD, "==================");
1173         SLOG(LOG_DEBUG, TAG_VCCMD, " ");
1174
1175         return 0;
1176 }