Tahir Dawson | caaf099 | 2021-03-11 13:31:23 -0500 | [diff] [blame^] | 1 | /* Copyright (c) 2021, The Linux Foundation. All rights reserved. |
| 2 | |
| 3 | Redistribution and use in source and binary forms, with or without |
| 4 | modification, are permitted provided that the following conditions are |
| 5 | met: |
| 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 | |
| 16 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | IF 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 | |
| 39 | namespace aafap = aidl::android::frameworks::automotive::powerpolicy; |
| 40 | |
| 41 | using aafap::CarPowerPolicy; |
| 42 | using aafap::CarPowerPolicyFilter; |
| 43 | using aafap::PowerComponent; |
| 44 | using ::android::frameworks::automotive::powerpolicy::hasComponent; |
| 45 | using ::ndk::ScopedAStatus; |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | constexpr PowerComponent kAudioComponent = PowerComponent::AUDIO; |
| 50 | constexpr PowerComponent kMicComponent = PowerComponent::MICROPHONE; |
| 51 | |
| 52 | } // namespace |
| 53 | |
| 54 | PowerPolicyClient::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 | |
| 72 | PowerPolicyClient::~PowerPolicyClient() { |
| 73 | if (plugin_handle != NULL) |
| 74 | dlclose(plugin_handle); |
| 75 | } |
| 76 | |
| 77 | void PowerPolicyClient::onInitFailed() { |
| 78 | LOG(ERROR) << "Initializing power policy client failed"; |
| 79 | } |
| 80 | |
| 81 | std::vector<PowerComponent> PowerPolicyClient::getComponentsOfInterest() { |
| 82 | std::vector<PowerComponent> components{kAudioComponent, kMicComponent}; |
| 83 | return components; |
| 84 | } |
| 85 | |
| 86 | ScopedAStatus 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 | } |