Mikhail Naganov | 4fc43b4 | 2020-01-29 15:36:57 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #define LOG_TAG "LegacySupport" |
| 18 | |
| 19 | #include <android/hidl/base/1.0/IBase.h> |
| 20 | #include <hidl/HidlSupport.h> |
| 21 | #include <hidl/LegacySupport.h> |
| 22 | #include <hidl/ServiceManagement.h> |
| 23 | #include <hidl/Status.h> |
| 24 | |
| 25 | using android::hidl::base::V1_0::IBase; |
| 26 | |
| 27 | namespace android::hardware { |
| 28 | |
| 29 | namespace details { |
| 30 | |
| 31 | __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation( |
| 32 | const std::string& interfaceName, RegisterServiceBaseCb registerServiceCb, |
| 33 | const std::string& serviceName) { |
| 34 | sp<IBase> service = |
| 35 | getRawServiceInternal(interfaceName, serviceName, true /*retry*/, true /*getStub*/); |
| 36 | |
| 37 | if (service == nullptr) { |
| 38 | ALOGE("Could not get passthrough implementation for %s/%s.", interfaceName.c_str(), |
| 39 | serviceName.c_str()); |
| 40 | return EXIT_FAILURE; |
| 41 | } |
| 42 | |
| 43 | LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!", interfaceName.c_str(), |
| 44 | serviceName.c_str()); |
| 45 | |
| 46 | std::string actualName; |
| 47 | Return<void> result = service->interfaceDescriptor( |
| 48 | [&actualName](const hidl_string& descriptor) { actualName = descriptor; }); |
| 49 | LOG_FATAL_IF(!result.isOk(), "Error retrieving interface name from %s/%s: %s", |
| 50 | interfaceName.c_str(), serviceName.c_str(), result.description()); |
| 51 | LOG_FATAL_IF(actualName != interfaceName, "Implementation of %s/%s is actually %s!", |
| 52 | interfaceName.c_str(), serviceName.c_str(), actualName.c_str()); |
| 53 | |
| 54 | status_t status = registerServiceCb(service, serviceName); |
| 55 | |
| 56 | if (status == OK) { |
| 57 | ALOGI("Registration complete for %s/%s.", interfaceName.c_str(), serviceName.c_str()); |
| 58 | } else { |
| 59 | ALOGE("Could not register service %s/%s (%d).", interfaceName.c_str(), serviceName.c_str(), |
| 60 | status); |
| 61 | } |
| 62 | |
| 63 | return status; |
| 64 | } |
| 65 | |
| 66 | } // namespace details |
| 67 | |
| 68 | __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation( |
| 69 | const std::string& interfaceName, const std::string& serviceName) { |
| 70 | return details::registerPassthroughServiceImplementation( |
| 71 | interfaceName, |
| 72 | [](const sp<IBase>& service, const std::string& name) { |
| 73 | return details::registerAsServiceInternal(service, name); |
| 74 | }, |
| 75 | serviceName); |
| 76 | } |
| 77 | |
| 78 | } // namespace android::hardware |