HidlBinderAdapter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2017 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. #include <hidladapter/HidlBinderAdapter.h>
  17. #include <android-base/logging.h>
  18. #include <android-base/properties.h>
  19. #include <android/hidl/base/1.0/IBase.h>
  20. #include <android/hidl/manager/1.0/IServiceManager.h>
  21. #include <hidl/HidlTransportSupport.h>
  22. #include <unistd.h>
  23. #include <iostream>
  24. #include <map>
  25. #include <string>
  26. namespace android {
  27. namespace hardware {
  28. namespace details {
  29. using android::base::SetProperty;
  30. using android::base::WaitForProperty;
  31. const static std::string kDeactivateProp = "test.hidl.adapters.deactivated";
  32. int usage(const std::string& me) {
  33. std::cerr << "usage: " << me
  34. << " [-p|P] [-n instance-name] interface-name instance-name number-of-threads."
  35. << std::endl;
  36. std::cerr << " -p: stop based on property " << kDeactivateProp << "and reset it."
  37. << std::endl;
  38. std::cerr << " -P: stop based on interface specific property " << kDeactivateProp
  39. << ".<fq-name>.<instance-name>" << std::endl;
  40. std::cerr
  41. << " -n instance-name: register as a different instance name (does not de-register)"
  42. << std::endl;
  43. return EINVAL;
  44. }
  45. enum class StopMethod {
  46. NONE,
  47. ALL,
  48. SPECIFIC,
  49. };
  50. struct Args {
  51. StopMethod stopMethod = StopMethod::NONE;
  52. std::string interface; // e.x. IFoo
  53. std::string instanceName; // e.x. default
  54. int threadNumber;
  55. std::string registerInstanceName; // e.x. default
  56. };
  57. bool processArguments(int argc, char** argv, Args* args) {
  58. int c;
  59. while ((c = getopt(argc, argv, "pPn:")) != -1) {
  60. switch (c) {
  61. case 'p': {
  62. args->stopMethod = StopMethod::ALL;
  63. break;
  64. }
  65. case 'P': {
  66. args->stopMethod = StopMethod::SPECIFIC;
  67. break;
  68. }
  69. case 'n': {
  70. args->registerInstanceName = optarg;
  71. break;
  72. }
  73. default: { return false; }
  74. }
  75. }
  76. argc -= optind;
  77. argv += optind;
  78. if (argc != 3) {
  79. std::cerr << "ERROR: requires exactly three positional arguments for "
  80. "interface, instance name, and number of threads, but "
  81. << argc << " provided." << std::endl;
  82. return false;
  83. }
  84. args->interface = argv[0];
  85. args->instanceName = argv[1];
  86. args->threadNumber = std::stoi(argv[2]);
  87. if (args->threadNumber <= 0) {
  88. std::cerr << "ERROR: invalid thread number " << args->threadNumber
  89. << " must be a positive integer." << std::endl;
  90. return false;
  91. }
  92. if (args->registerInstanceName.empty()) {
  93. args->registerInstanceName = args->instanceName;
  94. }
  95. return true;
  96. }
  97. // only applies for -p argument
  98. void waitForAdaptersDeactivated(const std::string& property) {
  99. using std::literals::chrono_literals::operator""s;
  100. while (!WaitForProperty(property, "true", 30s)) {
  101. // Log this so that when using this option on testing devices, there is
  102. // a clear indication if adapters are not properly stopped
  103. LOG(WARNING) << "Adapter use in progress. Waiting for stop based on 'true' " << property;
  104. }
  105. SetProperty(property, "false");
  106. }
  107. int adapterMain(const std::string& package, int argc, char** argv,
  108. const AdaptersFactory& adapters) {
  109. using android::hardware::configureRpcThreadpool;
  110. using android::hidl::base::V1_0::IBase;
  111. using android::hidl::manager::V1_0::IServiceManager;
  112. const std::string& me = argc > 0 ? argv[0] : "(error)";
  113. Args args;
  114. if (!processArguments(argc, argv, &args)) {
  115. return usage(me);
  116. }
  117. std::string interfaceName = package + "::" + args.interface;
  118. auto it = adapters.find(interfaceName);
  119. if (it == adapters.end()) {
  120. std::cerr << "ERROR: could not resolve " << interfaceName << "." << std::endl;
  121. return 1;
  122. }
  123. std::cout << "Trying to adapt down " << interfaceName << "/" << args.instanceName << " to "
  124. << args.registerInstanceName << std::endl;
  125. configureRpcThreadpool(args.threadNumber, false /* callerWillJoin */);
  126. sp<IServiceManager> manager = IServiceManager::getService();
  127. if (manager == nullptr) {
  128. std::cerr << "ERROR: could not retrieve service manager." << std::endl;
  129. return 1;
  130. }
  131. sp<IBase> implementation = manager->get(interfaceName, args.instanceName).withDefault(nullptr);
  132. if (implementation == nullptr) {
  133. std::cerr << "ERROR: could not retrieve desired implementation" << std::endl;
  134. return 1;
  135. }
  136. sp<IBase> adapter = it->second(implementation);
  137. if (adapter == nullptr) {
  138. std::cerr << "ERROR: could not create adapter." << std::endl;
  139. return 1;
  140. }
  141. bool replaced = manager->add(args.registerInstanceName, adapter).withDefault(false);
  142. if (!replaced) {
  143. std::cerr << "ERROR: could not register the service with the service manager." << std::endl;
  144. return 1;
  145. }
  146. switch (args.stopMethod) {
  147. case StopMethod::NONE: {
  148. std::cout << "Press any key to disassociate adapter." << std::endl;
  149. getchar();
  150. break;
  151. };
  152. case StopMethod::SPECIFIC: {
  153. const std::string property =
  154. kDeactivateProp + "." + interfaceName + "." + args.registerInstanceName;
  155. std::cout << "Set " << property << " to true to deactivate." << std::endl;
  156. waitForAdaptersDeactivated(property);
  157. break;
  158. };
  159. case StopMethod::ALL: {
  160. std::cout << "Set " << kDeactivateProp << " to true to deactivate." << std::endl;
  161. waitForAdaptersDeactivated(kDeactivateProp);
  162. break;
  163. };
  164. }
  165. // automatically unregistered on process exit if it is a new instance name
  166. if (args.registerInstanceName == args.instanceName) {
  167. bool restored = manager->add(args.instanceName, implementation).withDefault(false);
  168. if (!restored) {
  169. std::cerr << "ERROR: could not re-register interface with the service manager."
  170. << std::endl;
  171. return 1;
  172. }
  173. }
  174. std::cout << "Success." << std::endl;
  175. return 0;
  176. }
  177. // If an interface is adapted to 1.0, it can then not be adapted to 1.1 in the same process.
  178. // This poses a problem in the following scenario:
  179. // auto interface = new V1_1::implementation::IFoo;
  180. // hidlObject1_0->foo(interface) // adaptation set at 1.0
  181. // hidlObject1_1->bar(interface) // adaptation still is 1.0
  182. // This could be solved by keeping a map of IBase,fqName -> IBase, but then you end up
  183. // with multiple names for the same interface.
  184. sp<IBase> adaptWithDefault(const sp<IBase>& something,
  185. const std::function<sp<IBase>()>& makeDefault) {
  186. static std::map<sp<IBase>, sp<IBase>> sAdapterMap;
  187. if (something == nullptr) {
  188. return something;
  189. }
  190. auto it = sAdapterMap.find(something);
  191. if (it == sAdapterMap.end()) {
  192. it = sAdapterMap.insert(it, {something, makeDefault()});
  193. }
  194. return it->second;
  195. }
  196. } // namespace details
  197. } // namespace hardware
  198. } // namespace android