-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESP32S2: Implement TCP Server bindings #3854
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Sorry I left these pending. Thanks for the ping!
@krittick want to test this PR? |
Sure, would love to. Will try to get to it later today. |
Does the CI failure have to do with my code?
My changes don't affect anything related to this, as far as I can tell? |
I don't think so. Dan and Jeff would have a better idea of the CI state atm. |
@tannewt looks like CI passed this time, should ready for re-review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! Thank you! I'm holding the approval so this is merged in for 6.2 rather than 6.1. @dhalbert can merge post-6.1
Sounds good. I have the TLS server separation PR finished and ready to go as soon as this gets merged, so we could potentially get both of them into the same release. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving and merging as requested.
Hey @hierophect, Here is the code import struct
import time
import rtc
import socketpool
import wifi
TZ_OFFSET = 3600
NTP_TO_UNIX_EPOCH = 2_208_988_800 # 1970-01-01 00:00:00
BILLION = 1_000_000_000
wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"])
pool = socketpool.SocketPool(wifi.radio)
packet = bytearray(48)
packet[0] = 0b00100011
for i in range(1, len(packet)):
packet[i] = 0
with pool.socket(pool.AF_INET, pool.SOCK_DGRAM) as sock:
sock.sendto(packet, ("pool.ntp.org", 123))
sock.recvfrom_into(packet)
destination = time.monotonic_ns()
seconds = struct.unpack_from("!I", packet, offset=len(packet) - 8)[0]
monotonic_start = seconds - NTP_TO_UNIX_EPOCH - (destination // BILLION)
rtc.RTC().datetime = time.localtime(time.monotonic_ns() // BILLION + monotonic_start + TZ_OFFSET) The call Cheers :) |
Hi @netroy, an issue with this PR was that when it expanded the timeout system to support non-blocking calls, it inadvertently set the default timeout of a socket to 0 instead of the Cpython default of None (ie infinite timeout). #4049 will change back to None, but until then you can use settimeout(None) to extend the timeout yourself. I think that should resolve your problem, but feel free to follow up with an issue if it doesn't. |
@hierophect Maybe we should fix this outside of #4049 so we can get it in faster. |
Wait I probably read that wrong, you mean change the default timeout in a new PR so we can take more time on #4049? |
This PR adds a few new features to support running a TCP server on the ESP32-S2:
bind
,listen
andaccept
, which allow connecting to remote sockets and sending/receiving data from them.SOCK_STREAM
automatically use MbedTLS instead of the LWIP socket API, and makes that decision on the fly based on what functions are used first.The differentiation between sockets that use TLS and those that don't is still a bit hacky. Right now, it's determined by what functions the socket calls first - if you use
connect
after initialization, it'll become a TLS socket and will be ineligible for any other uses. Otherwise (ie, callingbind
orsendto
first) it becomes a regular socket. I'd like to eventually improve this to allowconnect
without TLS, possibly by putting the TLS init in the SSL wrapper step. We could also consider moving all TLS functionality to a separate module (possibly a duplicate Socket based API) instead of having both kinds of operation under the hood here.Tested on a Saola 1 Wrover. Test programs below:
Resolves #3724
Resolves #3836