@@ -163,8 +163,32 @@ void handleNotifications()
163163 if (!isSupp && notifierUdp.remoteIP () == Network.localIP ()) return ; // don't process broadcasts we send ourselves
164164
165165 uint8_t udpIn[packetSize +1 ];
166- if (isSupp) notifier2Udp.read (udpIn, packetSize);
167- else notifierUdp.read (udpIn, packetSize);
166+ uint16_t len;
167+ if (isSupp) len = notifier2Udp.read (udpIn, packetSize);
168+ else len = notifierUdp.read (udpIn, packetSize);
169+
170+ // WLED nodes info notifications
171+ if (isSupp && udpIn[0 ] == 255 && udpIn[1 ] == 1 && len >= 40 ) {
172+ // IPAddress remoteIP = notifier2Udp.remoteIP();
173+
174+ uint8_t unit = udpIn[39 ];
175+ Nodes[unit].age = 0 ; // Create a new element when not present
176+ NodesMap::iterator it = Nodes.find (unit);
177+
178+ if (it != Nodes.end ()) {
179+ for (byte x = 0 ; x < 4 ; x++) {
180+ it->second .ip [x] = udpIn[x + 2 ];
181+ }
182+ it->second .age = 0 ; // reset 'age counter'
183+ char tmpNodeName[33 ] = { 0 };
184+ memcpy (&tmpNodeName[0 ], reinterpret_cast <byte *>(&udpIn[6 ]), 32 );
185+ tmpNodeName[32 ] = 0 ;
186+ it->second .nodeName = tmpNodeName;
187+ it->second .nodeName .trim ();
188+ it->second .nodeType = udpIn[38 ];
189+ }
190+ return ;
191+ }
168192
169193 // wled notifier, ignore if realtime packets active
170194 if (udpIn[0 ] == 0 && !realtimeMode && receiveNotifications)
@@ -349,3 +373,99 @@ void setRealtimePixel(uint16_t i, byte r, byte g, byte b, byte w)
349373 }
350374 }
351375}
376+
377+ /* ********************************************************************************************\
378+ Refresh aging for remote units, drop if too old...
379+ \*********************************************************************************************/
380+ void refreshNodeList ()
381+ {
382+ for (NodesMap::iterator it = Nodes.begin (); it != Nodes.end ();) {
383+ bool mustRemove = true ;
384+
385+ if (it->second .ip [0 ] != 0 ) {
386+ if (it->second .age < 10 ) {
387+ it->second .age ++;
388+ mustRemove = false ;
389+ ++it;
390+ }
391+ }
392+
393+ if (mustRemove) {
394+ it = Nodes.erase (it);
395+ }
396+ }
397+ }
398+
399+ /* ********************************************************************************************\
400+ Broadcast system info to other nodes. (to update node lists)
401+ \*********************************************************************************************/
402+ void sendSysInfoUDP (uint8_t repeats)
403+ {
404+ if (!udpConnected || !repeats) {
405+ return ;
406+ }
407+
408+ IPAddress ip = WiFi.localIP ();
409+
410+ // TODO: make a nice struct of it and clean up
411+ // 0: 1 byte 'binary token 255'
412+ // 1: 1 byte id '1'
413+ // 2: 4 byte ip
414+ // 6: 32 char name
415+ // 38: 1 byte node type id
416+ // 39: 1 byte node id
417+ // 40 bytes total
418+
419+ // send my info to the world...
420+ for (;repeats--;)
421+ {
422+ /*
423+ escapedMac // mac address
424+ */
425+
426+ uint8_t data[40 ] = {0 };
427+ data[0 ] = 255 ;
428+ data[1 ] = 1 ;
429+
430+ for (byte x = 0 ; x < 4 ; x++) {
431+ data[x + 2 ] = ip[x];
432+ }
433+ memcpy ((byte *)data + 6 , serverDescription, 32 );
434+ #ifdef ESP8266
435+ data[38 ] = NODE_TYPE_ID_ESP8266;
436+ #elif defined(ARDUINO_ARCH_ESP32)
437+ data[38 ] = NODE_TYPE_ID_ESP32;
438+ #else
439+ data[38 ] = NODE_TYPE_ID_UNDEFINED;
440+ #endif
441+ data[39 ] = ip[3 ]; // unit ID == last IP number
442+
443+ IPAddress broadcastIP (255 , 255 , 255 , 255 );
444+ notifier2Udp.beginPacket (broadcastIP, udpPort2);
445+ notifier2Udp.write (data, 40 );
446+ notifier2Udp.endPacket ();
447+
448+ if (repeats) delay (500 );
449+ }
450+
451+ Nodes[ip[3 ]].age = 0 ; // Create new node when not already present.
452+ // store my own info also in the list
453+ NodesMap::iterator it = Nodes.find (ip[3 ]);
454+
455+ if (it != Nodes.end ())
456+ {
457+ for (byte x = 0 ; x < 4 ; x++) {
458+ it->second .ip [x] = ip[x];
459+ }
460+ it->second .age = 0 ;
461+ it->second .nodeName = serverDescription;
462+ #ifdef ESP8266
463+ it->second .nodeType = NODE_TYPE_ID_ESP8266;
464+ #elif defined(ARDUINO_ARCH_ESP32)
465+ it->second .nodeType = NODE_TYPE_ID_ESP32;
466+ #else
467+ it->second .nodeType = NODE_TYPE_ID_UNDEFINED;
468+ #endif
469+ it->second .unit = ip[3 ];
470+ }
471+ }
0 commit comments