blob: 2ecd4771e3734b2baaaf8f1110a3ff0ca09f1640 [file] [log] [blame]
Jouni Malinencd4e3c32015-10-29 12:39:56 +02001/*
2 * Sigma Control API DUT (station/AP/sniffer)
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -07003 * Copyright (c) 2011-2013, 2017, Qualcomm Atheros, Inc.
Jouni Malinena0bf2442019-02-19 12:03:26 +02004 * Copyright (c) 2018-2019, The Linux Foundation
Jouni Malinencd4e3c32015-10-29 12:39:56 +02005 * All Rights Reserved.
6 * Licensed under the Clear BSD license. See README for more details.
7 */
8
9#include "sigma_dut.h"
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -070010#include <ctype.h>
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -070011#include "miracast.h"
Anurag Das2052daa2018-08-31 14:35:25 +053012#include <sys/wait.h>
13#include "wpa_ctrl.h"
14#include "wpa_helpers.h"
Jouni Malinencd4e3c32015-10-29 12:39:56 +020015
16
Jouni Malinena0bf2442019-02-19 12:03:26 +020017static enum sigma_cmd_result cmd_dev_send_frame(struct sigma_dut *dut,
18 struct sigma_conn *conn,
19 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020020{
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -070021#ifdef MIRACAST
22 const char *program = get_param(cmd, "Program");
23
24 if (program && (strcasecmp(program, "WFD") == 0 ||
25 strcasecmp(program, "DisplayR2") == 0))
26 return miracast_dev_send_frame(dut, conn, cmd);
27#endif /* MIRACAST */
28
Jouni Malinencd4e3c32015-10-29 12:39:56 +020029 if (dut->mode == SIGMA_MODE_STATION ||
30 dut->mode == SIGMA_MODE_UNKNOWN) {
31 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert "
32 "dev_send_frame to sta_send_frame");
33 return cmd_sta_send_frame(dut, conn, cmd);
34 }
35
36 if (dut->mode == SIGMA_MODE_AP) {
37 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert "
38 "dev_send_frame to ap_send_frame");
39 return cmd_ap_send_frame(dut, conn, cmd);
40 }
41
42#ifdef CONFIG_WLANTEST
43 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert dev_send_frame to "
44 "wlantest_send_frame");
45 return cmd_wlantest_send_frame(dut, conn, cmd);
46#else /* CONFIG_WLANTEST */
47 send_resp(dut, conn, SIGMA_ERROR,
48 "errorCode,Unsupported dev_send_frame");
Jouni Malinena0bf2442019-02-19 12:03:26 +020049 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020050#endif /* CONFIG_WLANTEST */
51}
52
53
Jouni Malinena0bf2442019-02-19 12:03:26 +020054static enum sigma_cmd_result cmd_dev_set_parameter(struct sigma_dut *dut,
55 struct sigma_conn *conn,
56 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020057{
58 const char *device = get_param(cmd, "Device");
59
60 if (device && strcasecmp(device, "STA") == 0) {
61 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert "
62 "dev_set_parameter to sta_set_parameter");
63 return cmd_sta_set_parameter(dut, conn, cmd);
64 }
65
Jouni Malinena0bf2442019-02-19 12:03:26 +020066 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020067}
68
69
Jouni Malinen134fe3c2019-06-12 04:16:49 +030070static enum sigma_cmd_result sta_server_cert_trust(struct sigma_dut *dut,
71 struct sigma_conn *conn,
72 const char *val)
73{
74 char buf[100];
75 struct wpa_ctrl *ctrl = NULL;
76 int e;
77 char resp[200];
78 int num_disconnected = 0;
79
80 strlcpy(resp, "ServerCertTrustResult,Accepted", sizeof(resp));
81
Jouni Malinen6a3005f2019-06-13 23:39:34 +030082 if (strcasecmp(val, "Accept") != 0 && strcasecmp(val, "Reject") != 0) {
83 sigma_dut_print(dut, DUT_MSG_INFO,
84 "Unknown ServerCertTrust value '%s'", val);
85 return INVALID_SEND_STATUS;
Jouni Malinen134fe3c2019-06-12 04:16:49 +030086 }
87
88 if (!dut->server_cert_hash[0]) {
89 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +030090 "ServerCertTrustResult,OverrideNotAllowed,Reason,No server certificate stored",
Jouni Malinen134fe3c2019-06-12 04:16:49 +030091 sizeof(resp));
92 goto done;
93 }
94
95 if (dut->sta_tod_policy) {
96 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +030097 "ServerCertTrustResult,OverrideNotAllowed,Reason,TOD policy",
Jouni Malinen134fe3c2019-06-12 04:16:49 +030098 sizeof(resp));
99 goto done;
100 }
101
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300102 if (strcasecmp(val, "Accept") != 0) {
103 strlcpy(resp, "ServerCertTrustResult,Rejected", sizeof(resp));
104 goto done;
105 }
106
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300107 snprintf(buf, sizeof(buf), "hash://server/sha256/%s",
108 dut->server_cert_hash);
109 if (set_network_quoted(get_station_ifname(), dut->infra_network_id,
110 "ca_cert", buf) < 0) {
111 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300112 "ServerCertTrustResult,OverrideNotAllowed,Reason,Could not configure server certificate hash for the network profile",
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300113 sizeof(resp));
114 goto done;
115 }
116
117 wpa_command(get_station_ifname(), "DISCONNECT");
118 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", dut->infra_network_id);
119 if (wpa_command(get_station_ifname(), buf) < 0) {
120 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
121 "network id %d on %s",
122 dut->infra_network_id,
123 get_station_ifname());
124 strlcpy(resp,
125 "ServerCertTrustResult,Accepted,Result,Could not request reconnection",
126 sizeof(resp));
127 goto done;
128 }
129
130 ctrl = open_wpa_mon(get_station_ifname());
131 if (!ctrl)
132 goto done;
133
134 for (e = 0; e < 20; e++) {
135 const char *events[] = {
136 "CTRL-EVENT-EAP-TLS-CERT-ERROR",
137 "CTRL-EVENT-DISCONNECTED",
138 "CTRL-EVENT-CONNECTED",
139 NULL
140 };
141 char buf[1024];
142 int res;
143
144 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
145 if (res < 0) {
146 strlcpy(resp,
147 "ServerCertTrustResult,Accepted,Result,Association did not complete",
148 sizeof(resp));
149 goto done;
150 }
151 sigma_dut_print(dut, DUT_MSG_DEBUG, "Connection event: %s",
152 buf);
153
154 if (strstr(buf, "CTRL-EVENT-EAP-TLS-CERT-ERROR")) {
155 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300156 "ServerCertTrustResult,Accepted,Result,TLS server certificate validation failed with updated profile",
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300157 sizeof(resp));
158 goto done;
159 }
160
161 if (strstr(buf, "CTRL-EVENT-DISCONNECTED")) {
162 num_disconnected++;
163
164 if (num_disconnected > 2) {
165 strlcpy(resp,
166 "ServerCertTrustResult,Accepted,Result,Connection failed",
167 sizeof(resp));
168 goto done;
169 }
170 }
171
172 if (strstr(buf, "CTRL-EVENT-CONNECTED")) {
173 strlcpy(resp,
174 "ServerCertTrustResult,Accepted,Result,Connected",
175 sizeof(resp));
176 break;
177 }
178 }
179
180done:
181 if (ctrl) {
182 wpa_ctrl_detach(ctrl);
183 wpa_ctrl_close(ctrl);
184 }
185
186 send_resp(dut, conn, SIGMA_COMPLETE, resp);
187 return STATUS_SENT;
188}
189
190
Jouni Malinena0bf2442019-02-19 12:03:26 +0200191static enum sigma_cmd_result cmd_dev_exec_action(struct sigma_dut *dut,
192 struct sigma_conn *conn,
193 struct sigma_cmd *cmd)
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700194{
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700195 const char *program = get_param(cmd, "Program");
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300196 const char *val;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700197
Jouni Malinend86e5822017-08-29 03:55:32 +0300198#ifdef MIRACAST
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700199 if (program && (strcasecmp(program, "WFD") == 0 ||
Jouni Malinend86e5822017-08-29 03:55:32 +0300200 strcasecmp(program, "DisplayR2") == 0)) {
201 if (get_param(cmd, "interface") == NULL)
Jouni Malinena0bf2442019-02-19 12:03:26 +0200202 return INVALID_SEND_STATUS;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700203 return miracast_dev_exec_action(dut, conn, cmd);
Jouni Malinend86e5822017-08-29 03:55:32 +0300204 }
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700205#endif /* MIRACAST */
206
Jouni Malinend86e5822017-08-29 03:55:32 +0300207 if (program && strcasecmp(program, "DPP") == 0)
208 return dpp_dev_exec_action(dut, conn, cmd);
209
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300210 val = get_param(cmd, "ServerCertTrust");
211 if (val)
212 return sta_server_cert_trust(dut, conn, val);
213
Jouni Malinena0bf2442019-02-19 12:03:26 +0200214 return ERROR_SEND_STATUS;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700215}
216
217
Jouni Malinena0bf2442019-02-19 12:03:26 +0200218static enum sigma_cmd_result cmd_dev_configure_ie(struct sigma_dut *dut,
219 struct sigma_conn *conn,
220 struct sigma_cmd *cmd)
Jouni Malinen3c367e82017-06-23 17:01:47 +0300221{
222 const char *ie_name = get_param(cmd, "IE_Name");
223 const char *contents = get_param(cmd, "Contents");
224
225 if (!ie_name || !contents)
Jouni Malinena0bf2442019-02-19 12:03:26 +0200226 return INVALID_SEND_STATUS;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300227
228 if (strcasecmp(ie_name, "RSNE") != 0) {
229 send_resp(dut, conn, SIGMA_ERROR,
230 "errorCode,Unsupported IE_Name value");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200231 return STATUS_SENT;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300232 }
233
234 free(dut->rsne_override);
235 dut->rsne_override = strdup(contents);
236
Jouni Malinena0bf2442019-02-19 12:03:26 +0200237 return dut->rsne_override ? SUCCESS_SEND_STATUS : ERROR_SEND_STATUS;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300238}
239
240
Jouni Malinena0bf2442019-02-19 12:03:26 +0200241static enum sigma_cmd_result cmd_dev_ble_action(struct sigma_dut *dut,
242 struct sigma_conn *conn,
243 struct sigma_cmd *cmd)
Anurag Das2052daa2018-08-31 14:35:25 +0530244{
245#ifdef ANDROID
Anurag Das2052daa2018-08-31 14:35:25 +0530246 const char *ble_op = get_param(cmd, "BLEOp");
247 const char *prog = get_param(cmd, "Prog");
248 const char *service_name = get_param(cmd, "ServiceName");
249 const char *ble_role = get_param(cmd, "BLERole");
250 const char *discovery_type = get_param(cmd, "DiscoveryType");
251 const char *msg_type = get_param(cmd, "messagetype");
252 const char *action = get_param(cmd, "action");
253 const char *M2Transmit = get_param(cmd, "M2Transmit");
254 char *argv[17];
255 pid_t pid;
256
257 if (prog && ble_role && action && msg_type) {
258 send_resp(dut, conn, SIGMA_COMPLETE,
259 "OrgID,0x00,TransDataHeader,0x00,BloomFilterElement,NULL");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200260 return STATUS_SENT;
Anurag Das2052daa2018-08-31 14:35:25 +0530261 }
262 if (!ble_op || !prog || !service_name || !ble_role || !discovery_type) {
263 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid arguments");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200264 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530265 }
266
267 if ((strcasecmp(prog, "NAN") != 0)) {
268 sigma_dut_print(dut, DUT_MSG_ERROR, "Program %s not supported",
269 prog);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200270 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530271 }
272
273 if (strcasecmp(ble_role, "seeker") != 0 &&
274 strcasecmp(ble_role, "provider") != 0 &&
275 strcasecmp(ble_role, "browser") != 0) {
276 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid BLERole: %s",
277 ble_role);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200278 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530279 }
280
281 if (strcasecmp(discovery_type, "active") != 0 &&
282 strcasecmp(discovery_type, "passive") != 0) {
283 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid DiscoveryType: %s",
284 discovery_type);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200285 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530286 }
287
288 if (!M2Transmit)
289 M2Transmit = "disable";
290
291 argv[0] = "am";
292 argv[1] = "start";
293 argv[2] = "-n";
294 argv[3] = "org.codeaurora.nanservicediscovery/org.codeaurora.nanservicediscovery.MainActivity";
295 argv[4] = "--es";
296 argv[5] = "service";
297 argv[6] = (char *) service_name;
298 argv[7] = "--es";
299 argv[8] = "role";
300 argv[9] = (char *) ble_role;
301 argv[10] = "--es";
302 argv[11] = "scantype";
303 argv[12] = (char *) discovery_type;
304 argv[13] = "--es";
305 argv[14] = "M2Transmit";
306 argv[15] = (char *) M2Transmit;
307 argv[16] = NULL;
308
309 pid = fork();
310 if (pid == -1) {
311 sigma_dut_print(dut, DUT_MSG_ERROR, "fork: %s",
312 strerror(errno));
Jouni Malinena0bf2442019-02-19 12:03:26 +0200313 return ERROR_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530314 }
315
316 if (pid == 0) {
317 execv("/system/bin/am", argv);
318 sigma_dut_print(dut, DUT_MSG_ERROR, "execv: %s",
319 strerror(errno));
320 exit(0);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200321 return ERROR_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530322 }
323
324 dut->nanservicediscoveryinprogress = 1;
325#endif /* ANDROID */
326
Jouni Malinena0bf2442019-02-19 12:03:26 +0200327 return SUCCESS_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530328}
329
330
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700331/* Runtime ID must contain only numbers */
332static int is_runtime_id_valid(struct sigma_dut *dut, const char *val)
333{
334 int i;
335
336 for (i = 0; val[i] != '\0'; i++) {
337 if (!isdigit(val[i])) {
338 sigma_dut_print(dut, DUT_MSG_DEBUG,
339 "Invalid Runtime_ID %s", val);
340 return 0;
341 }
342 }
343
344 return 1;
345}
346
347
348static int build_log_dir(struct sigma_dut *dut, char *dir, size_t dir_size)
349{
350 int res;
351 const char *vendor;
352 int i;
353
354 vendor = dut->vendor_name ? dut->vendor_name : "Qualcomm";
355
356 if (dut->log_file_dir) {
357 res = snprintf(dir, dir_size, "%s/%s", dut->log_file_dir,
358 vendor);
359 } else {
360#ifdef ANDROID
361 res = snprintf(dir, dir_size, "/data/vendor/wifi/%s",
362 vendor);
363#else /* ANDROID */
364 res = snprintf(dir, dir_size, "/var/log/%s", vendor);
365#endif /* ANDROID */
366 }
367
368 if (res < 0 || res >= dir_size)
369 return -1;
370
371 /* Check for valid vendor name in log dir path since the log dir
372 * (/var/log/vendor) is deleted in dev_stop routine. This check is to
373 * avoid any unintended file deletion.
374 */
375 for (i = 0; vendor[i] != '\0'; i++) {
376 if (!isalpha(vendor[i])) {
377 sigma_dut_print(dut, DUT_MSG_DEBUG,
378 "Invalid char %c in vendor name %s",
379 vendor[i], vendor);
380 return -1;
381 }
382 }
383
384 return 0;
385}
386
387
388/* User has to redirect wpa_supplicant logs to the following file. */
389#ifndef WPA_SUPPLICANT_LOG_FILE
390#define WPA_SUPPLICANT_LOG_FILE "/var/log/supplicant_log/wpa_log.txt"
391#endif /* WPA_SUPPLICANT_LOG_FILE */
Anurag Das2052daa2018-08-31 14:35:25 +0530392
Jouni Malinena0bf2442019-02-19 12:03:26 +0200393static enum sigma_cmd_result cmd_dev_start_test(struct sigma_dut *dut,
394 struct sigma_conn *conn,
395 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700396{
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700397 const char *val;
398 char buf[250];
399 char dir[200];
400 FILE *supp_log;
401 int res;
402
403 val = get_param(cmd, "Runtime_ID");
404 if (!(val && is_runtime_id_valid(dut, val)))
405 return INVALID_SEND_STATUS;
406
407 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
408 return ERROR_SEND_STATUS;
409
410 supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r");
411 if (!supp_log) {
412 sigma_dut_print(dut, DUT_MSG_ERROR,
413 "Failed to open wpa_log file %s",
414 WPA_SUPPLICANT_LOG_FILE);
415 } else {
416 /* Get the wpa_supplicant log file size */
417 if (fseek(supp_log, 0, SEEK_END))
418 sigma_dut_print(dut, DUT_MSG_ERROR,
419 "Failed to get file size for read");
420 else
421 dut->wpa_log_size = ftell(supp_log);
422
423 fclose(supp_log);
424 }
425
426 strlcpy(dut->dev_start_test_runtime_id, val,
427 sizeof(dut->dev_start_test_runtime_id));
428 sigma_dut_print(dut, DUT_MSG_DEBUG, "Runtime_ID %s",
429 dut->dev_start_test_runtime_id);
430
431 run_system_wrapper(dut, "rm -rf %s", dir);
432 run_system_wrapper(dut, "mkdir -p %s", dir);
433
434#ifdef ANDROID
435 run_system_wrapper(dut, "logcat -v time > %s/logcat_%s.txt &",
436 dir, dut->dev_start_test_runtime_id);
437#else /* ANDROID */
438 /* Open log file for sigma_dut logs. This is not needed for Android, as
439 * we are already collecting logcat. */
440 res = snprintf(buf, sizeof(buf), "%s/sigma_%s.txt", dir,
441 dut->dev_start_test_runtime_id);
442 if (res >= 0 && res < sizeof(buf)) {
443 if (dut->log_file_fd)
444 fclose(dut->log_file_fd);
445
446 dut->log_file_fd = fopen(buf, "a");
447 if (!dut->log_file_fd)
448 sigma_dut_print(dut, DUT_MSG_ERROR,
449 "Failed to create sigma_dut log %s",
450 buf);
451 }
452
453 run_system_wrapper(dut, "killall -9 cnss_diag_lite");
454 run_system_wrapper(dut,
455 "cnss_diag_lite -c -x 31 > %s/cnss_diag_id_%s.txt &",
456 dir, dut->dev_start_test_runtime_id);
457#endif /* ANDROID */
458
Jouni Malinena0bf2442019-02-19 12:03:26 +0200459 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700460}
461
462
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700463static int is_allowed_char(char ch)
464{
465 return strchr("./-_", ch) != NULL;
466}
467
468
469static int is_destpath_valid(struct sigma_dut *dut, const char *val)
470{
471 int i;
472
473 for (i = 0; val[i] != '\0'; i++) {
474 if (!(isalnum(val[i]) || is_allowed_char(val[i]))) {
475 sigma_dut_print(dut, DUT_MSG_DEBUG,
476 "Invalid char %c in destpath %s",
477 val[i], val);
478 return 0;
479 }
480 }
481
482 return 1;
483}
484
485
486#ifndef ANDROID
487#define SUPP_LOG_BUFF_SIZE 4 * 1024
488
489static int save_supplicant_log(struct sigma_dut *dut)
490{
491 char dir[200];
492 char buf[300];
493 FILE *wpa_log = NULL;
494 FILE *supp_log;
495 char *buff_ptr = NULL;
496 unsigned int file_size;
497 unsigned int file_size_orig;
498 int status = -1, res;
499
500 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
501 return -1;
502
503 res = snprintf(buf, sizeof(buf), "%s/wpa_supplicant_log_%s.txt", dir,
504 dut->dev_start_test_runtime_id);
505 if (res < 0 || res >= sizeof(buf))
506 return -1;
507
508 supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r");
509 if (!supp_log) {
510 sigma_dut_print(dut, DUT_MSG_ERROR,
511 "Failed to open wpa_log file %s",
512 WPA_SUPPLICANT_LOG_FILE);
513 return -1;
514 }
515
516 /* Get the wpa_supplicant log file size */
517 if (fseek(supp_log, 0, SEEK_END)) {
518 sigma_dut_print(dut, DUT_MSG_ERROR,
519 "Failed to get file size for read");
520 goto exit;
521 }
522 file_size_orig = ftell(supp_log);
523
524 if (file_size_orig < dut->wpa_log_size) {
525 sigma_dut_print(dut, DUT_MSG_ERROR,
526 "file size err, new size %u, old size %u",
527 file_size_orig, dut->wpa_log_size);
528 goto exit;
529 }
530
531 /* Get the wpa_supplicant file size for current test */
532 file_size = file_size_orig - dut->wpa_log_size;
533
534 wpa_log = fopen(buf, "w");
535 if (!wpa_log) {
536 sigma_dut_print(dut, DUT_MSG_ERROR,
537 "Failed to create tmp wpa_log file %s", buf);
538 goto exit;
539 }
540
541 if (fseek(supp_log, dut->wpa_log_size, SEEK_SET)) {
542 sigma_dut_print(dut, DUT_MSG_ERROR,
543 "Failed to set wpa_log file ptr for read");
544 goto exit;
545 }
546
547 buff_ptr = malloc(SUPP_LOG_BUFF_SIZE);
548 if (!buff_ptr) {
549 sigma_dut_print(dut, DUT_MSG_ERROR,
550 "Failed to alloc buffer of size %d",
551 SUPP_LOG_BUFF_SIZE);
552 goto exit;
553 }
554
555 /* Read wpa_supplicant log file in 4K byte chunks */
556 do {
557 unsigned int num_bytes_to_read;
558 unsigned int bytes_read;
559
560 num_bytes_to_read = (file_size > SUPP_LOG_BUFF_SIZE) ?
561 SUPP_LOG_BUFF_SIZE : file_size;
562 bytes_read = fread(buff_ptr, 1, num_bytes_to_read, supp_log);
563 if (!bytes_read) {
564 sigma_dut_print(dut, DUT_MSG_ERROR,
565 "Failed to read wpa_supplicant log");
566 goto exit;
567 }
568 if (bytes_read != num_bytes_to_read) {
569 sigma_dut_print(dut, DUT_MSG_DEBUG,
570 "wpa_supplicant log read err, read %d, num_bytes_to_read %d",
571 bytes_read, num_bytes_to_read);
572 goto exit;
573 }
574 fwrite(buff_ptr, 1, bytes_read, wpa_log);
575 file_size -= bytes_read;
576 } while (file_size > 0);
577 status = 0;
578
579exit:
580 if (wpa_log)
581 fclose(wpa_log);
582 fclose(supp_log);
583 free(buff_ptr);
584
585 return status;
586}
587#endif /* !ANDROID */
588
589
Jouni Malinena0bf2442019-02-19 12:03:26 +0200590static enum sigma_cmd_result cmd_dev_stop_test(struct sigma_dut *dut,
591 struct sigma_conn *conn,
592 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700593{
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700594 const char *val;
595 char buf[300];
596 char out_file[100];
597 char dir[200];
598 int res;
599
600 val = get_param(cmd, "Runtime_ID");
601 if (!val || strcmp(val, dut->dev_start_test_runtime_id) != 0) {
602 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid runtime id");
603 return ERROR_SEND_STATUS;
604 }
605
606 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
607 return ERROR_SEND_STATUS;
608
609#ifdef ANDROID
610 /* Copy all cnss_diag logs to dir */
611 run_system_wrapper(dut, "cp -a /data/vendor/wifi/wlan_logs/* %s", dir);
612#else /* ANDROID */
613 if (dut->log_file_fd) {
614 fclose(dut->log_file_fd);
615 dut->log_file_fd = NULL;
616 }
617 if (save_supplicant_log(dut))
618 sigma_dut_print(dut, DUT_MSG_ERROR,
619 "Failed to save wpa_supplicant log");
620#endif /* ANDROID */
621
622 res = snprintf(out_file, sizeof(out_file), "%s_%s_%s.tar.gz",
623 dut->vendor_name ? dut->vendor_name : "Qualcomm",
624 dut->model_name ? dut->model_name : "Unknown",
625 dut->dev_start_test_runtime_id);
626 if (res < 0 || res >= sizeof(out_file))
627 return ERROR_SEND_STATUS;
628
629 if (run_system_wrapper(dut, "tar -czvf %s/../%s %s", dir, out_file,
630 dir) < 0) {
631 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to create tar: %s",
632 buf);
633 return ERROR_SEND_STATUS;
634 }
635
636 val = get_param(cmd, "destpath");
637 if (!(val && is_destpath_valid(dut, val))) {
638 sigma_dut_print(dut, DUT_MSG_DEBUG, "Invalid path for TFTP %s",
639 val);
640 return ERROR_SEND_STATUS;
641 }
642
643 res = snprintf(buf, sizeof(buf), "tftp %s -c put %s/%s %s/%s",
644 inet_ntoa(conn->addr.sin_addr), dir, out_file, val,
645 out_file);
646 if (res < 0 || res >= sizeof(buf))
647 return ERROR_SEND_STATUS;
648 if (run_system_wrapper(dut, buf) < 0) {
649 sigma_dut_print(dut, DUT_MSG_ERROR,
650 "TFTP file transfer failed: %s", buf);
651 return ERROR_SEND_STATUS;
652 }
653 sigma_dut_print(dut, DUT_MSG_DEBUG, "TFTP file transfer: %s", buf);
654 snprintf(buf, sizeof(buf), "filename,%s", out_file);
655 send_resp(dut, conn, SIGMA_COMPLETE, buf);
656 run_system_wrapper(dut, "rm -f %s/../%s", dir, out_file);
657 run_system_wrapper(dut, "rm -rf %s", dir);
658
Jouni Malinena0bf2442019-02-19 12:03:26 +0200659 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700660}
661
662
Jouni Malinena0bf2442019-02-19 12:03:26 +0200663static enum sigma_cmd_result cmd_dev_get_log(struct sigma_dut *dut,
664 struct sigma_conn *conn,
665 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700666{
Jouni Malinena0bf2442019-02-19 12:03:26 +0200667 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700668}
669
670
Jouni Malinen3c367e82017-06-23 17:01:47 +0300671static int req_intf(struct sigma_cmd *cmd)
672{
673 return get_param(cmd, "interface") == NULL ? -1 : 0;
674}
675
676
Anurag Das2052daa2018-08-31 14:35:25 +0530677static int req_role_svcname(struct sigma_cmd *cmd)
678{
679 if (!get_param(cmd, "BLERole"))
680 return -1;
681 if (get_param(cmd, "BLEOp") && !get_param(cmd, "ServiceName"))
682 return -1;
683 return 0;
684}
685
686
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200687static int req_intf_prog(struct sigma_cmd *cmd)
688{
689 if (get_param(cmd, "interface") == NULL)
690 return -1;
691 if (get_param(cmd, "program") == NULL)
692 return -1;
693 return 0;
694}
695
696
Jouni Malinend86e5822017-08-29 03:55:32 +0300697static int req_prog(struct sigma_cmd *cmd)
698{
699 if (get_param(cmd, "program") == NULL)
700 return -1;
701 return 0;
702}
703
704
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200705void dev_register_cmds(void)
706{
Alexei Avshalom Lazar4a3c2f82019-05-02 13:35:37 +0300707 sigma_dut_reg_cmd("dev_send_frame", req_prog, cmd_dev_send_frame);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200708 sigma_dut_reg_cmd("dev_set_parameter", req_intf_prog,
709 cmd_dev_set_parameter);
Jouni Malinend86e5822017-08-29 03:55:32 +0300710 sigma_dut_reg_cmd("dev_exec_action", req_prog,
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700711 cmd_dev_exec_action);
Jouni Malinen3c367e82017-06-23 17:01:47 +0300712 sigma_dut_reg_cmd("dev_configure_ie", req_intf, cmd_dev_configure_ie);
Peng Xua7ac56e2018-06-26 16:07:19 -0700713 sigma_dut_reg_cmd("dev_start_test", NULL, cmd_dev_start_test);
714 sigma_dut_reg_cmd("dev_stop_test", NULL, cmd_dev_stop_test);
715 sigma_dut_reg_cmd("dev_get_log", NULL, cmd_dev_get_log);
Anurag Das2052daa2018-08-31 14:35:25 +0530716 sigma_dut_reg_cmd("dev_ble_action", req_role_svcname,
717 cmd_dev_ble_action);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200718}