blob: 62dcf397ad855e0a9383d05c5ce347c08d118655 [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
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -050054PowerPolicyClient::PowerPolicyClient(power_policy_init_config_t init_config) {
55
56 fp_in_set_power_policy = init_config.fp_in_set_power_policy;
57 fp_out_set_power_policy = init_config.fp_out_set_power_policy;
58
Tahir Dawsoncaaf0992021-03-11 13:31:23 -050059 plugin_handle = dlopen(LIB_AUDIO_HAL_PLUGIN, RTLD_NOW);
60 if (plugin_handle == NULL) {
61 LOG(ERROR) << "Failed to open plugin library";
62 return;
63 }
64
65 hal_plugin_send_msg = (hal_plugin_send_msg_t) dlsym(plugin_handle,
66 "audio_hal_plugin_send_msg");
67 if (hal_plugin_send_msg == NULL) {
68 LOG(ERROR) << "dlsym failed for audio_hal_plugin_send_msg";
69 dlclose(plugin_handle);
70 plugin_handle = NULL;
71 }
72
73 LOG(ERROR) << "PowerPolicyClient Initialzed";
74}
75
76PowerPolicyClient::~PowerPolicyClient() {
77 if (plugin_handle != NULL)
78 dlclose(plugin_handle);
79}
80
81void PowerPolicyClient::onInitFailed() {
82 LOG(ERROR) << "Initializing power policy client failed";
83}
84
85std::vector<PowerComponent> PowerPolicyClient::getComponentsOfInterest() {
86 std::vector<PowerComponent> components{kAudioComponent, kMicComponent};
87 return components;
88}
89
90ScopedAStatus PowerPolicyClient::onPolicyChanged(const CarPowerPolicy& powerPolicy) {
91 uint8_t disable = 0;
92
93 if (hasComponent(powerPolicy.enabledComponents, kAudioComponent)) {
94 LOG(ERROR) << "Power policy: Audio component is enabled";
95 disable = 0;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -050096
97 fp_out_set_power_policy(!disable);
Tahir Dawsoncaaf0992021-03-11 13:31:23 -050098 if (hal_plugin_send_msg != NULL)
99 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_SILENT_MODE,
100 &disable, sizeof(disable));
101 } else if (hasComponent(powerPolicy.disabledComponents, kAudioComponent)) {
102 LOG(ERROR) << "Power policy: Audio component is disabled";
103 disable = 1;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -0500104
105 fp_out_set_power_policy(!disable);
Tahir Dawsoncaaf0992021-03-11 13:31:23 -0500106 if (hal_plugin_send_msg != NULL)
107 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_SILENT_MODE,
108 &disable, sizeof(disable));
109 }
110
111 if (hasComponent(powerPolicy.enabledComponents, kMicComponent)) {
112 LOG(ERROR) << "Power policy: Microphone component is enabled";
113 disable = 0;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -0500114
115 fp_in_set_power_policy(!disable);
Tahir Dawsoncaaf0992021-03-11 13:31:23 -0500116 if (hal_plugin_send_msg != NULL)
117 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_MIC_STATE,
118 &disable, sizeof(disable));
119 } else if (hasComponent(powerPolicy.disabledComponents, kMicComponent)) {
120 disable = 1;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -0500121
122 fp_in_set_power_policy(!disable);
Tahir Dawsoncaaf0992021-03-11 13:31:23 -0500123 if (hal_plugin_send_msg != NULL)
124 hal_plugin_send_msg(AUDIO_HAL_PLUGIN_MSG_MIC_STATE,
125 &disable, sizeof(disable));
126 LOG(ERROR) << "Power policy: Microphone component is disabled";
127 }
128 return ScopedAStatus::ok();
129}