[BugFix][API-810][wifi] add countrycode support

Change-Id: I49c9763e26f824dff7fcfc1f6b4858a58c758f7c
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index 839f835..b9658b9 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -3538,28 +3538,111 @@
     return 0;
 }
 
-int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
-//    const char * cmd_str = "GET country";
-//    CHECK_IDX(idx, CTRL_AP);
-//    CHECK_WPA_CTRL(CTRL_STA);
+static int check_and_init_uci_config(char * country_code)
+{
+    FILE * fp;
+    int is_different = 0;
+    const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
+    const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
+    const char * commit_uci_cmd ="uci commit";
+    char set_country_cmd[MAX_CMD];
+    char lynq_cmd_ret[MAX_CMD]={0};
 
-//    DO_REQUEST(cmd_str);
-//    printf("result %s\n", cmd_reply);
+    sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
 
-    if (country_code == NULL || *country_code == '\0')
+    if (0 != system(check_uci_cmd))
     {
-        RLOGD("bad country code\n");
+        if (0 != system(create_uci_cmd))
+        {
+            RLOGE("creat_uci_cmd fail");
+            return -1;
+        }
+        is_different = 1;
+    }
+
+    if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
+    {
+        RLOGE("popen error!");
         return -1;
     }
 
-    char lynq_country_cmd[MAX_CMD];
-    sprintf(lynq_country_cmd, "wl country %s", country_code);
-    if (system(lynq_country_cmd) == 0)
+    if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
     {
-        return 0;
+        RLOGE("fread fail!");
+        fclose(fp);
+        return -1;
     }
 
-    return -1;
+    if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
+    {
+        RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
+        is_different = 1;
+    }
+
+    fclose(fp);
+
+    if (is_different)
+    {
+        if ( 0 != system(set_country_cmd))
+        {
+            RLOGE("set_country_cmd fail");
+            return -1;
+        }
+        if (0 != system(commit_uci_cmd))
+        {
+            RLOGE("commmit fail");
+        }
+    }
+
+    return is_different;
+}
+
+int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
+    char check_current_code[10];
+    const char * support_country[] = {"CN", "EU"};
+
+    int ret,is_different, i, cc_count;
+
+    if (country_code == NULL || country_code[0] == '\0')
+    {
+        RLOGE("bad country code\n");
+        return -1;
+    }
+
+    cc_count = sizeof (support_country) / sizeof (char*);
+    for(i=0; i < cc_count; i++)
+    {
+        if (strcmp(support_country[i], country_code) == 0)
+        {
+            break;
+        }
+    }
+
+    if (i >= cc_count)
+    {
+        RLOGE("unspported country code %s\n", country_code);
+        return -1;
+    }
+
+    is_different = check_and_init_uci_config(country_code);
+    if( is_different < 0 )
+    {
+        RLOGE("init set uci fail\n");
+        return -1;
+    }
+
+    ret = lynq_get_country_code(idx,check_current_code);
+    if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
+    {
+        ret = lynq_wifi_disable();
+        if(ret != 0 )
+        {
+            RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
+            return -1;
+        }
+    }
+
+    return 0;
 }
 
 int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)