-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cpp
More file actions
128 lines (93 loc) · 3.88 KB
/
Copy pathConfiguration.cpp
File metadata and controls
128 lines (93 loc) · 3.88 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
//
// Configuration.cpp
//
// The configuration data for the device is held in EEPROM (or flash) and the
// code in this module is here to save and retrieve the configuration data.
//
#include <HardwareSerial.h>
#include <EEPROM.h>
#include "Configuration.h"
// The following method is used to calculate a 16 bit checksum for the data held
// in the configuration. The algorithm is details here:
//
// https://en.wikipedia.org/wiki/Fletcher%27s_checksum
uint16_t Fletcher16( uint8_t *data, int count )
{
uint16_t sum1 = 0;
uint16_t sum2 = 0;
int index;
for( index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
void dumpConfiguration(struct Configuration *configuration)
{
Serial.println("Configuration");
Serial.printf(" MajorVersion: ........... %d\n",configuration->MajorVersion);
Serial.printf(" MinorVersion: ........... %d\n",configuration->MinorVersion);
Serial.printf(" AccessPointSSID: ........ %s\n",configuration->AccessPointSSID);
Serial.printf(" AccessPointPassword: .... %s\n",configuration->AccessPointPassword);
Serial.printf(" TCPPort: ................ %d\n",configuration->TCPPort);
Serial.printf(" MaximumTCPClientCount: .. %d\n",configuration->MaximumTCPClientCount);
Serial.printf(" BlueToothEnabled: ....... %s\n",true == configuration->BlueToothEnabled ? "true" : "false");
Serial.printf(" BlueToothDeviceName: .... %s\n",configuration->BlueToothDeviceName);
Serial.printf(" NMEABaudRate: ........... %d\n",configuration->NMEABaudRate);
}
//
// This method is called to read the configuration information from the EEPROM
//
bool readConfiguration(struct Configuration *configuration)
{
// Activate EEPROM access
EEPROM.begin(EEPROM_SIZE);
// We read from the EEPROM byte by byte, Allocate a structure to read
// into and locate the start of it.
struct EEPROM_DATA eepromData;
char *q = (char *)&eepromData;
// Read all the data from the EEPROM, this will include the checksum
for(int i = 0; i < sizeof(EEPROM_DATA); i++)
*q++ = EEPROM.read(i);
// End the EEPROM access
EEPROM.end();
// Either way, if the checksum is correct or not we will copy over the
// configuration information read back.
memcpy(configuration,&eepromData.Configuration,sizeof(Configuration));
// Display the stored size of checksum
Serial.print("readConfiguration, Size = ");
Serial.println(eepromData.Size);
Serial.print("readConfiguration, CheckSum = ");
Serial.println(eepromData.CheckSum);
// We can now check the checksum
uint16_t checkSum = Fletcher16((uint8_t *)&eepromData.Configuration,sizeof(Configuration));
Serial.print("readConfiguration, expected CheckSum = ");
Serial.println(checkSum);
// Return the a Bool to indicate if all is OK?
return (sizeof(Configuration) == eepromData.Size) && (checkSum == eepromData.CheckSum);
}
//
// This method is called to write the configuration information to the EEPROM
//
void writeConfiguration(struct Configuration *configuration)
{
// Activate EEPROM access
EEPROM.begin(EEPROM_SIZE);
// We read need to allocate an EEPROM_DATA object, populate it with the
// configuration information passed in, and update size and the checksum.
struct EEPROM_DATA eepromData;
memcpy(&eepromData.Configuration,configuration,sizeof(Configuration));
eepromData.Size = sizeof(Configuration);
eepromData.CheckSum = Fletcher16((uint8_t *)&eepromData.Configuration,sizeof(Configuration));
Serial.print("writeConfiguration, checksum = ");
Serial.println(eepromData.CheckSum);
// We write the data out byte by byte so we need a character pointer to
// the start of the data.
char *p = (char *)&eepromData;
// Write out the data
for(int i = 0; i < sizeof(EEPROM_DATA); i++)
EEPROM.write(i,*p++);
// End the EEPROM access
EEPROM.end();
}