blob: d507af6f5322b9315f08e8f1bc7f314f93d459dc [file] [log] [blame]
Badhri Jagan Sridharanb9f69ea2021-10-19 13:09:44 -07001/*
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
32namespace aidl {
33namespace android {
34namespace hardware {
35namespace usb {
36
37using ::aidl::android::hardware::usb::IUsbCallback;
38using ::aidl::android::hardware::usb::PortRole;
39using ::android::base::ReadFileToString;
40using ::android::base::WriteStringToFile;
41using ::android::sp;
42using ::ndk::ScopedAStatus;
43using ::std::shared_ptr;
44using ::std::string;
45
46struct 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 Sridharane7450582021-12-27 03:42:19 -080057 ScopedAStatus enableUsbDataWhileDocked(const string& in_portName,
58 int64_t in_transactionId) override;
Badhri Jagan Sridharan623f1332021-11-25 09:35:21 -080059 ScopedAStatus limitPowerTransfer(const std::string& in_portName, bool in_limit,
60 int64_t in_transactionId)override;
Ricky Niu45131a72021-12-07 20:27:37 +080061 ScopedAStatus resetUsbPort(const std::string& in_portName,
62 int64_t in_transactionId)override;
Badhri Jagan Sridharanb9f69ea2021-10-19 13:09:44 -070063
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