blob: bca86ae06cfea8340171d5c2f7ad6f8ce774fd09 [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;
57
58 shared_ptr<IUsbCallback> mCallback;
59 // Protects mCallback variable
60 pthread_mutex_t mLock;
61 // Protects roleSwitch operation
62 pthread_mutex_t mRoleSwitchLock;
63 // Threads waiting for the partner to come back wait here
64 pthread_cond_t mPartnerCV;
65 // lock protecting mPartnerCV
66 pthread_mutex_t mPartnerLock;
67 // Variable to signal partner coming back online after type switch
68 bool mPartnerUp;
69 private:
70 pthread_t mPoll;
71};
72
73} // namespace usb
74} // namespace hardware
75} // namespace android
76} // aidl