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