ServiceManagement.cpp revision 00f4a391c6f9a698bbcbbd05bf3d80e213c82884
1/*
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
19#include <hidl/ServiceManagement.h>
20#include <hidl/Static.h>
21#include <hidl/Status.h>
22
23#include <hwbinder/IPCThreadState.h>
24#include <hwbinder/Parcel.h>
25#include <utils/Log.h>
26#include <utils/SystemClock.h>
27
28#include <unistd.h>
29
30#include <android/hidl/manager/1.0/IServiceManager.h>
31#include <android/hidl/manager/1.0/IHwServiceManager.h>
32
33using android::hidl::manager::V1_0::IHwServiceManager;
34using android::hidl::manager::V1_0::IServiceManager;
35
36namespace android {
37namespace hardware {
38
39sp<IServiceManager> defaultServiceManager() {
40
41    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
42    if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
43        // HwBinder not available on this device or not accessible to
44        // this process.
45        return nullptr;
46    }
47    {
48        AutoMutex _l(gDefaultServiceManagerLock);
49        while (gDefaultServiceManager == NULL) {
50            gDefaultServiceManager = interface_cast<IHwServiceManager>(
51                ProcessState::self()->getContextObject(NULL));
52            if (gDefaultServiceManager == NULL)
53                sleep(1);
54        }
55    }
56
57    return gDefaultServiceManager;
58}
59
60}; // namespace hardware
61}; // namespace android
62