Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa81977e authored by Hayden Gomes's avatar Hayden Gomes
Browse files

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
parent 052ccf2b
Loading
Loading
Loading
Loading
+68 −2
Original line number Diff line number Diff line
@@ -16,11 +16,15 @@

#include "AudioControl.h"

#include <stdio.h>

#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <hidl/HidlTransportSupport.h>

#include <stdio.h>
#include <hidl/HidlTransportSupport.h>
#include <hwbinder/IPCThreadState.h>
#include <private/android_filesystem_config.h>

#include "CloseHandle.h"

@@ -104,6 +108,8 @@ void AudioControl::cmdDump(int fd, const hidl_vec<hidl_string>& 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 <USAGE> <ZONE_ID> <FOCUS_GAIN>: 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<hidl_string>& options) {
    if (!checkCallerHasWritePermissions(fd) || !checkArgumentsSize(fd, options, 3)) return;

    hidl_bitfield<AudioUsage> 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<AudioFocusChange> 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<hidl_string>& 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
+4 −0
Original line number Diff line number Diff line
@@ -43,10 +43,14 @@ class AudioControl : public IAudioControl {
  private:
    sp<IFocusListener> mFocusListener;

    static bool checkArgumentsSize(int fd, const hidl_vec<hidl_string>& 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<hidl_string>& options);
    void cmdHelp(int fd) const;
    void cmdRequestFocus(int fd, const hidl_vec<hidl_string>& options);
    void dump(int fd);
};