103 lines
2.1 KiB
C++
103 lines
2.1 KiB
C++
#include <Arduino.h>
|
|
#include <SPIFFS.h>
|
|
|
|
#include "Arena.h"
|
|
#include "Commander.h"
|
|
#include "Config.h"
|
|
#include "Dictionary.h"
|
|
#include "Display.h"
|
|
#include "Mode.h"
|
|
#include "WiFiMgr.h"
|
|
#include "WiFiMeter.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;
|
|
|
|
Display::Print("DAT FILE ");
|
|
Display::Print(String(fileSize).c_str());
|
|
Display::Println("B");
|
|
if (fileSize == 0) {
|
|
Display::Println("INIT PHONEBOOK");
|
|
phonebook.Set(HOME_SSID, HOME_SSIDLEN, HOME_WPA, HOME_WPALEN);
|
|
if (WriteArena(arena, pbFilePath) == 0) {
|
|
ok = true;
|
|
}
|
|
|
|
pbFile.close();
|
|
Config::SetLastSSID(HOME_SSID);
|
|
|
|
return ok;
|
|
}
|
|
fileSize = fileSize > PHONEBOOK_SIZE ? PHONEBOOK_SIZE : fileSize;
|
|
|
|
Display::Print("LOAD PHONEBOOK ");
|
|
if (fileSize != pbFile.read(phonebookBuffer, fileSize)) {
|
|
Display::Println("FAILED");
|
|
pbFile.close();
|
|
return false;
|
|
}
|
|
|
|
Display::Println("OK");
|
|
pbFile.close();
|
|
phonebook.DumpKVPairs();
|
|
return true;
|
|
}
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Display::Init();
|
|
|
|
Display::Println("MODEM BOOT");
|
|
Display::Clear();
|
|
Display::Println("MODEM BOOT");
|
|
InitializeArena(arena);
|
|
NewStaticArena(arena, phonebookBuffer, PHONEBOOK_SIZE);
|
|
Meter::Begin();
|
|
Meter::Enable();
|
|
|
|
Display::Println("SPIFFS INIT");
|
|
if (!SPIFFS.begin(true) && !SPIFFS.begin(false)) {
|
|
Display::Println("SPIFFS BEGIN FAIL");
|
|
while (true) ;
|
|
}
|
|
setupPhonebook();
|
|
|
|
if (Config::StartTryConnecting()) {
|
|
Display::Println("AUTO ATC");
|
|
if (!TryConnect(phonebook)) {
|
|
Autoconnect(phonebook);
|
|
}
|
|
}
|
|
|
|
Display::Println("MODEM READY");
|
|
CommandMode();
|
|
|
|
Serial.println((uintptr_t)currentMode, HEX);
|
|
Serial.println((uintptr_t)(currentMode->Process), HEX);
|
|
Serial.println((uintptr_t)(currentMode->Update), HEX);
|
|
}
|
|
|
|
|
|
void loop() {
|
|
if (millis() % 1000 == 0) Meter::Update();
|
|
if (currentMode->Update()) {
|
|
currentMode->Process();
|
|
}
|
|
delay(1);
|
|
}
|
|
|