Skip to content
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

Add recv_into method #151

Merged
merged 2 commits into from
Jan 14, 2022
Merged

Add recv_into method #151

merged 2 commits into from
Jan 14, 2022

Conversation

tekktrik
Copy link
Member

@tekktrik tekktrik commented Jan 13, 2022

Addresses Issue #55 by adding the recv_into() method. Tested with Adafruit CircuitPython 7.1.0-beta.0 on 2021-11-12; Adafruit Feather M4 Express with samd51j19 with AirLift FeatherWing, with a modified version of the esp32spi_tcp_client.py example!

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe for CPython compatibility, recv_into would return the number of bytes rather than the supplied buffer itself:
https://docs.python.org/3/library/socket.html#socket.socket.recv_into

Tested on:
Adafruit CircuitPython 7.1.0 on 2021-12-28; Adafruit PyPortal with samd51j20
with this code:

import time
from secrets import secrets  # pylint: disable=no-name-in-module
import board
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket


TIMEOUT = 5
# edit host and port to match server
HOST = "wifitest.adafruit.com"
PORT = 80
MAXBUF = 512
INTERVAL = 5

# PyPortal or similar; edit pins as needed
spi = board.SPI()
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# connect to wifi AP
print("Connecting to wifi...")
esp.connect(secrets)

# create the socket
socket.set_interface(esp)
socketaddr = socket.getaddrinfo(HOST, PORT)[0][4]
s = socket.socket()
s.settimeout(TIMEOUT)

buf = bytearray(MAXBUF)
while True:
    print("Connecting to socket...")
    # s.connect((HOST, PORT))
    s.connect(socketaddr)

    s.send(b"GET /testwifi/index.html HTTP/1.0\r\n\r\n")
    print("Sent")

    # size = s.recv_into(buf)
    ret_buf = s.recv_into(buf)
    # print('Received', buf[:size])
    print('Received:\n', buf[:len(buf)].decode(), "\n?=\n", ret_buf[:len(ret_buf)].decode())

    s.close()

    time.sleep(INTERVAL)

@tekktrik
Copy link
Member Author

Thanks @anecdata, good catch! Changed the return to the number of bytes read!

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-tested, size and buf are as expected. LGTM!

Since it's a new feature, there's little risk, but maybe one of the library devs want to look at it.

@tannewt tannewt merged commit 13102f3 into adafruit:main Jan 14, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 19, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_DRV2605 to 1.2.1 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_DRV2605#30 from tekktrik/doc/fix-code-block

Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 3.6.0 from 3.5.14:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#150 from tekktrik/feature/configurable-eol
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#151 from tekktrik/feature/add-recvinto
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#152 from tekktrik/hotfix/fix-docs
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#149 from tekktrik/doc/add-missing-to-api

Updating https://github.com/adafruit/Adafruit_CircuitPython_FRAM to 1.3.13 from 1.3.12:
  > Merge pull request adafruit/Adafruit_CircuitPython_FRAM#33 from tekktrik/doc/update-documentation

Updating https://github.com/adafruit/Adafruit_CircuitPython_HT16K33 to 4.2.0 from 4.1.9:
  > Merge pull request adafruit/Adafruit_CircuitPython_HT16K33#88 from jposada202020/including_animation_class

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 6.2.0 from 6.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#120 from FoamyGuy/force_content_type

Updating https://github.com/adafruit/Adafruit_CircuitPython_CursorControl to 2.5.2 from 2.5.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_CursorControl#32 from FoamyGuy/alt_click

Updating https://github.com/adafruit/Adafruit_CircuitPython_FunHouse to 2.1.10 from 2.1.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_FunHouse#33 from makermelissa/main
  > Merge pull request adafruit/Adafruit_CircuitPython_FunHouse#31 from makermelissa/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 5.2.0 from 5.1.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#97 from goliothlabs/feat/add-binary-support

Updating https://github.com/adafruit/Adafruit_CircuitPython_PIOASM to 0.6.0 from 0.5.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_PIOASM#29 from jepler/program-object
@tekktrik tekktrik deleted the feature/add-recvinto branch January 25, 2022 16:40
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.

3 participants