blob: 1e6e3df72569c78073c00f6696828a1f2f4cd032 [file] [log] [blame]
Steven Moreland5d5ef7f2016-10-20 19:19:55 -07001/*
2 * Copyright (C) 2016 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 "ServiceManagement"
18
Martijn Coenen12f04d92016-12-07 17:29:41 +010019#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070020#include <hidl/ServiceManagement.h>
21#include <hidl/Static.h>
22#include <hidl/Status.h>
23
24#include <hwbinder/IPCThreadState.h>
25#include <hwbinder/Parcel.h>
26#include <utils/Log.h>
27#include <utils/SystemClock.h>
28
29#include <unistd.h>
30
31#include <android/hidl/manager/1.0/IServiceManager.h>
Yifan Hong4e925992017-01-09 17:47:17 -080032#include <android/hidl/manager/1.0/BpHwServiceManager.h>
33#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070034
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070035using android::hidl::manager::V1_0::IServiceManager;
Yifan Hong4e925992017-01-09 17:47:17 -080036using android::hidl::manager::V1_0::BpHwServiceManager;
37using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038
39namespace android {
40namespace hardware {
41
42sp<IServiceManager> defaultServiceManager() {
43
44 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
45 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
46 // HwBinder not available on this device or not accessible to
47 // this process.
48 return nullptr;
49 }
50 {
51 AutoMutex _l(gDefaultServiceManagerLock);
52 while (gDefaultServiceManager == NULL) {
Yifan Hong4e925992017-01-09 17:47:17 -080053 gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070054 ProcessState::self()->getContextObject(NULL));
55 if (gDefaultServiceManager == NULL)
56 sleep(1);
57 }
58 }
59
60 return gDefaultServiceManager;
61}
62
63}; // namespace hardware
64}; // namespace android