79 lines
1.5 KiB
C++
79 lines
1.5 KiB
C++
#include <Arduino.h>
|
|
#include <SPIFFS.h>
|
|
|
|
#include "Arena.h"
|
|
#include "Dictionary.h"
|
|
#include "WiFiMgr.h"
|
|
#include "homenet.h"
|
|
|
|
|
|
constexpr size_t PHONEBOOK_SIZE = 512;
|
|
constexpr char pbFilePath[] = "/pb.dat";
|
|
uint8_t phonebookBuffer[PHONEBOOK_SIZE];
|
|
Arena arena;
|
|
Dictionary phonebook(arena);
|
|
|
|
|
|
static bool
|
|
setupPhonebook()
|
|
{
|
|
File pbFile = SPIFFS.open(pbFilePath, FILE_READ);
|
|
size_t fileSize = pbFile.size();
|
|
bool ok = false;
|
|
|
|
Serial.print("DAT FILE ");
|
|
Serial.print(fileSize);
|
|
Serial.println("B");
|
|
if (fileSize == 0) {
|
|
Serial.println("INIT PHONEBOOK");
|
|
phonebook.Set(HOME_SSID, HOME_SSIDLEN, HOME_WPA, HOME_WPALEN);
|
|
if (WriteArena(arena, pbFilePath) == 0) {
|
|
ok = true;
|
|
}
|
|
|
|
pbFile.close();
|
|
return ok;
|
|
}
|
|
fileSize = fileSize > PHONEBOOK_SIZE ? PHONEBOOK_SIZE : fileSize;
|
|
|
|
Serial.print("LOAD PHONEBOOK ");
|
|
if (fileSize != pbFile.read(phonebookBuffer, fileSize)) {
|
|
Serial.println("FAILED");
|
|
pbFile.close();
|
|
return false;
|
|
}
|
|
|
|
Serial.println("OK");
|
|
pbFile.close();
|
|
phonebook.DumpKVPairs();
|
|
return true;
|
|
}
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
while (!Serial) ;
|
|
|
|
Serial.println("MODEM BOOT");
|
|
InitializeArena(arena);
|
|
NewStaticArena(arena, phonebookBuffer, PHONEBOOK_SIZE);
|
|
|
|
if (!SPIFFS.begin(true) && !SPIFFS.begin(false)) {
|
|
Serial.println("SPIFFS BEGIN FAIL");
|
|
while (true) ;
|
|
}
|
|
setupPhonebook();
|
|
|
|
while (!Autoconnect(phonebook)) {
|
|
Serial.println("STANDBY");
|
|
delay(1000);
|
|
}
|
|
|
|
Serial.println("MODEM READY");
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
}
|
|
|