Christopher N. Hesse | 6b7dda7 | 2016-03-15 02:31:38 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Paul Keith | 464c0c9 | 2016-12-10 09:30:32 -0600 | [diff] [blame] | 31 | #include <samsung_consumerir.h> |
| 32 | |
Christopher N. Hesse | 6b7dda7 | 2016-03-15 02:31:38 +0100 | [diff] [blame] | 33 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 34 | #define UNUSED __attribute__((unused)) |
| 35 | |
Christopher N. Hesse | 6b7dda7 | 2016-03-15 02:31:38 +0100 | [diff] [blame] | 36 | static int fd = 0; |
| 37 | static pthread_mutex_t g_mtx; |
| 38 | |
Christopher N. Hesse | 6b7dda7 | 2016-03-15 02:31:38 +0100 | [diff] [blame] | 39 | static bool try_append_number(char *buffer, int *len, int size, int number) |
| 40 | { |
| 41 | int stored; |
| 42 | |
| 43 | stored = snprintf(&buffer[*len], size - *len, "%d,", number); |
| 44 | |
| 45 | if (stored < 0 || stored >= (size - *len)) |
| 46 | return false; |
| 47 | |
| 48 | *len += stored; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | static bool grow_buffer(char **buffer, int *size) |
| 53 | { |
| 54 | char *new_buffer; |
| 55 | |
| 56 | *size *= 2; |
| 57 | new_buffer = realloc(*buffer, *size); |
| 58 | if (new_buffer == NULL) |
| 59 | return false; |
| 60 | |
| 61 | *buffer = new_buffer; |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | static bool append_number(char **buffer, int *len, int *size, int number) |
| 66 | { |
| 67 | if (try_append_number(*buffer, len, *size, number)) |
| 68 | return true; |
| 69 | |
| 70 | if (!grow_buffer(buffer, size)) |
| 71 | return false; |
| 72 | |
| 73 | return try_append_number(*buffer, len, *size, number); |
| 74 | } |
| 75 | |
| 76 | static int consumerir_transmit(UNUSED struct consumerir_device *dev, |
| 77 | int carrier_freq, const int pattern[], int pattern_len) |
| 78 | { |
| 79 | int buffer_len = 0; |
| 80 | int buffer_size = 128; |
| 81 | int i; |
| 82 | float factor; |
| 83 | char *buffer; |
| 84 | |
| 85 | pthread_mutex_lock(&g_mtx); |
| 86 | |
| 87 | buffer = malloc(buffer_size); |
| 88 | if (buffer == NULL) |
| 89 | return -ENOMEM; |
| 90 | |
| 91 | // Write the header |
| 92 | if (!append_number(&buffer, &buffer_len, &buffer_size, carrier_freq)) |
| 93 | goto error; |
| 94 | |
| 95 | #ifndef MS_IR_SIGNAL |
| 96 | // Calculate factor of conversion from microseconds to pulses |
| 97 | factor = 1000000 / carrier_freq; |
| 98 | #else |
| 99 | factor = 1; |
| 100 | #endif |
| 101 | |
| 102 | // Write out the timing pattern |
| 103 | for (i = 0; i < pattern_len; i++) { |
| 104 | if (!append_number(&buffer, &buffer_len, &buffer_size, |
| 105 | (int) (pattern[i] / factor))) |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | buffer[buffer_len - 1] = 0; |
| 110 | write(fd, buffer, buffer_len - 1); |
| 111 | |
| 112 | free(buffer); |
| 113 | |
| 114 | pthread_mutex_unlock(&g_mtx); |
| 115 | |
| 116 | return 0; |
| 117 | |
| 118 | error: |
| 119 | free(buffer); |
| 120 | return -ENOMEM; |
| 121 | } |
| 122 | |
Kevin F. Haggerty | fcb04ea | 2016-11-26 13:12:31 -0700 | [diff] [blame] | 123 | static int consumerir_get_num_carrier_freqs(UNUSED struct consumerir_device *dev) |
| 124 | { |
| 125 | return ARRAY_SIZE(consumerir_freqs); |
| 126 | } |
| 127 | |
Christopher N. Hesse | 6b7dda7 | 2016-03-15 02:31:38 +0100 | [diff] [blame] | 128 | static int consumerir_get_carrier_freqs(struct consumerir_device *dev, |
| 129 | size_t len, consumerir_freq_range_t *ranges) |
| 130 | { |
| 131 | size_t to_copy = consumerir_get_num_carrier_freqs(dev); |
| 132 | |
| 133 | to_copy = len < to_copy ? len : to_copy; |
| 134 | memcpy(ranges, consumerir_freqs, to_copy * sizeof(consumerir_freq_range_t)); |
| 135 | return to_copy; |
| 136 | } |
| 137 | |
| 138 | static int consumerir_close(hw_device_t *dev) |
| 139 | { |
| 140 | free(dev); |
| 141 | close(fd); |
| 142 | pthread_mutex_destroy(&g_mtx); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Christopher N. Hesse | 6b7dda7 | 2016-03-15 02:31:38 +0100 | [diff] [blame] | 146 | /* |
| 147 | * Generic device handling |
| 148 | */ |
| 149 | static int consumerir_open(const hw_module_t *module, const char *name, |
| 150 | hw_device_t **device) |
| 151 | { |
| 152 | if (strcmp(name, CONSUMERIR_TRANSMITTER) != 0) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | if (device == NULL) { |
| 156 | ALOGE("NULL device on open"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | consumerir_device_t *dev = malloc(sizeof(consumerir_device_t)); |
| 161 | memset(dev, 0, sizeof(consumerir_device_t)); |
| 162 | |
| 163 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 164 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 165 | dev->common.version = 0; |
| 166 | dev->common.module = (struct hw_module_t*) module; |
| 167 | dev->common.close = consumerir_close; |
| 168 | |
| 169 | dev->transmit = consumerir_transmit; |
| 170 | dev->get_carrier_freqs = consumerir_get_carrier_freqs; |
| 171 | dev->get_num_carrier_freqs = consumerir_get_num_carrier_freqs; |
| 172 | |
| 173 | pthread_mutex_init(&g_mtx, NULL); |
| 174 | |
| 175 | *device = (hw_device_t*) dev; |
| 176 | fd = open(IR_PATH, O_RDWR); |
| 177 | return fd; |
| 178 | } |
| 179 | |
| 180 | static struct hw_module_methods_t consumerir_module_methods = { |
| 181 | .open = consumerir_open, |
| 182 | }; |
| 183 | |
| 184 | consumerir_module_t HAL_MODULE_INFO_SYM = { |
| 185 | .common = { |
| 186 | .tag = HARDWARE_MODULE_TAG, |
| 187 | .module_api_version = CONSUMERIR_MODULE_API_VERSION_1_0, |
| 188 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 189 | .id = CONSUMERIR_HARDWARE_MODULE_ID, |
| 190 | .name = "Consumer IR Module", |
| 191 | .author = "The CyanogenMod Project", |
| 192 | .methods = &consumerir_module_methods, |
| 193 | }, |
| 194 | }; |