cd09f21b3be050bd94839fc4c9c710146a4109dd
[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
44 #define TDLS_DISCOVER_TIMOUT 4 /*TDLS Unicast Discovery Timeout*/
45 #define TDLS_DISCOVER_BROADCAST_TIMOUT 8 /*TDLS Broadcast Discovery Timeout*/
46 static void stop_tdls_timer()
47 {
48         if (tdls_timer_id > 0) {
49                 g_source_remove(tdls_timer_id);
50                 tdls_timer_id = 0;
51         }
52 }
53
54 void __netconfig_wifi_notify_tdls_event(const char *sig_name, const char *peer_mac)
55 {
56         GVariantBuilder *builder;
57         GVariant *params;
58         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
59         g_variant_builder_add(builder, "{sv}", "peermac", g_variant_new_string(peer_mac));
60
61         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
62         g_variant_builder_unref(builder);
63
64         netconfig_dbus_emit_signal(NULL,
65                                 NETCONFIG_WIFI_PATH,
66                                 NETCONFIG_WIFI_INTERFACE,
67                                 sig_name,
68                                 params);
69
70         INFO("Sent signal (%s) Peer Mac (%s)", sig_name, peer_mac);
71 }
72
73 void __netconfig_wifi_notify_tdls_discover_event(const char *peer_mac, int discover_type)
74 {
75         GVariantBuilder *builder;
76         GVariant *params;
77         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
78
79         g_variant_builder_add(builder, "{sv}", "peermac", g_variant_new_string(peer_mac));
80         g_variant_builder_add(builder, "{sv}", "discover_type", g_variant_new_int32(discover_type));
81
82         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
83         g_variant_builder_unref(builder);
84
85         netconfig_dbus_emit_signal(NULL,
86                                 NETCONFIG_WIFI_PATH,
87                                 NETCONFIG_WIFI_INTERFACE,
88                                 "TDLSPeerFound",
89                                 params);
90
91         INFO("Sent signal (%s) Peer Mac (%s)", "TDLSPeerFound", peer_mac);
92 }
93
94 static gboolean _tdls_timer_discover_event(gpointer user_data)
95 {
96
97         if (tdls_timer_id == 0)
98                 return FALSE;
99
100         INFO("[TDLS Discover Timer Expired");
101         __netconfig_wifi_notify_tdls_discover_event("00:00:00:00:00:00", is_discover_broadcast);
102         is_discover_broadcast = 0;
103         stop_tdls_timer();
104         return FALSE;
105 }
106
107 static GVariant * __netconfig_wifi_tdls_send_dbus_str(const char* method, const char *str)
108 {
109         GVariant *message = NULL;
110         const char *if_path = NULL;
111         GVariant *params = NULL;
112
113         if_path = netconfig_wifi_get_supplicant_interface();
114         if (if_path == NULL) {
115                 ERR("Fail to get wpa_supplicant DBus path");
116                 return NULL;
117         }
118
119         params = g_variant_new("(s)", str);
120         INFO("[TizenMW-->WPAS]Sent Dbus Method :[%s],value[%s]", method, str);
121         message = netconfig_invoke_dbus_method(SUPPLICANT_SERVICE,
122                         if_path, SUPPLICANT_INTERFACE ".Interface", method, params);
123
124         INFO("TDLS Returned from Blocking method for Send DBUS Command");
125         return message;
126 }
127
128 gboolean handle_tdls_connect(Wifi *wifi, GDBusMethodInvocation *context,
129                         gchar *peer_mac_Addr)
130 {
131         DBG("[TizenMW-->WPAS]: TDLS Setup Request: [%s]", peer_mac_Addr);
132
133         if (is_connected) {
134                 ERR(" Already TDLS Connection !!!");
135         } else {
136                 GVariant *message = NULL;
137                 message = __netconfig_wifi_tdls_send_dbus_str("TDLSSetup", (const char*)peer_mac_Addr);
138
139                 if (message == NULL) {
140                         ERR(" TDLS : failed to connect !!!");
141                         netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsConnect");
142                         return FALSE;
143                 }
144
145                 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
146                 g_variant_unref(message);
147                 is_connected = 1;
148         }
149
150         wifi_complete_tdls_connect(wifi, context, 1);
151         return TRUE;
152 }
153
154 gboolean handle_tdls_discover(Wifi *wifi, GDBusMethodInvocation *context,
155                         gchar *peer_mac_Addr)
156 {
157         DBG("TDLS Discover Request: [%s]", peer_mac_Addr);
158         int discover_timeout = 0;
159
160         GVariant *message = NULL;
161         message = __netconfig_wifi_tdls_send_dbus_str("TDLSDiscover", (const char*)peer_mac_Addr);
162
163         if (message == NULL) {
164                 ERR(" TDLS : failed to discover !!!");
165                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDiscover");
166                 wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_DISCOVER);
167                 return FALSE;
168         }
169
170         DBG(" TDLS DBUS Command sent successfully");
171         g_variant_unref(message);
172
173         if (NULL != strstr(peer_mac_Addr, "ff:ff:ff:ff:ff:ff")) {
174                 DBG("TDLS: Broadcast Discovery");
175                 is_discover_broadcast = 1;
176                 discover_timeout = TDLS_DISCOVER_BROADCAST_TIMOUT;
177         } else {
178                 is_discover_broadcast = 0;
179                 discover_timeout = TDLS_DISCOVER_TIMOUT;
180         }
181
182         tdls_timer_id = g_timeout_add_seconds(discover_timeout,
183                                         _tdls_timer_discover_event, NULL);
184
185         wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
186         return TRUE;
187 }
188
189 gboolean handle_tdls_disconnect(Wifi *wifi, GDBusMethodInvocation *context,
190                         gchar *peer_mac_Addr)
191 {
192         DBG("[TizenMW-->WPAS]: TDLS Teardown Request: [%s]", peer_mac_Addr);
193
194         if (!is_connected) {
195                 ERR(" Already TDLS disconnection !!!");
196                 wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_ALREADY_DONE);
197         } else {
198                 GVariant *message = NULL;
199                 message = __netconfig_wifi_tdls_send_dbus_str("TDLSTeardown", (const char*)peer_mac_Addr);
200
201                 if (message == NULL) {
202                         ERR(" TDLS : failed to disconnect !!!");
203                         netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDisconnect");
204                         return FALSE;
205                 }
206
207                 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
208                 g_variant_unref(message);
209                 is_connected = 0;
210         }
211
212         wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
213         return TRUE;
214 }
215
216 gboolean handle_tdls_connected_peer(Wifi *wifi, GDBusMethodInvocation *context)
217 {
218         DBG("[TizenMW-->WPAS]: TDLS Connected Peer Request: ");
219
220         GVariant *message = NULL;
221         const gchar* reply_str = NULL;
222
223         if (peer_mac == NULL) {
224                 INFO("TDLS: No Active Connection");
225                 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
226                 return TRUE;
227         }
228         message = __netconfig_wifi_tdls_send_dbus_str("TDLSStatus", (const char*)peer_mac);
229         if (message == NULL) {
230                 ERR(" TDLS : No active TDLS Link Setup !!!");
231                 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
232                 return TRUE;
233         }
234
235         g_variant_get(message, "(&s)", &reply_str);
236         INFO("TDLS reply: [%s]", reply_str);
237         INFO("TDLS :peer_mac [%s]", peer_mac);
238
239         if (g_strcmp0("connected", reply_str) != 0) {
240                 ERR("[TizenMW<--WPAS] TDLS Connection not available");
241                 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
242                 g_variant_unref(message);
243                 return TRUE;
244         }
245
246         INFO("TDLS Connection available, Peer Mac address %s", peer_mac);
247         wifi_complete_tdls_connected_peer(wifi, context, peer_mac);
248
249         g_variant_unref(message);
250         return TRUE;
251 }
252
253 void netconfig_wifi_tdls_connected_event(GVariant *message)
254 {
255
256         DBG("WiFi TDLS Connected EVENT");
257         if (is_connected == 1) {
258                 INFO("TDLS Peer already connected");
259                 g_free(peer_mac);
260         }
261
262         g_variant_get(message, "(s)", &peer_mac);
263         INFO("Peer Mac Address: [%s]", peer_mac);
264
265         is_connected = 1;
266         __netconfig_wifi_notify_tdls_event("TDLSConnect", peer_mac);
267 }
268
269 void netconfig_wifi_tdls_disconnected_event(GVariant *message)
270 {
271         DBG("WiFi TDLS Disconnected EVENT");
272         const gchar *peer_mac_addr = NULL;
273
274         g_variant_get(message, "(&s)", &peer_mac_addr);
275         if (g_strcmp0(peer_mac, peer_mac_addr) == 0) {
276                 INFO("TDLS Peer Disconnected Mac Address: [%s]", peer_mac);
277                 is_connected = 0;
278                 __netconfig_wifi_notify_tdls_event("TDLSDisconnect", peer_mac);
279         } else
280                 INFO("TDLS Peer Disconnected peer_mac(%s) != peer_mac_address(%s)", peer_mac, peer_mac_addr);
281 }
282
283
284 void netconfig_wifi_tdls_peer_found_event(GVariant *message)
285 {
286         DBG("WiFi TDLS Discovery EVENT");
287         const gchar *peer_mac_addr = NULL;
288
289         g_variant_get(message, "(s)", &peer_mac_addr);
290         INFO("Discover Peer Mac Address: [%s]", peer_mac_addr);
291
292
293         if (is_discover_broadcast == 0)
294                 stop_tdls_timer();
295
296         __netconfig_wifi_notify_tdls_discover_event(peer_mac_addr, is_discover_broadcast);
297 }
298