Skip to content

Commit a9cae63

Browse files
committed
Added support for in flash storage of read-only files.
1 parent 0a564ec commit a9cae63

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

fs.c

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// fs.c - wrapper for vfs.c for use by httpd
33
//
4-
// v0.1 / 2021-09-08 / Io Engineering / Terje
4+
// v0.2 / 2021-10-04 / Io Engineering / Terje
55
//
66

77
/*
@@ -45,8 +45,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4545
#if HTTP_ENABLE
4646

4747
#include "vfs.h"
48-
#include "lwip/apps/fs.h"
48+
#include "httpd.h"
4949

50+
static const embedded_file_t **ro_files = NULL;
5051
static stream_write_ptr wrptr;
5152
static stream_block_tx_buffer_t txbuf = {0};
5253

@@ -92,7 +93,7 @@ static void fs_write (const char *s)
9293

9394
while(length > txbuf.max_length) {
9495
txbuf.length = txbuf.max_length;
95-
memcpy(txbuf.data, s, txbuf.length);
96+
memcpy(txbuf.s, s, txbuf.length);
9697
if(!vf_write())
9798
return;
9899
length -= txbuf.max_length;
@@ -131,8 +132,26 @@ struct fs_file *fs_create (void)
131132
return &v_file;
132133
}
133134

135+
static const embedded_file_t *file_is_embedded (const char *name)
136+
{
137+
uint_fast8_t idx = 0;
138+
const embedded_file_t *file = NULL;
139+
140+
if(*name == '/')
141+
name++;
142+
143+
if(ro_files) do {
144+
if(!strcmp(ro_files[idx]->name, name))
145+
file = ro_files[idx];
146+
} while(file == NULL && ro_files[++idx] != NULL);
147+
148+
return file;
149+
}
150+
134151
err_t fs_open (struct fs_file *file, const char *name)
135152
{
153+
const embedded_file_t *ro_file;
154+
136155
if(name == NULL)
137156
return ERR_ARG;
138157

@@ -154,6 +173,11 @@ err_t fs_open (struct fs_file *file, const char *name)
154173
if((file->pextension = vfs_open(NULL, fname, "r"))) {
155174
file->len = vfs_size((vfs_file_t *)file->pextension);
156175
file->is_custom_file = 0;
176+
} else if((ro_file = file_is_embedded(name))) {
177+
file->len = ro_file->size;
178+
file->data = (char *)ro_file->data;
179+
file->is_custom_file = true;
180+
file->pextension = (void *)ro_file;
157181
} else
158182
file->pextension = NULL;
159183
}
@@ -197,11 +221,8 @@ int fs_read (struct fs_file *file, char *buffer, int count)
197221
count = count > file->len ? file->len : count;
198222
memcpy(buffer, file->data, count);
199223
file->data += count;
200-
} else {
224+
} else
201225
file->len = count = 0;
202-
203-
}
204-
205226
}
206227

207228
file->len -= count;
@@ -227,4 +248,9 @@ void fs_reset (void)
227248
}
228249
}
229250

251+
void fs_register_embedded_files (const embedded_file_t **files)
252+
{
253+
ro_files = files;
254+
}
255+
230256
#endif

httpd.h

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ typedef struct {
8888
void *private_data;
8989
} httpd_uri_handler_t;
9090

91+
typedef struct {
92+
const char *name;
93+
size_t size;
94+
const uint8_t data[];
95+
} embedded_file_t; // TODO: move to new vfs.c
96+
9197
uint8_t http_get_param_count (http_request_t *request);
9298
const char *http_get_uri (http_request_t *request);
9399
char *http_get_param_value (http_request_t *request, const char *key, char *value, uint32_t size);

0 commit comments

Comments
 (0)