ota_tftp_download takes an optional receive_cb that will report on the status of the TFTP transfer

This commit is contained in:
Kenshi Kawaguchi 2016-05-25 00:07:13 -07:00 committed by Angus Gratton
parent d62fd4899a
commit 84856f80a9
2 changed files with 19 additions and 6 deletions

View file

@ -43,7 +43,7 @@
static void tftp_task(void *port_p); static void tftp_task(void *port_p);
static char *tftp_get_field(int field, struct netbuf *netbuf); static char *tftp_get_field(int field, struct netbuf *netbuf);
static err_t tftp_receive_data(struct netconn *nc, size_t write_offs, size_t limit_offs, size_t *received_len, ip_addr_t *peer_addr, int peer_port); static err_t tftp_receive_data(struct netconn *nc, size_t write_offs, size_t limit_offs, size_t *received_len, ip_addr_t *peer_addr, int peer_port, tftp_receive_cb receive_cb);
static err_t tftp_send_ack(struct netconn *nc, int block); static err_t tftp_send_ack(struct netconn *nc, int block);
static err_t tftp_send_rrq(struct netconn *nc, const char *filename); static err_t tftp_send_rrq(struct netconn *nc, const char *filename);
static void tftp_send_error(struct netconn *nc, int err_code, const char *err_msg); static void tftp_send_error(struct netconn *nc, int err_code, const char *err_msg);
@ -53,7 +53,8 @@ void ota_tftp_init_server(int listen_port)
xTaskCreate(tftp_task, (signed char *)"tftpOTATask", 512, (void *)listen_port, 2, NULL); xTaskCreate(tftp_task, (signed char *)"tftpOTATask", 512, (void *)listen_port, 2, NULL);
} }
err_t ota_tftp_download(const char *server, int port, const char *filename, int timeout, int ota_slot) err_t ota_tftp_download(const char *server, int port, const char *filename,
int timeout, int ota_slot, tftp_receive_cb receive_cb)
{ {
rboot_config rboot_config = rboot_get_config(); rboot_config rboot_config = rboot_get_config();
/* Validate the OTA slot parameter */ /* Validate the OTA slot parameter */
@ -101,7 +102,8 @@ err_t ota_tftp_download(const char *server, int port, const char *filename, int
} }
size_t received_len; size_t received_len;
err = tftp_receive_data(nc, flash_offset, flash_offset+MAX_IMAGE_SIZE, &received_len, &addr, port); err = tftp_receive_data(nc, flash_offset, flash_offset+MAX_IMAGE_SIZE,
&received_len, &addr, port, receive_cb);
netconn_delete(nc); netconn_delete(nc);
return err; return err;
} }
@ -189,7 +191,7 @@ static void tftp_task(void *listen_port)
/* Finished WRQ phase, start TFTP data transfer */ /* Finished WRQ phase, start TFTP data transfer */
size_t received_len; size_t received_len;
netconn_set_recvtimeout(nc, 10000); netconn_set_recvtimeout(nc, 10000);
int recv_err = tftp_receive_data(nc, conf.roms[slot], conf.roms[slot]+MAX_IMAGE_SIZE, &received_len, NULL, 0); int recv_err = tftp_receive_data(nc, conf.roms[slot], conf.roms[slot]+MAX_IMAGE_SIZE, &received_len, NULL, 0, NULL);
netconn_disconnect(nc); netconn_disconnect(nc);
printf("OTA TFTP receive data result %d bytes %d\r\n", recv_err, received_len); printf("OTA TFTP receive data result %d bytes %d\r\n", recv_err, received_len);
@ -240,7 +242,7 @@ static char *tftp_get_field(int field, struct netbuf *netbuf)
#define TFTP_TIMEOUT_RETRANSMITS 10 #define TFTP_TIMEOUT_RETRANSMITS 10
static err_t tftp_receive_data(struct netconn *nc, size_t write_offs, size_t limit_offs, size_t *received_len, ip_addr_t *peer_addr, int peer_port) static err_t tftp_receive_data(struct netconn *nc, size_t write_offs, size_t limit_offs, size_t *received_len, ip_addr_t *peer_addr, int peer_port, tftp_receive_cb receive_cb)
{ {
*received_len = 0; *received_len = 0;
const int DATA_PACKET_SZ = 512 + 4; /*( packet size plus header */ const int DATA_PACKET_SZ = 512 + 4; /*( packet size plus header */
@ -382,6 +384,11 @@ static err_t tftp_receive_data(struct netconn *nc, size_t write_offs, size_t lim
return ack_err; return ack_err;
} }
// Make sure ack was successful before calling callback.
if(receive_cb) {
receive_cb(*received_len);
}
if(len < DATA_PACKET_SZ) { if(len < DATA_PACKET_SZ) {
return ERR_OK; return ERR_OK;
} }

View file

@ -3,6 +3,8 @@
#include "lwip/err.h" #include "lwip/err.h"
typedef void (*tftp_receive_cb)(size_t bytes_received);
/* TFTP Server OTA Support /* TFTP Server OTA Support
* *
* To use, call ota_tftp_init_server() which will start the TFTP server task * To use, call ota_tftp_init_server() which will start the TFTP server task
@ -41,8 +43,12 @@ void ota_tftp_init_server(int listen_port);
Returns 0 on success, LWIP err.h values for errors. Returns 0 on success, LWIP err.h values for errors.
Does not change the current firmware slot, or reboot. Does not change the current firmware slot, or reboot.
receive_cb: called repeatedly after each successful packet that
has been written to flash and ACKed. Can pass NULL to omit.
*/ */
err_t ota_tftp_download(const char *server, int port, const char *filename, int timeout, int ota_slot); err_t ota_tftp_download(const char *server, int port, const char *filename,
int timeout, int ota_slot, tftp_receive_cb receive_cb);
#define TFTP_PORT 69 #define TFTP_PORT 69