Skip to content

Commit 0b01b07

Browse files
committed
[protocol] Convert console to use the new compact error format
Signed-off-by: Ellis Sarza-Nguyen <sarzanguyen@google.com>
1 parent 22b6d09 commit 0b01b07

7 files changed

Lines changed: 377 additions & 94 deletions

File tree

examples/htool.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,17 @@ static int command_console(const struct htool_invocation* inv) {
571571
return -1;
572572
}
573573

574+
libhoth_error err = HOTH_SUCCESS;
574575
if (opts.snapshot) {
575-
return htool_console_snapshot(dev, &opts);
576+
err = htool_console_snapshot(dev, &opts);
576577
} else {
577-
return htool_console_run(dev, &opts);
578+
err = htool_console_run(dev, &opts);
578579
}
580+
if (err != HOTH_SUCCESS) {
581+
htool_report_error("console", err);
582+
return -1;
583+
}
584+
return 0;
579585
}
580586

581587
static int command_flash_spi_info(const struct htool_invocation* inv) {

examples/htool_console.c

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,27 @@ void restore_terminal(int fd, const struct termios* old_termios) {
6161
tcsetattr(fd, TCSANOW, old_termios);
6262
}
6363

64-
int htool_console_run(struct libhoth_device* dev,
65-
const struct libhoth_htool_console_opts* opts) {
64+
libhoth_error htool_console_run(struct libhoth_device* dev,
65+
const struct libhoth_htool_console_opts* opts) {
6666
printf("%sStarting Interactive Console\n", kAnsiRed);
6767

6868
struct hoth_channel_uart_config uart_config = {};
69-
int status = libhoth_get_uart_config(dev, opts, &uart_config);
70-
if (status == LIBHOTH_OK) {
69+
libhoth_error status = libhoth_get_uart_config(dev, opts, &uart_config);
70+
if (status == HOTH_SUCCESS) {
7171
if (opts->baud_rate != 0) {
7272
uart_config.baud_rate = opts->baud_rate;
7373
status = libhoth_set_uart_config(dev, opts, &uart_config);
74-
if (status != LIBHOTH_OK) {
75-
fprintf(
76-
stderr,
77-
"libhoth_set_uart_config() failed: %d; unable to set baud-rate\n",
78-
status);
74+
if (status != HOTH_SUCCESS) {
75+
fprintf(stderr,
76+
"libhoth_set_uart_config() failed: 0x%016llx; unable to set "
77+
"baud-rate\n",
78+
(unsigned long long)status);
7979
return status;
8080
}
8181
status = libhoth_get_uart_config(dev, opts, &uart_config);
8282
}
8383
}
84-
if (status == LIBHOTH_OK) {
84+
if (status == HOTH_SUCCESS) {
8585
printf("Using baud-rate %d\n", uart_config.baud_rate);
8686
}
8787
printf("[ Use Ctrl+T-Q to quit ]%s\n", kAnsiReset);
@@ -90,8 +90,9 @@ int htool_console_run(struct libhoth_device* dev,
9090
// will be stored at this offset)
9191
uint32_t offset;
9292
status = libhoth_get_channel_status(dev, opts, &offset);
93-
if (status != LIBHOTH_OK) {
94-
fprintf(stderr, "libhoth_get_channel_status() failed: %d\n", status);
93+
if (status != HOTH_SUCCESS) {
94+
fprintf(stderr, "libhoth_get_channel_status() failed: 0x%016llx\n",
95+
(unsigned long long)status);
9596
return status;
9697
}
9798

@@ -106,15 +107,15 @@ int htool_console_run(struct libhoth_device* dev,
106107

107108
while (!quit) {
108109
// Any previous failure should reset all of the USB state before retrying
109-
while (status != LIBHOTH_OK) {
110+
while (status != HOTH_SUCCESS) {
110111
// TODO: Read STDIN during this time and buffer it so we can capture
111112
// quit events even when the console is disconnected. We will also want
112113
// to tune the reconnect time to match.
113114
if (libhoth_device_reconnect(dev) != HOTH_SUCCESS) {
114115
// Make sure we don't end up in a tight retry loop
115116
usleep(100 * 1000);
116117
} else {
117-
status = LIBHOTH_OK;
118+
status = HOTH_SUCCESS;
118119
}
119120
}
120121
// Give an opportunity for other clients to use the interface.
@@ -123,20 +124,21 @@ int htool_console_run(struct libhoth_device* dev,
123124
if (libhoth_claim_device(dev, 1000 * 1000 * opts->claim_timeout_secs) !=
124125
HOTH_SUCCESS) {
125126
// If USB is down we might fail claim, just go back and retry
126-
status = LIBHOTH_ERR_FAIL;
127+
status = LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH,
128+
LIBHOTH_ERR_FAIL);
127129
continue;
128130
}
129131

130132
status = libhoth_read_console(dev, STDOUT_FILENO, false, opts->channel_id,
131133
&offset);
132-
if (status != LIBHOTH_OK) {
134+
if (status != HOTH_SUCCESS) {
133135
// Device resets cause failures, just loop and allow reconnection
134136
continue;
135137
}
136138

137139
status = libhoth_write_console(dev, opts->channel_id, opts->force_drive_tx,
138140
&quit);
139-
if (status != LIBHOTH_OK) {
141+
if (status != HOTH_SUCCESS) {
140142
// Device resets cause failures, just loop and allow reconnection
141143
continue;
142144
}
@@ -147,12 +149,14 @@ int htool_console_run(struct libhoth_device* dev,
147149
return status;
148150
}
149151

150-
int htool_console_snapshot_legacy(struct libhoth_device* dev) {
152+
libhoth_error htool_console_snapshot_legacy(struct libhoth_device* dev) {
151153
size_t response_bytes_written;
152-
int status = libhoth_hostcmd_exec(dev, HOTH_CMD_CONSOLE_REQUEST, 0, NULL, 0,
153-
NULL, 0, &response_bytes_written);
154-
if (status != LIBHOTH_OK) {
155-
fprintf(stderr, "HOTH_CMD_CONSOLE_REQUEST status: %d\n", status);
154+
libhoth_error status =
155+
libhoth_hostcmd_exec_v2(dev, HOTH_CMD_CONSOLE_REQUEST, 0, NULL, 0, NULL,
156+
0, &response_bytes_written);
157+
if (status != HOTH_SUCCESS) {
158+
fprintf(stderr, "HOTH_CMD_CONSOLE_REQUEST status: 0x%016llx\n",
159+
(unsigned long long)status);
156160
return status;
157161
}
158162

@@ -162,11 +166,12 @@ int htool_console_snapshot_legacy(struct libhoth_device* dev) {
162166
MAILBOX_SIZE - sizeof(struct hoth_host_response);
163167
while (true) {
164168
char buf[MAILBOX_SIZE];
165-
status = libhoth_hostcmd_exec(dev, HOTH_CMD_CONSOLE_READ, 0, &read_request,
166-
sizeof(read_request), buf, max_bytes_per_read,
167-
&response_bytes_written);
168-
if (status != LIBHOTH_OK) {
169-
fprintf(stderr, "HOTH_CMD_CONSOLE_READ status: %d\n", status);
169+
status = libhoth_hostcmd_exec_v2(
170+
dev, HOTH_CMD_CONSOLE_READ, 0, &read_request, sizeof(read_request), buf,
171+
max_bytes_per_read, &response_bytes_written);
172+
if (status != HOTH_SUCCESS) {
173+
fprintf(stderr, "HOTH_CMD_CONSOLE_READ status: 0x%016llx\n",
174+
(unsigned long long)status);
170175
return status;
171176
}
172177
fwrite(buf, strnlen(buf, sizeof(buf)), 1, stdout);
@@ -176,8 +181,8 @@ int htool_console_snapshot_legacy(struct libhoth_device* dev) {
176181
return status;
177182
}
178183

179-
int htool_console_snapshot(struct libhoth_device* dev,
180-
const struct libhoth_htool_console_opts* opts) {
184+
libhoth_error htool_console_snapshot(
185+
struct libhoth_device* dev, const struct libhoth_htool_console_opts* opts) {
181186
// Legacy host commands for console snapshot.
182187
if (!opts->channel_id) {
183188
return htool_console_snapshot_legacy(dev);
@@ -186,17 +191,18 @@ int htool_console_snapshot(struct libhoth_device* dev,
186191
// Starting from current_offset - 0x80000000 so it's guaranteed to be outside
187192
// of the Hoth buffer. Repeat read until reaching current offset.
188193
uint32_t current_offset;
189-
int status = libhoth_get_channel_status(dev, opts, &current_offset);
190-
if (status != LIBHOTH_OK) {
191-
fprintf(stderr, "libhoth_get_channel_status) failed: %d\n", status);
194+
libhoth_error status = libhoth_get_channel_status(dev, opts, &current_offset);
195+
if (status != HOTH_SUCCESS) {
196+
fprintf(stderr, "libhoth_get_channel_status failed: 0x%016llx\n",
197+
(unsigned long long)status);
192198
return status;
193199
}
194200
uint32_t offset = current_offset - 0x80000000;
195201

196202
while (true) {
197203
status = libhoth_read_console(dev, STDOUT_FILENO, false, opts->channel_id,
198204
&offset);
199-
if (status != LIBHOTH_OK) {
205+
if (status != HOTH_SUCCESS) {
200206
break;
201207
}
202208
// Extra check in case UINT32_MAX wrap-around.

examples/htool_console.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ extern "C" {
2727

2828
struct libhoth_device;
2929

30-
int htool_console_run(struct libhoth_device* dev,
31-
const struct libhoth_htool_console_opts* opts);
30+
libhoth_error htool_console_run(struct libhoth_device* dev,
31+
const struct libhoth_htool_console_opts* opts);
3232

33-
int htool_console_snapshot(struct libhoth_device* dev,
34-
const struct libhoth_htool_console_opts* opts);
33+
libhoth_error htool_console_snapshot(
34+
struct libhoth_device* dev, const struct libhoth_htool_console_opts* opts);
3535

3636
#ifdef __cplusplus
3737
}

protocol/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,26 @@ cc_library(
542542
hdrs = ["console.h"],
543543
deps = [
544544
":host_cmd",
545+
":libhoth_status",
545546
":util",
546547
"//transports:libhoth_device",
547548
],
548549
)
549550

551+
cc_test(
552+
name = "console_test",
553+
srcs = ["console_test.cc"],
554+
deps = [
555+
":console",
556+
":host_cmd",
557+
":libhoth_status",
558+
"//protocol/test:libhoth_device_mock",
559+
"//transports:libhoth_device",
560+
"@googletest//:gtest",
561+
"@googletest//:gtest_main",
562+
],
563+
)
564+
550565
cc_library(
551566
name = "dfu_hostcmd",
552567
srcs = ["dfu_hostcmd.c"],

0 commit comments

Comments
 (0)