2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
32 wifi_mesh_h mesh = NULL;
34 extern struct menu_data menu_mesh_device[];
35 extern struct menu_data menu_mesh_network[];
37 /* Scanned Mesh Network */
38 extern GList *g_found_network_list;
40 const char* wifi_mesh_error_to_string(wifi_mesh_error_e err)
43 /* CHECK: List all enum values here */
44 CASE_TO_STR(WIFI_MESH_ERROR_NONE)
45 CASE_TO_STR(WIFI_MESH_ERROR_INVALID_PARAMETER)
46 CASE_TO_STR(WIFI_MESH_ERROR_OUT_OF_MEMORY)
47 CASE_TO_STR(WIFI_MESH_ERROR_NO_DATA)
48 CASE_TO_STR(WIFI_MESH_ERROR_INVALID_OPERATION)
49 CASE_TO_STR(WIFI_MESH_ERROR_ALREADY_IN_PROGRESS)
50 CASE_TO_STR(WIFI_MESH_ERROR_NOW_IN_PROGRESS)
51 CASE_TO_STR(WIFI_MESH_ERROR_PERMISSION_DENIED)
52 CASE_TO_STR(WIFI_MESH_ERROR_IO_ERROR)
53 CASE_TO_STR(WIFI_MESH_ERROR_NOT_SUPPORTED)
54 CASE_TO_STR(WIFI_MESH_ERROR_OPERATION_FAILED)
55 CASE_TO_STR(WIFI_MESH_ERROR_OPERATION_ABORTED)
56 CASE_TO_STR(WIFI_MESH_ERROR_ALREADY_INITIALIZED)
58 return "Unknown Error";
62 static const char* _wifi_mesh_event_to_string(wifi_mesh_event_e e)
65 /* CHECK: List all enum values here */
66 CASE_TO_STR(WIFI_MESH_EVENT_ENABLED)
67 CASE_TO_STR(WIFI_MESH_EVENT_SCAN_DONE)
68 CASE_TO_STR(WIFI_MESH_EVENT_CONNECTION_STATE)
69 CASE_TO_STR(WIFI_MESH_EVENT_STATION_JOIN)
70 CASE_TO_STR(WIFI_MESH_EVENT_STATION_LEFT)
72 return "MESH_EVENT_UNKNOWN";
76 const char* wifi_mesh_connection_event_to_string(wifi_mesh_connection_state_e e)
79 /* CHECK: List all enum values here */
80 CASE_TO_STR(WIFI_MESH_CONNECTION_STATE_DISCONNECTED)
81 CASE_TO_STR(WIFI_MESH_CONNECTION_STATE_ASSOCIATION)
82 CASE_TO_STR(WIFI_MESH_CONNECTION_STATE_CONFIGURATION)
83 CASE_TO_STR(WIFI_MESH_CONNECTION_STATE_CONNECTED)
85 return "Unknown state";
89 static const char *wifi_mesh_sta_type_to_string(wifi_mesh_station_type_e e)
92 CASE_TO_STR(MESH_STATION_TYPE_MESH_POINT)
93 CASE_TO_STR(MESH_STATION_TYPE_SOFTAP)
95 return "Unknown station type";
99 void event_cb(wifi_mesh_event_e event_type, wifi_mesh_event_data_s* event,
103 msgp("Event received [%s]", _wifi_mesh_event_to_string(event_type));
105 switch (event_type) {
106 case WIFI_MESH_EVENT_ENABLED: {
107 msgp(" Mesh Network Enabled Result = %d", event->data.wifi_mesh_enable->result);
109 case WIFI_MESH_EVENT_SCAN_DONE: {
110 msgp(" Mesh Scan Done");
112 case WIFI_MESH_EVENT_CONNECTION_STATE:{
113 msgp(" Connection state changed [%s]",
114 wifi_mesh_connection_event_to_string(event->data.connection_state->state));
115 msgp(" From Mesh ID[%-10s] BSSID[%s] Channel[%d] Security[%4s]",
116 event->data.connection_state->mesh_id,
117 event->data.connection_state->bssid,
118 event->data.connection_state->channel,
119 (MESH_SECURITY_SAE == event->data.connection_state->security) ? "SAE" : "NONE");
121 case WIFI_MESH_EVENT_STATION_JOIN: {
122 msgp(" New Station of type %s Joined = %s",
123 wifi_mesh_sta_type_to_string(event->data.sta_info->station_type),
124 event->data.sta_info->bssid);
126 case WIFI_MESH_EVENT_STATION_LEFT: {
127 msgp(" A Station of type %s Left = %s",
128 wifi_mesh_sta_type_to_string(event->data.sta_info->station_type),
129 event->data.sta_info->bssid);
132 msgr(" Unexpected event !");
137 static int __init_func(MManager *mm, struct menu_data *menu)
142 ret = wifi_mesh_initialize(&mesh);
144 msg("Failed to initialize mesh: [%s(0x%X)]",
145 wifi_mesh_error_to_string(ret), ret);
152 static struct menu_data menu_main[] = {
153 { "1", "Device", menu_mesh_device, NULL, NULL },
154 { "2", "Wi-Fi Mesh network", menu_mesh_network, NULL, NULL },
158 static gboolean __create_init_menu(struct menu_data init_menu[1])
160 init_menu[0].key = "1";
161 init_menu[0].title = "Init";
162 init_menu[0].sub_menu = menu_main;
163 init_menu[0].callback = __init_func;
164 init_menu[0].data = NULL;
169 int main(int arg, char **argv)
171 GMainLoop *mainloop = NULL;
172 GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
174 struct menu_data init_menu[1+1] = { {NULL, NULL, } };
176 #if !GLIB_CHECK_VERSION(2, 35, 0)
179 mainloop = g_main_loop_new(NULL, FALSE);
182 msg("* Wi-Fi Mesh Test application ");
183 msg("* Build On: %s %s", __DATE__, __TIME__);
185 if (__create_init_menu(init_menu) == FALSE)
188 manager = menu_manager_new(init_menu, mainloop);
192 menu_manager_run(manager);
194 g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
195 on_menu_manager_keyboard, manager);
196 g_main_loop_run(mainloop);
199 if (g_found_network_list)
200 g_list_free(g_found_network_list);
203 wifi_mesh_deinitialize(mesh);
205 g_main_loop_unref(mainloop);
206 msg("******* Bye bye *******");