1/*
2 * Copyright (C) 2013 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#define LOG_TAG "ConsumerIrHal"
17
18#include <errno.h>
19#include <string.h>
20#include <cutils/log.h>
21#include <hardware/hardware.h>
22#include <hardware/consumerir.h>
23
24#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
25
26static const consumerir_freq_range_t consumerir_freqs[] = {
27    {.min = 30000, .max = 30000},
28    {.min = 33000, .max = 33000},
29    {.min = 36000, .max = 36000},
30    {.min = 38000, .max = 38000},
31    {.min = 40000, .max = 40000},
32    {.min = 56000, .max = 56000},
33};
34
35static int consumerir_transmit(struct consumerir_device *dev __unused,
36   int carrier_freq, const int pattern[], int pattern_len)
37{
38    int total_time = 0;
39    long i;
40
41    for (i = 0; i < pattern_len; i++)
42        total_time += pattern[i];
43
44    /* simulate the time spent transmitting by sleeping */
45    ALOGD("transmit for %d uS at %d Hz", total_time, carrier_freq);
46    usleep(total_time);
47
48    return 0;
49}
50
51static int consumerir_get_num_carrier_freqs(struct consumerir_device *dev __unused)
52{
53    return ARRAY_SIZE(consumerir_freqs);
54}
55
56static int consumerir_get_carrier_freqs(struct consumerir_device *dev __unused,
57    size_t len, consumerir_freq_range_t *ranges)
58{
59    size_t to_copy = ARRAY_SIZE(consumerir_freqs);
60
61    to_copy = len < to_copy ? len : to_copy;
62    memcpy(ranges, consumerir_freqs, to_copy * sizeof(consumerir_freq_range_t));
63    return to_copy;
64}
65
66static int consumerir_close(hw_device_t *dev)
67{
68    free(dev);
69    return 0;
70}
71
72/*
73 * Generic device handling
74 */
75static int consumerir_open(const hw_module_t* module, const char* name,
76        hw_device_t** device)
77{
78    if (strcmp(name, CONSUMERIR_TRANSMITTER) != 0) {
79        return -EINVAL;
80    }
81    if (device == NULL) {
82        ALOGE("NULL device on open");
83        return -EINVAL;
84    }
85
86    consumerir_device_t *dev = malloc(sizeof(consumerir_device_t));
87    memset(dev, 0, sizeof(consumerir_device_t));
88
89    dev->common.tag = HARDWARE_DEVICE_TAG;
90    dev->common.version = 0;
91    dev->common.module = (struct hw_module_t*) module;
92    dev->common.close = consumerir_close;
93
94    dev->transmit = consumerir_transmit;
95    dev->get_num_carrier_freqs = consumerir_get_num_carrier_freqs;
96    dev->get_carrier_freqs = consumerir_get_carrier_freqs;
97
98    *device = (hw_device_t*) dev;
99    return 0;
100}
101
102static struct hw_module_methods_t consumerir_module_methods = {
103    .open = consumerir_open,
104};
105
106consumerir_module_t HAL_MODULE_INFO_SYM = {
107    .common = {
108        .tag                = HARDWARE_MODULE_TAG,
109        .module_api_version = CONSUMERIR_MODULE_API_VERSION_1_0,
110        .hal_api_version    = HARDWARE_HAL_API_VERSION,
111        .id                 = CONSUMERIR_HARDWARE_MODULE_ID,
112        .name               = "Demo IR HAL",
113        .author             = "The Android Open Source Project",
114        .methods            = &consumerir_module_methods,
115    },
116};
117