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
21 changes: 20 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum event_type {
EVENT_TYPE_REBOOT = 1,
EVENT_TYPE_GO,
EVENT_TYPE_SERVER_DONE,
EVENT_TYPE_ACTIVITY
};

struct event {
Expand All @@ -80,6 +81,9 @@ struct event {
struct {
uint32_t vtor;
} go;
struct {
bool active;
} activity;
};
};

Expand Down Expand Up @@ -562,6 +566,18 @@ static bool should_stay_in_bootloader()
return !gpio_get(BOOTLOADER_ENTRY_PIN) || wd_says_so;
}

static void handle_activity(bool active)
{
struct event ev = {
.type = EVENT_TYPE_ACTIVITY,
.activity = {
.active = active,
},
};

queue_try_add(&event_queue, &ev);
}

static void network_deinit()
{
#if PICOWOTA_WIFI_AP == 1
Expand Down Expand Up @@ -636,7 +652,7 @@ int main()
&reboot_cmd,
};

struct tcp_comm_ctx *tcp = tcp_comm_new(cmds, sizeof(cmds) / sizeof(cmds[0]), CMD_SYNC);
struct tcp_comm_ctx *tcp = tcp_comm_new(cmds, sizeof(cmds) / sizeof(cmds[0]), CMD_SYNC, handle_activity);

struct event ev = {
.type = EVENT_TYPE_SERVER_DONE,
Expand All @@ -659,6 +675,9 @@ int main()
picowota_reboot(ev.reboot.to_bootloader);
/* Should never get here */
break;
case EVENT_TYPE_ACTIVITY:
cyw43_arch_gpio_put (0, ev.activity.active);
break;
case EVENT_TYPE_GO:
tcp_comm_server_close(tcp);
network_deinit();
Expand Down
16 changes: 11 additions & 5 deletions tcp_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct tcp_comm_ctx {
const struct comm_command *const *cmds;
unsigned int n_cmds;
uint32_t sync_opcode;
void (*activity_func)(bool);
};

#define COMM_BUF_OPCODE(_buf) ((uint32_t *)((uint8_t *)(_buf)))
Expand Down Expand Up @@ -267,7 +268,7 @@ static err_t tcp_comm_client_close(struct tcp_comm_ctx *ctx)
{
err_t err = ERR_OK;

cyw43_arch_gpio_put (0, false);
ctx->activity_func(false);
ctx->conn_state = CONN_STATE_CLOSED;

if (!ctx->client_pcb) {
Expand Down Expand Up @@ -450,17 +451,21 @@ static void tcp_comm_client_err(void *arg, err_t err)
ctx->client_pcb = NULL;
ctx->conn_state = CONN_STATE_CLOSED;
ctx->rx_bytes_needed = 0;
cyw43_arch_gpio_put (0, false);
ctx->activity_func(false);
}

static void tcp_comm_client_init(struct tcp_comm_ctx *ctx, struct tcp_pcb *pcb)
{
ctx->client_pcb = pcb;
ctx->rx_bytes_received = 0;
ctx->rx_bytes_needed = 0;
ctx->rx_start_offs = 0;
ctx->tx_bytes_sent = 0;
ctx->tx_bytes_remaining = 0;
tcp_arg(pcb, ctx);

cyw43_arch_gpio_put (0, true);

tcp_comm_sync_begin(ctx);
ctx->activity_func(true);

tcp_sent(pcb, tcp_comm_client_sent);
tcp_recv(pcb, tcp_comm_client_recv);
Expand Down Expand Up @@ -522,7 +527,7 @@ err_t tcp_comm_listen(struct tcp_comm_ctx *ctx, uint16_t port)
}

struct tcp_comm_ctx *tcp_comm_new(const struct comm_command *const *cmds,
unsigned int n_cmds, uint32_t sync_opcode)
unsigned int n_cmds, uint32_t sync_opcode, void (*activity_func)(bool) )
{
struct tcp_comm_ctx *ctx = calloc(1, sizeof(struct tcp_comm_ctx));
if (!ctx) {
Expand All @@ -538,6 +543,7 @@ struct tcp_comm_ctx *tcp_comm_new(const struct comm_command *const *cmds,
ctx->cmds = cmds;
ctx->n_cmds = n_cmds;
ctx->sync_opcode = sync_opcode;
ctx->activity_func = activity_func;

return ctx;
}
Expand Down
2 changes: 1 addition & 1 deletion tcp_comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ err_t tcp_comm_server_close(struct tcp_comm_ctx *ctx);
bool tcp_comm_server_done(struct tcp_comm_ctx *ctx);

struct tcp_comm_ctx *tcp_comm_new(const struct comm_command *const *cmds,
unsigned int n_cmds, uint32_t sync_opcode);
unsigned int n_cmds, uint32_t sync_opcode, void (*activity_func)(bool));
void tcp_comm_delete(struct tcp_comm_ctx *ctx);

#endif /* __TCP_COMM_H__ */