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 #pragma once
18 
19 #include <android-base/file.h>
20 #include <aidl/android/hardware/usb/BnUsb.h>
21 #include <aidl/android/hardware/usb/BnUsbCallback.h>
22 #include <utils/Log.h>
23 
24 #define UEVENT_MSG_LEN     2048
25 #define UEVENT_MAX_EVENTS  64
26 // The type-c stack waits for 4.5 - 5.5 secs before declaring a port non-pd.
27 // The -partner directory would not be created until this is done.
28 // Having a margin of ~3 secs for the directory and other related bookeeping
29 // structures created and uvent fired.
30 #define PORT_TYPE_TIMEOUT 8
31 
32 namespace aidl {
33 namespace android {
34 namespace hardware {
35 namespace usb {
36 
37 using ::aidl::android::hardware::usb::IUsbCallback;
38 using ::aidl::android::hardware::usb::PortRole;
39 using ::android::base::ReadFileToString;
40 using ::android::base::WriteStringToFile;
41 using ::android::sp;
42 using ::ndk::ScopedAStatus;
43 using ::std::shared_ptr;
44 using ::std::string;
45 
46 struct Usb : public BnUsb {
47     Usb();
48 
49     ScopedAStatus enableContaminantPresenceDetection(const std::string& in_portName,
50             bool in_enable, int64_t in_transactionId) override;
51     ScopedAStatus queryPortStatus(int64_t in_transactionId) override;
52     ScopedAStatus setCallback(const shared_ptr<IUsbCallback>& in_callback) override;
53     ScopedAStatus switchRole(const string& in_portName, const PortRole& in_role,
54             int64_t in_transactionId) override;
55     ScopedAStatus enableUsbData(const string& in_portName, bool in_enable,
56             int64_t in_transactionId) override;
57     ScopedAStatus enableUsbDataWhileDocked(const string& in_portName,
58             int64_t in_transactionId) override;
59     ScopedAStatus limitPowerTransfer(const std::string& in_portName, bool in_limit,
60             int64_t in_transactionId)override;
61     ScopedAStatus resetUsbPort(const std::string& in_portName,
62             int64_t in_transactionId)override;
63 
64     shared_ptr<IUsbCallback> mCallback;
65     // Protects mCallback variable
66     pthread_mutex_t mLock;
67     // Protects roleSwitch operation
68     pthread_mutex_t mRoleSwitchLock;
69     // Threads waiting for the partner to come back wait here
70     pthread_cond_t mPartnerCV;
71     // lock protecting mPartnerCV
72     pthread_mutex_t mPartnerLock;
73     // Variable to signal partner coming back online after type switch
74     bool mPartnerUp;
75   private:
76     pthread_t mPoll;
77 };
78 
79 } // namespace usb
80 } // namespace hardware
81 } // namespace android
82 } // aidl
83