Skip to content
Snippets Groups Projects
Verified Commit 32382264 authored by koalo's avatar koalo Committed by rahix
Browse files

feat(bhi160): Use centralized return

parent d76cc7ff
No related branches found
No related tags found
No related merge requests found
...@@ -123,38 +123,39 @@ int epic_bhi160_enable_sensor( ...@@ -123,38 +123,39 @@ int epic_bhi160_enable_sensor(
enum bhi160_sensor_type sensor_type, enum bhi160_sensor_type sensor_type,
struct bhi160_sensor_config *config struct bhi160_sensor_config *config
) { ) {
int result = 0;
bhy_virtual_sensor_t vs_id = bhi160_lookup_vs_id(sensor_type); bhy_virtual_sensor_t vs_id = bhi160_lookup_vs_id(sensor_type);
if (vs_id < 0) { if (vs_id < 0) {
return -ENODEV; return -ENODEV;
} }
int lockret = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)); result = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100));
if (lockret < 0) { if (result < 0) {
return lockret; return result;
}
if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) != pdTRUE) {
result = -EBUSY;
goto out_free_i2c;
} }
if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) == pdTRUE) {
struct stream_info *stream = &bhi160_streams[sensor_type]; struct stream_info *stream = &bhi160_streams[sensor_type];
stream->item_size = bhi160_lookup_data_size(sensor_type); stream->item_size = bhi160_lookup_data_size(sensor_type);
/* TODO: Sanity check length */ /* TODO: Sanity check length */
stream->queue = xQueueCreate( stream->queue =
config->sample_buffer_len, stream->item_size xQueueCreate(config->sample_buffer_len, stream->item_size);
);
if (stream->queue == NULL) { if (stream->queue == NULL) {
xSemaphoreGive(bhi160_mutex); result = -ENOMEM;
hwlock_release(HWLOCK_I2C); goto out_free_both;
return -ENOMEM;
} }
int streamret = result = stream_register(bhi160_lookup_sd(sensor_type), stream);
stream_register(bhi160_lookup_sd(sensor_type), stream); if (result < 0) {
if (streamret < 0) { goto out_free_both;
xSemaphoreGive(bhi160_mutex);
hwlock_release(HWLOCK_I2C);
return streamret;
} }
int bhyret = bhy_enable_virtual_sensor( result = bhy_enable_virtual_sensor(
vs_id, vs_id,
VS_WAKEUP, VS_WAKEUP,
config->sample_rate, config->sample_rate,
...@@ -163,65 +164,60 @@ int epic_bhi160_enable_sensor( ...@@ -163,65 +164,60 @@ int epic_bhi160_enable_sensor(
0, 0,
config->dynamic_range /* dynamic range is sensor dependent */ config->dynamic_range /* dynamic range is sensor dependent */
); );
if (bhyret != BHY_SUCCESS) { if (result != BHY_SUCCESS) {
xSemaphoreGive(bhi160_mutex); goto out_free_both;
hwlock_release(HWLOCK_I2C);
return bhyret;
} }
bhi160_sensor_active[sensor_type] = true; bhi160_sensor_active[sensor_type] = true;
result = bhi160_lookup_sd(sensor_type);
out_free_both:
xSemaphoreGive(bhi160_mutex); xSemaphoreGive(bhi160_mutex);
} else { out_free_i2c:
hwlock_release(HWLOCK_I2C); hwlock_release(HWLOCK_I2C);
return -EBUSY; return result;
}
hwlock_release(HWLOCK_I2C);
return bhi160_lookup_sd(sensor_type);
} }
int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type) int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type)
{ {
int result = 0;
bhy_virtual_sensor_t vs_id = bhi160_lookup_vs_id(sensor_type); bhy_virtual_sensor_t vs_id = bhi160_lookup_vs_id(sensor_type);
if (vs_id < 0) { if (vs_id < 0) {
return -ENODEV; return -ENODEV;
} }
int lockret = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)); result = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100));
if (lockret < 0) { if (result < 0) {
return lockret; return result;
}
if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) != pdTRUE) {
result = -EBUSY;
goto out_free_i2c;
} }
if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) == pdTRUE) {
struct stream_info *stream = &bhi160_streams[sensor_type]; struct stream_info *stream = &bhi160_streams[sensor_type];
int streamret = stream_deregister( result = stream_deregister(bhi160_lookup_sd(sensor_type), stream);
bhi160_lookup_sd(sensor_type), stream if (result < 0) {
); goto out_free_both;
if (streamret < 0) {
xSemaphoreGive(bhi160_mutex);
hwlock_release(HWLOCK_I2C);
return streamret;
} }
vQueueDelete(stream->queue); vQueueDelete(stream->queue);
stream->queue = NULL; stream->queue = NULL;
int bhyret = bhy_disable_virtual_sensor(vs_id, VS_WAKEUP); result = bhy_disable_virtual_sensor(vs_id, VS_WAKEUP);
if (bhyret < 0) { if (result < 0) {
xSemaphoreGive(bhi160_mutex); goto out_free_both;
hwlock_release(HWLOCK_I2C);
return bhyret;
} }
bhi160_sensor_active[sensor_type] = false; bhi160_sensor_active[sensor_type] = false;
result = 0;
out_free_both:
xSemaphoreGive(bhi160_mutex); xSemaphoreGive(bhi160_mutex);
} else { out_free_i2c:
hwlock_release(HWLOCK_I2C); hwlock_release(HWLOCK_I2C);
return -EBUSY; return result;
}
hwlock_release(HWLOCK_I2C);
return 0;
} }
void epic_bhi160_disable_all_sensors() void epic_bhi160_disable_all_sensors()
...@@ -331,18 +327,18 @@ static int bhi160_fetch_fifo(void) ...@@ -331,18 +327,18 @@ static int bhi160_fetch_fifo(void)
* You'll probably be best of leaving it as it is ... * You'll probably be best of leaving it as it is ...
*/ */
int ret = BHY_SUCCESS; int result = 0;
/* Number of bytes left in BHI160's FIFO buffer */ /* Number of bytes left in BHI160's FIFO buffer */
uint16_t bytes_left_in_fifo = 1; uint16_t bytes_left_in_fifo = 1;
int lockret = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)); result = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100));
if (lockret < 0) { if (result < 0) {
return lockret; return result;
} }
if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) != pdTRUE) { if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) != pdTRUE) {
hwlock_release(HWLOCK_I2C); result = -EBUSY;
return -EBUSY; goto out_free_i2c;
} }
while (bytes_left_in_fifo) { while (bytes_left_in_fifo) {
...@@ -364,14 +360,14 @@ static int bhi160_fetch_fifo(void) ...@@ -364,14 +360,14 @@ static int bhi160_fetch_fifo(void)
while (bytes_left > 0) { while (bytes_left > 0) {
bhy_data_generic_t sensor_data; bhy_data_generic_t sensor_data;
bhy_data_type_t data_type; bhy_data_type_t data_type;
ret = bhy_parse_next_fifo_packet( result = bhy_parse_next_fifo_packet(
&fifo_ptr, &fifo_ptr,
&bytes_left, &bytes_left,
&sensor_data, &sensor_data,
&data_type &data_type
); );
if (ret == BHY_SUCCESS) { if (result == BHY_SUCCESS) {
bhi160_handle_packet(data_type, &sensor_data); bhi160_handle_packet(data_type, &sensor_data);
} else { } else {
break; break;
...@@ -387,8 +383,9 @@ static int bhi160_fetch_fifo(void) ...@@ -387,8 +383,9 @@ static int bhi160_fetch_fifo(void)
} }
xSemaphoreGive(bhi160_mutex); xSemaphoreGive(bhi160_mutex);
out_free_i2c:
hwlock_release(HWLOCK_I2C); hwlock_release(HWLOCK_I2C);
return 0; return result;
} }
/* /*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment