-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
332 lines (288 loc) · 7.99 KB
/
utils.cpp
File metadata and controls
332 lines (288 loc) · 7.99 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*!
* Copyright (c) 2021 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file utils.cpp
* @date 03 Jan 2024
* @version 2.1.5
*
* @brief utils
*
*
*/
#define _GNU_SOURCE
/* own header include */
#include "utils.h"
#include <ArduinoJson.h>
#include <math.h>
uint64_t utils::_tick_ms;
uint64_t utils::_tick_over_flow_cnt;
SdFat utils::_sd;
RTC_PCF8523 utils::_rtc;
char utils::_file_seed[DATA_LOG_FILE_SEED_SIZE];
uint32_t utils::_file_data_pos = 0;
bool utils::_is_conf_available;
/*!
* @brief This function creates the random alphanumeric file seed for the log file
*/
void utils::create_file_seed()
{
/* setup an alphanumeric characters buffer to choose from */
const char *letters = "abcdefghijklmnopqrstuvwxyz0123456789";
/* for each character of the file seed, randomly select one from the buffer */
for (uint32_t seed_char = 0; seed_char < 16; seed_char++)
{
uint32_t rand_c = random(0, 36);
_file_seed[seed_char] = letters[rand_c];
}
}
/*!
* @brief This function is a callback function to set the correct date for modified SD-card files
*/
void utils::date_time(uint16_t* date, uint16_t* time)
{
DateTime now = _rtc.now();
/* return date using FAT_DATE macro to format fields */
*date = FAT_DATE(now.year(), now.month(), now.day());
/* return time using FAT_TIME macro to format fields */
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
/*!
* @brief This function initializes the module
*/
demo_ret_code utils::begin()
{
demo_ret_code ret_code = EDK_OK;
if (!_sd.begin(PIN_SD_CS, SPI_EIGHTH_SPEED))
{
ret_code = EDK_SD_CARD_INIT_ERROR;
}
else if (!_rtc.begin())
{
ret_code = EDK_DATALOGGER_RTC_BEGIN_WARNING;
}
else if (!_rtc.initialized() || _rtc.lostPower())
{
_rtc.adjust(DateTime(F(__DATE__), F(__TIME__)) - TimeSpan((int32_t)(TIMEZONE * 3600)));
ret_code = EDK_DATALOGGER_RTC_ADJUST_WARNING;
}
randomSeed(analogRead(0));
create_file_seed();
SdFile::dateTimeCallback(date_time);
return ret_code;
}
/*!
* @brief This function retrieves the rtc handle
*/
RTC_PCF8523& utils::get_rtc()
{
return _rtc;
}
/*!
* @brief This function retrieves the created file seed
*/
String utils::get_file_seed()
{
return String(_file_seed);
}
/*!
* @brief This function creates a mac address string
*/
String utils::get_mac_address()
{
uint64_t mac = ESP.getEfuseMac();
char *mac_ptr = (char*)&mac;
char mac_str[13];
sprintf(mac_str, "%02X%02X%02X%02X%02X%02X", mac_ptr[0], mac_ptr[1], mac_ptr[2], mac_ptr[3], mac_ptr[4], mac_ptr[5]);
return String(mac_str);
}
/*!
* @brief This function creates a date string
*/
String utils::get_date_time()
{
char time_buffer[20];
DateTime date = _rtc.now();
sprintf(time_buffer, "%d_%02d_%02d_%02d_%02d", date.year(), date.month(), date.day(), date.hour(), date.minute());
return String(time_buffer);
}
/*!
* @brief This function retrieves the first file with provided file extension
*/
bool utils::get_file_with_extension(String& fName, const String& extension)
{
File root;
File file;
char file_name[90];
if (root.open("/"))
{
while (file.openNext(&root, O_READ))
{
if (file.isFile())
{
file.getName(file_name, sizeof(file_name));
if (String(file_name).endsWith(extension))
{
file.close();
fName = String(file_name);
return true;
}
}
file.close();
}
}
return false;
}
/*!
* @brief This function retrieves the latest file with provided file extension
*/
bool utils::get_latest_file_with_extension(String& fName, const String& extension)
{
File root;
File file;
char file_name[90];
bool flag = false;
if (root.open("/"))
{
while (file.openNext(&root, O_READ))
{
if (file.isFile())
{
file.getName(file_name, sizeof(file_name));
if (String(file_name).endsWith(extension))
{
file.close();
fName = String(file_name);
flag = true;
}
}
file.close();
}
}
if (flag)
{
return true;
}
else
{
return false;
}
}
/*!
* @brief This function retrives the bsec configuration string from the provided file
*/
demo_ret_code utils::get_bsec_config(const String& file_name, uint8_t config_str[BSEC_MAX_PROPERTY_BLOB_SIZE])
{
demo_ret_code ret_code = EDK_OK;
uint32_t config_str_len;
File configFile;
if (!configFile.open(file_name.c_str(), O_RDWR))
{
ret_code = EDK_BSEC_CONFIG_STR_FILE_ERROR;
}
else if (configFile.read(&config_str_len, sizeof(uint32_t)) != sizeof(uint32_t))
{
ret_code = EDK_BSEC_CONFIG_STR_READ_ERROR;
}
else if (config_str_len != BSEC_MAX_PROPERTY_BLOB_SIZE)
{
ret_code = EDK_BSEC_CONFIG_STR_SIZE_ERROR;
}
else if (configFile.read(config_str, config_str_len) != config_str_len)
{
ret_code = EDK_BSEC_CONFIG_STR_READ_ERROR;
}
return ret_code;
}
/*!
* @brief This function returns the tick value (ms)
*/
uint64_t utils::get_tick_ms(void)
{
uint64_t time_ms = millis();
if (_tick_ms > time_ms) /* An overflow occurred */
{
_tick_over_flow_cnt++;
}
_tick_ms = time_ms;
return time_ms + (_tick_over_flow_cnt * INT64_C(0xFFFFFFFF));
}
/*!
* @brief : This function reads the size of bytes from the file of given fileExtension
*/
demo_ret_code utils::read_file(const String& file_extension, size_t size, char *file_data)
{
File file;
demo_ret_code ret_code;
static String file_name;
static bool first_time = true;
memset(file_data, 0, size); /* Clears the previous data if any */
ret_code = utils::begin(); /* Initializes the SD card module */
size = size - 1;
if (ret_code >= EDK_OK)
{
if (first_time)
{
if (file_extension == BME68X_LABEL_INFO_FILE_EXT)
{
/* check for a file with given extension */
_is_conf_available = utils::get_latest_file_with_extension(file_name, file_extension);
}
else
{
/* check for a file with given extension */
_is_conf_available = utils::get_file_with_extension(file_name, file_extension);
}
}
if (_is_conf_available)
{
if (!file.open(file_name.c_str(), O_READ)) /* open the given file in read mode */
{
return EDK_FILE_OPEN_ERROR;
}
first_time = false;
file.seek(_file_data_pos); /* sets the position of the file to a particular byte */
if (file.available())
{
file.read(file_data, size); /* reads the given number of bytes of data */
_file_data_pos = file.position(); /* save the file position indicator for next read */
file.close(); /* closes the file */
return EDK_OK;
}
file.close();
_file_data_pos = 0; /* resets the file position indicator */
first_time = true;
return EDK_END_OF_FILE;
}
return EDK_EXTENSION_NOT_AVAILABLE;
}
return ret_code;
}