Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions examples/htool_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,29 @@ int htool_console_run(struct libhoth_device* dev,
// buffer is much smaller than 2GB).
if (opts->history) offset -= 0x80000000;
bool quit = false;
libhoth_error transport_err = HOTH_SUCCESS;

while (!quit) {
// Any previous failure should reset all of the USB state before retrying
while (status != LIBHOTH_OK) {
while (status != LIBHOTH_OK || transport_err != HOTH_SUCCESS) {
// TODO: Read STDIN during this time and buffer it so we can capture
// quit events even when the console is disconnected. We will also want
// to tune the reconnect time to match.
status = libhoth_device_reconnect(dev);
transport_err = libhoth_device_reconnect(dev);
// If USB is down we might fail reconnect, just retry
if (status != LIBHOTH_OK) {
if (transport_err != HOTH_SUCCESS) {
// Make sure we don't end up in a tight retry loop
usleep(100 * 1000);
} else {
status = LIBHOTH_OK;
}
}
// Give an opportunity for other clients to use the interface.
libhoth_release_device(dev);
usleep(1000 * opts->yield_ms);
status = libhoth_claim_device(dev, 1000 * 1000 * opts->claim_timeout_secs);
if (status != LIBHOTH_OK) {
transport_err =
libhoth_claim_device(dev, 1000 * 1000 * opts->claim_timeout_secs);
if (transport_err != HOTH_SUCCESS) {
// If USB is down we might fail claim, just go back and retry
continue;
}
Expand Down
7 changes: 4 additions & 3 deletions examples/htool_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ struct libhoth_device* htool_libhoth_usb_device(void) {
.prng_seed = prng_seed,
.timeout_us = timeout_us};

int rv = libhoth_usb_open(&opts, &result);
if (rv != LIBHOTH_OK) {
fprintf(stderr, "libhoth_usb_open failed: %d\n", rv);
libhoth_error rv = libhoth_usb_open(&opts, &result);
if (rv != HOTH_SUCCESS) {
fprintf(stderr, "libhoth_usb_open failed: 0x%016llx\n",
(unsigned long long)rv);
return NULL;
}

Expand Down
10 changes: 5 additions & 5 deletions protocol/authz_record_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ TEST_F(LibHothTest, authz_erase_test) {
UsesCommand(HOTH_CMD_BOARD_SPECIFIC_BASE +
HOTH_PRV_CMD_HOTH_SET_AUTHZ_RECORD),
_))
.WillOnce(Return(LIBHOTH_OK));
.WillOnce(Return(HOTH_SUCCESS));

libhoth_error mock_err = LIBHOTH_ERR_CONSTRUCT(
HOTH_CTX_USB, HOTH_HOST_SPACE_LIBHOTH, LIBHOTH_ERR_TIMEOUT);
EXPECT_CALL(mock_, receive)
.WillOnce(DoAll(CopyResp(&dummy, 0), Return(LIBHOTH_ERR_TIMEOUT)));
.WillOnce(DoAll(CopyResp(&dummy, 0), Return(mock_err)));

EXPECT_EQ(libhoth_authz_record_erase(&hoth_dev_),
LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH,
LIBHOTH_ERR_TIMEOUT));
EXPECT_EQ(libhoth_authz_record_erase(&hoth_dev_), mock_err);
}

TEST_F(LibHothTest, authz_read_test) {
Expand Down
7 changes: 1 addition & 6 deletions protocol/dfu_hostcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,5 @@ libhoth_error libhoth_dfu_update(struct libhoth_device* dev,

// TODO: Wait for chip to come back and confirm version
usleep(LIBHOTH_REBOOT_DELAY_MS * 1000);
int ret = libhoth_device_reconnect(dev);
if (ret != LIBHOTH_OK) {
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH,
ret);
}
return HOTH_SUCCESS;
return libhoth_device_reconnect(dev);
}
7 changes: 1 addition & 6 deletions protocol/firmware_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,5 @@ libhoth_error libhoth_firmware_update_from_flash_and_reset(
"Lost connection after firmware update command (error code 0x%016lx). "
"This is expected if the device reset. Attempting to reconnect...\n",
err);
int ret = libhoth_device_reconnect(dev);
if (ret != LIBHOTH_OK) {
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH,
ret);
}
return HOTH_SUCCESS;
return libhoth_device_reconnect(dev);
}
39 changes: 19 additions & 20 deletions protocol/host_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,18 @@ libhoth_error libhoth_hostcmd_exec_v2(struct libhoth_device* dev,
if (req_payload) {
memcpy(req.payload_buf, req_payload, req_payload_size);
}
int status = populate_ec_request_header(command, version, req.payload_buf,
req_payload_size, &req.hdr);
if (status != 0) {
fprintf(stderr, "populate_ec_request_header() failed: %d\n", status);
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_POSIX,
-status);
int rc = populate_ec_request_header(command, version, req.payload_buf,
req_payload_size, &req.hdr);
if (rc != 0) {
fprintf(stderr, "populate_ec_request_header() failed: %d\n", rc);
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_POSIX, -rc);
}
status = libhoth_send_request(dev, &req, sizeof(req.hdr) + req_payload_size);
if (status != LIBHOTH_OK) {
fprintf(stderr, "libhoth_send_request() failed: %d\n", status);
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH,
status);
libhoth_error status =
libhoth_send_request(dev, &req, sizeof(req.hdr) + req_payload_size);
if (status != HOTH_SUCCESS) {
fprintf(stderr, "libhoth_send_request() failed: 0x%016llx\n",
(unsigned long long)status);
return status;
}
struct {
struct hoth_host_response hdr;
Expand All @@ -238,16 +238,15 @@ libhoth_error libhoth_hostcmd_exec_v2(struct libhoth_device* dev,
size_t resp_size = 0;
status = libhoth_receive_response(dev, &resp, sizeof(resp), &resp_size,
HOTH_CMD_TIMEOUT_MS_DEFAULT);
if (status != LIBHOTH_OK) {
fprintf(stderr, "libhoth_receive_response() failed: %d\n", status);
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_LIBHOTH,
status);
if (status != HOTH_SUCCESS) {
fprintf(stderr, "libhoth_receive_response() failed: 0x%016llx\n",
(unsigned long long)status);
return status;
}
status = validate_ec_response_header(&resp.hdr, resp.payload_buf, resp_size);
if (status != 0) {
fprintf(stderr, "EC response header invalid: %d\n", status);
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_POSIX,
-status);
rc = validate_ec_response_header(&resp.hdr, resp.payload_buf, resp_size);
if (rc != 0) {
fprintf(stderr, "EC response header invalid: %d\n", rc);
return LIBHOTH_ERR_CONSTRUCT(HOTH_CTX_CMD_EXEC, HOTH_HOST_SPACE_POSIX, -rc);
}
if (resp.hdr.result != HOTH_RES_SUCCESS) {
fprintf(stderr, "EC response contained error: %d", resp.hdr.result);
Expand Down
12 changes: 6 additions & 6 deletions protocol/test/libhoth_device_mock.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "protocol/test/libhoth_device_mock.h"

static int send(struct libhoth_device* dev, const void* request,
size_t request_size) {
static libhoth_error send(struct libhoth_device* dev, const void* request,
size_t request_size) {
LibHothDeviceMock* mock = (LibHothDeviceMock*)dev->user_ctx;
return mock->send(dev, request, request_size);
}

static int receive(struct libhoth_device* dev, void* response,
size_t max_response_size, size_t* actual_size,
int timeout_ms) {
static libhoth_error receive(struct libhoth_device* dev, void* response,
size_t max_response_size, size_t* actual_size,
int timeout_ms) {
LibHothDeviceMock* mock = (LibHothDeviceMock*)dev->user_ctx;
return mock->receive(dev, response, max_response_size, actual_size,
timeout_ms);
}

static int reconnect(struct libhoth_device* dev) {
static libhoth_error reconnect(struct libhoth_device* dev) {
LibHothDeviceMock* mock = (LibHothDeviceMock*)dev->user_ctx;
return mock->reconnect(dev);
}
Expand Down
6 changes: 3 additions & 3 deletions protocol/test/libhoth_device_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

class LibHothDeviceMock {
public:
MOCK_METHOD(int, send,
MOCK_METHOD(libhoth_error, send,
(struct libhoth_device * dev, const void* request,
size_t request_size),
());
MOCK_METHOD(int, receive,
MOCK_METHOD(libhoth_error, receive,
(struct libhoth_device * dev, void* response,
size_t max_response_size, size_t* actual_size, int timeout_ms),
());
Expand All @@ -39,7 +39,7 @@ class LibHothDeviceMock {
const void* request, size_t request_size, void* response,
size_t max_response_size, size_t* bytes_read),
());
MOCK_METHOD(int, reconnect, (struct libhoth_device * dev), ());
MOCK_METHOD(libhoth_error, reconnect, (struct libhoth_device * dev), ());
};

class LibHothTest : public testing::Test {
Expand Down
11 changes: 10 additions & 1 deletion transports/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ cc_library(
name = "libhoth_device",
srcs = ["libhoth_device.c"],
hdrs = ["libhoth_device.h"],
deps = [
"//protocol:libhoth_status",
],
)

cc_library(
Expand All @@ -20,6 +23,7 @@ cc_library(
deps = [
":libhoth_device",
":libhoth_ec",
"//protocol:libhoth_status",
],
)

Expand All @@ -30,6 +34,7 @@ cc_library(
deps = [
":libhoth_device",
":libhoth_ec",
"//protocol:libhoth_status",
],
)

Expand All @@ -40,6 +45,7 @@ cc_library(
deps = [
":libhoth_device",
":libhoth_usb_device",
"//protocol:libhoth_status",
"//protocol:util",
"@libusb",
],
Expand All @@ -51,7 +57,10 @@ cc_library(
hdrs = ["libhoth_dbus.h"],
defines = ["DBUS_BACKEND"],
linkopts = ["-lsystemd"],
deps = [":libhoth_device"],
deps = [
":libhoth_device",
"//protocol:libhoth_status",
],
)

cc_library(
Expand Down
Loading
Loading