Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [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 "HwServiceManager" |
| 18 | |
| 19 | #include <hidl/IServiceManager.h> |
| 20 | #include <hidl/Static.h> |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | #include <hwbinder/IPCThreadState.h> |
| 24 | #include <hwbinder/Parcel.h> |
| 25 | #include <hwbinder/Static.h> |
| 26 | #include <utils/String8.h> |
| 27 | #include <utils/SystemClock.h> |
| 28 | |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | sp<IServiceManager> defaultServiceManager() |
| 35 | { |
| 36 | if (gDefaultServiceManager != NULL) return gDefaultServiceManager; |
| 37 | |
| 38 | { |
| 39 | AutoMutex _l(gDefaultServiceManagerLock); |
| 40 | while (gDefaultServiceManager == NULL) { |
| 41 | gDefaultServiceManager = interface_cast<IServiceManager>( |
| 42 | ProcessState::self()->getContextObject(NULL)); |
| 43 | if (gDefaultServiceManager == NULL) |
| 44 | sleep(1); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return gDefaultServiceManager; |
| 49 | } |
| 50 | |
| 51 | // ---------------------------------------------------------------------- |
| 52 | |
| 53 | class BpServiceManager : public BpInterface<IServiceManager> |
| 54 | { |
| 55 | public: |
| 56 | explicit BpServiceManager(const sp<IBinder>& impl) |
| 57 | : BpInterface<IServiceManager>(impl) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | virtual sp<IBinder> getService(const String16& name, const hidl_version& version) const |
| 62 | { |
| 63 | unsigned n; |
| 64 | for (n = 0; n < 5; n++){ |
| 65 | sp<IBinder> svc = checkService(name, version); |
| 66 | if (svc != NULL) return svc; |
| 67 | ALOGI("Waiting for service %s...\n", String8(name).string()); |
| 68 | sleep(1); |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | virtual sp<IBinder> checkService( const String16& name, const hidl_version& version) const |
| 74 | { |
| 75 | Parcel data, reply; |
| 76 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); |
| 77 | data.writeString16(name); |
| 78 | version.writeToParcel(data); |
| 79 | remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply); |
| 80 | return reply.readStrongBinder(); |
| 81 | } |
| 82 | |
| 83 | virtual status_t addService(const String16& name, |
| 84 | const sp<IBinder>& service, const hidl_version& version, |
| 85 | bool allowIsolated) |
| 86 | { |
| 87 | Parcel data, reply; |
| 88 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); |
| 89 | data.writeString16(name); |
| 90 | data.writeStrongBinder(service); |
| 91 | version.writeToParcel(data); |
| 92 | data.writeInt32(allowIsolated ? 1 : 0); |
| 93 | status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply); |
| 94 | return err == NO_ERROR ? reply.readExceptionCode() : err; |
| 95 | } |
| 96 | |
| 97 | virtual Vector<String16> listServices() |
| 98 | { |
| 99 | Vector<String16> res; |
| 100 | int n = 0; |
| 101 | |
| 102 | for (;;) { |
| 103 | Parcel data, reply; |
| 104 | data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); |
| 105 | data.writeInt32(n++); |
| 106 | status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply); |
| 107 | if (err != NO_ERROR) |
| 108 | break; |
| 109 | res.add(reply.readString16()); |
| 110 | } |
| 111 | return res; |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | IMPLEMENT_HWBINDER_META_INTERFACE(ServiceManager, "android.hardware.IServiceManager"); |
| 116 | |
| 117 | }; // namespace hardware |
| 118 | }; // namespace android |