blob: d368fca46cf37aaf90a3b449cda5e70a4d547645 [file] [log] [blame]
Steven Morelandff189a02017-09-15 16:22:48 -07001/*
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
17#include <hidladapter/HidlBinderAdapter.h>
18
Steven Moreland3269d852018-01-03 11:13:17 -080019#include <android-base/logging.h>
20#include <android-base/properties.h>
Steven Morelandff189a02017-09-15 16:22:48 -070021#include <android/hidl/base/1.0/IBase.h>
22#include <android/hidl/manager/1.0/IServiceManager.h>
23#include <hidl/HidlTransportSupport.h>
24
Steven Moreland3269d852018-01-03 11:13:17 -080025#include <unistd.h>
Steven Morelandff189a02017-09-15 16:22:48 -070026#include <iostream>
27#include <map>
28#include <string>
29
30namespace android {
31namespace hardware {
32namespace details {
33
Steven Moreland3269d852018-01-03 11:13:17 -080034using android::base::SetProperty;
35using android::base::WaitForProperty;
36
37const static std::string kDeactivateProp = "test.hidl.adapters.deactivated";
38
Steven Moreland71d6bf22018-04-05 14:24:52 -070039int usage(const std::string& me) {
Steven Moreland33a5b042018-04-05 14:35:37 -070040 std::cerr << "usage: " << me
41 << " [-p] [-n instance-name] interface-name instance-name number-of-threads."
Steven Moreland3269d852018-01-03 11:13:17 -080042 << std::endl;
43 std::cerr << " -p: stop based on property " << kDeactivateProp << "and reset it."
44 << std::endl;
Steven Moreland33a5b042018-04-05 14:35:37 -070045 std::cerr
46 << " -n instance-name: register as a different instance name (does not de-register)"
47 << std::endl;
Steven Moreland71d6bf22018-04-05 14:24:52 -070048 return EINVAL;
Steven Moreland3269d852018-01-03 11:13:17 -080049}
50
Steven Moreland71d6bf22018-04-05 14:24:52 -070051struct Args {
52 bool propertyStop = false;
53 std::string interface; // e.x. IFoo
54 std::string instanceName; // e.x. default
55 int threadNumber;
Steven Moreland33a5b042018-04-05 14:35:37 -070056 std::string registerInstanceName; // e.x. default
Steven Moreland71d6bf22018-04-05 14:24:52 -070057};
58
59bool processArguments(int argc, char** argv, Args* args) {
Steven Moreland3269d852018-01-03 11:13:17 -080060 int c;
Steven Moreland33a5b042018-04-05 14:35:37 -070061 while ((c = getopt(argc, argv, "pn:")) != -1) {
Steven Moreland3269d852018-01-03 11:13:17 -080062 switch (c) {
63 case 'p': {
Steven Moreland71d6bf22018-04-05 14:24:52 -070064 args->propertyStop = true;
Steven Moreland3269d852018-01-03 11:13:17 -080065 break;
66 }
Steven Moreland33a5b042018-04-05 14:35:37 -070067 case 'n': {
68 args->registerInstanceName = optarg;
69 break;
70 }
Steven Moreland3269d852018-01-03 11:13:17 -080071 default: { return false; }
72 }
73 }
74
Steven Moreland71d6bf22018-04-05 14:24:52 -070075 argc -= optind;
76 argv += optind;
77
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
85 args->interface = argv[0];
86 args->instanceName = argv[1];
87 args->threadNumber = std::stoi(argv[2]);
88
89 if (args->threadNumber <= 0) {
90 std::cerr << "ERROR: invalid thread number " << args->threadNumber
91 << " must be a positive integer." << std::endl;
92 return false;
93 }
94
Steven Moreland33a5b042018-04-05 14:35:37 -070095 if (args->registerInstanceName.empty()) {
96 args->registerInstanceName = args->instanceName;
97 }
98
Steven Moreland3269d852018-01-03 11:13:17 -080099 return true;
100}
101
102// only applies for -p argument
103void waitForAdaptersDeactivated() {
104 using std::literals::chrono_literals::operator""s;
105
106 while (!WaitForProperty(kDeactivateProp, "true", 30s)) {
107 // Log this so that when using this option on testing devices, there is
108 // a clear indication if adapters are not properly stopped
109 LOG(WARNING) << "Adapter use in progress. Waiting for stop based on 'true' "
110 << kDeactivateProp;
111 }
112
113 SetProperty(kDeactivateProp, "false");
114}
115
Steven Morelandff189a02017-09-15 16:22:48 -0700116int adapterMain(const std::string& package, int argc, char** argv,
117 const AdaptersFactory& adapters) {
118 using android::hardware::configureRpcThreadpool;
119 using android::hidl::base::V1_0::IBase;
120 using android::hidl::manager::V1_0::IServiceManager;
121
Steven Moreland3269d852018-01-03 11:13:17 -0800122 const std::string& me = argc > 0 ? argv[0] : "(error)";
123
Steven Moreland71d6bf22018-04-05 14:24:52 -0700124 Args args;
125 if (!processArguments(argc, argv, &args)) {
126 return usage(me);
Steven Morelandff189a02017-09-15 16:22:48 -0700127 }
128
Steven Moreland71d6bf22018-04-05 14:24:52 -0700129 std::string interfaceName = package + "::" + args.interface;
Steven Morelandff189a02017-09-15 16:22:48 -0700130
131 auto it = adapters.find(interfaceName);
132 if (it == adapters.end()) {
133 std::cerr << "ERROR: could not resolve " << interfaceName << "." << std::endl;
134 return 1;
135 }
136
Steven Moreland33a5b042018-04-05 14:35:37 -0700137 std::cout << "Trying to adapt down " << interfaceName << "/" << args.instanceName << " to "
138 << args.registerInstanceName << std::endl;
Steven Morelandff189a02017-09-15 16:22:48 -0700139
Steven Moreland71d6bf22018-04-05 14:24:52 -0700140 configureRpcThreadpool(args.threadNumber, false /* callerWillJoin */);
Steven Morelandff189a02017-09-15 16:22:48 -0700141
142 sp<IServiceManager> manager = IServiceManager::getService();
143 if (manager == nullptr) {
144 std::cerr << "ERROR: could not retrieve service manager." << std::endl;
145 return 1;
146 }
147
Steven Moreland71d6bf22018-04-05 14:24:52 -0700148 sp<IBase> implementation = manager->get(interfaceName, args.instanceName).withDefault(nullptr);
Steven Morelandff189a02017-09-15 16:22:48 -0700149 if (implementation == nullptr) {
150 std::cerr << "ERROR: could not retrieve desired implementation" << std::endl;
151 return 1;
152 }
153
154 sp<IBase> adapter = it->second(implementation);
155 if (adapter == nullptr) {
156 std::cerr << "ERROR: could not create adapter." << std::endl;
157 return 1;
158 }
159
Steven Moreland33a5b042018-04-05 14:35:37 -0700160 bool replaced = manager->add(args.registerInstanceName, adapter).withDefault(false);
Steven Morelandff189a02017-09-15 16:22:48 -0700161 if (!replaced) {
162 std::cerr << "ERROR: could not register the service with the service manager." << std::endl;
163 return 1;
164 }
165
Steven Moreland71d6bf22018-04-05 14:24:52 -0700166 if (args.propertyStop) {
Steven Moreland3269d852018-01-03 11:13:17 -0800167 std::cout << "Set " << kDeactivateProp << " to true to deactivate." << std::endl;
168 waitForAdaptersDeactivated();
169 } else {
170 std::cout << "Press any key to disassociate adapter." << std::endl;
171 getchar();
172 }
Steven Morelandff189a02017-09-15 16:22:48 -0700173
Steven Moreland33a5b042018-04-05 14:35:37 -0700174 // automatically unregistered on process exit if it is a new instance name
175 if (args.registerInstanceName == args.instanceName) {
176 bool restored = manager->add(args.instanceName, implementation).withDefault(false);
177 if (!restored) {
178 std::cerr << "ERROR: could not re-register interface with the service manager."
179 << std::endl;
180 return 1;
181 }
Steven Morelandff189a02017-09-15 16:22:48 -0700182 }
183
184 std::cout << "Success." << std::endl;
185
186 return 0;
187}
188
189// If an interface is adapted to 1.0, it can then not be adapted to 1.1 in the same process.
190// This poses a problem in the following scenario:
191// auto interface = new V1_1::implementation::IFoo;
192// hidlObject1_0->foo(interface) // adaptation set at 1.0
193// hidlObject1_1->bar(interface) // adaptation still is 1.0
194// This could be solved by keeping a map of IBase,fqName -> IBase, but then you end up
195// with multiple names for the same interface.
196sp<IBase> adaptWithDefault(const sp<IBase>& something,
197 const std::function<sp<IBase>()>& makeDefault) {
198 static std::map<sp<IBase>, sp<IBase>> sAdapterMap;
199
Steven Morelandbc4ece32017-09-26 16:56:51 -0700200 if (something == nullptr) {
201 return something;
202 }
203
Steven Morelandff189a02017-09-15 16:22:48 -0700204 auto it = sAdapterMap.find(something);
205 if (it == sAdapterMap.end()) {
206 it = sAdapterMap.insert(it, {something, makeDefault()});
207 }
208
209 return it->second;
210}
211
212} // namespace details
213} // namespace hardware
214} // namespace android