usbd.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #define LOG_TAG "usbd"
  17. #include <string>
  18. #include <android-base/logging.h>
  19. #include <android-base/properties.h>
  20. #include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
  21. #include <hidl/HidlTransportSupport.h>
  22. using android::base::GetProperty;
  23. using android::base::SetProperty;
  24. using android::hardware::configureRpcThreadpool;
  25. using android::hardware::usb::gadget::V1_0::GadgetFunction;
  26. using android::hardware::usb::gadget::V1_0::IUsbGadget;
  27. using android::hardware::Return;
  28. int main(int /*argc*/, char** /*argv*/) {
  29. if (GetProperty("ro.bootmode", "") == "charger") exit(0);
  30. configureRpcThreadpool(1, true /*callerWillJoin*/);
  31. android::sp<IUsbGadget> gadget = IUsbGadget::getService();
  32. Return<void> ret;
  33. if (gadget != nullptr) {
  34. LOG(INFO) << "Usb HAL found.";
  35. std::string function = GetProperty("persist.sys.usb.config", "");
  36. if (function == "adb") {
  37. LOG(INFO) << "peristent prop is adb";
  38. SetProperty("ctl.start", "adbd");
  39. ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::ADB),
  40. nullptr, 0);
  41. } else {
  42. LOG(INFO) << "Signal MTP to enable default functions";
  43. ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::MTP),
  44. nullptr, 0);
  45. }
  46. if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
  47. } else {
  48. LOG(INFO) << "Usb HAL not found";
  49. }
  50. exit(0);
  51. }