#include "wled.h" #ifdef ARDUINO_ARCH_ESP32 #include "esp_ota_ops.h" #endif /* * Adalight and TPM2 handler */ #define SERIAL_MAXTIME_MILLIS 100 // to avoid blocking other activities, do not spend more than 100ms with continuous reading // at 115200 baud, 100ms is enough to send/receive 1280 chars enum class AdaState { Header_A, Header_d, Header_a, Header_CountHi, Header_CountLo, Header_CountCheck, Data_Red, Data_Green, Data_Blue, TPM2_Header_Type, TPM2_Header_CountHi, TPM2_Header_CountLo, }; uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100) bool continuousSendLED = false; uint32_t lastUpdate = 0; void updateBaudRate(uint32_t rate){ uint16_t rate100 = rate/100; if (rate100 == currentBaud || rate100 < 96) return; currentBaud = rate100; if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut){ if (Serial) { Serial.print(F("Baud is now ")); Serial.println(rate); } } if (Serial) Serial.flush(); Serial.begin(rate); } // RGB LED data return as JSON array. Slow, but easy to use on the other end. void sendJSON(){ if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut) { if (!Serial) return; // WLEDMM avoid writing to unconnected USB-CDC uint16_t used = strip.getLengthTotal(); Serial.write('['); for (uint16_t i=0; i RGB map Serial.write(qadd8(W(c), G(c))); //G Serial.write(qadd8(W(c), B(c))); //B } Serial.write(0x36); Serial.write('\n'); } } bool canUseSerial(void) { // WLEDMM returns true if Serial can be used for debug output (i.e. not configured for other purpose) #if ARDUINO_USB_CDC_ON_BOOT && (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32P4)) && !defined(WLED_DEBUG_HOST) // on S3/C3/C6/P4, USB CDC blocks if disconnected! so check if Serial is active before printing to it. if (!Serial) return false; #endif if (pinManager.isPinAllocated(hardwareTX) && (pinManager.getPinOwner(hardwareTX) != PinOwner::DebugOut)) return false; // TX allocated to LEDs or other functions if ((realtimeMode == REALTIME_MODE_GENERIC) || (realtimeMode == REALTIME_MODE_ADALIGHT) || (realtimeMode == REALTIME_MODE_TPM2NET)) return false; // Serial in use for adaLight or other serial communication //if ((improvActive == 1) || (improvActive == 2)) return false; // don't interfere when IMPROV communication is ongoing if (improvActive > 0) return false; // don't interfere when IMPROV communication is ongoing if (continuousSendLED == true) return false; // Continuous Serial Streaming return true; } // WLEDMM end #if defined(WLED_ENABLE_INFINITY_CONTROLLER) && defined(WLED_INFINITY_MASTER) namespace { String rfpJsonValue(const String& json, const char* key) { StaticJsonDocument<384> command; DeserializationError error = deserializeJson(command, json); if (error) return String(); return command[key] | ""; } uint32_t rfpJsonU32(const String& json, const char* key) { StaticJsonDocument<384> command; DeserializationError error = deserializeJson(command, json); if (error) return 0; return command[key] | 0; } uint32_t rfpParseChunkLength(const String& line, String& encoded) { if (!line.startsWith("RFPCHUNK1 ")) return 0; const int separator = line.indexOf(' ', 10); if (separator < 0) return 0; const int32_t length = line.substring(10, separator).toInt(); encoded = line.substring(separator + 1); return length > 0 ? static_cast(length) : 0; } void rfpSerialPrintError(const __FlashStringHelper* message) { Serial.print(F("RFPERR1 {\"error\":\"")); Serial.print(message); Serial.println(F("\"}")); } void rfpSerialPrintErrorDetail(const __FlashStringHelper* message, uint32_t a, uint32_t b) { Serial.print(F("RFPERR1 {\"error\":\"")); Serial.print(message); Serial.print(F("\",\"a\":")); Serial.print(a); Serial.print(F(",\"b\":")); Serial.print(b); Serial.println(F("}")); } int8_t rfpBase64Value(char c) { if (c >= 'A' && c <= 'Z') return c - 'A'; if (c >= 'a' && c <= 'z') return c - 'a' + 26; if (c >= '0' && c <= '9') return c - '0' + 52; if (c == '+') return 62; if (c == '/') return 63; return -1; } int rfpDecodeBase64Chunk(const String& encoded, uint8_t* output, size_t outputSize) { uint32_t accumulator = 0; uint8_t bits = 0; size_t written = 0; for (uint16_t i = 0; i < encoded.length(); i++) { const char c = encoded[i]; if (c == '=') break; const int8_t value = rfpBase64Value(c); if (value < 0) return -1; accumulator = (accumulator << 6) | static_cast(value); bits += 6; if (bits >= 8) { bits -= 8; if (written >= outputSize) return -1; output[written++] = static_cast((accumulator >> bits) & 0xFF); } } return static_cast(written); } bool rfpReadHttpBody(WiFiClient& client, String& body, uint32_t timeoutMs) { const uint32_t start = millis(); bool inBody = false; String headerTail; body.reserve(512); while ((millis() - start) < timeoutMs) { while (client.available()) { const char c = static_cast(client.read()); if (!inBody) { headerTail += c; if (headerTail.length() > 4) headerTail.remove(0, headerTail.length() - 4); if (headerTail == "\r\n\r\n") inBody = true; } else if (body.length() < 2048) { body += c; } } if (!client.connected() && !client.available()) return inBody; delay(1); } return inBody; } void rfpHandleInfoCommand(const String& json) { const String target = rfpJsonValue(json, "target"); if (target.length() == 0) { rfpSerialPrintError(F("missing target")); return; } WiFiClient client; if (!client.connect(target.c_str(), 80)) { rfpSerialPrintError(F("node connect failed")); return; } client.print(F("GET /json/info HTTP/1.1\r\nHost: ")); client.print(target); client.print(F("\r\nConnection: close\r\n\r\n")); String body; if (!rfpReadHttpBody(client, body, 5000) || body.length() == 0) { rfpSerialPrintError(F("node info failed")); client.stop(); return; } client.stop(); body.replace("\r", ""); body.replace("\n", ""); Serial.print(F("RFPINFO1 ")); Serial.println(body); } void rfpHandleOtaCommand(const String& json) { const String target = rfpJsonValue(json, "target"); const uint32_t firmwareSize = rfpJsonU32(json, "size"); const uint32_t ackBytes = rfpJsonU32(json, "ackBytes"); if (target.length() == 0 || firmwareSize == 0) { rfpSerialPrintError(F("missing target or size")); return; } constexpr char boundary[] = "----RFPInfinityOtaBoundary"; const String head = String("--") + boundary + "\r\n" "Content-Disposition: form-data; name=\"update\"; filename=\"firmware.bin\"\r\n" "Content-Type: application/octet-stream\r\n\r\n"; const String tail = String("\r\n--") + boundary + "\r\n" "Content-Disposition: form-data; name=\"skipValidation\"\r\n\r\n" "1\r\n" "--" + boundary + "--\r\n"; const uint32_t contentLength = head.length() + firmwareSize + tail.length(); WiFiClient client; if (!client.connect(target.c_str(), 80)) { rfpSerialPrintError(F("node connect failed")); return; } client.print(F("POST /update?skipValidation=1 HTTP/1.1\r\nHost: ")); client.print(target); client.print(F("\r\nConnection: close\r\nContent-Type: multipart/form-data; boundary=")); client.print(boundary); client.print(F("\r\nContent-Length: ")); client.print(contentLength); client.print(F("\r\n\r\n")); client.print(head); Serial.print(F("RFPREADY1 {\"target\":\"")); Serial.print(target); Serial.print(F("\",\"size\":")); Serial.print(firmwareSize); Serial.print(F(",\"ackBytes\":")); Serial.print(ackBytes); Serial.print(F(",\"proto\":4")); Serial.println(F("}")); Serial.flush(); Serial.setTimeout(20000); String dataStart = Serial.readStringUntil('\n'); dataStart.trim(); if (dataStart != "RFPDATA1") { client.stop(); rfpSerialPrintError(F("serial data start timeout")); return; } uint8_t buffer[768]; uint32_t remaining = firmwareSize; uint32_t nextAck = ackBytes; uint32_t lastDataMs = millis(); while (remaining > 0) { String chunkHeader = Serial.readStringUntil('\n'); chunkHeader.trim(); String encoded; const uint32_t chunkLength = rfpParseChunkLength(chunkHeader, encoded); if (chunkLength == 0 || chunkLength > remaining) { client.stop(); rfpSerialPrintError(F("serial chunk header invalid")); return; } if (chunkLength > sizeof(buffer) || encoded.length() == 0) { client.stop(); rfpSerialPrintError(F("serial chunk too large")); return; } const int decoded = rfpDecodeBase64Chunk(encoded, buffer, sizeof(buffer)); if (decoded != static_cast(chunkLength)) { client.stop(); rfpSerialPrintErrorDetail(F("serial chunk decode failed"), encoded.length(), decoded < 0 ? 0 : decoded); return; } lastDataMs = millis(); if (client.write(buffer, decoded) != static_cast(decoded)) { client.stop(); rfpSerialPrintError(F("node write failed")); return; } remaining -= decoded; yield(); const uint32_t received = firmwareSize - remaining; if (ackBytes > 0 && (received >= nextAck || remaining == 0)) { Serial.print(F("RFPACK1 {\"bytes\":")); Serial.print(received); Serial.println(F("}")); nextAck += ackBytes; } } client.print(tail); String body; rfpReadHttpBody(client, body, 30000); client.stop(); body.replace("\r", " "); body.replace("\n", " "); if (body.length() > 360) body = body.substring(0, 360); Serial.print(F("RFPDONE1 {\"target\":\"")); Serial.print(target); Serial.print(F("\",\"bytes\":")); Serial.print(firmwareSize); Serial.print(F(",\"response\":\"")); for (uint16_t i = 0; i < body.length(); i++) { const char c = body[i]; Serial.print(c == '"' || c == '\\' ? '_' : c); } Serial.println(F("\"}")); } bool rfpHandleSerialCommandLine() { Serial.setTimeout(2000); String line = Serial.readStringUntil('\n'); line.trim(); if (line.startsWith("RFPINFO1 ")) { rfpHandleInfoCommand(line.substring(9)); return true; } if (line.startsWith("RFPOTA1 ")) { rfpHandleOtaCommand(line.substring(8)); return true; } rfpSerialPrintError(F("unknown rfp command")); return true; } } // namespace #endif void handleSerial() { #if !ARDUINO_USB_CDC_ON_BOOT // some USB-CDC boards.json set RX and TX to the USB+ and USB- pins. These pins cannot be assigned to other purposes, so always availeable if (pinManager.isPinAllocated(hardwareRX)) return; #endif if (((pinManager.isPinAllocated(hardwareTX)) && (pinManager.getPinOwner(hardwareTX) != PinOwner::DebugOut))) return; // WLEDMM serial TX is necessary for adalight / TPM2 if (!Serial) return; // arduino docs: `if (Serial)` indicates whether or not the USB CDC serial connection is open. For all non-USB CDC ports, this will always return true #ifdef WLED_ENABLE_ADALIGHT static auto state = AdaState::Header_A; static uint16_t count = 0; static uint16_t pixel = 0; static byte check = 0x00; static byte red = 0x00; static byte green = 0x00; unsigned long startTime = millis(); while ((Serial.available() > 0) && (millis() - startTime < SERIAL_MAXTIME_MILLIS)) { yield(); byte next = Serial.peek(); switch (state) { case AdaState::Header_A: if (next == 'A') state = AdaState::Header_d; else if (next == 0xC9) { //TPM2 start byte state = AdaState::TPM2_Header_Type; } else if (next == 'I') { handleImprovPacket(); return; } else if (next == 'v') { Serial.print("WLED"); Serial.write(' '); Serial.println(VERSION); } else if (next == '^') { #ifdef ARDUINO_ARCH_ESP32 esp_err_t err; const esp_partition_t *boot_partition = esp_ota_get_boot_partition(); const esp_partition_t *running_partition = esp_ota_get_running_partition(); USER_PRINTF("Running on %s and we should have booted from %s. This %s\n",running_partition->label,boot_partition->label,(String(running_partition->label) == String(boot_partition->label))?"is what we expect.":"means OTA messed up!"); if (String(running_partition->label) == String(boot_partition->label)) { esp_partition_iterator_t new_boot_partition_iterator = NULL; if (boot_partition->subtype == ESP_PARTITION_SUBTYPE_APP_OTA_0) { new_boot_partition_iterator = esp_partition_find(ESP_PARTITION_TYPE_APP,ESP_PARTITION_SUBTYPE_APP_OTA_1,"app1"); } else { new_boot_partition_iterator = esp_partition_find(ESP_PARTITION_TYPE_APP,ESP_PARTITION_SUBTYPE_APP_OTA_0,"app0"); } const esp_partition_t* new_boot_partition = esp_partition_get(new_boot_partition_iterator); err = esp_ota_set_boot_partition(new_boot_partition); if (err == ESP_OK) { USER_PRINTF("Switching boot partitions from %s to %s in 3 seconds!\n",boot_partition->label,new_boot_partition->label); delay(3000); esp_restart(); } else { USER_PRINTF("Looks like the other app partition (%s) is invalid. Ignoring.\n",new_boot_partition->label); } } else { USER_PRINTF("Looks like the other partion is invalid as we exepected %s but we booted failsafe to %s. Ignoring boot change.\n",boot_partition->label,running_partition->label); } #else USER_PRINTLN("Boot partition switching is only available for ESP32 and newer boards."); #endif } else if (next == 'X') { forceReconnect = true; // WLEDMM - force reconnect via Serial } else if (next == 'R') { #if defined(WLED_ENABLE_INFINITY_CONTROLLER) && defined(WLED_INFINITY_MASTER) rfpHandleSerialCommandLine(); return; #endif } else if (next == 0xB0) {updateBaudRate( 115200); } else if (next == 0xB1) {updateBaudRate( 230400); } else if (next == 0xB2) {updateBaudRate( 460800); } else if (next == 0xB3) {updateBaudRate( 500000); } else if (next == 0xB4) {updateBaudRate( 576000); } else if (next == 0xB5) {updateBaudRate( 921600); } else if (next == 0xB6) {updateBaudRate(1000000); } else if (next == 0xB7) {updateBaudRate(1500000); } else if (next == 'l') {sendJSON(); // Send LED data as JSON Array } else if (next == 'L') {sendBytes(); // Send LED data as TPM2 Data Packet } else if (next == 'o') {continuousSendLED = false; // Disable Continuous Serial Streaming } else if (next == 'O') {continuousSendLED = true; // Enable Continuous Serial Streaming } else if (next == '{') { //JSON API bool verboseResponse = false; if (!requestJSONBufferLock(16)) { if (Serial) Serial.println(F("{\"error\":3}")); // ERR_NOBUF return; } Serial.setTimeout(100); DeserializationError error = deserializeJson(doc, Serial); if (error) { releaseJSONBufferLock(); return; } verboseResponse = deserializeState(doc.as()); //only send response if TX pin is unused for other purposes if (verboseResponse && (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut)) { doc.clear(); JsonObject state = doc.createNestedObject("state"); serializeState(state); JsonObject info = doc.createNestedObject("info"); serializeInfo(info); serializeJson(doc, Serial); Serial.println(); } releaseJSONBufferLock(); } break; case AdaState::Header_d: if (next == 'd') state = AdaState::Header_a; else state = AdaState::Header_A; break; case AdaState::Header_a: if (next == 'a') state = AdaState::Header_CountHi; else state = AdaState::Header_A; break; case AdaState::Header_CountHi: pixel = 0; count = next * 0x100; check = next; state = AdaState::Header_CountLo; break; case AdaState::Header_CountLo: count += next + 1; check = check ^ next ^ 0x55; state = AdaState::Header_CountCheck; break; case AdaState::Header_CountCheck: if (check == next) state = AdaState::Data_Red; else state = AdaState::Header_A; break; case AdaState::TPM2_Header_Type: state = AdaState::Header_A; //(unsupported) TPM2 command or invalid type if (next == 0xDA) state = AdaState::TPM2_Header_CountHi; //TPM2 data else if (next == 0xAA) Serial.write(0xAC); //TPM2 ping break; case AdaState::TPM2_Header_CountHi: pixel = 0; count = (next * 0x100) /3; state = AdaState::TPM2_Header_CountLo; break; case AdaState::TPM2_Header_CountLo: count += next /3; state = AdaState::Data_Red; break; case AdaState::Data_Red: red = next; state = AdaState::Data_Green; break; case AdaState::Data_Green: green = next; state = AdaState::Data_Blue; break; case AdaState::Data_Blue: byte blue = next; if (!realtimeOverride) setRealtimePixel(pixel++, red, green, blue, 0); if (--count > 0) state = AdaState::Data_Red; else { realtimeLock(realtimeTimeoutMs, REALTIME_MODE_ADALIGHT); if (!realtimeOverride) strip.show(); state = AdaState::Header_A; } break; } // All other received bytes will disable Continuous Serial Streaming if (continuousSendLED && next != 'O'){ continuousSendLED = false; } Serial.read(); //discard the byte } //#ifdef WLED_DEBUG if ((millis() - startTime) > SERIAL_MAXTIME_MILLIS) { USER_PRINTLN(F("handleSerial(): need a break after >100ms of activity.")); } //#endif #else #pragma message "Serial protocols (AdaLight, Serial JSON, Serial LED driver, improv) disabled" #endif // If Continuous Serial Streaming is enabled, send new LED data as bytes if (continuousSendLED && (lastUpdate != strip.getLastShow())){ sendBytes(); lastUpdate = strip.getLastShow(); } }