@ -39,15 +39,15 @@ It's written for use with httpd, but doesn't need to be used as such.
# include "heatshrink_decoder.h"
# endif
static char * espFsData = NULL ;
static const char * espFsData = NULL ;
struct EspFsFile {
EspFsHeader * header ;
const EspFsHeader * header ;
char decompressor ;
int32_t posDecomp ;
char * posStart ;
char * posComp ;
const char * posStart ;
const char * posComp ;
void * decompData ;
} ;
@ -67,7 +67,8 @@ Accessing the flash through the mem emulation at 0x40200000 is a bit hairy: All
a memory exception , crashing the program .
*/
EspFsInitResult ICACHE_FLASH_ATTR espFsInit ( void * flashAddress ) {
EspFsInitResult ICACHE_FLASH_ATTR espFsInit ( const void * flashAddress )
{
if ( ( uint32_t ) flashAddress > 0x40200000 ) {
flashAddress = ( void * ) ( ( uint32_t ) flashAddress - 0x40200000 ) ;
}
@ -84,7 +85,7 @@ EspFsInitResult ICACHE_FLASH_ATTR espFsInit(void *flashAddress) {
return ESPFS_INIT_RESULT_NO_IMAGE ;
}
espFsData = ( char * ) flashAddress ;
espFsData = ( const char * ) flashAddress ;
return ESPFS_INIT_RESULT_OK ;
}
@ -93,7 +94,8 @@ EspFsInitResult ICACHE_FLASH_ATTR espFsInit(void *flashAddress) {
//ToDo: perhaps memcpy also does unaligned accesses?
# ifdef __ets__
void ICACHE_FLASH_ATTR readFlashUnaligned ( char * dst , char * src , int len ) {
void ICACHE_FLASH_ATTR readFlashUnaligned ( char * dst , char * src , int len )
{
uint8_t src_offset = ( ( uint32_t ) src ) & 3 ;
uint32_t src_address = ( ( uint32_t ) src ) - src_offset ;
@ -106,7 +108,8 @@ void ICACHE_FLASH_ATTR readFlashUnaligned(char *dst, char *src, int len) {
# endif
// Returns flags of opened file.
int ICACHE_FLASH_ATTR espFsFlags ( EspFsFile * fh ) {
int ICACHE_FLASH_ATTR espFsFlags ( EspFsFile * fh )
{
if ( fh = = NULL ) {
httpd_printf ( " File handle not ready \n " ) ;
return - 1 ;
@ -118,13 +121,16 @@ int ICACHE_FLASH_ATTR espFsFlags(EspFsFile *fh) {
}
//Open a file and return a pointer to the file desc struct.
EspFsFile ICACHE_FLASH_ATTR * espFsOpen ( char * fileName ) {
EspFsFile ICACHE_FLASH_ATTR * espFsOpen ( const char * fileName )
{
printf ( " Open file %s \n " , fileName ) ;
if ( espFsData = = NULL ) {
httpd_printf ( " Call espFsInit first! \n " ) ;
return NULL ;
}
char * p = espFsData ;
char * hpos ;
const char * p = espFsData ;
const char * hpos ;
char namebuf [ 256 ] ;
EspFsHeader h ;
EspFsFile * r ;
@ -141,7 +147,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
return NULL ;
}
if ( h . flags & FLAG_LASTFILE ) {
httpd_printf ( " End of image .\n " ) ;
httpd_printf ( " File %s not found in EspFS .\n " , fileName ) ;
return NULL ;
}
//Grab the name of the file.
@ -155,7 +161,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
r = ( EspFsFile * ) malloc ( sizeof ( EspFsFile ) ) ; //Alloc file desc mem
// httpd_printf("Alloc %p\n", r);
if ( r = = NULL ) return NULL ;
r - > header = ( EspFsHeader * ) hpos ;
r - > header = ( const EspFsHeader * ) hpos ;
r - > decompressor = h . compression ;
r - > posComp = p ;
r - > posStart = p ;
@ -168,7 +174,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
char parm ;
heatshrink_decoder * dec ;
//Decoder params are stored in 1st byte.
readFlashUnaligned ( & parm , r - > posComp , 1 ) ;
readFlashUnaligned ( & parm , ( char * ) r - > posComp , 1 ) ;
r - > posComp + + ;
httpd_printf ( " Heatshrink compressed file; decode parms = %x \n " , parm ) ;
dec = heatshrink_decoder_alloc ( 16 , ( parm > > 4 ) & 0xf , parm & 0xf ) ;
@ -187,7 +193,8 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
}
//Read len bytes from the given file into buff. Returns the actual amount of bytes read.
int ICACHE_FLASH_ATTR espFsRead ( EspFsFile * fh , char * buff , int len ) {
int ICACHE_FLASH_ATTR espFsRead ( EspFsFile * fh , char * buff , int len )
{
int flen , fdlen ;
if ( fh = = NULL ) return 0 ;
@ -199,7 +206,7 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
toRead = flen - ( fh - > posComp - fh - > posStart ) ;
if ( len > toRead ) len = toRead ;
// httpd_printf("Reading %d bytes from %x\n", len, (unsigned int)fh->posComp);
readFlashUnaligned ( buff , fh - > posComp , len ) ;
readFlashUnaligned ( buff , ( char * ) fh - > posComp , len ) ;
fh - > posDecomp + = len ;
fh - > posComp + = len ;
// httpd_printf("Done reading %d bytes, pos=%x\n", len, fh->posComp);
@ -225,7 +232,7 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
//ToDo: Check ret val of heatshrink fns for errors
elen = flen - ( fh - > posComp - fh - > posStart ) ;
if ( elen > 0 ) {
readFlashUnaligned ( ebuff , fh - > posComp , 16 ) ;
readFlashUnaligned ( ebuff , ( char * ) fh - > posComp , 16 ) ;
heatshrink_decoder_sink ( dec , ( uint8_t * ) ebuff , ( elen > 16 ) ? 16 : elen , & rlen ) ;
fh - > posComp + = rlen ;
}
@ -252,7 +259,8 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
}
//Close the file.
void ICACHE_FLASH_ATTR espFsClose ( EspFsFile * fh ) {
void ICACHE_FLASH_ATTR espFsClose ( EspFsFile * fh )
{
if ( fh = = NULL ) return ;
# ifdef ESPFS_HEATSHRINK
if ( fh - > decompressor = = COMPRESS_HEATSHRINK ) {