Change for documentation link change
[platform/core/api/wifi-mesh.git] / test / main.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <sys/socket.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 #include <glib.h>
27 #include <gio/gio.h>
28
29 #include "menu.h"
30 #include "common.h"
31
32 wifi_mesh_h mesh = NULL;
33
34 extern struct menu_data menu_mesh_device[];
35 extern struct menu_data menu_mesh_network[];
36
37 /* Scanned Mesh Network */
38 extern GList *g_found_network_list;
39
40 const char* wifi_mesh_error_to_string(wifi_mesh_error_e err)
41 {
42         switch (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)
57         default :
58                 return "Unknown Error";
59         }
60 }
61
62 static const char* _wifi_mesh_event_to_string(wifi_mesh_event_e e)
63 {
64         switch (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)
71         default :
72                 return "MESH_EVENT_UNKNOWN";
73         }
74 }
75
76 const char* wifi_mesh_connection_event_to_string(wifi_mesh_connection_state_e e)
77 {
78         switch (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)
84         default :
85                 return "Unknown state";
86         }
87 }
88
89 static const char *wifi_mesh_sta_type_to_string(wifi_mesh_station_type_e e)
90 {
91         switch (e) {
92         CASE_TO_STR(MESH_STATION_TYPE_MESH_POINT)
93         CASE_TO_STR(MESH_STATION_TYPE_SOFTAP)
94         default :
95                 return "Unknown station type";
96         }
97 }
98
99 void event_cb(wifi_mesh_event_e event_type, wifi_mesh_event_data_s* event,
100                           void *user_data)
101 {
102         msg("");
103         msgp("Event received [%s]", _wifi_mesh_event_to_string(event_type));
104
105         switch (event_type) {
106         case WIFI_MESH_EVENT_ENABLED: {
107                 msgp("  Mesh Network Enabled Result = %d", event->data.wifi_mesh_enable->result);
108         } break;
109         case WIFI_MESH_EVENT_SCAN_DONE: {
110                 msgp("  Mesh Scan Done");
111         } break;
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");
120         } break;
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);
125         } break;
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);
130         } break;
131         default:
132                 msgr("  Unexpected event !");
133         break;
134         }
135 }
136
137 static int __init_func(MManager *mm, struct menu_data *menu)
138 {
139 #if 0
140         int ret = -1;
141
142         ret = wifi_mesh_initialize(&mesh);
143         if (ret != 0) {
144                 msg("Failed to initialize mesh: [%s(0x%X)]",
145                                 wifi_mesh_error_to_string(ret), ret);
146                 return RET_FAILURE;
147         }
148 #endif
149         return RET_SUCCESS;
150 }
151
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 },
155         { NULL, NULL, },
156 };
157
158 static gboolean __create_init_menu(struct menu_data init_menu[1])
159 {
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;
165
166         return TRUE;
167 }
168
169 int main(int arg, char **argv)
170 {
171         GMainLoop *mainloop = NULL;
172         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
173         MManager *manager;
174         struct menu_data init_menu[1+1] = { {NULL, NULL, } };
175
176 #if !GLIB_CHECK_VERSION(2, 35, 0)
177         g_type_init();
178 #endif
179         mainloop = g_main_loop_new(NULL, FALSE);
180
181         msg("");
182         msg("* Wi-Fi Mesh Test application ");
183         msg("* Build On: %s  %s", __DATE__, __TIME__);
184
185         if (__create_init_menu(init_menu) == FALSE)
186                 goto OUT;
187
188         manager = menu_manager_new(init_menu, mainloop);
189         if (!manager)
190                 goto OUT;
191
192         menu_manager_run(manager);
193
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);
197
198 OUT:
199         if (g_found_network_list)
200                 g_list_free(g_found_network_list);
201
202         if (mesh)
203                 wifi_mesh_deinitialize(mesh);
204
205         g_main_loop_unref(mainloop);
206         msg("******* Bye bye *******");
207
208         return 0;
209 }