-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.cpp
240 lines (209 loc) · 6.51 KB
/
config.cpp
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
/**
config.cpp - Modem persistent configuration
Copyright (C) 2018 Costin STROIE <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
Profile::Profile() {
}
Profile::~Profile() {
}
/**
Try to load the specified stored profile or use the factory defaults
@param cfg the configuration structure
@param slot the stored profile
*/
void Profile::init(CFG_t *cfg, uint8_t slot) {
this->read(cfg, slot, true);
}
/**
CRC8 computing
@param inCrc partial CRC
@param inData new data
@return updated CRC
*/
uint8_t Profile::CRC8(uint8_t inCrc, uint8_t inData) {
uint8_t outCrc = inCrc ^ inData;
for (uint8_t i = 0; i < 8; ++i)
if ((outCrc & 0x80 ) != 0) {
outCrc <<= 1;
outCrc ^= 0x07;
}
else
outCrc <<= 1;
return outCrc;
}
/**
Compute the CRC8 of the configuration structure
@param cfgTemp the configuration structure
@return computed CRC8
*/
uint8_t Profile::crc(CFG_t *cfg) {
// Compute the CRC8 checksum of the data
uint8_t crc8 = 0;
for (uint8_t i = 1; i < eeProfLen; i++)
crc8 = this->CRC8(crc8, cfg->data[i]);
return crc8;
}
/**
Compare two configuration structures
@param cfg1 the first configuration structure
@param cfg1 the second configuration structure
@return true if equal
*/
bool Profile::equal(CFG_t *cfg1, CFG_t *cfg2) {
bool result = true;
// Check CRC first
if (cfg1->crc8 != cfg2->crc8)
result = false;
else
// Compare the overlayed array of the two structures
for (uint8_t i = 1; i < eeProfLen; i++)
if (cfg1->data[i] != cfg2->data[i])
result = false;
return result;
}
/**
Write the configuration to EEPROM, along with CRC8, if different
@param cfg the configuration structure
@param slot the stored profile
@return always true
*/
bool Profile::write(CFG_t *cfg, uint8_t slot) {
// Temporary configuration structure
struct CFG_t cfgTemp;
// Read the data from EEPROM into the temporary structure
EEPROM.get(eeAddress + slot * eeProfLen, cfgTemp.data);
// Compute the CRC8 checksum of the read data
uint8_t crc8 = this->crc(&cfgTemp);
// Compute the CRC8 checksum of the actual data, make sure S1 is zero
cfg->sregs[1] = 0;
cfg->crc8 = this->crc(cfg);
// Compare the new and the stored data and check if the stored data is valid
if (not this->equal(cfg, &cfgTemp) or (cfgTemp.crc8 != crc8)) {
// Copy the actual data to the temporary structure
for (uint8_t i = 0; i < eeProfLen; i++)
cfgTemp.data[i] = cfg->data[i];
// Write the data
EEPROM.put(eeAddress + slot * eeProfLen, cfgTemp.data);
}
// Always return true, even if data is not written
return true;
}
/**
Read the configuration from EEPROM, along with CRC8, and verify
@param cfg the configuration structure
@param slot the stored profile
@param useDefaults use the factory defauls in case of error
@return CRC verification
*/
bool Profile::read(CFG_t *cfg, uint8_t slot, bool useDefaults) {
// Temporary configuration structure
struct CFG_t cfgTemp;
// Read the data from EEPROM into the temporary structure
EEPROM.get(eeAddress + slot * eeProfLen, cfgTemp);
// Compute the CRC8 checksum of the read data
uint8_t crc8 = this->crc(&cfgTemp);
// And compare with the read crc8 checksum, also check S1 and S2
if (cfgTemp.crc8 == crc8 and
cfgTemp.sregs[1] == 0 and
cfgTemp.sregs[2] >= ' ' and
cfgTemp.sregs[2] <= '~')
// Copy the temporary structure to configuration and the crc8
for (uint8_t i = 0; i < eeProfLen; i++)
cfg->data[i] = cfgTemp.data[i];
else if (useDefaults)
factory(cfg);
return (cfgTemp.crc8 == crc8);
}
/**
Reset the configuration to factory defaults
@param cfg the configuration structure
@return always true
*/
bool Profile::factory(CFG_t *cfg) {
// Set the configuration
cfg->compro = 0x10; // ATB
cfg->txcarr = 0x01; // ATC
cfg->cmecho = 0x01; // ATE
cfg->dtecho = 0x01; // ATF
cfg->spklvl = 0x01; // ATL
cfg->spkmod = 0x00; // ATM
cfg->quiet = 0x00; // ATQ
cfg->verbal = 0x01; // ATV
cfg->selcpm = 0x00; // ATX
cfg->dialpt = 0x01; // ATP/T
cfg->revans = 0x00; // AT&A
cfg->dcdopt = 0x01; // AT&C
cfg->dtropt = 0x01; // AT&D
cfg->jcksel = 0x00; // AT&J
cfg->flwctr = 0x00; // AT&K
cfg->lnetpe = 0x00; // AT&L
cfg->plsrto = 0x00; // AT&P
cfg->rtsopt = 0x00; // AT&R
cfg->dsropt = 0x00; // AT&S
// Set the S regs
memcpy_P(&cfg->sregs, &sRegs, 16);
//Always return true
return true;
};
/**
Get the specfied S register value
@param cfg the configuration structure
@param reg the specified S register
@return the register value
*/
uint8_t Profile::sregGet(CFG_t *cfg, uint8_t reg) {
return cfg->sregs[reg];
}
/**
Set the value to specfied S register
@param cfg the configuration structure
@param reg the specified S register
@param value the value to set
*/
void Profile::sregSet(CFG_t *cfg, uint8_t reg, uint8_t value) {
cfg->sregs[reg] = value;
}
/**
Get the stored phone number from the specfied slot
@param phone the phone number
@param slot the storge slot
@return success result
*/
uint8_t Profile::pbGet(char *phone, uint8_t slot) {
// Compute the eeprom address
uint16_t address = eeAddress + eeProfNums * eeProfLen + slot * eePhoneLen;
// Get the chars
for (uint8_t i = 0; i < eePhoneLen; i++) {
phone[i] = EEPROM.read(address + i);
if (phone[i] == '\0') break;
}
// Minimal validation
if (phone[0] != '\0' and strchr_P(PSTR("TP0123456789*#"), phone[0]) == NULL)
phone[0] = '\0';
}
/**
Set the phone number from the specfied slot
@param phone the phone number
@param slot the storge slot
*/
void Profile::pbSet(char *phone, uint8_t slot) {
// Compute the eeprom address
uint16_t address = eeAddress + eeProfNums * eeProfLen + slot * eePhoneLen;
// Store the chars only if changed
for (uint8_t i = 0; i < eePhoneLen; i++) {
if (EEPROM.read(address + i) != phone[i])
EEPROM.write(address + i, phone[i]);
if (phone[i] == '\0') break;
}
}