lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] proper way to add extern fs support to htttpserver_raw


From: Emiliano Idà
Subject: Re: [lwip-users] proper way to add extern fs support to htttpserver_raw
Date: Sat, 4 May 2013 12:17:43 +0200

Hi Joseph,
I had some experience to use custom files in SD card with httpserver_raw. 
My goal was to use httpserver_raw to serve some web pages in fsdata.c using the "standard" way, and also to allow user to download, via httpserver_raw, some log files in SD card.
I had some difficulties but at the end i've made it. So, i hope the following guidelines will help you.

1) First of all, i implemented my own module defining the functions to open / close / read files on SD

you have to implement 3 functions:

int fs_open_custom(struct fs_file *file, const char *name);
void  fs_close_custom(struct fs_file *file);
int fs_read_custom(struct fs_file *file, char *buffer, int count);

In detail:

The  fs_open_custom must open your custom file in SD having name "name". Function must return 1 if file is opened successfully, 0 otherwise.

here a snippet from my application, in pseudo-C code:

int fs_open_custom(struct fs_file *file, const char *name) {

    int resopen = <open file from SD>
    if (resopen == -1) {
        return 0; 
    }

    file->data = "">    file->len = <your file size>
    file->index = 0;
    file->pextension = NULL;
    
    return 1;
}


The following function must close the file:
fs_close_custom(struct fs_file *file);

Finally, the fs_read_custom function must read "at most" count bytes from file, put them in buffer, and return the number of bytes read.
I say "at most", because you try to read N bytes, and the remaining bytes to read are M, with N > M, the function will return M
Note that if you have reached the end of file, the function must return FS_READ_EOF

int fs_read_custom(struct fs_file *file, char *buffer, int count);

here an example:

int fs_read_custom(struct fs_file *file, char *buffer, int count)  {
int read = 0;    
if (file->index < file->len) {
        read = <call your function to read "count" bytes from SD, fill in buffer, and return the amount of bytes read>
    }
    else {
        read = FS_READ_EOF;
    }
    return read;
}


2) The second step: enable two defines in fs.h

LWIP_HTTPD_CUSTOM_FILES
LWIP_HTTPD_DYNAMIC_FILE_READ

another define, LWIP_HTTPD_FS_ASYNC_READ can be used if you read data asynchronoysly from your device, i.e. if you use a DMA channel, which was not my case. I didn't enabled it.


3) Third step, you have to modify the fs_read function in fs.c to call your custom read.
int fs_read(struct fs_file *file, char *buffer, int count)

add these 3 instructions at the beginning of the function:

if (file->is_custom_file) {
    return fs_read_custom( file, buffer, count);
}



4) Last step, you have to modify httpd.c in order to fix a (possible?) bug i've found in httpd.c
In http_init_file remove line 2169 and substitute with 4 instructions as follows (i include a snippet of my diff for your convenience).
     hs->handle = file;
     hs->file = (char*)file->data;
     LWIP_ASSERT("File length must be positive!", (file->len >= 0));
-    hs->left = file->len;
+       if (file->is_custom_file == 0)
+        hs->left = file->len;
+       else 
+        hs->left = 0;
     hs->retries = 0;

I hope i haven't forgotten something, please tell us if these hints works for you. 
Best regards!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]