agm: Update graph init to select acdb file path

Remove form factor check and add split snd card.
Append acdb file path with snd card name.

Change-Id: I0285ff65902c36e8b95e67046e896d2947f64152
Signed-off-by: Rutvik Patel <prutvikr@codeaurora.org>
diff --git a/service/inc/private/agm/device.h b/service/inc/private/agm/device.h
index 97d061d..7dadaf8 100644
--- a/service/inc/private/agm/device.h
+++ b/service/inc/private/agm/device.h
@@ -200,4 +200,5 @@
 int device_get_group_list(struct aif_info *aif_list, size_t *num_groups);
 
 int device_get_start_refcnt(struct device_obj *dev_obj);
+bool get_file_path_extn(char* file_path_extn);
 #endif
diff --git a/service/src/device.c b/service/src/device.c
index 965748f..ef26b93 100644
--- a/service/src/device.c
+++ b/service/src/device.c
@@ -64,6 +64,11 @@
 #define DEVICE_ENABLE 1
 #define DEVICE_DISABLE 0
 
+#define BUF_SIZE 1024
+#define FILE_PATH_EXTN_MAX_SIZE 80
+#define MAX_RETRY_CNT 20
+#define SND_CARD_DEVICE_FILE "/proc/asound/cards"
+
 /* Global list to store supported devices */
 static struct listnode device_list;
 static struct listnode device_group_data_list;
@@ -1066,3 +1071,148 @@
         mixer_close(mixer);
 #endif
 }
+
+static void split_snd_card_name(const char * in_snd_card_name, char* file_path_extn)
+{
+    /* Sound card name follows below mentioned convention:
+       <target name>-<form factor>-<variant>-snd-card.
+    */
+    char *snd_card_name = NULL;
+    char *tmp = NULL;
+    char *card_sub_str = NULL;
+
+    snd_card_name = strdup(in_snd_card_name);
+    if (snd_card_name == NULL) {
+        goto done;
+    }
+
+    card_sub_str = strtok_r(snd_card_name, "-", &tmp);
+    if (card_sub_str == NULL) {
+        AGM_LOGE("called on invalid snd card name(%s)", in_snd_card_name);
+        goto done;
+    }
+    strlcat(file_path_extn, card_sub_str, FILE_PATH_EXTN_MAX_SIZE);
+
+    while ((card_sub_str = strtok_r(NULL, "-", &tmp))) {
+        if (strncmp(card_sub_str, "snd", strlen("snd"))) {
+            strlcat(file_path_extn, "_", FILE_PATH_EXTN_MAX_SIZE);
+            strlcat(file_path_extn, card_sub_str, FILE_PATH_EXTN_MAX_SIZE);
+        }
+        else
+            break;
+    }
+
+done:
+    if (snd_card_name)
+        free(snd_card_name);
+}
+
+static bool check_and_update_snd_card(char *in_snd_card_str)
+{
+    char *str = NULL, *tmp = NULL;
+    int card = 0, card_id = -1;
+    bool is_updated = false;
+    char *snd_card_str = strdup(in_snd_card_str);
+
+    if (snd_card_str == NULL) {
+        return is_updated;
+    }
+
+    card = device_get_snd_card_id();
+    if (card < 0) {
+        goto done;
+    }
+
+    str = strtok_r(snd_card_str, "[:] ", &tmp);
+    if (str == NULL) {
+        goto done;
+    }
+    card_id = atoi(str);
+
+    str = strtok_r(NULL, "[:] ", &tmp);
+    if (str == NULL) {
+        goto done;
+    }
+
+    if (card_id == card) {
+        is_updated = true;
+    }
+
+done:
+    if (snd_card_str)
+        free(snd_card_str);
+    return is_updated;
+}
+
+static bool update_snd_card_info(char snd_card_name[])
+{
+    bool is_updated = false;
+    char line1[BUF_SIZE] = {0}, line2[BUF_SIZE] = {0};
+    FILE *file = NULL;
+    int len = 0, retries = MAX_RETRY;
+    char *card_name = NULL, *tmp = NULL;
+
+    if (access(SND_CARD_DEVICE_FILE, F_OK) != -1) {
+        file = fopen(SND_CARD_DEVICE_FILE, "r");
+        if (file == NULL) {
+            AGM_LOGE("open %s: failed\n", SND_CARD_DEVICE_FILE);
+            goto done;
+        }
+    } else {
+        AGM_LOGE("Unable to access %s\n", SND_CARD_DEVICE_FILE);
+        goto done;
+    }
+
+    /* Look for only default codec sound card */
+    /* Ignore USB sound card if detected */
+    /* Example of data read from /proc/asound/cards: */
+    /* card-id [dummyidpsndcard]: dummy-idp-variant-snd- - dummy-idp-variant-snd-card */
+    /*                       dummy-idp-variant-snd-card */
+    do {
+        if (!fgets(line1, BUF_SIZE - 1, file)) {
+            break;
+        }
+        len = strlen(line1);
+        line1[len - 1] = '\0';
+
+        if (!fgets(line2, BUF_SIZE - 1, file)) {
+            break;
+        }
+        len = strlen(line2);
+        line2[len - 1] = '\0';
+
+        if (check_and_update_snd_card(line1)) {
+            card_name = strtok_r(line2, "[:] ", &tmp);
+            if (card_name != NULL) {
+                strlcpy(snd_card_name, card_name, FILE_PATH_EXTN_MAX_SIZE);
+                is_updated = true;
+            }
+        }
+    } while(!is_updated && --retries);
+
+done:
+    if (file)
+        fclose(file);
+    return is_updated;
+}
+
+bool get_file_path_extn(char* file_path_extn)
+{
+    int snd_card_found = false, retry = 0;
+    char snd_card_name[FILE_PATH_EXTN_MAX_SIZE];
+
+    do {
+        snd_card_found = update_snd_card_info(snd_card_name);
+
+        if (snd_card_found) {
+            split_snd_card_name(snd_card_name, file_path_extn);
+            AGM_LOGV("Found Codec sound card");
+            break;
+        } else {
+            AGM_LOGI("Sound card not found, retry %d", retry++);
+            sleep(1);
+        }
+    } while (!snd_card_found && retry <= MAX_RETRY_CNT);
+
+    return snd_card_found;
+}
diff --git a/service/src/graph.c b/service/src/graph.c
index 9731436..320b7d7 100644
--- a/service/src/graph.c
+++ b/service/src/graph.c
@@ -50,8 +50,7 @@
 
 #define DEVICE_RX 0
 #define DEVICE_TX 1
-#define MAX_PATH 256
-#define BUF_SIZE 1024
+#define FILE_PATH_EXTN_MAX_SIZE 80
 #define ACDB_PATH_MAX_LENGTH 50
 
 #define TAGGED_MOD_SIZE_BYTES 1024
@@ -280,54 +279,23 @@
     struct gsl_acdb_file delta_file;
     struct gsl_init_data init_data;
     const char *delta_file_path;
-    unsigned int card = 0;
-    FILE *file = NULL;
-    int len = 0;
-    char *snd_card_name = NULL;
-    char filename[MAX_PATH];
+    char file_path_extn[FILE_PATH_EXTN_MAX_SIZE] = {0};
+    bool snd_card_found = false;
 
 #ifndef ACDB_PATH
 #  error "Define -DACDB_PATH="PATH" in the makefile to compile"
 #endif
-    card = device_get_snd_card_id();
-    if (card < 0) {
-        ret = -EINVAL;
-        goto err;
-    }
-
     /*Populate acdbfiles from the shared file path*/
     acdb_files.num_files = 0;
-    snprintf(filename, MAX_PATH, "/proc/asound/card%d/id", card);
-    if (access(filename, F_OK) != -1) {
-        file = fopen(filename, "r");
-        if (!file) {
-            AGM_LOGE("open %s: failed\n", filename);
-            ret = -EIO;
-            goto err;
-        } else {
-            snd_card_name = calloc(1, BUF_SIZE);
-            if (!snd_card_name) {
-                ret = -ENOMEM;
-                goto err;
-            }
-            if (fgets(snd_card_name, BUF_SIZE - 1, file)) {
-                len = strlen(snd_card_name);
-                snd_card_name[len - 1] = '\0';
-                if (strstr(snd_card_name, "qrd")) {
-                    snprintf(acdb_path, ACDB_PATH_MAX_LENGTH, "%s%s", ACDB_PATH, "QRD");
-                } else {
-                    snprintf(acdb_path, ACDB_PATH_MAX_LENGTH, "%s%s", ACDB_PATH, "IDP");
-                    if (strstr(snd_card_name, "slate")) {
-                        strlcat(acdb_path, "/slate", ACDB_PATH_MAX_LENGTH);
-                    }
-                }
-                free(snd_card_name);
-                snd_card_name = NULL;
-                fclose(file);
-                file = NULL;
-            }
-        }
+
+    snd_card_found = get_file_path_extn(file_path_extn);
+    if (snd_card_found) {
+        snprintf(acdb_path, ACDB_PATH_MAX_LENGTH, "%s%s", ACDB_PATH, file_path_extn);
+    } else {
+        ret = -ENOENT;
+        goto err;
     }
+    AGM_LOGI("acdb file path: %s\n", acdb_path);
 
     ret = get_acdb_files_from_directory(acdb_path, &acdb_files);
     if (ret)
@@ -358,10 +326,6 @@
     }
 
 err:
-    if (file) {
-        fclose(file);
-        file = NULL;
-    }
     return ret;
 }