UDP callbacks now working

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
dbalnaves
Posts: 7
Joined: Thu Jan 05, 2017 12:53 pm

UDP callbacks now working

Post by dbalnaves » Fri Mar 03, 2017 11:53 pm

Hey Everyone,

I apologize in advance for any inaccurate use of terminology, failing to observe standards or just general ignorance. Please correct me as any guidance is really appreciated as it helps me learn.

So, thanks to a topic (http://forum.micropython.org/viewtopic.php?f=16&t=2854) some time ago where Roberthh sent me down the path of using TCP callbacks on esp8266 I was able to understand the code and found that support for UDP callbacks was fairly trivial.

Code: Select all

$ git diff extmod/modlwip.c
diff --git a/extmod/modlwip.c b/extmod/modlwip.c
index 62699bd..b73d248 100644
--- a/extmod/modlwip.c
+++ b/extmod/modlwip.c
@@ -281,6 +281,7 @@ STATIC void _lwip_udp_incoming(void *arg, struct udp_pcb *upcb, struct pbuf *p,
         socket->incoming.pbuf = p;
         socket->peer_port = (mp_uint_t)port;
         memcpy(&socket->peer, addr, sizeof(socket->peer));
+       exec_user_callback(socket);
     }
 }
I get the impression that the use of setsockopt 20 is really just there to make webrepl work and is far from the decided approach. Does anybody know what the lasting approach will be going forward?

The point of this post was that I wanted to contribute this back to the community. UDP callbacks are so wonderful that everyone should have them; I wanted UDP callbacks to decrease latency for NTP (with milli support), but I imagine this would be attractive to people looking to implement mDNS. I'm happy to post some code that demonstrates this as working, but what should I do in order to contribute this back to the community?
Last edited by dbalnaves on Sat Mar 04, 2017 4:36 am, edited 1 time in total.

User avatar
dbalnaves
Posts: 7
Joined: Thu Jan 05, 2017 12:53 pm

Re: UDP callbacks now working

Post by dbalnaves » Sat Mar 04, 2017 12:45 am

I did say I had some code to test this, this NTP code doesn't support subsecond but it demonstrates the callbacks:

Code: Select all

import machine
try:
    import usocket as socket
except:
    import socket
try:
    import ustruct as struct
except:
    import struct
try:
    import utime as time
except:
    import time

# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
query_packet = bytearray(48)
response_packet = bytes(48)
dst_addr = socket.getaddrinfo('192.168.1.1', 123)[0][-1]
src_addr = socket.getaddrinfo('0.0.0.0', 123)[0][-1]

def ntp_cb(socket):
    msg = socket.recv(48)
    val = struct.unpack("!I", msg[40:44])[0]
    tm = time.localtime(val - NTP_DELTA)
    tupple = tm[0:3] + (0,) + tm[3:6] + (0,)
    machine.RTC().datetime(tupple)
    print(time.localtime())


print("running main")
while not webrepl.network.WLAN().isconnected():
    time.sleep(1)
print("Wifi connected: ",webrepl.network.WLAN().ifconfig())
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, 20, ntp_cb)
s.bind(src_addr)
while True:
    res = s.sendto(NTP_QUERY, dst_addr)
    time.sleep(1)
Which should produce output like this:

Code: Select all

running main

connected with Utopia, channel 1
dhcp client start...
ip:192.168.1.17,mask:255.255.255.0,gw:192.168.1.1
Wifi connected:  ('192.168.1.17', '255.255.255.0', '192.168.1.1', '192.168.1.1')
(2017, 3, 4, 0, 43, 43, 5, 63)
(2017, 3, 4, 0, 43, 44, 5, 63)
(2017, 3, 4, 0, 43, 45, 5, 63)
(2017, 3, 4, 0, 43, 46, 5, 63)
(2017, 3, 4, 0, 43, 47, 5, 63)
(2017, 3, 4, 0, 43, 48, 5, 63)
(2017, 3, 4, 0, 43, 49, 5, 63)
(2017, 3, 4, 0, 43, 50, 5, 63)

larsks
Posts: 22
Joined: Mon Feb 13, 2017 5:27 pm

Re: UDP callbacks now working

Post by larsks » Wed Nov 08, 2017 12:39 pm

This looks like a really useful feature. It doesn't look like there was ever a reply here in the forum. Did you end up contributing this to the project?

Post Reply