-
Notifications
You must be signed in to change notification settings - Fork 1
/
Preferences.cpp
93 lines (81 loc) · 2.16 KB
/
Preferences.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
/**
* @brief IoT Train preferences on onboard flash memory for Seeed XIAO nRF52840 BLE (Sense)
* @file Preferences.cpp
* @author TANAHASHI, Jiro (aka jtFuruhata) <[email protected]>
* @version 0.0.1
* @date 2023-03-21
*
* @copyright Copyright (c) 2023 jtLab, Hokkaido Information University,
* Release under the MIT License.
* See LICENSE.
*/
#include "Preferences.h"
String readFile(const char *filename){
String result = "";
Serial.println(filename);
FILE *file = fopen(filename, "r");
if (file) {
char buf[33];
int len = fread(buf, 1, sizeof(buf), file);
fclose(file);
if (len > 0) {
buf[len] = '\0';
result = String(buf);
} else {
remove(fileMaBeee);
}
}
return result;
}
int writeFile(const char *filename, String data) {
int result = -1;
FILE *file = fopen(filename, "w");
if (file) {
char buf[33];
strcpy(buf, data.c_str());
result = fwrite(buf, 1, sizeof(buf), file);
fclose(file);
}
return result;
}
bool hasPairedMaBeee() {
return pairedMaBeeeName.length() > 0;
}
bool isPairedMaBeee(String name) {
return pairedMaBeeeName.equals(name);
}
String getPairedMaBeeeName(){
return pairedMaBeeeName;
}
bool setPairedMaBeeeName(String name){
bool result = false;
if (name.startsWith("MaBeee") && name.length() == 12) {
pairedMaBeeeName = name;
writeFile(fileMaBeee, pairedMaBeeeName);
result = true;
}
return result;
}
bool hasAllowedHost() {
return allowedHostAddress.length() > 0;
}
bool isAllowedHost(String address) {
return !hasAllowedHost() || allowedHostAddress.equals(address);
}
String getAllowedHostAddress(){
return allowedHostAddress;
}
bool setAllowedHostAddress(String address){
bool result = false;
if (address.length() == 17) {
allowedHostAddress = address;
if (writeFile(fileHost, address)) {
result = true;
}
}
return result;
}
void readSettings() {
setPairedMaBeeeName(readFile(fileMaBeee));
setAllowedHostAddress(readFile(fileHost));
}