From df5769f5e8311eff63f455f2dbb72dd332167d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Rist=20Sk=C3=B8ien?= Date: Tue, 15 Aug 2023 16:40:41 +0200 Subject: [PATCH] Applications: nrf5340_audio: Resolved Coverity issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OCT-2680 Signed-off-by: Kristoffer Rist Skøien --- .../nrf5340_audio/src/audio/audio_datapath.c | 1 - .../nrf5340_audio/src/audio/audio_system.c | 10 ++--- .../nrf5340_audio/src/audio/streamctrl.c | 1 - .../nrf5340_audio/src/audio/sw_codec_select.c | 41 +++++++++---------- .../bt_management/advertising/bt_mgmt_adv.c | 5 +++ .../src/bluetooth/bt_management/bt_mgmt.c | 2 +- .../bluetooth/bt_management/dfu/bt_mgmt_dfu.c | 3 -- .../bt_renderer/volume/bt_rend_vol.c | 4 +- .../src/bluetooth/le_audio_cis_gateway.c | 34 +++++++-------- .../nrf5340_audio/src/modules/audio_usb.c | 2 +- applications/nrf5340_audio/src/modules/led.c | 17 ++++---- .../nrf5340_audio/src/utils/board_version.c | 3 +- 12 files changed, 58 insertions(+), 65 deletions(-) diff --git a/applications/nrf5340_audio/src/audio/audio_datapath.c b/applications/nrf5340_audio/src/audio/audio_datapath.c index 9d95572c35d7..566076e6469b 100644 --- a/applications/nrf5340_audio/src/audio/audio_datapath.c +++ b/applications/nrf5340_audio/src/audio/audio_datapath.c @@ -15,7 +15,6 @@ #include "nrf5340_audio_common.h" #include "macros_common.h" -#include "board.h" #include "led.h" #include "audio_i2s.h" #include "sw_codec_select.h" diff --git a/applications/nrf5340_audio/src/audio/audio_system.c b/applications/nrf5340_audio/src/audio/audio_system.c index b2a763de2b82..4e01e17d0b84 100644 --- a/applications/nrf5340_audio/src/audio/audio_system.c +++ b/applications/nrf5340_audio/src/audio/audio_system.c @@ -14,7 +14,6 @@ #include "audio_datapath.h" #include "audio_i2s.h" #include "data_fifo.h" -#include "led.h" #include "hw_codec.h" #include "tone.h" #include "contin_array.h" @@ -303,11 +302,10 @@ void audio_system_start(void) sw_codec_cfg.initialized = true; if (sw_codec_cfg.encoder.enabled && encoder_thread_id == NULL) { - encoder_thread_id = - k_thread_create(&encoder_thread_data, encoder_thread_stack, - CONFIG_ENCODER_STACK_SIZE, (k_thread_entry_t)encoder_thread, - NULL, NULL, NULL, - K_PRIO_PREEMPT(CONFIG_ENCODER_THREAD_PRIO), 0, K_NO_WAIT); + encoder_thread_id = k_thread_create( + &encoder_thread_data, encoder_thread_stack, CONFIG_ENCODER_STACK_SIZE, + (k_thread_entry_t)encoder_thread, NULL, NULL, NULL, + K_PRIO_PREEMPT(CONFIG_ENCODER_THREAD_PRIO), 0, K_NO_WAIT); ret = k_thread_name_set(encoder_thread_id, "ENCODER"); ERR_CHK(ret); } diff --git a/applications/nrf5340_audio/src/audio/streamctrl.c b/applications/nrf5340_audio/src/audio/streamctrl.c index 81af5f03631b..821c02a61611 100644 --- a/applications/nrf5340_audio/src/audio/streamctrl.c +++ b/applications/nrf5340_audio/src/audio/streamctrl.c @@ -23,7 +23,6 @@ #include "audio_system.h" #include "button_handler.h" #include "data_fifo.h" -#include "board.h" #include "le_audio.h" #include "bt_mgmt.h" #include "bt_rend.h" diff --git a/applications/nrf5340_audio/src/audio/sw_codec_select.c b/applications/nrf5340_audio/src/audio/sw_codec_select.c index d1aff99ca0e7..19f95950fcff 100644 --- a/applications/nrf5340_audio/src/audio/sw_codec_select.c +++ b/applications/nrf5340_audio/src/audio/sw_codec_select.c @@ -23,7 +23,7 @@ static struct sw_codec_config m_config; int sw_codec_encode(void *pcm_data, size_t pcm_size, uint8_t **encoded_data, size_t *encoded_size) { /* Temp storage for split stereo PCM signal */ - char pcm_data_mono[AUDIO_CH_NUM][PCM_NUM_BYTES_MONO] = { 0 }; + char pcm_data_mono[AUDIO_CH_NUM][PCM_NUM_BYTES_MONO] = {0}; /* Make sure we have enough space for two frames (stereo) */ static uint8_t m_encoded_data[ENC_MAX_FRAME_SIZE * AUDIO_CH_NUM]; @@ -109,7 +109,7 @@ int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool } int ret; - char pcm_data_mono[PCM_NUM_BYTES_MONO] = { 0 }; + char pcm_data_mono[PCM_NUM_BYTES_MONO] = {0}; static char pcm_data_stereo[PCM_NUM_BYTES_STEREO]; size_t pcm_size_stereo = 0; @@ -119,7 +119,7 @@ int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool case SW_CODEC_LC3: { #if (CONFIG_SW_CODEC_LC3) /* Typically used for right channel if stereo signal */ - char pcm_data_mono_right[PCM_NUM_BYTES_MONO] = { 0 }; + char pcm_data_mono_right[PCM_NUM_BYTES_MONO] = {0}; switch (m_config.decoder.num_ch) { case SW_CODEC_MONO: { @@ -127,10 +127,9 @@ int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool memset(pcm_data_mono, 0, PCM_NUM_BYTES_MONO); pcm_size_session = PCM_NUM_BYTES_MONO; } else { - ret = sw_codec_lc3_dec_run(encoded_data, encoded_size, - LC3_PCM_NUM_BYTES_MONO, 0, pcm_data_mono, - (uint16_t *)&pcm_size_session, - bad_frame); + ret = sw_codec_lc3_dec_run( + encoded_data, encoded_size, LC3_PCM_NUM_BYTES_MONO, 0, + pcm_data_mono, (uint16_t *)&pcm_size_session, bad_frame); if (ret) { return ret; } @@ -155,20 +154,18 @@ int sw_codec_decode(uint8_t const *const encoded_data, size_t encoded_size, bool pcm_size_session = PCM_NUM_BYTES_MONO; } else { /* Decode left channel */ - ret = sw_codec_lc3_dec_run(encoded_data, encoded_size / 2, - LC3_PCM_NUM_BYTES_MONO, AUDIO_CH_L, - pcm_data_mono, - (uint16_t *)&pcm_size_session, - bad_frame); + ret = sw_codec_lc3_dec_run( + encoded_data, encoded_size / 2, LC3_PCM_NUM_BYTES_MONO, + AUDIO_CH_L, pcm_data_mono, (uint16_t *)&pcm_size_session, + bad_frame); if (ret) { return ret; } /* Decode right channel */ - ret = sw_codec_lc3_dec_run((encoded_data + (encoded_size / 2)), - encoded_size / 2, LC3_PCM_NUM_BYTES_MONO, - AUDIO_CH_R, pcm_data_mono_right, - (uint16_t *)&pcm_size_session, - bad_frame); + ret = sw_codec_lc3_dec_run( + (encoded_data + (encoded_size / 2)), encoded_size / 2, + LC3_PCM_NUM_BYTES_MONO, AUDIO_CH_R, pcm_data_mono_right, + (uint16_t *)&pcm_size_session, bad_frame); if (ret) { return ret; } @@ -288,19 +285,19 @@ int sw_codec_init(struct sw_codec_config sw_codec_cfg) CONFIG_AUDIO_SAMPLE_RATE_HZ, CONFIG_AUDIO_BIT_DEPTH_BITS, CONFIG_AUDIO_FRAME_DURATION_US, sw_codec_cfg.decoder.num_ch); - ret = sw_codec_lc3_dec_init(CONFIG_AUDIO_SAMPLE_RATE_HZ, - CONFIG_AUDIO_BIT_DEPTH_BITS, - CONFIG_AUDIO_FRAME_DURATION_US, - sw_codec_cfg.decoder.num_ch); + ret = sw_codec_lc3_dec_init( + CONFIG_AUDIO_SAMPLE_RATE_HZ, CONFIG_AUDIO_BIT_DEPTH_BITS, + CONFIG_AUDIO_FRAME_DURATION_US, sw_codec_cfg.decoder.num_ch); if (ret) { return ret; } } break; -#endif /* (CONFIG_SW_CODEC_LC3) */ +#else LOG_ERR("LC3 is not compiled in, please open menuconfig and select LC3"); return -ENODEV; +#endif /* (CONFIG_SW_CODEC_LC3) */ } default: LOG_ERR("Unsupported codec: %d", sw_codec_cfg.sw_codec); diff --git a/applications/nrf5340_audio/src/bluetooth/bt_management/advertising/bt_mgmt_adv.c b/applications/nrf5340_audio/src/bluetooth/bt_management/advertising/bt_mgmt_adv.c index 05132be5a62c..701a668a4213 100644 --- a/applications/nrf5340_audio/src/bluetooth/bt_management/advertising/bt_mgmt_adv.c +++ b/applications/nrf5340_audio/src/bluetooth/bt_management/advertising/bt_mgmt_adv.c @@ -52,6 +52,11 @@ static void bond_find(const struct bt_bond_info *info, void *user_data) struct bt_conn_info conn_info; ret = bt_conn_get_info(conn, &conn_info); + if (ret) { + LOG_WRN("Could not get conn info"); + bt_conn_unref(conn); + return; + } if (conn_info.state == BT_CONN_STATE_CONNECTED) { LOG_WRN("Already connected"); diff --git a/applications/nrf5340_audio/src/bluetooth/bt_management/bt_mgmt.c b/applications/nrf5340_audio/src/bluetooth/bt_management/bt_mgmt.c index bc2d5565b0b7..aee4b30a36c5 100644 --- a/applications/nrf5340_audio/src/bluetooth/bt_management/bt_mgmt.c +++ b/applications/nrf5340_audio/src/bluetooth/bt_management/bt_mgmt.c @@ -74,7 +74,7 @@ static void conn_state_connected_check(struct bt_conn *conn, void *data) static void connected_cb(struct bt_conn *conn, uint8_t err) { int ret; - char addr[BT_ADDR_LE_STR_LEN]; + char addr[BT_ADDR_LE_STR_LEN] = {0}; uint8_t num_conn = 0; uint16_t conn_handle; enum ble_hci_vs_tx_power conn_tx_pwr; diff --git a/applications/nrf5340_audio/src/bluetooth/bt_management/dfu/bt_mgmt_dfu.c b/applications/nrf5340_audio/src/bluetooth/bt_management/dfu/bt_mgmt_dfu.c index 93ff6a9615df..388c6db0ab4d 100644 --- a/applications/nrf5340_audio/src/bluetooth/bt_management/dfu/bt_mgmt_dfu.c +++ b/applications/nrf5340_audio/src/bluetooth/bt_management/dfu/bt_mgmt_dfu.c @@ -11,11 +11,8 @@ #include #include #include -#include #include "string.h" -#include "button_assignments.h" -#include "button_handler.h" #include "macros_common.h" #include "channel_assignment.h" diff --git a/applications/nrf5340_audio/src/bluetooth/bt_renderer/volume/bt_rend_vol.c b/applications/nrf5340_audio/src/bluetooth/bt_renderer/volume/bt_rend_vol.c index ceb01cdb8807..40d0d37e1fb5 100644 --- a/applications/nrf5340_audio/src/bluetooth/bt_renderer/volume/bt_rend_vol.c +++ b/applications/nrf5340_audio/src/bluetooth/bt_renderer/volume/bt_rend_vol.c @@ -244,9 +244,9 @@ int bt_rend_vol_down(void) i, ret); } } - - return 0; } + + return 0; } else if (IS_ENABLED(CONFIG_BT_VCP_VOL_REND)) { return bt_vcp_vol_rend_unmute_vol_down(); } diff --git a/applications/nrf5340_audio/src/bluetooth/le_audio_cis_gateway.c b/applications/nrf5340_audio/src/bluetooth/le_audio_cis_gateway.c index 167355940d85..56c0f087c135 100644 --- a/applications/nrf5340_audio/src/bluetooth/le_audio_cis_gateway.c +++ b/applications/nrf5340_audio/src/bluetooth/le_audio_cis_gateway.c @@ -19,7 +19,6 @@ #include "macros_common.h" #include "nrf5340_audio_common.h" -#include "channel_assignment.h" #include LOG_MODULE_REGISTER(cis_gateway, CONFIG_BLE_LOG_LEVEL); @@ -375,7 +374,7 @@ static void pac_record_cb(struct bt_conn *conn, enum bt_audio_dir dir, const str } /* num_caps is an increasing index that starts at 0 */ - if (temp_cap[temp_cap_index].num_caps <= ARRAY_SIZE(temp_cap[temp_cap_index].codec)) { + if (temp_cap[temp_cap_index].num_caps < ARRAY_SIZE(temp_cap[temp_cap_index].codec)) { struct bt_codec *codec_loc = &temp_cap[temp_cap_index].codec[temp_cap[temp_cap_index].num_caps]; @@ -1021,7 +1020,7 @@ static int discover_source(struct bt_conn *conn) return ret; } -static int iso_stream_send(uint8_t const *const data, size_t size, struct le_audio_headset headset) +static int iso_stream_send(uint8_t const *const data, size_t size, struct le_audio_headset *headset) { int ret; struct net_buf *buf; @@ -1034,18 +1033,18 @@ static int iso_stream_send(uint8_t const *const data, size_t size, struct le_aud * Data will be discarded if allocation becomes too high, to avoid audio delays. * If the NET and APP core operates in clock sync, discarding should not occur. */ - if (atomic_get(&headset.iso_tx_pool_alloc) >= HCI_ISO_BUF_ALLOC_PER_CHAN) { - if (!headset.hci_wrn_printed) { - LOG_WRN("HCI ISO TX overrun on %s ch - Single print", headset.ch_name); - headset.hci_wrn_printed = true; + if (atomic_get(&headset->iso_tx_pool_alloc) >= HCI_ISO_BUF_ALLOC_PER_CHAN) { + if (!headset->hci_wrn_printed) { + LOG_WRN("HCI ISO TX overrun on %s ch - Single print", headset->ch_name); + headset->hci_wrn_printed = true; } return -ENOMEM; } - headset.hci_wrn_printed = false; + headset->hci_wrn_printed = false; - buf = net_buf_alloc(headset.iso_tx_pool, K_NO_WAIT); + buf = net_buf_alloc(headset->iso_tx_pool, K_NO_WAIT); if (buf == NULL) { /* This should never occur because of the iso_tx_pool_alloc check above */ LOG_WRN("Out of TX buffers"); @@ -1055,14 +1054,15 @@ static int iso_stream_send(uint8_t const *const data, size_t size, struct le_aud net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE); net_buf_add_mem(buf, data, size); - atomic_inc(&headset.iso_tx_pool_alloc); + atomic_inc(&headset->iso_tx_pool_alloc); - ret = bt_bap_stream_send(&headset.sink_stream, buf, - get_and_incr_seq_num(&headset.sink_stream), BT_ISO_TIMESTAMP_NONE); + ret = bt_bap_stream_send(&headset->sink_stream, buf, + get_and_incr_seq_num(&headset->sink_stream), + BT_ISO_TIMESTAMP_NONE); if (ret < 0) { - LOG_WRN("Failed to send audio data to %s: %d", headset.ch_name, ret); + LOG_WRN("Failed to send audio data to %s: %d", headset->ch_name, ret); net_buf_unref(buf); - atomic_dec(&headset.iso_tx_pool_alloc); + atomic_dec(&headset->iso_tx_pool_alloc); } return 0; @@ -1349,7 +1349,7 @@ int le_audio_send(struct encoded_audio enc_audio) } if (ep_state_check(headsets[AUDIO_CH_L].sink_stream.ep, BT_BAP_EP_STATE_STREAMING)) { - ret = iso_stream_send(enc_audio.data, data_size_pr_stream, headsets[AUDIO_CH_L]); + ret = iso_stream_send(enc_audio.data, data_size_pr_stream, &headsets[AUDIO_CH_L]); if (ret) { LOG_DBG("Failed to send data to left channel"); } @@ -1358,10 +1358,10 @@ int le_audio_send(struct encoded_audio enc_audio) if (ep_state_check(headsets[AUDIO_CH_R].sink_stream.ep, BT_BAP_EP_STATE_STREAMING)) { if (enc_audio.num_ch == 1) { ret = iso_stream_send(enc_audio.data, data_size_pr_stream, - headsets[AUDIO_CH_R]); + &headsets[AUDIO_CH_R]); } else { ret = iso_stream_send(&enc_audio.data[data_size_pr_stream], - data_size_pr_stream, headsets[AUDIO_CH_R]); + data_size_pr_stream, &headsets[AUDIO_CH_R]); } if (ret) { LOG_DBG("Failed to send data to right channel"); diff --git a/applications/nrf5340_audio/src/modules/audio_usb.c b/applications/nrf5340_audio/src/modules/audio_usb.c index f3a9bf3b9aca..1dc0b38daedb 100644 --- a/applications/nrf5340_audio/src/modules/audio_usb.c +++ b/applications/nrf5340_audio/src/modules/audio_usb.c @@ -75,7 +75,7 @@ static void data_received(const struct device *dev, struct net_buf *buffer, size return; } - if (!buffer || !size) { + if (buffer == NULL || size == 0) { /* This should never happen */ ERR_CHK(-EINVAL); } diff --git a/applications/nrf5340_audio/src/modules/led.c b/applications/nrf5340_audio/src/modules/led.c index 9dc0b93880c9..79a1385db2cc 100644 --- a/applications/nrf5340_audio/src/modules/led.c +++ b/applications/nrf5340_audio/src/modules/led.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -19,19 +18,19 @@ #include LOG_MODULE_REGISTER(led, CONFIG_MODULE_LED_LOG_LEVEL); -#define BLINK_FREQ_MS 1000 +#define BLINK_FREQ_MS 1000 /* Maximum number of LED_UNITS. 1 RGB LED = 1 UNIT of 3 LEDS */ -#define LED_UNIT_MAX 10 -#define NUM_COLORS_RGB 3 -#define BASE_10 10 -#define DT_LABEL_AND_COMMA(node_id) DT_PROP(node_id, label), +#define LED_UNIT_MAX 10 +#define NUM_COLORS_RGB 3 +#define BASE_10 10 +#define DT_LABEL_AND_COMMA(node_id) DT_PROP(node_id, label), #define GPIO_DT_SPEC_GET_AND_COMMA(node_id) GPIO_DT_SPEC_GET(node_id, gpios), /* The following arrays are populated compile time from the .dts*/ -static const char *const led_labels[] = { DT_FOREACH_CHILD(DT_PATH(leds), DT_LABEL_AND_COMMA) }; +static const char *const led_labels[] = {DT_FOREACH_CHILD(DT_PATH(leds), DT_LABEL_AND_COMMA)}; -static const struct gpio_dt_spec leds[] = { DT_FOREACH_CHILD(DT_PATH(leds), - GPIO_DT_SPEC_GET_AND_COMMA) }; +static const struct gpio_dt_spec leds[] = { + DT_FOREACH_CHILD(DT_PATH(leds), GPIO_DT_SPEC_GET_AND_COMMA)}; enum led_type { LED_MONOCHROME, diff --git a/applications/nrf5340_audio/src/utils/board_version.c b/applications/nrf5340_audio/src/utils/board_version.c index 43be3df4aa2d..d768a7a77ce9 100644 --- a/applications/nrf5340_audio/src/utils/board_version.c +++ b/applications/nrf5340_audio/src/utils/board_version.c @@ -10,7 +10,6 @@ #include #include #include -#include #include "board.h" #include "macros_common.h" @@ -24,7 +23,7 @@ static const struct adc_dt_spec adc = ADC_DT_SPEC_GET(BOARD_ID); static const struct gpio_dt_spec power_gpios = GPIO_DT_SPEC_GET(BOARD_ID, power_gpios); /* We allow the ADC register value to deviate by N points in either direction */ -#define BOARD_VERSION_TOLERANCE 70 +#define BOARD_VERSION_TOLERANCE 70 #define VOLTAGE_STABILIZE_TIME_US 5 static int16_t sample_buffer;