Skip to content

Commit 21037ef

Browse files
GoldenBugfacebook-github-bot
authored andcommitted
Refactor drive_init() to Remove "if" Nesting.
Summary: drive_init() functions contains deeply nested if statements. We should remove the nesting so it's easier to understand the flow. Reviewed By: doranand Differential Revision: D37155640 fbshipit-source-id: a68f5102211fc19c1404104ac7c331870aa9a123
1 parent 3db4826 commit 21037ef

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

‎common/service/sensor/sensor.c‎

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -369,41 +369,48 @@ void add_sensor_config(sensor_cfg config)
369369
}
370370
}
371371

372+
static inline bool init_drive_type(sensor_cfg *p, uint16_t current_drive)
373+
{
374+
int ret = -1;
375+
if (p->type == sensor_drive_tbl[current_drive].dev) {
376+
return false;
377+
}
378+
379+
if (p->pre_sensor_read_hook) {
380+
if (p->pre_sensor_read_hook(p->num, p->pre_sensor_read_args) == false) {
381+
printf("[%s] sensor 0x%x pre sensor read failed!\n", __func__, p->num);
382+
return false;
383+
}
384+
}
385+
386+
ret = sensor_drive_tbl[current_drive].init(p->num);
387+
if (ret != SENSOR_INIT_SUCCESS) {
388+
printf("sensor num %d initial fail, ret %d\n", p->num, ret);
389+
}
390+
391+
if (p->post_sensor_read_hook) {
392+
if (p->post_sensor_read_hook(p->num, p->post_sensor_read_args, NULL) == false) {
393+
printf("[%s] sensor 0x%x post sensor read failed!\n", __func__, p->num);
394+
}
395+
}
396+
397+
return true;
398+
}
399+
372400
static void drive_init(void)
373401
{
374-
uint16_t drive_num = ARRAY_SIZE(sensor_drive_tbl);
375-
uint16_t i, j;
376-
uint8_t ret;
402+
const uint16_t max_drive_num = ARRAY_SIZE(sensor_drive_tbl);
403+
uint16_t current_drive;
377404

378-
for (i = 0; i < SDR_NUM; i++) {
405+
for (int i = 0; i < SDR_NUM; i++) {
379406
sensor_cfg *p = sensor_config + i;
380-
for (j = 0; j < drive_num; j++) {
381-
if (p->type == sensor_drive_tbl[j].dev) {
382-
if (p->pre_sensor_read_hook) {
383-
if (p->pre_sensor_read_hook(
384-
p->num, p->pre_sensor_read_args) == false) {
385-
printf("[%s] sensor 0x%x pre sensor read failed!\n",
386-
__func__, p->num);
387-
continue;
388-
}
389-
}
390-
ret = sensor_drive_tbl[j].init(p->num);
391-
if (ret != SENSOR_INIT_SUCCESS)
392-
printf("sensor num %d initial fail, ret %d\n", p->num, ret);
393-
394-
if (p->post_sensor_read_hook) {
395-
if (p->post_sensor_read_hook(p->num,
396-
p->post_sensor_read_args,
397-
NULL) == false) {
398-
printf("[%s] sensor 0x%x post sensor read failed!\n",
399-
__func__, p->num);
400-
}
401-
}
407+
for (current_drive = 0; current_drive < max_drive_num; current_drive++) {
408+
if (init_drive_type(p, current_drive)) {
402409
break;
403410
}
404411
}
405412

406-
if (j == drive_num) {
413+
if (current_drive == max_drive_num) {
407414
printf("sensor %d, type = %d is not supported!\n", i, p->type);
408415
p->read = NULL;
409416
}

0 commit comments

Comments
 (0)