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 gboolean handle_tdls_connect(Wifi *wifi, GDBusMethodInvocation *context,
129 gchar *peer_mac_Addr)
131 DBG("[TizenMW-->WPAS]: TDLS Setup Request: [%s]", peer_mac_Addr);
134 ERR(" Already TDLS Connection !!!");
136 GVariant *message = NULL;
137 message = __netconfig_wifi_tdls_send_dbus_str("TDLSSetup", (const char*)peer_mac_Addr);
139 if (message == NULL) {
140 ERR(" TDLS : failed to connect !!!");
141 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsConnect");
145 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
146 g_variant_unref(message);
150 wifi_complete_tdls_connect(wifi, context, 1);
154 gboolean handle_tdls_discover(Wifi *wifi, GDBusMethodInvocation *context,
155 gchar *peer_mac_Addr)
157 DBG("TDLS Discover Request: [%s]", peer_mac_Addr);
158 int discover_timeout = 0;
160 GVariant *message = NULL;
161 message = __netconfig_wifi_tdls_send_dbus_str("TDLSDiscover", (const char*)peer_mac_Addr);
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);
170 DBG(" TDLS DBUS Command sent successfully");
171 g_variant_unref(message);
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;
178 is_discover_broadcast = 0;
179 discover_timeout = TDLS_DISCOVER_TIMOUT;
182 tdls_timer_id = g_timeout_add_seconds(discover_timeout,
183 _tdls_timer_discover_event, NULL);
185 wifi_complete_tdls_discover(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
189 gboolean handle_tdls_disconnect(Wifi *wifi, GDBusMethodInvocation *context,
190 gchar *peer_mac_Addr)
192 DBG("[TizenMW-->WPAS]: TDLS Teardown Request: [%s]", peer_mac_Addr);
195 ERR(" Already TDLS disconnection !!!");
196 wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_ALREADY_DONE);
198 GVariant *message = NULL;
199 message = __netconfig_wifi_tdls_send_dbus_str("TDLSTeardown", (const char*)peer_mac_Addr);
201 if (message == NULL) {
202 ERR(" TDLS : failed to disconnect !!!");
203 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailTdlsDisconnect");
207 DBG("[TizenMW<--WPAS] TDLS DBUS Command sent successfully");
208 g_variant_unref(message);
212 wifi_complete_tdls_disconnect(wifi, context, NETCONFIG_ERROR_TDLS_NO_ERROR);
216 gboolean handle_tdls_connected_peer(Wifi *wifi, GDBusMethodInvocation *context)
218 DBG("[TizenMW-->WPAS]: TDLS Connected Peer Request: ");
220 GVariant *message = NULL;
221 const gchar* reply_str = NULL;
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");
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");
235 g_variant_get(message, "(&s)", &reply_str);
236 INFO("TDLS reply: [%s]", reply_str);
237 INFO("TDLS :peer_mac [%s]", peer_mac);
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);
246 INFO("TDLS Connection available, Peer Mac address %s", peer_mac);
247 wifi_complete_tdls_connected_peer(wifi, context, peer_mac);
249 g_variant_unref(message);
253 void netconfig_wifi_tdls_connected_event(GVariant *message)
256 DBG("WiFi TDLS Connected EVENT");
257 if (is_connected == 1) {
258 INFO("TDLS Peer already connected");
262 g_variant_get(message, "(s)", &peer_mac);
263 INFO("Peer Mac Address: [%s]", peer_mac);
266 __netconfig_wifi_notify_tdls_event("TDLSConnect", peer_mac);
269 void netconfig_wifi_tdls_disconnected_event(GVariant *message)
271 DBG("WiFi TDLS Disconnected EVENT");
272 const gchar *peer_mac_addr = NULL;
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);
278 __netconfig_wifi_notify_tdls_event("TDLSDisconnect", peer_mac);
280 INFO("TDLS Peer Disconnected peer_mac(%s) != peer_mac_address(%s)", peer_mac, peer_mac_addr);
284 void netconfig_wifi_tdls_peer_found_event(GVariant *message)
286 DBG("WiFi TDLS Discovery EVENT");
287 const gchar *peer_mac_addr = NULL;
289 g_variant_get(message, "(s)", &peer_mac_addr);
290 INFO("Discover Peer Mac Address: [%s]", peer_mac_addr);
293 if (is_discover_broadcast == 0)
296 __netconfig_wifi_notify_tdls_discover_event(peer_mac_addr, is_discover_broadcast);