Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | #pragma once |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | |
| 22 | #include <android/binder_auto_utils.h> |
Mikhail Naganov | 065985f | 2024-03-25 10:50:00 -0700 | [diff] [blame] | 23 | #include <android/binder_ibinder.h> |
Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 24 | #include <android/binder_manager.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame] | 28 | /* |
| 29 | * Helper macro to add instance name, function name in logs |
| 30 | * classes should provide getInstanceName and getClassName API to use these macros. |
| 31 | * print function names along with instance name. |
| 32 | * |
| 33 | * Usage: |
| 34 | * AUGMENT_LOG(I, "hello!"); |
| 35 | * AUGMENT_LOG(W, "value: %d", value); |
| 36 | * |
| 37 | * AUGMENT_LOG_IF(D, value < 0, "negative"); |
| 38 | * AUGMENT_LOG_IF(E, value < 0, "bad value: %d", value); |
| 39 | */ |
| 40 | |
| 41 | #define AUGMENT_LOG(level, ...) \ |
| 42 | ALOG##level("[%s] %s: " __android_second(0, __VA_ARGS__, ""), getInstanceName().c_str(), \ |
| 43 | __func__ __android_rest(__VA_ARGS__)) |
| 44 | |
| 45 | #define AUGMENT_LOG_IF(level, cond, ...) \ |
| 46 | ALOG##level##_IF(cond, "[%s] %s: " __android_second(0, __VA_ARGS__, ""), \ |
| 47 | getInstanceName().c_str(), __func__ __android_rest(__VA_ARGS__)) |
| 48 | // Used to register an entry into the function |
| 49 | #define LOG_ENTRY() ALOGD("[%s] %s", getInstanceName().c_str(), __func__) |
| 50 | |
| 51 | // entry logging as verbose level |
| 52 | #define LOG_ENTRY_V() ALOGV("[%s] %s", getInstanceName().c_str(), __func__) |
| 53 | |
Mikhail Naganov | 065985f | 2024-03-25 10:50:00 -0700 | [diff] [blame] | 54 | class HalDeathHandler { |
| 55 | public: |
| 56 | static HalDeathHandler& getInstance(); |
| 57 | |
| 58 | bool registerHandler(AIBinder* binder); |
| 59 | private: |
| 60 | static void OnBinderDied(void*); |
| 61 | |
| 62 | HalDeathHandler(); |
| 63 | |
| 64 | ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; |
| 65 | }; |
| 66 | |
Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 67 | template<class Intf> |
| 68 | std::shared_ptr<Intf> getServiceInstance(const std::string& instanceName) { |
| 69 | const std::string serviceName = |
| 70 | std::string(Intf::descriptor).append("/").append(instanceName); |
| 71 | std::shared_ptr<Intf> service; |
| 72 | while (!service) { |
| 73 | AIBinder* serviceBinder = nullptr; |
| 74 | while (!serviceBinder) { |
| 75 | // 'waitForService' may return a nullptr, hopefully a transient error. |
| 76 | serviceBinder = AServiceManager_waitForService(serviceName.c_str()); |
| 77 | } |
| 78 | // `fromBinder` may fail and return a nullptr if the service has died in the meantime. |
| 79 | service = Intf::fromBinder(ndk::SpAIBinder(serviceBinder)); |
Mikhail Naganov | 065985f | 2024-03-25 10:50:00 -0700 | [diff] [blame] | 80 | if (service != nullptr) { |
| 81 | HalDeathHandler::getInstance().registerHandler(serviceBinder); |
| 82 | } |
Mikhail Naganov | d8f2441 | 2024-03-20 18:15:42 -0700 | [diff] [blame] | 83 | } |
| 84 | return service; |
| 85 | } |
| 86 | |
| 87 | } // namespace android |