add new dbus method for preferred Ipv6 address
[platform/core/connectivity/net-config.git] / src / wifi-tdls.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <time.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include "neterror.h"
27 #include "netdbus.h"
28 #include "netsupplicant.h"
29 #include "network-state.h"
30 #include <vconf.h>
31 #include <vconf-keys.h>
32 #include <arpa/inet.h>
33 #include <log.h>
34 #include "util.h"
35 #include "neterror.h"
36 #include "wifi-tdls.h"
37 #include <glib.h>
38
39 char *peer_mac = NULL;
40 int is_connected = 0;
41 static gint tdls_timer_id = 0;
42 int is_discover_broadcast = 0;
43 int is_timer_expired = 0;
44
45 #define TDLS_DISCOVER_TIMOUT 4 /*TDLS Unicast Discovery Timeout*/
46 #define TDLS_DISCOVER_BROADCAST_TIMOUT 8 /*TDLS Broadcast Discovery Timeout*/
47 static void stop_tdls_timer()
48 {
49         if (tdls_timer_id > 0) {
50                 g_source_remove(tdls_timer_id);
51                 tdls_timer_id = 0;
52         }
53 }
54
55 void __netconfig_wifi_notify_tdls_event(const char *sig_name, const char *peer_mac)
56 {
57         GVariantBuilder *builder;
58         GVariant *params;
59         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
60         g_variant_builder_add(builder, "{sv}", "peermac", g_variant_new_string(peer_mac));
61
62         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
63         g_variant_builder_unref(builder);
64
65         netconfig_dbus_emit_signal(NULL,
66                                 NETCONFIG_WIFI_PATH,
67                                 NETCONFIG_WIFI_INTERFACE,
68                                 sig_name,
69                                 params);
70
71         INFO("Sent signal (%s) Peer Mac (%s)", sig_name, peer_mac);
72 }
73
74 void __netconfig_wifi_notify_tdls_discover_event(const char *peer_mac, int discover_type)
75 {
76         GVariantBuilder *builder;
77         GVariant *params;
78         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
79
80         g_variant_builder_add(builder, "{sv}", "peermac", g_variant_new_string(peer_mac));
81         g_variant_builder_add(builder, "{sv}", "discover_type", g_variant_new_int32(discover_type));
82
83         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
84         g_variant_builder_unref(builder);
85
86         netconfig_dbus_emit_signal(NULL,
87                                 NETCONFIG_WIFI_PATH,
88                                 NETCONFIG_WIFI_INTERFACE,
89                                 "TDLSPeerFound",
90                                 params);
91
92         INFO("Sent signal (%s) Peer Mac (%s)", "TDLSPeerFound", peer_mac);
93 }
94
95 static gboolean _tdls_timer_discover_event(gpointer user_data)
96 {
97
98         if (tdls_timer_id == 0)
99                 return FALSE;
100
101         INFO("[TDLS Discover Timer Expired");
102         __netconfig_wifi_notify_tdls_discover_event("00:00:00:00:00:00", is_discover_broadcast);
103         is_discover_broadcast = 0;
104         stop_tdls_timer();
105         is_timer_expired = 1;
106
107         return FALSE;
108 }
109
110 static GVariant * __netconfig_wifi_tdls_send_dbus_str(const char* method, const char *str)
111 {
112         GVariant *message = NULL;
113         const char *if_path = NULL;
114         GVariant *params = NULL;
115
116         if_path = netconfig_wifi_get_supplicant_interface();
117         if (if_path == NULL) {
118                 ERR("Fail to get wpa_supplicant DBus path");
119                 return NULL;
120         }
121
122         params = g_variant_new("(s)", str);
123         INFO("[TizenMW-->WPAS]Sent Dbus Method :[%s],value[%s]", method, str);
124         message = netconfig_invoke_dbus_method(SUPPLICANT_SERVICE,
125                         if_path, SUPPLICANT_INTERFACE ".Interface", method, params);
126
127         INFO("TDLS Returned from Blocking method for Send DBUS Command");
128         return message;
129 }
130
131 static unsigned char _netconfig_freq_to_channel(int freq)
132 {
133         if (freq < 2412 || freq > 5825 ||
134                 (freq > 2484 && freq < 5180)) {
135                 ERR("Invalid Frequence Range");
136                 return 0;
137         }
138         if (freq >= 5180)
139                 return 36 + (freq - 5180)/5;
140         else if (freq <= 2472)
141                 return 1 + (freq - 2412)/5;
142         else if (freq == 2484)
143                 return 14;
144         else
145                 return 0;
146 }
147
148 static unsigned char _netconfig_get_operating_class(int freq)
149 {
150         unsigned char channel = 0;
151         unsigned char oper_class = 0;
152
153         channel = _netconfig_freq_to_channel(freq);
154
155         if (channel) {
156                 /* Operating class 81 - 2.4 GHz band channels 1..13 */
157                 if (channel >= 1 && channel <= 13)
158                         oper_class = 81;
159                 /* Operating class 115 - 5 GHz, channels 36-48 */
160                 else if (channel >= 36 && channel <= 48)
161                         oper_class = 115;
162                 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
163                 else
164                         oper_class = 124;
165
166                 INFO("TDLS: Operating Class  is [%d]", oper_class);
167                 return oper_class;
168         }
169         return 0;
170 }
171
172 gboolean handle_tdls_connect(Wifi *wifi, GDBusMethodInvocation *context,
173                         gchar *peer_mac_Addr)
174 {
175         DBG("[TizenMW-->WPAS]: TDLS Setup Request: [%s]", peer_mac_Addr);
176
177         if (is_connected) {
178                 ERR(" Already TDLS Connection !!!");
179         } else {
180                 GVariant *message = NULL;
181                 message = __netconfig_wifi_tdls_send_dbus_str("TDLSSetup", (const char*)peer_mac_Addr);
182
183                 if (message == NULL) {
184                         ERR(" TDLS : failed to connect !!!");
185                         netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsConnect");
186                         return FALSE;
187                 }
188
189                 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
190                 g_variant_unref(message);
191                 is_connected = 1;
192         }
193
194         wifi_complete_tdls_connect(wifi, context, 1);
195         return TRUE;
196 }
197
198 gboolean handle_tdls_discover(Wifi *wifi, GDBusMethodInvocation *context,
199                         gchar *peer_mac_Addr)
200 {
201         DBG("TDLS Discover Request: [%s]", peer_mac_Addr);
202         int discover_timeout = 0;
203
204         GVariant *message = NULL;
205
206         if (tdls_timer_id > 0) {
207                 DBG(" TDLS Discover is already progress !!!");
208                 wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_ALREADY_DONE);
209                 return TRUE;
210         }
211
212         message = __netconfig_wifi_tdls_send_dbus_str("TDLSDiscover", (const char*)peer_mac_Addr);
213
214         if (message == NULL) {
215                 ERR(" TDLS : failed to discover !!!");
216                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDiscover");
217                 wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_DISCOVER);
218                 return FALSE;
219         }
220
221         DBG(" TDLS DBUS Command sent successfully");
222         g_variant_unref(message);
223
224         if (NULL != strstr(peer_mac_Addr, "ff:ff:ff:ff:ff:ff")) {
225                 DBG("TDLS: Broadcast Discovery");
226                 is_discover_broadcast = 1;
227                 discover_timeout = TDLS_DISCOVER_BROADCAST_TIMOUT;
228         } else {
229                 is_discover_broadcast = 0;
230                 discover_timeout = TDLS_DISCOVER_TIMOUT;
231         }
232
233         is_timer_expired = 0;
234         tdls_timer_id = g_timeout_add_seconds(discover_timeout,
235                                         _tdls_timer_discover_event, NULL);
236
237         wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
238         return TRUE;
239 }
240
241 gboolean handle_tdls_disconnect(Wifi *wifi, GDBusMethodInvocation *context,
242                         gchar *peer_mac_Addr)
243 {
244         DBG("[TizenMW-->WPAS]: TDLS Teardown Request: [%s]", peer_mac_Addr);
245
246         if (!is_connected) {
247                 ERR(" Already TDLS disconnection !!!");
248                 wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_ALREADY_DONE);
249         } else {
250                 GVariant *message = NULL;
251                 message = __netconfig_wifi_tdls_send_dbus_str("TDLSTeardown", (const char*)peer_mac_Addr);
252
253                 if (message == NULL) {
254                         ERR(" TDLS : failed to disconnect !!!");
255                         netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDisconnect");
256                         return FALSE;
257                 }
258
259                 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
260                 g_variant_unref(message);
261                 is_connected = 0;
262         }
263
264         wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
265         return TRUE;
266 }
267
268 gboolean handle_tdls_connected_peer(Wifi *wifi, GDBusMethodInvocation *context)
269 {
270         DBG("[TizenMW-->WPAS]: TDLS Connected Peer Request: ");
271
272         GVariant *message = NULL;
273         const gchar* reply_str = NULL;
274
275         if (peer_mac == NULL) {
276                 INFO("TDLS: No Active Connection");
277                 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
278                 return TRUE;
279         }
280         message = __netconfig_wifi_tdls_send_dbus_str("TDLSStatus", (const char*)peer_mac);
281         if (message == NULL) {
282                 ERR(" TDLS : No active TDLS Link Setup !!!");
283                 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
284                 return TRUE;
285         }
286
287         g_variant_get(message, "(&s)", &reply_str);
288         INFO("TDLS reply: [%s]", reply_str);
289         INFO("TDLS :peer_mac [%s]", peer_mac);
290
291         if (g_strcmp0("connected", reply_str) != 0) {
292                 ERR("[TizenMW<--WPAS] TDLS Connection not available");
293                 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
294                 g_variant_unref(message);
295                 return TRUE;
296         }
297
298         INFO("TDLS Connection available, Peer Mac address %s", peer_mac);
299         wifi_complete_tdls_connected_peer(wifi, context, peer_mac);
300
301         g_variant_unref(message);
302         return TRUE;
303 }
304
305 gboolean handle_tdls_channel_switch(Wifi *wifi, GDBusMethodInvocation *context,
306                         gchar *peer_mac_Addr, int freq)
307 {
308         GVariant *message = NULL;
309         GVariantBuilder *builder;
310         GVariant *params;
311         const char *if_path = NULL;
312         unsigned char oper_class = 0;
313
314         if (peer_mac_Addr == NULL) {
315                 ERR("TDLS: Invalid Parameter");
316                 wifi_complete_tdls_channel_switch(wifi, context,
317                                                         NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
318                 return TRUE;
319         }
320
321         oper_class = _netconfig_get_operating_class(freq);
322
323         if (!oper_class) {
324                 ERR("TDLS: Invalid Parameter");
325                 wifi_complete_tdls_channel_switch(wifi, context,
326                                                         NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
327                 return TRUE;
328         }
329
330         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
331
332         g_variant_builder_add(builder, "{sv}", "PeerAddress", g_variant_new_string(peer_mac_Addr));
333         g_variant_builder_add(builder, "{sv}", "Frequency", g_variant_new_uint32(freq));
334         g_variant_builder_add(builder, "{sv}", "OperClass", g_variant_new_byte(oper_class));
335         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
336         g_variant_builder_unref(builder);
337
338         if_path = netconfig_wifi_get_supplicant_interface();
339
340         if (if_path == NULL) {
341                 ERR("Fail to get wpa_supplicant DBus path");
342                 wifi_complete_tdls_channel_switch(wifi, context,
343                                                 NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
344                 return TRUE;
345         }
346
347         message = netconfig_invoke_dbus_method(SUPPLICANT_SERVICE,
348                                 if_path, SUPPLICANT_INTERFACE ".Interface", "TDLSChannelSwitch", params);
349
350         if (message == NULL) {
351                 ERR(" TDLS : Fail to Process TDLS Channel Switch Request !!!");
352                 wifi_complete_tdls_channel_switch(wifi, context,
353                                                 NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
354                 return TRUE;
355         }
356
357         INFO("TDLS Channel Change Request: Success");
358         wifi_complete_tdls_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
359
360         g_variant_unref(message);
361         return TRUE;
362 }
363
364
365 gboolean handle_tdls_cancel_channel_switch(Wifi *wifi, GDBusMethodInvocation *context,
366                         gchar *peer_mac_Addr)
367 {
368         GVariant *message = NULL;
369
370         if (peer_mac_Addr == NULL) {
371                 INFO("TDLS: Invalid Parameter");
372                 wifi_complete_tdls_cancel_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
373                 return TRUE;
374         }
375         message = __netconfig_wifi_tdls_send_dbus_str("TDLSCancelChannelSwitch", (const char*)peer_mac_Addr);
376         if (message == NULL) {
377                 ERR(" TDLS : Fail to TDLS Cancel Channel Swicth Request !!!");
378                 wifi_complete_tdls_cancel_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
379                 return TRUE;
380         }
381
382         INFO("TDLS Cancel Channel Swicth Request : Success");
383         wifi_complete_tdls_cancel_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
384
385         g_variant_unref(message);
386         return TRUE;
387 }
388
389 void netconfig_wifi_tdls_connected_event(GVariant *message)
390 {
391
392         DBG("WiFi TDLS Connected EVENT");
393         if (is_connected == 1) {
394                 INFO("TDLS Peer already connected");
395                 g_free(peer_mac);
396         }
397
398         g_variant_get(message, "(s)", &peer_mac);
399         INFO("Peer Mac Address: [%s]", peer_mac);
400
401         is_connected = 1;
402         __netconfig_wifi_notify_tdls_event("TDLSConnect", peer_mac);
403 }
404
405 void netconfig_wifi_tdls_disconnected_event(GVariant *message)
406 {
407         DBG("WiFi TDLS Disconnected EVENT");
408         const gchar *peer_mac_addr = NULL;
409
410         g_variant_get(message, "(&s)", &peer_mac_addr);
411         if (g_strcmp0(peer_mac, peer_mac_addr) == 0) {
412                 INFO("TDLS Peer Disconnected Mac Address: [%s]", peer_mac);
413                 is_connected = 0;
414                 __netconfig_wifi_notify_tdls_event("TDLSDisconnect", peer_mac);
415         } else
416                 INFO("TDLS Peer Disconnected peer_mac(%s) != peer_mac_address(%s)", peer_mac, peer_mac_addr);
417 }
418
419
420 void netconfig_wifi_tdls_peer_found_event(GVariant *message)
421 {
422         DBG("WiFi TDLS Discovery EVENT Received !!");
423         const gchar *peer_mac_addr = NULL;
424
425         if (!is_timer_expired) {
426
427                 g_variant_get(message, "(s)", &peer_mac_addr);
428                 INFO("Discover Peer Mac Address: [%s]", peer_mac_addr);
429
430                 if (is_discover_broadcast == 0)
431                         stop_tdls_timer();
432
433                 __netconfig_wifi_notify_tdls_discover_event(peer_mac_addr, is_discover_broadcast);
434         } else
435                 DBG("Timer expired: Do not process the TDLS Discovery Event");
436 }
437