Skip to content

Commit

Permalink
Applications: nrf5340_audio: Resolved Coverity issues
Browse files Browse the repository at this point in the history
OCT-2680

Signed-off-by: Kristoffer Rist Skøien <kristoffer.skoien@nordicsemi.no>
  • Loading branch information
koffes authored and rlubos committed Aug 21, 2023
1 parent 5f944dd commit df5769f
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 65 deletions.
1 change: 0 additions & 1 deletion applications/nrf5340_audio/src/audio/audio_datapath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 4 additions & 6 deletions applications/nrf5340_audio/src/audio/audio_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion applications/nrf5340_audio/src/audio/streamctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 19 additions & 22 deletions applications/nrf5340_audio/src/audio/sw_codec_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -119,18 +119,17 @@ 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: {
if (bad_frame && IS_ENABLED(CONFIG_SW_CODEC_OVERRIDE_PLC)) {
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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/settings/settings.h>

#include "string.h"
#include "button_assignments.h"
#include "button_handler.h"
#include "macros_common.h"
#include "channel_assignment.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
34 changes: 17 additions & 17 deletions applications/nrf5340_audio/src/bluetooth/le_audio_cis_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "macros_common.h"
#include "nrf5340_audio_common.h"
#include "channel_assignment.h"

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(cis_gateway, CONFIG_BLE_LOG_LEVEL);
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion applications/nrf5340_audio/src/modules/audio_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
17 changes: 8 additions & 9 deletions applications/nrf5340_audio/src/modules/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <zephyr/kernel.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <hal/nrf_gpio.h>
#include <string.h>
#include <stdlib.h>
#include <zephyr/device.h>
Expand All @@ -19,19 +18,19 @@
#include <zephyr/logging/log.h>
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,
Expand Down
3 changes: 1 addition & 2 deletions applications/nrf5340_audio/src/utils/board_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/gpio.h>
#include <stdlib.h>
#include <nrfx_saadc.h>

#include "board.h"
#include "macros_common.h"
Expand All @@ -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;
Expand Down

0 comments on commit df5769f

Please sign in to comment.