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 | |
| 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/properties.h> |
| 23 | #include <android/hardware/usb/gadget/1.0/IUsbGadget.h> |
| 24 | |
Badhri Jagan Sridharan | f123d45 | 2018-10-17 18:26:37 -0700 | [diff] [blame] | 25 | #include <hidl/HidlTransportSupport.h> |
| 26 | |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 27 | using android::base::GetProperty; |
| 28 | using android::base::SetProperty; |
Badhri Jagan Sridharan | f123d45 | 2018-10-17 18:26:37 -0700 | [diff] [blame] | 29 | using android::hardware::configureRpcThreadpool; |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 30 | using android::hardware::usb::gadget::V1_0::GadgetFunction; |
| 31 | using android::hardware::usb::gadget::V1_0::IUsbGadget; |
| 32 | using android::hardware::Return; |
| 33 | |
| 34 | int main(int /*argc*/, char** /*argv*/) { |
Badhri Jagan Sridharan | abc9299 | 2019-06-20 11:14:35 -0700 | [diff] [blame] | 35 | if (GetProperty("ro.bootmode", "") == "charger") exit(0); |
Badhri Jagan Sridharan | f123d45 | 2018-10-17 18:26:37 -0700 | [diff] [blame] | 36 | |
Badhri Jagan Sridharan | abc9299 | 2019-06-20 11:14:35 -0700 | [diff] [blame] | 37 | configureRpcThreadpool(1, true /*callerWillJoin*/); |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 38 | android::sp<IUsbGadget> gadget = IUsbGadget::getService(); |
| 39 | Return<void> ret; |
| 40 | |
| 41 | if (gadget != nullptr) { |
| 42 | LOG(INFO) << "Usb HAL found."; |
Badhri Jagan Sridharan | abc9299 | 2019-06-20 11:14:35 -0700 | [diff] [blame] | 43 | std::string function = GetProperty("persist.sys.usb.config", ""); |
Badhri Jagan Sridharan | 5e1a0ca | 2017-12-02 13:15:01 -0800 | [diff] [blame] | 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); |
| 49 | } else { |
| 50 | LOG(INFO) << "Signal MTP to enable default functions"; |
| 51 | ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::MTP), |
| 52 | nullptr, 0); |
| 53 | } |
| 54 | |
| 55 | if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal"; |
| 56 | } else { |
| 57 | LOG(INFO) << "Usb HAL not found"; |
| 58 | } |
| 59 | exit(0); |
| 60 | } |