Skip to content

Bring more compatibility with native sockets #218

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Neradoc
Copy link
Contributor

@Neradoc Neradoc commented Feb 26, 2025

This PR adds methods to socketpool and socket to make it more compatible with the native socketpool and sockets. This was specifically made to make it work with adafruit_httpserver, which it does, once the httpserver library is changed to not import ssl unconditionally.

A breaking change was made to the socket's settimeout(), by copying the core API, which also matches the python API: None blocks, 0 doesn't. Previously this library did the opposite. I don't know what code out there is impacted by this. Maybe this should be investigated a bit before validating the PR.

https://docs.python.org/3/library/socket.html#socket.socket.settimeout
https://docs.circuitpython.org/en/latest/shared-bindings/socketpool/index.html#socketpool.Socket.settimeout

Added missing methods from Socket:

  • bind just saves the address/port parameter
  • listen uses the ninafw start_server()
  • accept gets a client socket from the server (if any)
  • setblocking is a shortcut for settimeout() with the appropriate values (True -> None, False -> 0)
  • setsockopt currently does nothing

Changes to Socketpool/Socket and ESP_SPIcontrol:

  • re-add the socknum argument to the Socket() constructor. It's not in the python socket API, but we need to be able to wrap a Socket object around a socket number returned by ninafw when getting a client connection to the server.
  • ESP_SPIcontrol.socket_write returns the number of bytes written, so that:
  • Socket.send now returns the number of bytes written (as does sendto) to match the python socket() API.
  • SOL_SOCKET and SO_REUSEADDR have been added to SocketPool.

The PR was tested on a Matrix Portal M4 with a modified version of adafruit_httpserver.

socket_write() and send() return the number of bytes sent
add some of the constants used by httpserver (others missing)
dummy entries for setsockopt(), listen() and setblocking()
implement bind() by using ninafw start_server()
implement accept() by getting a clien socket from that server

add back socknum parameter to allow wrapping a Socket around a ninafw socket number, which is required to be able to receive a connection from the server
re-implement timeout similar to Circuitpython (None blocks, 0 doesn't)
NOTE: this is a breaking change for any code that uses it

bind() saves the bound address and port
listen() uses the saved address and port or uses defaults
some fixes for pylint/black
@Neradoc
Copy link
Contributor Author

Neradoc commented Feb 26, 2025

Here is an example on Matrix Portal M4 that doesn't need external files

# SPDX-FileCopyrightText: Copyright 2025 Neradoc, https://neradoc.me
# SPDX-License-Identifier: MIT
import asyncio
import board
import displayio
import gc
import keypad
import time
import os
import vectorio
from adafruit_httpserver import Server, Response, MIMETypes

PORT = 80

############################################################################
# Matrix
############################################################################

from adafruit_matrixportal.matrix import Matrix

matrix = Matrix(rotation=0)
display = matrix.display
splash = displayio.Group()
display.root_group = splash

color_palette = displayio.Palette(8)
color_palette[0] = 0
color_palette[1] = 0xFFFFFF
color_palette[2] = 0xE40303
color_palette[3] = 0xFF8C00
color_palette[4] = 0xFFED00
color_palette[5] = 0x008026
color_palette[6] = 0x004CFF
color_palette[7] = 0x732982

rectangle = vectorio.Rectangle(
    pixel_shader=color_palette, color_index=1,
    x=display.width//2 - 4, y=display.height//2 - 4,
    width=8, height=8, 
)
splash.append(rectangle)

############################################################################
# modes
############################################################################

class Box:
    def __init__(self, value):
        self.value = value

current_color = Box(1)

def set_color():
    current_color.value = current_color.value % len(color_palette)
    rectangle.color_index = current_color.value

############################################################################
# wifi
############################################################################
gc.collect()

import busio
from digitalio import *
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socketpool as socketpool

def setup_wifi():
    esp32_cs = DigitalInOut(board.ESP_CS)
    esp32_ready = DigitalInOut(board.ESP_BUSY)
    esp32_reset = DigitalInOut(board.ESP_RESET)

    spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
    esp = adafruit_esp32spi.ESP_SPIcontrol(
        spi, esp32_cs, esp32_ready, esp32_reset
    )

    esp.connect_AP(
        os.getenv("WIFI_SSID"),
        os.getenv("WIFI_PASSWORD"),
    )
    esp.set_hostname("microphone")

    return esp

esp = setup_wifi()
server = Server(socketpool.SocketPool(esp))
IP_ADDRESS = "%d.%d.%d.%d" % tuple(esp.ip_address)

############################################################################
# server routes and app logic
############################################################################
gc.collect()

index_page_html = """<body style="background:black; color:white;">"""
for index, color in enumerate(color_palette):
    index_page_html += f"""<p><a style="text-decoration:none; color:white;" href="/color/{index}">Color {index} <span style="border:1px solid grey; background:#{color:06X}"> &nbsp;&nbsp;&nbsp; </span></a></p>"""
index_page_html += """</body>"""

@server.route("/color/get")
def status_get(request):
    # respond with status
    color = color_palette[current_color.value]
    return Response(request, f"#{color:06X}")

@server.route("/color/<index>")
def status_set(request, index):
    try:
        status = int(index)
        current_color.value = status
        set_color()
    except ValueError:
        return Response(request, "Bad Color")
    gc.collect()
    return Response(request, index_page_html, content_type="text/html")

@server.route("/")
def index(request):
    return Response(request, index_page_html, content_type="text/html")

############################################################################
# start and loop
############################################################################

async def main():
    print(f"Started server on http://{IP_ADDRESS}:{PORT}/")
    server.start(host=str(IP_ADDRESS), port=PORT)
    errors_remaining = 5
    while True:
        try:
            server.poll()
            gc.collect()
        except (TimeoutError, ConnectionError) as e:
            # reload if multiple timeouts
            if isinstance(e, TimeoutError):
                errors_remaining -= 1
                if errors_remaining == 0:
                    import supervisor
                    supervisor.reload()
            time.sleep(1)
        await asyncio.sleep(0.01)

asyncio.run(main())

@justmobilize
Copy link
Collaborator

We should also see if this fixes/works with: adafruit/Adafruit_CircuitPython_WSGI#21

@justmobilize
Copy link
Collaborator

I'll add comments tomorrow, I had gone down a different path for passing the socket number to the socket, to keep things matching across the board

@justmobilize
Copy link
Collaborator

We should also potentially wait until the final firmware comes out, since it may have benefits...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants