wpa-supplicant applicant: Scan Method

实现

代码过程:

  • 注册信号回调函数
  • 创建gdbus连接
  • 创建gdbus事件循环
  • 获取wlan0的对象路径(这一步比较关键,后面Scan方法需要wlan0的对象路径作为参数传入)
  • 分别订阅ScanDone 信号、BSSAdded 信号、BSSRemoved 信号、PropertiesChanged 信号
  • 调用Scan方法
  • 启动gdbus事件循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
gcc src/main.c -o wpa-ctl \
`pkg-config --cflags --libs gio-2.0`\
`pkg-config --cflags --libs glib-2.0`\
`pkg-config --cflags --libs dbus-1`
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

#include <glib.h>
#include <gio/gio.h>
#include <glib/gprintf.h>

GMainLoop *g_loop;
GDBusConnection *g_gdbus_con;

guint g_prop_changed;
guint g_scan_done;
guint g_bss_removed;
guint g_bss_added;

static void wpa_property_value(const gchar *key, GVariant *value)
{
const gchar *type = g_variant_get_type_string(value);
// g_printf("type: %s\n", type);
g_printf("\t%s(%s) : ", key, type);
switch(*type)
{
case 'o': // object_path
g_printf("%s\n", g_variant_get_string(value, NULL));
break;
case 's': // string
g_printf("%s\n", g_variant_get_string(value, NULL));
break;
case 'b': // boolean
g_printf("%s\n", g_variant_get_boolean(value)? "true":"false");
break;
case 'y':
g_printf("%#x\n", g_variant_get_byte(value));
break;
case 'u': // uint32
g_printf("%d\n", g_variant_get_uint32(value));
break;
case 'n': // int16
g_printf("%hd\n", g_variant_get_int16(value));
break;
case 'q': // uint16
g_printf("%u\n", g_variant_get_uint16(value));
break;
case 'i': // int32
g_printf("%d\n", g_variant_get_int32(value));
break;
case 'a': // array,[]
/* TODO Handling only 'as', but not array of dicts */
if(!g_strcmp0(type, "as"))
{
g_printf("\n");
gchar *uuid;
GVariantIter i;
g_variant_iter_init(&i, value);
while(g_variant_iter_next(&i, "s", &uuid))
{
g_printf("\t\t%s\n", uuid);
g_free(uuid);
}
}
else if (!g_strcmp0(type, "ay"))
{
guint8 byte;
GVariantIter i;
g_variant_iter_init(&i, value);
gsize size = g_variant_iter_n_children(&i);
if (!g_strcmp0("SSID", key))
{
while(g_variant_iter_next(&i, "y", &byte))
g_printf("%c", byte);
}
else if (!g_strcmp0("BSSID", key))
{
guint8 cnt = 0;
while(g_variant_iter_next(&i, "y", &byte))
{
if (cnt == size -1)
g_printf("%02X", byte);
else
g_printf("%02X-", byte);
cnt++;
}
}
else
{
while(g_variant_iter_next(&i, "y", &byte))
g_printf("0x%x ", byte);
}
g_printf("\n");
}
else if (!g_strcmp0(type, "ao"))
{
g_printf("\n");
gchar *opath;
GVariantIter i;
g_variant_iter_init(&i, value);
while(g_variant_iter_next(&i, "o", &opath))
{
g_printf("\t\t%s\n", opath);
g_free(opath);
}
}
else if (!g_strcmp0(type, "au"))
{
guint32 rate = 0;
GVariantIter i;
g_variant_iter_init(&i, value);
while(g_variant_iter_next(&i, "u", &rate))
g_printf("%u ", rate);
g_printf("\n");
}
else if (!g_strcmp0(type, "a{sv}"))
{
g_printf("\n");
GVariantIter iter;
GVariant *prop_val;
gchar *prop_name;
g_variant_iter_init(&iter, value);
while (g_variant_iter_next (&iter, "{sv}", &prop_name, &prop_val))
{
g_printf("\t\t%s: ", prop_name);
if (g_variant_is_of_type(prop_val, G_VARIANT_TYPE_STRING))
{
g_printf("%s\n", g_variant_get_string(prop_val, NULL));
}
else if (g_variant_is_of_type(prop_val, G_VARIANT_TYPE_STRING_ARRAY))
{
gchar *str;
GVariantIter i;
g_variant_iter_init(&i, prop_val);
while (g_variant_iter_next(&i, "s", &str))
{
g_printf("%s ", str);
g_free(str);
}
g_printf("\n");
}
g_free(prop_name);
g_variant_unref(prop_val);
}
}
else
{
g_printf("\n");
}
break;
default:
g_printf("Other\n");
break;
}
}

static void wpa_signal_scan_done_cb(GDBusConnection *sig,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
// parameters data type: (b)
(void)sig;
(void)sender_name;
(void)object_path;
(void)interface;
(void)signal_name;
(void)user_data;

const gchar *type = g_variant_get_type_string(parameters);
g_printf("ScanDone signal data type: %s\n", type);
g_printf("%s\n", g_variant_print(parameters, true));

gboolean val;
g_variant_get(parameters, "(b)", &val);
g_printf("ScanDone: %s\n", val ? "true":"false");
g_printf("\n");

return;
}

static void wpa_signal_bss_added_cb(GDBusConnection *sig,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
// parameters data type: (oa{sv})
(void)sig;
(void)sender_name;
(void)object_path;
(void)interface;
(void)signal_name;
(void)user_data;

GVariantIter *iter;
const char *object;
gchar *interface_name;
GVariant *properties;

const gchar *type = g_variant_get_type_string(parameters);
g_printf("BssAdded data type: %s\n", type);
g_printf("%s\n", g_variant_print(parameters, true));

g_variant_get(parameters, "(oa{sv})", &object, &iter);
g_printf("object_path: %s\n", object);
while (g_variant_iter_next(iter, "{sv}", &interface_name, &properties))
{
wpa_property_value(interface_name, properties);
g_free(interface_name);
g_variant_unref(properties);
}

g_printf("\n");
g_variant_iter_free(iter);

return;
}

static void wpa_signal_bss_removed_cb(GDBusConnection *sig,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
// parameters data type: (o)
(void)sig;
(void)sender_name;
(void)object_path;
(void)interface;
(void)signal_name;
(void)user_data;

const gchar *type = g_variant_get_type_string(parameters);
g_printf("BssRemoved data type: %s\n", type);
g_printf("%s\n", g_variant_print(parameters, true));

const gchar *val = NULL;
g_variant_get(parameters, "(o)", &val);
g_printf("BssRemoved: %s\n", val);
g_printf("\n");

return;
}

static void wpa_signal_properties_changed_cb(GDBusConnection *sig,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
// parameters data type: (a{sv})
(void)sig;
(void)sender_name;
(void)object_path;
(void)interface;
(void)signal_name;
(void)user_data;

GVariantIter *iter;
gchar *property_name;
GVariant *prop_val;

const gchar *type = g_variant_get_type_string(parameters);
g_printf("PropertiesChanged data type: %s\n", type);
g_printf("%s\n", g_variant_print(parameters, true));

g_variant_get(parameters, "(a{sv})", &iter);
// while (g_variant_iter_loop(iter, "{sv}", &property_name, &prop_val))
while (g_variant_iter_next(iter, "{sv}", &property_name, &prop_val))
{
wpa_property_value(property_name, prop_val);
g_free(property_name);
g_variant_unref(prop_val);
}
g_printf("\n");

g_variant_iter_free(iter);
return;
}

static void main_loop_stop(int sig)
{
struct sigaction sigact = { .sa_handler = SIG_DFL };
sigaction(sig, &sigact, NULL);
g_main_loop_quit(g_loop);
}

int main(int argc, char *argv[])
{
// 注册信号回调
struct sigaction sigact = { .sa_handler = main_loop_stop };
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGINT, &sigact, NULL);

// 创建gdbus连接
g_gdbus_con = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
if(g_gdbus_con == NULL)
{
g_printf("Not able to get connection to system bus\n");
return 1;
}

// 创建主循环
g_loop = g_main_loop_new(NULL, FALSE);

// 同步调用,获取wlan0的对象路径
GVariant *result;
GError *error = NULL;
result = g_dbus_connection_call_sync(
g_gdbus_con,
"fi.w1.wpa_supplicant1",
"/fi/w1/wpa_supplicant1",
"fi.w1.wpa_supplicant1",
"GetInterface",
g_variant_new("(s)", "wlan0"),
G_VARIANT_TYPE("(o)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (NULL != error)
{
g_printf("Error GetInterface: %s\n", error->message);
g_error_free(error);
goto fail;
}
// g_printf("%s\n", g_variant_print(result, true));

// 解析object path
const gchar *object_path = NULL;
g_variant_get(result, "(o)", &object_path);
g_variant_unref(result);
if (object_path == NULL)
{
g_print("Failed to parse object path from the result.\n");
goto fail;
}
g_printf("Received object path from wlan0: %s\n\n", object_path);

// 订阅 ScanDone 信号
g_scan_done = g_dbus_connection_signal_subscribe(
g_gdbus_con, // connection
"fi.w1.wpa_supplicant1", // sender
"fi.w1.wpa_supplicant1.Interface", // interface_name
"ScanDone", // member:method or signal
object_path, // object path
NULL, // arg0
G_DBUS_SIGNAL_FLAGS_NONE, // flags
wpa_signal_scan_done_cb, // callback
NULL, // user_data
NULL);
// 订阅 BSSAdded 信号
g_bss_added = g_dbus_connection_signal_subscribe(
g_gdbus_con,
"fi.w1.wpa_supplicant1",
"fi.w1.wpa_supplicant1.Interface",
"BSSAdded",
object_path,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
wpa_signal_bss_added_cb,
NULL,
NULL);
// 订阅 BSSRemoved 信号
g_bss_removed = g_dbus_connection_signal_subscribe(
g_gdbus_con,
"fi.w1.wpa_supplicant1",
"fi.w1.wpa_supplicant1.Interface",
"BSSRemoved",
object_path,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
wpa_signal_bss_removed_cb,
NULL,
NULL);
// 订阅 PropertiesChanged 信号
g_prop_changed = g_dbus_connection_signal_subscribe(
g_gdbus_con,
"fi.w1.wpa_supplicant1",
"fi.w1.wpa_supplicant1.Interface",
"PropertiesChanged",
object_path,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
wpa_signal_properties_changed_cb,
NULL,
NULL);

/* 扫描 */
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(builder, "{sv}", "Type", g_variant_new_string("active"));
GVariant *variant = g_variant_builder_end(builder);
g_variant_builder_unref(builder);

g_dbus_connection_call(g_gdbus_con,
"fi.w1.wpa_supplicant1",
object_path,
"fi.w1.wpa_supplicant1.Interface",
"Scan",
g_variant_new_tuple(&variant, 1),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL,
NULL);


fail:
g_main_loop_run(g_loop);

// 释放gdbus连接
g_object_unref(g_gdbus_con);
printf("@@@@@@ App Stop @@@@@@");

return 0;
}

难点

难点1:

这里实现过程的逻辑参考了wpa_supplicant dbus api example using python ,通过python编程实现。

难点2:

订阅各种信号的返回值类型以及如何对它进行解析也是一个问题。这需要参考官方文档:wpa_supplicant 官方dbus接口文档 ,为了方便查询,我把它转换成了表格形式,具体查看这篇文章 {% post_link wpa-supplicant application %}

这里对各种信号原始数据进行记录:

主要涉及各种信号:ScanDone、BSSAdded、BssRemoved、PropertiesChanged

ScanDone

data type: (b)

1
(true,)

BSSAdded

data type: (oa{sv})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(
objectpath '/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/257',
{
'SSID': <[byte 0x43, 0x6f, 0x6f, 0x6c, 0x4b, 0x69, 0x74, 0x5f, 0x35, 0x47]>,
'BSSID': <[byte 0x60, 0xdb, 0x15, 0xfa, 0x8b, 0x05]>,
'Privacy': <true>,
'Mode': <'infrastructure'>,
'Signal': <int16 -70>,
'Frequency': <uint16 5805>,
'Rates': <[uint32 54000000, 48000000, 36000000, 24000000, 18000000, 12000000, 9000000, 6000000]>,
'WPA': <{
'KeyMgmt': <['wpa-psk']>,
'Group': <'tkip'>,
'Pairwise': <['tkip', 'ccmp']>
}>,
'RSN': <{
'KeyMgmt': <['wpa-psk']>,
'Group': <'tkip'>,
'Pairwise': <['tkip', 'ccmp']>,
'MgmtGroup': <'aes128cmac'>
}>,
'WPS': <@a{sv} {}>,
'IEs': <[byte 0x00, 0x0a, 0x43, 0x6f, 0x6f, 0x6c, 0x4b, 0x69, 0x74, 0x5f, 0x35, 0x47, 0x01, 0x08, 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c, 0x03, 0x01, 0xa1, 0xdd, 0x1a, 0x00, 0x50, 0xf2, 0x01, 0x01, 0x00, 0x00, 0x50, 0xf2, 0x02, 0x02, 0x00, 0x00, 0x50, 0xf2, 0x02, 0x00, 0x50, 0xf2, 0x04, 0x01, 0x00, 0x00, 0x50, 0xf2, 0x02, 0x30, 0x18, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x02, 0x02, 0x00, 0x00, 0x0f, 0xac, 0x02, 0x00, 0x0f, 0xac, 0x04, 0x01, 0x00, 0x00, 0x0f, 0xac, 0x02, 0x00, 0x00, 0x2d, 0x1a, 0xef, 0x09, 0x17, 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x04, 0x87, 0x09, 0x00, 0x3d, 0x16, 0xa1, 0x07, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x08, 0x00, 0x00, 0x08, 0x80, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x05, 0x0f, 0x00, 0xca, 0x12, 0x7a, 0xdd, 0x18, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x01, 0x00, 0x00, 0x03, 0xa4, 0x00, 0x00, 0x27, 0xa4, 0x00, 0x00, 0x42, 0x43, 0x5e, 0x00, 0x62, 0x32, 0x2f, 0x00, 0x20, 0x01, 0x00, 0x23, 0x02, 0x3f, 0x00, 0xc3, 0x04, 0x02, 0x2f, 0x2f, 0x2f, 0x07, 0x0a, 0x43, 0x4e, 0x04, 0x24, 0x08, 0x1e, 0x95, 0x05, 0x1e, 0x00, 0xbf, 0x0c, 0xb1, 0x79, 0xc9, 0x33, 0xfa, 0xff, 0x0c, 0x03, 0xfa, 0xff, 0x0c, 0x03, 0xc0, 0x05, 0x01, 0x9b, 0x00, 0xfc, 0xff, 0xff, 0x1c, 0x23, 0x01, 0x00, 0x08, 0x1a, 0x44, 0x10, 0x04, 0x20, 0x0e, 0x92, 0x6f, 0x09, 0x8f, 0x08, 0x00, 0x00, 0x00, 0xfa, 0xff, 0xfa, 0xff, 0x39, 0x1c, 0xc7, 0x71, 0x1c, 0x07, 0xff, 0x07, 0x24, 0xf4, 0x3f, 0x00, 0x26, 0xfc, 0xff, 0xff, 0x02, 0x27, 0x03, 0xff, 0x0e, 0x26, 0x00, 0x00, 0xff, 0x00, 0x20, 0xff, 0x00, 0x40, 0xff, 0x00, 0x60, 0xff, 0x00, 0xdd, 0x07, 0x00, 0x0c, 0x43, 0x00, 0x00, 0x00, 0x00, 0xdd, 0x21, 0x00, 0x0c, 0xe7, 0x00, 0x00, 0x00, 0x00, 0xbf, 0x0c, 0xb1, 0x01, 0xc0, 0x33, 0x2a, 0xff, 0x92, 0x04, 0x2a, 0xff, 0x92, 0x04, 0xc0, 0x05, 0x00, 0x00, 0x00, 0x2a, 0xff, 0xc3, 0x03, 0x01, 0x02, 0x02, 0xf0, 0x02, 0x00, 0x00]>,
'Age': <uint32 0>
}
)

BSSRemoved

data type: (o)

1
(objectpath '/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/519',)

PropertiesChanged

data type: (a{sv})

数据1:

1
2
3
4
5
6
7
8
9
10
11
(
{
'Scanning': <false>,
'BSSs': <[objectpath
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/2',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/423',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/424',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/425',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/426']>
},
)

数据2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(
{
'BSSs': <[objectpath
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/2',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1363',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1364',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1365',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1366',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1367',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1369',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1372',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1374',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1375',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1376',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1377',
'/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/1378']>
},
)