diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 512e55cc8..8f2bc4c93 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,6 +1,7 @@ /* 6.5.0.16 20190611 * Refactored TLS based on BearSSL, warning breaking change for fingerprints validation (see doc) * Add checkbox to GUI password field enabling visibility during password entry only (#5934) + * Add using heap when more than 199 IRSend values need to be send. May need increase of define MQTT_MAX_PACKET_SIZE too (#5950) * * 6.5.0.15 20190606 * Change pubsubclient MQTT_KEEPALIVE from 10 to 30 seconds in preparation of AWS IoT support diff --git a/sonoff/xdrv_05_irremote.ino b/sonoff/xdrv_05_irremote.ino index 9bd0694e3..79bd67ecb 100644 --- a/sonoff/xdrv_05_irremote.ino +++ b/sonoff/xdrv_05_irremote.ino @@ -639,12 +639,32 @@ bool IrSendCommand(void) } else { // At least two raw data values // IRsend 0,896,876,900,888,894,876,1790,874,872,1810,1736,948,872,880,872,936,872,1792,900,888,1734 count++; - uint16_t raw_array[count]; // It's safe to use stack for up to 240 packets (limited by mqtt_data length) - for (uint16_t i = 0; i < count; i++) { - raw_array[i] = strtol(strtok_r(nullptr, ", ", &p), nullptr, 0); // Allow decimal (20496) and hexadecimal (0x5010) input + if (count < 200) { + uint16_t raw_array[count]; // It's safe to use stack for up to 200 packets (limited by mqtt_data length) + for (uint16_t i = 0; i < count; i++) { + raw_array[i] = strtol(strtok_r(nullptr, ", ", &p), nullptr, 0); // Allow decimal (20496) and hexadecimal (0x5010) input + } + +// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DBG: stack count %d"), count); + + irsend_active = true; + irsend->sendRaw(raw_array, count, freq); + } else { + uint16_t *raw_array = reinterpret_cast(malloc(count * sizeof(uint16_t))); + if (raw_array == nullptr) { + error = IE_INVALID_RAWDATA; + } else { + for (uint16_t i = 0; i < count; i++) { + raw_array[i] = strtol(strtok_r(nullptr, ", ", &p), nullptr, 0); // Allow decimal (20496) and hexadecimal (0x5010) input + } + +// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DBG: heap count %d"), count); + + irsend_active = true; + irsend->sendRaw(raw_array, count, freq); + free(raw_array); + } } - irsend_active = true; - irsend->sendRaw(raw_array, count, freq); } } }