4 Copyright (C) 2004 Nokia Corporation. All rights reserved.
6 Copyright (C) 2009 Javier S. Pedro
8 @author Janne Ylalehto <janne.ylalehto@nokia.com>
9 @author Johan Hedberg <johan.hedberg@nokia.com>
11 @author Javier S. Pedro <javispedro@javispedro.com>
13 This file is part of libicd-network-wpa.
15 This program is free software; you can redistribute it and/or modify it
16 under the terms of the GNU General Public License as published by the
17 Free Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License along
26 with this program; if not, write to the Free Software Foundation, Inc.,
27 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <dbus/dbus.h>
37 #include <wlancond-dbus.h>
38 #include <wlancond-supp.h>
42 #include "dbus-helper.h"
48 static wlan_found_ap found_ap_cb = NULL;
50 int wlan_scan(const char * ssid, wlan_found_ap new_found_ap_cb)
52 DBusMessage *msg, *reply;
55 DLOG_DEBUG("%s: %s (active)", __func__, ssid);
57 dbus_error_init(&error);
59 found_ap_cb = new_found_ap_cb;
61 msg = new_dbus_method_call(
64 WLANCOND_REQ_INTERFACE,
67 gint32 power_level = WLANCOND_TX_POWER100;
72 DBUS_TYPE_INT32, &power_level,
73 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &ssid, strlen(ssid) + 1,
74 DBUS_TYPE_UINT32, &flags,
78 reply = dbus_connection_send_with_reply_and_block(get_dbus_connection(),
81 dbus_message_unref(msg);
84 DLOG_DEBUG("Scan reply OK");
85 dbus_message_unref(reply);
88 } else if (dbus_error_is_set(&error)) {
89 DLOG_INFO("Scan pending call result:%s", error.name);
90 dbus_error_free(&error);
94 DLOG_WARN("Scan without reply");
100 gboolean wlan_is_scanning()
102 return found_ap_cb ? TRUE : FALSE;
105 void wlan_notify_ap(const char *ssid, const char *bssid,
106 int rssi, unsigned int channel, unsigned int cap_bits)
109 found_ap_cb(SEARCH_CONTINUE, ssid, bssid, rssi / 2 - 110);
112 void wlan_notify_end_of_search()
114 wlan_found_ap prev_found_ap_cb = found_ap_cb;
117 // A new search may be started right after calling this callback
119 if (prev_found_ap_cb)
120 prev_found_ap_cb(SEARCH_FINISHED, NULL, NULL, 0);
125 static wlan_connected connected_cb;
127 static void connect_reply_cb(DBusPendingCall *pending, void *user_data)
132 DLOG_DEBUG("Connect reply callback");
134 dbus_error_init(&error);
136 reply = dbus_pending_call_steal_reply(pending);
138 if (dbus_set_error_from_message(&error, reply)) {
140 DLOG_DEBUG("Connect pending call result:%s", error.name);
142 connected_cb(-1, error.name);
143 dbus_error_free(&error);
145 connected_cb(0, NULL);
149 dbus_message_unref(reply);
150 dbus_pending_call_unref(pending);
153 void wlan_connect(const char * ssid, wlan_connected _connected_cb)
156 DBusPendingCall *pending;
158 DLOG_DEBUG("%s: %s", __func__, ssid);
160 connected_cb = _connected_cb;
162 msg = new_dbus_method_call(
165 WLANCOND_REQ_INTERFACE,
166 WLANCOND_SETTINGS_AND_CONNECT_REQ);
169 guint32 *dummyP = &dummy;
171 gint32 power_level, mode, encryption, default_key;
172 guint32 adhoc_channel, flags;
174 power_level = WLANCOND_TX_POWER100;
175 mode = WLANCOND_INFRA;
179 flags = WLANCOND_USE_SUPPLICANT;
183 DBUS_TYPE_INT32, &power_level,
184 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &ssid, strlen(ssid),
185 DBUS_TYPE_INT32, &mode,
186 DBUS_TYPE_INT32, &encryption,
187 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &dummyP, 0,
188 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &dummyP, 0,
189 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &dummyP, 0,
190 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &dummyP, 0,
191 DBUS_TYPE_INT32, &default_key,
192 DBUS_TYPE_UINT32, &adhoc_channel,
193 DBUS_TYPE_UINT32, &flags,
196 if (!dbus_connection_send_with_reply(get_dbus_connection(),
198 die("Out of memory");
200 if (!dbus_pending_call_set_notify(pending, connect_reply_cb, NULL, NULL))
201 die("Out of memory");
203 dbus_message_unref(msg);
206 static void disconnect_reply_cb(DBusPendingCall *pending, void *user_data)
211 DLOG_DEBUG("Disconnect reply callback");
213 dbus_error_init(&error);
215 reply = dbus_pending_call_steal_reply(pending);
217 if (dbus_set_error_from_message(&error, reply)) {
219 DLOG_DEBUG("Disconnect pending call result:%s", error.name);
221 dbus_error_free(&error);
224 // No need to notify, wpa_supplicant will signal that for us
227 dbus_message_unref(reply);
228 dbus_pending_call_unref(pending);
231 void wlan_disconnect()
234 DBusPendingCall *pending;
236 DLOG_DEBUG("%s", __func__);
238 msg = new_dbus_method_call(
241 WLANCOND_REQ_INTERFACE,
242 WLANCOND_DISCONNECT_REQ);
244 if (!dbus_connection_send_with_reply(get_dbus_connection(),
246 die("Out of memory");
248 if (!dbus_pending_call_set_notify(pending, disconnect_reply_cb, NULL, NULL))
249 die("Out of memory");
251 dbus_message_unref(msg);
256 static wlan_status_reply status_cb;
258 static void status_reply_cb(DBusPendingCall *pending, void *user_data)
263 DLOG_DEBUG("%s", __func__);
265 dbus_error_init(&error);
267 reply = dbus_pending_call_steal_reply(pending);
269 if (dbus_set_error_from_message(&error, reply)) {
271 DLOG_DEBUG("%s error: %s", __func__, error.name);
274 char *essid, *bssid, *ifname;
275 int essid_len, bssid_len;
276 unsigned long sens, channel, capability, security;
278 if (!dbus_message_get_args(
280 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &essid, &essid_len,
281 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &bssid, &bssid_len,
282 DBUS_TYPE_UINT32, &sens,
283 DBUS_TYPE_UINT32, &channel,
284 DBUS_TYPE_UINT32, &capability,
285 DBUS_TYPE_UINT32, &security,
286 DBUS_TYPE_STRING, &ifname,
289 DLOG_DEBUG("%s parse reply error: %s", __func__,
294 status_cb(0, essid, essid_len, bssid, bssid_len,
295 sens, channel, capability, security,
300 dbus_message_unref(reply);
301 dbus_pending_call_unref(pending);
305 status_cb(-1, NULL, 0, NULL, 0,
308 dbus_error_free(&error);
311 dbus_message_unref(reply);
312 dbus_pending_call_unref(pending);
315 void wlan_get_status(wlan_status_reply reply_cb)
318 DBusPendingCall *pending;
320 DLOG_DEBUG("%s", __func__);
322 status_cb = reply_cb;
324 msg = new_dbus_method_call(
327 WLANCOND_REQ_INTERFACE,
328 WLANCOND_STATUS_REQ);
330 if (!dbus_connection_send_with_reply(get_dbus_connection(),
332 die("Out of memory");
334 if (!dbus_pending_call_set_notify(pending, status_reply_cb, NULL, NULL))
335 die("Out of memory");
337 dbus_message_unref(msg);