VFS serve file from RAM?

phatpaul
Posts: 109
Joined: Fri Aug 24, 2018 1:14 pm

VFS serve file from RAM?

Postby phatpaul » Fri Apr 09, 2021 1:29 am

My application has an http server and a gatt server which read/write to both a FATfs and ESPFS (readonly) using VFS.

Now, I need to serve a file from NVS which I can read into RAM with nvs_get_blob(). Is it feasible to mount a file in NVS (or RAM) to VFS somehow so I don't have to mess with my webserver and ble code? i.e. I just pass a new VFS path like "/ramfs/myfile.json".

My webserver cgi takes the file like this: (GATTS is essentially the same)

Code: Select all

	file = findAndOpenFile(filename, "r");
...
	len=fread(buff, 1, FILE_CHUNK_LEN, file);
	if (len>0) httpdSend(connData, buff, len);
	if (len!=FILE_CHUNK_LEN) {
		//We're done.
		fclose(file);
		ESP_LOGD(__func__, "fclose: %s, r", filename);

		return HTTPD_CGI_DONE;
	} else {
		//Ok, till next time.
		return HTTPD_CGI_MORE;
	}
where findAndOpenFile() is essentially:

Code: Select all

FILE *findAndOpenFile(const char *filename, const char *opentype)
{
    FILE *thefile = NULL;
    thefile = fopen(filename, opentype);
    return thefile ;
   }
I'd prefer to only modify findAndOpenFile(). Is there a way to return a FILE pointer which just references a blob in RAM?
i.e.

Code: Select all

FILE *findAndOpenFile(const char *filename, const char *opentype)
{
    FILE *thefile = NULL;
    bool using_nvs = // todo: determine this from the path, i.e. begins with "/nvs/"
    if (using_nvs) 
    {
        size_t blob_size;
        char *blobBuf = NULL;
	err = nvs_get_blob(fdata_nvs_handle, cgistate->key, NULL, &blob_size); //  WinAPI-style length query.
	if (err != ESP_OK)
	{
		return NULL;
	}

	blobBuf = heap_caps_malloc(blob_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); // Force allocate to SPIRAM (external RAM)
	if (blobBuf == NULL)
	{
		ESP_LOGE(TAG, "out of mem");
		return NULL;
	}
	err = nvs_get_blob(fdata_nvs_handle, cgistate->key, cgistate->blobBuf, &blob_size);
	
	//do some magic here to create a FILE which references blobBuf
	thefile = createRamFile(blobBuf,blob_size);
    } 
    else 
    {
        thefile = fopen(filename, "r");
    }
    return thefile ;
}

phatpaul
Posts: 109
Joined: Fri Aug 24, 2018 1:14 pm

Re: VFS serve file from RAM?

Postby phatpaul » Fri Apr 09, 2021 9:59 pm

bump? I added some more details to my question above.

phatpaul
Posts: 109
Joined: Fri Aug 24, 2018 1:14 pm

Re: VFS serve file from RAM?

Postby phatpaul » Sat Apr 10, 2021 2:35 am

FYI I got it working with:

Code: Select all

return fmemopen(buffer, length, "r"); // return a file that points to the allocated RAM

Who is online

Users browsing this forum: HighVoltage and 116 guests