Skip to content

WiFiUDP not reading last byte #145

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

Closed
henrikkunzelmann opened this issue Jan 23, 2017 · 3 comments
Closed

WiFiUDP not reading last byte #145

henrikkunzelmann opened this issue Jan 23, 2017 · 3 comments

Comments

@henrikkunzelmann
Copy link

henrikkunzelmann commented Jan 23, 2017

The WiFiUDP implementation is not reading the last byte in WiFiUDP::read().

Example (code is working fine on ESP8266 and ESP32 with one byte extra padding when sending packets to it):

int size = udp->parsePacket();
udp->read(buffer, size);

Size is for example 17 but read() reads only 16 bytes. The bug is in the cbuf class: cbuf::write() copies 16 bytes because cbuf::room() returns only 16 bytes (and not the correct length of 17 bytes).

The bug in the cbuf class should be fixed, but also a new method should be added to WiFiUDP:

int WiFiUDP::parsePacket(char* buffer, size_t size) (or some other signature).

parsePacket copies the packet content in the user provided buffer and returning the size of the packet. With this approach there is no need to create an internal cbuf instance and saving some memory overall.

@me-no-dev
Copy link
Member

I agree that cbuf might need to be looked at, though that same cbuf is running on esp8266 also and is used by many libs, so it's strange to me to not be working.
int size = udp->parsePacket(); is used by many(all) libraries that use UDP and that is the original signature from Arduino. I'm not sure that adding another method will do any good.

@henrikkunzelmann
Copy link
Author

I've patched the "bug" in cbuf::room() by using:

size_t cbuf::room() const
{
    if(_end >= _begin) {
        return _size - (_end - _begin);
    }
    return _begin - _end - 1;
}

room() will then return the correct free size (e.g. 17 bytes), but then available() returns 0, because _begin is the same value as _end (wrap_if_bufend).

Proposed fix for this problem is to actually patch WiFiUDP::parsePacket():

rx_buffer = new cbuf(len);

should be

rx_buffer = new cbuf(len + 1);

The usage of cbuf should be checked (is cbuf actually working correctly and only used "wrong" in WiFiUDP?).

I agree that cbuf might need to be looked at, though that same cbuf is running on esp8266 also and is used by many libs, so it's strange to me to not be working.

For me it's actually strange that the one-off bug was not catched by anyone else here.

int size = udp->parsePacket(); is used by many(all) libraries that use UDP and that is the original signature from Arduino. I'm not sure that adding another method will do any good.

I can kinda agreee that overloading the method maybe confusing to other users (e.g. using parsePacket(buf, sizeof(buf)) and then trying to use WiFiUDP::read() or so). I will provide a pull request with my approach.

@me-no-dev
Copy link
Member

done :)

blue-2357 pushed a commit to blue-2357/arduino-esp32 that referenced this issue Jul 17, 2024
brentru pushed a commit to adafruit/arduino-esp32 that referenced this issue Oct 22, 2024
…rary

throw warnings if action_install.sh is used and repo does not have 'arduino-library' tag in its topic
darkxst pushed a commit to darkxst/arduino-esp32 that referenced this issue Dec 5, 2024
* Update CMakeLists.txt

Removes RainMaker and replaces "all" to get it working with WSL Ubuntu

* Tool fix

Moving SR files copy from tools/copy-libs.sh to /build.sh :: WSL fix
Commenting out all cloned components that are now part of the Managed Components.

* Update idf_component.yml

Lib builder Managed Components for the top level (not in Arduino as IDF Component level). Includes only Tensor Flow and Deep Learning.
All other managed component will be added to ESP32-Arduino repository.

* Adds Camera Component

* DL component only for S3

* get Camera component from GH

* Camera component for any Version + Public

* Test for SR Models building in CI

* add SR component to the CI

* fix idf_component.yml format

* Reverting not necessary change about ESP-SR component
dash0820 added a commit to dash0820/arduino-esp32-stripped that referenced this issue Mar 10, 2025
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

No branches or pull requests

2 participants