blob: e9282867dd8d7f81bce109b3bba28327b1282936 [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>
24#include <android/binder_manager.h>
25#include <android/binder_process.h>
26
27#include <android-base/logging.h>
28
29class AudioHalBinderServiceUtil {
30 public:
31 ndk::SpAIBinder connectToService(const std::string& serviceName) {
32 mServiceName = serviceName;
33 mBinder = ndk::SpAIBinder(AServiceManager_getService(serviceName.c_str()));
34 if (mBinder == nullptr) {
35 LOG(ERROR) << "Failed to get service " << serviceName;
36 } else {
37 LOG(DEBUG) << "succeed to get service " << serviceName;
38 }
39 return mBinder;
40 }
41
42 ndk::SpAIBinder restartService(
43 std::chrono::milliseconds timeoutMs = std::chrono::milliseconds(3000)) {
44 mDeathHandler.reset(new AidlDeathRecipient(mBinder));
45 if (STATUS_OK != mDeathHandler->linkToDeath()) {
46 LOG(ERROR) << "linkToDeath failed";
47 return nullptr;
48 }
49 if (!android::base::SetProperty("sys.audio.restart.hal", "1")) {
50 LOG(ERROR) << "SetProperty failed";
51 return nullptr;
52 }
53 if (!mDeathHandler->waitForFired(timeoutMs)) {
54 LOG(ERROR) << "Timeout wait for death";
55 return nullptr;
56 }
57 mDeathHandler.reset();
58 return connectToService(mServiceName);
59 }
60
61 private:
62 class AidlDeathRecipient {
63 public:
64 explicit AidlDeathRecipient(const ndk::SpAIBinder& binder)
65 : binder(binder), recipient(AIBinder_DeathRecipient_new(&binderDiedCallbackAidl)) {}
66
67 binder_status_t linkToDeath() {
68 return AIBinder_linkToDeath(binder.get(), recipient.get(), this);
69 }
70
71 bool waitForFired(std::chrono::milliseconds timeoutMs) {
72 std::unique_lock<std::mutex> lock(mutex);
73 condition.wait_for(lock, timeoutMs, [this]() { return fired; });
74 return fired;
75 }
76
77 private:
78 const ndk::SpAIBinder binder;
79 const ndk::ScopedAIBinder_DeathRecipient recipient;
80 std::mutex mutex;
81 std::condition_variable condition;
82 bool fired = false;
83
84 void binderDied() {
85 std::unique_lock<std::mutex> lock(mutex);
86 fired = true;
87 condition.notify_one();
88 };
89
90 static void binderDiedCallbackAidl(void* cookie) {
91 AidlDeathRecipient* self = static_cast<AidlDeathRecipient*>(cookie);
92 self->binderDied();
93 }
94 };
95
96 std::string mServiceName;
97 ndk::SpAIBinder mBinder;
98 std::unique_ptr<AidlDeathRecipient> mDeathHandler;
99};