检测rt_sensor_device下的ops和ops中的函数指针,防止因未定义ops中的函数而造成的问题

This commit is contained in:
xfwangqiang 2021-03-16 01:15:58 +08:00 committed by wangqiang
parent b12615f344
commit 613f89a372
1 changed files with 59 additions and 31 deletions

View File

@ -147,7 +147,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
{
if (sensor->ops->control != RT_NULL)
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* If polling mode is supported, configure it to polling mode */
sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
@ -156,7 +156,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
}
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
{
if (sensor->ops->control != RT_NULL)
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* If interrupt mode is supported, configure it to interrupt mode */
sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT);
@ -167,7 +167,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
}
else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
{
if (sensor->ops->control != RT_NULL)
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* If fifo mode is supported, configure it to fifo mode */
sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO);
@ -183,10 +183,17 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
}
/* Configure power mode to normal mode */
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
{
sensor->config.power = RT_SENSOR_POWER_NORMAL;
}
}
else
{
sensor->config.power = RT_SENSOR_POWER_NORMAL;
}
__exit:
if (sensor->module)
@ -211,10 +218,17 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
}
/* Configure power mode to power down mode */
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
{
sensor->config.power = RT_SENSOR_POWER_DOWN;
}
}
else
{
sensor->config.power = RT_SENSOR_POWER_DOWN;
}
if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf != RT_NULL)
{
@ -279,7 +293,7 @@ static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_siz
sensor->data_len = 0;
result = len;
}
else
else if ((sensor->ops != RT_NULL) && (sensor->ops->fetch_data != RT_NULL))
{
/* If the buffer is empty read the data */
result = sensor->ops->fetch_data(sensor, buf, len);
@ -308,9 +322,12 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
{
case RT_SENSOR_CTRL_GET_ID:
if (args)
{
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args);
}
}
break;
case RT_SENSOR_CTRL_GET_INFO:
if (args)
@ -319,7 +336,8 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
}
break;
case RT_SENSOR_CTRL_SET_RANGE:
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* Configuration measurement range */
result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_RANGE, args);
if (result == RT_EOK)
@ -327,9 +345,11 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
sensor->config.range = (rt_int32_t)args;
LOG_D("set range %d", sensor->config.range);
}
}
break;
case RT_SENSOR_CTRL_SET_ODR:
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* Configuration data output rate */
result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_ODR, args);
if (result == RT_EOK)
@ -337,9 +357,11 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
LOG_D("set odr %d", sensor->config.odr);
}
}
break;
case RT_SENSOR_CTRL_SET_POWER:
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* Configuration sensor power mode */
result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, args);
if (result == RT_EOK)
@ -347,19 +369,25 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
sensor->config.power = (rt_uint32_t)args & 0xFF;
LOG_D("set power mode code:", sensor->config.power);
}
}
break;
case RT_SENSOR_CTRL_SELF_TEST:
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* Device self-test */
result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
}
break;
default:
if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
{
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
{
/* Custom commands */
result = sensor->ops->control(sensor, cmd, args);
}
}
else
{
result = -RT_ERROR;