Browse Source

present files on fat fs via http

Thomas Buck 11 months ago
parent
commit
fcee4f1041
2 changed files with 45 additions and 16 deletions
  1. 4
    4
      conf/lwipopts.h
  2. 41
    12
      src/http.c

+ 4
- 4
conf/lwipopts.h View File

74
 #define DHCP_DEBUG                  LWIP_DBG_OFF
74
 #define DHCP_DEBUG                  LWIP_DBG_OFF
75
 
75
 
76
 #define LWIP_HTTPD 1
76
 #define LWIP_HTTPD 1
77
-#define LWIP_HTTPD_SSI 1
77
+#define LWIP_HTTPD_SSI 0
78
 #define LWIP_HTTPD_CGI 1
78
 #define LWIP_HTTPD_CGI 1
79
-// don't include the tag comment - less work for the CPU, but may be harder to debug
80
-#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
81
-// use generated fsdata
79
+#define LWIP_HTTPD_CUSTOM_FILES 1
80
+#define LWIP_HTTPD_FILE_EXTENSION 1
81
+#define LWIP_HTTPD_DYNAMIC_HEADERS 1
82
 #define HTTPD_FSDATA_FILE "httpd_fsdata.c"
82
 #define HTTPD_FSDATA_FILE "httpd_fsdata.c"
83
 
83
 
84
 #endif /* __LWIPOPTS_H__ */
84
 #endif /* __LWIPOPTS_H__ */

+ 41
- 12
src/http.c View File

21
  */
21
  */
22
 
22
 
23
 #include "lwip/apps/httpd.h"
23
 #include "lwip/apps/httpd.h"
24
+#include "lwip/apps/fs.h"
25
+#include "ff.h"
26
+
27
+#include <string.h>
24
 
28
 
25
 #include "config.h"
29
 #include "config.h"
26
 #include "log.h"
30
 #include "log.h"
31
+#include "debug_disk.h"
27
 #include "http.h"
32
 #include "http.h"
28
 
33
 
29
 void http_init(void) {
34
 void http_init(void) {
30
     httpd_init();
35
     httpd_init();
31
 }
36
 }
32
 
37
 
33
-#include "lwip/apps/fs.h"
34
-#include <string.h>
35
-
36
 int fs_open_custom(struct fs_file *file, const char *name) {
38
 int fs_open_custom(struct fs_file *file, const char *name) {
37
-    // TODO hook up to fat fs
38
-    if (strcmp(name, "/src.tar.xz") == 0) {
39
-        /*
39
+    debug("'%s'", name);
40
+
41
+    // TODO only do this when not mounted via USB
42
+    debug_disk_mount();
43
+
44
+    FIL f;
45
+    FRESULT r = f_open (&f, name, FA_READ);
46
+    if (r == FR_OK) {
47
+        FSIZE_t len = f_size(&f);
48
+        char *data = malloc(len);
49
+        if (!data) {
50
+            debug("error: not enough memory");
51
+            f_close(&f);
52
+            debug_disk_unmount();
53
+            return 0;
54
+        }
55
+
56
+        UINT read_count = 0;
57
+        r = f_read(&f, data, len, &read_count);
58
+        if ((r != FR_OK) || (read_count != len)) {
59
+            debug("invalid read: %d %d %ld", r, read_count, len);
60
+        }
61
+
40
         memset(file, 0, sizeof(struct fs_file));
62
         memset(file, 0, sizeof(struct fs_file));
41
-        file->data = (const char *)data_tar_xz;
42
-        file->len = data_tar_xz_len;
63
+        file->data = data;
64
+        file->len = len;
43
         file->index = file->len;
65
         file->index = file->len;
44
         file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
66
         file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
67
+
68
+        f_close(&f);
69
+        debug_disk_unmount();
45
         return 1;
70
         return 1;
46
-        */
47
-        (void)file;
48
-        return 0;
49
     }
71
     }
72
+
73
+    debug_disk_unmount();
50
     return 0;
74
     return 0;
51
 }
75
 }
52
 
76
 
53
 void fs_close_custom(struct fs_file *file) {
77
 void fs_close_custom(struct fs_file *file) {
54
-    (void)file;
78
+    debug("len=%d", file->len);
79
+
80
+    if (file && file->data) {
81
+        free((void *)file->data);
82
+        file->data = NULL;
83
+    }
55
 }
84
 }

Loading…
Cancel
Save