audio: Extend platform parser to allow device name aliasing

Author: Ethan Chen <intervigil@gmail.com>
Date:   Fri Sep 5 13:01:26 2014 -0700

    audio: Extend platform parser to allow device name aliasing

    * Supported sections now include device names
    * This allows for aliasing device names to a custom name

    Change-Id: I880e90a7e887f020517d89ba276199c700c0eeae

Author: Nicholas Flintham <nick@flinny.org>
Date:   Fri Oct 19 21:56:28 2018 +0100

    Fix mismerge of audio: Extend platform parser to allow device name aliasing

        * device name aliases were not being applied.

    Change-Id: I20493069d2e4cb59250006ac8b14fbb086c297fa

Change-Id: I23319cb7c1fa2ce0c345f96e59455cb64d47988c
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 64e9383..f3cd987 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -421,7 +421,7 @@
 };
 
 /* Array to store sound devices */
-static const char * const device_table[SND_DEVICE_MAX] = {
+static const char * device_table[SND_DEVICE_MAX] = {
     [SND_DEVICE_NONE] = "none",
     /* Playback sound devices */
     [SND_DEVICE_OUT_HANDSET] = "handset",
@@ -8029,6 +8029,21 @@
     return snd_device;
 }
 
+int platform_set_snd_device_name(snd_device_t device, const char *name)
+{
+    int ret = 0;
+
+    if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
+        ALOGE("%s:: Invalid snd_device = %d", __func__, device);
+        ret = -EINVAL;
+        goto done;
+    }
+
+    device_table[device] = strdup(name);
+done:
+    return ret;
+}
+
 int platform_set_sidetone(struct audio_device *adev,
                           snd_device_t out_snd_device,
                           bool enable,
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
index 90105cd..9317488 100644
--- a/hal/msm8960/platform.c
+++ b/hal/msm8960/platform.c
@@ -1281,6 +1281,12 @@
     ALOGE("%s: Not implemented", __func__);
 }
 
+int platform_set_snd_device_name(snd_device_t snd_device __unused,
+                                 const char * name __unused)
+{
+    return -ENOSYS;
+}
+
 bool platform_can_enable_spkr_prot_on_device(snd_device_t snd_device __unused)
 {
     /* speaker protection not implemented for this platform*/
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 4a1d49c..ecf628e 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -488,7 +488,7 @@
 };
 
 /* Array to store sound devices */
-static const char * const device_table[SND_DEVICE_MAX] = {
+static const char * device_table[SND_DEVICE_MAX] = {
     [SND_DEVICE_NONE] = "none",
     /* Playback sound devices */
     [SND_DEVICE_OUT_HANDSET] = "handset",
@@ -10254,6 +10254,21 @@
     return ret;
 }
 
+int platform_set_snd_device_name(snd_device_t device, const char *name)
+{
+    int ret = 0;
+
+    if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
+        ALOGE("%s:: Invalid snd_device = %d", __func__, device);
+        ret = -EINVAL;
+        goto done;
+    }
+
+    device_table[device] = strdup(name);
+done:
+    return ret;
+}
+
 int platform_set_sidetone(struct audio_device *adev,
                           snd_device_t out_snd_device,
                           bool enable,
diff --git a/hal/platform_api.h b/hal/platform_api.h
index 30a10c5..932932b 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -247,6 +247,7 @@
 void platform_add_app_type(const char *uc_type,
                            const char *mode,
                            int bw, int app_type, int max_sr);
+int platform_set_snd_device_name(snd_device_t snd_device, const char * name);
 
 /* From platform_info.c */
 int platform_info_init(const char *filename, void *, caller_t);
diff --git a/hal/platform_info.c b/hal/platform_info.c
index d73792c..b603ff7 100644
--- a/hal/platform_info.c
+++ b/hal/platform_info.c
@@ -75,6 +75,7 @@
     EXTERNAL_DEVICE_SPECIFIC,
     CUSTOM_MTMX_IN_PARAMS,
     CUSTOM_MTMX_PARAM_IN_CH_INFO,
+    DEVICE_NAME,
 } section_t;
 
 typedef void (* section_process_fn)(const XML_Char **attr);
@@ -101,6 +102,7 @@
 static void process_external_dev(const XML_Char **attr);
 static void process_custom_mtmx_in_params(const XML_Char **attr);
 static void process_custom_mtmx_param_in_ch_info(const XML_Char **attr);
+static void process_device_name(const XML_Char **attr);
 
 static section_process_fn section_table[] = {
     [ROOT] = process_root,
@@ -124,6 +126,7 @@
     [EXTERNAL_DEVICE_SPECIFIC] = process_external_dev,
     [CUSTOM_MTMX_IN_PARAMS] = process_custom_mtmx_in_params,
     [CUSTOM_MTMX_PARAM_IN_CH_INFO] = process_custom_mtmx_param_in_ch_info,
+    [DEVICE_NAME] = process_device_name,
 };
 
 static section_t section;
@@ -273,6 +276,11 @@
  *      ...
  * </operator_specific>
  *
+ * <device_names>
+ * <device name="???" alias="???"/>
+ * ...
+ * ...
+ * </device_names>
  * </audio_platform_info>
  */
 
@@ -1190,7 +1198,38 @@
     }
 
     platform_add_custom_mtmx_params((void *)my_data.platform, &mtmx_params_info);
+}
 
+static void process_device_name(const XML_Char **attr)
+{
+    int index;
+
+    if (strcmp(attr[0], "name") != 0) {
+        ALOGE("%s: 'name' not found, no alias set!", __func__);
+        goto done;
+    }
+
+    index = platform_get_snd_device_index((char *)attr[1]);
+    if (index < 0) {
+        ALOGE("%s: Device %s in platform info xml not found, no alias set!",
+              __func__, attr[1]);
+        goto done;
+    }
+
+    if (strcmp(attr[2], "alias") != 0) {
+        ALOGE("%s: Device %s in platform info xml has no alias, no alias set!",
+              __func__, attr[1]);
+        goto done;
+    }
+
+    if (platform_set_snd_device_name(index, attr[3]) < 0) {
+        ALOGE("%s: Device %s, alias %s was not set!",
+              __func__, attr[1], attr[3]);
+        goto done;
+    }
+
+done:
+    return;
 }
 
 static void start_tag(void *userdata __unused, const XML_Char *tag_name,
@@ -1235,11 +1274,14 @@
             section = MICROPHONE_CHARACTERISTIC;
         } else if (strcmp(tag_name, "snd_devices") == 0) {
             section = SND_DEVICES;
+        } else if (strcmp(tag_name, "device_names") == 0) {
+            section = DEVICE_NAME;
         } else if (strcmp(tag_name, "device") == 0) {
             if ((section != ACDB) && (section != AEC) && (section != NS) &&
                 (section != BACKEND_NAME) && (section != BITWIDTH) &&
-                (section != INTERFACE_NAME) && (section != OPERATOR_SPECIFIC)) {
-                ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
+                (section != INTERFACE_NAME) && (section != OPERATOR_SPECIFIC) &&
+                (section != DEVICE_NAME)) {
+                ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface/device names");
                 return;
             }
 
@@ -1435,6 +1477,8 @@
         section = ROOT;
     } else if (strcmp(tag_name, "custom_mtmx_param_in_chs") == 0) {
         section = CUSTOM_MTMX_IN_PARAMS;
+    } else if (strcmp(tag_name, "device_names") == 0) {
+        section = ROOT;
     }
 }