1 /*
2 * Copyright (C) 2021 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 #include <android-base/properties.h>
20
21 #include <cutils/properties.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <linux/ioctl.h>
26 #include <poll.h>
27
28 #include "HdmiCecDefault.h"
29
30 #define PROPERTY_DEVICE_TYPE "ro.hdmi.device_type"
31 #define MIN_PORT_ID 0
32 #define MAX_PORT_ID 15
33 #define INVALID_PHYSICAL_ADDRESS 0xFFFF
34
35 namespace android {
36 namespace hardware {
37 namespace tv {
38 namespace cec {
39 namespace V1_0 {
40 namespace implementation {
41
42 using android::base::GetUintProperty;
43 using std::stoi;
44 using std::string;
45
HdmiCecDefault()46 HdmiCecDefault::HdmiCecDefault() {
47 mCecEnabled = false;
48 mWakeupEnabled = false;
49 mCecControlEnabled = false;
50 mCallback = nullptr;
51 }
52
~HdmiCecDefault()53 HdmiCecDefault::~HdmiCecDefault() {
54 release();
55 }
56
57 // Methods from ::android::hardware::tv::cec::V1_0::IHdmiCec follow.
addLogicalAddress(CecLogicalAddress addr)58 Return<Result> HdmiCecDefault::addLogicalAddress(CecLogicalAddress addr) {
59 if (addr < CecLogicalAddress::TV || addr >= CecLogicalAddress::BROADCAST) {
60 LOG(ERROR) << "Add logical address failed, Invalid address";
61 return Result::FAILURE_INVALID_ARGS;
62 }
63
64 cec_log_addrs cecLogAddrs;
65 int ret = ioctl(mHdmiCecPorts[MIN_PORT_ID]->mCecFd, CEC_ADAP_G_LOG_ADDRS, &cecLogAddrs);
66 if (ret) {
67 LOG(ERROR) << "Add logical address failed, Error = " << strerror(errno);
68 return Result::FAILURE_BUSY;
69 }
70
71 cecLogAddrs.cec_version = getCecVersion();
72 cecLogAddrs.vendor_id = getVendorId();
73
74 unsigned int logAddrType = CEC_LOG_ADDR_TYPE_UNREGISTERED;
75 unsigned int allDevTypes = 0;
76 unsigned int primDevType = 0xff;
77 switch (addr) {
78 case CecLogicalAddress::TV:
79 primDevType = CEC_OP_PRIM_DEVTYPE_TV;
80 logAddrType = CEC_LOG_ADDR_TYPE_TV;
81 allDevTypes = CEC_OP_ALL_DEVTYPE_TV;
82 break;
83 case CecLogicalAddress::RECORDER_1:
84 case CecLogicalAddress::RECORDER_2:
85 case CecLogicalAddress::RECORDER_3:
86 primDevType = CEC_OP_PRIM_DEVTYPE_RECORD;
87 logAddrType = CEC_LOG_ADDR_TYPE_RECORD;
88 allDevTypes = CEC_OP_ALL_DEVTYPE_RECORD;
89 break;
90 case CecLogicalAddress::TUNER_1:
91 case CecLogicalAddress::TUNER_2:
92 case CecLogicalAddress::TUNER_3:
93 case CecLogicalAddress::TUNER_4:
94 primDevType = CEC_OP_PRIM_DEVTYPE_TUNER;
95 logAddrType = CEC_LOG_ADDR_TYPE_TUNER;
96 allDevTypes = CEC_OP_ALL_DEVTYPE_TUNER;
97 break;
98 case CecLogicalAddress::PLAYBACK_1:
99 case CecLogicalAddress::PLAYBACK_2:
100 case CecLogicalAddress::PLAYBACK_3:
101 primDevType = CEC_OP_PRIM_DEVTYPE_PLAYBACK;
102 logAddrType = CEC_LOG_ADDR_TYPE_PLAYBACK;
103 allDevTypes = CEC_OP_ALL_DEVTYPE_PLAYBACK;
104 cecLogAddrs.flags |= CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU;
105 break;
106 case CecLogicalAddress::AUDIO_SYSTEM:
107 primDevType = CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM;
108 logAddrType = CEC_LOG_ADDR_TYPE_AUDIOSYSTEM;
109 allDevTypes = CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM;
110 break;
111 case CecLogicalAddress::FREE_USE:
112 primDevType = CEC_OP_PRIM_DEVTYPE_PROCESSOR;
113 logAddrType = CEC_LOG_ADDR_TYPE_SPECIFIC;
114 allDevTypes = CEC_OP_ALL_DEVTYPE_SWITCH;
115 break;
116 case CecLogicalAddress::UNREGISTERED:
117 cecLogAddrs.flags |= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK;
118 break;
119 }
120
121 int logAddrIndex = cecLogAddrs.num_log_addrs;
122
123 cecLogAddrs.num_log_addrs += 1;
124 cecLogAddrs.log_addr[logAddrIndex] = static_cast<cec_logical_address_t>(addr);
125 cecLogAddrs.log_addr_type[logAddrIndex] = logAddrType;
126 cecLogAddrs.primary_device_type[logAddrIndex] = primDevType;
127 cecLogAddrs.all_device_types[logAddrIndex] = allDevTypes;
128 cecLogAddrs.features[logAddrIndex][0] = 0;
129 cecLogAddrs.features[logAddrIndex][1] = 0;
130
131 // Return failure only if add logical address fails for all the ports
132 Return<Result> result = Result::FAILURE_BUSY;
133 for (int i = 0; i < mHdmiCecPorts.size(); i++) {
134 ret = ioctl(mHdmiCecPorts[i]->mCecFd, CEC_ADAP_S_LOG_ADDRS, &cecLogAddrs);
135 if (ret) {
136 LOG(ERROR) << "Add logical address failed for port " << mHdmiCecPorts[i]->mPortId
137 << ", Error = " << strerror(errno);
138 } else {
139 result = Result::SUCCESS;
140 }
141 }
142 return result;
143 }
144
clearLogicalAddress()145 Return<void> HdmiCecDefault::clearLogicalAddress() {
146 cec_log_addrs cecLogAddrs;
147 memset(&cecLogAddrs, 0, sizeof(cecLogAddrs));
148 for (int i = 0; i < mHdmiCecPorts.size(); i++) {
149 int ret = ioctl(mHdmiCecPorts[i]->mCecFd, CEC_ADAP_S_LOG_ADDRS, &cecLogAddrs);
150 if (ret) {
151 LOG(ERROR) << "Clear logical Address failed for port " << mHdmiCecPorts[i]->mPortId
152 << ", Error = " << strerror(errno);
153 }
154 }
155 return Void();
156 }
157
getPhysicalAddress(getPhysicalAddress_cb callback)158 Return<void> HdmiCecDefault::getPhysicalAddress(getPhysicalAddress_cb callback) {
159 uint16_t addr;
160 int ret = ioctl(mHdmiCecPorts[MIN_PORT_ID]->mCecFd, CEC_ADAP_G_PHYS_ADDR, &addr);
161 if (ret) {
162 LOG(ERROR) << "Get physical address failed, Error = " << strerror(errno);
163 callback(Result::FAILURE_INVALID_STATE, addr);
164 return Void();
165 }
166 callback(Result::SUCCESS, addr);
167 return Void();
168 }
169
sendMessage(const CecMessage & message)170 Return<SendMessageResult> HdmiCecDefault::sendMessage(const CecMessage& message) {
171 if (!mCecEnabled) {
172 return SendMessageResult::FAIL;
173 }
174
175 cec_msg cecMsg;
176 memset(&cecMsg, 0, sizeof(cec_msg));
177
178 int initiator = static_cast<cec_logical_address_t>(message.initiator);
179 int destination = static_cast<cec_logical_address_t>(message.destination);
180
181 cecMsg.msg[0] = (initiator << 4) | destination;
182 for (size_t i = 0; i < message.body.size(); ++i) {
183 cecMsg.msg[i + 1] = message.body[i];
184 }
185 cecMsg.len = message.body.size() + 1;
186
187 // Return failure only if send message fails for all the ports
188 Return<SendMessageResult> result = SendMessageResult::FAIL;
189 for (int i = 0; i < mHdmiCecPorts.size(); i++) {
190 int ret = ioctl(mHdmiCecPorts[i]->mCecFd, CEC_TRANSMIT, &cecMsg);
191
192 if (ret) {
193 LOG(ERROR) << "Send message failed, Error = " << strerror(errno);
194 continue;
195 }
196
197 if (cecMsg.tx_status != CEC_TX_STATUS_OK) {
198 LOG(ERROR) << "Send message tx_status = " << cecMsg.tx_status;
199 }
200
201 if (result != SendMessageResult::SUCCESS) {
202 result = getSendMessageResult(cecMsg.tx_status);
203 }
204 }
205 return result;
206 }
207
setCallback(const sp<IHdmiCecCallback> & callback)208 Return<void> HdmiCecDefault::setCallback(const sp<IHdmiCecCallback>& callback) {
209 if (mCallback != nullptr) {
210 mCallback->unlinkToDeath(this);
211 mCallback = nullptr;
212 }
213
214 if (callback != nullptr) {
215 mCallback = callback;
216 mCallback->linkToDeath(this, 0 /*cookie*/);
217 }
218 return Void();
219 }
220
getCecVersion()221 Return<int32_t> HdmiCecDefault::getCecVersion() {
222 return property_get_int32("ro.hdmi.cec_version", CEC_OP_CEC_VERSION_1_4);
223 }
224
getVendorId()225 Return<uint32_t> HdmiCecDefault::getVendorId() {
226 return property_get_int32("ro.hdmi.vendor_id", 0x000c03 /* HDMI LLC vendor ID */);
227 }
228
getPortInfo(getPortInfo_cb callback)229 Return<void> HdmiCecDefault::getPortInfo(getPortInfo_cb callback) {
230 hidl_vec<HdmiPortInfo> portInfos(mHdmiCecPorts.size());
231 for (int i = 0; i < mHdmiCecPorts.size(); i++) {
232 uint16_t addr = INVALID_PHYSICAL_ADDRESS;
233 int ret = ioctl(mHdmiCecPorts[i]->mCecFd, CEC_ADAP_G_PHYS_ADDR, &addr);
234 if (ret) {
235 LOG(ERROR) << "Get port info failed for port : " << mHdmiCecPorts[i]->mPortId
236 << ", Error = " << strerror(errno);
237 }
238 HdmiPortType type = HdmiPortType::INPUT;
239 uint32_t deviceType = GetUintProperty<uint32_t>(PROPERTY_DEVICE_TYPE, CEC_DEVICE_PLAYBACK);
240 if (deviceType != CEC_DEVICE_TV && i == MIN_PORT_ID) {
241 type = HdmiPortType::OUTPUT;
242 }
243 portInfos[i] = {.type = type,
244 .portId = mHdmiCecPorts[i]->mPortId,
245 .cecSupported = true,
246 .arcSupported = false,
247 .physicalAddress = addr};
248 }
249 callback(portInfos);
250 return Void();
251 }
252
setOption(OptionKey key,bool value)253 Return<void> HdmiCecDefault::setOption(OptionKey key, bool value) {
254 switch (key) {
255 case OptionKey::ENABLE_CEC:
256 LOG(DEBUG) << "setOption: Enable CEC: " << value;
257 mCecEnabled = value;
258 break;
259 case OptionKey::WAKEUP:
260 LOG(DEBUG) << "setOption: WAKEUP: " << value;
261 mWakeupEnabled = value;
262 break;
263 case OptionKey::SYSTEM_CEC_CONTROL:
264 LOG(DEBUG) << "setOption: SYSTEM_CEC_CONTROL: " << value;
265 mCecControlEnabled = value;
266 break;
267 }
268 return Void();
269 }
270
setLanguage(const hidl_string &)271 Return<void> HdmiCecDefault::setLanguage(const hidl_string& /*language*/) {
272 return Void();
273 }
274
enableAudioReturnChannel(int32_t,bool)275 Return<void> HdmiCecDefault::enableAudioReturnChannel(int32_t /*portId*/, bool /*enable*/) {
276 return Void();
277 }
278
isConnected(int32_t portId)279 Return<bool> HdmiCecDefault::isConnected(int32_t portId) {
280 uint16_t addr;
281 if (portId < 0 || portId >= mHdmiCecPorts.size()) {
282 LOG(ERROR) << "Port id is out of bounds, portId = " << portId;
283 return false;
284 }
285 int ret = ioctl(mHdmiCecPorts[portId]->mCecFd, CEC_ADAP_G_PHYS_ADDR, &addr);
286 if (ret) {
287 LOG(ERROR) << "Is connected failed, Error = " << strerror(errno);
288 return false;
289 }
290 if (addr == CEC_PHYS_ADDR_INVALID) {
291 return false;
292 }
293 return true;
294 }
295
getPortId(string cecFilename)296 int getPortId(string cecFilename) {
297 int portId = stoi(cecFilename.substr(3));
298 if (portId >= MIN_PORT_ID && portId <= MAX_PORT_ID) {
299 return portId;
300 } else {
301 return -1;
302 }
303 }
304
305 // Initialise the cec file descriptors
init()306 Return<Result> HdmiCecDefault::init() {
307 const char* parentPath = "/dev/";
308 DIR* dir = opendir(parentPath);
309 const char* cecFilename = "cec";
310
311 while (struct dirent* dirEntry = readdir(dir)) {
312 string filename = dirEntry->d_name;
313 if (filename.compare(0, 3, cecFilename, 0, 3) == 0) {
314 int portId = getPortId(filename);
315 if (portId == -1) {
316 continue;
317 }
318 shared_ptr<HdmiCecPort> hdmiCecPort(new HdmiCecPort(portId));
319 string filepath = parentPath + filename;
320 Result result = hdmiCecPort->init(filepath.c_str());
321 if (result != Result::SUCCESS) {
322 continue;
323 }
324 thread eventThread(&HdmiCecDefault::event_thread, this, hdmiCecPort.get());
325 mEventThreads.push_back(std::move(eventThread));
326 mHdmiCecPorts.push_back(std::move(hdmiCecPort));
327 }
328 }
329
330 if (mHdmiCecPorts.empty()) {
331 return Result::FAILURE_NOT_SUPPORTED;
332 }
333
334 mCecEnabled = true;
335 mWakeupEnabled = true;
336 mCecControlEnabled = true;
337 return Result::SUCCESS;
338 }
339
release()340 Return<void> HdmiCecDefault::release() {
341 mCecEnabled = false;
342 mWakeupEnabled = false;
343 mCecControlEnabled = false;
344 for (thread& eventThread : mEventThreads) {
345 if (eventThread.joinable()) {
346 eventThread.join();
347 }
348 }
349 setCallback(nullptr);
350 mHdmiCecPorts.clear();
351 mEventThreads.clear();
352 return Void();
353 }
354
event_thread(HdmiCecPort * hdmiCecPort)355 void HdmiCecDefault::event_thread(HdmiCecPort* hdmiCecPort) {
356 struct pollfd ufds[3] = {
357 {hdmiCecPort->mCecFd, POLLIN, 0},
358 {hdmiCecPort->mCecFd, POLLERR, 0},
359 {hdmiCecPort->mExitFd, POLLIN, 0},
360 };
361
362 while (1) {
363 ufds[0].revents = 0;
364 ufds[1].revents = 0;
365 ufds[2].revents = 0;
366
367 int ret = poll(ufds, /* size(ufds) = */ 3, /* timeout = */ -1);
368
369 if (ret <= 0) {
370 continue;
371 }
372
373 if (ufds[2].revents == POLLIN) { /* Exit */
374 break;
375 }
376
377 if (ufds[1].revents == POLLERR) { /* CEC Event */
378 cec_event ev;
379 ret = ioctl(hdmiCecPort->mCecFd, CEC_DQEVENT, &ev);
380
381 if (ret) {
382 LOG(ERROR) << "CEC_DQEVENT failed, Error = " << strerror(errno);
383 continue;
384 }
385
386 if (!mCecEnabled) {
387 continue;
388 }
389
390 if (ev.event == CEC_EVENT_STATE_CHANGE) {
391 if (mCallback != nullptr) {
392 HotplugEvent hotplugEvent{
393 .connected = (ev.state_change.phys_addr != CEC_PHYS_ADDR_INVALID),
394 .portId = hdmiCecPort->mPortId};
395 mCallback->onHotplugEvent(hotplugEvent);
396 } else {
397 LOG(ERROR) << "No event callback for hotplug";
398 }
399 }
400 }
401
402 if (ufds[0].revents == POLLIN) { /* CEC Driver */
403 cec_msg msg = {};
404 ret = ioctl(hdmiCecPort->mCecFd, CEC_RECEIVE, &msg);
405
406 if (ret) {
407 LOG(ERROR) << "CEC_RECEIVE failed, Error = " << strerror(errno);
408 continue;
409 }
410
411 if (msg.rx_status != CEC_RX_STATUS_OK) {
412 LOG(ERROR) << "msg rx_status = " << msg.rx_status;
413 continue;
414 }
415
416 if (!mCecEnabled) {
417 continue;
418 }
419
420 if (!mWakeupEnabled && isWakeupMessage(msg)) {
421 LOG(DEBUG) << "Filter wakeup message";
422 continue;
423 }
424
425 if (!mCecControlEnabled && !isTransferableInSleep(msg)) {
426 LOG(DEBUG) << "Filter message in standby mode";
427 continue;
428 }
429
430 if (mCallback != nullptr) {
431 size_t length = std::min(msg.len - 1, (uint32_t)MaxLength::MESSAGE_BODY);
432 CecMessage cecMessage{
433 .initiator = static_cast<CecLogicalAddress>(msg.msg[0] >> 4),
434 .destination = static_cast<CecLogicalAddress>(msg.msg[0] & 0xf),
435 };
436 cecMessage.body.resize(length);
437 for (size_t i = 0; i < length; ++i) {
438 cecMessage.body[i] = static_cast<uint8_t>(msg.msg[i + 1]);
439 }
440 mCallback->onCecMessage(cecMessage);
441 } else {
442 LOG(ERROR) << "no event callback for message";
443 }
444 }
445 }
446 }
447
getOpcode(cec_msg message)448 int HdmiCecDefault::getOpcode(cec_msg message) {
449 return static_cast<uint8_t>(message.msg[1]);
450 }
451
isWakeupMessage(cec_msg message)452 bool HdmiCecDefault::isWakeupMessage(cec_msg message) {
453 int opcode = getOpcode(message);
454 switch (opcode) {
455 case CEC_MESSAGE_TEXT_VIEW_ON:
456 case CEC_MESSAGE_IMAGE_VIEW_ON:
457 return true;
458 default:
459 return false;
460 }
461 }
462
isTransferableInSleep(cec_msg message)463 bool HdmiCecDefault::isTransferableInSleep(cec_msg message) {
464 int opcode = getOpcode(message);
465 switch (opcode) {
466 case CEC_MESSAGE_ABORT:
467 case CEC_MESSAGE_DEVICE_VENDOR_ID:
468 case CEC_MESSAGE_GET_CEC_VERSION:
469 case CEC_MESSAGE_GET_MENU_LANGUAGE:
470 case CEC_MESSAGE_GIVE_DEVICE_POWER_STATUS:
471 case CEC_MESSAGE_GIVE_DEVICE_VENDOR_ID:
472 case CEC_MESSAGE_GIVE_OSD_NAME:
473 case CEC_MESSAGE_GIVE_PHYSICAL_ADDRESS:
474 case CEC_MESSAGE_REPORT_PHYSICAL_ADDRESS:
475 case CEC_MESSAGE_REPORT_POWER_STATUS:
476 case CEC_MESSAGE_SET_OSD_NAME:
477 case CEC_MESSAGE_DECK_CONTROL:
478 case CEC_MESSAGE_PLAY:
479 case CEC_MESSAGE_IMAGE_VIEW_ON:
480 case CEC_MESSAGE_TEXT_VIEW_ON:
481 case CEC_MESSAGE_SYSTEM_AUDIO_MODE_REQUEST:
482 return true;
483 case CEC_MESSAGE_USER_CONTROL_PRESSED:
484 return isPowerUICommand(message);
485 default:
486 return false;
487 }
488 }
489
getFirstParam(cec_msg message)490 int HdmiCecDefault::getFirstParam(cec_msg message) {
491 return static_cast<uint8_t>(message.msg[2]);
492 }
493
isPowerUICommand(cec_msg message)494 bool HdmiCecDefault::isPowerUICommand(cec_msg message) {
495 int uiCommand = getFirstParam(message);
496 switch (uiCommand) {
497 case CEC_OP_UI_CMD_POWER:
498 case CEC_OP_UI_CMD_DEVICE_ROOT_MENU:
499 case CEC_OP_UI_CMD_POWER_ON_FUNCTION:
500 return true;
501 default:
502 return false;
503 }
504 }
505
getSendMessageResult(int tx_status)506 Return<SendMessageResult> HdmiCecDefault::getSendMessageResult(int tx_status) {
507 switch (tx_status) {
508 case CEC_TX_STATUS_OK:
509 return SendMessageResult::SUCCESS;
510 case CEC_TX_STATUS_ARB_LOST:
511 return SendMessageResult::BUSY;
512 case CEC_TX_STATUS_NACK:
513 return SendMessageResult::NACK;
514 default:
515 return SendMessageResult::FAIL;
516 }
517 }
518 } // namespace implementation
519 } // namespace V1_0
520 } // namespace cec
521 } // namespace tv
522 } // namespace hardware
523 } // namespace android
524