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