blob: 71c9a8c841decf2f4cffded83531dee8d9ab4813 [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 Moreland3269d852018-01-03 11:13:17 -080040 std::cerr << "usage: " << me << " [-p] interface-name instance-name number-of-threads."
41 << std::endl;
42 std::cerr << " -p: stop based on property " << kDeactivateProp << "and reset it."
43 << std::endl;
Steven Moreland71d6bf22018-04-05 14:24:52 -070044 return EINVAL;
Steven Moreland3269d852018-01-03 11:13:17 -080045}
46
Steven Moreland71d6bf22018-04-05 14:24:52 -070047struct Args {
48 bool propertyStop = false;
49 std::string interface; // e.x. IFoo
50 std::string instanceName; // e.x. default
51 int threadNumber;
52};
53
54bool processArguments(int argc, char** argv, Args* args) {
Steven Moreland3269d852018-01-03 11:13:17 -080055 int c;
Steven Moreland71d6bf22018-04-05 14:24:52 -070056 while ((c = getopt(argc, argv, "p")) != -1) {
Steven Moreland3269d852018-01-03 11:13:17 -080057 switch (c) {
58 case 'p': {
Steven Moreland71d6bf22018-04-05 14:24:52 -070059 args->propertyStop = true;
Steven Moreland3269d852018-01-03 11:13:17 -080060 break;
61 }
62 default: { return false; }
63 }
64 }
65
Steven Moreland71d6bf22018-04-05 14:24:52 -070066 argc -= optind;
67 argv += optind;
68
69 if (argc != 3) {
70 std::cerr << "ERROR: requires exactly three positional arguments for "
71 "interface, instance name, and number of threads, but "
72 << argc << " provided." << std::endl;
73 return false;
74 }
75
76 args->interface = argv[0];
77 args->instanceName = argv[1];
78 args->threadNumber = std::stoi(argv[2]);
79
80 if (args->threadNumber <= 0) {
81 std::cerr << "ERROR: invalid thread number " << args->threadNumber
82 << " must be a positive integer." << std::endl;
83 return false;
84 }
85
Steven Moreland3269d852018-01-03 11:13:17 -080086 return true;
87}
88
89// only applies for -p argument
90void waitForAdaptersDeactivated() {
91 using std::literals::chrono_literals::operator""s;
92
93 while (!WaitForProperty(kDeactivateProp, "true", 30s)) {
94 // Log this so that when using this option on testing devices, there is
95 // a clear indication if adapters are not properly stopped
96 LOG(WARNING) << "Adapter use in progress. Waiting for stop based on 'true' "
97 << kDeactivateProp;
98 }
99
100 SetProperty(kDeactivateProp, "false");
101}
102
Steven Morelandff189a02017-09-15 16:22:48 -0700103int adapterMain(const std::string& package, int argc, char** argv,
104 const AdaptersFactory& adapters) {
105 using android::hardware::configureRpcThreadpool;
106 using android::hidl::base::V1_0::IBase;
107 using android::hidl::manager::V1_0::IServiceManager;
108
Steven Moreland3269d852018-01-03 11:13:17 -0800109 const std::string& me = argc > 0 ? argv[0] : "(error)";
110
Steven Moreland71d6bf22018-04-05 14:24:52 -0700111 Args args;
112 if (!processArguments(argc, argv, &args)) {
113 return usage(me);
Steven Morelandff189a02017-09-15 16:22:48 -0700114 }
115
Steven Moreland71d6bf22018-04-05 14:24:52 -0700116 std::string interfaceName = package + "::" + args.interface;
Steven Morelandff189a02017-09-15 16:22:48 -0700117
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
Steven Moreland71d6bf22018-04-05 14:24:52 -0700124 std::cout << "Trying to adapt down " << interfaceName << "/" << args.instanceName << std::endl;
Steven Morelandff189a02017-09-15 16:22:48 -0700125
Steven Moreland71d6bf22018-04-05 14:24:52 -0700126 configureRpcThreadpool(args.threadNumber, false /* callerWillJoin */);
Steven Morelandff189a02017-09-15 16:22:48 -0700127
128 sp<IServiceManager> manager = IServiceManager::getService();
129 if (manager == nullptr) {
130 std::cerr << "ERROR: could not retrieve service manager." << std::endl;
131 return 1;
132 }
133
Steven Moreland71d6bf22018-04-05 14:24:52 -0700134 sp<IBase> implementation = manager->get(interfaceName, args.instanceName).withDefault(nullptr);
Steven Morelandff189a02017-09-15 16:22:48 -0700135 if (implementation == nullptr) {
136 std::cerr << "ERROR: could not retrieve desired implementation" << std::endl;
137 return 1;
138 }
139
140 sp<IBase> adapter = it->second(implementation);
141 if (adapter == nullptr) {
142 std::cerr << "ERROR: could not create adapter." << std::endl;
143 return 1;
144 }
145
Steven Moreland71d6bf22018-04-05 14:24:52 -0700146 bool replaced = manager->add(args.instanceName, adapter).withDefault(false);
Steven Morelandff189a02017-09-15 16:22:48 -0700147 if (!replaced) {
148 std::cerr << "ERROR: could not register the service with the service manager." << std::endl;
149 return 1;
150 }
151
Steven Moreland71d6bf22018-04-05 14:24:52 -0700152 if (args.propertyStop) {
Steven Moreland3269d852018-01-03 11:13:17 -0800153 std::cout << "Set " << kDeactivateProp << " to true to deactivate." << std::endl;
154 waitForAdaptersDeactivated();
155 } else {
156 std::cout << "Press any key to disassociate adapter." << std::endl;
157 getchar();
158 }
Steven Morelandff189a02017-09-15 16:22:48 -0700159
Steven Moreland71d6bf22018-04-05 14:24:52 -0700160 bool restored = manager->add(args.instanceName, implementation).withDefault(false);
Steven Morelandff189a02017-09-15 16:22:48 -0700161 if (!restored) {
162 std::cerr << "ERROR: could not re-register interface with the service manager."
163 << std::endl;
164 return 1;
165 }
166
167 std::cout << "Success." << std::endl;
168
169 return 0;
170}
171
172// If an interface is adapted to 1.0, it can then not be adapted to 1.1 in the same process.
173// This poses a problem in the following scenario:
174// auto interface = new V1_1::implementation::IFoo;
175// hidlObject1_0->foo(interface) // adaptation set at 1.0
176// hidlObject1_1->bar(interface) // adaptation still is 1.0
177// This could be solved by keeping a map of IBase,fqName -> IBase, but then you end up
178// with multiple names for the same interface.
179sp<IBase> adaptWithDefault(const sp<IBase>& something,
180 const std::function<sp<IBase>()>& makeDefault) {
181 static std::map<sp<IBase>, sp<IBase>> sAdapterMap;
182
Steven Morelandbc4ece32017-09-26 16:56:51 -0700183 if (something == nullptr) {
184 return something;
185 }
186
Steven Morelandff189a02017-09-15 16:22:48 -0700187 auto it = sAdapterMap.find(something);
188 if (it == sAdapterMap.end()) {
189 it = sAdapterMap.insert(it, {something, makeDefault()});
190 }
191
192 return it->second;
193}
194
195} // namespace details
196} // namespace hardware
197} // namespace android