Skip to content

Commit

Permalink
feat: Adds mmap backend for nvram
Browse files Browse the repository at this point in the history
  • Loading branch information
ntlhui committed Dec 23, 2024
1 parent 5b40a24 commit 8c86d22
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/sys/NVRAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
*/
#include "NVRAM.hpp"


#include "cli/conio.hpp"
#include "consts.hpp"
#include "product.hpp"

#include <cstddef>

#if SF_PLATFORM == SF_PLATFORM_PARTICLE
#include "Particle.h"
#elif SF_PLATFORM == SF_PLATFORM_GLIBC
#endif

NVRAM& NVRAM::getInstance(void)
{
Expand Down
75 changes: 66 additions & 9 deletions src/sys/NVRAM.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
#ifndef __NVRAM_HPP__
#define __NVRAM_HPP__

#include "product.hpp"
#if SF_PLATFORM == SF_PLATFORM_PARTICLE
#include "Particle.h"
#elif SF_PLATFORM == SF_PLATFORM_GLIBC
#include "cli/conio.hpp"
#include "consts.hpp"

#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif

#include <cstdint>
class NVRAM
Expand Down Expand Up @@ -55,7 +66,7 @@ class NVRAM
* @param pData Data
* @return int Sucsess
*/
template <typename T> int get(DATA_ID_e dataID, T& pData)
template <typename T> int get(DATA_ID_e dataID, T &rData)
{
uint32_t addr;
const NVRAM::nvramTableEntry_t* tableEntry;
Expand All @@ -66,13 +77,17 @@ class NVRAM
return 0;
}

if(sizeof(pData) != tableEntry->len)
if (sizeof(rData) != tableEntry->len)
{
return 0;
}

addr = tableEntry->addr;
#if SF_PLATFORM == SF_PLATFORM_PARTICLE
EEPROM.get(addr, pData);
#elif SF_PLATFORM == SF_PLATFORM_GLIBC
std::memcpy(&rData, eeprom_mmap + addr, sizeof(T));
#endif
return 1;
}

Expand All @@ -84,7 +99,7 @@ class NVRAM
* @param pData data
* @return int Sucsess value
*/
template <typename T> int put(DATA_ID_e dataID, const T& pData)
template <typename T> int put(DATA_ID_e dataID, const T &rData)
{
uint32_t addr;
const NVRAM::nvramTableEntry_t* tableEntry;
Expand All @@ -95,23 +110,65 @@ class NVRAM
return 0;
}

if(sizeof(pData) != tableEntry->len)
if (sizeof(rData) != tableEntry->len)
{
return 0;
}

addr = tableEntry->addr;
#if SF_PLATFORM == SF_PLATFORM_PARTICLE
EEPROM.put(addr, pData);
#elif SF_PLATFORM == SF_PLATFORM_GLIBC
std::memcpy(eeprom_mmap + addr, &rData, sizeof(T));
#endif
return 1;
}


void displayNVRAM(void);

private:
const nvramTableEntry_t* getTableEntry(DATA_ID_e);
NVRAM(){}
NVRAM(NVRAM const&);
void operator=(NVRAM const&);
#if SF_PLATFORM == SF_PLATFORM_GLIBC
int eeprom_fp;
char *eeprom_mmap = 0;
size_t eeprom_length = 0;
#endif
const nvramTableEntry_t *getTableEntry(DATA_ID_e);
NVRAM()
{
#if SF_PLATFORM == SF_PLATFORM_GLIBC
eeprom_fp = open("eeprom.bin", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
if (-1 == eeprom_fp)
{
SF_OSAL_printf("Failed to open! %s" __NL__, strerror(errno));
}
for (std::size_t idx = 0; idx < NUM_DATA_IDs; idx++)
{
if (LAYOUT[idx].addr + LAYOUT[idx].len > eeprom_length)
{
eeprom_length = LAYOUT[idx].addr + LAYOUT[idx].len;
}
}
if (fallocate(eeprom_fp, 0, 0, eeprom_length))
{
SF_OSAL_printf("Failed to expand file! %s" __NL__, strerror(errno));
}
eeprom_mmap = (char *)mmap(
nullptr, eeprom_length, PROT_READ | PROT_WRITE, MAP_PRIVATE, eeprom_fp, 0);
if (MAP_FAILED == eeprom_mmap)
{
SF_OSAL_printf("Failed to create mmap! %s" __NL__, strerror(errno));
}
#endif
}
~NVRAM()
{
#if SF_PLATFORM == SF_PLATFORM_GLIBC
munmap(eeprom_mmap, eeprom_length);
close(eeprom_fp);
#endif
}
NVRAM(NVRAM const &);
void operator=(NVRAM const &);
};
#endif

0 comments on commit 8c86d22

Please sign in to comment.