Add 'MESH_CONNECTED_EVENT' event handler
[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 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* mesh_error_to_string(mesh_error_e err)
41 {
42         switch (err) {
43         /* CHECK: List all enum values here */
44         CASE_TO_STR(MESH_ERROR_NONE)
45         CASE_TO_STR(MESH_ERROR_INVALID_PARAMETER)
46         CASE_TO_STR(MESH_ERROR_OUT_OF_MEMORY)
47         CASE_TO_STR(MESH_ERROR_NO_DATA)
48         CASE_TO_STR(MESH_ERROR_INVALID_OPERATION)
49         CASE_TO_STR(MESH_ERROR_ALREADY_IN_PROGRESS)
50         CASE_TO_STR(MESH_ERROR_NOW_IN_PROGRESS)
51         CASE_TO_STR(MESH_ERROR_PERMISSION_DENIED)
52         CASE_TO_STR(MESH_ERROR_IO_ERROR)
53         CASE_TO_STR(MESH_ERROR_NOT_SUPPORTED)
54         CASE_TO_STR(MESH_ERROR_OPERATION_FAILED)
55         CASE_TO_STR(MESH_ERROR_OPERATION_ABORTED)
56         default :
57                 return "Unknown Error";
58         }
59 }
60
61 static const char* _mesh_event_to_string(mesh_event_e e)
62 {
63         switch (e) {
64         /* CHECK: List all enum values here */
65         CASE_TO_STR(MESH_MESH_ENABLED_EVENT)
66         CASE_TO_STR(MESH_SCAN_DONE_EVENT)
67         CASE_TO_STR(MESH_JOIN_NETWORK_EVENT)
68         CASE_TO_STR(MESH_LEFT_NETWORK_EVENT)
69         CASE_TO_STR(MESH_STATION_JOIN_EVENT)
70         CASE_TO_STR(MESH_STATION_LEFT_EVENT)
71         CASE_TO_STR(MESH_CONNECTED_EVENT)
72         default :
73                 return "MESH_EVENT_UNKNOWN";
74         }
75 }
76
77 void event_cb(mesh_event_e event_type, mesh_event_data_s* event)
78 {
79         msg("");
80         msgp("Event received [%s]", _mesh_event_to_string(event_type));
81
82         switch(event_type) {
83                 case MESH_MESH_ENABLED_EVENT: {
84                         msgp("  Mesh Network Enabled Result = %d", event->data.mesh_enable->result);
85                 } break;
86                 case MESH_SCAN_DONE_EVENT: {
87                         msgp("  Mesh Scan Done");
88                 } break;
89                 case MESH_JOIN_NETWORK_EVENT:{
90                         msgp("  Joined Network");
91                 } break;
92                 case MESH_LEFT_NETWORK_EVENT: {
93                         msgp("  Left Current Network");
94                 } break;
95                 case MESH_STATION_JOIN_EVENT: {
96                         msgp("  New Station Joined = %s", event->data.sta_info->bssid);
97                 } break;
98                 case MESH_STATION_LEFT_EVENT: {
99                         msgp("  A Station Left = %s", event->data.sta_info->bssid);
100                 } break;
101                 case MESH_CONNECTED_EVENT: {
102                         msgp("  Connection sequence finished [%s]",
103                                 mesh_error_to_string(event->data.connected_result->result));
104                 } break;
105         }
106 }
107
108 static int __init_func(MManager *mm, struct menu_data *menu)
109 {
110 #if 0
111         int ret = -1;
112
113         ret = mesh_initialize(&mesh);
114         if (ret != 0) {
115                 msg("Failed to initialize mesh: [%s(0x%X)]",
116                                 mesh_error_to_string(ret), ret);
117                 return RET_FAILURE;
118         }
119 #endif
120         return RET_SUCCESS;
121 }
122
123 static struct menu_data menu_main[] = {
124         { "1", "Device", menu_mesh_device, NULL, NULL },
125         { "2", "Mesh network", menu_mesh_network, NULL, NULL },
126         { NULL, NULL, },
127 };
128
129 static gboolean __create_init_menu(struct menu_data init_menu[1])
130 {
131         init_menu[0].key = "1";
132         init_menu[0].title = "Init";
133         init_menu[0].sub_menu = menu_main;
134         init_menu[0].callback = __init_func;
135         init_menu[0].data = NULL;
136
137         return TRUE;
138 }
139
140 int main(int arg, char **argv)
141 {
142         GMainLoop *mainloop = NULL;
143         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
144         MManager *manager;
145         struct menu_data init_menu[1+1] = { {NULL, NULL, } };
146
147 #if !GLIB_CHECK_VERSION(2, 35, 0)
148         g_type_init();
149 #endif
150         mainloop = g_main_loop_new(NULL, FALSE);
151
152         msg("");
153         msg("* Mesh Test application ");
154         msg("* Build On: %s  %s", __DATE__, __TIME__);
155
156         if (__create_init_menu(init_menu) == FALSE)
157                 goto OUT;
158
159         manager = menu_manager_new(init_menu, mainloop);
160         if (!manager)
161                 goto OUT;
162
163         menu_manager_run(manager);
164
165         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
166                                 on_menu_manager_keyboard, manager);
167         g_main_loop_run(mainloop);
168
169 OUT:
170         if (g_found_network_list)
171                 g_list_free(g_found_network_list);
172
173         if (mesh)
174                 mesh_deinitialize(mesh);
175
176         g_main_loop_unref(mainloop);
177         msg("******* Bye bye *******");
178
179         return 0;
180 }