Net utils
[apps/native/gear-racing-car.git] / src / net-util.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include "net-util.h"
18 #include <wifi-manager.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include "log.h"
22
23 typedef struct net_util_ {
24     wifi_manager_h wifi;
25     connection_h conn;
26     connection_type_e net_state;
27     char *ap_mac;
28     char *ap_ssid;
29     char *ip_addr;
30     wifi_connection_changed_cb cb;
31     void *cb_user_data;
32 } net_util_t;
33
34 static net_util_t net_util;
35
36 static int _set_ap_mac();
37 static int _set_ap_ssid();
38 static int _set_ip_addr();
39 static void _connection_changed_cb(connection_type_e type, void *user_data);
40
41 int net_util_init()
42 {
43     int ret = 0;
44     ret = connection_create(&net_util.conn);
45     if (ret != CONNECTION_ERROR_NONE) {
46         _E("connection_create() failed, error: %s", get_error_message(ret));
47         return -1;
48     }
49
50     ret = connection_get_type(net_util.conn, &net_util.net_state);
51     if (ret != CONNECTION_ERROR_NONE) {
52         _E("connection_get_type() failed, error: %s", get_error_message(ret));
53         goto ERROR_1;
54     }
55
56     ret = wifi_manager_initialize(&net_util.wifi);
57     if (ret != WIFI_MANAGER_ERROR_NONE) {
58         _E("wifi_manager_initialize() failed, error: %s", get_error_message(ret));
59         goto ERROR_1;
60     }
61
62     ret = connection_set_type_changed_cb(net_util.conn, _connection_changed_cb, NULL);
63     if (ret != CONNECTION_ERROR_NONE) {
64         _E("connection_set_type_changed_cb() failed, error: %s", get_error_message(ret));
65         goto ERROR_2;
66     }
67
68     _set_ap_mac();
69     _set_ap_ssid();
70     _set_ip_addr();
71
72     return 0;
73
74     ERROR_2:
75     ret = wifi_manager_deinitialize(&net_util.wifi);
76     if (ret != WIFI_MANAGER_ERROR_NONE) {
77         _E("wifi_manager_deinitialize() failed, error: %s", get_error_message(ret));
78     }
79
80     ERROR_1:
81     ret = connection_destroy(&net_util.conn);
82     if (ret != CONNECTION_ERROR_NONE) {
83         _E("wifi_manager_deinitialize() failed, error: %s", get_error_message(ret));
84     }
85     return -1;
86 }
87
88 void net_util_fini()
89 {
90     net_util.net_state = CONNECTION_TYPE_DISCONNECTED;
91
92     int ret = 0;
93     ret = connection_destroy(net_util.conn);
94     if (ret != CONNECTION_ERROR_NONE) {
95         _E("connection_set_type_changed_cb() failed, error: %s", get_error_message(ret));
96     }
97
98     ret = wifi_manager_deinitialize(&net_util.wifi);
99     if (ret != WIFI_MANAGER_ERROR_NONE) {
100         _E("wifi_manager_deinitialize() failed, error: %s", get_error_message(ret));
101     }
102
103     free(net_util.ap_mac);
104     free(net_util.ap_ssid);
105     free(net_util.ip_addr);
106 }
107
108 int net_util_set_wifi_connection_changed_cb(wifi_connection_changed_cb callback, void *user_data)
109 {
110     if (net_util.cb)
111     {
112         _E("Callback already set");
113         return -1;
114     }
115
116     net_util.cb = callback;
117     net_util.cb_user_data = user_data;
118     return 0;
119 }
120
121 int net_util_get_ap_mac(char **mac)
122 {
123     retv_if(!mac, -1);
124
125     if (!net_util.ap_mac && _set_ap_mac() != 0) {
126         return -1;
127     }
128
129     *mac = strdup(net_util.ap_mac);
130     return 0;
131 }
132
133 int net_util_get_ap_ssid(char **ssid)
134 {
135     retv_if(!ssid, -1);
136
137     if (!net_util.ap_ssid && _set_ap_ssid() != 0) {
138         return -1;
139     }
140
141     *ssid = strdup(net_util.ap_ssid);
142     return 0;
143 }
144
145 int net_util_get_ip_addr(char **ip)
146 {
147     retv_if(!ip, -1);
148
149     if (!net_util.ip_addr && _set_ip_addr() != 0) {
150         return -1;
151     }
152
153     *ip = strdup(net_util.ip_addr);
154     return 0;
155 }
156
157 static int _set_ap_mac()
158 {
159     wifi_manager_ap_h ap_h = NULL;
160     int ret = WIFI_MANAGER_ERROR_NONE;
161
162     free(net_util.ap_mac);
163     net_util.ap_mac = NULL;
164
165     if (net_util.net_state != CONNECTION_TYPE_WIFI) {
166         return -1;
167     }
168
169     ret = wifi_manager_get_connected_ap(net_util.wifi, &ap_h);
170     if (ret != WIFI_MANAGER_ERROR_NONE) {
171         _E("wifi_manager_get_connected_ap() failed, error: %s", get_error_message(ret));
172         return -1;
173     }
174
175     int r_code = 0;
176     ret = wifi_manager_ap_get_bssid(ap_h, &net_util.ap_mac);
177     if (ret != WIFI_MANAGER_ERROR_NONE) {
178         _E("wifi_manager_ap_get_bssid() failed, error: %s", get_error_message(ret));
179         r_code = -1;
180     }
181
182     ret = wifi_manager_ap_destroy(ap_h);
183     if (ret != WIFI_MANAGER_ERROR_NONE) {
184         _E("wifi_manager_ap_destroy() failed, error: %s", get_error_message(ret));
185     }
186     return r_code;
187 }
188
189 static int _set_ap_ssid()
190 {
191     wifi_manager_ap_h ap_h = NULL;
192     int ssid_len;
193     int ret = WIFI_MANAGER_ERROR_NONE;
194
195     free(net_util.ap_ssid);
196     net_util.ap_ssid = NULL;
197
198     if (net_util.net_state != CONNECTION_TYPE_WIFI) {
199         return -1;
200     }
201
202     ret = wifi_manager_get_connected_ap(net_util.wifi, &ap_h);
203     if (ret != WIFI_MANAGER_ERROR_NONE) {
204         _E("wifi_manager_get_connected_ap() failed, error: %s", get_error_message(ret));
205         return -1;
206     }
207
208     int r_code = 0;
209     ret = wifi_manager_ap_get_raw_ssid(ap_h, &net_util.ap_ssid, &ssid_len);
210     if (ret != WIFI_MANAGER_ERROR_NONE) {
211         _E("wifi_manager_ap_get_raw_ssid() failed, error: %s", get_error_message(ret));
212         r_code = -1;
213     }
214
215     ret = wifi_manager_ap_destroy(ap_h);
216     if (ret != WIFI_MANAGER_ERROR_NONE) {
217         _E("wifi_manager_ap_destroy() failed, error: %s", get_error_message(ret));
218     }
219     return r_code;
220 }
221
222 static int _set_ip_addr()
223 {
224     int ret = 0;
225
226     free(net_util.ip_addr);
227     net_util.ap_ssid = NULL;
228
229     if (net_util.net_state == CONNECTION_TYPE_DISCONNECTED) {
230         return -1;
231     }
232
233     ret = connection_get_ip_address(net_util.conn, CONNECTION_ADDRESS_FAMILY_IPV4, &net_util.ip_addr);
234     if (CONNECTION_ERROR_NONE != ret) {
235         _E("connection_get_ip_address() failed, error: %s", get_error_message(ret));
236         return -1;
237     }
238
239     return 0;
240 }
241
242 static void _connection_changed_cb(connection_type_e type, void *user_data)
243 {
244     net_util.net_state = type;
245     _set_ap_mac();
246     _set_ap_ssid();
247     _set_ip_addr();
248
249     if (type == CONNECTION_TYPE_WIFI && net_util.cb) {
250         net_util.cb(net_util.ap_mac, net_util.ap_ssid, net_util.ip_addr, net_util.cb_user_data);
251     }
252 }