Get IR available information from system-info
[platform/core/api/device.git] / src / ir.c
1 /*
2  * Copyright (c) 2016 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 <stdio.h>
18 #include <system_info.h>
19
20 #include "ir.h"
21 #include "common.h"
22 #include "dbus.h"
23
24 #define METHOD_IS_AVAILABLE             "IRIsAvailable"
25 #define METHOD_TRANSMIT                 "TransmitIR"
26 #define IR_FEATURE                      "http://tizen.org/feature/consumer_ir"
27
28
29 int device_ir_is_available(bool *available)
30 {
31         int ret;
32         bool ir_avail;
33
34         if (!available)
35                 return DEVICE_ERROR_INVALID_PARAMETER;
36
37         ret = system_info_get_platform_bool(IR_FEATURE, &ir_avail);
38
39         if (ret < 0) {
40                 *available = false;
41                 return DEVICE_ERROR_OPERATION_FAILED;
42         } else if (ret == 0 && !ir_avail) {
43                 *available = false;
44                 return DEVICE_ERROR_NOT_SUPPORTED;
45         }
46
47         ret = dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH_IR,
48                         DEVICED_INTERFACE_IR, METHOD_IS_AVAILABLE,
49                         NULL, NULL);
50
51         if (!ret) {
52                 *available = false;
53                 return DEVICE_ERROR_OPERATION_FAILED;
54         }
55
56         *available = ir_avail;
57         return DEVICE_ERROR_NONE;
58 }
59
60 int device_ir_transmit(int carrier_frequency, int *pattern, int size)
61 {
62         char *arr[1];
63         struct dbus_int pattern_list;
64         int freq_pattern[size + 1];
65         int ret;
66         int i;
67         bool ir_avail;
68
69         ret = device_ir_is_available(&ir_avail);
70         if (!ir_avail) {
71                 if (ret < 0) {
72                         _E("IR is not supported or IR operation failed");
73                         return ret;
74                 }
75                 _E("IR is not supported");
76                 return DEVICE_ERROR_OPERATION_FAILED;
77         }
78
79         if (!pattern)
80                 return DEVICE_ERROR_INVALID_PARAMETER;
81         if (size <= 0) {
82                 _E("IR pattern size is invalid");
83                 return DEVICE_ERROR_INVALID_PARAMETER;
84         }
85
86         freq_pattern[0] = carrier_frequency;
87         for (i = 1; i <= size; i++)
88                 freq_pattern[i] = pattern[i-1];
89
90         pattern_list.list = freq_pattern;
91         pattern_list.size = size + 1;
92         arr[0] = (char *)&pattern_list;
93
94         ret = dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH_IR,
95                         DEVICED_INTERFACE_IR, METHOD_TRANSMIT,
96                         "ai", arr);
97
98         if (ret < 0)
99                 return DEVICE_ERROR_OPERATION_FAILED;
100
101         return DEVICE_ERROR_NONE;
102 }