blob: 0616cfb0011cfa9d4f21247c7058367602833c04 [file] [log] [blame]
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -08001/*
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 Niuba43d542022-12-12 19:52:26 +080021#include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
22#include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080023#include <android-base/logging.h>
24#include <android-base/properties.h>
Ricky Niuba43d542022-12-12 19:52:26 +080025#include <android/binder_manager.h>
26#include <android/binder_process.h>
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080027#include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
28
Ricky Niuba43d542022-12-12 19:52:26 +080029using aidl::android::hardware::usb::gadget::GadgetFunction;
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080030using android::base::GetProperty;
31using android::base::SetProperty;
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080032using android::hardware::Return;
Ricky Niuba43d542022-12-12 19:52:26 +080033using ndk::ScopedAStatus;
34using std::shared_ptr;
35
36std::atomic<int> sUsbOperationCount{};
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080037
38int main(int /*argc*/, char** /*argv*/) {
Badhri Jagan Sridharanabc92992019-06-20 11:14:35 -070039 if (GetProperty("ro.bootmode", "") == "charger") exit(0);
Ricky Niuba43d542022-12-12 19:52:26 +080040 int operationId = sUsbOperationCount++;
Badhri Jagan Sridharanf123d452018-10-17 18:26:37 -070041
Ricky Niuba43d542022-12-12 19:52:26 +080042 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 Sridharan5e1a0ca2017-12-02 13:15:01 -080047
Ricky Niuba43d542022-12-12 19:52:26 +080048 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 Sridharan5e1a0ca2017-12-02 13:15:01 -080071 } else {
Ricky Niuba43d542022-12-12 19:52:26 +080072 LOG(INFO) << "Usb AIDL HAL not found";
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080073 }
Badhri Jagan Sridharan5e1a0ca2017-12-02 13:15:01 -080074 } else {
Ricky Niuba43d542022-12-12 19:52:26 +080075 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 Sridharan5e1a0ca2017-12-02 13:15:01 -080093 }
94 exit(0);
95}