|
18 | 18 |
|
19 | 19 | #include <string>
|
20 | 20 |
|
| 21 | +#include <aidl/android/hardware/usb/gadget/GadgetFunction.h> |
| 22 | +#include <aidl/android/hardware/usb/gadget/IUsbGadget.h> |
21 | 23 | #include <android-base/logging.h>
|
22 | 24 | #include <android-base/properties.h>
|
| 25 | +#include <android/binder_manager.h> |
| 26 | +#include <android/binder_process.h> |
23 | 27 | #include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
|
24 | 28 |
|
25 |
| -#include <hidl/HidlTransportSupport.h> |
26 |
| - |
| 29 | +using aidl::android::hardware::usb::gadget::GadgetFunction; |
27 | 30 | using android::base::GetProperty;
|
28 | 31 | 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; |
32 | 32 | using android::hardware::Return;
|
| 33 | +using ndk::ScopedAStatus; |
| 34 | +using std::shared_ptr; |
| 35 | + |
| 36 | +std::atomic<int> sUsbOperationCount{}; |
33 | 37 |
|
34 | 38 | int main(int /*argc*/, char** /*argv*/) {
|
35 | 39 | 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 | + } |
36 | 53 |
|
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"; |
49 | 71 | } 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"; |
53 | 73 | }
|
54 |
| - |
55 |
| - if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal"; |
56 | 74 | } 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 | + } |
58 | 93 | }
|
59 | 94 | exit(0);
|
60 | 95 | }
|
0 commit comments