jklincn opened a new pull request, #18164:
URL: https://github.com/apache/nuttx/pull/18164
## 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`:
```c
#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
* [x] This PR introduces only one functional change.
* [x] I have updated all required description fields above.
* [x] My PR adheres to Contributing
[Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md) and
[Documentation](https://nuttx.apache.org/docs/latest/contributing/index.html)
(git commit title and message, coding standard, etc).
* [ ] My PR is still work in progress (not ready for review).
* [x] My PR is ready for review and can be safely merged into a codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]