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 | |
| 22 | #include <binder/IServiceManager.h> |
Devin Moore | 6a9394f | 2020-07-31 17:24:51 -0700 | [diff] [blame^] | 23 | #include <binder/LazyServiceRegistrar.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 24 | |
| 25 | using ::android::defaultServiceManager; |
| 26 | using ::android::IBinder; |
| 27 | using ::android::IServiceManager; |
| 28 | using ::android::sp; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 29 | using ::android::status_t; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 30 | using ::android::String16; |
| 31 | |
| 32 | binder_status_t AServiceManager_addService(AIBinder* binder, const char* instance) { |
| 33 | if (binder == nullptr || instance == nullptr) { |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 34 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | sp<IServiceManager> sm = defaultServiceManager(); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 38 | status_t status = sm->addService(String16(instance), binder->getBinder()); |
| 39 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 40 | } |
Steven Moreland | dea68bb | 2019-02-15 10:53:08 -0800 | [diff] [blame] | 41 | AIBinder* AServiceManager_checkService(const char* instance) { |
| 42 | if (instance == nullptr) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
| 46 | sp<IServiceManager> sm = defaultServiceManager(); |
| 47 | sp<IBinder> binder = sm->checkService(String16(instance)); |
| 48 | |
| 49 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 50 | AIBinder_incStrong(ret.get()); |
| 51 | return ret.get(); |
| 52 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 53 | AIBinder* AServiceManager_getService(const char* instance) { |
| 54 | if (instance == nullptr) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | sp<IServiceManager> sm = defaultServiceManager(); |
| 59 | sp<IBinder> binder = sm->getService(String16(instance)); |
| 60 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 61 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 62 | AIBinder_incStrong(ret.get()); |
| 63 | return ret.get(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 64 | } |
Devin Moore | 6a9394f | 2020-07-31 17:24:51 -0700 | [diff] [blame^] | 65 | binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) { |
| 66 | if (binder == nullptr || instance == nullptr) { |
| 67 | return STATUS_UNEXPECTED_NULL; |
| 68 | } |
| 69 | |
| 70 | auto serviceRegistrar = android::binder::LazyServiceRegistrar::getInstance(); |
| 71 | status_t status = serviceRegistrar.registerService(binder->getBinder(), instance); |
| 72 | |
| 73 | return PruneStatusT(status); |
| 74 | } |
| 75 | AIBinder* AServiceManager_waitForService(const char* instance) { |
| 76 | if (instance == nullptr) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | sp<IServiceManager> sm = defaultServiceManager(); |
| 81 | sp<IBinder> binder = sm->waitForService(String16(instance)); |
| 82 | |
| 83 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder); |
| 84 | AIBinder_incStrong(ret.get()); |
| 85 | return ret.get(); |
| 86 | } |
| 87 | bool AServiceManager_isDeclared(const char* instance) { |
| 88 | if (instance == nullptr) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | sp<IServiceManager> sm = defaultServiceManager(); |
| 93 | return sm->isDeclared(String16(instance)); |
| 94 | } |