Browse Source

ftp: tentatively report the download state to the web

Sergey Alirzaev 4 years ago
parent
commit
59f2417c43
3 changed files with 36 additions and 11 deletions
  1. 14 6
      modules/HTTP_Server/http_server.c
  2. 19 4
      modules/ftp.c
  3. 3 1
      modules/ftp.h

+ 14 - 6
modules/HTTP_Server/http_server.c

@@ -72,6 +72,7 @@ char *send_file(char *filename, char *pnonmatch,  struct fs_file *file, uint16_t
 static uint32_t Parse_Header(char *data, uint32_t len, const char *field, uint32_t flen, char *value);
 bool GetFileName(char *inStr, char *fileName, uint8_t *fileNameLen);
 static char *HTTP_FTPFWUpdate(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
+static char *HTTP_FTPFWState(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
 
 #define NUM_LOCKS 3
 static void *locks[NUM_LOCKS];
@@ -192,6 +193,7 @@ web_func_handler_t process_web_funcs[] = {
     { "GET /fw_update.cgi",             18,   COMMON_ANSWER,        TIME_ACCESS,  HTTP_ConfirmBootPwd },
 #ifdef HARDWARE_BT6711
     { X("POST /ftp_fw_update.cgi"),           COMMON_ANSWER,        TIME_ACCESS,  HTTP_FTPFWUpdate },
+    { X("GET /fw_dl_state.cgi"),              COMMON_ANSWER,        TIME_ACCESS,  HTTP_FTPFWState },
 #endif // HARDWARE_BT6711
     { "GET",                            3,    COMMON_ANSWER,        ALL_ACCESS,   HTTP_GetRequest },
     { "",                               0,    COMMON_ANSWER,        ALL_ACCESS,   HTTP_NoFound },
@@ -1429,13 +1431,19 @@ static char *HTTP_FTPFWUpdate(uint32_t reqNum, char *bufIn, char *bufOut, uint16
         strcpy(settings->pass, "guest");
     }
 
-    start_ftp_client(&ftpcfg);
-    // TODO write the settings to NVRAM
-    // TODO wait for the client to connect to the server and check whether the file exists
-
-    strcpy(bufOut, HTTP_200_OK);
-    *lenBufOut = strlen(bufOut);
+    HTTP_SaveSettings();
+    char *error = start_ftp_client(&ftpcfg);
+    *lenBufOut = sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\n%s", error);
+    return bufOut;
+}
 
+static char *HTTP_FTPFWState(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
+{
+    (void)bufIn;
+    (void)lenBufIn;
+    (void)reqNum;
+    unsigned percentage = get_ftp_progress();
+    *lenBufOut = sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\n%u", percentage);
     return bufOut;
 }
 #endif // HARDWARE_BT6711

+ 19 - 4
modules/ftp.c

@@ -42,6 +42,8 @@
 #include "common_config.h"
 #include "spi_flash.h"
 #include "web_params_api.h"
+#include "FreeRTOS.h"
+#include "task.h"
 
 /** Enable debugging for LWFTP */
 #ifndef LWFTP_DEBUG
@@ -115,6 +117,8 @@ static err_t lwftp_data_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, er
   (void)err;
   lwftp_session_t *s = (lwftp_session_t*)arg;
   if (p) {
+    // notify the web interface that we can proceed
+    s->error = "1";
     if (s->data_sink) {
       struct pbuf *q;
       for (q=p; q; q=q->next) {
@@ -348,8 +352,8 @@ anonymous:
           s->control_state = LWFTP_XFERING;
         } else if (response==550) {
             s->control_state = LWFTP_QUIT;
-            //result = LWFTP_RESULT_ERR_FILENAME;
-            LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: Failed to open file '%s'\n", s->settings->remote_path));
+            s->error = "Failed to open file"
+            LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s '%s'\n", error, s->settings->remote_path));
         }
         else {
           s->control_state = LWFTP_QUIT;
@@ -462,7 +466,8 @@ static void lwftp_control_err(void *arg, err_t err)
     lwftp_session_t *s = (lwftp_session_t*)arg;
     int result;
     if( s->control_state == LWFTP_CLOSED ) {
-      LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:failed to connect to server (%s)\n",lwip_strerr(err)));
+      s->error = "failed to connect to server";
+      LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s (%s)\n",lwip_strerr(err)));
       result = LWFTP_RESULT_ERR_CONNECT;
     } else {
       LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
@@ -678,13 +683,23 @@ static unsigned data_sink(void *arg, const char* ptr, unsigned len)
   return len;
 }
 
-void start_ftp_client(lwftp_session_t *s)
+char *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
+  //// wait for the client to connect to the server and check whether the file exists
+  //while (s->error == NULL) {
+  //  vTaskDelay(200);
+  //}
+  return "1";
+}
+
+uint32_t get_ftp_progress()
+{
+  return received_bytes_count * 100 / MAIN_FW_SIZE;
 }
 
 #endif // !BT6702_SERVICE

+ 3 - 1
modules/ftp.h

@@ -77,10 +77,12 @@ typedef struct {
   lwftp_state_t   data_state;
   struct tcp_pcb  *control_pcb;
   struct tcp_pcb  *data_pcb;
+  char *error;
 } lwftp_session_t;
 
 // LWFTP API
 err_t lwftp_store(lwftp_session_t *s);
-void start_ftp_client(lwftp_session_t *s);
+char *start_ftp_client(lwftp_session_t *s);
+uint32_t get_ftp_progress(void);
 
 #endif