Skip to content

Instantly share code, notes, and snippets.

@devyte
Last active June 6, 2018 18:06
Show Gist options
  • Save devyte/22cc237e50d58a03ea43ce37c425cb14 to your computer and use it in GitHub Desktop.
Save devyte/22cc237e50d58a03ea43ce37c425cb14 to your computer and use it in GitHub Desktop.
Templated Certificate Store
#ifndef __CERTSTOREFILE_H__
#define __CERTSTOREFILE_H__
namespace CertStore
{
template <typename FSType>
class CertStoreFile
{
public:
CertStoreFile(FSType &fs, const char *name)
: _fs(fs), _name(name) {
};
// The main API
bool open(bool write = false) {
_file = _fs.open(_name, write ? FSTraits<FSType>::writePermission() : FSTraits<FSType>::readPermission(););
return _file;
}
bool seek(size_t absolute_pos) {
return _file.seek(absolute_pos, SeekSet);
}
size_t read(void *dest, size_t bytes) {
return _file.readBytes((char*)dest, bytes);
}
size_t write(void *dest, size_t bytes) {
return _file.write((uint8_t*)dest, bytes);
}
close() {
_file.close();
}
private:
FSType &_fs;
FSTraits<FSType>::File _file;
const char *_name;
};
template <typename FSType>
CertStoreFile<FSType>
CertStoreFilerGenerate(FSType &fs, const char * name)
{
return CertStoreFile<FSType>(fs, name);
}
} //CertStore
#endif
#if defined(USE_SDCERTSTOREFILE)
#include "SDCertStoreFile.h"
using namespace sd;
#elif defined(USE_SPIFFSCERTSTOREFILE)
#include "SPIFFSCertStoreFile.h"
using namespace spiffs;
#else
//User-defined instantiation with no global-space singletons
//Example: use both
#include "SDCertStoreFile.h"
#include "SPIFFSCertStoreFile.h"
//access to each certstore would have to be via fully qualified name, i.e.: sd::CertStoreFile
#endif
void setup()
{
}
void loop()
{
}
#ifndef __FSTRAITS_H__
#define __FSTRAITS_H__
namespace fs
{
template <typename FSType>
struct FSTraits;
}
#endif
#include "SDCertStoreFile.h"
namespace sd
{
CertStoreFileType CertStoreFile = CertStore::CertStoreFileGenerate(SD, "blah.ar");
}
#ifndef __SDCERTSTOREFILE_H__
#define __SDCERTSTOREFILE_H__
#include "SDFSTraits.h"
#include "CertStoreFile.h"
namespace sd
{
using CertStoreFileType = CertStore::CertStoreFile<SDClass>;
extern CertStoreFileType CertStoreFile;
}
#endif
#ifndef __SDFSTRAITS_H__
#define __SDFSTRAITS_H__
#include "FSTraits.h"
#include "SD.h"
namespace fs
{
template <>
struct FSTraits<SDClass>
{
using File = File;
static const int readPermission() {return FILE_READ;}
static const int writePermission() {return FILE_WRITE;}
};
}
#endif
#include "SPIFFSCertStoreFile.h"
namespace spiffs
{
CertStoreFileType CertStoreFile = CertStore::CertStoreFileGenerate(SPIFFS, "blah.ar");
}
#ifndef __SPIFFSCERTSTOREFILE_H__
#define __SPIFFSCERTSTOREFILE_H__
#include "SPIFFSTraits.h"
#include "CertStoreFile.h"
namespace spiffs
{
using CertStoreFileType = CertStore::CertStoreFile<fs::FS>;
extern CertStoreFileType CertStoreFile;
}
#endif
#ifndef __SPIFFSTRAITS_H__
#define __SPIFFSTRAITS_H__
#include "FSTraits.h"
#include "FS.h"
namespace fs
{
template <>
struct FSTraits<fs::FS>
{
using File = fs::File;
static const char * readPermission() {return "r";}
static const char * writePermission() {return "w";}
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment