1
0
forked from tanchou/Verilog

Enhance ESP32 command processing: add validation, error handling, and new command types for device management

This commit is contained in:
Gamenight77
2025-04-23 15:52:36 +02:00
parent 2b7582808e
commit 0eeecbca2e
2 changed files with 141 additions and 10 deletions

View File

@@ -83,6 +83,80 @@ void onClientDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
digitalWrite(2, LOW);
}
#define BUFFER_SIZE 64
uint8_t rxBuffer[BUFFER_SIZE];
uint8_t rxIndex = 0;
bool inFrame = false;
void processCommand(uint8_t* data, int length) {
// Vérifie la validité de la trame
if (length < 4 || data[0] != 0x02 || data[length - 2] != 0x1B || data[length - 1] != 0x03) {
byte packet[] = {0x02, 0x00, 0x03, 0x1B, 0x03}; // Erreur : Trame invalide
Serial.write(packet, sizeof(packet));
return;
}
uint8_t type = data[1];
switch (type) {
case 0x01: { // Wi-Fi State
bool wifiUp = WiFi.status() == WL_CONNECTED;
byte packet[] = {0x02, 0x01, wifiUp ? 0x01 : 0x00, 0x1B, 0x03};
Serial.write(packet, sizeof(packet));
break;
}
case 0x03: { // Request Connected Devices
wifi_sta_list_t staList;
tcpip_adapter_sta_list_t adapterList;
if (esp_wifi_ap_get_sta_list(&staList) == ESP_OK &&
tcpip_adapter_get_sta_list(&staList, &adapterList) == ESP_OK) {
for (int i = 0; i < adapterList.num; i++) {
byte packet[12] = {0x02, 0x02, 0x01}; // 0x01 = connect
memcpy(packet + 3, adapterList.sta[i].mac, 6);
packet[9] = 0x1B;
packet[10] = 0x03;
Serial.write(packet, 11);
}
} else {
byte packet[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // Erreur : args
Serial.write(packet, sizeof(packet));
}
break;
}
case 0x04: { // Send Message
if (length < 12) {
byte packet[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // args error
Serial.write(packet, sizeof(packet));
break;
}
uint8_t* mac = &data[2];
uint8_t msgLen = data[8];
if (length != 9 + msgLen + 2) {
byte packet[] = {0x02, 0x00, 0x04, 0x1B, 0x03}; // too long trame
Serial.write(packet, sizeof(packet));
break;
}
// Ici tu pourrais ajouter une logique pour router le message au bon appareil (plus tard)
// ACK possible :
byte ack[] = {0x02, 0x04, 0x01, 0x1B, 0x03}; // ACK
Serial.write(ack, sizeof(ack));
break;
}
default: {
byte packet[] = {0x02, 0x00, 0x01, 0x1B, 0x03}; // Erreur : commande inconnue
Serial.write(packet, sizeof(packet));
}
}
}
void setup() {
Serial.begin(115200);
if (!WiFi.softAP(ssid, password)) {
@@ -95,7 +169,6 @@ void setup() {
delay(1000); // Donne un peu de temps pour démarrer le WiFi
server.on("/", handleRoot);
server.begin();
@@ -108,4 +181,44 @@ void setup() {
void loop() {
server.handleClient();
bool escaping = false;
while (Serial.available()) {
uint8_t b = Serial.read();
if (!inFrame) {
if (b == 0x02) {
inFrame = true;
rxIndex = 0;
rxBuffer[rxIndex++] = b;
}
continue;
}
if (escaping) {
if (rxIndex < BUFFER_SIZE) {
rxBuffer[rxIndex++] = b;
}
escaping = false;
continue;
}
if (b == 0x1B) {
escaping = true;
} else if (b == 0x03) {
rxBuffer[rxIndex++] = b;
processCommand(rxBuffer, rxIndex);
inFrame = false;
rxIndex = 0;
} else {
if (rxIndex < BUFFER_SIZE) {
rxBuffer[rxIndex++] = b;
} else {
inFrame = false;
rxIndex = 0;
}
}
}
}