blob: 4ebc1b18b532ecd9bd94f87fece65f3bf971f382 [file] [log] [blame]
Shunkai Yao67b1be62022-07-13 05:01:42 +00001/*
2 * Copyright (C) 2022 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 <condition_variable>
20#include <memory>
21#include <mutex>
22
23#include <android-base/properties.h>
Mikhail Naganov3b125b72022-10-05 02:12:39 +000024#include <android/binder_auto_utils.h>
Shunkai Yao67b1be62022-07-13 05:01:42 +000025#include <android/binder_manager.h>
26#include <android/binder_process.h>
27
28#include <android-base/logging.h>
29
30class AudioHalBinderServiceUtil {
31 public:
32 ndk::SpAIBinder connectToService(const std::string& serviceName) {
33 mServiceName = serviceName;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000034 mBinder = ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()));
Shunkai Yao67b1be62022-07-13 05:01:42 +000035 if (mBinder == nullptr) {
36 LOG(ERROR) << "Failed to get service " << serviceName;
37 } else {
Mikhail Naganov3b125b72022-10-05 02:12:39 +000038 LOG(DEBUG) << "Succeeded to get service " << serviceName;
Shunkai Yao67b1be62022-07-13 05:01:42 +000039 }
40 return mBinder;
41 }
42
43 ndk::SpAIBinder restartService(
44 std::chrono::milliseconds timeoutMs = std::chrono::milliseconds(3000)) {
Bart Van Assche17b32012023-11-06 12:44:27 -080045 if (!stopService(timeoutMs)) {
46 return {};
Shunkai Yao67b1be62022-07-13 05:01:42 +000047 }
Shunkai Yao67b1be62022-07-13 05:01:42 +000048 return connectToService(mServiceName);
49 }
50
51 private:
52 class AidlDeathRecipient {
53 public:
54 explicit AidlDeathRecipient(const ndk::SpAIBinder& binder)
55 : binder(binder), recipient(AIBinder_DeathRecipient_new(&binderDiedCallbackAidl)) {}
56
57 binder_status_t linkToDeath() {
58 return AIBinder_linkToDeath(binder.get(), recipient.get(), this);
59 }
60
61 bool waitForFired(std::chrono::milliseconds timeoutMs) {
62 std::unique_lock<std::mutex> lock(mutex);
Bart Van Asschea76d4d82023-11-06 12:51:38 -080063 return condition.wait_for(lock, timeoutMs, [this]() { return fired; });
Shunkai Yao67b1be62022-07-13 05:01:42 +000064 }
65
66 private:
67 const ndk::SpAIBinder binder;
68 const ndk::ScopedAIBinder_DeathRecipient recipient;
69 std::mutex mutex;
70 std::condition_variable condition;
71 bool fired = false;
72
73 void binderDied() {
74 std::unique_lock<std::mutex> lock(mutex);
75 fired = true;
76 condition.notify_one();
77 };
78
79 static void binderDiedCallbackAidl(void* cookie) {
80 AidlDeathRecipient* self = static_cast<AidlDeathRecipient*>(cookie);
81 self->binderDied();
82 }
83 };
84
Bart Van Assche17b32012023-11-06 12:44:27 -080085 bool stopService(std::chrono::milliseconds timeoutMs) {
86 AidlDeathRecipient deathHandler(mBinder);
87 if (STATUS_OK != deathHandler.linkToDeath()) {
88 LOG(ERROR) << "linkToDeath failed";
89 return false;
90 }
91 if (!android::base::SetProperty("sys.audio.restart.hal", "1")) {
92 LOG(ERROR) << "SetProperty failed";
93 return false;
94 }
95 if (!deathHandler.waitForFired(timeoutMs)) {
Bart Van Assche0fde7832023-11-06 15:48:00 -080096 LOG(ERROR) << "Timeout wait for death of " << mServiceName;
Bart Van Assche17b32012023-11-06 12:44:27 -080097 return false;
98 }
99 return true;
100 }
101
Shunkai Yao67b1be62022-07-13 05:01:42 +0000102 std::string mServiceName;
103 ndk::SpAIBinder mBinder;
Shunkai Yao67b1be62022-07-13 05:01:42 +0000104};