-
Notifications
You must be signed in to change notification settings - Fork 4
/
eth_save_process.c
301 lines (258 loc) · 9.26 KB
/
eth_save_process.c
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
#include "eth_save_process.h"
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include <locale/locale.h>
#define TAG "EthSave"
#define STORAGE_FILE_BUF_LEN 50
// fuction spizzhena from archive_favorites.c
static bool storage_read_line(File* file, FuriString* str_result) {
furi_string_reset(str_result);
uint8_t buffer[STORAGE_FILE_BUF_LEN];
bool result = false;
do {
uint16_t read_count = storage_file_read(file, buffer, STORAGE_FILE_BUF_LEN);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
for(uint16_t i = 0; i < read_count; i++) {
if(buffer[i] == '\n') {
uint32_t position = storage_file_tell(file);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
position = position - read_count + i + 1;
storage_file_seek(file, position, true);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
result = true;
break;
} else {
furi_string_push_back(str_result, buffer[i]);
}
}
if(result || read_count == 0) {
break;
}
} while(true);
return result;
}
static bool storage_printf(File* file, const char* format, ...) {
va_list args;
va_start(args, format);
FuriString* fstring = furi_string_alloc_vprintf(format, args);
va_end(args);
if(storage_file_write(file, furi_string_get_cstr(fstring), furi_string_size(fstring)) &&
storage_file_write(file, "\n", 1)) {
furi_string_free(fstring);
return true;
}
furi_string_free(fstring);
return false;
}
static bool storage_write_config(File* file, const EthernetSaveConfig* cfg) {
storage_file_seek(file, 0, true);
storage_file_truncate(file);
bool result = true;
result = result ? storage_printf(
file,
"mac: %02X-%02X-%02X-%02X-%02X-%02X",
cfg->mac[0],
cfg->mac[1],
cfg->mac[2],
cfg->mac[3],
cfg->mac[4],
cfg->mac[5]) :
result;
result = result ?
storage_printf(
file, "ip: %d.%d.%d.%d", cfg->ip[0], cfg->ip[1], cfg->ip[2], cfg->ip[3]) :
result;
result =
result ?
storage_printf(
file, "mask: %d.%d.%d.%d", cfg->mask[0], cfg->mask[1], cfg->mask[2], cfg->mask[3]) :
result;
result = result ? storage_printf(
file,
"gateway: %d.%d.%d.%d",
cfg->gateway[0],
cfg->gateway[1],
cfg->gateway[2],
cfg->gateway[3]) :
result;
result =
result ?
storage_printf(
file, "dns: %d.%d.%d.%d", cfg->dns[0], cfg->dns[1], cfg->dns[2], cfg->dns[3]) :
result;
result = result ? storage_printf(
file,
"ping_ip: %d.%d.%d.%d",
cfg->ping_ip[0],
cfg->ping_ip[1],
cfg->ping_ip[2],
cfg->ping_ip[3]) :
result;
return result;
}
static void read_02X(const char* str, uint8_t* byte) {
uint8_t b[2] = {str[0], str[1]};
for(int i = 0; i < 2; ++i) {
if('0' <= b[i] && b[i] <= '9') {
b[i] -= '0';
} else if('A' <= b[i] && b[i] <= 'F') {
b[i] -= 'A' - 0x0A;
} else {
b[i] = 0;
}
}
*byte = b[1] + (b[0] << 4);
}
static void read_ip(const char* str, uint8_t* ip) {
uint8_t ip_i = 0;
uint8_t i = 0;
uint16_t tmp = 0;
for(;;) {
if('0' <= str[i] && str[i] <= '9') {
tmp = tmp * 10 + str[i] - '0';
} else if(str[i] == '.' || str[i] != '\n' || str[i] != '\0') {
if(tmp <= 0xFF && ip_i < 4) {
ip[ip_i] = tmp;
ip_i += 1;
tmp = 0;
} else {
break;
}
if(str[i] != '\n' && ip_i == 4) {
return;
}
} else {
break;
}
i += 1;
}
FURI_LOG_E(TAG, "cannot parse as ip string [%s]", str);
ip[0] = ip[1] = ip[2] = ip[3] = 0;
return;
}
static void set_default_config(EthernetSaveConfig* cfg) {
const uint8_t def_mac[6] = ETHERNET_SAVE_DEFAULT_MAC;
const uint8_t def_ip[4] = ETHERNET_SAVE_DEFAULT_IP;
const uint8_t def_mask[4] = ETHERNET_SAVE_DEFAULT_MASK;
const uint8_t def_gateway[4] = ETHERNET_SAVE_DEFAULT_GATEWAY;
const uint8_t def_dns[4] = ETHERNET_SAVE_DEFAULT_DNS;
const uint8_t def_ping_ip[4] = ETHERNET_SAVE_DEFAULT_PING_IP;
memcpy(cfg->mac, def_mac, 6);
memcpy(cfg->ip, def_ip, 4);
memcpy(cfg->mask, def_mask, 4);
memcpy(cfg->gateway, def_gateway, 4);
memcpy(cfg->dns, def_dns, 4);
memcpy(cfg->ping_ip, def_ping_ip, 4);
}
bool storage_read_config(File* file, EthernetSaveConfig* cfg) {
FuriString* fstring = furi_string_alloc();
while(storage_read_line(file, fstring)) {
const char* str = furi_string_get_cstr(fstring);
if(!strncmp(str, "mac: ", 5)) {
read_02X(str + strlen("mac: "), &cfg->mac[0]);
read_02X(str + strlen("mac: XX-"), &cfg->mac[1]);
read_02X(str + strlen("mac: XX-XX-"), &cfg->mac[2]);
read_02X(str + strlen("mac: XX-XX-XX-"), &cfg->mac[3]);
read_02X(str + strlen("mac: XX-XX-XX-XX-"), &cfg->mac[4]);
read_02X(str + strlen("mac: XX-XX-XX-XX-XX-"), &cfg->mac[5]);
} else if(!strncmp(str, "ip: ", 4)) {
read_ip(str + strlen("ip: "), cfg->ip);
} else if(!strncmp(str, "mask: ", 6)) {
read_ip(str + strlen("mask: "), cfg->mask);
} else if(!strncmp(str, "gateway: ", 9)) {
read_ip(str + strlen("gateway: "), cfg->gateway);
} else if(!strncmp(str, "dns: ", 5)) {
read_ip(str + strlen("dns: "), cfg->dns);
} else if(!strncmp(str, "ping_ip: ", 9)) {
read_ip(str + strlen("ping_ip: "), cfg->ping_ip);
}
}
furi_string_free(fstring);
return true;
}
void ethernet_save_process_write(const EthernetSaveConfig* config) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, APP_DATA_PATH("config.txt"), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
FURI_LOG_E(TAG, "Failed to open file");
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return;
}
if(!storage_write_config(file, config)) {
FURI_LOG_E(TAG, "Failed to write cpnfig to file");
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return;
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
void ethernet_save_process_read(EthernetSaveConfig* config) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, APP_DATA_PATH("config.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Failed to open file or file not exists");
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return;
}
if(!storage_read_config(file, config)) {
FURI_LOG_E(TAG, "Failed to read config from file");
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return;
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
EthernetSaveConfig* ehternet_save_process_malloc() {
EthernetSaveConfig* config = malloc(sizeof(EthernetSaveConfig));
set_default_config(config);
ethernet_save_process_read(config);
Storage* storage = furi_record_open(RECORD_STORAGE);
FURI_LOG_E(TAG, "ehternet_save_process_malloc");
File* file = storage_file_alloc(storage);
if(!storage_file_open(file, APP_DATA_PATH("log.txt"), FSAM_WRITE, FSOM_OPEN_APPEND)) {
FURI_LOG_E(TAG, "Failed to open file or file not exists");
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return NULL;
}
config->log_file = file;
return config;
}
void ehternet_save_process_print(EthernetSaveConfig* config, const char* str) {
furi_assert(config);
DateTime datetime = {0};
furi_hal_rtc_get_datetime(&datetime);
storage_printf(
config->log_file,
"%4d.%02d.%02d-%02d:%2d:%02d || %s",
datetime.year,
datetime.month,
datetime.day,
datetime.hour,
datetime.minute,
datetime.second,
str);
}
void ehternet_save_process_free(EthernetSaveConfig* config) {
FURI_LOG_E(TAG, "ehternet_save_process_free");
ethernet_save_process_write(config);
storage_file_close(config->log_file);
storage_file_free(config->log_file);
furi_record_close(RECORD_STORAGE);
free(config);
}