blob: 356b84111ae9ce9ac6efeeeb6c34f2944b3faa7a [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 Malinenfee3c4a2019-06-20 01:06:26 +030017extern char *sigma_cert_path;
18
19
Jouni Malinena0bf2442019-02-19 12:03:26 +020020static enum sigma_cmd_result cmd_dev_send_frame(struct sigma_dut *dut,
21 struct sigma_conn *conn,
22 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020023{
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -070024#ifdef MIRACAST
25 const char *program = get_param(cmd, "Program");
26
27 if (program && (strcasecmp(program, "WFD") == 0 ||
28 strcasecmp(program, "DisplayR2") == 0))
29 return miracast_dev_send_frame(dut, conn, cmd);
30#endif /* MIRACAST */
31
Jouni Malinencd4e3c32015-10-29 12:39:56 +020032 if (dut->mode == SIGMA_MODE_STATION ||
33 dut->mode == SIGMA_MODE_UNKNOWN) {
34 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert "
35 "dev_send_frame to sta_send_frame");
36 return cmd_sta_send_frame(dut, conn, cmd);
37 }
38
39 if (dut->mode == SIGMA_MODE_AP) {
40 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert "
41 "dev_send_frame to ap_send_frame");
42 return cmd_ap_send_frame(dut, conn, cmd);
43 }
44
45#ifdef CONFIG_WLANTEST
46 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert dev_send_frame to "
47 "wlantest_send_frame");
48 return cmd_wlantest_send_frame(dut, conn, cmd);
49#else /* CONFIG_WLANTEST */
50 send_resp(dut, conn, SIGMA_ERROR,
51 "errorCode,Unsupported dev_send_frame");
Jouni Malinena0bf2442019-02-19 12:03:26 +020052 return STATUS_SENT;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020053#endif /* CONFIG_WLANTEST */
54}
55
56
Jouni Malinena0bf2442019-02-19 12:03:26 +020057static enum sigma_cmd_result cmd_dev_set_parameter(struct sigma_dut *dut,
58 struct sigma_conn *conn,
59 struct sigma_cmd *cmd)
Jouni Malinencd4e3c32015-10-29 12:39:56 +020060{
61 const char *device = get_param(cmd, "Device");
62
63 if (device && strcasecmp(device, "STA") == 0) {
64 sigma_dut_print(dut, DUT_MSG_DEBUG, "Convert "
65 "dev_set_parameter to sta_set_parameter");
66 return cmd_sta_set_parameter(dut, conn, cmd);
67 }
68
Jouni Malinena0bf2442019-02-19 12:03:26 +020069 return INVALID_SEND_STATUS;
Jouni Malinencd4e3c32015-10-29 12:39:56 +020070}
71
72
Jouni Malinen134fe3c2019-06-12 04:16:49 +030073static enum sigma_cmd_result sta_server_cert_trust(struct sigma_dut *dut,
74 struct sigma_conn *conn,
75 const char *val)
76{
Jouni Malinenfee3c4a2019-06-20 01:06:26 +030077 char buf[200];
Jouni Malinen134fe3c2019-06-12 04:16:49 +030078 struct wpa_ctrl *ctrl = NULL;
79 int e;
80 char resp[200];
81 int num_disconnected = 0;
82
83 strlcpy(resp, "ServerCertTrustResult,Accepted", sizeof(resp));
84
Jouni Malinen6a3005f2019-06-13 23:39:34 +030085 if (strcasecmp(val, "Accept") != 0 && strcasecmp(val, "Reject") != 0) {
86 sigma_dut_print(dut, DUT_MSG_INFO,
87 "Unknown ServerCertTrust value '%s'", val);
88 return INVALID_SEND_STATUS;
Jouni Malinen134fe3c2019-06-12 04:16:49 +030089 }
90
Jouni Malinenfee3c4a2019-06-20 01:06:26 +030091 snprintf(buf, sizeof(buf), "%s/uosc-disabled", sigma_cert_path);
92 if (file_exists(buf)) {
93 strlcpy(resp,
94 "ServerCertTrustResult,OverrideNotAllowed,Reason,UOSC disabled on device",
95 sizeof(resp));
96 goto done;
97 }
98
Jouni Malinen134fe3c2019-06-12 04:16:49 +030099 if (!dut->server_cert_hash[0]) {
100 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300101 "ServerCertTrustResult,OverrideNotAllowed,Reason,No server certificate stored",
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300102 sizeof(resp));
103 goto done;
104 }
105
106 if (dut->sta_tod_policy) {
107 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300108 "ServerCertTrustResult,OverrideNotAllowed,Reason,TOD policy",
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300109 sizeof(resp));
110 goto done;
111 }
112
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300113 if (strcasecmp(val, "Accept") != 0) {
114 strlcpy(resp, "ServerCertTrustResult,Rejected", sizeof(resp));
115 goto done;
116 }
117
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300118 snprintf(buf, sizeof(buf), "hash://server/sha256/%s",
119 dut->server_cert_hash);
120 if (set_network_quoted(get_station_ifname(), dut->infra_network_id,
121 "ca_cert", buf) < 0) {
122 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300123 "ServerCertTrustResult,OverrideNotAllowed,Reason,Could not configure server certificate hash for the network profile",
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300124 sizeof(resp));
125 goto done;
126 }
127
128 wpa_command(get_station_ifname(), "DISCONNECT");
129 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", dut->infra_network_id);
130 if (wpa_command(get_station_ifname(), buf) < 0) {
131 sigma_dut_print(dut, DUT_MSG_INFO, "Failed to select "
132 "network id %d on %s",
133 dut->infra_network_id,
134 get_station_ifname());
135 strlcpy(resp,
136 "ServerCertTrustResult,Accepted,Result,Could not request reconnection",
137 sizeof(resp));
138 goto done;
139 }
140
141 ctrl = open_wpa_mon(get_station_ifname());
142 if (!ctrl)
143 goto done;
144
145 for (e = 0; e < 20; e++) {
146 const char *events[] = {
147 "CTRL-EVENT-EAP-TLS-CERT-ERROR",
148 "CTRL-EVENT-DISCONNECTED",
149 "CTRL-EVENT-CONNECTED",
150 NULL
151 };
152 char buf[1024];
153 int res;
154
155 res = get_wpa_cli_events(dut, ctrl, events, buf, sizeof(buf));
156 if (res < 0) {
157 strlcpy(resp,
158 "ServerCertTrustResult,Accepted,Result,Association did not complete",
159 sizeof(resp));
160 goto done;
161 }
162 sigma_dut_print(dut, DUT_MSG_DEBUG, "Connection event: %s",
163 buf);
164
165 if (strstr(buf, "CTRL-EVENT-EAP-TLS-CERT-ERROR")) {
166 strlcpy(resp,
Jouni Malinen6a3005f2019-06-13 23:39:34 +0300167 "ServerCertTrustResult,Accepted,Result,TLS server certificate validation failed with updated profile",
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300168 sizeof(resp));
169 goto done;
170 }
171
172 if (strstr(buf, "CTRL-EVENT-DISCONNECTED")) {
173 num_disconnected++;
174
175 if (num_disconnected > 2) {
176 strlcpy(resp,
177 "ServerCertTrustResult,Accepted,Result,Connection failed",
178 sizeof(resp));
179 goto done;
180 }
181 }
182
183 if (strstr(buf, "CTRL-EVENT-CONNECTED")) {
184 strlcpy(resp,
185 "ServerCertTrustResult,Accepted,Result,Connected",
186 sizeof(resp));
187 break;
188 }
189 }
190
191done:
192 if (ctrl) {
193 wpa_ctrl_detach(ctrl);
194 wpa_ctrl_close(ctrl);
195 }
196
197 send_resp(dut, conn, SIGMA_COMPLETE, resp);
198 return STATUS_SENT;
199}
200
201
Jouni Malinena0bf2442019-02-19 12:03:26 +0200202static enum sigma_cmd_result cmd_dev_exec_action(struct sigma_dut *dut,
203 struct sigma_conn *conn,
204 struct sigma_cmd *cmd)
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700205{
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700206 const char *program = get_param(cmd, "Program");
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300207 const char *val;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700208
Jouni Malinend86e5822017-08-29 03:55:32 +0300209#ifdef MIRACAST
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700210 if (program && (strcasecmp(program, "WFD") == 0 ||
Jouni Malinend86e5822017-08-29 03:55:32 +0300211 strcasecmp(program, "DisplayR2") == 0)) {
212 if (get_param(cmd, "interface") == NULL)
Jouni Malinena0bf2442019-02-19 12:03:26 +0200213 return INVALID_SEND_STATUS;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700214 return miracast_dev_exec_action(dut, conn, cmd);
Jouni Malinend86e5822017-08-29 03:55:32 +0300215 }
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700216#endif /* MIRACAST */
217
Jouni Malinend86e5822017-08-29 03:55:32 +0300218 if (program && strcasecmp(program, "DPP") == 0)
219 return dpp_dev_exec_action(dut, conn, cmd);
220
Jouni Malinen134fe3c2019-06-12 04:16:49 +0300221 val = get_param(cmd, "ServerCertTrust");
222 if (val)
223 return sta_server_cert_trust(dut, conn, val);
224
Jouni Malinena0bf2442019-02-19 12:03:26 +0200225 return ERROR_SEND_STATUS;
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700226}
227
228
Jouni Malinena0bf2442019-02-19 12:03:26 +0200229static enum sigma_cmd_result cmd_dev_configure_ie(struct sigma_dut *dut,
230 struct sigma_conn *conn,
231 struct sigma_cmd *cmd)
Jouni Malinen3c367e82017-06-23 17:01:47 +0300232{
233 const char *ie_name = get_param(cmd, "IE_Name");
234 const char *contents = get_param(cmd, "Contents");
235
236 if (!ie_name || !contents)
Jouni Malinena0bf2442019-02-19 12:03:26 +0200237 return INVALID_SEND_STATUS;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300238
239 if (strcasecmp(ie_name, "RSNE") != 0) {
240 send_resp(dut, conn, SIGMA_ERROR,
241 "errorCode,Unsupported IE_Name value");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200242 return STATUS_SENT;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300243 }
244
245 free(dut->rsne_override);
246 dut->rsne_override = strdup(contents);
247
Jouni Malinena0bf2442019-02-19 12:03:26 +0200248 return dut->rsne_override ? SUCCESS_SEND_STATUS : ERROR_SEND_STATUS;
Jouni Malinen3c367e82017-06-23 17:01:47 +0300249}
250
251
Jouni Malinena0bf2442019-02-19 12:03:26 +0200252static enum sigma_cmd_result cmd_dev_ble_action(struct sigma_dut *dut,
253 struct sigma_conn *conn,
254 struct sigma_cmd *cmd)
Anurag Das2052daa2018-08-31 14:35:25 +0530255{
256#ifdef ANDROID
Anurag Das2052daa2018-08-31 14:35:25 +0530257 const char *ble_op = get_param(cmd, "BLEOp");
258 const char *prog = get_param(cmd, "Prog");
259 const char *service_name = get_param(cmd, "ServiceName");
260 const char *ble_role = get_param(cmd, "BLERole");
261 const char *discovery_type = get_param(cmd, "DiscoveryType");
262 const char *msg_type = get_param(cmd, "messagetype");
263 const char *action = get_param(cmd, "action");
264 const char *M2Transmit = get_param(cmd, "M2Transmit");
265 char *argv[17];
266 pid_t pid;
267
268 if (prog && ble_role && action && msg_type) {
269 send_resp(dut, conn, SIGMA_COMPLETE,
270 "OrgID,0x00,TransDataHeader,0x00,BloomFilterElement,NULL");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200271 return STATUS_SENT;
Anurag Das2052daa2018-08-31 14:35:25 +0530272 }
273 if (!ble_op || !prog || !service_name || !ble_role || !discovery_type) {
274 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid arguments");
Jouni Malinena0bf2442019-02-19 12:03:26 +0200275 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530276 }
277
278 if ((strcasecmp(prog, "NAN") != 0)) {
279 sigma_dut_print(dut, DUT_MSG_ERROR, "Program %s not supported",
280 prog);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200281 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530282 }
283
284 if (strcasecmp(ble_role, "seeker") != 0 &&
285 strcasecmp(ble_role, "provider") != 0 &&
286 strcasecmp(ble_role, "browser") != 0) {
287 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid BLERole: %s",
288 ble_role);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200289 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530290 }
291
292 if (strcasecmp(discovery_type, "active") != 0 &&
293 strcasecmp(discovery_type, "passive") != 0) {
294 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid DiscoveryType: %s",
295 discovery_type);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200296 return INVALID_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530297 }
298
299 if (!M2Transmit)
300 M2Transmit = "disable";
301
302 argv[0] = "am";
303 argv[1] = "start";
304 argv[2] = "-n";
305 argv[3] = "org.codeaurora.nanservicediscovery/org.codeaurora.nanservicediscovery.MainActivity";
306 argv[4] = "--es";
307 argv[5] = "service";
308 argv[6] = (char *) service_name;
309 argv[7] = "--es";
310 argv[8] = "role";
311 argv[9] = (char *) ble_role;
312 argv[10] = "--es";
313 argv[11] = "scantype";
314 argv[12] = (char *) discovery_type;
315 argv[13] = "--es";
316 argv[14] = "M2Transmit";
317 argv[15] = (char *) M2Transmit;
318 argv[16] = NULL;
319
320 pid = fork();
321 if (pid == -1) {
322 sigma_dut_print(dut, DUT_MSG_ERROR, "fork: %s",
323 strerror(errno));
Jouni Malinena0bf2442019-02-19 12:03:26 +0200324 return ERROR_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530325 }
326
327 if (pid == 0) {
328 execv("/system/bin/am", argv);
329 sigma_dut_print(dut, DUT_MSG_ERROR, "execv: %s",
330 strerror(errno));
331 exit(0);
Jouni Malinena0bf2442019-02-19 12:03:26 +0200332 return ERROR_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530333 }
334
335 dut->nanservicediscoveryinprogress = 1;
336#endif /* ANDROID */
337
Jouni Malinena0bf2442019-02-19 12:03:26 +0200338 return SUCCESS_SEND_STATUS;
Anurag Das2052daa2018-08-31 14:35:25 +0530339}
340
341
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700342/* Runtime ID must contain only numbers */
343static int is_runtime_id_valid(struct sigma_dut *dut, const char *val)
344{
345 int i;
346
347 for (i = 0; val[i] != '\0'; i++) {
348 if (!isdigit(val[i])) {
349 sigma_dut_print(dut, DUT_MSG_DEBUG,
350 "Invalid Runtime_ID %s", val);
351 return 0;
352 }
353 }
354
355 return 1;
356}
357
358
359static int build_log_dir(struct sigma_dut *dut, char *dir, size_t dir_size)
360{
361 int res;
362 const char *vendor;
363 int i;
364
365 vendor = dut->vendor_name ? dut->vendor_name : "Qualcomm";
366
367 if (dut->log_file_dir) {
368 res = snprintf(dir, dir_size, "%s/%s", dut->log_file_dir,
369 vendor);
370 } else {
371#ifdef ANDROID
372 res = snprintf(dir, dir_size, "/data/vendor/wifi/%s",
373 vendor);
374#else /* ANDROID */
375 res = snprintf(dir, dir_size, "/var/log/%s", vendor);
376#endif /* ANDROID */
377 }
378
379 if (res < 0 || res >= dir_size)
380 return -1;
381
382 /* Check for valid vendor name in log dir path since the log dir
383 * (/var/log/vendor) is deleted in dev_stop routine. This check is to
384 * avoid any unintended file deletion.
385 */
386 for (i = 0; vendor[i] != '\0'; i++) {
387 if (!isalpha(vendor[i])) {
388 sigma_dut_print(dut, DUT_MSG_DEBUG,
389 "Invalid char %c in vendor name %s",
390 vendor[i], vendor);
391 return -1;
392 }
393 }
394
395 return 0;
396}
397
398
399/* User has to redirect wpa_supplicant logs to the following file. */
400#ifndef WPA_SUPPLICANT_LOG_FILE
401#define WPA_SUPPLICANT_LOG_FILE "/var/log/supplicant_log/wpa_log.txt"
402#endif /* WPA_SUPPLICANT_LOG_FILE */
Anurag Das2052daa2018-08-31 14:35:25 +0530403
Jouni Malinena0bf2442019-02-19 12:03:26 +0200404static enum sigma_cmd_result cmd_dev_start_test(struct sigma_dut *dut,
405 struct sigma_conn *conn,
406 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700407{
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700408 const char *val;
409 char buf[250];
410 char dir[200];
411 FILE *supp_log;
412 int res;
413
414 val = get_param(cmd, "Runtime_ID");
415 if (!(val && is_runtime_id_valid(dut, val)))
416 return INVALID_SEND_STATUS;
417
418 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
419 return ERROR_SEND_STATUS;
420
421 supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r");
422 if (!supp_log) {
423 sigma_dut_print(dut, DUT_MSG_ERROR,
424 "Failed to open wpa_log file %s",
425 WPA_SUPPLICANT_LOG_FILE);
426 } else {
427 /* Get the wpa_supplicant log file size */
428 if (fseek(supp_log, 0, SEEK_END))
429 sigma_dut_print(dut, DUT_MSG_ERROR,
430 "Failed to get file size for read");
431 else
432 dut->wpa_log_size = ftell(supp_log);
433
434 fclose(supp_log);
435 }
436
437 strlcpy(dut->dev_start_test_runtime_id, val,
438 sizeof(dut->dev_start_test_runtime_id));
439 sigma_dut_print(dut, DUT_MSG_DEBUG, "Runtime_ID %s",
440 dut->dev_start_test_runtime_id);
441
442 run_system_wrapper(dut, "rm -rf %s", dir);
443 run_system_wrapper(dut, "mkdir -p %s", dir);
444
445#ifdef ANDROID
446 run_system_wrapper(dut, "logcat -v time > %s/logcat_%s.txt &",
447 dir, dut->dev_start_test_runtime_id);
448#else /* ANDROID */
449 /* Open log file for sigma_dut logs. This is not needed for Android, as
450 * we are already collecting logcat. */
451 res = snprintf(buf, sizeof(buf), "%s/sigma_%s.txt", dir,
452 dut->dev_start_test_runtime_id);
453 if (res >= 0 && res < sizeof(buf)) {
454 if (dut->log_file_fd)
455 fclose(dut->log_file_fd);
456
457 dut->log_file_fd = fopen(buf, "a");
458 if (!dut->log_file_fd)
459 sigma_dut_print(dut, DUT_MSG_ERROR,
460 "Failed to create sigma_dut log %s",
461 buf);
462 }
463
464 run_system_wrapper(dut, "killall -9 cnss_diag_lite");
465 run_system_wrapper(dut,
466 "cnss_diag_lite -c -x 31 > %s/cnss_diag_id_%s.txt &",
467 dir, dut->dev_start_test_runtime_id);
468#endif /* ANDROID */
469
Jouni Malinena0bf2442019-02-19 12:03:26 +0200470 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700471}
472
473
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700474static int is_allowed_char(char ch)
475{
476 return strchr("./-_", ch) != NULL;
477}
478
479
480static int is_destpath_valid(struct sigma_dut *dut, const char *val)
481{
482 int i;
483
484 for (i = 0; val[i] != '\0'; i++) {
485 if (!(isalnum(val[i]) || is_allowed_char(val[i]))) {
486 sigma_dut_print(dut, DUT_MSG_DEBUG,
487 "Invalid char %c in destpath %s",
488 val[i], val);
489 return 0;
490 }
491 }
492
493 return 1;
494}
495
496
497#ifndef ANDROID
498#define SUPP_LOG_BUFF_SIZE 4 * 1024
499
500static int save_supplicant_log(struct sigma_dut *dut)
501{
502 char dir[200];
503 char buf[300];
504 FILE *wpa_log = NULL;
505 FILE *supp_log;
506 char *buff_ptr = NULL;
507 unsigned int file_size;
508 unsigned int file_size_orig;
509 int status = -1, res;
510
511 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
512 return -1;
513
514 res = snprintf(buf, sizeof(buf), "%s/wpa_supplicant_log_%s.txt", dir,
515 dut->dev_start_test_runtime_id);
516 if (res < 0 || res >= sizeof(buf))
517 return -1;
518
519 supp_log = fopen(WPA_SUPPLICANT_LOG_FILE, "r");
520 if (!supp_log) {
521 sigma_dut_print(dut, DUT_MSG_ERROR,
522 "Failed to open wpa_log file %s",
523 WPA_SUPPLICANT_LOG_FILE);
524 return -1;
525 }
526
527 /* Get the wpa_supplicant log file size */
528 if (fseek(supp_log, 0, SEEK_END)) {
529 sigma_dut_print(dut, DUT_MSG_ERROR,
530 "Failed to get file size for read");
531 goto exit;
532 }
533 file_size_orig = ftell(supp_log);
534
535 if (file_size_orig < dut->wpa_log_size) {
536 sigma_dut_print(dut, DUT_MSG_ERROR,
537 "file size err, new size %u, old size %u",
538 file_size_orig, dut->wpa_log_size);
539 goto exit;
540 }
541
542 /* Get the wpa_supplicant file size for current test */
543 file_size = file_size_orig - dut->wpa_log_size;
544
545 wpa_log = fopen(buf, "w");
546 if (!wpa_log) {
547 sigma_dut_print(dut, DUT_MSG_ERROR,
548 "Failed to create tmp wpa_log file %s", buf);
549 goto exit;
550 }
551
552 if (fseek(supp_log, dut->wpa_log_size, SEEK_SET)) {
553 sigma_dut_print(dut, DUT_MSG_ERROR,
554 "Failed to set wpa_log file ptr for read");
555 goto exit;
556 }
557
558 buff_ptr = malloc(SUPP_LOG_BUFF_SIZE);
559 if (!buff_ptr) {
560 sigma_dut_print(dut, DUT_MSG_ERROR,
561 "Failed to alloc buffer of size %d",
562 SUPP_LOG_BUFF_SIZE);
563 goto exit;
564 }
565
566 /* Read wpa_supplicant log file in 4K byte chunks */
567 do {
568 unsigned int num_bytes_to_read;
569 unsigned int bytes_read;
570
571 num_bytes_to_read = (file_size > SUPP_LOG_BUFF_SIZE) ?
572 SUPP_LOG_BUFF_SIZE : file_size;
573 bytes_read = fread(buff_ptr, 1, num_bytes_to_read, supp_log);
574 if (!bytes_read) {
575 sigma_dut_print(dut, DUT_MSG_ERROR,
576 "Failed to read wpa_supplicant log");
577 goto exit;
578 }
579 if (bytes_read != num_bytes_to_read) {
580 sigma_dut_print(dut, DUT_MSG_DEBUG,
581 "wpa_supplicant log read err, read %d, num_bytes_to_read %d",
582 bytes_read, num_bytes_to_read);
583 goto exit;
584 }
585 fwrite(buff_ptr, 1, bytes_read, wpa_log);
586 file_size -= bytes_read;
587 } while (file_size > 0);
588 status = 0;
589
590exit:
591 if (wpa_log)
592 fclose(wpa_log);
593 fclose(supp_log);
594 free(buff_ptr);
595
596 return status;
597}
598#endif /* !ANDROID */
599
600
Jouni Malinena0bf2442019-02-19 12:03:26 +0200601static enum sigma_cmd_result cmd_dev_stop_test(struct sigma_dut *dut,
602 struct sigma_conn *conn,
603 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700604{
Kiran Kumar Lokere0128bf92019-04-08 18:36:16 -0700605 const char *val;
606 char buf[300];
607 char out_file[100];
608 char dir[200];
609 int res;
610
611 val = get_param(cmd, "Runtime_ID");
612 if (!val || strcmp(val, dut->dev_start_test_runtime_id) != 0) {
613 sigma_dut_print(dut, DUT_MSG_ERROR, "Invalid runtime id");
614 return ERROR_SEND_STATUS;
615 }
616
617 if (build_log_dir(dut, dir, sizeof(dir)) < 0)
618 return ERROR_SEND_STATUS;
619
620#ifdef ANDROID
621 /* Copy all cnss_diag logs to dir */
622 run_system_wrapper(dut, "cp -a /data/vendor/wifi/wlan_logs/* %s", dir);
623#else /* ANDROID */
624 if (dut->log_file_fd) {
625 fclose(dut->log_file_fd);
626 dut->log_file_fd = NULL;
627 }
628 if (save_supplicant_log(dut))
629 sigma_dut_print(dut, DUT_MSG_ERROR,
630 "Failed to save wpa_supplicant log");
631#endif /* ANDROID */
632
633 res = snprintf(out_file, sizeof(out_file), "%s_%s_%s.tar.gz",
634 dut->vendor_name ? dut->vendor_name : "Qualcomm",
635 dut->model_name ? dut->model_name : "Unknown",
636 dut->dev_start_test_runtime_id);
637 if (res < 0 || res >= sizeof(out_file))
638 return ERROR_SEND_STATUS;
639
640 if (run_system_wrapper(dut, "tar -czvf %s/../%s %s", dir, out_file,
641 dir) < 0) {
642 sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to create tar: %s",
643 buf);
644 return ERROR_SEND_STATUS;
645 }
646
647 val = get_param(cmd, "destpath");
648 if (!(val && is_destpath_valid(dut, val))) {
649 sigma_dut_print(dut, DUT_MSG_DEBUG, "Invalid path for TFTP %s",
650 val);
651 return ERROR_SEND_STATUS;
652 }
653
654 res = snprintf(buf, sizeof(buf), "tftp %s -c put %s/%s %s/%s",
655 inet_ntoa(conn->addr.sin_addr), dir, out_file, val,
656 out_file);
657 if (res < 0 || res >= sizeof(buf))
658 return ERROR_SEND_STATUS;
659 if (run_system_wrapper(dut, buf) < 0) {
660 sigma_dut_print(dut, DUT_MSG_ERROR,
661 "TFTP file transfer failed: %s", buf);
662 return ERROR_SEND_STATUS;
663 }
664 sigma_dut_print(dut, DUT_MSG_DEBUG, "TFTP file transfer: %s", buf);
665 snprintf(buf, sizeof(buf), "filename,%s", out_file);
666 send_resp(dut, conn, SIGMA_COMPLETE, buf);
667 run_system_wrapper(dut, "rm -f %s/../%s", dir, out_file);
668 run_system_wrapper(dut, "rm -rf %s", dir);
669
Jouni Malinena0bf2442019-02-19 12:03:26 +0200670 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700671}
672
673
Jouni Malinena0bf2442019-02-19 12:03:26 +0200674static enum sigma_cmd_result cmd_dev_get_log(struct sigma_dut *dut,
675 struct sigma_conn *conn,
676 struct sigma_cmd *cmd)
Peng Xua7ac56e2018-06-26 16:07:19 -0700677{
Jouni Malinena0bf2442019-02-19 12:03:26 +0200678 return SUCCESS_SEND_STATUS;
Peng Xua7ac56e2018-06-26 16:07:19 -0700679}
680
681
Jouni Malinen3c367e82017-06-23 17:01:47 +0300682static int req_intf(struct sigma_cmd *cmd)
683{
684 return get_param(cmd, "interface") == NULL ? -1 : 0;
685}
686
687
Anurag Das2052daa2018-08-31 14:35:25 +0530688static int req_role_svcname(struct sigma_cmd *cmd)
689{
690 if (!get_param(cmd, "BLERole"))
691 return -1;
692 if (get_param(cmd, "BLEOp") && !get_param(cmd, "ServiceName"))
693 return -1;
694 return 0;
695}
696
697
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200698static int req_intf_prog(struct sigma_cmd *cmd)
699{
700 if (get_param(cmd, "interface") == NULL)
701 return -1;
702 if (get_param(cmd, "program") == NULL)
703 return -1;
704 return 0;
705}
706
707
Jouni Malinend86e5822017-08-29 03:55:32 +0300708static int req_prog(struct sigma_cmd *cmd)
709{
710 if (get_param(cmd, "program") == NULL)
711 return -1;
712 return 0;
713}
714
715
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200716void dev_register_cmds(void)
717{
Alexei Avshalom Lazar4a3c2f82019-05-02 13:35:37 +0300718 sigma_dut_reg_cmd("dev_send_frame", req_prog, cmd_dev_send_frame);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200719 sigma_dut_reg_cmd("dev_set_parameter", req_intf_prog,
720 cmd_dev_set_parameter);
Jouni Malinend86e5822017-08-29 03:55:32 +0300721 sigma_dut_reg_cmd("dev_exec_action", req_prog,
Amarnath Hullur Subramanyam9c381f52017-03-17 00:04:41 -0700722 cmd_dev_exec_action);
Jouni Malinen3c367e82017-06-23 17:01:47 +0300723 sigma_dut_reg_cmd("dev_configure_ie", req_intf, cmd_dev_configure_ie);
Peng Xua7ac56e2018-06-26 16:07:19 -0700724 sigma_dut_reg_cmd("dev_start_test", NULL, cmd_dev_start_test);
725 sigma_dut_reg_cmd("dev_stop_test", NULL, cmd_dev_stop_test);
726 sigma_dut_reg_cmd("dev_get_log", NULL, cmd_dev_get_log);
Anurag Das2052daa2018-08-31 14:35:25 +0530727 sigma_dut_reg_cmd("dev_ble_action", req_role_svcname,
728 cmd_dev_ble_action);
Jouni Malinencd4e3c32015-10-29 12:39:56 +0200729}