Linux NVM: updated implementation to use the new standard API

This commit is contained in:
Silvano Seva 2023-11-25 16:12:57 +01:00
parent 096da4102d
commit 419d02a203
2 changed files with 79 additions and 127 deletions

View File

@ -288,7 +288,8 @@ linux_platform_src = ['platform/targets/linux/emulator/emulator.c',
'platform/drivers/audio/audio_linux.c', 'platform/drivers/audio/audio_linux.c',
'platform/drivers/audio/file_source.c', 'platform/drivers/audio/file_source.c',
'platform/targets/linux/platform.c', 'platform/targets/linux/platform.c',
'platform/drivers/CPS/cps_io_libc.c'] 'platform/drivers/CPS/cps_io_libc.c',
'platform/drivers/NVM/posix_file.c']
linux_src = src + linux_platform_src linux_src = src + linux_platform_src

View File

@ -21,28 +21,37 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <interfaces/nvmem.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <posix_file.h>
#include <nvmem_access.h>
#include <interfaces/nvmem.h>
#define _NVM_MAX_PATHLEN 256 #define NVM_MAX_PATHLEN 256
// path indexes for memory_paths POSIX_FILE_DEVICE_DEFINE(stateDevice, NULL, 1024)
enum path_idxs
const struct nvmPartition statePartitions[] =
{ {
P_SETTINGS = 0, {
P_VFO, .offset = 0x0000, // First partition, radio state
P_LEN .size = 512
},
{
.offset = 0x0200, // Second partition, settings
.size = 512
}
}; };
// absolute paths to files storing chunk of memory const struct nvmArea stateArea =
char *memory_paths[P_LEN]; {
.name = "Device state NVM area",
.dev = &stateDevice,
.startAddr = 0x0000,
.size = 1024,
.partitions = statePartitions
};
// Simulate CPS with 16 channels, 16 zones, 16 contacts
const uint32_t maxNumZones = 16;
const uint32_t maxNumChannels = 16;
const uint32_t maxNumContacts = 16;
const freq_t dummy_base_freq = 145500000;
/** /**
* Creates a directory if it does not exist. * Creates a directory if it does not exist.
@ -50,98 +59,47 @@ const freq_t dummy_base_freq = 145500000;
* \param path: the directory path * \param path: the directory path
* \return 0 on success, -1 on failure * \return 0 on success, -1 on failure
*/ */
int _nvm_mkdir(const char *path) static int create_dir(const char *path)
{ {
struct stat sb; struct stat sb;
if(stat(path, &sb) == 0) if(stat(path, &sb) == 0)
{ {
if(S_ISDIR(sb.st_mode)) if(S_ISDIR(sb.st_mode))
{
return 0; return 0;
}
printf("%s is not a directory\n", path); printf("%s is not a directory\n", path);
return -1;
} }
else if(errno == ENOENT) else if(errno == ENOENT)
{ {
if(mkdir(path, 0700) == 0) if(mkdir(path, 0700) == 0)
{
return 0; return 0;
}
printf("Cannot create %s. %s\n", path, strerror(errno)); printf("Cannot create %s. %s\n", path, strerror(errno));
return -1;
} }
else else
{ {
printf("Cannot stat %s. %s", path, strerror(errno)); printf("Cannot stat %s. %s", path, strerror(errno));
return -1;
}
}
/**
* Write binary information into a file
*
* \param path: the file to write
* \param data: the struct to write
* \param size: the size of the data to write
* \return 0 on success, -1 on failure
*/
int _nvm_write(const char *path, const void *data, size_t size)
{
printf("Writing %s\n", path);
FILE * file= fopen(path, "wb");
if(file != NULL)
{
fwrite(data, size, 1, file);
return fclose(file);
} }
return -1; return -1;
} }
/**
* Read binary information into a file
*
* \param path: the file to read
* \param data: the struct to populate with the file content
* \param size: the size of the data to read
* \return 0 on success, -1 on failure
*/
int _cps_read(const char *path, void *data, size_t size)
{
printf("Reading %s\n", path);
FILE * file= fopen(path, "rb");
if(file != NULL)
{
fread(data, size, 1, file);
return fclose(file);
}
return -1;
}
void nvm_init() void nvm_init()
{ {
const char *name = "XDG_STATE_HOME"; const char *env_state_path = getenv("XDG_STATE_HOME");
const char *env_state_path = getenv(name); const char *openrtx = "/OpenRTX/";
const char *openrtx = "/OpenRTX";
char memory_path[_NVM_MAX_PATHLEN]; char memory_path[NVM_MAX_PATHLEN];
if(env_state_path) if(env_state_path)
{ {
if(_nvm_mkdir(env_state_path) != 0) if(create_dir(env_state_path) != 0)
{
exit(1); exit(1);
}
// we append /OpenRTX to env_state_path // we append /OpenRTX to env_state_path
if(strlen(env_state_path) + strlen(openrtx) >= _NVM_MAX_PATHLEN) if(strlen(env_state_path) + strlen(openrtx) >= NVM_MAX_PATHLEN)
goto toolong; goto toolong;
strcpy(memory_path, env_state_path); strcpy(memory_path, env_state_path);
@ -157,56 +115,29 @@ void nvm_init()
const char *state = "/state"; const char *state = "/state";
if(strlen(home) + strlen(local) + strlen(state) + strlen(openrtx) if(strlen(home) + strlen(local) + strlen(state) + strlen(openrtx)
>= _NVM_MAX_PATHLEN) >= NVM_MAX_PATHLEN)
goto toolong; goto toolong;
strcpy(memory_path, home); strcpy(memory_path, home);
strcat(memory_path, local); strcat(memory_path, local);
if(_nvm_mkdir(memory_path) != 0) if(create_dir(memory_path) != 0)
{
exit(1); exit(1);
}
strcat(memory_path, state); strcat(memory_path, state);
if(_nvm_mkdir(memory_path) != 0) if(create_dir(memory_path) != 0)
{
exit(1); exit(1);
}
strcat(memory_path, openrtx); strcat(memory_path, openrtx);
} }
if(_nvm_mkdir(memory_path) != 0) if(create_dir(memory_path) != 0)
{
exit(1); exit(1);
}
// init memory_paths strcat(memory_path, "state.bin");
const char *file_settings = "/settings.dat";
const char *file_vfo = "/vfo.dat";
unsigned long base_len = strlen(memory_path);
for(enum path_idxs i = 0; i < P_LEN; i++) int ret = posixFile_init(&stateDevice, memory_path);
{ if(ret < 0)
const char *path; printf("Opening of state file failed with status %d\n", ret);
switch(i)
{
case P_SETTINGS:
path = file_settings;
break;
case P_VFO:
path = file_vfo;
break;
case P_LEN:
continue;
}
size_t path_size = strlen(path) + base_len + 1;
memory_paths[i] = malloc(path_size);
strcpy(memory_paths[i], memory_path);
strcat(memory_paths[i], path);
}
return; return;
@ -217,10 +148,7 @@ toolong:
void nvm_terminate() void nvm_terminate()
{ {
for(enum path_idxs i = 0; i < P_LEN; i++) posixFile_terminate(&stateDevice);
{
free(memory_paths[i]);
}
} }
void nvm_readHwInfo(hwInfo_t *info) void nvm_readHwInfo(hwInfo_t *info)
@ -231,25 +159,48 @@ void nvm_readHwInfo(hwInfo_t *info)
int nvm_readVfoChannelData(channel_t *channel) int nvm_readVfoChannelData(channel_t *channel)
{ {
return _cps_read(memory_paths[P_VFO], channel, sizeof(channel_t)); int ret = nvmArea_readPartition(&stateArea, 0, 0, channel, sizeof(channel_t));
} if(ret < 0)
return ret;
int nvm_readSettings(settings_t *settings) // TODO: implement a more serious integrity check
{ for(size_t i = 0; i < sizeof(channel_t); i++)
return _cps_read(memory_paths[P_SETTINGS], settings, sizeof(settings_t));
}
int nvm_writeSettings(const settings_t *settings)
{
return _nvm_write(memory_paths[P_SETTINGS], settings, sizeof(settings_t));
}
int nvm_writeSettingsAndVfo(const settings_t *settings, const channel_t *vfo)
{
if(nvm_writeSettings(settings) == 0)
{ {
return _nvm_write(memory_paths[P_VFO], vfo, sizeof(channel_t)); const uint8_t *p = (const uint8_t *) channel;
if(p[i] != 0x00)
return 0;
} }
return -1; return -1;
} }
int nvm_readSettings(settings_t *settings)
{
int ret = nvmArea_readPartition(&stateArea, 1, 0, settings, sizeof(settings_t));
if(ret < 0)
return ret;
// TODO: implement a more serious integrity check
for(size_t i = 0; i < sizeof(settings_t); i++)
{
const uint8_t *p = (const uint8_t *) settings;
if(p[i] != 0x00)
return 0;
}
return -1;
}
int nvm_writeSettings(const settings_t *settings)
{
return nvmArea_writePartition(&stateArea, 1, 0, settings, sizeof(settings_t));
}
int nvm_writeSettingsAndVfo(const settings_t *settings, const channel_t *vfo)
{
int ret = nvmArea_writePartition(&stateArea, 1, 0, settings, sizeof(settings_t));
if(ret < 0)
return ret;
return nvmArea_writePartition(&stateArea, 0, 0, vfo, sizeof(channel_t));
}