blob: e3a9e2256b8da1b0f64a728bf20e5f22e3a2ee6d [file] [log] [blame]
Tahir Dawsoncaaf0992021-03-11 13:31:23 -05001/* Copyright (c) 2021, The Linux Foundation. All rights reserved.
2
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions are
5met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above
9 copyright notice, this list of conditions and the following
10 disclaimer in the documentation and/or other materials provided
11 with the distribution.
12 * Neither the name of The Linux Foundation nor the names of its
13 contributors may be used to endorse or promote products derived
14 from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
27
28#include "PowerPolicyClient.h"
29
30#include <android-base/logging.h>
31#include <dlfcn.h>
32
33#ifdef DAEMON_SUPPORT_AUTO
34#define LIB_AUDIO_HAL_PLUGIN "libaudiohalpluginclient.so"
35#else
36#define LIB_AUDIO_HAL_PLUGIN "libaudiohalplugin.so"
37#endif
38
39namespace aafap = aidl::android::frameworks::automotive::powerpolicy;
40
41using aafap::CarPowerPolicy;
42using aafap::CarPowerPolicyFilter;
43using aafap::PowerComponent;
44using ::android::frameworks::automotive::powerpolicy::hasComponent;
45using ::ndk::ScopedAStatus;
46
47namespace {
48
49constexpr PowerComponent kAudioComponent = PowerComponent::AUDIO;
50constexpr PowerComponent kMicComponent = PowerComponent::MICROPHONE;
51
52} // namespace
53
54PowerPolicyClient::PowerPolicyClient() {
55 plugin_handle = dlopen(LIB_AUDIO_HAL_PLUGIN, RTLD_NOW);
56 if (plugin_handle == NULL) {
57 LOG(ERROR) << "Failed to open plugin library";
58 return;
59 }
60
61 hal_plugin_send_msg = (hal_plugin_send_msg_t) dlsym(plugin_handle,
62 "audio_hal_plugin_send_msg");
63 if (hal_plugin_send_msg == NULL) {
64 LOG(ERROR) << "dlsym failed for audio_hal_plugin_send_msg";
65 dlclose(plugin_handle);
66 plugin_handle = NULL;
67 }
68
69 LOG(ERROR) << "PowerPolicyClient Initialzed";
70}
71
72PowerPolicyClient::~PowerPolicyClient() {
73 if (plugin_handle != NULL)
74 dlclose(plugin_handle);
75}
76
77void PowerPolicyClient::onInitFailed() {
78 LOG(ERROR) << "Initializing power policy client failed";
79}
80
81std::vector<PowerComponent> PowerPolicyClient::getComponentsOfInterest() {
82 std::vector<PowerComponent> components{kAudioComponent, kMicComponent};
83 return components;
84}
85
86ScopedAStatus PowerPolicyClient::onPolicyChanged(const CarPowerPolicy& powerPolicy) {
87 uint8_t disable = 0;
88
89 if (hasComponent(powerPolicy.enabledComponents, kAudioComponent)) {
90 LOG(ERROR) << "Power policy: Audio component is enabled";
91 disable = 0;
92 if (hal_plugin_send_msg != NULL)
93 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_SILENT_MODE,
94 &disable, sizeof(disable));
95 } else if (hasComponent(powerPolicy.disabledComponents, kAudioComponent)) {
96 LOG(ERROR) << "Power policy: Audio component is disabled";
97 disable = 1;
98 if (hal_plugin_send_msg != NULL)
99 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_SILENT_MODE,
100 &disable, sizeof(disable));
101 }
102
103 if (hasComponent(powerPolicy.enabledComponents, kMicComponent)) {
104 LOG(ERROR) << "Power policy: Microphone component is enabled";
105 disable = 0;
106 if (hal_plugin_send_msg != NULL)
107 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_MIC_STATE,
108 &disable, sizeof(disable));
109 } else if (hasComponent(powerPolicy.disabledComponents, kMicComponent)) {
110 disable = 1;
111 if (hal_plugin_send_msg != NULL)
112 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_MIC_STATE,
113 &disable, sizeof(disable));
114 LOG(ERROR) << "Power policy: Microphone component is disabled";
115 }
116 return ScopedAStatus::ok();
117}