Add status function to wifi_connect_tool 08/235708/3
authorCheoleun Moon <chleun.moon@samsung.com>
Tue, 9 Jun 2020 05:31:43 +0000 (14:31 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Tue, 9 Jun 2020 05:38:56 +0000 (14:38 +0900)
Change-Id: I5522e5ebab5c3d87d39ec72490b30930c706b978

tool/wifi_connect_tool.c

index 031b52b..61721e8 100755 (executable)
@@ -316,6 +316,63 @@ static void start()
        PRINT_RESULT("Connected\n");
 }
 
+static const char *convert_state_to_string(wifi_manager_connection_state_e state)
+{
+       switch(state) {
+       case WIFI_MANAGER_CONNECTION_STATE_FAILURE:
+               return "Failure";
+       case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
+               return "Disconnected";
+       case WIFI_MANAGER_CONNECTION_STATE_ASSOCIATION:
+               return "Association";
+       case WIFI_MANAGER_CONNECTION_STATE_CONFIGURATION:
+               return "Configuration";
+       case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
+               return "Connected";
+       default:
+               return "Error";
+       }
+}
+
+static void show_status()
+{
+       bool power = false;
+       wifi_manager_connection_state_e state;
+       wifi_manager_ap_h ap = NULL;
+       char *ap_name = NULL;
+       int ret = wifi_manager_initialize(&g_wifi);
+       EXIT_IF_RET_IS_ERROR(ret, "wifi_manager_initialize");
+
+       ret = wifi_manager_is_activated(g_wifi, &power);
+       EXIT_IF_RET_IS_ERROR(ret, "wifi_manager_is_activated");
+
+       if (power) {
+               ret = wifi_manager_get_connection_state(g_wifi, &state);
+               EXIT_IF_RET_IS_ERROR(ret, "wifi_manager_get_connection_state");
+
+               if (state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED) {
+                       ret = wifi_manager_get_connected_ap(g_wifi, &ap);
+                       EXIT_IF_RET_IS_ERROR(ret, "wifi_manager_get_connected_ap");
+                       ret = wifi_manager_ap_get_essid(ap, &ap_name);
+                       EXIT_IF_RET_IS_ERROR(ret, "wifi_manager_get_essid");
+                       printf("[WIFI] Connected %s\n", ap_name);
+               }
+               else {
+                       printf("[WIFI] %s\n", convert_state_to_string(state));
+               }
+       }
+       else {
+               printf("[WIFI] Power off\n");
+       }
+}
+
+static void print_usage()
+{
+       printf("Usage:\n");
+       printf("  wifi_connect_tool [SSID] [PASSWORD]\n");
+       printf("  wifi_connect_tool status\n");
+}
+
 int main(int argc, char **argv)
 {
        int passwd_len = 0;
@@ -324,11 +381,20 @@ int main(int argc, char **argv)
        g_type_init();
 #endif
 
-       if (argc != 3) {
-               printf("Usage: %s [SSID] [PASSWORD]\n", argv[0]);
+       if (argc != 3 && argc != 2) {
+               print_usage();
                return 1;
        }
 
+       if (argc == 2) {
+               if (strncmp(argv[1], "status", 6) != 0) {
+                       print_usage();
+                       return 1;
+               }
+               show_status();
+               return 0;
+       }
+
        if (strlen(argv[1]) > MAX_SSID_LEN) {
                PRINT_ERROR("Too long SSID\n");
                return 1;