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>
18 
19 #include "ibinder_internal.h"
20 #include "status_internal.h"
21 
22 #include <binder/IServiceManager.h>
23 
24 using ::android::defaultServiceManager;
25 using ::android::IBinder;
26 using ::android::IServiceManager;
27 using ::android::sp;
28 using ::android::status_t;
29 using ::android::String16;
30 
AServiceManager_addService(AIBinder * binder,const char * instance)31 binder_status_t AServiceManager_addService(AIBinder* binder, const char* instance) {
32     if (binder == nullptr || instance == nullptr) {
33         return STATUS_UNEXPECTED_NULL;
34     }
35 
36     sp<IServiceManager> sm = defaultServiceManager();
37     status_t status = sm->addService(String16(instance), binder->getBinder());
38     return PruneStatusT(status);
39 }
AServiceManager_checkService(const char * instance)40 AIBinder* AServiceManager_checkService(const char* instance) {
41     if (instance == nullptr) {
42         return nullptr;
43     }
44 
45     sp<IServiceManager> sm = defaultServiceManager();
46     sp<IBinder> binder = sm->checkService(String16(instance));
47 
48     sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
49     AIBinder_incStrong(ret.get());
50     return ret.get();
51 }
AServiceManager_getService(const char * instance)52 AIBinder* AServiceManager_getService(const char* instance) {
53     if (instance == nullptr) {
54         return nullptr;
55     }
56 
57     sp<IServiceManager> sm = defaultServiceManager();
58     sp<IBinder> binder = sm->getService(String16(instance));
59 
60     sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(binder);
61     AIBinder_incStrong(ret.get());
62     return ret.get();
63 }
64