IoAbstraction
MockEepromAbstraction.h
1 #ifndef _MOCK_EEPROM_ABSTRACTION_H_
2 #define _MOCK_EEPROM_ABSTRACTION_H_
3 
4 #include "EepromAbstraction.h"
5 #include "IoLogging.h"
6 
21 private:
22  bool errorFlag;
23  uint8_t *data;
24  unsigned int memSize;
25 public:
26  explicit MockEepromAbstraction(unsigned int size = 128) {
27  data = new uint8_t[size];
28  errorFlag = false;
29  memSize = size;
30  memset(data, 0, memSize);
31  }
32  ~MockEepromAbstraction() override {
33  delete[] data;
34  }
35 
36  bool hasErrorOccurred() override { return errorFlag;}
37  void clearError() {errorFlag = false;}
38 
39  void checkBounds(EepromPosition pos, int len) {
40  if(uint16_t(pos + len) >= memSize) {
41  serlogF2(SER_DEBUG, "checkbounds exceeded: ", pos+len);
42  errorFlag = true;
43  }
44  }
45 
46  void reset() {
47  memset(data, 0, memSize);
48  }
49 
50  uint8_t read8(EepromPosition position) override {
51  checkBounds(position, 1);
52  return data[position];
53  }
54 
55  void write8(EepromPosition position, uint8_t val) override {
56  checkBounds(position, 1);
57  data[position] = val;
58  }
59 
60  uint16_t read16(EepromPosition position) override {
61  return read8(position) | (read8(position + 1) << 8);
62  }
63 
64  void write16(EepromPosition position, uint16_t val) override {
65  write8(position, val);
66  write8(position + 1, val >> 8);
67  }
68 
69  virtual uint32_t read32(EepromPosition position) override {
70  checkBounds(position, 4);
71  return (uint32_t)read8(position) | ((uint32_t)read8(position + 1) << 8) | ((uint32_t)read8(position + 2) << 16) | ((uint32_t)read8(position + 3) << 24);
72  }
73 
74  virtual void write32(EepromPosition position, uint32_t val) override {
75  checkBounds(position, 4);
76  write8(position, val);
77  write8(position + 1, val >> 8);
78  write8(position + 2, val >> 16);
79  write8(position + 3, val >> 24);
80  }
81 
82  virtual void readIntoMemArray(uint8_t* memDest, EepromPosition romSrc, uint8_t len) override {
83  checkBounds(romSrc, len);
84  memcpy(memDest, &data[romSrc], len);
85  }
86 
87  virtual void writeArrayToRom(EepromPosition romDest, const uint8_t* memSrc, uint8_t len) override {
88  checkBounds(romDest, len);
89  memcpy(&data[romDest], memSrc, len);
90  }
91 
92  void serPrintContents(int start, int len) {
93  if(len >= 63) {
94  serlogF(SER_DEBUG, "Mock rom debug - len too big");
95  return;
96  }
97  char str[64];
98  str[0] = 0;
99  int i;
100  for(i=0; i<len; i++) {
101  if(data[i + start] >= 30)
102  str[i] = data[i + start];
103  else
104  str[i] = '?';
105  }
106  str[i] = 0;
107  serlogF4(SER_DEBUG, "MockRom contents start, len, data: ", start, len, str);
108  }
109 };
110 
111 #endif
Wraps up EEPROM support in a way that is compatible between implementations. For example presently th...
uint16_t EepromPosition
Definition: EepromAbstraction.h:26
Definition: EepromAbstraction.h:33
This file contains implementations of the EepromAbstraction that are useful for dev & testing....
Definition: MockEepromAbstraction.h:20
void write16(EepromPosition position, uint16_t val) override
Definition: MockEepromAbstraction.h:64
virtual uint32_t read32(EepromPosition position) override
Definition: MockEepromAbstraction.h:69
virtual void readIntoMemArray(uint8_t *memDest, EepromPosition romSrc, uint8_t len) override
Definition: MockEepromAbstraction.h:82
bool hasErrorOccurred() override
Definition: MockEepromAbstraction.h:36
uint16_t read16(EepromPosition position) override
Definition: MockEepromAbstraction.h:60
uint8_t read8(EepromPosition position) override
Definition: MockEepromAbstraction.h:50
virtual void write32(EepromPosition position, uint32_t val) override
Definition: MockEepromAbstraction.h:74
virtual void writeArrayToRom(EepromPosition romDest, const uint8_t *memSrc, uint8_t len) override
Definition: MockEepromAbstraction.h:87
void write8(EepromPosition position, uint8_t val) override
Definition: MockEepromAbstraction.h:55