blob: e41d447cc041f56617a4514f70b53a35f20e20b2 [file] [log] [blame]
Christopher N. Hesse6b7dda72016-03-15 02:31:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
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#define LOG_TAG "ConsumerIrHal"
18
19#include <cutils/log.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <hardware/hardware.h>
23#include <hardware/consumerir.h>
24#include <malloc.h>
25#include <pthread.h>
26#include <stdbool.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
32#define UNUSED __attribute__((unused))
33
34#define IR_PATH "/sys/class/sec/sec_ir/ir_send"
35
36static int fd = 0;
37static pthread_mutex_t g_mtx;
38
39static const consumerir_freq_range_t consumerir_freqs[] = {
40#ifdef USE_ONE_FREQ_RANGE
41 {.min = 16000, .max = 60000},
42#else
43 {.min = 30000, .max = 30000},
44 {.min = 33000, .max = 33000},
45 {.min = 36000, .max = 36000},
46 {.min = 38000, .max = 38000},
47 {.min = 40000, .max = 40000},
48 {.min = 56000, .max = 56000},
49#endif
50};
51
52static bool try_append_number(char *buffer, int *len, int size, int number)
53{
54 int stored;
55
56 stored = snprintf(&buffer[*len], size - *len, "%d,", number);
57
58 if (stored < 0 || stored >= (size - *len))
59 return false;
60
61 *len += stored;
62 return true;
63}
64
65static bool grow_buffer(char **buffer, int *size)
66{
67 char *new_buffer;
68
69 *size *= 2;
70 new_buffer = realloc(*buffer, *size);
71 if (new_buffer == NULL)
72 return false;
73
74 *buffer = new_buffer;
75 return true;
76}
77
78static bool append_number(char **buffer, int *len, int *size, int number)
79{
80 if (try_append_number(*buffer, len, *size, number))
81 return true;
82
83 if (!grow_buffer(buffer, size))
84 return false;
85
86 return try_append_number(*buffer, len, *size, number);
87}
88
89static int consumerir_transmit(UNUSED struct consumerir_device *dev,
90 int carrier_freq, const int pattern[], int pattern_len)
91{
92 int buffer_len = 0;
93 int buffer_size = 128;
94 int i;
95 float factor;
96 char *buffer;
97
98 pthread_mutex_lock(&g_mtx);
99
100 buffer = malloc(buffer_size);
101 if (buffer == NULL)
102 return -ENOMEM;
103
104 // Write the header
105 if (!append_number(&buffer, &buffer_len, &buffer_size, carrier_freq))
106 goto error;
107
108#ifndef MS_IR_SIGNAL
109 // Calculate factor of conversion from microseconds to pulses
110 factor = 1000000 / carrier_freq;
111#else
112 factor = 1;
113#endif
114
115 // Write out the timing pattern
116 for (i = 0; i < pattern_len; i++) {
117 if (!append_number(&buffer, &buffer_len, &buffer_size,
118 (int) (pattern[i] / factor)))
119 goto error;
120 }
121
122 buffer[buffer_len - 1] = 0;
123 write(fd, buffer, buffer_len - 1);
124
125 free(buffer);
126
127 pthread_mutex_unlock(&g_mtx);
128
129 return 0;
130
131error:
132 free(buffer);
133 return -ENOMEM;
134}
135
Kevin F. Haggertyfcb04ea2016-11-26 13:12:31 -0700136static int consumerir_get_num_carrier_freqs(UNUSED struct consumerir_device *dev)
137{
138 return ARRAY_SIZE(consumerir_freqs);
139}
140
Christopher N. Hesse6b7dda72016-03-15 02:31:38 +0100141static int consumerir_get_carrier_freqs(struct consumerir_device *dev,
142 size_t len, consumerir_freq_range_t *ranges)
143{
144 size_t to_copy = consumerir_get_num_carrier_freqs(dev);
145
146 to_copy = len < to_copy ? len : to_copy;
147 memcpy(ranges, consumerir_freqs, to_copy * sizeof(consumerir_freq_range_t));
148 return to_copy;
149}
150
151static int consumerir_close(hw_device_t *dev)
152{
153 free(dev);
154 close(fd);
155 pthread_mutex_destroy(&g_mtx);
156 return 0;
157}
158
Christopher N. Hesse6b7dda72016-03-15 02:31:38 +0100159/*
160 * Generic device handling
161 */
162static int consumerir_open(const hw_module_t *module, const char *name,
163 hw_device_t **device)
164{
165 if (strcmp(name, CONSUMERIR_TRANSMITTER) != 0)
166 return -EINVAL;
167
168 if (device == NULL) {
169 ALOGE("NULL device on open");
170 return -EINVAL;
171 }
172
173 consumerir_device_t *dev = malloc(sizeof(consumerir_device_t));
174 memset(dev, 0, sizeof(consumerir_device_t));
175
176 dev->common.tag = HARDWARE_DEVICE_TAG;
177 dev->common.tag = HARDWARE_DEVICE_TAG;
178 dev->common.version = 0;
179 dev->common.module = (struct hw_module_t*) module;
180 dev->common.close = consumerir_close;
181
182 dev->transmit = consumerir_transmit;
183 dev->get_carrier_freqs = consumerir_get_carrier_freqs;
184 dev->get_num_carrier_freqs = consumerir_get_num_carrier_freqs;
185
186 pthread_mutex_init(&g_mtx, NULL);
187
188 *device = (hw_device_t*) dev;
189 fd = open(IR_PATH, O_RDWR);
190 return fd;
191}
192
193static struct hw_module_methods_t consumerir_module_methods = {
194 .open = consumerir_open,
195};
196
197consumerir_module_t HAL_MODULE_INFO_SYM = {
198 .common = {
199 .tag = HARDWARE_MODULE_TAG,
200 .module_api_version = CONSUMERIR_MODULE_API_VERSION_1_0,
201 .hal_api_version = HARDWARE_HAL_API_VERSION,
202 .id = CONSUMERIR_HARDWARE_MODULE_ID,
203 .name = "Consumer IR Module",
204 .author = "The CyanogenMod Project",
205 .methods = &consumerir_module_methods,
206 },
207};