blob: 6b2184ebaaa8062470ddb9a0717bf04f50fbb735 [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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 Moreland5d62e442018-09-13 15:01:02 -070018
Steven Moreland4d5ad492018-09-13 12:49:16 -070019#include "ibinder_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070020#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070021
22#include <binder/IServiceManager.h>
Devin Moore6a9394f2020-07-31 17:24:51 -070023#include <binder/LazyServiceRegistrar.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070024
25using ::android::defaultServiceManager;
26using ::android::IBinder;
27using ::android::IServiceManager;
28using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070029using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070030using ::android::String16;
31
32binder_status_t AServiceManager_addService(AIBinder* binder, const char* instance) {
33 if (binder == nullptr || instance == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -070034 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -070035 }
36
37 sp<IServiceManager> sm = defaultServiceManager();
Steven Moreland5d62e442018-09-13 15:01:02 -070038 status_t status = sm->addService(String16(instance), binder->getBinder());
39 return PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -070040}
Steven Morelanddea68bb2019-02-15 10:53:08 -080041AIBinder* 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 Moreland2e87adc2018-08-20 19:47:00 -070053AIBinder* 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 Moreland94968952018-09-05 14:42:59 -070061 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
Steven Moreland71cddc32018-08-30 23:39:22 -070062 AIBinder_incStrong(ret.get());
63 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -070064}
Devin Moore6a9394f2020-07-31 17:24:51 -070065binder_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}
75AIBinder* 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}
87bool 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}