in common/sensor/sensor.c [119:198]
uint8_t get_sensor_reading(uint8_t sensor_num, int *reading, uint8_t read_mode)
{
if (SensorNum_SDR_map[sensor_num] == 0xFF) { // look for sensor in SDR table
return SENSOR_NOT_FOUND;
}
if (!access_check(sensor_num)) { // sensor not accessable
return SENSOR_NOT_ACCESSIBLE;
}
sensor_cfg *cfg = &sensor_config[SensorNum_SensorCfg_map[sensor_num]];
if (read_mode == get_from_sensor) {
if (cfg->pre_sensor_read_hook) {
if (cfg->pre_sensor_read_hook(sensor_num, cfg->pre_sensor_read_args) ==
false) {
printf("sensor %d pre sensor read failed!\n", sensor_num);
return SENSOR_PRE_READ_ERROR;
}
}
int status = SENSOR_READ_API_UNREGISTER;
if (cfg->read)
status = cfg->read(sensor_num, reading);
if (status == SENSOR_READ_SUCCESS || status == SENSOR_READ_ACUR_SUCCESS) {
cfg->retry = 0;
if (!access_check(
sensor_num)) { // double check access to avoid not accessible read at same moment status change
return SENSOR_NOT_ACCESSIBLE;
}
if (cfg->post_sensor_read_hook) {
if (cfg->post_sensor_read_hook(sensor_num,
cfg->post_sensor_read_args,
reading) == false) {
printf("sensor %d post sensor read failed!\n", sensor_num);
cfg->cache_status = SENSOR_POST_READ_ERROR;
return SENSOR_POST_READ_ERROR;
}
}
memcpy(&cfg->cache, reading, sizeof(*reading));
status = SENSOR_READ_4BYTE_ACUR_SUCCESS;
cfg->cache_status = status;
return cfg->cache_status;
} else {
/* common retry */
if (cfg->retry >= SENSOR_READ_RETRY_MAX)
cfg->cache_status = status;
else
cfg->retry++;
return cfg->cache_status;
}
} else if (read_mode == get_from_cache) {
if (cfg->cache_status == SENSOR_READ_SUCCESS ||
cfg->cache_status == SENSOR_READ_ACUR_SUCCESS ||
cfg->cache_status == SENSOR_READ_4BYTE_ACUR_SUCCESS) {
if (cfg->cache_status == SENSOR_READ_4BYTE_ACUR_SUCCESS)
memcpy(reading, &cfg->cache, sizeof(cfg->cache));
else
*reading = cfg->cache;
if (!access_check(
sensor_num)) { // double check access to avoid not accessible read at same moment status change
return SENSOR_NOT_ACCESSIBLE;
}
return cfg->cache_status;
} else if (cfg->cache_status == SENSOR_INIT_STATUS) {
cfg->cache = sensor_fail;
return cfg->cache_status;
} else {
cfg->cache = sensor_fail;
printf("sensor[%x] cache read fail, status %x\n", sensor_num,
cfg->cache_status);
return cfg->cache_status;
}
}
return SENSOR_UNSPECIFIED_ERROR; // should not reach here
}