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;
}
Code: Select all
FILE *findAndOpenFile(const char *filename, const char *opentype)
{
FILE *thefile = NULL;
thefile = fopen(filename, opentype);
return thefile ;
}
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 ;
}