[Bugfix][API-495][wifi] add mutex to fix customer's stop call fail
Change-Id: I0cc5e6073e3c02946ca526a8b006d3c1180f77f4
diff --git a/lib/liblynq-wifi6/libwifi6.c b/lib/liblynq-wifi6/libwifi6.c
index 03fc67a..c60a6d4 100755
--- a/lib/liblynq-wifi6/libwifi6.c
+++ b/lib/liblynq-wifi6/libwifi6.c
@@ -60,7 +60,14 @@
const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
const char * STATE_COMPLETED = "COMPLETED";
-static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
+struct local_wpa_ctrl{
+ struct wpa_ctrl *ctrl;
+ pthread_mutex_t mutex;
+};
+
+static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
typedef struct __curr_status_info {
@@ -69,6 +76,48 @@
int net_no;
}curr_status_info;
+static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
+ char *reply, size_t *reply_len,
+ void (*msg_cb)(char *msg, size_t len))
+{
+ int ret;
+ if (ctrl->ctrl == NULL) {
+ printf("local_wpa_ctrl_request ctrl is null\n");
+ return -1;
+ }
+ pthread_mutex_lock(&ctrl->mutex);
+ ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
+ pthread_mutex_unlock(&ctrl->mutex);
+ return ret;
+}
+
+static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
+ int repeat_cnt;
+ struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
+ pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
+ for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
+ pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
+// printf("wait enable finish\n");
+ usleep(500 * 1000);
+ pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
+ }
+ if (NULL == g_lynq_wpa_ctrl[index]) {
+ goto out_addr;
+ }
+ if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
+ g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
+ if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
+ printf("wpa_ctrl_open fail\n");
+ goto out_addr;
+ }
+ pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
+ }
+ lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
+out_addr:
+ pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
+ return lynq_wpa_ctrl;
+}
+
#define PRINT_AND_RETURN_VALUE(str,value) \
{\
perror((str));\
@@ -86,23 +135,17 @@
#define CHECK_WPA_CTRL(index) int ret = 0;\
size_t reply_len = MAX_RET; \
char cmd_reply[MAX_RET]={0}; \
- struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
+ struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
do{ \
- if (NULL == g_lynq_wpa_ctrl[index]) { \
- g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
- if (NULL == g_lynq_wpa_ctrl[index]) { \
- printf("wpa_ctrl_open fail\n"); \
- return -1; \
- } \
- } \
- lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
+ lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
+ if (NULL == lynq_wpa_ctrl) return -1; \
}while(0)
#define DO_REQUEST(cmd_str) do { \
reply_len = MAX_RET;\
cmd_reply[0] = '\0'; \
- printf("to call [%s]\n", cmd_str); \
- ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
+ printf("to call 3 [%s]\n", cmd_str); \
+ ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
if (ret != 0) { \
printf("call "#cmd_str" fail %d\n", ret); \
return ret; \
@@ -244,6 +287,13 @@
{
int ret = 0;
int i;
+
+ pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
+
+ if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
+ goto out_enable;
+ }
+
const char * cmd_check_service =
"state=`systemctl is-active wg870_drv_insmod.service`\n"
"[ \"\"$state == \"active\" ] && exit 0\n"
@@ -255,7 +305,8 @@
ret = system(cmd_check_service);
if (ret != 0) {
printf("service state %d\n", ret);
- return -1;
+ ret = -1;
+ goto out_enable;
}
for (i=0; i<10; i++) {
@@ -266,7 +317,8 @@
}
if (i >= 10) {
- return -1;
+ ret = -1;
+ goto out_enable;
}
//@todo delete add temp check for socket avilable start (20220606)
@@ -280,7 +332,8 @@
if (i >= 60)
{
- return -1;
+ ret = -1;
+ goto out_enable;
}
//@todo delete add temp check for socket avilable end (20220606)
@@ -298,14 +351,16 @@
if (g_ap_watcher_pid == 0 ) {
ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
if(ret<0){
- return -1;
+ ret = -1;
+ goto out_enable;
}
}
if (g_sta_watcher_pid == 0 ) {
ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
if(ret<0){
- return -1;
+ ret = -1;
+ goto out_enable;
}
}
@@ -316,11 +371,16 @@
}
}
+ g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
+ g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
+ out_enable:
+ pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
return ret;
}
int lynq_wifi_disable(void)
{
+ pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
g_ap_watcher_stop_flag = 1;
g_sta_watcher_stop_flag = 1;
if (g_ap_watcher_pid != 0)
@@ -336,6 +396,7 @@
g_lynq_wpa_ctrl[0] = NULL;
g_lynq_wpa_ctrl[1] = NULL;
system("systemctl stop wg870_drv_insmod.service");
+ pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
return 0;
}
@@ -1426,6 +1487,7 @@
}
if (index >= len || NULL == p || network_len <= 0) {
+ printf("index:%d-%d, %p, network_len:%d\n", index, len, p, network_len);
return -1;
}
@@ -1923,7 +1985,7 @@
sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
reply_len = MAX_RET;
cmd_reply[0] = '\0';
- ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
+ ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
printf("run %s fail \n", lynq_next_sta_cmd);
break;
@@ -2214,7 +2276,7 @@
// reply_len = MAX_RET;
// cmd_reply[0] = '\0';
// printf("to call [%s]\n", cmd_str);
-// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
+// ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
// if (ret != 0) {
// printf("call ##cmd_str fail %d\n", ret);
// return ret;