blob: 7e8422e39b1d1043459fac397d6772b17442dcb4 [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;
Badhri Jagan Sridharanb9f69ea2021-10-19 13:09:44 -070061
62 shared_ptr<IUsbCallback> mCallback;
63 // Protects mCallback variable
64 pthread_mutex_t mLock;
65 // Protects roleSwitch operation
66 pthread_mutex_t mRoleSwitchLock;
67 // Threads waiting for the partner to come back wait here
68 pthread_cond_t mPartnerCV;
69 // lock protecting mPartnerCV
70 pthread_mutex_t mPartnerLock;
71 // Variable to signal partner coming back online after type switch
72 bool mPartnerUp;
73 private:
74 pthread_t mPoll;
75};
76
77} // namespace usb
78} // namespace hardware
79} // namespace android
80} // aidl