Skip to content

Commit

Permalink
protect unconnected USB-CDC from being used
Browse files Browse the repository at this point in the history
found a few more places where Serial was used without first checking if its connected.

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
  • Loading branch information
softhack007 committed May 29, 2024
1 parent 6a93f46 commit 15199dc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tools/ESP32-Chip_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ void show_psram_info_part2(void)

void showRealSpeed() {
//Serial.begin(115200);
if (!Serial) return; // Avoid writing to unconnected USB-CDC

Serial.flush();
Serial.println(F("\n"));
for(int aa=0; aa<65; aa++) Serial.print("="); Serial.println();
Expand Down
2 changes: 2 additions & 0 deletions wled00/wled_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void updateBaudRate(uint32_t 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<used; i++) {
Expand All @@ -55,6 +56,7 @@ void sendJSON(){
// RGB LED data returned as bytes in TPM2 format. Faster, and slightly less easy to use on the other end.
void sendBytes(){
if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut) {
if (!Serial) return; // WLEDMM avoid writing to unconnected USB-CDC
Serial.write(0xC9); Serial.write(0xDA);
uint16_t used = strip.getLengthTotal();
uint16_t len = used*3;
Expand Down

0 comments on commit 15199dc

Please sign in to comment.