Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define LOG_TAG "usbd" |
| 18 | |
| 19 | #include <string> |
| 20 | |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 21 | #include <aidl/android/hardware/usb/gadget/GadgetFunction.h> |
| 22 | #include <aidl/android/hardware/usb/gadget/IUsbGadget.h> |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/properties.h> |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 25 | #include <android/binder_manager.h> |
| 26 | #include <android/binder_process.h> |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 27 | #include <android/hardware/usb/gadget/1.0/IUsbGadget.h> |
| 28 | |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 29 | using aidl::android::hardware::usb::gadget::GadgetFunction; |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 30 | using android::base::GetProperty; |
| 31 | using android::base::SetProperty; |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 32 | using android::hardware::Return; |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 33 | using ndk::ScopedAStatus; |
| 34 | using std::shared_ptr; |
| 35 | |
| 36 | std::atomic<int> sUsbOperationCount{}; |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 37 | |
| 38 | int main(int /*argc*/, char** /*argv*/) { |
Badhri Jagan Sridharan | abc9299 | 2019-06-20 11:14:35 -0700 | [diff] [blame] | 39 | if (GetProperty("ro.bootmode", "") == "charger") exit(0); |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 40 | int operationId = sUsbOperationCount++; |
Badhri Jagan Sridharan | f123d45 | 2018-10-17 18:26:37 -0700 | [diff] [blame] | 41 | |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 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"); |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 47 | |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 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 | } |
| 53 | |
| 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"; |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 71 | } else { |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 72 | LOG(INFO) << "Usb AIDL HAL not found"; |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 73 | } |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 74 | } else { |
Ricky Niu | ba43d54 | 2022-12-12 19:52:26 +0800 | [diff] [blame] | 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 | } |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 93 | } |
| 94 | exit(0); |
| 95 | } |