diff --git a/automotive/audiocontrol/aidl/default/Android.bp b/automotive/audiocontrol/aidl/default/Android.bp new file mode 100644 index 0000000000..faf7ad2485 --- /dev/null +++ b/automotive/audiocontrol/aidl/default/Android.bp @@ -0,0 +1,36 @@ +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +cc_binary { + name: "android.hardware.automotive.audiocontrol-service.example", + relative_install_path: "hw", + init_rc: ["audiocontrol-default.rc"], + vintf_fragments: ["audiocontrol-default.xml"], + vendor: true, + generated_headers: ["audio_policy_configuration_V7_0"], + generated_sources: ["audio_policy_configuration_V7_0"], + header_libs: ["libxsdc-utils"], + shared_libs: [ + "android.hardware.automotive.audiocontrol-ndk_platform", + "libbase", + "libbinder_ndk", + "liblog", + "libcutils", + "libxml2", + ], + srcs: [ + "AudioControl.cpp", + "main.cpp", + ], +} diff --git a/automotive/audiocontrol/aidl/default/AudioControl.cpp b/automotive/audiocontrol/aidl/default/AudioControl.cpp new file mode 100644 index 0000000000..b6373108eb --- /dev/null +++ b/automotive/audiocontrol/aidl/default/AudioControl.cpp @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AudioControl.h" + +#include +#include + +#include +#include +#include + +#include +#include + +#include + +namespace aidl::android::hardware::automotive::audiocontrol { + +using ::android::base::EqualsIgnoreCase; +using ::android::base::ParseInt; +using ::std::string; + +namespace xsd { +using namespace audio::policy::configuration::V7_0; +} + +namespace { +const float kLowerBound = -1.0f; +const float kUpperBound = 1.0f; +bool checkCallerHasWritePermissions(int fd) { + // Double check that's only called by root - it should be be blocked at debug() level, + // but it doesn't hurt to make sure... + if (AIBinder_getCallingUid() != AID_ROOT) { + dprintf(fd, "Must be root\n"); + return false; + } + return true; +} + +bool isValidValue(float value) { + return (value >= kLowerBound) && (value <= kUpperBound); +} + +bool safelyParseInt(string s, int* out) { + if (!ParseInt(s, out)) { + return false; + } + return true; +} +} // namespace + +ndk::ScopedAStatus AudioControl::registerFocusListener( + const shared_ptr& in_listener) { + LOG(DEBUG) << "registering focus listener"; + + if (in_listener.get()) { + std::atomic_store(&mFocusListener, in_listener); + } else { + LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op."; + } + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus AudioControl::setBalanceTowardRight(float value) { + if (isValidValue(value)) { + // Just log in this default mock implementation + LOG(INFO) << "Balance set to " << value; + } else { + LOG(ERROR) << "Balance value out of range -1 to 1 at " << value; + } + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus AudioControl::setFadeTowardFront(float value) { + if (isValidValue(value)) { + // Just log in this default mock implementation + LOG(INFO) << "Fader set to " << value; + } else { + LOG(ERROR) << "Fader value out of range -1 to 1 at " << value; + } + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus AudioControl::onAudioFocusChange(const string& in_usage, int32_t in_zoneId, + AudioFocusChange in_focusChange) { + LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for usage " + << in_usage.c_str() << " in zone " << in_zoneId; + return ndk::ScopedAStatus::ok(); +} + +binder_status_t AudioControl::dump(int fd, const char** args, uint32_t numArgs) { + if (numArgs == 0) { + return dumpsys(fd); + } + + string option = string(args[0]); + if (EqualsIgnoreCase(option, "--help")) { + return cmdHelp(fd); + } else if (EqualsIgnoreCase(option, "--request")) { + return cmdRequestFocus(fd, args, numArgs); + } else if (EqualsIgnoreCase(option, "--abandon")) { + return cmdAbandonFocus(fd, args, numArgs); + } else { + dprintf(fd, "Invalid option: %s\n", option.c_str()); + return STATUS_BAD_VALUE; + } +} + +binder_status_t AudioControl::dumpsys(int fd) { + if (mFocusListener == nullptr) { + dprintf(fd, "No focus listener registered\n"); + } else { + dprintf(fd, "Focus listener registered\n"); + } + return STATUS_OK; +} + +binder_status_t AudioControl::cmdHelp(int fd) const { + dprintf(fd, "Usage: \n\n"); + dprintf(fd, "[no args]: dumps focus listener status\n"); + dprintf(fd, "--help: shows this help\n"); + dprintf(fd, + "--request : requests audio focus for specified " + "usage (string), audio zone ID (int), and focus gain type (int)\n"); + dprintf(fd, + "--abandon : abandons audio focus for specified usage (string) and " + "audio zone ID (int)\n"); + dprintf(fd, "See audio_policy_configuration.xsd for valid AudioUsage values.\n"); + return STATUS_OK; +} + +binder_status_t AudioControl::cmdRequestFocus(int fd, const char** args, uint32_t numArgs) { + if (!checkCallerHasWritePermissions(fd)) { + return STATUS_PERMISSION_DENIED; + } + if (numArgs != 4) { + dprintf(fd, + "Invalid number of arguments: please provide --request " + "\n"); + return STATUS_BAD_VALUE; + } + + string usage = string(args[1]); + if (xsd::stringToAudioUsage(usage) == xsd::AudioUsage::UNKNOWN) { + dprintf(fd, + "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 " + "for supported values\n", + usage.c_str()); + return STATUS_BAD_VALUE; + } + + int zoneId; + if (!safelyParseInt(string(args[2]), &zoneId)) { + dprintf(fd, "Non-integer zoneId provided with request: %s\n", string(args[2]).c_str()); + return STATUS_BAD_VALUE; + } + + int focusGainValue; + if (!safelyParseInt(string(args[3]), &focusGainValue)) { + dprintf(fd, "Non-integer focusGain provided with request: %s\n", string(args[3]).c_str()); + return STATUS_BAD_VALUE; + } + AudioFocusChange focusGain = AudioFocusChange(focusGainValue); + + if (mFocusListener == nullptr) { + dprintf(fd, "Unable to request focus - no focus listener registered\n"); + return STATUS_BAD_VALUE; + } + + mFocusListener->requestAudioFocus(usage, zoneId, focusGain); + dprintf(fd, "Requested focus for usage %s, zoneId %d, and focusGain %d\n", usage.c_str(), + zoneId, focusGain); + return STATUS_OK; +} + +binder_status_t AudioControl::cmdAbandonFocus(int fd, const char** args, uint32_t numArgs) { + if (!checkCallerHasWritePermissions(fd)) { + return STATUS_PERMISSION_DENIED; + } + if (numArgs != 3) { + dprintf(fd, "Invalid number of arguments: please provide --abandon \n"); + return STATUS_BAD_VALUE; + } + + string usage = string(args[1]); + if (xsd::stringToAudioUsage(usage) == xsd::AudioUsage::UNKNOWN) { + dprintf(fd, + "Unknown usage provided: %s. Please see audio_policy_configuration.xsd V7_0 " + "for supported values\n", + usage.c_str()); + return STATUS_BAD_VALUE; + } + + int zoneId; + if (!safelyParseInt(string(args[2]), &zoneId)) { + dprintf(fd, "Non-integer zoneId provided with abandon: %s\n", string(args[2]).c_str()); + return STATUS_BAD_VALUE; + } + + if (mFocusListener == nullptr) { + dprintf(fd, "Unable to abandon focus - no focus listener registered\n"); + return STATUS_BAD_VALUE; + } + + mFocusListener->abandonAudioFocus(usage, zoneId); + dprintf(fd, "Abandoned focus for usage %s and zoneId %d\n", usage.c_str(), zoneId); + return STATUS_OK; +} + +} // namespace aidl::android::hardware::automotive::audiocontrol diff --git a/automotive/audiocontrol/aidl/default/AudioControl.h b/automotive/audiocontrol/aidl/default/AudioControl.h new file mode 100644 index 0000000000..a77da3e6d1 --- /dev/null +++ b/automotive/audiocontrol/aidl/default/AudioControl.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_AUDIOCONTROL_H +#define ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_AUDIOCONTROL_H + +#include +#include + +namespace aidl::android::hardware::automotive::audiocontrol { + +using ::std::shared_ptr; + +class AudioControl : public BnAudioControl { + public: + ndk::ScopedAStatus onAudioFocusChange(const std::string& in_usage, int32_t in_zoneId, + AudioFocusChange in_focusChange) override; + ndk::ScopedAStatus registerFocusListener( + const shared_ptr& in_listener) override; + ndk::ScopedAStatus setBalanceTowardRight(float in_value) override; + ndk::ScopedAStatus setFadeTowardFront(float in_value) override; + binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; + + private: + // This focus listener will only be used by this HAL instance to communicate with + // a single instance of CarAudioService. As such, it doesn't have explicit serialization. + // If a different AudioControl implementation were to have multiple threads leveraging this + // listener, then it should also include mutexes or make the listener atomic. + shared_ptr mFocusListener; + + binder_status_t cmdHelp(int fd) const; + binder_status_t cmdRequestFocus(int fd, const char** args, uint32_t numArgs); + binder_status_t cmdAbandonFocus(int fd, const char** args, uint32_t numArgs); + binder_status_t dumpsys(int fd); +}; + +} // namespace aidl::android::hardware::automotive::audiocontrol + +#endif // ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_AUDIOCONTROL_H diff --git a/automotive/audiocontrol/aidl/default/audiocontrol-default.rc b/automotive/audiocontrol/aidl/default/audiocontrol-default.rc new file mode 100644 index 0000000000..88d180dbd1 --- /dev/null +++ b/automotive/audiocontrol/aidl/default/audiocontrol-default.rc @@ -0,0 +1,4 @@ +service vendor.audiocontrol-default /vendor/bin/hw/android.hardware.automotive.audiocontrol-service.example + class hal + user audioserver + group system diff --git a/automotive/audiocontrol/aidl/default/audiocontrol-default.xml b/automotive/audiocontrol/aidl/default/audiocontrol-default.xml new file mode 100644 index 0000000000..f95d05fe77 --- /dev/null +++ b/automotive/audiocontrol/aidl/default/audiocontrol-default.xml @@ -0,0 +1,6 @@ + + + android.hardware.automotive.audiocontrol + IAudioControl/default + + diff --git a/automotive/audiocontrol/aidl/default/main.cpp b/automotive/audiocontrol/aidl/default/main.cpp new file mode 100644 index 0000000000..996665fe5c --- /dev/null +++ b/automotive/audiocontrol/aidl/default/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AudioControl.h" + +#include +#include +#include + +using aidl::android::hardware::automotive::audiocontrol::AudioControl; + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + std::shared_ptr audioControl = ndk::SharedRefBase::make(); + + const std::string instance = std::string() + AudioControl::descriptor + "/default"; + binder_status_t status = + AServiceManager_addService(audioControl->asBinder().get(), instance.c_str()); + CHECK(status == STATUS_OK); + + ABinderProcess_joinThreadPool(); + return EXIT_FAILURE; // should not reach +}