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