blob: 813849bce468936fc22e03ae0a9e7df7d67f1f72 [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (NAN functionality)
Rakesh Sunki4b75f962017-03-30 14:47:55 -07003 * Copyright (c) 2014-2017, Qualcomm Atheros, Inc.
Jouni Malinencd4e3c32015-10-29 12:39:56 +02004 * All Rights Reserved.
5 * Licensed under the Clear BSD license. See README for more details.
6 */
7
8#include "sigma_dut.h"
9#include <sys/stat.h>
10#include "wpa_ctrl.h"
11#include "wpa_helpers.h"
12#include "wifi_hal.h"
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -070013#include "nan_cert.h"
Jouni Malinencd4e3c32015-10-29 12:39:56 +020014
Rakesh Sunki4b75f962017-03-30 14:47:55 -070015#if NAN_CERT_VERSION >= 2
16
Jouni Malinencd4e3c32015-10-29 12:39:56 +020017pthread_cond_t gCondition;
18pthread_mutex_t gMutex;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -070019wifi_handle global_wifi_handle;
20wifi_interface_handle global_interface_handle;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020021static int nan_state = 0;
22static int event_anyresponse = 0;
23static int is_fam = 0;
24
Rakesh Sunki4c086672017-03-30 14:47:55 -070025static uint16_t global_ndp_instance_id = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020026uint16_t global_header_handle = 0;
27uint32_t global_match_handle = 0;
28
29#define MAC_ADDR_ARRAY(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
30#define MAC_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
31#ifndef ETH_ALEN
32#define ETH_ALEN 6
33#endif
34
35struct sigma_dut *global_dut = NULL;
36static char global_nan_mac_addr[ETH_ALEN];
37static char global_event_resp_buf[1024];
38
39static int nan_further_availability_tx(struct sigma_dut *dut,
40 struct sigma_conn *conn,
41 struct sigma_cmd *cmd);
42static int nan_further_availability_rx(struct sigma_dut *dut,
43 struct sigma_conn *conn,
44 struct sigma_cmd *cmd);
45
46
47void nan_hex_dump(struct sigma_dut *dut, uint8_t *data, size_t len)
48{
49 char buf[512];
50 uint16_t index;
51 uint8_t *ptr;
52 int pos;
53
54 memset(buf, 0, sizeof(buf));
55 ptr = data;
56 pos = 0;
57 for (index = 0; index < len; index++) {
58 pos += sprintf(&(buf[pos]), "%02x ", *ptr++);
59 if (pos > 508)
60 break;
61 }
62 sigma_dut_print(dut, DUT_MSG_INFO, "HEXDUMP len=[%d]", (int) len);
63 sigma_dut_print(dut, DUT_MSG_INFO, "buf:%s", buf);
64}
65
66
67int nan_parse_hex(unsigned char c)
68{
69 if (c >= '0' && c <= '9')
70 return c - '0';
71 if (c >= 'a' && c <= 'f')
72 return c - 'a' + 10;
73 if (c >= 'A' && c <= 'F')
74 return c - 'A' + 10;
75 return 0;
76}
77
78
79int nan_parse_token(const char *tokenIn, u8 *tokenOut, int *filterLen)
80{
81 int total_len = 0, len = 0;
82 char *saveptr = NULL;
83
84 tokenIn = strtok_r((char *) tokenIn, ":", &saveptr);
85 while (tokenIn != NULL) {
86 len = strlen(tokenIn);
87 if (len == 1 && *tokenIn == '*')
88 len = 0;
89 tokenOut[total_len++] = (u8) len;
90 if (len != 0)
91 memcpy((u8 *) tokenOut + total_len, tokenIn, len);
92 total_len += len;
93 tokenIn = strtok_r(NULL, ":", &saveptr);
94 }
95 *filterLen = total_len;
96 return 0;
97}
98
99
100int nan_parse_mac_address(struct sigma_dut *dut, const char *arg, u8 *addr)
101{
102 if (strlen(arg) != 17) {
103 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid mac address %s",
104 arg);
105 sigma_dut_print(dut, DUT_MSG_ERROR,
106 "expected format xx:xx:xx:xx:xx:xx");
107 return -1;
108 }
109
110 addr[0] = nan_parse_hex(arg[0]) << 4 | nan_parse_hex(arg[1]);
111 addr[1] = nan_parse_hex(arg[3]) << 4 | nan_parse_hex(arg[4]);
112 addr[2] = nan_parse_hex(arg[6]) << 4 | nan_parse_hex(arg[7]);
113 addr[3] = nan_parse_hex(arg[9]) << 4 | nan_parse_hex(arg[10]);
114 addr[4] = nan_parse_hex(arg[12]) << 4 | nan_parse_hex(arg[13]);
115 addr[5] = nan_parse_hex(arg[15]) << 4 | nan_parse_hex(arg[16]);
116
117 return 0;
118}
119
120
121int nan_parse_mac_address_list(struct sigma_dut *dut, const char *input,
122 u8 *output, u16 max_addr_allowed)
123{
124 /*
125 * Reads a list of mac address separated by space. Each MAC address
126 * should have the format of aa:bb:cc:dd:ee:ff.
127 */
128 char *saveptr;
129 char *token;
130 int i = 0;
131
132 for (i = 0; i < max_addr_allowed; i++) {
133 token = strtok_r((i == 0) ? (char *) input : NULL,
134 " ", &saveptr);
135 if (token) {
136 nan_parse_mac_address(dut, token, output);
137 output += NAN_MAC_ADDR_LEN;
138 } else
139 break;
140 }
141
142 sigma_dut_print(dut, DUT_MSG_INFO, "Num MacAddress:%d", i);
143
144 return i;
145}
146
147
148int nan_parse_hex_string(struct sigma_dut *dut, const char *input,
149 u8 *output, int *outputlen)
150{
151 int i = 0;
152 int j = 0;
153
154 for (i = 0; i < (int) strlen(input) && j < *outputlen; i += 2) {
155 output[j] = nan_parse_hex(input[i]);
156 if (i + 1 < (int) strlen(input)) {
157 output[j] = ((output[j] << 4) |
158 nan_parse_hex(input[i + 1]));
159 }
160 j++;
161 }
162 *outputlen = j;
163 sigma_dut_print(dut, DUT_MSG_INFO, "Input:%s inputlen:%d outputlen:%d",
164 input, (int) strlen(input), (int) *outputlen);
165 return 0;
166}
167
168
169int wait(struct timespec abstime)
170{
171 struct timeval now;
172
173 gettimeofday(&now, NULL);
174
175 abstime.tv_sec += now.tv_sec;
176 if (((abstime.tv_nsec + now.tv_usec * 1000) > 1000 * 1000 * 1000) ||
177 (abstime.tv_nsec + now.tv_usec * 1000 < 0)) {
178 abstime.tv_sec += 1;
179 abstime.tv_nsec += now.tv_usec * 1000;
180 abstime.tv_nsec -= 1000 * 1000 * 1000;
181 } else {
182 abstime.tv_nsec += now.tv_usec * 1000;
183 }
184
185 return pthread_cond_timedwait(&gCondition, &gMutex, &abstime);
186}
187
188
189int nan_cmd_sta_preset_testparameters(struct sigma_dut *dut,
190 struct sigma_conn *conn,
191 struct sigma_cmd *cmd)
192{
Rakesh Sunki8dd1d882017-03-30 14:47:55 -0700193 const char *oper_chan = get_param(cmd, "oper_chn");
Rakesh Sunki7d37f412017-03-30 14:47:55 -0700194 const char *pmk = get_param(cmd, "PMK");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200195
Rakesh Sunki8dd1d882017-03-30 14:47:55 -0700196 if (oper_chan) {
197 sigma_dut_print(dut, DUT_MSG_INFO, "Operating Channel: %s",
198 oper_chan);
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -0700199 dut->sta_channel = atoi(oper_chan);
Rakesh Sunki8dd1d882017-03-30 14:47:55 -0700200 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200201
Rakesh Sunki7d37f412017-03-30 14:47:55 -0700202 if (pmk) {
203 int pmk_len;
204
205 sigma_dut_print(dut, DUT_MSG_INFO, "%s given string pmk: %s",
206 __func__, pmk);
207 memset(dut->nan_pmk, 0, NAN_PMK_INFO_LEN);
208 dut->nan_pmk_len = 0;
209 pmk_len = NAN_PMK_INFO_LEN;
210 nan_parse_hex_string(dut, &pmk[2], &dut->nan_pmk[0], &pmk_len);
211 dut->nan_pmk_len = pmk_len;
212 sigma_dut_print(dut, DUT_MSG_INFO, "%s: pmk len = %d",
213 __func__, dut->nan_pmk_len);
214 sigma_dut_print(dut, DUT_MSG_INFO, "%s:hex pmk", __func__);
215 nan_hex_dump(dut, &dut->nan_pmk[0], dut->nan_pmk_len);
216 }
217
Rakesh Sunki14bff1d2017-03-30 14:47:55 -0700218 send_resp(dut, conn, SIGMA_COMPLETE, NULL);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200219 return 0;
220}
221
222
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700223void nan_print_further_availability_chan(struct sigma_dut *dut,
224 u8 num_chans,
225 NanFurtherAvailabilityChannel *fachan)
226{
227 int idx;
228
229 sigma_dut_print(dut, DUT_MSG_INFO,
230 "********Printing FurtherAvailabilityChan Info******");
231 sigma_dut_print(dut, DUT_MSG_INFO, "Numchans:%d", num_chans);
232 for (idx = 0; idx < num_chans; idx++) {
233 sigma_dut_print(dut, DUT_MSG_INFO,
234 "[%d]: NanAvailDuration:%d class_val:%02x channel:%d",
235 idx, fachan->entry_control,
236 fachan->class_val, fachan->channel);
237 sigma_dut_print(dut, DUT_MSG_INFO,
238 "[%d]: mapid:%d Availability bitmap:%08x",
239 idx, fachan->mapid,
240 fachan->avail_interval_bitmap);
241 }
242 sigma_dut_print(dut, DUT_MSG_INFO,
243 "*********************Done**********************");
244}
245
246
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200247int sigma_nan_enable(struct sigma_dut *dut, struct sigma_conn *conn,
248 struct sigma_cmd *cmd)
249{
250 const char *master_pref = get_param(cmd, "MasterPref");
251 const char *rand_fac = get_param(cmd, "RandFactor");
252 const char *hop_count = get_param(cmd, "HopCount");
253 const char *high_tsf = get_param(cmd, "HighTSF");
254 const char *sdftx_band = get_param(cmd, "SDFTxBand");
255 const char *oper_chan = get_param(cmd, "oper_chn");
256 const char *further_avail_ind = get_param(cmd, "FurtherAvailInd");
257 const char *band = get_param(cmd, "Band");
258 const char *only_5g = get_param(cmd, "5GOnly");
259 struct timespec abstime;
260 NanEnableRequest req;
261
262 memset(&req, 0, sizeof(NanEnableRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200263 req.cluster_low = 0;
264 req.cluster_high = 0xFFFF;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700265 req.master_pref = 100;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200266
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700267 /* This is a debug hack to beacon in channel 11 */
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200268 if (oper_chan) {
269 req.config_2dot4g_support = 1;
270 req.support_2dot4g_val = 111;
271 }
272
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200273 if (master_pref) {
274 int master_pref_val = strtoul(master_pref, NULL, 0);
275
276 req.master_pref = master_pref_val;
277 }
278
279 if (rand_fac) {
280 int rand_fac_val = strtoul(rand_fac, NULL, 0);
281
282 req.config_random_factor_force = 1;
283 req.random_factor_force_val = rand_fac_val;
284 }
285
286 if (hop_count) {
287 int hop_count_val = strtoul(hop_count, NULL, 0);
288
289 req.config_hop_count_force = 1;
290 req.hop_count_force_val = hop_count_val;
291 }
292
293 if (sdftx_band) {
294 if (strcasecmp(sdftx_band, "5G") == 0) {
295 req.config_2dot4g_support = 1;
296 req.support_2dot4g_val = 0;
297 }
298 }
299
Rakesh Sunki47a276a2017-03-30 14:47:55 -0700300 sigma_dut_print(dut, DUT_MSG_INFO,
301 "%s: Setting dual band 2.4 GHz and 5 GHz by default",
302 __func__);
303 /* Enable 2.4 GHz support */
304 req.config_2dot4g_support = 1;
305 req.support_2dot4g_val = 1;
306 req.config_2dot4g_beacons = 1;
307 req.beacon_2dot4g_val = 1;
308 req.config_2dot4g_sdf = 1;
309 req.sdf_2dot4g_val = 1;
310
311 /* Enable 5 GHz support */
312 req.config_support_5g = 1;
313 req.support_5g_val = 1;
314 req.config_5g_beacons = 1;
315 req.beacon_5g_val = 1;
316 req.config_5g_sdf = 1;
317 req.sdf_5g_val = 1;
318
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200319 if (band) {
320 if (strcasecmp(band, "24G") == 0) {
321 sigma_dut_print(dut, DUT_MSG_INFO,
Rakesh Sunki47a276a2017-03-30 14:47:55 -0700322 "Band 2.4 GHz selected, disable 5 GHz");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200323 /* Disable 5G support */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700324 req.config_support_5g = 1;
325 req.support_5g_val = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200326 req.config_5g_beacons = 1;
327 req.beacon_5g_val = 0;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700328 req.config_5g_sdf = 1;
329 req.sdf_5g_val = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200330 }
331 }
332
333 if (further_avail_ind) {
334 sigma_dut_print(dut, DUT_MSG_INFO, "FAM Test Enabled");
335 if (strcasecmp(further_avail_ind, "tx") == 0) {
336 is_fam = 1;
337 nan_further_availability_tx(dut, conn, cmd);
338 return 0;
339 } else if (strcasecmp(further_avail_ind, "rx") == 0) {
340 nan_further_availability_rx(dut, conn, cmd);
341 return 0;
342 }
343 }
344
345 if (only_5g && atoi(only_5g)) {
346 sigma_dut_print(dut, DUT_MSG_INFO, "5GHz only enabled");
347 req.config_2dot4g_support = 1;
348 req.support_2dot4g_val = 1;
349 req.config_2dot4g_beacons = 1;
350 req.beacon_2dot4g_val = 0;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700351 req.config_2dot4g_sdf = 1;
352 req.sdf_2dot4g_val = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200353 }
354
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700355 nan_enable_request(0, global_interface_handle, &req);
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700356
357 /* To ensure sta_get_events to get the events
358 * only after joining the NAN cluster. */
359 abstime.tv_sec = 30;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200360 abstime.tv_nsec = 0;
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700361 wait(abstime);
362
363 return 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200364}
365
366
367int sigma_nan_disable(struct sigma_dut *dut, struct sigma_conn *conn,
368 struct sigma_cmd *cmd)
369{
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200370 struct timespec abstime;
371
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700372 nan_disable_request(0, global_interface_handle);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200373
374 abstime.tv_sec = 4;
375 abstime.tv_nsec = 0;
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700376 wait(abstime);
377
378 return 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200379}
380
381
382int sigma_nan_config_enable(struct sigma_dut *dut, struct sigma_conn *conn,
383 struct sigma_cmd *cmd)
384{
385 const char *master_pref = get_param(cmd, "MasterPref");
386 const char *rand_fac = get_param(cmd, "RandFactor");
387 const char *hop_count = get_param(cmd, "HopCount");
Rakesh Sunki107356c2017-03-30 14:47:55 -0700388 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200389 struct timespec abstime;
390 NanConfigRequest req;
391
392 memset(&req, 0, sizeof(NanConfigRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200393 req.config_rssi_proximity = 1;
394 req.rssi_proximity = 70;
395
396 if (master_pref) {
397 int master_pref_val = strtoul(master_pref, NULL, 0);
398
399 req.config_master_pref = 1;
400 req.master_pref = master_pref_val;
401 }
402
403 if (rand_fac) {
404 int rand_fac_val = strtoul(rand_fac, NULL, 0);
405
406 req.config_random_factor_force = 1;
407 req.random_factor_force_val = rand_fac_val;
408 }
409
410 if (hop_count) {
411 int hop_count_val = strtoul(hop_count, NULL, 0);
412
413 req.config_hop_count_force = 1;
414 req.hop_count_force_val = hop_count_val;
415 }
416
Rakesh Sunki107356c2017-03-30 14:47:55 -0700417 ret = nan_config_request(0, global_interface_handle, &req);
418 if (ret != WIFI_SUCCESS)
419 send_resp(dut, conn, SIGMA_ERROR, "NAN config request failed");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200420
421 abstime.tv_sec = 4;
422 abstime.tv_nsec = 0;
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700423 wait(abstime);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200424
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700425 return 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200426}
427
428
429static int sigma_nan_subscribe_request(struct sigma_dut *dut,
430 struct sigma_conn *conn,
431 struct sigma_cmd *cmd)
432{
433 const char *subscribe_type = get_param(cmd, "SubscribeType");
434 const char *service_name = get_param(cmd, "ServiceName");
435 const char *disc_range = get_param(cmd, "DiscoveryRange");
436 const char *rx_match_filter = get_param(cmd, "rxMatchFilter");
437 const char *tx_match_filter = get_param(cmd, "txMatchFilter");
438 const char *sdftx_dw = get_param(cmd, "SDFTxDW");
439 const char *discrange_ltd = get_param(cmd, "DiscRangeLtd");
440 const char *include_bit = get_param(cmd, "IncludeBit");
441 const char *mac = get_param(cmd, "MAC");
442 const char *srf_type = get_param(cmd, "SRFType");
443 NanSubscribeRequest req;
444 int filter_len_rx = 0, filter_len_tx = 0;
445 u8 input_rx[NAN_MAX_MATCH_FILTER_LEN];
446 u8 input_tx[NAN_MAX_MATCH_FILTER_LEN];
Rakesh Sunki107356c2017-03-30 14:47:55 -0700447 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200448
449 memset(&req, 0, sizeof(NanSubscribeRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200450 req.ttl = 0;
451 req.period = 1000;
452 req.subscribe_type = 1;
453 req.serviceResponseFilter = 1; /* MAC */
454 req.serviceResponseInclude = 0;
455 req.ssiRequiredForMatchIndication = 0;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700456 req.subscribe_match_indicator = NAN_MATCH_ALG_MATCH_CONTINUOUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200457 req.subscribe_count = 0;
458
459 if (subscribe_type) {
460 if (strcasecmp(subscribe_type, "Active") == 0) {
461 req.subscribe_type = 1;
462 } else if (strcasecmp(subscribe_type, "Passive") == 0) {
463 req.subscribe_type = 0;
464 } else if (strcasecmp(subscribe_type, "Cancel") == 0) {
465 NanSubscribeCancelRequest req;
466
467 memset(&req, 0, sizeof(NanSubscribeCancelRequest));
Rakesh Sunki107356c2017-03-30 14:47:55 -0700468 ret = nan_subscribe_cancel_request(
469 0, global_interface_handle, &req);
470 if (ret != WIFI_SUCCESS) {
471 send_resp(dut, conn, SIGMA_ERROR,
472 "NAN subscribe cancel request failed");
473 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200474 return 0;
475 }
476 }
477
478 if (disc_range)
479 req.rssi_threshold_flag = atoi(disc_range);
480
481 if (sdftx_dw)
482 req.subscribe_count = atoi(sdftx_dw);
483
484 /* Check this once again if config can be called here (TBD) */
485 if (discrange_ltd)
486 req.rssi_threshold_flag = atoi(discrange_ltd);
487
488 if (include_bit) {
489 int include_bit_val = atoi(include_bit);
490
491 req.serviceResponseInclude = include_bit_val;
492 sigma_dut_print(dut, DUT_MSG_INFO, "Includebit set %d",
493 req.serviceResponseInclude);
494 }
495
496 if (srf_type) {
497 int srf_type_val = atoi(srf_type);
498
499 if (srf_type_val == 1)
500 req.serviceResponseFilter = 0; /* Bloom */
501 else
502 req.serviceResponseFilter = 1; /* MAC */
503 req.useServiceResponseFilter = 1;
504 sigma_dut_print(dut, DUT_MSG_INFO, "srfFilter %d",
505 req.serviceResponseFilter);
506 }
507
508 if (mac) {
509 sigma_dut_print(dut, DUT_MSG_INFO, "MAC_ADDR List %s", mac);
510 req.num_intf_addr_present = nan_parse_mac_address_list(
511 dut, mac, &req.intf_addr[0][0],
512 NAN_MAX_SUBSCRIBE_MAX_ADDRESS);
513 }
514
515 memset(input_rx, 0, sizeof(input_rx));
516 memset(input_tx, 0, sizeof(input_tx));
517 if (rx_match_filter) {
518 nan_parse_token(rx_match_filter, input_rx, &filter_len_rx);
519 sigma_dut_print(dut, DUT_MSG_INFO, "RxFilterLen %d",
520 filter_len_rx);
521 }
522 if (tx_match_filter) {
523 nan_parse_token(tx_match_filter, input_tx, &filter_len_tx);
524 sigma_dut_print(dut, DUT_MSG_INFO, "TxFilterLen %d",
525 filter_len_tx);
526 }
527
528 if (tx_match_filter) {
529 req.tx_match_filter_len = filter_len_tx;
530 memcpy(req.tx_match_filter, input_tx, filter_len_tx);
531 nan_hex_dump(dut, req.tx_match_filter, filter_len_tx);
532 }
533 if (rx_match_filter) {
534 req.rx_match_filter_len = filter_len_rx;
535 memcpy(req.rx_match_filter, input_rx, filter_len_rx);
536 nan_hex_dump(dut, req.rx_match_filter, filter_len_rx);
537 }
538
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -0700539 if (service_name) {
540 strlcpy((char *) req.service_name, service_name,
541 strlen(service_name) + 1);
542 req.service_name_len = strlen(service_name);
543 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200544
Rakesh Sunki107356c2017-03-30 14:47:55 -0700545 ret = nan_subscribe_request(0, global_interface_handle, &req);
546 if (ret != WIFI_SUCCESS) {
547 send_resp(dut, conn, SIGMA_ERROR,
548 "NAN subscribe request failed");
549 }
550
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200551 return 0;
552}
553
554
Rakesh Sunkid7344c02017-03-30 14:47:55 -0700555static int sigma_ndp_configure_band(struct sigma_dut *dut,
556 struct sigma_conn *conn,
557 struct sigma_cmd *cmd,
558 NdpSupportedBand band_config_val)
559{
560 wifi_error ret;
561 NanDebugParams cfg_debug;
562 int size;
563
564 memset(&cfg_debug, 0, sizeof(NanDebugParams));
565 cfg_debug.cmd = NAN_TEST_MODE_CMD_NAN_SUPPORTED_BANDS;
566 memcpy(cfg_debug.debug_cmd_data, &band_config_val, sizeof(int));
567 sigma_dut_print(dut, DUT_MSG_INFO, "%s:setting debug cmd=0x%x",
568 __func__, cfg_debug.cmd);
569 size = sizeof(u32) + sizeof(int);
570 ret = nan_debug_command_config(0, global_interface_handle, cfg_debug,
571 size);
572 if (ret != WIFI_SUCCESS)
573 send_resp(dut, conn, SIGMA_ERROR, "Nan config request failed");
574
575 return 0;
576}
577
578
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200579int config_post_disc_attr(void)
580{
Rakesh Sunki107356c2017-03-30 14:47:55 -0700581 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200582 NanConfigRequest configReq;
583
584 memset(&configReq, 0, sizeof(NanConfigRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200585
586 /* Configure Post disc attr */
587 /* Make these defines and use correct enum */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700588 configReq.num_config_discovery_attr = 1;
589 configReq.discovery_attr_val[0].type = 4; /* Further Nan discovery */
590 configReq.discovery_attr_val[0].role = 0;
591 configReq.discovery_attr_val[0].transmit_freq = 1;
592 configReq.discovery_attr_val[0].duration = 0;
593 configReq.discovery_attr_val[0].avail_interval_bitmap = 0x00000008;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200594
Rakesh Sunki107356c2017-03-30 14:47:55 -0700595 ret = nan_config_request(0, global_interface_handle, &configReq);
596 if (ret != WIFI_SUCCESS) {
597 sigma_dut_print(global_dut, DUT_MSG_INFO,
598 "NAN config request failed while configuring post discovery attribute");
599 }
600
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200601 return 0;
602}
603
604
605int sigma_nan_publish_request(struct sigma_dut *dut, struct sigma_conn *conn,
606 struct sigma_cmd *cmd)
607{
608 const char *publish_type = get_param(cmd, "PublishType");
609 const char *service_name = get_param(cmd, "ServiceName");
610 const char *disc_range = get_param(cmd, "DiscoveryRange");
611 const char *rx_match_filter = get_param(cmd, "rxMatchFilter");
612 const char *tx_match_filter = get_param(cmd, "txMatchFilter");
613 const char *sdftx_dw = get_param(cmd, "SDFTxDW");
614 const char *discrange_ltd = get_param(cmd, "DiscRangeLtd");
615 NanPublishRequest req;
616 int filter_len_rx = 0, filter_len_tx = 0;
617 u8 input_rx[NAN_MAX_MATCH_FILTER_LEN];
618 u8 input_tx[NAN_MAX_MATCH_FILTER_LEN];
Rakesh Sunki107356c2017-03-30 14:47:55 -0700619 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200620
621 memset(&req, 0, sizeof(NanPublishRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200622 req.ttl = 0;
623 req.period = 500;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700624 req.publish_match_indicator = 1;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200625 req.publish_type = NAN_PUBLISH_TYPE_UNSOLICITED;
626 req.tx_type = NAN_TX_TYPE_BROADCAST;
627 req.publish_count = 0;
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -0700628
629 if (service_name) {
630 strlcpy((char *) req.service_name, service_name,
631 strlen(service_name) + 1);
632 req.service_name_len = strlen(service_name);
633 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200634
635 if (publish_type) {
636 if (strcasecmp(publish_type, "Solicited") == 0) {
637 req.publish_type = NAN_PUBLISH_TYPE_SOLICITED;
638 } else if (strcasecmp(publish_type, "Cancel") == 0) {
639 NanPublishCancelRequest req;
640
641 memset(&req, 0, sizeof(NanPublishCancelRequest));
Rakesh Sunki107356c2017-03-30 14:47:55 -0700642 ret = nan_publish_cancel_request(
643 0, global_interface_handle, &req);
644 if (ret != WIFI_SUCCESS) {
645 send_resp(dut, conn, SIGMA_ERROR,
646 "Unable to cancel nan publish request");
647 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200648 return 0;
649 }
650 }
651
652 if (disc_range)
653 req.rssi_threshold_flag = atoi(disc_range);
654
655 if (sdftx_dw)
656 req.publish_count = atoi(sdftx_dw);
657
658 if (discrange_ltd)
659 req.rssi_threshold_flag = atoi(discrange_ltd);
660
661 memset(input_rx, 0, sizeof(input_rx));
662 memset(input_tx, 0, sizeof(input_tx));
663 if (rx_match_filter) {
664 nan_parse_token(rx_match_filter, input_rx, &filter_len_rx);
665 sigma_dut_print(dut, DUT_MSG_INFO, "RxFilterLen %d",
666 filter_len_rx);
667 }
668 if (tx_match_filter) {
669 nan_parse_token(tx_match_filter, input_tx, &filter_len_tx);
670 sigma_dut_print(dut, DUT_MSG_INFO, "TxFilterLen %d",
671 filter_len_tx);
672 }
673
674 if (is_fam == 1) {
675 config_post_disc_attr();
676 /* TODO: Add comments regarding this step */
677 req.connmap = 0x10;
678 }
679
680 if (tx_match_filter) {
681 req.tx_match_filter_len = filter_len_tx;
682 memcpy(req.tx_match_filter, input_tx, filter_len_tx);
683 nan_hex_dump(dut, req.tx_match_filter, filter_len_tx);
684 }
685
686 if (rx_match_filter) {
687 req.rx_match_filter_len = filter_len_rx;
688 memcpy(req.rx_match_filter, input_rx, filter_len_rx);
689 nan_hex_dump(dut, req.rx_match_filter, filter_len_rx);
690 }
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -0700691
692 if (service_name) {
693 strlcpy((char *) req.service_name, service_name,
694 strlen(service_name) + 1);
695 req.service_name_len = strlen(service_name);
696 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200697
Rakesh Sunki107356c2017-03-30 14:47:55 -0700698 ret = nan_publish_request(0, global_interface_handle, &req);
699 if (ret != WIFI_SUCCESS)
700 send_resp(dut, conn, SIGMA_ERROR, "Unable to publish");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200701
702 return 0;
703}
704
705
706static int nan_further_availability_rx(struct sigma_dut *dut,
707 struct sigma_conn *conn,
708 struct sigma_cmd *cmd)
709{
710 const char *master_pref = get_param(cmd, "MasterPref");
711 const char *rand_fac = get_param(cmd, "RandFactor");
712 const char *hop_count = get_param(cmd, "HopCount");
Rakesh Sunki107356c2017-03-30 14:47:55 -0700713 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200714 struct timespec abstime;
715
716 NanEnableRequest req;
717
718 memset(&req, 0, sizeof(NanEnableRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200719 req.cluster_low = 0;
720 req.cluster_high = 0xFFFF;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200721 req.master_pref = 30;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200722
723 if (master_pref)
724 req.master_pref = strtoul(master_pref, NULL, 0);
725
726 if (rand_fac) {
727 int rand_fac_val = strtoul(rand_fac, NULL, 0);
728
729 req.config_random_factor_force = 1;
730 req.random_factor_force_val = rand_fac_val;
731 }
732
733 if (hop_count) {
734 int hop_count_val = strtoul(hop_count, NULL, 0);
735
736 req.config_hop_count_force = 1;
737 req.hop_count_force_val = hop_count_val;
738 }
739
Rakesh Sunki107356c2017-03-30 14:47:55 -0700740 ret = nan_enable_request(0, global_interface_handle, &req);
741 if (ret != WIFI_SUCCESS) {
742 send_resp(dut, conn, SIGMA_ERROR, "Unable to enable nan");
743 return 0;
744 }
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700745
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200746 abstime.tv_sec = 4;
747 abstime.tv_nsec = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200748 wait(abstime);
Kantesh Mundaragi116be192016-10-19 17:10:52 -0700749
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200750 return 0;
751}
752
753
754static int nan_further_availability_tx(struct sigma_dut *dut,
755 struct sigma_conn *conn,
756 struct sigma_cmd *cmd)
757{
758 const char *master_pref = get_param(cmd, "MasterPref");
759 const char *rand_fac = get_param(cmd, "RandFactor");
760 const char *hop_count = get_param(cmd, "HopCount");
Rakesh Sunki107356c2017-03-30 14:47:55 -0700761 wifi_error ret;
762
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200763 NanEnableRequest req;
764 NanConfigRequest configReq;
765
766 memset(&req, 0, sizeof(NanEnableRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200767 req.cluster_low = 0;
768 req.cluster_high = 0xFFFF;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200769 req.master_pref = 30;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200770
771 if (master_pref)
772 req.master_pref = strtoul(master_pref, NULL, 0);
773
774 if (rand_fac) {
775 int rand_fac_val = strtoul(rand_fac, NULL, 0);
776
777 req.config_random_factor_force = 1;
778 req.random_factor_force_val = rand_fac_val;
779 }
780
781 if (hop_count) {
782 int hop_count_val = strtoul(hop_count, NULL, 0);
783
784 req.config_hop_count_force = 1;
785 req.hop_count_force_val = hop_count_val;
786 }
787
Rakesh Sunki107356c2017-03-30 14:47:55 -0700788 ret = nan_enable_request(0, global_interface_handle, &req);
789 if (ret != WIFI_SUCCESS) {
790 send_resp(dut, conn, SIGMA_ERROR, "Unable to enable nan");
791 return 0;
792 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200793
794 /* Start the config of fam */
795
796 memset(&configReq, 0, sizeof(NanConfigRequest));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200797
798 configReq.config_fam = 1;
799 configReq.fam_val.numchans = 1;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700800 configReq.fam_val.famchan[0].entry_control = 0;
801 configReq.fam_val.famchan[0].class_val = 81;
802 configReq.fam_val.famchan[0].channel = 6;
803 configReq.fam_val.famchan[0].mapid = 0;
804 configReq.fam_val.famchan[0].avail_interval_bitmap = 0x7ffffffe;
805
Rakesh Sunki107356c2017-03-30 14:47:55 -0700806 ret = nan_config_request(0, global_interface_handle, &configReq);
807 if (ret != WIFI_SUCCESS)
808 send_resp(dut, conn, SIGMA_ERROR, "Nan config request failed");
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200809
810 return 0;
811}
812
813
814int sigma_nan_transmit_followup(struct sigma_dut *dut,
815 struct sigma_conn *conn,
816 struct sigma_cmd *cmd)
817{
818 const char *mac = get_param(cmd, "mac");
819 const char *requestor_id = get_param(cmd, "RemoteInstanceId");
820 const char *local_id = get_param(cmd, "LocalInstanceId");
821 const char *service_name = get_param(cmd, "servicename");
Rakesh Sunki107356c2017-03-30 14:47:55 -0700822 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200823 NanTransmitFollowupRequest req;
824
825 memset(&req, 0, sizeof(NanTransmitFollowupRequest));
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700826 req.requestor_instance_id = global_match_handle;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200827 req.addr[0] = 0xFF;
828 req.addr[1] = 0xFF;
829 req.addr[2] = 0xFF;
830 req.addr[3] = 0xFF;
831 req.addr[4] = 0xFF;
832 req.addr[5] = 0xFF;
833 req.priority = NAN_TX_PRIORITY_NORMAL;
834 req.dw_or_faw = 0;
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -0700835
836 if (service_name)
837 req.service_specific_info_len = strlen(service_name);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200838
839 if (requestor_id) {
840 /* int requestor_id_val = atoi(requestor_id); */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700841 req.requestor_instance_id = global_match_handle;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200842 }
843 if (local_id) {
844 /* int local_id_val = atoi(local_id); */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700845 req.publish_subscribe_id = global_header_handle;
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200846 }
847
848 if (mac == NULL) {
849 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid MAC Address");
850 return -1;
851 }
852 nan_parse_mac_address(dut, mac, req.addr);
853
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200854 if (requestor_id)
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700855 req.requestor_instance_id = strtoul(requestor_id, NULL, 0);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200856
Rakesh Sunki107356c2017-03-30 14:47:55 -0700857 ret = nan_transmit_followup_request(0, global_interface_handle, &req);
858 if (ret != WIFI_SUCCESS) {
859 send_resp(dut, conn, SIGMA_ERROR,
860 "Unable to complete nan transmit followup");
861 }
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700862
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200863 return 0;
864}
865
Rakesh Sunki107356c2017-03-30 14:47:55 -0700866
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200867/* NotifyResponse invoked to notify the status of the Request */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700868void nan_notify_response(transaction_id id, NanResponseMsg *rsp_data)
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200869{
870 sigma_dut_print(global_dut, DUT_MSG_INFO,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700871 "%s: status %d value %d response_type %d",
872 __func__,
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200873 rsp_data->status, rsp_data->value,
874 rsp_data->response_type);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200875 if (rsp_data->response_type == NAN_RESPONSE_STATS) {
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700876 sigma_dut_print(global_dut, DUT_MSG_INFO, "%s: stats_type %d",
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200877 __func__,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700878 rsp_data->body.stats_response.stats_type);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200879 }
880#if 0
881 if (rsp_data->response_type == NAN_RESPONSE_CONFIG &&
882 rsp_data->status == 0)
883 pthread_cond_signal(&gCondition);
884#endif
885}
886
887
888/* Events Callback */
889void nan_event_publish_replied(NanPublishRepliedInd *event)
890{
891 sigma_dut_print(global_dut, DUT_MSG_INFO,
892 "%s: handle %d " MAC_ADDR_STR " rssi:%d",
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700893 __func__, event->requestor_instance_id,
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200894 MAC_ADDR_ARRAY(event->addr), event->rssi_value);
895 event_anyresponse = 1;
896 snprintf(global_event_resp_buf, sizeof(global_event_resp_buf),
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700897 "EventName,Replied,RemoteInstanceID %d,mac," MAC_ADDR_STR,
898 (event->requestor_instance_id >> 24),
899 MAC_ADDR_ARRAY(event->addr));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200900}
901
902
903/* Events Callback */
904void nan_event_publish_terminated(NanPublishTerminatedInd *event)
905{
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700906 sigma_dut_print(global_dut, DUT_MSG_INFO, "%s: publish_id %d reason %d",
907 __func__, event->publish_id, event->reason);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200908}
909
910
911/* Events Callback */
912void nan_event_match(NanMatchInd *event)
913{
914 sigma_dut_print(global_dut, DUT_MSG_INFO,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700915 "%s: Pub/Sub Id %d remote_requestor_id %08x "
916 MAC_ADDR_STR
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200917 " rssi:%d",
918 __func__,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700919 event->publish_subscribe_id,
920 event->requestor_instance_id,
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200921 MAC_ADDR_ARRAY(event->addr),
922 event->rssi_value);
923 event_anyresponse = 1;
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700924 global_header_handle = event->publish_subscribe_id;
925 global_match_handle = event->requestor_instance_id;
926
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200927 /* memset(event_resp_buf, 0, sizeof(event_resp_buf)); */
928 /* global_pub_sub_handle = event->header.handle; */
929 /* Print the SSI */
930 sigma_dut_print(global_dut, DUT_MSG_INFO, "Printing SSI:");
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700931 nan_hex_dump(global_dut, event->service_specific_info,
932 event->service_specific_info_len);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200933 snprintf(global_event_resp_buf, sizeof(global_event_resp_buf),
934 "EventName,DiscoveryResult,RemoteInstanceID,%d,LocalInstanceID,%d,mac,"
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700935 MAC_ADDR_STR " ", (event->requestor_instance_id >> 24),
936 event->publish_subscribe_id, MAC_ADDR_ARRAY(event->addr));
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200937
938 /* Print the match filter */
939 sigma_dut_print(global_dut, DUT_MSG_INFO, "Printing sdf match filter:");
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700940 nan_hex_dump(global_dut, event->sdf_match_filter,
941 event->sdf_match_filter_len);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200942
943 /* Print the conn_capability */
944 sigma_dut_print(global_dut, DUT_MSG_INFO,
945 "Printing PostConnectivity Capability");
946 if (event->is_conn_capability_valid) {
947 sigma_dut_print(global_dut, DUT_MSG_INFO, "Wfd supported:%s",
948 event->conn_capability.is_wfd_supported ?
949 "yes" : "no");
950 sigma_dut_print(global_dut, DUT_MSG_INFO, "Wfds supported:%s",
951 (event->conn_capability.is_wfds_supported ?
952 "yes" : "no"));
953 sigma_dut_print(global_dut, DUT_MSG_INFO, "TDLS supported:%s",
954 (event->conn_capability.is_tdls_supported ?
955 "yes" : "no"));
956 sigma_dut_print(global_dut, DUT_MSG_INFO, "IBSS supported:%s",
957 (event->conn_capability.is_ibss_supported ?
958 "yes" : "no"));
959 sigma_dut_print(global_dut, DUT_MSG_INFO, "Mesh supported:%s",
960 (event->conn_capability.is_mesh_supported ?
961 "yes" : "no"));
962 sigma_dut_print(global_dut, DUT_MSG_INFO, "Infra Field:%d",
963 event->conn_capability.wlan_infra_field);
964 } else {
965 sigma_dut_print(global_dut, DUT_MSG_INFO,
966 "PostConnectivity Capability not present");
967 }
968
969 /* Print the discovery_attr */
970 sigma_dut_print(global_dut, DUT_MSG_INFO,
971 "Printing PostDiscovery Attribute");
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -0700972 if (event->num_rx_discovery_attr) {
973 int idx;
974
975 for (idx = 0; idx < event->num_rx_discovery_attr; idx++) {
976 sigma_dut_print(global_dut, DUT_MSG_INFO,
977 "PostDiscovery Attribute - %d", idx);
978 sigma_dut_print(global_dut, DUT_MSG_INFO,
979 "Conn Type:%d Device Role:%d"
980 MAC_ADDR_STR,
981 event->discovery_attr[idx].type,
982 event->discovery_attr[idx].role,
983 MAC_ADDR_ARRAY(event->discovery_attr[idx].addr));
984 sigma_dut_print(global_dut, DUT_MSG_INFO,
985 "Duration:%d MapId:%d "
986 "avail_interval_bitmap:%04x",
987 event->discovery_attr[idx].duration,
988 event->discovery_attr[idx].mapid,
989 event->discovery_attr[idx].avail_interval_bitmap);
990 sigma_dut_print(global_dut, DUT_MSG_INFO,
991 "Printing Mesh Id:");
992 nan_hex_dump(global_dut,
993 event->discovery_attr[idx].mesh_id,
994 event->discovery_attr[idx].mesh_id_len);
995 sigma_dut_print(global_dut, DUT_MSG_INFO,
996 "Printing Infrastructure Ssid:");
997 nan_hex_dump(global_dut,
998 event->discovery_attr[idx].infrastructure_ssid_val,
999 event->discovery_attr[idx].infrastructure_ssid_len);
1000 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001001 } else {
1002 sigma_dut_print(global_dut, DUT_MSG_INFO,
1003 "PostDiscovery attribute not present");
1004 }
1005
1006 /* Print the fam */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001007 if (event->num_chans) {
1008 nan_print_further_availability_chan(global_dut,
1009 event->num_chans,
1010 &event->famchan[0]);
1011 } else {
1012 sigma_dut_print(global_dut, DUT_MSG_INFO,
1013 "Further Availability Map not present");
1014 }
1015 if (event->cluster_attribute_len) {
1016 sigma_dut_print(global_dut, DUT_MSG_INFO,
1017 "Printing Cluster Attribute:");
1018 nan_hex_dump(global_dut, event->cluster_attribute,
1019 event->cluster_attribute_len);
1020 } else {
1021 sigma_dut_print(global_dut, DUT_MSG_INFO,
1022 "Cluster Attribute not present");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001023 }
1024}
1025
1026
1027/* Events Callback */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001028void nan_event_match_expired(NanMatchExpiredInd *event)
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001029{
1030 sigma_dut_print(global_dut, DUT_MSG_INFO,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001031 "%s: publish_subscribe_id %d match_handle %08x",
1032 __func__, event->publish_subscribe_id,
1033 event->requestor_instance_id);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001034}
1035
1036
1037/* Events Callback */
1038void nan_event_subscribe_terminated(NanSubscribeTerminatedInd *event)
1039{
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001040 sigma_dut_print(global_dut, DUT_MSG_INFO,
1041 "%s: Subscribe Id %d reason %d",
1042 __func__, event->subscribe_id, event->reason);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001043}
1044
1045
1046/* Events Callback */
1047void nan_event_followup(NanFollowupInd *event)
1048{
1049 sigma_dut_print(global_dut, DUT_MSG_INFO,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001050 "%s: Publish/Subscribe Id %d match_handle 0x%08x dw_or_faw %d "
1051 MAC_ADDR_STR, __func__, event->publish_subscribe_id,
1052 event->requestor_instance_id, event->dw_or_faw,
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001053 MAC_ADDR_ARRAY(event->addr));
1054
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001055 global_match_handle = event->publish_subscribe_id;
1056 global_header_handle = event->requestor_instance_id;
1057 sigma_dut_print(global_dut, DUT_MSG_INFO, "%s: Printing SSI", __func__);
1058 nan_hex_dump(global_dut, event->service_specific_info,
1059 event->service_specific_info_len);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001060 event_anyresponse = 1;
1061 snprintf(global_event_resp_buf, sizeof(global_event_resp_buf),
1062 "EventName,FollowUp,RemoteInstanceID,%d,LocalInstanceID,%d,mac,"
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001063 MAC_ADDR_STR " ", event->requestor_instance_id >> 24,
1064 event->publish_subscribe_id, MAC_ADDR_ARRAY(event->addr));
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001065}
1066
1067
1068/* Events Callback */
1069void nan_event_disceng_event(NanDiscEngEventInd *event)
1070{
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001071 sigma_dut_print(global_dut, DUT_MSG_INFO, "%s: event_type %d",
1072 __func__, event->event_type);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001073
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001074 if (event->event_type == NAN_EVENT_ID_JOINED_CLUSTER) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001075 sigma_dut_print(global_dut, DUT_MSG_INFO, "%s: Joined cluster "
1076 MAC_ADDR_STR,
1077 __func__,
1078 MAC_ADDR_ARRAY(event->data.cluster.addr));
Kantesh Mundaragi116be192016-10-19 17:10:52 -07001079 /* To ensure sta_get_events to get the events
1080 * only after joining the NAN cluster. */
1081 pthread_cond_signal(&gCondition);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001082 }
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001083 if (event->event_type == NAN_EVENT_ID_STARTED_CLUSTER) {
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001084 sigma_dut_print(global_dut, DUT_MSG_INFO,
1085 "%s: Started cluster " MAC_ADDR_STR,
1086 __func__,
1087 MAC_ADDR_ARRAY(event->data.cluster.addr));
1088 }
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001089 if (event->event_type == NAN_EVENT_ID_DISC_MAC_ADDR) {
1090 sigma_dut_print(global_dut, DUT_MSG_INFO,
1091 "%s: Discovery Mac Address "
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001092 MAC_ADDR_STR,
1093 __func__,
1094 MAC_ADDR_ARRAY(event->data.mac_addr.addr));
1095 memcpy(global_nan_mac_addr, event->data.mac_addr.addr,
1096 sizeof(global_nan_mac_addr));
1097 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001098}
1099
1100
1101/* Events Callback */
1102void nan_event_disabled(NanDisabledInd *event)
1103{
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001104 sigma_dut_print(global_dut, DUT_MSG_INFO, "%s: reason %d",
1105 __func__, event->reason);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001106 /* pthread_cond_signal(&gCondition); */
1107}
1108
1109
Rakesh Sunki4c086672017-03-30 14:47:55 -07001110/* Events callback */
1111static void ndp_event_data_indication(NanDataPathRequestInd *event)
1112{
1113 sigma_dut_print(global_dut, DUT_MSG_INFO,
1114 "%s: Service Instance Id: %d Peer Discovery MAC ADDR "
1115 MAC_ADDR_STR
1116 " NDP Instance Id: %d App Info len %d App Info %s",
1117 __func__,
1118 event->service_instance_id,
1119 MAC_ADDR_ARRAY(event->peer_disc_mac_addr),
1120 event->ndp_instance_id,
1121 event->app_info.ndp_app_info_len,
1122 event->app_info.ndp_app_info);
1123
1124 global_ndp_instance_id = event->ndp_instance_id;
1125}
1126
1127
1128/* Events callback */
1129static void ndp_event_data_confirm(NanDataPathConfirmInd *event)
1130{
1131 sigma_dut_print(global_dut, DUT_MSG_INFO,
1132 "Received NDP Confirm Indication");
1133
1134 global_ndp_instance_id = event->ndp_instance_id;
1135 if (system("ifconfig nan0 up") != 0) {
1136 sigma_dut_print(global_dut, DUT_MSG_ERROR,
1137 "Failed to set nan interface up");
1138 return;
1139 }
1140 if (system("ip -6 route add fe80::/64 dev nan0 table local") != 0) {
1141 sigma_dut_print(global_dut, DUT_MSG_ERROR,
1142 "Failed to run:ip -6 route replace fe80::/64 dev nan0 table local");
1143 return;
1144 }
1145}
1146
1147
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001148void * my_thread_function(void *ptr)
1149{
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001150 wifi_event_loop(global_wifi_handle);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001151 pthread_exit(0);
1152 return (void *) NULL;
1153}
1154
1155
1156static NanCallbackHandler callbackHandler = {
1157 .NotifyResponse = nan_notify_response,
1158 .EventPublishReplied = nan_event_publish_replied,
1159 .EventPublishTerminated = nan_event_publish_terminated,
1160 .EventMatch = nan_event_match,
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001161 .EventMatchExpired = nan_event_match_expired,
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001162 .EventSubscribeTerminated = nan_event_subscribe_terminated,
1163 .EventFollowup = nan_event_followup,
1164 .EventDiscEngEvent = nan_event_disceng_event,
1165 .EventDisabled = nan_event_disabled,
Rakesh Sunki4c086672017-03-30 14:47:55 -07001166 .EventDataRequest = ndp_event_data_indication,
1167 .EventDataConfirm = ndp_event_data_confirm,
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001168};
1169
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -07001170
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001171void nan_init(struct sigma_dut *dut)
1172{
1173 pthread_t thread1; /* thread variables */
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001174 wifi_error err = wifi_initialize(&global_wifi_handle);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001175
1176 if (err) {
1177 printf("wifi hal initialize failed\n");
1178 return;
1179 }
1180
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001181 global_interface_handle = wifi_get_iface_handle(global_wifi_handle,
1182 (char *) "wlan0");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001183 /* create threads 1 */
1184 pthread_create(&thread1, NULL, &my_thread_function, NULL);
1185
1186 pthread_mutex_init(&gMutex, NULL);
1187 pthread_cond_init(&gCondition, NULL);
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -07001188 if (global_interface_handle)
1189 nan_register_handler(global_interface_handle, callbackHandler);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001190}
1191
1192
1193void nan_cmd_sta_reset_default(struct sigma_dut *dut, struct sigma_conn *conn,
1194 struct sigma_cmd *cmd)
1195{
1196 sigma_dut_print(dut, DUT_MSG_INFO, "NAN sta_reset_default");
1197
1198 if (nan_state == 0) {
1199 nan_init(dut);
1200 nan_state = 1;
1201 }
1202 is_fam = 0;
1203 event_anyresponse = 0;
1204 global_dut = dut;
Rakesh Sunki7d37f412017-03-30 14:47:55 -07001205 memset(&dut->nan_pmk[0], 0, NAN_PMK_INFO_LEN);
1206 dut->nan_pmk_len = 0;
Rakesh Sunkid7344c02017-03-30 14:47:55 -07001207 dut->sta_channel = 0;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001208 memset(global_event_resp_buf, 0, sizeof(global_event_resp_buf));
Rakesh Sunki7a40a202017-03-30 14:47:55 -07001209
1210 nan_data_interface_delete(0, global_interface_handle, (char *) "nan0");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001211 sigma_nan_disable(dut, conn, cmd);
1212}
1213
1214
1215int nan_cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
1216 struct sigma_cmd *cmd)
1217{
1218 const char *program = get_param(cmd, "Prog");
1219 const char *nan_op = get_param(cmd, "NANOp");
1220 const char *method_type = get_param(cmd, "MethodType");
Rakesh Sunkid7344c02017-03-30 14:47:55 -07001221 const char *band = get_param(cmd, "band");
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001222 char resp_buf[100];
Rakesh Sunki7a40a202017-03-30 14:47:55 -07001223 wifi_error ret;
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001224
1225 if (program == NULL)
1226 return -1;
1227
1228 if (strcasecmp(program, "NAN") != 0) {
1229 send_resp(dut, conn, SIGMA_ERROR,
1230 "ErrorCode,Unsupported program");
1231 return 0;
1232 }
1233
1234 if (nan_op) {
1235 /*
1236 * NANOp has been specified.
1237 * We will build a nan_enable or nan_disable command.
1238 */
1239 if (strcasecmp(nan_op, "On") == 0) {
1240 if (sigma_nan_enable(dut, conn, cmd) == 0) {
Rakesh Sunki7a40a202017-03-30 14:47:55 -07001241 ret = nan_data_interface_create(
1242 0, global_interface_handle,
1243 (char *) "nan0");
1244 if (ret != WIFI_SUCCESS) {
1245 sigma_dut_print(
1246 global_dut, DUT_MSG_ERROR,
1247 "Unable to create NAN data interface");
1248 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001249 snprintf(resp_buf, sizeof(resp_buf), "mac,"
1250 MAC_ADDR_STR,
1251 MAC_ADDR_ARRAY(global_nan_mac_addr));
1252 send_resp(dut, conn, SIGMA_COMPLETE, resp_buf);
1253 } else {
1254 send_resp(dut, conn, SIGMA_ERROR,
1255 "NAN_ENABLE_FAILED");
1256 return -1;
1257 }
Rakesh Sunkid7344c02017-03-30 14:47:55 -07001258
1259 if (band && strcasecmp(band, "24g") == 0) {
1260 sigma_dut_print(dut, DUT_MSG_INFO,
1261 "%s: Setting band to 2G Only",
1262 __func__);
1263 sigma_ndp_configure_band(
1264 dut, conn, cmd,
1265 NAN_DATA_PATH_SUPPORTED_BAND_2G);
1266 } else if (band && dut->sta_channel > 12) {
1267 sigma_ndp_configure_band(
1268 dut, conn, cmd,
1269 NAN_DATA_PATH_SUPPORT_DUAL_BAND);
1270 }
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001271 } else if (strcasecmp(nan_op, "Off") == 0) {
1272 sigma_nan_disable(dut, conn, cmd);
1273 send_resp(dut, conn, SIGMA_COMPLETE, "NULL");
1274 }
1275 }
1276 if (nan_state && nan_op == NULL) {
1277 if (method_type) {
1278 if (strcasecmp(method_type, "Publish") == 0) {
1279 sigma_nan_publish_request(dut, conn, cmd);
1280 send_resp(dut, conn, SIGMA_COMPLETE, "NULL");
1281 }
1282 if (strcasecmp(method_type, "Subscribe") == 0) {
1283 sigma_nan_subscribe_request(dut, conn, cmd);
1284 send_resp(dut, conn, SIGMA_COMPLETE, "NULL");
1285 }
1286 if (strcasecmp(method_type, "Followup") == 0) {
1287 sigma_nan_transmit_followup(dut, conn, cmd);
1288 send_resp(dut, conn, SIGMA_COMPLETE, "NULL");
1289 }
1290 } else {
1291 sigma_nan_config_enable(dut, conn, cmd);
1292 snprintf(resp_buf, sizeof(resp_buf), "mac,"
1293 MAC_ADDR_STR,
1294 MAC_ADDR_ARRAY(global_nan_mac_addr));
1295 send_resp(dut, conn, SIGMA_COMPLETE, resp_buf);
1296 }
1297 }
1298
1299 return 0;
1300}
1301
1302
1303int nan_cmd_sta_get_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
1304 struct sigma_cmd *cmd)
1305{
1306
1307 const char *program = get_param(cmd, "Program");
1308 const char *parameter = get_param(cmd, "Parameter");
1309 char resp_buf[100];
1310 NanStaParameter rsp;
1311
1312 if (program == NULL) {
1313 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid Program Name");
1314 return -1;
1315 }
1316 if (strcasecmp(program, "NAN") != 0) {
1317 send_resp(dut, conn, SIGMA_ERROR,
1318 "ErrorCode,Unsupported program");
1319 return 0;
1320 }
1321
1322 if (parameter == NULL) {
1323 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid Parameter");
1324 return -1;
1325 }
1326 memset(&rsp, 0, sizeof(NanStaParameter));
1327
Amarnath Hullur Subramanyam1854ec62016-08-11 19:29:35 -07001328 nan_get_sta_parameter(0, global_interface_handle, &rsp);
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001329 sigma_dut_print(dut, DUT_MSG_INFO,
1330 "%s: NanStaparameter Master_pref:%02x, Random_factor:%02x, hop_count:%02x beacon_transmit_time:%d",
1331 __func__, rsp.master_pref, rsp.random_factor,
1332 rsp.hop_count, rsp.beacon_transmit_time);
1333
1334 if (strcasecmp(parameter, "MasterPref") == 0) {
1335 snprintf(resp_buf, sizeof(resp_buf), "MasterPref,0x%x",
1336 rsp.master_pref);
1337 } else if (strcasecmp(parameter, "MasterRank") == 0) {
1338 snprintf(resp_buf, sizeof(resp_buf), "MasterRank,0x%lx",
1339 rsp.master_rank);
1340 } else if (strcasecmp(parameter, "RandFactor") == 0) {
1341 snprintf(resp_buf, sizeof(resp_buf), "RandFactor,0x%x",
1342 rsp.random_factor);
1343 } else if (strcasecmp(parameter, "HopCount") == 0) {
1344 snprintf(resp_buf, sizeof(resp_buf), "HopCount,0x%x",
1345 rsp.hop_count);
1346 } else if (strcasecmp(parameter, "BeaconTransTime") == 0) {
1347 snprintf(resp_buf, sizeof(resp_buf), "BeaconTransTime 0x%x",
1348 rsp.beacon_transmit_time);
1349 } else if (strcasecmp(parameter, "NANStatus") == 0) {
1350 if (nan_state == 1)
1351 snprintf(resp_buf, sizeof(resp_buf), "On");
1352 else
1353 snprintf(resp_buf, sizeof(resp_buf), "Off");
1354 } else {
1355 send_resp(dut, conn, SIGMA_ERROR, "Invalid Parameter");
1356 return 0;
1357 }
1358
1359 send_resp(dut, conn, SIGMA_COMPLETE, resp_buf);
1360 return 0;
1361}
1362
1363
1364int nan_cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
1365 struct sigma_cmd *cmd)
1366{
1367 const char *action = get_param(cmd, "Action");
1368
Kantesh Mundaragi132c6d22016-10-28 16:17:50 -07001369 if (!action)
1370 return 0;
1371
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001372 /* Check action for start, stop and get events. */
1373 if (strcasecmp(action, "Start") == 0) {
1374 memset(global_event_resp_buf, 0, sizeof(global_event_resp_buf));
1375 send_resp(dut, conn, SIGMA_COMPLETE, NULL);
1376 } else if (strcasecmp(action, "Stop") == 0) {
1377 event_anyresponse = 0;
1378 memset(global_event_resp_buf, 0, sizeof(global_event_resp_buf));
1379 send_resp(dut, conn, SIGMA_COMPLETE, NULL);
1380 } else if (strcasecmp(action, "Get") == 0) {
1381 if (event_anyresponse == 1) {
1382 send_resp(dut, conn, SIGMA_COMPLETE,
1383 global_event_resp_buf);
1384 } else {
1385 send_resp(dut, conn, SIGMA_COMPLETE, "EventList,NONE");
1386 }
1387 }
1388 return 0;
1389}
Rakesh Sunki4b75f962017-03-30 14:47:55 -07001390
1391#else /* #if NAN_CERT_VERSION */
1392
1393int nan_cmd_sta_preset_testparameters(struct sigma_dut *dut,
1394 struct sigma_conn *conn,
1395 struct sigma_cmd *cmd)
1396{
1397 return 1;
1398}
1399
1400
1401int nan_cmd_sta_get_parameter(struct sigma_dut *dut, struct sigma_conn *conn,
1402 struct sigma_cmd *cmd)
1403{
1404 return 0;
1405
1406}
1407
1408
1409void nan_cmd_sta_reset_default(struct sigma_dut *dut, struct sigma_conn *conn,
1410 struct sigma_cmd *cmd)
1411{
1412 return;
1413}
1414
1415
1416int nan_cmd_sta_get_events(struct sigma_dut *dut, struct sigma_conn *conn,
1417 struct sigma_cmd *cmd)
1418{
1419 return 0;
1420}
1421
1422
1423int nan_cmd_sta_exec_action(struct sigma_dut *dut, struct sigma_conn *conn,
1424 struct sigma_cmd *cmd)
1425{
1426 return 0;
1427}
1428
1429#endif /* #if NAN_CERT_VERSION */