2 * Network Configuration Module
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include "netsupplicant.h"
29 #include "network-state.h"
31 #include <vconf-keys.h>
32 #include <arpa/inet.h>
36 #include "wifi-tdls.h"
39 char *peer_mac = NULL;
41 static gint tdls_timer_id = 0;
42 int is_discover_broadcast = 0;
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()
48 if (tdls_timer_id > 0) {
49 g_source_remove(tdls_timer_id);
54 void __netconfig_wifi_notify_tdls_event(const char *sig_name, const char *peer_mac)
56 GVariantBuilder *builder;
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));
61 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
62 g_variant_builder_unref(builder);
64 netconfig_dbus_emit_signal(NULL,
66 NETCONFIG_WIFI_INTERFACE,
70 INFO("Sent signal (%s) Peer Mac (%s)", sig_name, peer_mac);
73 void __netconfig_wifi_notify_tdls_discover_event(const char *peer_mac, int discover_type)
75 GVariantBuilder *builder;
77 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
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));
82 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
83 g_variant_builder_unref(builder);
85 netconfig_dbus_emit_signal(NULL,
87 NETCONFIG_WIFI_INTERFACE,
91 INFO("Sent signal (%s) Peer Mac (%s)", "TDLSPeerFound", peer_mac);
94 static gboolean _tdls_timer_discover_event(gpointer user_data)
97 if (tdls_timer_id == 0)
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;
107 static GVariant * __netconfig_wifi_tdls_send_dbus_str(const char* method, const char *str)
109 GVariant *message = NULL;
110 const char *if_path = NULL;
111 GVariant *params = NULL;
113 if_path = netconfig_wifi_get_supplicant_interface();
114 if (if_path == NULL) {
115 ERR("Fail to get wpa_supplicant DBus path");
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);
124 INFO("TDLS Returned from Blocking method for Send DBUS Command");
128 static unsigned char _netconfig_freq_to_channel(int freq)
130 if (freq < 2412 || freq > 5825 ||
131 (freq > 2484 && freq < 5180)) {
132 ERR("Invalid Frequence Range");
136 return 36 + (freq - 5180)/5;
137 else if (freq <= 2472)
138 return 1 + (freq - 2412)/5;
139 else if (freq == 2484)
145 static unsigned char _netconfig_get_operating_class(int freq)
147 unsigned char channel = 0;
148 unsigned char oper_class = 0;
150 channel = _netconfig_freq_to_channel(freq);
153 /* Operating class 81 - 2.4 GHz band channels 1..13 */
154 if (channel >= 1 && channel <= 13)
156 /* Operating class 115 - 5 GHz, channels 36-48 */
157 else if (channel >= 36 && channel <= 48)
159 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
163 INFO("TDLS: Operating Class is [%d]", oper_class);
169 gboolean handle_tdls_connect(Wifi *wifi, GDBusMethodInvocation *context,
170 gchar *peer_mac_Addr)
172 DBG("[TizenMW-->WPAS]: TDLS Setup Request: [%s]", peer_mac_Addr);
175 ERR(" Already TDLS Connection !!!");
177 GVariant *message = NULL;
178 message = __netconfig_wifi_tdls_send_dbus_str("TDLSSetup", (const char*)peer_mac_Addr);
180 if (message == NULL) {
181 ERR(" TDLS : failed to connect !!!");
182 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsConnect");
186 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
187 g_variant_unref(message);
191 wifi_complete_tdls_connect(wifi, context, 1);
195 gboolean handle_tdls_discover(Wifi *wifi, GDBusMethodInvocation *context,
196 gchar *peer_mac_Addr)
198 DBG("TDLS Discover Request: [%s]", peer_mac_Addr);
199 int discover_timeout = 0;
201 GVariant *message = NULL;
202 message = __netconfig_wifi_tdls_send_dbus_str("TDLSDiscover", (const char*)peer_mac_Addr);
204 if (message == NULL) {
205 ERR(" TDLS : failed to discover !!!");
206 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDiscover");
207 wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_DISCOVER);
211 DBG(" TDLS DBUS Command sent successfully");
212 g_variant_unref(message);
214 if (NULL != strstr(peer_mac_Addr, "ff:ff:ff:ff:ff:ff")) {
215 DBG("TDLS: Broadcast Discovery");
216 is_discover_broadcast = 1;
217 discover_timeout = TDLS_DISCOVER_BROADCAST_TIMOUT;
219 is_discover_broadcast = 0;
220 discover_timeout = TDLS_DISCOVER_TIMOUT;
223 tdls_timer_id = g_timeout_add_seconds(discover_timeout,
224 _tdls_timer_discover_event, NULL);
226 wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
230 gboolean handle_tdls_disconnect(Wifi *wifi, GDBusMethodInvocation *context,
231 gchar *peer_mac_Addr)
233 DBG("[TizenMW-->WPAS]: TDLS Teardown Request: [%s]", peer_mac_Addr);
236 ERR(" Already TDLS disconnection !!!");
237 wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_ALREADY_DONE);
239 GVariant *message = NULL;
240 message = __netconfig_wifi_tdls_send_dbus_str("TDLSTeardown", (const char*)peer_mac_Addr);
242 if (message == NULL) {
243 ERR(" TDLS : failed to disconnect !!!");
244 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDisconnect");
248 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
249 g_variant_unref(message);
253 wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
257 gboolean handle_tdls_connected_peer(Wifi *wifi, GDBusMethodInvocation *context)
259 DBG("[TizenMW-->WPAS]: TDLS Connected Peer Request: ");
261 GVariant *message = NULL;
262 const gchar* reply_str = NULL;
264 if (peer_mac == NULL) {
265 INFO("TDLS: No Active Connection");
266 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
269 message = __netconfig_wifi_tdls_send_dbus_str("TDLSStatus", (const char*)peer_mac);
270 if (message == NULL) {
271 ERR(" TDLS : No active TDLS Link Setup !!!");
272 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
276 g_variant_get(message, "(&s)", &reply_str);
277 INFO("TDLS reply: [%s]", reply_str);
278 INFO("TDLS :peer_mac [%s]", peer_mac);
280 if (g_strcmp0("connected", reply_str) != 0) {
281 ERR("[TizenMW<--WPAS] TDLS Connection not available");
282 wifi_complete_tdls_connected_peer(wifi, context, "00.00.00.00.00.00");
283 g_variant_unref(message);
287 INFO("TDLS Connection available, Peer Mac address %s", peer_mac);
288 wifi_complete_tdls_connected_peer(wifi, context, peer_mac);
290 g_variant_unref(message);
294 gboolean handle_tdls_channel_switch(Wifi *wifi, GDBusMethodInvocation *context,
295 gchar *peer_mac_Addr, int freq)
297 GVariant *message = NULL;
298 GVariantBuilder *builder;
300 const char *if_path = NULL;
301 unsigned char oper_class = 0;
303 if (peer_mac_Addr == NULL) {
304 ERR("TDLS: Invalid Parameter");
305 wifi_complete_tdls_channel_switch(wifi, context,
306 NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
310 oper_class = _netconfig_get_operating_class(freq);
313 ERR("TDLS: Invalid Parameter");
314 wifi_complete_tdls_channel_switch(wifi, context,
315 NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
319 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
321 g_variant_builder_add(builder, "{sv}", "PeerAddress", g_variant_new_string(peer_mac_Addr));
322 g_variant_builder_add(builder, "{sv}", "Frequency", g_variant_new_int32(freq));
323 g_variant_builder_add(builder, "{sv}", "OperClass", g_variant_new_byte(oper_class));
324 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
325 g_variant_builder_unref(builder);
327 if_path = netconfig_wifi_get_supplicant_interface();
329 if (if_path == NULL) {
330 ERR("Fail to get wpa_supplicant DBus path");
331 wifi_complete_tdls_channel_switch(wifi, context,
332 NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
336 message = netconfig_invoke_dbus_method(SUPPLICANT_SERVICE,
337 if_path, SUPPLICANT_INTERFACE ".Interface", "TDLSChannelSwitch", params);
339 if (message == NULL) {
340 ERR(" TDLS : Fail to Process TDLS Channel Switch Request !!!");
341 wifi_complete_tdls_channel_switch(wifi, context,
342 NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
346 INFO("TDLS Channel Change Request: Success");
347 wifi_complete_tdls_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
349 g_variant_unref(message);
354 gboolean handle_tdls_cancel_channel_switch(Wifi *wifi, GDBusMethodInvocation *context,
355 gchar *peer_mac_Addr)
357 GVariant *message = NULL;
359 if (peer_mac_Addr == NULL) {
360 INFO("TDLS: Invalid Parameter");
361 wifi_complete_tdls_cancel_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
364 message = __netconfig_wifi_tdls_send_dbus_str("TDLSCancelChannelSwitch", (const char*)peer_mac_Addr);
365 if (message == NULL) {
366 ERR(" TDLS : Fail to TDLS Cancel Channel Swicth Request !!!");
367 wifi_complete_tdls_cancel_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_FAIL_CHANNEL_SWITCH);
371 INFO("TDLS Cancel Channel Swicth Request : Success");
372 wifi_complete_tdls_cancel_channel_switch(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
374 g_variant_unref(message);
378 void netconfig_wifi_tdls_connected_event(GVariant *message)
381 DBG("WiFi TDLS Connected EVENT");
382 if (is_connected == 1) {
383 INFO("TDLS Peer already connected");
387 g_variant_get(message, "(s)", &peer_mac);
388 INFO("Peer Mac Address: [%s]", peer_mac);
391 __netconfig_wifi_notify_tdls_event("TDLSConnect", peer_mac);
394 void netconfig_wifi_tdls_disconnected_event(GVariant *message)
396 DBG("WiFi TDLS Disconnected EVENT");
397 const gchar *peer_mac_addr = NULL;
399 g_variant_get(message, "(&s)", &peer_mac_addr);
400 if (g_strcmp0(peer_mac, peer_mac_addr) == 0) {
401 INFO("TDLS Peer Disconnected Mac Address: [%s]", peer_mac);
403 __netconfig_wifi_notify_tdls_event("TDLSDisconnect", peer_mac);
405 INFO("TDLS Peer Disconnected peer_mac(%s) != peer_mac_address(%s)", peer_mac, peer_mac_addr);
409 void netconfig_wifi_tdls_peer_found_event(GVariant *message)
411 DBG("WiFi TDLS Discovery EVENT");
412 const gchar *peer_mac_addr = NULL;
414 g_variant_get(message, "(s)", &peer_mac_addr);
415 INFO("Discover Peer Mac Address: [%s]", peer_mac_addr);
418 if (is_discover_broadcast == 0)
421 __netconfig_wifi_notify_tdls_discover_event(peer_mac_addr, is_discover_broadcast);