-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathp2p_usd.c
317 lines (270 loc) · 8.29 KB
/
p2p_usd.c
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
/*
* Sigma Control API DUT (USD functionality)
* Copyright (c) 2024, Qualcomm Innovation Center, Inc.
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include "sigma_dut.h"
#include <sys/stat.h>
#include "wpa_ctrl.h"
#include "wpa_helpers.h"
enum sigma_cmd_result sigma_usd_publish(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *program = get_param(cmd, "Prog");
const char *ifname = get_param(cmd, "interface");
const char *service_name = get_param(cmd, "ServiceName");
const char *ssi = get_param(cmd, "ServSpecificInfoPayload");
const char *serv_proto_type = get_param(cmd, "ServProtoType");
const char *oper_chan = get_param(cmd, "AdvertiseChannel");
const char *bootstrapmethod = get_param(cmd, "PairingBootstrapMethod");
char buf[3000];
int i, res;
size_t len;
if (!ifname)
ifname = get_station_ifname(dut);
if (bootstrapmethod) {
if (wpa_command(ifname, "P2P_SET pairing_setup 1") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"ErrorCode,Failed to set pairing setup");
return STATUS_SENT;
}
if (wpa_command(ifname, "P2P_SET pairing_cache 1") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"ErrorCode,Failed to set pairing cache");
return STATUS_SENT;
}
}
if (!service_name)
return ERROR_SEND_STATUS;
res = snprintf(buf, sizeof(buf), "NAN_PUBLISH service_name=%s ttl=100",
service_name);
if (res < 0 || res >= sizeof(buf))
return ERROR_SEND_STATUS;
len = res;
if (program && strcasecmp(program, "P2P") == 0) {
res = snprintf(buf + len, sizeof(buf) - len, " p2p=1");
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
}
if (ssi) {
size_t ssi_len = atoi(ssi);
char ssi_hex[2048];
if (ssi_len * 2 + 1 > sizeof(ssi_hex))
return ERROR_SEND_STATUS;
for (i = 0; i < ssi_len; i++)
sprintf(ssi_hex + i * 2, "%02x", i);
ssi_hex[ssi_len * 2] = '\0';
res = snprintf(buf + len, sizeof(buf) - len, " ssi=%s",
ssi_hex);
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
}
if (serv_proto_type) {
res = snprintf(buf + len, sizeof(buf) - len,
" srv_proto_type=%s", serv_proto_type);
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
}
if (oper_chan) {
int chan, freq;
chan = atoi(oper_chan);
freq = channel_to_freq(dut, chan);
res = snprintf(buf + len, sizeof(buf) - len, " freq=%d", freq);
} else {
res = snprintf(buf + len, sizeof(buf) - len, " freq=2437");
}
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
if (wpa_command(ifname, buf)) {
send_resp(dut, conn, SIGMA_ERROR, "Unable to USD publish");
return STATUS_SENT_ERROR;
}
return SUCCESS_SEND_STATUS;
}
enum sigma_cmd_result sigma_usd_subscribe(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *program = get_param(cmd, "Prog");
const char *ifname = get_param(cmd, "interface");
const char *service_name = get_param(cmd, "ServiceName");
const char *ssi = get_param(cmd, "ServSpecificInfoPayload");
const char *serv_proto_type = get_param(cmd, "ServProtoType");
const char *default_chan = get_param(cmd, "DefaultPublishChannel");
const char *chan_list = get_param(cmd, "PublishChannelList");
const char *bootstrapmethod = get_param(cmd, "PairingBootstrapMethod");
char buf[3000];
int i, res;
size_t len;
if (!ifname)
ifname = get_station_ifname(dut);
if (bootstrapmethod) {
if (wpa_command(ifname, "P2P_SET pairing_setup 1") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"ErrorCode,Failed to set pairing setup");
return STATUS_SENT;
}
if (wpa_command(ifname, "P2P_SET pairing_cache 1") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"ErrorCode,Failed to set pairing cache");
return STATUS_SENT;
}
if (wpa_command(ifname, "P2P_SET pairing_verification 1") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"ErrorCode,Failed to enable pairing verification");
return STATUS_SENT;
}
}
if (!service_name)
return ERROR_SEND_STATUS;
res = snprintf(buf, sizeof(buf),
"NAN_SUBSCRIBE service_name=%s ttl=100",
service_name);
if (res < 0 || res >= sizeof(buf))
return ERROR_SEND_STATUS;
len = res;
if (program && strcasecmp(program, "P2P") == 0) {
res = snprintf(buf + len, sizeof(buf) - len, " p2p=1");
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
}
if (ssi) {
size_t ssi_len = atoi(ssi);
char ssi_hex[2048];
if (ssi_len * 2 + 1 > sizeof(ssi_hex))
return ERROR_SEND_STATUS;
for (i = 0; i < ssi_len; i++)
sprintf(ssi_hex + i * 2, "%02x", i);
ssi_hex[ssi_len * 2] = '\0';
res = snprintf(buf + len, sizeof(buf) - len, " ssi=%s",
ssi_hex);
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
}
if (serv_proto_type) {
res = snprintf(buf + len, sizeof(buf) - len,
" srv_proto_type=%s", serv_proto_type);
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
}
if (default_chan) {
int chan, freq;
chan = atoi(default_chan);
freq = channel_to_freq(dut, chan);
res = snprintf(buf + len, sizeof(buf) - len, " freq=%d", freq);
} else {
res = snprintf(buf + len, sizeof(buf) - len, " freq=2437");
}
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
if (chan_list) {
char freq_list[200], *token;
size_t flen = 0;
char *ch_list = strdup(chan_list);
if (!ch_list)
return ERROR_SEND_STATUS;
freq_list[0] = '\0';
token = strtok(ch_list, " ");
while (token && strlen(freq_list) < sizeof(freq_list) - 10) {
int chan, freq;
chan = atoi(token);
freq = channel_to_freq(dut, chan);
res = snprintf(freq_list, sizeof(freq_list) - flen,
"%d,", freq);
if (res < 0 || res >= sizeof(freq_list) - flen)
return ERROR_SEND_STATUS;
flen += res;
token = strtok(NULL, " ");
}
free(ch_list);
if (flen > 0)
freq_list[flen - 1] = '\0';
res = snprintf(buf + len, sizeof(buf) - len, " freq_list=%s",
freq_list);
} else {
res = snprintf(buf + len, sizeof(buf) - len, " freq_list=5180");
}
if (res < 0 || res >= sizeof(buf) - len)
return ERROR_SEND_STATUS;
len += res;
if (wpa_command(ifname, buf)) {
send_resp(dut, conn, SIGMA_ERROR, "Unable to USD subscribe");
return STATUS_SENT_ERROR;
}
return SUCCESS_SEND_STATUS;
}
enum sigma_cmd_result usd_cmd_sta_exec_action(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
int ret;
u8 len;
char buf[200];
const char *ifname = get_param(cmd, "interface");
const char *method_type = get_param(cmd, "MethodType");
const char *pairing_bootstrap_method;
int bootstrap = 0;
if (!ifname)
ifname = get_station_ifname(dut);
if (!dut->usd_enabled) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,USD not enabled");
return STATUS_SENT;
}
pairing_bootstrap_method = get_param(cmd, "PairingBootstrapMethod");
if (pairing_bootstrap_method) {
if (strcasecmp(pairing_bootstrap_method, "NonZero") == 0) {
/* opportunistic */
bootstrap = BIT(0);
/* Display pincode and passphrase */
bootstrap |= BIT(1) | BIT(2);
/* Keypad pincode and passphrase */
bootstrap |= BIT(5) | BIT(6);
} else {
bootstrap = atoi(pairing_bootstrap_method);
}
len = snprintf(buf, sizeof(buf),
"P2P_SET supported_bootstrapmethods %d",
bootstrap);
if (len < 0 || len >= sizeof(buf))
return ERROR_SEND_STATUS;
sigma_dut_print(dut, DUT_MSG_INFO, "PP2_SET to wpa_command: %s",
buf);
if (wpa_command(ifname, buf)) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Unable to set pairing bootstrap method");
return STATUS_SENT_ERROR;
}
}
if (!method_type) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Missing USD Type");
return STATUS_SENT_ERROR;
}
if (strcasecmp(method_type, "ADVERTISE") == 0) {
ret = sigma_usd_publish(dut, conn, cmd);
if (ret < 0)
return ret;
send_resp(dut, conn, SIGMA_COMPLETE, "NULL");
return STATUS_SENT;
}
if (strcasecmp(method_type, "SEEK") == 0) {
ret = sigma_usd_subscribe(dut, conn, cmd);
if (ret < 0)
return ret;
send_resp(dut, conn, SIGMA_COMPLETE, "NULL");
return STATUS_SENT;
}
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Unsupported USD MethodType");
return STATUS_SENT;
}