aml_commonlib: Add to get the ssid and password of the connected wifi [1/1]
PD#SWPL-161035
Problem:
Missing interface and implementation to get ssid and password
Solution:
Get current ssid from wpa status and password from /etc/wpa_supplicant.conf
Verify:
a113l2
Change-Id: Ifaf354733892c1bcdeb659eb302226c771089c59
Signed-off-by: yifan.li <yifan.li@amlogic.com>
diff --git a/aml_wpa_wifi_util/Makefile b/aml_wpa_wifi_util/Makefile
index f9606bc..97c3f77 100644
--- a/aml_wpa_wifi_util/Makefile
+++ b/aml_wpa_wifi_util/Makefile
@@ -20,7 +20,7 @@
-o $@ $^ $(LDLIBS)
ifeq ($(BUILD_BIN_TARGET),1)
- $(CC) $(CFLAGS) $(LDFLAGS) -o $(BIN_TARGET) aml_wpa_wifi_test_demo.c -L. -laml_wpa_wifi_util $(LDLIBS) -w
+ $(CC) $(CFLAGS) -o $(BIN_TARGET) aml_wpa_wifi_test_demo.c -L. -laml_wpa_wifi_util $(LDLIBS) -w
endif
clean:
diff --git a/aml_wpa_wifi_util/aml_wpa_wifi_test_demo.c b/aml_wpa_wifi_util/aml_wpa_wifi_test_demo.c
index dbde73f..6c6f451 100644
--- a/aml_wpa_wifi_util/aml_wpa_wifi_test_demo.c
+++ b/aml_wpa_wifi_util/aml_wpa_wifi_test_demo.c
@@ -36,7 +36,7 @@
#include "aml_wpa_wifi_util.h"
#define INVALID_INPUT -99
-#define MAX_TEST_CASE 7
+#define MAX_TEST_CASE 8
#define INPUT_NUMBER(var, ...) get_input_string(type_int, var, __VA_ARGS__)
#define INPUT_STRING(var, ...) get_input_string(type_string, var, __VA_ARGS__)
#define INPUT_CHAR(var, ...) get_input_string(type_char, var, __VA_ARGS__)
@@ -128,6 +128,7 @@
printf(" [ 5 ] Get current wifi connection status\n");
printf(" [ 6 ] Get current wifi scan status \n");
printf(" [ 7 ] Get current wifi connection info \n");
+ printf(" [ 8 ] Get current connected ssid&pw \n");
printf(" [ h ] Show this list \n");
printf(" [ q ] Quit \n");
printf("***********************************************************************\n");
@@ -209,6 +210,15 @@
case 7:
wpa_wifi_send_status_cmd();
break;
+ case 8:
+ ret = wpa_wifi_get_current_connected_ssid_and_password(ssid, password);
+ if (ret == RETURN_OK) {
+ printf("current connected Wi-Fi SSID: %s\n", ssid);
+ printf("current connected Wi-Fi Password: %s\n", password);
+ } else {
+ printf("failed to get current Wi-Fi SSID and password.\n");
+ }
+ break;
default:
printf("Option [%d] not supported \n",select);
continue;
diff --git a/aml_wpa_wifi_util/aml_wpa_wifi_util.c b/aml_wpa_wifi_util/aml_wpa_wifi_util.c
index 30580c1..bccfbe2 100644
--- a/aml_wpa_wifi_util/aml_wpa_wifi_util.c
+++ b/aml_wpa_wifi_util/aml_wpa_wifi_util.c
@@ -337,7 +337,7 @@
return ret;
}
-int wpa_wifi_send_connect_cmd(const char* ssid, const char* passward) {
+int wpa_wifi_send_connect_cmd(const char* ssid, const char* password) {
char result[64];
pthread_mutex_lock(&g_wpa_manager.sup_lock);
g_wpa_manager.cur_wifi_status = WPA_WIFI_INVALID;
@@ -348,7 +348,7 @@
send_wpa_cli_command(result, sizeof(result)-1, "SET_NETWORK %d ssid \"%s\"", g_wpa_manager.cur_enable_network_id, ssid);
send_wpa_cli_command(result, sizeof(result)-1, "SET_NETWORK %d key_mgmt WPA-PSK", g_wpa_manager.cur_enable_network_id);
// set psk
- send_wpa_cli_command(result, sizeof(result)-1, "SET_NETWORK %d psk \"%s\"", g_wpa_manager.cur_enable_network_id, passward);
+ send_wpa_cli_command(result, sizeof(result)-1, "SET_NETWORK %d psk \"%s\"", g_wpa_manager.cur_enable_network_id, password);
send_wpa_cli_command(result, sizeof(result)-1, "SET_NETWORK %d pairwise CCMP TKIP", g_wpa_manager.cur_enable_network_id);
send_wpa_cli_command(result, sizeof(result)-1, "SET_NETWORK %d group CCMP TKIP", g_wpa_manager.cur_enable_network_id);
@@ -572,6 +572,79 @@
return RETURN_OK;
}
+int wpa_wifi_get_current_connected_ssid_and_password(char* ssid, char* password) {
+ if (ssid == NULL || password == NULL) {
+ AML_LOGE("Error: NULL pointer of ssid or password passed.\n");
+ return RETURN_ERR;
+ }
+ char result[512];
+ int ret;
+ int result_len = sizeof(result)-1;
+
+ // Get current ssid
+ if (g_wpa_manager.cur_wifi_status != WPA_WIFI_SUCCESS) {
+ AML_LOGW("Current wifi is unconnected \n");
+ return RETURN_ERR;
+ }
+ pthread_mutex_lock(&g_wpa_manager.sup_lock);
+ ret = send_wpa_cli_command(result, result_len, "STATUS");
+ pthread_mutex_unlock(&g_wpa_manager.sup_lock);
+ if (ret) {
+ AML_LOGE("send wpa cmd STATUS fail\n");
+ return ret;
+ }
+ char* cur_line = strtok(result, "\n");
+ while (cur_line != NULL) {
+ if (strncmp(cur_line, "ssid=", 5) == 0) {
+ char* ssid_start = cur_line + 5;
+ char* ssid_end = strchr(ssid_start, '\0');
+ if (ssid_end) {
+ *ssid_end = '\0'; // Null-terminate the SSID string
+ strncpy(ssid, ssid_start, ssid_end - ssid_start + 1);
+ break;
+ } else {
+ AML_LOGE("SSID not found in STATUS command result.\n");
+ return RETURN_ERR;
+ }
+ }
+ cur_line = strtok(NULL, "\n");
+ }
+
+ if (cur_line == NULL) {
+ AML_LOGE("SSID not found in STATUS command result.\n");
+ return RETURN_ERR;
+ }
+
+ // Read the wpa_supplicant.conf file to find the PSK for the current SSID
+ FILE* conf = fopen(WPA_SUPPLICANT_CONF_PATH, "r");
+ if (!conf) {
+ AML_LOGE("Failed to open wpa_supplicant.conf.\n");
+ return RETURN_ERR;
+ }
+
+ char line[256];
+ int ssid_found = 0;
+ while (fgets(line, sizeof(line), conf) != NULL) {
+ if (strstr(line, ssid) && strstr(line, "ssid=\"")) {
+ ssid_found = 1;
+ } else if (ssid_found && strstr(line, "psk=\"")) {
+ char* psk_start = strstr(line, "psk=\"") + 5;
+ char* psk_end = strchr(psk_start, '\"');
+ if (psk_end) {
+ *psk_end = '\0';
+ strncpy(password, psk_start, psk_end - psk_start + 1);
+ fclose(conf);
+ return RETURN_OK;
+ }
+ }
+ }
+
+ fclose(conf);
+ AML_LOGE("PSK not found for SSID %s.\n", ssid);
+ return RETURN_ERR;
+
+}
+
/*
Mainly to make sure to get the status of connected, when the wifi is already connected;
the other statuses don't matter
diff --git a/aml_wpa_wifi_util/aml_wpa_wifi_util.h b/aml_wpa_wifi_util/aml_wpa_wifi_util.h
index 86a01f6..6e5f447 100644
--- a/aml_wpa_wifi_util/aml_wpa_wifi_util.h
+++ b/aml_wpa_wifi_util/aml_wpa_wifi_util.h
@@ -134,13 +134,14 @@
/*
Only wpa-psk is supported for now, any legal encryption is supported
*/
-int wpa_wifi_send_connect_cmd(const char* ssid, const char* passward);
+int wpa_wifi_send_connect_cmd(const char* ssid, const char* password);
int wpa_wifi_send_disconnect_cmd();
int wpa_wifi_send_reconnect_cmd();
int wpa_wifi_get_current_wifi_status(WPA_WIFI_STATUS_TYPE* status);
int wpa_wifi_get_current_scan_status(WPA_WIFI_SCAN_STATE* status);
int wpa_wifi_get_scan_results(WPA_WIFI_AP_INFO wifi_array_to_fill[], const int array_size, int* ap_count, int is_sort_by_strength);
+int wpa_wifi_get_current_connected_ssid_and_password(char* ssid, char* password);
int wpa_wifi_init(const char* wpa_supl_ctrl, wpa_wifi_connect_callback connect_callback, int enable_network_id, int save_when_connected);
int wpa_wifi_uninit();