i2s_stream.c有需要改进的地方(不判断返回值导致reboot)

Liiidr
Posts: 4
Joined: Thu Nov 28, 2019 4:28 am

i2s_stream.c有需要改进的地方(不判断返回值导致reboot)

Postby Liiidr » Fri Dec 20, 2019 8:46 am

audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config);函数在调用i2s_driver_install()时未判断返回值,在i2s驱动安装失败时,后面的程序执行导致reboot!!!!
代码中我自己添加了保护:
//modify by li begin 2019/11/13
if(i2s_driver_install(i2s->config.i2s_port, &i2s->config.i2s_config, 0, NULL) != ESP_OK){
audio_free(i2s);
return NULL;
}
//modify by li end 2019/11/13[/color]


  1. audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config)
  2. {
  3.     audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG();
  4.     audio_element_handle_t el;
  5.     cfg.open = _i2s_open;
  6.     cfg.close = _i2s_close;
  7.     cfg.process = _i2s_process;
  8.     cfg.destroy = _i2s_destroy;
  9.     cfg.task_stack = config->task_stack;
  10.     cfg.task_prio = config->task_prio;
  11.     cfg.task_core = config->task_core;
  12.     cfg.out_rb_size = config->out_rb_size;
  13.     cfg.multi_out_rb_num = config->multi_out_num;
  14.     cfg.tag = "iis";
  15.     cfg.buffer_len = I2S_STREAM_BUF_SIZE;
  16.  
  17.     i2s_stream_t *i2s = audio_calloc(1, sizeof(i2s_stream_t));
  18.     AUDIO_MEM_CHECK(TAG, i2s, return NULL);
  19.     memcpy(&i2s->config, config, sizeof(i2s_stream_cfg_t));
  20.  
  21.     i2s->type = config->type;
  22.     i2s->use_alc = config->use_alc;
  23.     i2s->volume = config->volume;
  24.  
  25.     if (config->type == AUDIO_STREAM_READER) {
  26.         cfg.read = _i2s_read;
  27.     } else if (config->type == AUDIO_STREAM_WRITER) {
  28.         cfg.write = _i2s_write;
  29.     }
  30.     el = audio_element_init(&cfg);
  31.  
  32.     AUDIO_MEM_CHECK(TAG, el, {
  33.         audio_free(i2s);
  34.         return NULL;
  35.     });
  36.     audio_element_setdata(el, i2s);
  37.  
  38.     audio_element_info_t info;
  39.     audio_element_getinfo(el, &info);
  40.     info.sample_rates = config->i2s_config.sample_rate;
  41.     info.channels = config->i2s_config.channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  42.     info.bits = config->i2s_config.bits_per_sample;
  43.  
  44.     audio_element_setinfo(el, &info);
  45. [color=#FF0000]    //modify by li begin 2019/11/13
  46.     if(i2s_driver_install(i2s->config.i2s_port, &i2s->config.i2s_config, 0, NULL) != ESP_OK){  
  47.         audio_free(i2s);
  48.         return NULL;   
  49.     }
  50.     //modify by li end   2019/11/13[/color]
  51.  
  52.     if ((config->i2s_config.mode & I2S_MODE_DAC_BUILT_IN) != 0) {
  53.         i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  54.     } else {
  55.         i2s_pin_config_t i2s_pin_cfg = {0};
  56.         get_i2s_pins(i2s->config.i2s_port, &i2s_pin_cfg);
  57.         i2s_set_pin(i2s->config.i2s_port, &i2s_pin_cfg);
  58.     }
  59.     i2s_mclk_gpio_select(i2s->config.i2s_port, GPIO_NUM_0);
  60.  
  61.     return el;
  62. }

jason.mao
Posts: 98
Joined: Mon Nov 19, 2018 2:05 am

Re: i2s_stream.c有需要改进的地方(不判断返回值导致reboot)

Postby jason.mao » Fri Dec 20, 2019 12:22 pm

谢谢你的建议

Who is online

Users browsing this forum: Google [Bot] and 38 guests