Skip to content

Commit ba43d54

Browse files
author
Ricky Niu
committed
usb: migration to USB gadget AIDL
Using the USB gadget AIDL Bug: 261027750 Test: USB function switch success and AIDL service is running. Change-Id: I4781ac9eb641a5340ecfb6bda0881761b7a01eb2
1 parent 515c924 commit ba43d54

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

usbd/Android.bp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ cc_binary {
88
srcs: ["usbd.cpp"],
99
shared_libs: [
1010
"libbase",
11+
"libbinder_ndk",
1112
"libhidlbase",
1213
"liblog",
1314
"libutils",
1415
"libhardware",
1516
17+
"android.hardware.usb.gadget-V1-ndk",
1618
],
1719
}

usbd/usbd.cpp

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,78 @@
1818

1919
#include <string>
2020

21+
#include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
22+
#include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
2123
#include <android-base/logging.h>
2224
#include <android-base/properties.h>
25+
#include <android/binder_manager.h>
26+
#include <android/binder_process.h>
2327
#include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
2428

25-
#include <hidl/HidlTransportSupport.h>
26-
29+
using aidl::android::hardware::usb::gadget::GadgetFunction;
2730
using android::base::GetProperty;
2831
using android::base::SetProperty;
29-
using android::hardware::configureRpcThreadpool;
30-
using android::hardware::usb::gadget::V1_0::GadgetFunction;
31-
using android::hardware::usb::gadget::V1_0::IUsbGadget;
3232
using android::hardware::Return;
33+
using ndk::ScopedAStatus;
34+
using std::shared_ptr;
35+
36+
std::atomic<int> sUsbOperationCount{};
3337

3438
int main(int /*argc*/, char** /*argv*/) {
3539
if (GetProperty("ro.bootmode", "") == "charger") exit(0);
40+
int operationId = sUsbOperationCount++;
41+
42+
ABinderProcess_setThreadPoolMaxThreadCount(1);
43+
ABinderProcess_startThreadPool();
44+
const std::string service_name =
45+
std::string(aidl::android::hardware::usb::gadget::IUsbGadget::descriptor)
46+
.append("/default");
47+
48+
std::string function = GetProperty("persist.sys.usb.config", "");
49+
if (function == "adb") {
50+
LOG(INFO) << "persistent prop is adb";
51+
SetProperty("ctl.start", "adbd");
52+
}
3653

37-
configureRpcThreadpool(1, true /*callerWillJoin*/);
38-
android::sp<IUsbGadget> gadget = IUsbGadget::getService();
39-
Return<void> ret;
40-
41-
if (gadget != nullptr) {
42-
LOG(INFO) << "Usb HAL found.";
43-
std::string function = GetProperty("persist.sys.usb.config", "");
44-
if (function == "adb") {
45-
LOG(INFO) << "peristent prop is adb";
46-
SetProperty("ctl.start", "adbd");
47-
ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::ADB),
48-
nullptr, 0);
54+
if (AServiceManager_isDeclared(service_name.c_str())) {
55+
shared_ptr<aidl::android::hardware::usb::gadget::IUsbGadget> gadget_aidl =
56+
aidl::android::hardware::usb::gadget::IUsbGadget::fromBinder(
57+
ndk::SpAIBinder(AServiceManager_waitForService(service_name.c_str())));
58+
ScopedAStatus ret;
59+
if (gadget_aidl != nullptr) {
60+
LOG(INFO) << "Usb AIDL HAL found.";
61+
if (function == "adb") {
62+
ret = gadget_aidl->setCurrentUsbFunctions(
63+
static_cast<uint64_t>(GadgetFunction::ADB), nullptr, 0, operationId);
64+
} else {
65+
LOG(INFO) << "Signal MTP to enable default functions";
66+
ret = gadget_aidl->setCurrentUsbFunctions(
67+
static_cast<uint64_t>(GadgetFunction::MTP), nullptr, 0, operationId);
68+
}
69+
70+
if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
4971
} else {
50-
LOG(INFO) << "Signal MTP to enable default functions";
51-
ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::MTP),
52-
nullptr, 0);
72+
LOG(INFO) << "Usb AIDL HAL not found";
5373
}
54-
55-
if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
5674
} else {
57-
LOG(INFO) << "Usb HAL not found";
75+
android::sp<android::hardware::usb::gadget::V1_0::IUsbGadget> gadget =
76+
android::hardware::usb::gadget::V1_0::IUsbGadget::getService();
77+
Return<void> ret;
78+
if (gadget != nullptr) {
79+
LOG(INFO) << "Usb HAL found.";
80+
if (function == "adb") {
81+
ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::ADB),
82+
nullptr, 0);
83+
} else {
84+
LOG(INFO) << "Signal MTP to enable default functions";
85+
ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::MTP),
86+
nullptr, 0);
87+
}
88+
89+
if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
90+
} else {
91+
LOG(INFO) << "Usb HAL not found";
92+
}
5893
}
5994
exit(0);
6095
}

0 commit comments

Comments
 (0)