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;
}
}
}
}

View File

@@ -8,21 +8,39 @@ Ce protocole permet la communication entre le FPGA et l'ESP32 via UART, principa
Chaque trame est encadrée par des caractères spéciaux :
- **ESCAPE** (Before special char) : `0x1B`
- **STX** (Start of Text) : `0x02`
- **ETX** (End of Text) : `0x03`
**Format de trame :**
**Format de trame :**
```
0x02 OP_CODE VALUE_1 VALUE_2 ... 0x1B 0x03
```
---
## Détail des commandes
| OP_CODE | Nom | Description | Format |
|---------|---------------------------|--------------------------------------------------------------|------------------------------------------------------------------------|
| 0x01 | Wi-Fi State | Indique l'état du Wi-Fi (0 = down, 1 = up) | `0x02 0x01 [0x00/0x01] 0x03` |
| 0x02 | Connection Update | Notification de connexion ou déconnexion dun appareil | `0x02 0x02 [0x00/0x01] [MAC_ADDR (6 bytes)] 0x03` |
| 0x03 | Request Connected Devices | Demande la liste des appareils connectés | `0x02 0x03 0x03` |
| 0x04 | Send Message | Envoie un message à un appareil connecté via son MAC | `0x02 0x04 [MAC_ADDR (6 bytes)] [LEN (1 byte)] [MESSAGE] 0x03` |
|---------|---------------------------|---------------------------------------------------------|-------------------------------------------------------------------------------|
| 0x00 | Error | Indique une erreur | `0x02 0x00 [ERROR_CODE] 0x1B 0x03` |
| 0x01 | Wi-Fi State | Indique l'état du Wi-Fi (0 = down, 1 = up) | `0x02 0x01 [0x00/0x01] 0x1B 0x03` |
| 0x02 | Connection Update | Notification de connexion ou déconnexion dun appareil | `0x02 0x02 [0x00/0x01] [MAC_ADDR (6 bytes)] 0x1B 0x03` |
| 0x03 | Request Connected Devices | Demande la liste des appareils connectés | `0x02 0x03 0x1B 0x03` |
| 0x04 | Send Connected Devices | Envoie la liste des appareil connecter | `0x02 0x04 [LEN (1 byte)] [MAC_LIST (n bytes)[MAC_ADDR (6 bytes)]] 0x1B 0x03` |
| 0x05 | Send Message | Envoie un message à un appareil connecté via son MAC | `0x02 0x05 [MAC_ADDR (6 bytes)] [LEN (1 byte)] [MESSAGE] 0x1B 0x03` |
---
## Détail des code erreur
| ERROR_CODE | Description |
|---------------|---------------------------------------------------------------|
| 0x00 | |
| 0x01 | Unknow command |
| 0x02 | Args error |
| 0x03 | Invalid Trame |
| 0x04 | Too long trame |
---