Repository navigation
Expand file tree
/
Copy pathPrefHandler.cpp
More file actions
274 lines (235 loc) · 7.41 KB
/
Copy pathPrefHandler.cpp
File metadata and controls
274 lines (235 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* PrefHandler.cpp
*
* Abstracts away the particulars of how preferences are stored.
* Transparently supports main and "last known good" storage and retrieval
*
Copyright (c) 2013 Collin Kidder, Michael Neuweiler, Charles Galpin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "PrefHandler.h"
/*
* Each device must initialize its own PrefHandler with its ID.
* The device is looked up in the device table in the EEPROM and added if necessary.
*/
PrefHandler::PrefHandler(DeviceId id_in)
{
uint16_t id;
deviceId = id_in;
enabled = false;
lkg_address = EE_MAIN_OFFSET;
initDeviceTable();
position = findDevice(deviceId);
if (position > -1) {
memCache.Read(EE_DEVICE_TABLE + (2 * position), &id);
if (id & 0x8000) {
enabled = true;
}
base_address = EE_DEVICES_BASE + (EE_DEVICE_SIZE * position);
Logger::debug("Device ID %#x was found in device table at entry %i", (int) deviceId, position);
return;
}
//if we got here then there was no entry for this device in the table yet.
//try to find an empty spot and place it there.
position = findDevice(NEW);
if (position > -1) {
base_address = EE_DEVICES_BASE + (EE_DEVICE_SIZE * position);
lkg_address = EE_MAIN_OFFSET;
memCache.Write(EE_DEVICE_TABLE + (2 * position), (uint16_t)deviceId);
Logger::debug("Device ID: %#x was placed into device table at entry: %i", (int) deviceId, position);
return;
}
//we found no matches and could not allocate a space. This is bad. Error out here
base_address = 0xF0F0;
Logger::error("PrefManager - Device Table Full (Device ID: %#x) !!!", deviceId);
}
PrefHandler::~PrefHandler()
{
}
/*
* Initialize the device table area in the eeprom if GEVCU marker is missing
*/
void PrefHandler::initDeviceTable()
{
uint16_t id;
memCache.Read(EE_DEVICE_TABLE, &id);
if (id == EE_GEVCU_MARKER) { // the device table was properly initialized
return;
}
Logger::debug("Initializing EEPROM device table");
//initialize table with zeros
id = 0;
for (int x = 1; x <= EE_NUM_DEVICES; x++) {
memCache.Write(EE_DEVICE_TABLE + (2 * x), id);
}
//write out magic entry
id = EE_GEVCU_MARKER;
memCache.Write(EE_DEVICE_TABLE, id);
}
/*
* Is the device enabled in the configuration
*/
bool PrefHandler::isEnabled()
{
return enabled;
}
/*
* Enable / disable a device
*/
bool PrefHandler::setEnabled(bool en)
{
uint16_t id = deviceId;
enabled = en;
if (enabled) {
id |= 0x8000; //set enabled bit
} else {
id &= 0x7FFF; //clear enabled bit
}
return memCache.Write(EE_DEVICE_TABLE + (2 * position), id);
}
/*
* Search the device table for a device with a given ID
*/
int8_t PrefHandler::findDevice(DeviceId deviceId)
{
uint16_t id;
for (int pos = 1; pos <= EE_NUM_DEVICES; pos++) {
memCache.Read(EE_DEVICE_TABLE + (2 * pos), &id);
if ((id & 0x7FFF) == deviceId) {
return pos;
}
}
return -1;
}
/*
* Enable/Disable the LKG (last known good) configuration
*/
void PrefHandler::LKG_mode(bool mode)
{
if (mode) {
lkg_address = EE_LKG_OFFSET;
} else {
lkg_address = EE_MAIN_OFFSET;
}
}
/*
* Write one byte to an address relative to the device's base
*/
bool PrefHandler::write(uint16_t address, uint8_t val)
{
if (address >= EE_DEVICE_SIZE) {
return false;
}
return memCache.Write((uint32_t) address + base_address + lkg_address, val);
}
/*
* Write two bytes to n address relative to the device's base
*/
bool PrefHandler::write(uint16_t address, uint16_t val)
{
if (address >= EE_DEVICE_SIZE) {
return false;
}
return memCache.Write((uint32_t) address + base_address + lkg_address, val);
}
/*
* Write four bytes to an address relative to the device's base
*/
bool PrefHandler::write(uint16_t address, uint32_t val)
{
if (address >= EE_DEVICE_SIZE) {
return false;
}
return memCache.Write((uint32_t) address + base_address + lkg_address, val);
}
/*
* Read one byte from an address relative to the device's base
*/
bool PrefHandler::read(uint16_t address, uint8_t *val)
{
if (address >= EE_DEVICE_SIZE) {
return false;
}
return memCache.Read((uint32_t) address + base_address + lkg_address, val);
}
/*
* Read two bytes from an address relative to the device's base
*/
bool PrefHandler::read(uint16_t address, uint16_t *val)
{
if (address >= EE_DEVICE_SIZE) {
return false;
}
return memCache.Read((uint32_t) address + base_address + lkg_address, val);
}
/*
* Read four bytes from an address relative to the device's base
*/
bool PrefHandler::read(uint16_t address, uint32_t *val)
{
if (address >= EE_DEVICE_SIZE) {
return false;
}
return memCache.Read((uint32_t) address + base_address + lkg_address, val);
}
/*
* Calculate the checksum for a device configuration (block of EE_DEVICE_SIZE bytes)
*/
uint8_t PrefHandler::calcChecksum()
{
uint16_t counter;
uint8_t accum = 0;
uint8_t temp;
for (counter = 1; counter < EE_DEVICE_SIZE; counter++) {
memCache.Read((uint32_t) counter + base_address + lkg_address, &temp);
accum += temp;
}
return accum;
}
/*
* Calculate the current checksum and save it to the proper place within the device config (EE_CHECKSUM)
*/
void PrefHandler::saveChecksum()
{
uint8_t csum;
csum = calcChecksum();
memCache.Write(EE_CHECKSUM + base_address + lkg_address, csum);
}
/*
* Get checksum from EEPROM and calculate the current checksum to see if they match
*/
bool PrefHandler::checksumValid()
{
uint8_t stored_chk, calc_chk;
memCache.Read(EE_CHECKSUM + base_address + lkg_address, &stored_chk);
calc_chk = calcChecksum();
if (stored_chk == calc_chk) {
Logger::debug("%#x valid checksum, using stored config values", deviceId);
return true;
} else {
Logger::warn("#x invalid checksum, using hard coded config values (stored: %#x, calc: %#x", deviceId, stored_chk, calc_chk);
return false;
}
}
/*
* Write all dirty pages of the cache to the eeprom
*/
void PrefHandler::forceCacheWrite()
{
memCache.FlushAllPages();
}