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 "ConsumerIrService"
18
19#include <log/log.h>
20
21#include <hardware/hardware.h>
22#include <hardware/consumerir.h>
23
24#include "ConsumerIr.h"
25
26namespace android {
27namespace hardware {
28namespace ir {
29namespace V1_0 {
30namespace implementation {
31
32ConsumerIr::ConsumerIr(consumerir_device_t *device) {
33    mDevice = device;
34}
35
36// Methods from ::android::hardware::consumerir::V1_0::IConsumerIr follow.
37Return<bool> ConsumerIr::transmit(int32_t carrierFreq, const hidl_vec<int32_t>& pattern) {
38    return mDevice->transmit(mDevice, carrierFreq, pattern.data(), pattern.size()) == 0;
39}
40
41Return<void> ConsumerIr::getCarrierFreqs(getCarrierFreqs_cb _hidl_cb) {
42    int32_t len = mDevice->get_num_carrier_freqs(mDevice);
43    if (len < 0) {
44        _hidl_cb(false, {});
45        return Void();
46    }
47
48    consumerir_freq_range_t *rangeAr = new consumerir_freq_range_t[len];
49    bool success = (mDevice->get_carrier_freqs(mDevice, len, rangeAr) >= 0);
50    if (!success) {
51        _hidl_cb(false, {});
52        return Void();
53    }
54
55    hidl_vec<ConsumerIrFreqRange> rangeVec;
56    rangeVec.resize(len);
57    for (int32_t i = 0; i < len; i++) {
58        rangeVec[i].min = static_cast<uint32_t>(rangeAr[i].min);
59        rangeVec[i].max = static_cast<uint32_t>(rangeAr[i].max);
60    }
61    _hidl_cb(true, rangeVec);
62    return Void();
63}
64
65
66IConsumerIr* HIDL_FETCH_IConsumerIr(const char * /*name*/) {
67    consumerir_device_t *dev;
68    const hw_module_t *hw_module = NULL;
69
70    int ret = hw_get_module(CONSUMERIR_HARDWARE_MODULE_ID, &hw_module);
71    if (ret != 0) {
72        ALOGE("hw_get_module %s failed: %d", CONSUMERIR_HARDWARE_MODULE_ID, ret);
73        return nullptr;
74    }
75    ret = hw_module->methods->open(hw_module, CONSUMERIR_TRANSMITTER, (hw_device_t **) &dev);
76    if (ret < 0) {
77        ALOGE("Can't open consumer IR transmitter, error: %d", ret);
78        return nullptr;
79    }
80    return new ConsumerIr(dev);
81}
82
83}  // namespace implementation
84}  // namespace V1_0
85}  // namespace ir
86}  // namespace hardware
87}  // namespace android
88