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 "android.hardware.tv.cec@1.0-impl"
18 #include <android-base/logging.h>
19
20 #include <hardware/hardware.h>
21 #include <hardware/hdmi_cec.h>
22 #include "HdmiCec.h"
23 #include "HdmiCecDefault.h"
24
25 namespace android {
26 namespace hardware {
27 namespace tv {
28 namespace cec {
29 namespace V1_0 {
30 namespace implementation {
31
32 static_assert(CEC_DEVICE_INACTIVE == static_cast<int>(CecDeviceType::INACTIVE),
33 "CecDeviceType::INACTIVE must match legacy value.");
34 static_assert(CEC_DEVICE_TV == static_cast<int>(CecDeviceType::TV),
35 "CecDeviceType::TV must match legacy value.");
36 static_assert(CEC_DEVICE_RECORDER == static_cast<int>(CecDeviceType::RECORDER),
37 "CecDeviceType::RECORDER must match legacy value.");
38 static_assert(CEC_DEVICE_TUNER == static_cast<int>(CecDeviceType::TUNER),
39 "CecDeviceType::TUNER must match legacy value.");
40 static_assert(CEC_DEVICE_PLAYBACK == static_cast<int>(CecDeviceType::PLAYBACK),
41 "CecDeviceType::PLAYBACK must match legacy value.");
42 static_assert(CEC_DEVICE_AUDIO_SYSTEM == static_cast<int>(CecDeviceType::AUDIO_SYSTEM),
43 "CecDeviceType::AUDIO_SYSTEM must match legacy value.");
44 static_assert(CEC_DEVICE_MAX == static_cast<int>(CecDeviceType::MAX),
45 "CecDeviceType::MAX must match legacy value.");
46
47 static_assert(CEC_ADDR_TV == static_cast<int>(CecLogicalAddress::TV),
48 "CecLogicalAddress::TV must match legacy value.");
49 static_assert(CEC_ADDR_RECORDER_1 == static_cast<int>(CecLogicalAddress::RECORDER_1),
50 "CecLogicalAddress::RECORDER_1 must match legacy value.");
51 static_assert(CEC_ADDR_RECORDER_2 == static_cast<int>(CecLogicalAddress::RECORDER_2),
52 "CecLogicalAddress::RECORDER_2 must match legacy value.");
53 static_assert(CEC_ADDR_TUNER_1 == static_cast<int>(CecLogicalAddress::TUNER_1),
54 "CecLogicalAddress::TUNER_1 must match legacy value.");
55 static_assert(CEC_ADDR_PLAYBACK_1 == static_cast<int>(CecLogicalAddress::PLAYBACK_1),
56 "CecLogicalAddress::PLAYBACK_1 must match legacy value.");
57 static_assert(CEC_ADDR_AUDIO_SYSTEM == static_cast<int>(CecLogicalAddress::AUDIO_SYSTEM),
58 "CecLogicalAddress::AUDIO_SYSTEM must match legacy value.");
59 static_assert(CEC_ADDR_TUNER_2 == static_cast<int>(CecLogicalAddress::TUNER_2),
60 "CecLogicalAddress::TUNER_2 must match legacy value.");
61 static_assert(CEC_ADDR_TUNER_3 == static_cast<int>(CecLogicalAddress::TUNER_3),
62 "CecLogicalAddress::TUNER_3 must match legacy value.");
63 static_assert(CEC_ADDR_PLAYBACK_2 == static_cast<int>(CecLogicalAddress::PLAYBACK_2),
64 "CecLogicalAddress::PLAYBACK_2 must match legacy value.");
65 static_assert(CEC_ADDR_RECORDER_3 == static_cast<int>(CecLogicalAddress::RECORDER_3),
66 "CecLogicalAddress::RECORDER_3 must match legacy value.");
67 static_assert(CEC_ADDR_TUNER_4 == static_cast<int>(CecLogicalAddress::TUNER_4),
68 "CecLogicalAddress::TUNER_4 must match legacy value.");
69 static_assert(CEC_ADDR_PLAYBACK_3 == static_cast<int>(CecLogicalAddress::PLAYBACK_3),
70 "CecLogicalAddress::PLAYBACK_3 must match legacy value.");
71 static_assert(CEC_ADDR_FREE_USE == static_cast<int>(CecLogicalAddress::FREE_USE),
72 "CecLogicalAddress::FREE_USE must match legacy value.");
73 static_assert(CEC_ADDR_UNREGISTERED == static_cast<int>(CecLogicalAddress::UNREGISTERED),
74 "CecLogicalAddress::UNREGISTERED must match legacy value.");
75 static_assert(CEC_ADDR_BROADCAST == static_cast<int>(CecLogicalAddress::BROADCAST),
76 "CecLogicalAddress::BROADCAST must match legacy value.");
77
78 static_assert(CEC_MESSAGE_FEATURE_ABORT == static_cast<int>(CecMessageType::FEATURE_ABORT),
79 "CecMessageType::FEATURE_ABORT must match legacy value.");
80 static_assert(CEC_MESSAGE_IMAGE_VIEW_ON == static_cast<int>(CecMessageType::IMAGE_VIEW_ON),
81 "CecMessageType::IMAGE_VIEW_ON must match legacy value.");
82 static_assert(CEC_MESSAGE_TUNER_STEP_INCREMENT == static_cast<int>(
83 CecMessageType::TUNER_STEP_INCREMENT),
84 "CecMessageType::TUNER_STEP_INCREMENT must match legacy value.");
85 static_assert(CEC_MESSAGE_TUNER_STEP_DECREMENT == static_cast<int>(
86 CecMessageType::TUNER_STEP_DECREMENT),
87 "CecMessageType::TUNER_STEP_DECREMENT must match legacy value.");
88 static_assert(CEC_MESSAGE_TUNER_DEVICE_STATUS == static_cast<int>(
89 CecMessageType::TUNER_DEVICE_STATUS),
90 "CecMessageType::TUNER_DEVICE_STATUS must match legacy value.");
91 static_assert(CEC_MESSAGE_GIVE_TUNER_DEVICE_STATUS == static_cast<int>(
92 CecMessageType::GIVE_TUNER_DEVICE_STATUS),
93 "CecMessageType::GIVE_TUNER_DEVICE_STATUS must match legacy value.");
94 static_assert(CEC_MESSAGE_RECORD_ON == static_cast<int>(CecMessageType::RECORD_ON),
95 "CecMessageType::RECORD_ON must match legacy value.");
96 static_assert(CEC_MESSAGE_RECORD_STATUS == static_cast<int>(CecMessageType::RECORD_STATUS),
97 "CecMessageType::RECORD_STATUS must match legacy value.");
98 static_assert(CEC_MESSAGE_RECORD_OFF == static_cast<int>(CecMessageType::RECORD_OFF),
99 "CecMessageType::RECORD_OFF must match legacy value.");
100 static_assert(CEC_MESSAGE_TEXT_VIEW_ON == static_cast<int>(CecMessageType::TEXT_VIEW_ON),
101 "CecMessageType::TEXT_VIEW_ON must match legacy value.");
102 static_assert(CEC_MESSAGE_RECORD_TV_SCREEN == static_cast<int>(CecMessageType::RECORD_TV_SCREEN),
103 "CecMessageType::RECORD_TV_SCREEN must match legacy value.");
104 static_assert(CEC_MESSAGE_GIVE_DECK_STATUS == static_cast<int>(CecMessageType::GIVE_DECK_STATUS),
105 "CecMessageType::GIVE_DECK_STATUS must match legacy value.");
106 static_assert(CEC_MESSAGE_STANDBY == static_cast<int>(CecMessageType::STANDBY),
107 "CecMessageType::STANDBY must match legacy value.");
108 static_assert(CEC_MESSAGE_PLAY == static_cast<int>(CecMessageType::PLAY),
109 "CecMessageType::PLAY must match legacy value.");
110 static_assert(CEC_MESSAGE_DECK_CONTROL == static_cast<int>(CecMessageType::DECK_CONTROL),
111 "CecMessageType::DECK_CONTROL must match legacy value.");
112 static_assert(CEC_MESSAGE_TIMER_CLEARED_STATUS == static_cast<int>(
113 CecMessageType::TIMER_CLEARED_STATUS),
114 "CecMessageType::TIMER_CLEARED_STATUS must match legacy value.");
115 static_assert(CEC_MESSAGE_USER_CONTROL_PRESSED == static_cast<int>(
116 CecMessageType::USER_CONTROL_PRESSED),
117 "CecMessageType::USER_CONTROL_PRESSED must match legacy value.");
118 static_assert(CEC_MESSAGE_USER_CONTROL_RELEASED == static_cast<int>(
119 CecMessageType::USER_CONTROL_RELEASED),
120 "CecMessageType::USER_CONTROL_RELEASED must match legacy value.");
121 static_assert(CEC_MESSAGE_GIVE_OSD_NAME == static_cast<int>(CecMessageType::GIVE_OSD_NAME),
122 "CecMessageType::GIVE_OSD_NAME must match legacy value.");
123 static_assert(CEC_MESSAGE_SET_OSD_NAME == static_cast<int>(CecMessageType::SET_OSD_NAME),
124 "CecMessageType::SET_OSD_NAME must match legacy value.");
125 static_assert(CEC_MESSAGE_SYSTEM_AUDIO_MODE_REQUEST == static_cast<int>(
126 CecMessageType::SYSTEM_AUDIO_MODE_REQUEST),
127 "CecMessageType::SYSTEM_AUDIO_MODE_REQUEST must match legacy value.");
128 static_assert(CEC_MESSAGE_GIVE_AUDIO_STATUS == static_cast<int>(CecMessageType::GIVE_AUDIO_STATUS),
129 "CecMessageType::GIVE_AUDIO_STATUS must match legacy value.");
130 static_assert(CEC_MESSAGE_SET_SYSTEM_AUDIO_MODE == static_cast<int>(
131 CecMessageType::SET_SYSTEM_AUDIO_MODE),
132 "CecMessageType::SET_SYSTEM_AUDIO_MODE must match legacy value.");
133 static_assert(CEC_MESSAGE_REPORT_AUDIO_STATUS == static_cast<int>(
134 CecMessageType::REPORT_AUDIO_STATUS),
135 "CecMessageType::REPORT_AUDIO_STATUS must match legacy value.");
136 static_assert(CEC_MESSAGE_GIVE_SYSTEM_AUDIO_MODE_STATUS == static_cast<int>(
137 CecMessageType::GIVE_SYSTEM_AUDIO_MODE_STATUS),
138 "CecMessageType::GIVE_SYSTEM_AUDIO_MODE_STATUS must match legacy value.");
139 static_assert(CEC_MESSAGE_SYSTEM_AUDIO_MODE_STATUS == static_cast<int>(
140 CecMessageType::SYSTEM_AUDIO_MODE_STATUS),
141 "CecMessageType::SYSTEM_AUDIO_MODE_STATUS must match legacy value.");
142 static_assert(CEC_MESSAGE_ROUTING_CHANGE == static_cast<int>(CecMessageType::ROUTING_CHANGE),
143 "CecMessageType::ROUTING_CHANGE must match legacy value.");
144 static_assert(CEC_MESSAGE_ROUTING_INFORMATION == static_cast<int>(
145 CecMessageType::ROUTING_INFORMATION),
146 "CecMessageType::ROUTING_INFORMATION must match legacy value.");
147 static_assert(CEC_MESSAGE_ACTIVE_SOURCE == static_cast<int>(CecMessageType::ACTIVE_SOURCE),
148 "CecMessageType::ACTIVE_SOURCE must match legacy value.");
149 static_assert(CEC_MESSAGE_GIVE_PHYSICAL_ADDRESS == static_cast<int>(
150 CecMessageType::GIVE_PHYSICAL_ADDRESS),
151 "CecMessageType::GIVE_PHYSICAL_ADDRESS must match legacy value.");
152 static_assert(CEC_MESSAGE_REPORT_PHYSICAL_ADDRESS == static_cast<int>(
153 CecMessageType::REPORT_PHYSICAL_ADDRESS),
154 "CecMessageType::REPORT_PHYSICAL_ADDRESS must match legacy value.");
155 static_assert(CEC_MESSAGE_REQUEST_ACTIVE_SOURCE == static_cast<int>(
156 CecMessageType::REQUEST_ACTIVE_SOURCE),
157 "CecMessageType::REQUEST_ACTIVE_SOURCE must match legacy value.");
158 static_assert(CEC_MESSAGE_SET_STREAM_PATH == static_cast<int>(CecMessageType::SET_STREAM_PATH),
159 "CecMessageType::SET_STREAM_PATH must match legacy value.");
160 static_assert(CEC_MESSAGE_DEVICE_VENDOR_ID == static_cast<int>(CecMessageType::DEVICE_VENDOR_ID),
161 "CecMessageType::DEVICE_VENDOR_ID must match legacy value.");
162 static_assert(CEC_MESSAGE_VENDOR_COMMAND == static_cast<int>(CecMessageType::VENDOR_COMMAND),
163 "CecMessageType::VENDOR_COMMAND must match legacy value.");
164 static_assert(CEC_MESSAGE_VENDOR_REMOTE_BUTTON_DOWN == static_cast<int>(
165 CecMessageType::VENDOR_REMOTE_BUTTON_DOWN),
166 "CecMessageType::VENDOR_REMOTE_BUTTON_DOWN must match legacy value.");
167 static_assert(CEC_MESSAGE_VENDOR_REMOTE_BUTTON_UP == static_cast<int>(
168 CecMessageType::VENDOR_REMOTE_BUTTON_UP),
169 "CecMessageType::VENDOR_REMOTE_BUTTON_UP must match legacy value.");
170 static_assert(CEC_MESSAGE_GIVE_DEVICE_VENDOR_ID == static_cast<int>(
171 CecMessageType::GIVE_DEVICE_VENDOR_ID),
172 "CecMessageType::GIVE_DEVICE_VENDOR_ID must match legacy value.");
173 static_assert(CEC_MESSAGE_MENU_REQUEST == static_cast<int>(CecMessageType::MENU_REQUEST),
174 "CecMessageType::MENU_REQUEST must match legacy value.");
175 static_assert(CEC_MESSAGE_MENU_STATUS == static_cast<int>(CecMessageType::MENU_STATUS),
176 "CecMessageType::MENU_STATUS must match legacy value.");
177 static_assert(CEC_MESSAGE_GIVE_DEVICE_POWER_STATUS == static_cast<int>(
178 CecMessageType::GIVE_DEVICE_POWER_STATUS),
179 "CecMessageType::GIVE_DEVICE_POWER_STATUS must match legacy value.");
180 static_assert(CEC_MESSAGE_REPORT_POWER_STATUS == static_cast<int>(
181 CecMessageType::REPORT_POWER_STATUS),
182 "CecMessageType::REPORT_POWER_STATUS must match legacy value.");
183 static_assert(CEC_MESSAGE_GET_MENU_LANGUAGE == static_cast<int>(CecMessageType::GET_MENU_LANGUAGE),
184 "CecMessageType::GET_MENU_LANGUAGE must match legacy value.");
185 static_assert(CEC_MESSAGE_SELECT_ANALOG_SERVICE == static_cast<int>(
186 CecMessageType::SELECT_ANALOG_SERVICE),
187 "CecMessageType::SELECT_ANALOG_SERVICE must match legacy value.");
188 static_assert(CEC_MESSAGE_SELECT_DIGITAL_SERVICE == static_cast<int>(
189 CecMessageType::SELECT_DIGITAL_SERVICE),
190 "CecMessageType::SELECT_DIGITAL_SERVICE must match legacy value.");
191 static_assert(CEC_MESSAGE_SET_DIGITAL_TIMER == static_cast<int>(CecMessageType::SET_DIGITAL_TIMER),
192 "CecMessageType::SET_DIGITAL_TIMER must match legacy value.");
193 static_assert(CEC_MESSAGE_CLEAR_DIGITAL_TIMER == static_cast<int>(
194 CecMessageType::CLEAR_DIGITAL_TIMER),
195 "CecMessageType::CLEAR_DIGITAL_TIMER must match legacy value.");
196 static_assert(CEC_MESSAGE_SET_AUDIO_RATE == static_cast<int>(CecMessageType::SET_AUDIO_RATE),
197 "CecMessageType::SET_AUDIO_RATE must match legacy value.");
198 static_assert(CEC_MESSAGE_INACTIVE_SOURCE == static_cast<int>(CecMessageType::INACTIVE_SOURCE),
199 "CecMessageType::INACTIVE_SOURCE must match legacy value.");
200 static_assert(CEC_MESSAGE_CEC_VERSION == static_cast<int>(CecMessageType::CEC_VERSION),
201 "CecMessageType::CEC_VERSION must match legacy value.");
202 static_assert(CEC_MESSAGE_GET_CEC_VERSION == static_cast<int>(CecMessageType::GET_CEC_VERSION),
203 "CecMessageType::GET_CEC_VERSION must match legacy value.");
204 static_assert(CEC_MESSAGE_VENDOR_COMMAND_WITH_ID == static_cast<int>(
205 CecMessageType::VENDOR_COMMAND_WITH_ID),
206 "CecMessageType::VENDOR_COMMAND_WITH_ID must match legacy value.");
207 static_assert(CEC_MESSAGE_CLEAR_EXTERNAL_TIMER == static_cast<int>(
208 CecMessageType::CLEAR_EXTERNAL_TIMER),
209 "CecMessageType::CLEAR_EXTERNAL_TIMER must match legacy value.");
210 static_assert(CEC_MESSAGE_SET_EXTERNAL_TIMER == static_cast<int>(
211 CecMessageType::SET_EXTERNAL_TIMER),
212 "CecMessageType::SET_EXTERNAL_TIMER must match legacy value.");
213 static_assert(CEC_MESSAGE_INITIATE_ARC == static_cast<int>(CecMessageType::INITIATE_ARC),
214 "CecMessageType::INITIATE_ARC must match legacy value.");
215 static_assert(CEC_MESSAGE_REPORT_ARC_INITIATED == static_cast<int>(
216 CecMessageType::REPORT_ARC_INITIATED),
217 "CecMessageType::REPORT_ARC_INITIATED must match legacy value.");
218 static_assert(CEC_MESSAGE_REPORT_ARC_TERMINATED == static_cast<int>(
219 CecMessageType::REPORT_ARC_TERMINATED),
220 "CecMessageType::REPORT_ARC_TERMINATED must match legacy value.");
221 static_assert(CEC_MESSAGE_REQUEST_ARC_INITIATION == static_cast<int>(
222 CecMessageType::REQUEST_ARC_INITIATION),
223 "CecMessageType::REQUEST_ARC_INITIATION must match legacy value.");
224 static_assert(CEC_MESSAGE_REQUEST_ARC_TERMINATION == static_cast<int>(
225 CecMessageType::REQUEST_ARC_TERMINATION),
226 "CecMessageType::REQUEST_ARC_TERMINATION must match legacy value.");
227 static_assert(CEC_MESSAGE_TERMINATE_ARC == static_cast<int>(CecMessageType::TERMINATE_ARC),
228 "CecMessageType::TERMINATE_ARC must match legacy value.");
229 static_assert(CEC_MESSAGE_ABORT == static_cast<int>(CecMessageType::ABORT),
230 "CecMessageType::ABORT must match legacy value.");
231
232 static_assert(ABORT_UNRECOGNIZED_MODE == static_cast<int>(AbortReason::UNRECOGNIZED_MODE),
233 "AbortReason::UNRECOGNIZED_MODE must match legacy value.");
234 static_assert(ABORT_NOT_IN_CORRECT_MODE == static_cast<int>(AbortReason::NOT_IN_CORRECT_MODE),
235 "AbortReason::NOT_IN_CORRECT_MODE must match legacy value.");
236 static_assert(ABORT_CANNOT_PROVIDE_SOURCE == static_cast<int>(AbortReason::CANNOT_PROVIDE_SOURCE),
237 "AbortReason::CANNOT_PROVIDE_SOURCE must match legacy value.");
238 static_assert(ABORT_INVALID_OPERAND == static_cast<int>(AbortReason::INVALID_OPERAND),
239 "AbortReason::INVALID_OPERAND must match legacy value.");
240 static_assert(ABORT_REFUSED == static_cast<int>(AbortReason::REFUSED),
241 "AbortReason::REFUSED must match legacy value.");
242 static_assert(ABORT_UNABLE_TO_DETERMINE == static_cast<int>(AbortReason::UNABLE_TO_DETERMINE),
243 "AbortReason::UNABLE_TO_DETERMINE must match legacy value.");
244
245 static_assert(HDMI_RESULT_SUCCESS == static_cast<int>(SendMessageResult::SUCCESS),
246 "SendMessageResult::SUCCESS must match legacy value.");
247 static_assert(HDMI_RESULT_NACK == static_cast<int>(SendMessageResult::NACK),
248 "SendMessageResult::NACK must match legacy value.");
249 static_assert(HDMI_RESULT_BUSY == static_cast<int>(SendMessageResult::BUSY),
250 "SendMessageResult::BUSY must match legacy value.");
251 static_assert(HDMI_RESULT_FAIL == static_cast<int>(SendMessageResult::FAIL),
252 "SendMessageResult::FAIL must match legacy value.");
253
254 static_assert(HDMI_INPUT == static_cast<int>(HdmiPortType::INPUT),
255 "HdmiPortType::INPUT must match legacy value.");
256 static_assert(HDMI_OUTPUT == static_cast<int>(HdmiPortType::OUTPUT),
257 "HdmiPortType::OUTPUT must match legacy value.");
258
259 static_assert(HDMI_OPTION_WAKEUP == static_cast<int>(OptionKey::WAKEUP),
260 "OptionKey::WAKEUP must match legacy value.");
261 static_assert(HDMI_OPTION_ENABLE_CEC == static_cast<int>(OptionKey::ENABLE_CEC),
262 "OptionKey::ENABLE_CEC must match legacy value.");
263 static_assert(HDMI_OPTION_SYSTEM_CEC_CONTROL == static_cast<int>(OptionKey::SYSTEM_CEC_CONTROL),
264 "OptionKey::SYSTEM_CEC_CONTROL must match legacy value.");
265
266 sp<IHdmiCecCallback> HdmiCec::mCallback = nullptr;
267
HdmiCec(hdmi_cec_device_t * device)268 HdmiCec::HdmiCec(hdmi_cec_device_t* device) : mDevice(device) {}
269
270 // Methods from ::android::hardware::tv::cec::V1_0::IHdmiCec follow.
addLogicalAddress(CecLogicalAddress addr)271 Return<Result> HdmiCec::addLogicalAddress(CecLogicalAddress addr) {
272 int ret = mDevice->add_logical_address(mDevice, static_cast<cec_logical_address_t>(addr));
273 switch (ret) {
274 case 0:
275 return Result::SUCCESS;
276 case -EINVAL:
277 return Result::FAILURE_INVALID_ARGS;
278 case -ENOTSUP:
279 return Result::FAILURE_NOT_SUPPORTED;
280 case -EBUSY:
281 return Result::FAILURE_BUSY;
282 default:
283 return Result::FAILURE_UNKNOWN;
284 }
285 }
286
clearLogicalAddress()287 Return<void> HdmiCec::clearLogicalAddress() {
288 mDevice->clear_logical_address(mDevice);
289 return Void();
290 }
291
getPhysicalAddress(getPhysicalAddress_cb _hidl_cb)292 Return<void> HdmiCec::getPhysicalAddress(getPhysicalAddress_cb _hidl_cb) {
293 uint16_t addr;
294 int ret = mDevice->get_physical_address(mDevice, &addr);
295 switch (ret) {
296 case 0:
297 _hidl_cb(Result::SUCCESS, addr);
298 break;
299 case -EBADF:
300 _hidl_cb(Result::FAILURE_INVALID_STATE, addr);
301 break;
302 default:
303 _hidl_cb(Result::FAILURE_UNKNOWN, addr);
304 break;
305 }
306 return Void();
307 }
308
sendMessage(const CecMessage & message)309 Return<SendMessageResult> HdmiCec::sendMessage(const CecMessage& message) {
310 if (message.body.size() > CEC_MESSAGE_BODY_MAX_LENGTH) {
311 return SendMessageResult::FAIL;
312 }
313 cec_message_t legacyMessage {
314 .initiator = static_cast<cec_logical_address_t>(message.initiator),
315 .destination = static_cast<cec_logical_address_t>(message.destination),
316 .length = message.body.size(),
317 };
318 for (size_t i = 0; i < message.body.size(); ++i) {
319 legacyMessage.body[i] = static_cast<unsigned char>(message.body[i]);
320 }
321 return static_cast<SendMessageResult>(mDevice->send_message(mDevice, &legacyMessage));
322 }
323
setCallback(const sp<IHdmiCecCallback> & callback)324 Return<void> HdmiCec::setCallback(const sp<IHdmiCecCallback>& callback) {
325 if (mCallback != nullptr) {
326 mCallback->unlinkToDeath(this);
327 mCallback = nullptr;
328 }
329
330 if (callback != nullptr) {
331 mCallback = callback;
332 mCallback->linkToDeath(this, 0 /*cookie*/);
333 mDevice->register_event_callback(mDevice, eventCallback, nullptr);
334 }
335 return Void();
336 }
337
getCecVersion()338 Return<int32_t> HdmiCec::getCecVersion() {
339 int version;
340 mDevice->get_version(mDevice, &version);
341 return static_cast<int32_t>(version);
342 }
343
getVendorId()344 Return<uint32_t> HdmiCec::getVendorId() {
345 uint32_t vendor_id;
346 mDevice->get_vendor_id(mDevice, &vendor_id);
347 return vendor_id;
348 }
349
getPortInfo(getPortInfo_cb _hidl_cb)350 Return<void> HdmiCec::getPortInfo(getPortInfo_cb _hidl_cb) {
351 struct hdmi_port_info* legacyPorts;
352 int numPorts;
353 hidl_vec<HdmiPortInfo> portInfos;
354 mDevice->get_port_info(mDevice, &legacyPorts, &numPorts);
355 portInfos.resize(numPorts);
356 for (int i = 0; i < numPorts; ++i) {
357 portInfos[i] = {
358 .type = static_cast<HdmiPortType>(legacyPorts[i].type),
359 .portId = static_cast<uint32_t>(legacyPorts[i].port_id),
360 .cecSupported = legacyPorts[i].cec_supported != 0,
361 .arcSupported = legacyPorts[i].arc_supported != 0,
362 .physicalAddress = legacyPorts[i].physical_address
363 };
364 }
365 _hidl_cb(portInfos);
366 return Void();
367 }
368
setOption(OptionKey key,bool value)369 Return<void> HdmiCec::setOption(OptionKey key, bool value) {
370 mDevice->set_option(mDevice, static_cast<int>(key), value ? 1 : 0);
371 return Void();
372 }
373
setLanguage(const hidl_string & language)374 Return<void> HdmiCec::setLanguage(const hidl_string& language) {
375 if (language.size() != 3) {
376 LOG(ERROR) << "Wrong language code: expected 3 letters, but it was " << language.size()
377 << ".";
378 return Void();
379 }
380 const char *languageStr = language.c_str();
381 int convertedLanguage = ((languageStr[0] & 0xFF) << 16)
382 | ((languageStr[1] & 0xFF) << 8)
383 | (languageStr[2] & 0xFF);
384 mDevice->set_option(mDevice, HDMI_OPTION_SET_LANG, convertedLanguage);
385 return Void();
386 }
387
enableAudioReturnChannel(int32_t portId,bool enable)388 Return<void> HdmiCec::enableAudioReturnChannel(int32_t portId, bool enable) {
389 mDevice->set_audio_return_channel(mDevice, portId, enable ? 1 : 0);
390 return Void();
391 }
392
isConnected(int32_t portId)393 Return<bool> HdmiCec::isConnected(int32_t portId) {
394 return mDevice->is_connected(mDevice, portId) > 0;
395 }
396
getHdmiCecDefault()397 IHdmiCec* getHdmiCecDefault() {
398 HdmiCecDefault* hdmiCecDefault = new HdmiCecDefault();
399 Result result = hdmiCecDefault->init();
400 if (result == Result::SUCCESS) {
401 return hdmiCecDefault;
402 }
403 LOG(ERROR) << "Failed to load default HAL.";
404 return nullptr;
405 }
406
HIDL_FETCH_IHdmiCec(const char * hal)407 IHdmiCec* HIDL_FETCH_IHdmiCec(const char* hal) {
408 hdmi_cec_device_t* hdmi_cec_device;
409 int ret = 0;
410 const hw_module_t* hw_module = nullptr;
411
412 ret = hw_get_module (HDMI_CEC_HARDWARE_MODULE_ID, &hw_module);
413 if (ret == 0) {
414 ret = hdmi_cec_open (hw_module, &hdmi_cec_device);
415 if (ret != 0) {
416 LOG(ERROR) << "hdmi_cec_open " << hal << " failed: " << ret;
417 }
418 } else {
419 LOG(ERROR) << "hw_get_module " << hal << " failed: " << ret;
420 }
421
422 if (ret == 0) {
423 return new HdmiCec(hdmi_cec_device);
424 } else {
425 LOG(ERROR) << "Passthrough failed to load legacy HAL.";
426 return getHdmiCecDefault();
427 }
428 }
429
430 } // namespace implementation
431 } // namespace V1_0
432 } // namespace cec
433 } // namespace tv
434 } // namespace hardware
435 } // namespace android
436