Emiliano,
Thank you very much for your detailed reply.  Your note confirms my suspicion 
that it is not possible to make this work with SD card without making 
modifications to the distributed code.  I was hoping that I could support this 
without needing to modify any code from lwip.

My approach is almost the same as yours.  I made my own version of fs.c that is 
almost the same as what is distributed but adds the extra custom file 
functions, same as you.  I have a fs_read_custom(), but I also added a 
fs_bytes_left_custom().  This allows me to set file->len=0 in the 
fs_open_custom() function which solves the same problem you are solving with 
your modification to httpd.c.  With fs_bytes_left_custom() returning the actual 
file size remaining, the rest of httpd.c seems to work correctly and I didn't 
need to modify it.

Regards,
Joe K


From: [email protected] 
[mailto:[email protected]] On Behalf Of 
Emiliano Idà
Sent: Saturday, May 04, 2013 05:18
To: [email protected]
Subject: Re: [lwip-users] proper way to add extern fs support to htttpserver_raw

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 = NULL;
    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!





This message (including any attachments) is intended only for the use of the 
individual or entity to which it is addressed and may contain information that 
is non-public, proprietary, privileged, confidential, and exempt from 
disclosure under applicable law or may constitute as attorney work product.  If 
you are not the intended recipient, you are hereby notified that any use, 
dissemination, distribution, or copying of this communication is strictly 
prohibited. If you have received this communication in error, notify us 
immediately by telephone and  destroy this message if a facsimile or (ii) 
delete this message immediately if this is an electronic communication.  
Thank you.

_______________________________________________
lwip-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/lwip-users

Reply via email to