|  | @@ -1,4 +1,5 @@
 | 
	
		
			
				|  |  |  #ifdef HARDWARE_BT6711
 | 
	
		
			
				|  |  | +#ifndef BT6702_SERVICE
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  |   * lwftp.c : a lightweight FTP client using raw API of LWIP
 | 
	
		
			
				|  |  |   *
 | 
	
	
		
			
				|  | @@ -40,6 +41,7 @@
 | 
	
		
			
				|  |  |  #include "stm32f4xx.h"
 | 
	
		
			
				|  |  |  #include "common_config.h"
 | 
	
		
			
				|  |  |  #include "spi_flash.h"
 | 
	
		
			
				|  |  | +#include "web_params_api.h"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /** Enable debugging for LWFTP */
 | 
	
		
			
				|  |  |  #ifndef LWFTP_DEBUG
 | 
	
	
		
			
				|  | @@ -54,6 +56,7 @@
 | 
	
		
			
				|  |  |  #define PTRNLEN(s)  s,(sizeof(s)-1)
 | 
	
		
			
				|  |  |  #define min(x,y) ((x)<(y)?(x):(y))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +static unsigned received_bytes_count = 0;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /** Close control or data pcb
 | 
	
		
			
				|  |  |   * @param pointer to lwftp session data
 | 
	
	
		
			
				|  | @@ -123,12 +126,14 @@ static err_t lwftp_data_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, er
 | 
	
		
			
				|  |  |      tcp_recved(tpcb, p->tot_len);
 | 
	
		
			
				|  |  |      pbuf_free(p);
 | 
	
		
			
				|  |  |    } else {
 | 
	
		
			
				|  |  | -    // NULL pbuf shall lead to close the pcb. Close is postponed after
 | 
	
		
			
				|  |  | -    // the session state machine updates. No need to close right here.
 | 
	
		
			
				|  |  | -    // Instead we kindly tell data sink we are done
 | 
	
		
			
				|  |  |      if (s->data_sink) {
 | 
	
		
			
				|  |  |        s->data_sink(s->data_handle, NULL, 0);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +    // NULL pbuf shall lead to close the pcb.
 | 
	
		
			
				|  |  | +    if (s->data_pcb) {
 | 
	
		
			
				|  |  | +      lwftp_pcb_close(s->data_pcb);
 | 
	
		
			
				|  |  | +      s->data_pcb = NULL;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |    return ERR_OK;
 | 
	
		
			
				|  |  |  }
 | 
	
	
		
			
				|  | @@ -239,10 +244,6 @@ static err_t lwftp_send_msg(lwftp_session_t *s, const char* msg, size_t len)
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  static void lwftp_control_close(lwftp_session_t *s, int result)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -  if (s->data_pcb) {
 | 
	
		
			
				|  |  | -    lwftp_pcb_close(s->data_pcb);
 | 
	
		
			
				|  |  | -    s->data_pcb = NULL;
 | 
	
		
			
				|  |  | -  }
 | 
	
		
			
				|  |  |    if (s->control_pcb) {
 | 
	
		
			
				|  |  |      lwftp_pcb_close(s->control_pcb);
 | 
	
		
			
				|  |  |      s->control_pcb = NULL;
 | 
	
	
		
			
				|  | @@ -623,7 +624,7 @@ static bool validate_spif_firmware(uint32_t fw_len)
 | 
	
		
			
				|  |  |    CRC->CR = 1;
 | 
	
		
			
				|  |  |    for (uint32_t offset = 0; offset < crc_offset; offset += 4) {
 | 
	
		
			
				|  |  |      uint32_t data;
 | 
	
		
			
				|  |  | -    spi_flash_read(offset, &data, sizeof(data), 0);
 | 
	
		
			
				|  |  | +    spi_flash_read(spif_firmware_offset + offset, &data, sizeof(data), 0);
 | 
	
		
			
				|  |  |      CRC->DR = data;
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |    uint32_t calculated_crc = CRC->DR;
 | 
	
	
		
			
				|  | @@ -636,6 +637,7 @@ static bool validate_spif_firmware(uint32_t fw_len)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  static void erase_spif_firmware()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | +  printf("ftp: erasing SPI firmware partition...\r\n");
 | 
	
		
			
				|  |  |    for (int sec = 0; sec < FIRMWARE_UPDATE_SECTOR_COUNT; ++sec) {
 | 
	
		
			
				|  |  |      spi_flash_erase_sector(SPI_FLASH_SECTOR_SIZE * (FIRMWARE_UPDATE_SECTOR_OFFSET + sec), 0);
 | 
	
		
			
				|  |  |    }
 | 
	
	
		
			
				|  | @@ -644,30 +646,30 @@ static void erase_spif_firmware()
 | 
	
		
			
				|  |  |  static unsigned data_sink(void *arg, const char* ptr, unsigned len)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |    (void)arg;
 | 
	
		
			
				|  |  | -  static unsigned offset = 0;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  if (ptr) {
 | 
	
		
			
				|  |  | +  if (ptr && len) {
 | 
	
		
			
				|  |  |      // we received some data from the FTP server
 | 
	
		
			
				|  |  | -    if (offset == 0) {
 | 
	
		
			
				|  |  | +    if (received_bytes_count == 0) {
 | 
	
		
			
				|  |  |        // we need to erase the flash before we can write it
 | 
	
		
			
				|  |  |        erase_spif_firmware();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    if (offset + len > MAIN_FW_SIZE) {
 | 
	
		
			
				|  |  | -      // the firmware won't fit into the MCU flash
 | 
	
		
			
				|  |  | +    if (received_bytes_count + len > MAIN_FW_SIZE) {
 | 
	
		
			
				|  |  | +      printf("ftp: the firmware is too big! aborting the download\r\n");
 | 
	
		
			
				|  |  |        return 0;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + offset, ptr, len, 0);
 | 
	
		
			
				|  |  | -    offset += len;
 | 
	
		
			
				|  |  | +    spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + received_bytes_count, ptr, len, 0);
 | 
	
		
			
				|  |  | +    received_bytes_count += len;
 | 
	
		
			
				|  |  |    } else {
 | 
	
		
			
				|  |  | -    // all the data is in
 | 
	
		
			
				|  |  | -    uint32_t fw_size = offset;
 | 
	
		
			
				|  |  | +    printf("ftp: the firmware is downloaded, verifying...\r\n");
 | 
	
		
			
				|  |  | +    uint32_t fw_size = received_bytes_count;
 | 
	
		
			
				|  |  |      // next time it comes, overwrite the existing SPI contents
 | 
	
		
			
				|  |  | -    offset = 0;
 | 
	
		
			
				|  |  | +    received_bytes_count = 0;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      bool good_firmware = validate_spif_firmware(fw_size);
 | 
	
		
			
				|  |  |      if (good_firmware) {
 | 
	
		
			
				|  |  | -      // TODO reboot
 | 
	
		
			
				|  |  | +      printf("ftp: the firmware is valid, rebooting...\r\n");
 | 
	
		
			
				|  |  | +      HTTP_StartResetTask(true);
 | 
	
		
			
				|  |  |      } else {
 | 
	
		
			
				|  |  |        // erase it so the bootloader won't try to verify it every time
 | 
	
		
			
				|  |  |        erase_spif_firmware();
 | 
	
	
		
			
				|  | @@ -678,10 +680,12 @@ static unsigned data_sink(void *arg, const char* ptr, unsigned len)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  void start_ftp_client(lwftp_session_t *s)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | +  received_bytes_count = 0;
 | 
	
		
			
				|  |  |    s->data_sink = data_sink;
 | 
	
		
			
				|  |  |    //s->done_fn = ftp_retr_callback;
 | 
	
		
			
				|  |  |    lwftp_retr(s);
 | 
	
		
			
				|  |  |    // FTP session will continue with the connection callback
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +#endif // !BT6702_SERVICE
 | 
	
		
			
				|  |  |  #endif // HARDWARE_BT6711
 |