From fa81977e1a072a42fcdfd1515eee0a13cdd59f43 Mon Sep 17 00:00:00 2001 From: Hayden Gomes Date: Thu, 2 Apr 2020 13:40:22 -0700 Subject: [PATCH] Adding --request command to default AudioControl HAL Adding a lshal command for requesting focus Bug: 148098383 Test: adb root && adb shell lshal debug android.hardware.automotive.audiocontrol@2.0::IAudioControl/default --request 1 0 1 Change-Id: I51f78ff495d81f03903515a9151c9ddd3350ee96 --- .../audiocontrol/2.0/default/AudioControl.cpp | 74 ++++++++++++++++++- .../audiocontrol/2.0/default/AudioControl.h | 4 + 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/automotive/audiocontrol/2.0/default/AudioControl.cpp b/automotive/audiocontrol/2.0/default/AudioControl.cpp index eac3514960..7a121f5d3f 100644 --- a/automotive/audiocontrol/2.0/default/AudioControl.cpp +++ b/automotive/audiocontrol/2.0/default/AudioControl.cpp @@ -16,12 +16,16 @@ #include "AudioControl.h" -#include -#include -#include - #include +#include +#include +#include + +#include +#include +#include + #include "CloseHandle.h" namespace android::hardware::automotive::audiocontrol::V2_0::implementation { @@ -104,6 +108,8 @@ void AudioControl::cmdDump(int fd, const hidl_vec& options) { std::string option = options[0]; if (EqualsIgnoreCase(option, "--help")) { cmdHelp(fd); + } else if (EqualsIgnoreCase(option, "--request")) { + cmdRequestFocus(fd, options); } else { dprintf(fd, "Invalid option: %s\n", option.c_str()); } @@ -121,6 +127,66 @@ void 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 (int), audio zone ID (int), and focus gain type (int)\n"); +} + +void AudioControl::cmdRequestFocus(int fd, const hidl_vec& options) { + if (!checkCallerHasWritePermissions(fd) || !checkArgumentsSize(fd, options, 3)) return; + + hidl_bitfield usage; + if (!safelyParseInt(options[1], &usage)) { + dprintf(fd, "Non-integer usage provided with request: %s\n", options[1].c_str()); + return; + } + int zoneId; + if (!safelyParseInt(options[2], &zoneId)) { + dprintf(fd, "Non-integer zoneId provided with request: %s\n", options[2].c_str()); + return; + } + hidl_bitfield focusGain; + if (!safelyParseInt(options[3], &focusGain)) { + dprintf(fd, "Non-integer focusGain provided with request: %s\n", options[3].c_str()); + return; + } + + if (mFocusListener == nullptr) { + dprintf(fd, "Unable to request focus - no focus listener registered\n"); + return; + } + + mFocusListener->requestAudioFocus(usage, zoneId, focusGain); + dprintf(fd, "Requested focus for usage %d, zoneId %d, and focusGain %d\n", usage, zoneId, + focusGain); +} + +bool AudioControl::checkCallerHasWritePermissions(int fd) { + // Double check that's only called by root - it should be be blocked at the HIDL debug() level, + // but it doesn't hurt to make sure... + if (hardware::IPCThreadState::self()->getCallingUid() != AID_ROOT) { + dprintf(fd, "Must be root\n"); + return false; + } + return true; +} + +bool AudioControl::checkArgumentsSize(int fd, const hidl_vec& options, + size_t expectedSize) { + // options includes the command, so reducing size by one + size_t size = options.size() - 1; + if (size == expectedSize) { + return true; + } + dprintf(fd, "Invalid number of arguments: required %zu, got %zu\n", expectedSize, size); + return false; +} + +bool AudioControl::safelyParseInt(std::string s, int* out) { + if (!android::base::ParseInt(s, out)) { + return false; + } + return true; } } // namespace android::hardware::automotive::audiocontrol::V2_0::implementation diff --git a/automotive/audiocontrol/2.0/default/AudioControl.h b/automotive/audiocontrol/2.0/default/AudioControl.h index 6a3f21b55b..1cd7cbc5de 100644 --- a/automotive/audiocontrol/2.0/default/AudioControl.h +++ b/automotive/audiocontrol/2.0/default/AudioControl.h @@ -43,10 +43,14 @@ class AudioControl : public IAudioControl { private: sp mFocusListener; + static bool checkArgumentsSize(int fd, const hidl_vec& options, size_t minSize); + static bool checkCallerHasWritePermissions(int fd); static bool isValidValue(float value); + static bool safelyParseInt(std::string s, int* out); void cmdDump(int fd, const hidl_vec& options); void cmdHelp(int fd) const; + void cmdRequestFocus(int fd, const hidl_vec& options); void dump(int fd); };