Skip to content

Commit 041b19e

Browse files
author
Jamie C. Driver
committed
usbstorage: pass opaque context object to usb action callbacks
1 parent e06d36b commit 041b19e

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

main/usbhmsc/usbmode.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,21 @@ static const char SIGNED_PSBT_SUFFIX[] = "_signed.psbt";
4848
// Function predicate to filter filenames available for a particular action
4949
typedef bool (*filename_filter_fn_t)(const char* path, const char* filename, const size_t filename_len);
5050

51-
// Fucntion/action to call on a usb-storage directory
52-
typedef bool (*usbstorage_action_fn_t)(const char* extra_path);
53-
5451
// State of usb storage
5552
struct usbstorage_state_t {
5653
SemaphoreHandle_t semaphore_usbstorage_event;
5754
enum { USBSTORAGE_STATE_NONE, USBSTORAGE_STATE_ERROR, USBSTORAGE_STATE_MOUNTED } usbstorage_state;
5855
};
5956

57+
// Context object passed through to action callbacks
58+
typedef struct {
59+
const char* extra_path;
60+
void* ctx;
61+
} usbstorage_action_context_t;
62+
63+
// Function/action to call on a usb-storage directory
64+
typedef bool (*usbstorage_action_fn_t)(const usbstorage_action_context_t* ctx);
65+
6066
static bool file_exists(const char* file_path)
6167
{
6268
JADE_ASSERT(file_path);
@@ -286,12 +292,12 @@ static void handle_usbstorage_event(const usbstorage_event_t event, const uint8_
286292
}
287293

288294
// Generic handler to run usb storage actions
289-
static bool handle_usbstorage_action(
290-
const char* title, usbstorage_action_fn_t usbstorage_action, const char* extra_path, const bool async_action)
295+
static bool handle_usbstorage_action(const char* title, usbstorage_action_fn_t usbstorage_action,
296+
const usbstorage_action_context_t* ctx, const bool async_action)
291297
{
292298
JADE_ASSERT(title);
293299
JADE_ASSERT(usbstorage_action);
294-
// extra_path is optional
300+
JADE_ASSERT(ctx);
295301

296302
while (usb_connected()) {
297303
const char* message[] = { "Disconnect USB power and", "connect a storage device" };
@@ -326,7 +332,7 @@ static bool handle_usbstorage_action(
326332
while (true) {
327333
// If the usb_storage is mounted, run the action
328334
if (state.usbstorage_state == USBSTORAGE_STATE_MOUNTED) {
329-
action_initiated = usbstorage_action(extra_path);
335+
action_initiated = usbstorage_action(ctx);
330336
break;
331337
}
332338

@@ -692,12 +698,16 @@ static bool is_full_fw_file(const char* path, const char* filename, const size_t
692698
return file_exists(hash_filename);
693699
}
694700

695-
static bool initiate_usb_ota(const char* extra_path)
701+
static bool initiate_usb_ota(const usbstorage_action_context_t* ctx)
696702
{
703+
JADE_ASSERT(ctx);
704+
697705
// extra_path is optional
706+
JADE_ASSERT(!ctx->ctx);
698707

699708
char filename[MAX_FILENAME_SIZE];
700-
if (!select_file_from_filtered_list("Select Firmware", extra_path, is_full_fw_file, filename, sizeof(filename))) {
709+
if (!select_file_from_filtered_list(
710+
"Select Firmware", ctx->extra_path, is_full_fw_file, filename, sizeof(filename))) {
701711
// User abandoned
702712
return false;
703713
}
@@ -732,7 +742,8 @@ bool usbstorage_firmware_ota(const char* extra_path)
732742
{
733743
// extra_path is optional
734744
const bool is_async = true;
735-
return handle_usbstorage_action("Firmware Upgrade", initiate_usb_ota, extra_path, is_async);
745+
const usbstorage_action_context_t ctx = { .extra_path = extra_path };
746+
return handle_usbstorage_action("Firmware Upgrade", initiate_usb_ota, &ctx, is_async);
736747
}
737748

738749
// Sign PSBT
@@ -742,12 +753,15 @@ static bool is_psbt_file(const char* path, const char* filename, const size_t fi
742753
return STR_ENDSWITH(filename, filename_len, PSBT_SUFFIX, strlen(PSBT_SUFFIX));
743754
}
744755

745-
static bool sign_usb_psbt(const char* extra_path)
756+
static bool sign_usb_psbt(const usbstorage_action_context_t* ctx)
746757
{
758+
JADE_ASSERT(ctx);
759+
747760
// extra_path is optional
761+
JADE_ASSERT(!ctx->ctx);
748762

749763
char filename[MAX_FILENAME_SIZE];
750-
if (!select_file_from_filtered_list("Select PSBT", extra_path, is_psbt_file, filename, sizeof(filename))) {
764+
if (!select_file_from_filtered_list("Select PSBT", ctx->extra_path, is_psbt_file, filename, sizeof(filename))) {
751765
return false;
752766
}
753767

@@ -880,5 +894,6 @@ bool usbstorage_sign_psbt(const char* extra_path)
880894
{
881895
// extra_path is optional
882896
const bool is_async = false;
883-
return handle_usbstorage_action("Sign PSBT", sign_usb_psbt, extra_path, is_async);
897+
const usbstorage_action_context_t ctx = { .extra_path = extra_path };
898+
return handle_usbstorage_action("Sign PSBT", sign_usb_psbt, &ctx, is_async);
884899
}

0 commit comments

Comments
 (0)