Badhri Jagan Sridharan | b9f69ea | 2021-10-19 13:09:44 -0700 | [diff] [blame] | 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; |
Badhri Jagan Sridharan | 623f133 | 2021-11-25 09:35:21 -0800 | [diff] [blame] | 57 | ScopedAStatus limitPowerTransfer(const std::string& in_portName, bool in_limit, |
| 58 | int64_t in_transactionId)override; |
Badhri Jagan Sridharan | b9f69ea | 2021-10-19 13:09:44 -0700 | [diff] [blame] | 59 | |
| 60 | shared_ptr<IUsbCallback> mCallback; |
| 61 | // Protects mCallback variable |
| 62 | pthread_mutex_t mLock; |
| 63 | // Protects roleSwitch operation |
| 64 | pthread_mutex_t mRoleSwitchLock; |
| 65 | // Threads waiting for the partner to come back wait here |
| 66 | pthread_cond_t mPartnerCV; |
| 67 | // lock protecting mPartnerCV |
| 68 | pthread_mutex_t mPartnerLock; |
| 69 | // Variable to signal partner coming back online after type switch |
| 70 | bool mPartnerUp; |
| 71 | private: |
| 72 | pthread_t mPoll; |
| 73 | }; |
| 74 | |
| 75 | } // namespace usb |
| 76 | } // namespace hardware |
| 77 | } // namespace android |
| 78 | } // aidl |