Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 19 | #include <hidl/HidlBinderSupport.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 20 | #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 Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 32 | #include <android/hidl/manager/1.0/BpHwServiceManager.h> |
| 33 | #include <android/hidl/manager/1.0/BnHwServiceManager.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 34 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 35 | using android::hidl::manager::V1_0::IServiceManager; |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 36 | using android::hidl::manager::V1_0::BpHwServiceManager; |
| 37 | using android::hidl::manager::V1_0::BnHwServiceManager; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace hardware { |
| 41 | |
| 42 | sp<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 Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 53 | gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>( |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 54 | 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 |