@@ -6,6 +6,7 @@ class UdpNameSync : public Usermod {
66
77 bool enabled = false ;
88 char segmentName[WLED_MAX_SEGNAME_LEN] = {0 };
9+ static constexpr uint8_t kPacketType = 200 ; // custom usermod packet type
910 static const char _name[];
1011 static const char _enabled[];
1112
@@ -21,17 +22,19 @@ class UdpNameSync : public Usermod {
2122 inline bool isEnabled () const { return enabled; }
2223
2324 void setup () override {
25+ enable (true );
2426 }
2527
2628 void loop () override {
29+ if (!enabled) return ;
2730 if (!WLED_CONNECTED) return ;
2831 if (!udpConnected) return ;
2932 Segment& mainseg = strip.getMainSegment ();
3033 if (!strlen (segmentName) && !mainseg.name ) return ; // name was never set, do nothing
3134
3235 IPAddress broadcastIp = uint32_t (Network.localIP ()) | ~uint32_t (Network.subnetMask());
3336 byte udpOut[WLED_MAX_SEGNAME_LEN + 2 ];
34- udpOut[0 ] = 200 ; // custom usermod packet type (avoid 0..5 used by core protocols)
37+ udpOut[0 ] = kPacketType ; // custom usermod packet type (avoid 0..5 used by core protocols)
3538 if (strlen (segmentName) && !mainseg.name ) { // name cleared
3639 notifierUdp.beginPacket (broadcastIp, udpPort);
3740 segmentName[0 ] = ' \0 ' ;
@@ -48,6 +51,7 @@ class UdpNameSync : public Usermod {
4851 notifierUdp.beginPacket (broadcastIp, udpPort);
4952 DEBUG_PRINT (F (" UdpNameSync: saving segment name " ));
5053 DEBUG_PRINTLN (mainseg.name );
54+ DEBUG_PRINTLN (curName);
5155 strlcpy (segmentName, mainseg.name , sizeof (segmentName));
5256 strlcpy ((char *)&udpOut[1 ], segmentName, sizeof (udpOut) - 1 ); // leave room for header byte
5357 notifierUdp.write (udpOut, 2 + strnlen ((char *)&udpOut[1 ], sizeof (udpOut) - 1 ));
@@ -58,14 +62,20 @@ class UdpNameSync : public Usermod {
5862
5963 bool onUdpPacket (uint8_t * payload, size_t len) override {
6064 DEBUG_PRINT (F (" UdpNameSync: Received packet" ));
65+ if (!enabled) return false ;
6166 if (receiveDirect) return false ;
62- if (payload[ 0 ] != 200 ) return false ;
63- // else
67+ if (len < 2 ) return false ; // need type + at least 1 byte for name (can be 0)
68+ if (payload[ 0 ] != kPacketType ) return false ;
6469 Segment& mainseg = strip.getMainSegment ();
65- mainseg.setName ((char *)&payload[1 ]);
70+ char tmp[WLED_MAX_SEGNAME_LEN] = {0 };
71+ size_t copyLen = len - 1 ;
72+ if (copyLen > sizeof (tmp) - 1 ) copyLen = sizeof (tmp) - 1 ;
73+ memcpy (tmp, &payload[1 ], copyLen);
74+ tmp[copyLen] = ' \0 ' ;
75+ mainseg.setName (tmp);
6676 DEBUG_PRINT (F (" UdpNameSync: set segment name" ));
6777 return true ;
68- }
78+ }
6979};
7080
7181
0 commit comments