From d4a1bd480003f3e1a0590bc46fbcb24f05652ca7 Mon Sep 17 00:00:00 2001
From: tzh <tanzhtanzh@gmail.com>
Date: Thu, 15 Aug 2024 06:56:47 +0000
Subject: [PATCH] feat(wfit/bt): update aic8800 wifi/bt drive and hal

---
 android/hardware/aw/wireless/hwinfo/libhwinfo.c |  123 +++++++++++++++++++++++++++++++++-------
 1 files changed, 100 insertions(+), 23 deletions(-)

diff --git a/android/hardware/aw/wireless/hwinfo/libhwinfo.c b/android/hardware/aw/wireless/hwinfo/libhwinfo.c
index 9ad0fa6..12653bd 100755
--- a/android/hardware/aw/wireless/hwinfo/libhwinfo.c
+++ b/android/hardware/aw/wireless/hwinfo/libhwinfo.c
@@ -29,6 +29,7 @@
 #include <log/log.h>
 #include <cutils/properties.h>
 #include <pthread.h>
+#include <regex.h>
 
 #define ARRAY_SIZE(arr)         (sizeof(arr) / sizeof((arr)[0]))
 #define UEVENT_MSG_LEN          1024
@@ -111,12 +112,8 @@
 
 static const struct info_t wifi_drv_para[] = {
     {"broadcom",
-        {"nvram_path=/vendor/etc/firmware/nvram_",
-         "$module_name",
-         ".txt ",
-         "config_path=/vendor/etc/firmware/config_",
-         "$module_name",
-         ".txt",
+        {"nvram_path=/vendor/etc/firmware/nvram_${module_name}.txt",
+         "config_path=/vendor/etc/firmware/config_${module_name}.txt",
          0,
         }
     },
@@ -126,11 +123,7 @@
         }
     },
     {"ssv",
-        {"stacfgpath=/vendor/etc/firmware/",
-         "$module_name",
-         "/",
-         "$module_name",
-         "-wifi.cfg",
+        {"stacfgpath=/vendor/etc/firmware/${module_name}/${module_name}-wifi.cfg",
          0,
         }
     },
@@ -253,6 +246,90 @@
     p++;
     strncpy(val, dst, p -dst);
     val[p - dst] = 0;
+    return 0;
+}
+
+// You must free the result if result is non-NULL.
+static char *str_replace(char *orig, char *rep, char *with)
+{
+    char *result; // the return string
+    char *ins;    // the next insert point
+    char *tmp;    // varies
+    int len_rep;  // length of rep (the string to remove)
+    int len_with; // length of with (the string to replace rep with)
+    int len_front; // distance between rep and end of last rep
+    int count;    // number of replacements
+
+    // sanity checks and initialization
+    if (!orig || !rep)
+        return NULL;
+    len_rep = strlen(rep);
+    if (len_rep == 0)
+        return NULL; // empty rep causes infinite loop during count
+    if (!with)
+        with = "";
+    len_with = strlen(with);
+
+    // count the number of replacements needed
+    ins = orig;
+    for (count = 0; (tmp = strstr(ins, rep)); ++count) {
+        ins = tmp + len_rep;
+    }
+
+    tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
+
+    if (!result)
+        return NULL;
+
+    // first time through the loop, all the variable are set correctly
+    // from here on,
+    //    tmp points to the end of the result string
+    //    ins points to the next occurrence of rep in orig
+    //    orig points to the remainder of orig after "end of rep"
+    while (count--) {
+        ins = strstr(orig, rep);
+        len_front = ins - orig;
+        tmp = strncpy(tmp, orig, len_front) + len_front;
+        tmp = strcpy(tmp, with) + len_with;
+        orig += len_front + len_rep; // move to next "end of rep"
+    }
+    strcpy(tmp, orig);
+    return result;
+}
+
+static int str_expand(char *text, char *expand)
+{
+    static const char *pattern = "\\$\\{[^}]+\\}";
+    regex_t reg;
+    const size_t nmatch = 1;
+    int i, j, status;
+    regmatch_t pmatch[1];
+    char matchstr[32];
+    char env_name[32];
+    char *env_val = NULL;
+    char *orig = text;
+    char *out;
+
+    status = regcomp(&reg, pattern, REG_EXTENDED);
+    while (regexec(&reg, orig, nmatch, pmatch, 0) == 0) {
+        memcpy(matchstr, orig + pmatch[0].rm_so, pmatch[0].rm_eo - pmatch[0].rm_so);
+        matchstr[pmatch[0].rm_eo - pmatch[0].rm_so] = 0;
+
+        memcpy(env_name, orig + pmatch[0].rm_so + 2, pmatch[0].rm_eo - pmatch[0].rm_so - 3);
+        env_name[pmatch[0].rm_eo - pmatch[0].rm_so - 3] = 0;
+
+        env_val = getenv(env_name);
+        out = str_replace(orig, matchstr, env_val);
+
+        if (out) {
+            memcpy(expand, out, strlen(out));
+            expand[strlen(out)] = 0;
+            free(out);
+            orig = expand;
+        }
+    }
+    regfree(&reg);
+
     return 0;
 }
 
@@ -783,6 +860,12 @@
             property_set(MODULE_BT_SUPPORT_PROP,  wifiinfo->bt_support ? "1" : "0");
         }
     }
+
+    for (i = 0; i < ARRAY_SIZE(matchtab); i++) {
+        if (matchtab[i].type == TYPE_PCHAR)
+            setenv(matchtab[i].keyname, (char *)(*(ADDR_T *)((ADDR_T)wifiinfo + matchtab[i].offset)), 1);
+    }
+
     return wifiinfo;
 }
 
@@ -812,23 +895,17 @@
 
 const char *get_driver_module_arg(void)
 {
-    static char module_arg[256] = {0};
+    static char module_arg[1024] = {0};
     struct wifi_hardware_info *hwinfo = get_wifi_hardware_info();
-    const  char *para;
+    char   buffer[1024];
     int    n = 0;
     for (int i = 0; i < ARRAY_SIZE(wifi_drv_para); i++) {
         if (strncmp(hwinfo->vendor_name, wifi_drv_para[i].vendor, strlen(hwinfo->vendor_name)) == 0) {
-            for (int j = 0; (j < INFO_SESSION_MAX) && ((para = wifi_drv_para[i].info[j]) != NULL); j++) {
-                if (*para == '$') {
-                   for (int k = 0; k < ARRAY_SIZE(matchtab); k++) {
-                       if (strncmp(para + 1, matchtab[k].keyname, strlen(matchtab[k].keyname)) == 0) {
-                          para = (const char *)(*(ADDR_T *)((ADDR_T)hwinfo + matchtab[k].offset));
-                          break;
-                       }
-                   }
-                }
-                n += sprintf(&module_arg[n], "%s", para);
+            for (int j = 0; (j < INFO_SESSION_MAX) && wifi_drv_para[i].info[j] != NULL; j++) {
+                n += sprintf(&buffer[n], "%s ", wifi_drv_para[i].info[j]);
             }
+            buffer[n - 2] = '\0';
+            str_expand(buffer, module_arg);
             return module_arg;
         }
     }

--
Gitblit v1.6.2