mirror of
https://github.com/apache/nuttx.git
synced 2026-05-21 04:52:02 +08:00
drivers/sensors/dhtxx: Fix read return values to be POSIX compliant
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>
This commit is contained in:
@@ -434,7 +434,7 @@ static int dhtxx_open(FAR struct file *filep)
|
||||
static ssize_t dhtxx_read(FAR struct file *filep, FAR char *buffer,
|
||||
size_t buflen)
|
||||
{
|
||||
int ret = OK;
|
||||
int ret;
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct dhtxx_dev_s *priv = inode->i_private;
|
||||
FAR struct dhtxx_sensor_data_s *data =
|
||||
@@ -443,13 +443,13 @@ static ssize_t dhtxx_read(FAR struct file *filep, FAR char *buffer,
|
||||
if (!buffer)
|
||||
{
|
||||
snerr("ERROR: Buffer is null.\n");
|
||||
return -1;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (buflen < sizeof(FAR struct dhtxx_sensor_data_s))
|
||||
{
|
||||
snerr("ERROR: Not enough memory to read data sample.\n");
|
||||
return -ENOSYS;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
memset(priv->raw_data, 0u, sizeof(priv->raw_data));
|
||||
@@ -465,32 +465,33 @@ static ssize_t dhtxx_read(FAR struct file *filep, FAR char *buffer,
|
||||
if (dht_prepare_reading(priv) != 0)
|
||||
{
|
||||
data->status = DHTXX_TIMEOUT;
|
||||
ret = -1;
|
||||
ret = -ETIMEDOUT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (dht_read_raw_data(priv) != 0)
|
||||
{
|
||||
data->status = DHTXX_TIMEOUT;
|
||||
ret = -1;
|
||||
ret = -ETIMEDOUT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!dht_verify_checksum(priv))
|
||||
{
|
||||
data->status = DHTXX_CHECKSUM_ERROR;
|
||||
ret = -1;
|
||||
ret = -EIO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (dht_parse_data(priv, data) != 0)
|
||||
{
|
||||
data->status = DHTXX_READ_ERROR;
|
||||
ret = -1;
|
||||
ret = -EIO;
|
||||
}
|
||||
else
|
||||
{
|
||||
data->status = DHTXX_SUCCESS;
|
||||
ret = sizeof(struct dhtxx_sensor_data_s);
|
||||
}
|
||||
|
||||
out:
|
||||
|
||||
Reference in New Issue
Block a user