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 #ifndef ANDROID_HARDWARE_TV_CEC_V1_0_HDMICEC_H
18 #define ANDROID_HARDWARE_TV_CEC_V1_0_HDMICEC_H
19 
20 #include <algorithm>
21 
22 #include <android/hardware/tv/cec/1.0/IHdmiCec.h>
23 #include <hidl/Status.h>
24 #include <hardware/hardware.h>
25 #include <hardware/hdmi_cec.h>
26 
27 #include <hidl/MQDescriptor.h>
28 namespace android {
29 namespace hardware {
30 namespace tv {
31 namespace cec {
32 namespace V1_0 {
33 namespace implementation {
34 
35 using ::android::hardware::tv::cec::V1_0::CecLogicalAddress;
36 using ::android::hardware::tv::cec::V1_0::CecMessage;
37 using ::android::hardware::tv::cec::V1_0::MaxLength;
38 using ::android::hardware::tv::cec::V1_0::HdmiPortInfo;
39 using ::android::hardware::tv::cec::V1_0::IHdmiCec;
40 using ::android::hardware::tv::cec::V1_0::IHdmiCecCallback;
41 using ::android::hardware::tv::cec::V1_0::OptionKey;
42 using ::android::hardware::tv::cec::V1_0::Result;
43 using ::android::hardware::tv::cec::V1_0::SendMessageResult;
44 using ::android::hardware::Return;
45 using ::android::hardware::Void;
46 using ::android::hardware::hidl_vec;
47 using ::android::hardware::hidl_string;
48 using ::android::sp;
49 
50 struct HdmiCec : public IHdmiCec, public hidl_death_recipient {
51     HdmiCec(hdmi_cec_device_t* device);
52     // Methods from ::android::hardware::tv::cec::V1_0::IHdmiCec follow.
53     Return<Result> addLogicalAddress(CecLogicalAddress addr)  override;
54     Return<void> clearLogicalAddress()  override;
55     Return<void> getPhysicalAddress(getPhysicalAddress_cb _hidl_cb)  override;
56     Return<SendMessageResult> sendMessage(const CecMessage& message)  override;
57     Return<void> setCallback(const sp<IHdmiCecCallback>& callback)  override;
58     Return<int32_t> getCecVersion()  override;
59     Return<uint32_t> getVendorId()  override;
60     Return<void> getPortInfo(getPortInfo_cb _hidl_cb)  override;
61     Return<void> setOption(OptionKey key, bool value)  override;
62     Return<void> setLanguage(const hidl_string& language)  override;
63     Return<void> enableAudioReturnChannel(int32_t portId, bool enable)  override;
64     Return<bool> isConnected(int32_t portId)  override;
65 
eventCallbackHdmiCec66     static void eventCallback(const hdmi_event_t* event, void* /* arg */) {
67         if (mCallback != nullptr && event != nullptr) {
68             if (event->type == HDMI_EVENT_CEC_MESSAGE) {
69                 size_t length = std::min(event->cec.length,
70                         static_cast<size_t>(MaxLength::MESSAGE_BODY));
71                 CecMessage cecMessage {
72                     .initiator = static_cast<CecLogicalAddress>(event->cec.initiator),
73                     .destination = static_cast<CecLogicalAddress>(event->cec.destination),
74                 };
75                 cecMessage.body.resize(length);
76                 for (size_t i = 0; i < length; ++i) {
77                     cecMessage.body[i] = static_cast<uint8_t>(event->cec.body[i]);
78                 }
79                 mCallback->onCecMessage(cecMessage);
80             } else if (event->type == HDMI_EVENT_HOT_PLUG) {
81                 HotplugEvent hotplugEvent {
82                     .connected = event->hotplug.connected > 0,
83                     .portId = static_cast<uint32_t>(event->hotplug.port_id)
84                 };
85                 mCallback->onHotplugEvent(hotplugEvent);
86             }
87         }
88     }
89 
serviceDiedHdmiCec90     virtual void serviceDied(uint64_t /*cookie*/,
91                              const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
92         setCallback(nullptr);
93     }
94 
95    private:
96     static sp<IHdmiCecCallback> mCallback;
97     const hdmi_cec_device_t* mDevice;
98 };
99 
100 extern "C" IHdmiCec* HIDL_FETCH_IHdmiCec(const char* name);
101 
102 }  // namespace implementation
103 }  // namespace V1_0
104 }  // namespace cec
105 }  // namespace tv
106 }  // namespace hardware
107 }  // namespace android
108 
109 #endif  // ANDROID_HARDWARE_TV_CEC_V1_0_HDMICEC_H
110