11String replaceChars(
const char *str) {
18 case '<': result +=
"\xC2\xAB";
break;
19 case '>': result +=
"\xC2\xBB";
break;
20 case ':': result +=
"\xC3\xB7";
break;
21 case '"': result +=
"\xC2\xAA";
break;
22 case '/': result +=
"\xE2\x88\x9A";
break;
23 case '\\': result +=
"\xC3\xAC";
break;
24 case '|': result +=
"\xE2\x94\x82";
break;
25 case '?': result +=
"\xC2\xBF";
break;
26 case '*': result +=
"\xC2\xBA";
break;
27 default: result += *str;
break;
34bool initializeSDCard(
int csPin, Elog *logger, SemaphoreHandle_t *mutex, TickType_t delay) {
35 if (!SD.begin(csPin)) {
36 logger->log(ERROR,
"[BT SD] Couldn't initialize SD card for persistence.");
40 if (!xSemaphoreTake(*mutex, delay)) {
41 logger->log(ERROR,
"[BT SD] Couldn't take mutex for initialization.");
48 index = String(FILE_PATH).indexOf(
"/", index+1);
50 String subpath = String(FILE_PATH).substring(0, index);
51 if (!SD.exists(subpath) and !SD.mkdir(subpath)) {
52 logger->log(ERROR,
"[BT SD] Couldn't create folder (%s).", subpath.c_str());
53 xSemaphoreGive(*mutex);
60 if (!SD.exists(FILE_PATH) and !SD.mkdir(FILE_PATH)) {
61 logger->log(ERROR,
"[BT SD] Couldn't create folder (%s).", String(FILE_PATH).c_str());
62 xSemaphoreGive(*mutex);
65 xSemaphoreGive(*mutex);
69void restoreBTs(std::vector<BrokerTopic> *topicsVector, Elog *logger, SemaphoreHandle_t *mutex,
71 if (!xSemaphoreTake(*mutex, delay)) {
72 logger->log(ERROR,
"[BT SD] Couldn't take mutex for BTs restoration.");
76 File dir = SD.open(FILE_PATH);
79 logger->log(ERROR,
"[BT SD] Couldn't open main directory %s.", FILE_PATH);
81 while (File file = dir.openNextFile()) {
82 String filename = file.name();
83 if (!file.isDirectory() && filename.endsWith(FILE_FORMAT)) {
88 DeserializationError error = deserializeJson(doc, file);
90 logger->log(ERROR,
"[BT SD] Error parsing JSON file %s.", file.name());
95 JsonArray subscribersArray = doc[
"subscribers"];
97 if (subscribersArray.size() == 0) {
98 logger->log(WARNING,
"[BT SD] No subscribers found in topic '%s' of the SD card, skipped.", topic);
104 for (
const auto &mac : subscribersArray) {
105 std::array<uint8_t, 6> macArray;
106 sscanf(mac,
"%02X:%02X:%02X:%02X:%02X:%02X\n",
107 &macArray[0], &macArray[1], &macArray[2], &macArray[3], &macArray[4], &macArray[5]);
109 newTopic.subscribe(macArray);
111 String filename = file.name();
112 filename.replace(FILE_FORMAT,
"");
113 newTopic.setFilename(filename.c_str());
114 topicsVector->push_back(newTopic);
115 logger->log(INFO,
"[BT SD] Created topic '%s' found on SD card.", topic);
121 xSemaphoreGive(*mutex);
124void writeBTToFile(
BrokerTopic* brokerTopic, Elog* logger, SemaphoreHandle_t *mutex, TickType_t delay) {
126 doc[
"topic"] = brokerTopic->getTopic();
127 JsonArray subscribersArray = doc[
"subscribers"].to<JsonArray>();
128 for (
const auto& mac : brokerTopic->getSubscribers()) {
130 snprintf(macChar,
sizeof(macChar),
"%02X:%02X:%02X:%02X:%02X:%02X\n",
131 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
132 subscribersArray.add(macChar);
135 String fullFilepath = (String(FILE_PATH)+
"/"+brokerTopic->getFilename()+String(FILE_FORMAT));
137 if (!xSemaphoreTake(*mutex, delay)) {
138 logger->log(ERROR,
"[BT SD] Couldn't take mutex for BT file write.");
142 File file = SD.open(fullFilepath, FILE_WRITE);
144 logger->log(ERROR,
"[BT SD] Couldn't open file for writing (%s).", fullFilepath.c_str());
146 serializeJsonPretty(doc, file);
147 logger->log(DEBUG,
"[BT SD] Wrote topic '%s' to file '%s' successfully.", brokerTopic->getTopic(),
148 (brokerTopic->getFilename()+String(FILE_FORMAT)));
151 xSemaphoreGive(*mutex);
154void deleteBTFile(
const char* filename, Elog* logger, SemaphoreHandle_t *mutex, TickType_t delay) {
155 if (!xSemaphoreTake(*mutex, delay)) {
156 logger->log(ERROR,
"[BT SD] Couldn't take mutex for BT file deletion.");
160 String fullFilepath = (String(FILE_PATH)+
"/"+filename+String(FILE_FORMAT));
161 File file = SD.open(fullFilepath, FILE_WRITE);
163 logger->log(ERROR,
"[BT SD] Couldn't open file for deletion (%s).", fullFilepath.c_str());
165 if (!SD.remove(file.path())) {
166 logger->log(ERROR,
"[BT SD] Couldn't delete file (%s).", fullFilepath.c_str());
168 logger->log(DEBUG,
"[BT SD] Deleted file %s successfully.", file.name());
172 xSemaphoreGive(*mutex);