blob: eba8f95f57b82e29643025d35c1c65018f1af108 [file] [log] [blame]
Amye4ddc0d2019-06-13 19:21:24 -07001/*
2 * Copyright (C) 2019 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 "android.hardware.tv.cec@1.0-mock"
18#include <android-base/logging.h>
19#include <utils/Log.h>
20
21#include <hardware/hardware.h>
22#include <hardware/hdmi_cec.h>
23#include "HdmiCecMock.h"
24
25namespace android {
26namespace hardware {
27namespace tv {
28namespace cec {
29namespace V1_0 {
30namespace implementation {
31
32/*
33 * (*set_option)() passes flags controlling the way HDMI-CEC service works down
34 * to HAL implementation. Those flags will be used in case the feature needs
35 * update in HAL itself, firmware or microcontroller.
36 */
37void HdmiCecMock::cec_set_option(int flag, int value) {
38 // maintain options and set them accordingly
39 switch (flag) {
40 case HDMI_OPTION_WAKEUP:
41 mOptionWakeUp = value;
42 break;
43 case HDMI_OPTION_ENABLE_CEC:
44 mOptionEnableCec = value;
45 break;
46 case HDMI_OPTION_SYSTEM_CEC_CONTROL:
47 mOptionSystemCecControl = value;
48 break;
49 case HDMI_OPTION_SET_LANG:
50 mOptionLanguage = value;
51 }
52}
53
54// Methods from ::android::hardware::tv::cec::V1_0::IHdmiCec follow.
55Return<Result> HdmiCecMock::addLogicalAddress(CecLogicalAddress addr) {
56 // have a list to maintain logical addresses
57 int size = mLogicalAddresses.size();
58 mLogicalAddresses.resize(size + 1);
59 mLogicalAddresses[size + 1] = addr;
60 return Result::SUCCESS;
61}
62
63Return<void> HdmiCecMock::clearLogicalAddress() {
64 // remove logical address from the list
65 mLogicalAddresses = {};
66 return Void();
67}
68
69Return<void> HdmiCecMock::getPhysicalAddress(getPhysicalAddress_cb _hidl_cb) {
70 // maintain a physical address and return it
71 // default 0xFFFF, update on hotplug event
72 _hidl_cb(Result::SUCCESS, mPhysicalAddress);
73 return Void();
74}
75
76Return<SendMessageResult> HdmiCecMock::sendMessage(const CecMessage& message) {
77 if (message.body.size() == 0) {
78 return SendMessageResult::NACK;
79 }
80 return SendMessageResult::SUCCESS;
81}
82
83Return<void> HdmiCecMock::setCallback(const sp<IHdmiCecCallback>& callback) {
84 if (mCallback != nullptr) {
85 mCallback = nullptr;
86 }
87
88 if (callback != nullptr) {
89 mCallback = callback;
90 mCallback->linkToDeath(this, 0 /*cookie*/);
91 }
92 return Void();
93}
94
95Return<int32_t> HdmiCecMock::getCecVersion() {
96 // maintain a cec version and return it
97 return mCecVersion;
98}
99
100Return<uint32_t> HdmiCecMock::getVendorId() {
101 return mCecVendorId;
102}
103
104Return<void> HdmiCecMock::getPortInfo(getPortInfo_cb _hidl_cb) {
105 vector<HdmiPortInfo> portInfos;
106 // TODO ready port info from device specific config
107 portInfos.resize(mTotalPorts);
108 for (int i = 0; i < mTotalPorts; ++i) {
109 portInfos[i] = {.type = HdmiPortType::INPUT,
110 .portId = static_cast<uint32_t>(i),
111 .cecSupported = true,
112 .arcSupported = (i == 0),
113 .physicalAddress = static_cast<uint16_t>(i << 12)};
114 }
115 _hidl_cb(portInfos);
116 return Void();
117}
118
119Return<void> HdmiCecMock::setOption(OptionKey key, bool value) {
120 cec_set_option(static_cast<int>(key), value ? 1 : 0);
121 return Void();
122}
123
124Return<void> HdmiCecMock::setLanguage(const hidl_string& language) {
125 if (language.size() != 3) {
126 LOG(ERROR) << "Wrong language code: expected 3 letters, but it was " << language.size()
127 << ".";
128 return Void();
129 }
130 // TODO validate if language is a valid language code
131 const char* languageStr = language.c_str();
132 int convertedLanguage = ((languageStr[0] & 0xFF) << 16) | ((languageStr[1] & 0xFF) << 8) |
133 (languageStr[2] & 0xFF);
134 cec_set_option(HDMI_OPTION_SET_LANG, convertedLanguage);
135 return Void();
136}
137
138Return<void> HdmiCecMock::enableAudioReturnChannel(int32_t portId __unused, bool enable __unused) {
139 // Maintain ARC status
140 return Void();
141}
142
143Return<bool> HdmiCecMock::isConnected(int32_t portId __unused) {
144 // maintain port connection status and update on hotplug event
145 return false;
146}
147
148HdmiCecMock::HdmiCecMock() {
149 ALOGE("Opening a virtual HAL for testing and virtual machine.");
150}
151
152} // namespace implementation
153} // namespace V1_0
154} // namespace cec
155} // namespace tv
156} // namespace hardware
157} // namespace android