Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <android/binder_manager.h> |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 18 | |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 19 | #include "ibinder_internal.h" |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 20 | #include "status_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 21 | |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame^] | 22 | #include <android-base/logging.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
Devin Moore | 6a9394f | 2020-07-31 17:24:51 -0700 | [diff] [blame] | 24 | #include <binder/LazyServiceRegistrar.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 25 | |
| 26 | using ::android::defaultServiceManager; |
| 27 | using ::android::IBinder; |
| 28 | using ::android::IServiceManager; |
| 29 | using ::android::sp; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 30 | using ::android::status_t; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 31 | using ::android::String16; |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame^] | 32 | using ::android::String8; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 33 | |
Steven Moreland | d50b453 | 2020-08-31 21:45:00 +0000 | [diff] [blame] | 34 | binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 35 | if (binder == nullptr || instance == nullptr) { |
Steven Moreland | d50b453 | 2020-08-31 21:45:00 +0000 | [diff] [blame] | 36 | return EX_ILLEGAL_ARGUMENT; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | sp<IServiceManager> sm = defaultServiceManager(); |
Steven Moreland | d50b453 | 2020-08-31 21:45:00 +0000 | [diff] [blame] | 40 | status_t exception = sm->addService(String16(instance), binder->getBinder()); |
| 41 | return PruneException(exception); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 42 | } |
Steven Moreland | dea68bb | 2019-02-15 10:53:08 -0800 | [diff] [blame] | 43 | AIBinder* AServiceManager_checkService(const char* instance) { |
| 44 | if (instance == nullptr) { |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
| 48 | sp<IServiceManager> sm = defaultServiceManager(); |
| 49 | sp<IBinder> binder = sm->checkService(String16(instance)); |
| 50 | |
| 51 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 52 | AIBinder_incStrong(ret.get()); |
| 53 | return ret.get(); |
| 54 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 55 | AIBinder* AServiceManager_getService(const char* instance) { |
| 56 | if (instance == nullptr) { |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | sp<IServiceManager> sm = defaultServiceManager(); |
| 61 | sp<IBinder> binder = sm->getService(String16(instance)); |
| 62 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 63 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 64 | AIBinder_incStrong(ret.get()); |
| 65 | return ret.get(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 66 | } |
Devin Moore | 6a9394f | 2020-07-31 17:24:51 -0700 | [diff] [blame] | 67 | binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) { |
| 68 | if (binder == nullptr || instance == nullptr) { |
| 69 | return STATUS_UNEXPECTED_NULL; |
| 70 | } |
| 71 | |
| 72 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 73 | status_t status = serviceRegistrar.registerService(binder->getBinder(), instance); |
| 74 | |
| 75 | return PruneStatusT(status); |
| 76 | } |
| 77 | AIBinder* AServiceManager_waitForService(const char* instance) { |
| 78 | if (instance == nullptr) { |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | sp<IServiceManager> sm = defaultServiceManager(); |
| 83 | sp<IBinder> binder = sm->waitForService(String16(instance)); |
| 84 | |
| 85 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 86 | AIBinder_incStrong(ret.get()); |
| 87 | return ret.get(); |
| 88 | } |
| 89 | bool AServiceManager_isDeclared(const char* instance) { |
| 90 | if (instance == nullptr) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | sp<IServiceManager> sm = defaultServiceManager(); |
| 95 | return sm->isDeclared(String16(instance)); |
| 96 | } |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame^] | 97 | void AServiceManager_forEachDeclaredInstance(const char* interface, void* context, |
| 98 | void (*callback)(const char*, void*)) { |
| 99 | CHECK(interface != nullptr); |
| 100 | // context may be nullptr |
| 101 | CHECK(callback != nullptr); |
| 102 | |
| 103 | sp<IServiceManager> sm = defaultServiceManager(); |
| 104 | for (const String16& instance : sm->getDeclaredInstances(String16(interface))) { |
| 105 | callback(String8(instance).c_str(), context); |
| 106 | } |
| 107 | } |
Amos Bianchi | 9cf9276 | 2021-01-26 11:38:58 -0800 | [diff] [blame] | 108 | void AServiceManager_forceLazyServicesPersist(bool persist) { |
| 109 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 110 | serviceRegistrar.forcePersist(persist); |
| 111 | } |
| 112 | void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context) { |
| 113 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 114 | std::function<bool(bool)> fn = [=](bool hasClients) -> bool { |
| 115 | return callback(hasClients, context); |
| 116 | }; |
| 117 | serviceRegistrar.setActiveServicesCallback(fn); |
| 118 | } |
| 119 | bool AServiceManager_tryUnregister() { |
| 120 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 121 | return serviceRegistrar.tryUnregister(); |
| 122 | } |
| 123 | void AServiceManager_reRegister() { |
| 124 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 125 | serviceRegistrar.reRegister(); |
Steven Moreland | d687f33 | 2021-02-19 02:06:48 +0000 | [diff] [blame^] | 126 | } |