• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <aidl/android/hardware/usb/gadget/GadgetFunction.h>
22 #include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android/binder_manager.h>
26 #include <android/binder_process.h>
27 #include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
28 
29 using aidl::android::hardware::usb::gadget::GadgetFunction;
30 using android::base::GetProperty;
31 using android::base::SetProperty;
32 using android::hardware::Return;
33 using ndk::ScopedAStatus;
34 using std::shared_ptr;
35 
36 std::atomic<int> sUsbOperationCount{};
37 
main(int,char **)38 int main(int /*argc*/, char** /*argv*/) {
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     }
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";
71         } else {
72             LOG(INFO) << "Usb AIDL HAL not found";
73         }
74     } else {
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         }
93     }
94     exit(0);
95 }
96