blob: e70b7faeab010823be8ab2cc07a6dccd4112da83 [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
82 if (strcasecmp(val, "Accept") != 0) {
83 strlcpy(resp,
84 "ServerCertTrustResult,NotAccepted,Reason,Unsupported ServerCertTrust value",
85 sizeof(resp));
86 goto done;
87 }
88
89 if (!dut->server_cert_hash[0]) {
90 strlcpy(resp,
91 "ServerCertTrustResult,NotAccepted,Reason,No server certificate stored",
92 sizeof(resp));
93 goto done;
94 }
95
96 if (dut->sta_tod_policy) {
97 strlcpy(resp,
98 "ServerCertTrustResult,NotAccepted,Reason,TOD policy",
99 sizeof(resp));
100 goto done;
101 }
102
103 snprintf(buf, sizeof(buf), "hash://server/sha256/%s",
104 dut->server_cert_hash);
105 if (set_network_quoted(get_station_ifname(), dut->infra_network_id,
106 "ca_cert", buf) < 0) {
107 strlcpy(resp,
108 "ServerCertTrustResult,NotAccepted,Reason,Could not configure server certificate hash for the network profile",
109 sizeof(resp));
110 goto done;
111 }
112
113 wpa_command(get_station_ifname(), "DISCONNECT");
114 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", dut->infra_network_id);
115 if (wpa_command(get_station_ifname(), buf) < 0) {
116 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
117 "network id %d on %s",
118 dut->infra_network_id,
119 get_station_ifname());
120 strlcpy(resp,
121 "ServerCertTrustResult,Accepted,Result,Could not request reconnection",
122 sizeof(resp));
123 goto done;
124 }
125
126 ctrl = open_wpa_mon(get_station_ifname());
127 if (!ctrl)
128 goto done;
129
130 for (e = 0; e < 20; e++) {
131 const char *events[] = {
132 "CTRL-EVENT-EAP-TLS-CERT-ERROR",
133 "CTRL-EVENT-DISCONNECTED",
134 "CTRL-EVENT-CONNECTED",
135 NULL
136 };
137 char buf[1024];
138 int res;
139
140 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
141 if (res < 0) {
142 strlcpy(resp,
143 "ServerCertTrustResult,Accepted,Result,Association did not complete",
144 sizeof(resp));
145 goto done;
146 }
147 sigma_dut_print(dut, DUT_MSG_DEBUG, "Connection event: %s",
148 buf);
149
150 if (strstr(buf, "CTRL-EVENT-EAP-TLS-CERT-ERROR")) {
151 strlcpy(resp,
152 "ServerCertTrustResult,Accepted,Result,TLS server certitficate validation failed with updated profile",
153 sizeof(resp));
154 goto done;
155 }
156
157 if (strstr(buf, "CTRL-EVENT-DISCONNECTED")) {
158 num_disconnected++;
159
160 if (num_disconnected > 2) {
161 strlcpy(resp,
162 "ServerCertTrustResult,Accepted,Result,Connection failed",
163 sizeof(resp));
164 goto done;
165 }
166 }
167
168 if (strstr(buf, "CTRL-EVENT-CONNECTED")) {
169 strlcpy(resp,
170 "ServerCertTrustResult,Accepted,Result,Connected",
171 sizeof(resp));
172 break;
173 }
174 }
175
176done:
177 if (ctrl) {
178 wpa_ctrl_detach(ctrl);
179 wpa_ctrl_close(ctrl);
180 }
181
182 send_resp(dut, conn, SIGMA_COMPLETE, resp);
183 return STATUS_SENT;
184}
185
186
Jouni Malinena0bf2442019-02-19 12:03:26 +0200187static enum sigma_cmd_result cmd_dev_exec_action(struct sigma_dut *dut,
188 struct sigma_conn *conn,
189 struct sigma_cmd *cmd)
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700190{
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700191 const char *program = get_param(cmd, "Program");
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300192 const char *val;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700193
Jouni Malinend86e5822017-08-29 03:55:32 +0300194#ifdef MIRACAST
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700195 if (program && (strcasecmp(program, "WFD") == 0 ||
Jouni Malinend86e5822017-08-29 03:55:32 +0300196 strcasecmp(program, "DisplayR2") == 0)) {
197 if (get_param(cmd, "interface") == NULL)
Jouni Malinena0bf2442019-02-19 12:03:26 +0200198 return INVALID_SEND_STATUS;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700199 return miracast_dev_exec_action(dut, conn, cmd);
Jouni Malinend86e5822017-08-29 03:55:32 +0300200 }
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700201#endif /* MIRACAST */
202
Jouni Malinend86e5822017-08-29 03:55:32 +0300203 if (program && strcasecmp(program, "DPP") == 0)
204 return dpp_dev_exec_action(dut, conn, cmd);
205
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300206 val = get_param(cmd, "ServerCertTrust");
207 if (val)
208 return sta_server_cert_trust(dut, conn, val);
209
Jouni Malinena0bf2442019-02-19 12:03:26 +0200210 return ERROR_SEND_STATUS;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700211}
212
213
Jouni Malinena0bf2442019-02-19 12:03:26 +0200214static enum sigma_cmd_result cmd_dev_configure_ie(struct sigma_dut *dut,
215 struct sigma_conn *conn,
216 struct sigma_cmd *cmd)
Jouni Malinen3c367e82017-06-23 17:01:47 +0300217{
218 const char *ie_name = get_param(cmd, "IE_Name");
219 const char *contents = get_param(cmd, "Contents");
220
221 if (!ie_name || !contents)
Jouni Malinena0bf2442019-02-19 12:03:26 +0200222 return INVALID_SEND_STATUS;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300223
224 if (strcasecmp(ie_name, "RSNE") != 0) {
225 send_resp(dut, conn, SIGMA_ERROR,
226 "errorCode,Unsupported IE_Name value");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200227 return STATUS_SENT;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300228 }
229
230 free(dut->rsne_override);
231 dut->rsne_override = strdup(contents);
232
Jouni Malinena0bf2442019-02-19 12:03:26 +0200233 return dut->rsne_override ? SUCCESS_SEND_STATUS : ERROR_SEND_STATUS;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300234}
235
236
Jouni Malinena0bf2442019-02-19 12:03:26 +0200237static enum sigma_cmd_result cmd_dev_ble_action(struct sigma_dut *dut,
238 struct sigma_conn *conn,
239 struct sigma_cmd *cmd)
Anurag Das2052daa2018-08-31 14:35:25 +0530240{
241#ifdef ANDROID
Anurag Das2052daa2018-08-31 14:35:25 +0530242 const char *ble_op = get_param(cmd, "BLEOp");
243 const char *prog = get_param(cmd, "Prog");
244 const char *service_name = get_param(cmd, "ServiceName");
245 const char *ble_role = get_param(cmd, "BLERole");
246 const char *discovery_type = get_param(cmd, "DiscoveryType");
247 const char *msg_type = get_param(cmd, "messagetype");
248 const char *action = get_param(cmd, "action");
249 const char *M2Transmit = get_param(cmd, "M2Transmit");
250 char *argv[17];
251 pid_t pid;
252
253 if (prog && ble_role && action && msg_type) {
254 send_resp(dut, conn, SIGMA_COMPLETE,
255 "OrgID,0x00,TransDataHeader,0x00,BloomFilterElement,NULL");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200256 return STATUS_SENT;
Anurag Das2052daa2018-08-31 14:35:25 +0530257 }
258 if (!ble_op || !prog || !service_name || !ble_role || !discovery_type) {
259 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid arguments");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200260 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530261 }
262
263 if ((strcasecmp(prog, "NAN") != 0)) {
264 sigma_dut_print(dut, DUT_MSG_ERROR, "Program %s not supported",
265 prog);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200266 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530267 }
268
269 if (strcasecmp(ble_role, "seeker") != 0 &&
270 strcasecmp(ble_role, "provider") != 0 &&
271 strcasecmp(ble_role, "browser") != 0) {
272 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid BLERole: %s",
273 ble_role);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200274 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530275 }
276
277 if (strcasecmp(discovery_type, "active") != 0 &&
278 strcasecmp(discovery_type, "passive") != 0) {
279 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid DiscoveryType: %s",
280 discovery_type);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200281 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530282 }
283
284 if (!M2Transmit)
285 M2Transmit = "disable";
286
287 argv[0] = "am";
288 argv[1] = "start";
289 argv[2] = "-n";
290 argv[3] = "org.codeaurora.nanservicediscovery/org.codeaurora.nanservicediscovery.MainActivity";
291 argv[4] = "--es";
292 argv[5] = "service";
293 argv[6] = (char *) service_name;
294 argv[7] = "--es";
295 argv[8] = "role";
296 argv[9] = (char *) ble_role;
297 argv[10] = "--es";
298 argv[11] = "scantype";
299 argv[12] = (char *) discovery_type;
300 argv[13] = "--es";
301 argv[14] = "M2Transmit";
302 argv[15] = (char *) M2Transmit;
303 argv[16] = NULL;
304
305 pid = fork();
306 if (pid == -1) {
307 sigma_dut_print(dut, DUT_MSG_ERROR, "fork: %s",
308 strerror(errno));
Jouni Malinena0bf2442019-02-19 12:03:26 +0200309 return ERROR_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530310 }
311
312 if (pid == 0) {
313 execv("/system/bin/am", argv);
314 sigma_dut_print(dut, DUT_MSG_ERROR, "execv: %s",
315 strerror(errno));
316 exit(0);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200317 return ERROR_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530318 }
319
320 dut->nanservicediscoveryinprogress = 1;
321#endif /* ANDROID */
322
Jouni Malinena0bf2442019-02-19 12:03:26 +0200323 return SUCCESS_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530324}
325
326
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700327/* Runtime ID must contain only numbers */
328static int is_runtime_id_valid(struct sigma_dut *dut, const char *val)
329{
330 int i;
331
332 for (i = 0; val[i] != '\0'; i++) {
333 if (!isdigit(val[i])) {
334 sigma_dut_print(dut, DUT_MSG_DEBUG,
335 "Invalid Runtime_ID %s", val);
336 return 0;
337 }
338 }
339
340 return 1;
341}
342
343
344static int build_log_dir(struct sigma_dut *dut, char *dir, size_t dir_size)
345{
346 int res;
347 const char *vendor;
348 int i;
349
350 vendor = dut->vendor_name ? dut->vendor_name : "Qualcomm";
351
352 if (dut->log_file_dir) {
353 res = snprintf(dir, dir_size, "%s/%s", dut->log_file_dir,
354 vendor);
355 } else {
356#ifdef ANDROID
357 res = snprintf(dir, dir_size, "/data/vendor/wifi/%s",
358 vendor);
359#else /* ANDROID */
360 res = snprintf(dir, dir_size, "/var/log/%s", vendor);
361#endif /* ANDROID */
362 }
363
364 if (res < 0 || res >= dir_size)
365 return -1;
366
367 /* Check for valid vendor name in log dir path since the log dir
368 * (/var/log/vendor) is deleted in dev_stop routine. This check is to
369 * avoid any unintended file deletion.
370 */
371 for (i = 0; vendor[i] != '\0'; i++) {
372 if (!isalpha(vendor[i])) {
373 sigma_dut_print(dut, DUT_MSG_DEBUG,
374 "Invalid char %c in vendor name %s",
375 vendor[i], vendor);
376 return -1;
377 }
378 }
379
380 return 0;
381}
382
383
384/* User has to redirect wpa_supplicant logs to the following file. */
385#ifndef WPA_SUPPLICANT_LOG_FILE
386#define WPA_SUPPLICANT_LOG_FILE "/var/log/supplicant_log/wpa_log.txt"
387#endif /* WPA_SUPPLICANT_LOG_FILE */
Anurag Das2052daa2018-08-31 14:35:25 +0530388
Jouni Malinena0bf2442019-02-19 12:03:26 +0200389static enum sigma_cmd_result cmd_dev_start_test(struct sigma_dut *dut,
390 struct sigma_conn *conn,
391 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700392{
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700393 const char *val;
394 char buf[250];
395 char dir[200];
396 FILE *supp_log;
397 int res;
398
399 val = get_param(cmd, "Runtime_ID");
400 if (!(val && is_runtime_id_valid(dut, val)))
401 return INVALID_SEND_STATUS;
402
403 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
404 return ERROR_SEND_STATUS;
405
406 supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r");
407 if (!supp_log) {
408 sigma_dut_print(dut, DUT_MSG_ERROR,
409 "Failed to open wpa_log file %s",
410 WPA_SUPPLICANT_LOG_FILE);
411 } else {
412 /* Get the wpa_supplicant log file size */
413 if (fseek(supp_log, 0, SEEK_END))
414 sigma_dut_print(dut, DUT_MSG_ERROR,
415 "Failed to get file size for read");
416 else
417 dut->wpa_log_size = ftell(supp_log);
418
419 fclose(supp_log);
420 }
421
422 strlcpy(dut->dev_start_test_runtime_id, val,
423 sizeof(dut->dev_start_test_runtime_id));
424 sigma_dut_print(dut, DUT_MSG_DEBUG, "Runtime_ID %s",
425 dut->dev_start_test_runtime_id);
426
427 run_system_wrapper(dut, "rm -rf %s", dir);
428 run_system_wrapper(dut, "mkdir -p %s", dir);
429
430#ifdef ANDROID
431 run_system_wrapper(dut, "logcat -v time > %s/logcat_%s.txt &",
432 dir, dut->dev_start_test_runtime_id);
433#else /* ANDROID */
434 /* Open log file for sigma_dut logs. This is not needed for Android, as
435 * we are already collecting logcat. */
436 res = snprintf(buf, sizeof(buf), "%s/sigma_%s.txt", dir,
437 dut->dev_start_test_runtime_id);
438 if (res >= 0 && res < sizeof(buf)) {
439 if (dut->log_file_fd)
440 fclose(dut->log_file_fd);
441
442 dut->log_file_fd = fopen(buf, "a");
443 if (!dut->log_file_fd)
444 sigma_dut_print(dut, DUT_MSG_ERROR,
445 "Failed to create sigma_dut log %s",
446 buf);
447 }
448
449 run_system_wrapper(dut, "killall -9 cnss_diag_lite");
450 run_system_wrapper(dut,
451 "cnss_diag_lite -c -x 31 > %s/cnss_diag_id_%s.txt &",
452 dir, dut->dev_start_test_runtime_id);
453#endif /* ANDROID */
454
Jouni Malinena0bf2442019-02-19 12:03:26 +0200455 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700456}
457
458
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700459static int is_allowed_char(char ch)
460{
461 return strchr("./-_", ch) != NULL;
462}
463
464
465static int is_destpath_valid(struct sigma_dut *dut, const char *val)
466{
467 int i;
468
469 for (i = 0; val[i] != '\0'; i++) {
470 if (!(isalnum(val[i]) || is_allowed_char(val[i]))) {
471 sigma_dut_print(dut, DUT_MSG_DEBUG,
472 "Invalid char %c in destpath %s",
473 val[i], val);
474 return 0;
475 }
476 }
477
478 return 1;
479}
480
481
482#ifndef ANDROID
483#define SUPP_LOG_BUFF_SIZE 4 * 1024
484
485static int save_supplicant_log(struct sigma_dut *dut)
486{
487 char dir[200];
488 char buf[300];
489 FILE *wpa_log = NULL;
490 FILE *supp_log;
491 char *buff_ptr = NULL;
492 unsigned int file_size;
493 unsigned int file_size_orig;
494 int status = -1, res;
495
496 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
497 return -1;
498
499 res = snprintf(buf, sizeof(buf), "%s/wpa_supplicant_log_%s.txt", dir,
500 dut->dev_start_test_runtime_id);
501 if (res < 0 || res >= sizeof(buf))
502 return -1;
503
504 supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r");
505 if (!supp_log) {
506 sigma_dut_print(dut, DUT_MSG_ERROR,
507 "Failed to open wpa_log file %s",
508 WPA_SUPPLICANT_LOG_FILE);
509 return -1;
510 }
511
512 /* Get the wpa_supplicant log file size */
513 if (fseek(supp_log, 0, SEEK_END)) {
514 sigma_dut_print(dut, DUT_MSG_ERROR,
515 "Failed to get file size for read");
516 goto exit;
517 }
518 file_size_orig = ftell(supp_log);
519
520 if (file_size_orig < dut->wpa_log_size) {
521 sigma_dut_print(dut, DUT_MSG_ERROR,
522 "file size err, new size %u, old size %u",
523 file_size_orig, dut->wpa_log_size);
524 goto exit;
525 }
526
527 /* Get the wpa_supplicant file size for current test */
528 file_size = file_size_orig - dut->wpa_log_size;
529
530 wpa_log = fopen(buf, "w");
531 if (!wpa_log) {
532 sigma_dut_print(dut, DUT_MSG_ERROR,
533 "Failed to create tmp wpa_log file %s", buf);
534 goto exit;
535 }
536
537 if (fseek(supp_log, dut->wpa_log_size, SEEK_SET)) {
538 sigma_dut_print(dut, DUT_MSG_ERROR,
539 "Failed to set wpa_log file ptr for read");
540 goto exit;
541 }
542
543 buff_ptr = malloc(SUPP_LOG_BUFF_SIZE);
544 if (!buff_ptr) {
545 sigma_dut_print(dut, DUT_MSG_ERROR,
546 "Failed to alloc buffer of size %d",
547 SUPP_LOG_BUFF_SIZE);
548 goto exit;
549 }
550
551 /* Read wpa_supplicant log file in 4K byte chunks */
552 do {
553 unsigned int num_bytes_to_read;
554 unsigned int bytes_read;
555
556 num_bytes_to_read = (file_size > SUPP_LOG_BUFF_SIZE) ?
557 SUPP_LOG_BUFF_SIZE : file_size;
558 bytes_read = fread(buff_ptr, 1, num_bytes_to_read, supp_log);
559 if (!bytes_read) {
560 sigma_dut_print(dut, DUT_MSG_ERROR,
561 "Failed to read wpa_supplicant log");
562 goto exit;
563 }
564 if (bytes_read != num_bytes_to_read) {
565 sigma_dut_print(dut, DUT_MSG_DEBUG,
566 "wpa_supplicant log read err, read %d, num_bytes_to_read %d",
567 bytes_read, num_bytes_to_read);
568 goto exit;
569 }
570 fwrite(buff_ptr, 1, bytes_read, wpa_log);
571 file_size -= bytes_read;
572 } while (file_size > 0);
573 status = 0;
574
575exit:
576 if (wpa_log)
577 fclose(wpa_log);
578 fclose(supp_log);
579 free(buff_ptr);
580
581 return status;
582}
583#endif /* !ANDROID */
584
585
Jouni Malinena0bf2442019-02-19 12:03:26 +0200586static enum sigma_cmd_result cmd_dev_stop_test(struct sigma_dut *dut,
587 struct sigma_conn *conn,
588 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700589{
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700590 const char *val;
591 char buf[300];
592 char out_file[100];
593 char dir[200];
594 int res;
595
596 val = get_param(cmd, "Runtime_ID");
597 if (!val || strcmp(val, dut->dev_start_test_runtime_id) != 0) {
598 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid runtime id");
599 return ERROR_SEND_STATUS;
600 }
601
602 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
603 return ERROR_SEND_STATUS;
604
605#ifdef ANDROID
606 /* Copy all cnss_diag logs to dir */
607 run_system_wrapper(dut, "cp -a /data/vendor/wifi/wlan_logs/* %s", dir);
608#else /* ANDROID */
609 if (dut->log_file_fd) {
610 fclose(dut->log_file_fd);
611 dut->log_file_fd = NULL;
612 }
613 if (save_supplicant_log(dut))
614 sigma_dut_print(dut, DUT_MSG_ERROR,
615 "Failed to save wpa_supplicant log");
616#endif /* ANDROID */
617
618 res = snprintf(out_file, sizeof(out_file), "%s_%s_%s.tar.gz",
619 dut->vendor_name ? dut->vendor_name : "Qualcomm",
620 dut->model_name ? dut->model_name : "Unknown",
621 dut->dev_start_test_runtime_id);
622 if (res < 0 || res >= sizeof(out_file))
623 return ERROR_SEND_STATUS;
624
625 if (run_system_wrapper(dut, "tar -czvf %s/../%s %s", dir, out_file,
626 dir) < 0) {
627 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to create tar: %s",
628 buf);
629 return ERROR_SEND_STATUS;
630 }
631
632 val = get_param(cmd, "destpath");
633 if (!(val && is_destpath_valid(dut, val))) {
634 sigma_dut_print(dut, DUT_MSG_DEBUG, "Invalid path for TFTP %s",
635 val);
636 return ERROR_SEND_STATUS;
637 }
638
639 res = snprintf(buf, sizeof(buf), "tftp %s -c put %s/%s %s/%s",
640 inet_ntoa(conn->addr.sin_addr), dir, out_file, val,
641 out_file);
642 if (res < 0 || res >= sizeof(buf))
643 return ERROR_SEND_STATUS;
644 if (run_system_wrapper(dut, buf) < 0) {
645 sigma_dut_print(dut, DUT_MSG_ERROR,
646 "TFTP file transfer failed: %s", buf);
647 return ERROR_SEND_STATUS;
648 }
649 sigma_dut_print(dut, DUT_MSG_DEBUG, "TFTP file transfer: %s", buf);
650 snprintf(buf, sizeof(buf), "filename,%s", out_file);
651 send_resp(dut, conn, SIGMA_COMPLETE, buf);
652 run_system_wrapper(dut, "rm -f %s/../%s", dir, out_file);
653 run_system_wrapper(dut, "rm -rf %s", dir);
654
Jouni Malinena0bf2442019-02-19 12:03:26 +0200655 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700656}
657
658
Jouni Malinena0bf2442019-02-19 12:03:26 +0200659static enum sigma_cmd_result cmd_dev_get_log(struct sigma_dut *dut,
660 struct sigma_conn *conn,
661 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700662{
Jouni Malinena0bf2442019-02-19 12:03:26 +0200663 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700664}
665
666
Jouni Malinen3c367e82017-06-23 17:01:47 +0300667static int req_intf(struct sigma_cmd *cmd)
668{
669 return get_param(cmd, "interface") == NULL ? -1 : 0;
670}
671
672
Anurag Das2052daa2018-08-31 14:35:25 +0530673static int req_role_svcname(struct sigma_cmd *cmd)
674{
675 if (!get_param(cmd, "BLERole"))
676 return -1;
677 if (get_param(cmd, "BLEOp") && !get_param(cmd, "ServiceName"))
678 return -1;
679 return 0;
680}
681
682
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200683static int req_intf_prog(struct sigma_cmd *cmd)
684{
685 if (get_param(cmd, "interface") == NULL)
686 return -1;
687 if (get_param(cmd, "program") == NULL)
688 return -1;
689 return 0;
690}
691
692
Jouni Malinend86e5822017-08-29 03:55:32 +0300693static int req_prog(struct sigma_cmd *cmd)
694{
695 if (get_param(cmd, "program") == NULL)
696 return -1;
697 return 0;
698}
699
700
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200701void dev_register_cmds(void)
702{
Alexei Avshalom Lazar4a3c2f82019-05-02 13:35:37 +0300703 sigma_dut_reg_cmd("dev_send_frame", req_prog, cmd_dev_send_frame);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200704 sigma_dut_reg_cmd("dev_set_parameter", req_intf_prog,
705 cmd_dev_set_parameter);
Jouni Malinend86e5822017-08-29 03:55:32 +0300706 sigma_dut_reg_cmd("dev_exec_action", req_prog,
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700707 cmd_dev_exec_action);
Jouni Malinen3c367e82017-06-23 17:01:47 +0300708 sigma_dut_reg_cmd("dev_configure_ie", req_intf, cmd_dev_configure_ie);
Peng Xua7ac56e2018-06-26 16:07:19 -0700709 sigma_dut_reg_cmd("dev_start_test", NULL, cmd_dev_start_test);
710 sigma_dut_reg_cmd("dev_stop_test", NULL, cmd_dev_stop_test);
711 sigma_dut_reg_cmd("dev_get_log", NULL, cmd_dev_get_log);
Anurag Das2052daa2018-08-31 14:35:25 +0530712 sigma_dut_reg_cmd("dev_ble_action", req_role_svcname,
713 cmd_dev_ble_action);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200714}