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
diff --git a/automotive/audiocontrol/2.0/default/AudioControl.cpp b/automotive/audiocontrol/2.0/default/AudioControl.cpp
index eac3514..7a121f5 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 <android-base/logging.h>
-#include <android-base/strings.h>
-#include <hidl/HidlTransportSupport.h>
-
 #include <stdio.h>
 
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
+
+#include <hidl/HidlTransportSupport.h>
+#include <hwbinder/IPCThreadState.h>
+#include <private/android_filesystem_config.h>
+
 #include "CloseHandle.h"
 
 namespace android::hardware::automotive::audiocontrol::V2_0::implementation {
@@ -104,6 +108,8 @@
     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 @@
     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
diff --git a/automotive/audiocontrol/2.0/default/AudioControl.h b/automotive/audiocontrol/2.0/default/AudioControl.h
index 6a3f21b..1cd7cbc 100644
--- a/automotive/audiocontrol/2.0/default/AudioControl.h
+++ b/automotive/audiocontrol/2.0/default/AudioControl.h
@@ -43,10 +43,14 @@
   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);
 };