Skip to content

Conversation

@jklincn
Copy link
Contributor

@jklincn jklincn commented Jan 26, 2026

Summary

This PR fixes the read method in the dhtxx character driver to comply with standard POSIX behavior.

  • Why: The driver currently returns 0 on success, which does not conform to the standard read interface (which should return the number of bytes read). Additionally, it uses non-standard or incorrect error codes (e.g., returning -ENOSYS for invalid arguments or generic -1).
  • What:
    1. Updated dhtxx_read to return the actual number of bytes read (sizeof(struct dhtxx_sensor_data_s)) on success.
    2. Replaced generic -1 and incorrect -ENOSYS with standard errno:
      • -EINVAL for invalid buffer arguments.
      • -ETIMEDOUT for sensor timeouts.
      • -EIO for checksum verification or parsing failures.

Impact

  • Is new feature added? NO
  • Impact on user? YES. Applications checking the return value of read() will now receive the correct byte count instead of 0.
  • Impact on build? NO
  • Impact on hardware? NO
  • Impact on documentation? NO
  • Impact on security? NO
  • Impact on compatibility? YES. Improves POSIX compatibility and standard driver behavior.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Linux (Ubuntu), x86_64, arm-none-eabi-gcc
  • Target(s): STM32F103ZET6 (Custom Board), DHT11 Sensor

Testing logs before change:

NuttShell (NSH) NuttX-12.12.0
nsh> dht11
Reading DHT11...
Read failed. ret=0
nsh> 

Testing logs after change:

NuttShell (NSH) NuttX-12.12.0
nsh> dht11
Reading DHT11...
Humidity:    21.0 %
Temperature: 24.0 C
nsh> 

Test Source Code dht11:

#include <fcntl.h>
#include <nuttx/config.h>
#include <nuttx/sensors/dhtxx.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  int fd;
  struct dhtxx_sensor_data_s data;
  ssize_t ret;

  fd = open("/dev/dht0", O_RDONLY);
  if (fd < 0) {
    printf("Failed to open /dev/dht0\n");
    return -1;
  }

  printf("Reading DHT11...\n");

  // Read data from DHT11 sensor
  ret = read(fd, &data, sizeof(data));

  if (ret != sizeof(data)) {
    printf("Read failed. ret=%zd\n", ret);
  } else {
    if (data.status == DHTXX_SUCCESS) {
      printf("Humidity:    %.1f %%\n", data.hum);
      printf("Temperature: %.1f C\n", data.temp);
    } else {
      printf("Sensor Error Status: %d\n", data.status);
    }
  }

  close(fd);
  return 0;
}

PR verification Self-Check

  • This PR introduces only one functional change.
  • I have updated all required description fields above.
  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).
  • My PR is still work in progress (not ready for review).
  • My PR is ready for review and can be safely merged into a codebase.

The dhtxx driver previously returned 0 on success, which violates standard
character device behavior where the number of bytes read should be returned.
It also used non-standard error codes like -1 or -ENOSYS for operational
errors.

This commit fixes the following behavior to comply with POSIX standards:
* Return sizeof(struct dhtxx_sensor_data_s) on success instead of 0.
* Return -EINVAL instead of -ENOSYS/-1 for invalid buffer arguments.
* Return -ETIMEDOUT instead of -1 for sensor timeouts.
* Return -EIO instead of -1 for checksum or parsing errors.

Signed-off-by: jklincn <jklincn@foxmail.com>
@github-actions github-actions bot added Area: Sensors Sensors issues Size: S The size of the change in this PR is small labels Jan 26, 2026
@xiaoxiang781216 xiaoxiang781216 merged commit 6538477 into apache:master Jan 26, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Sensors Sensors issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants