Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions subsys/bluetooth/audio/vocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define LOG_MODULE_NAME bt_vocs
#include "common/log.h"

#define VALID_VOCS_OPCODE(opcode) ((opcode) == VOCS_OPCODE_SET_OFFSET)
#define VALID_VOCS_OPCODE(opcode) ((opcode) == BT_VOCS_OPCODE_SET_OFFSET)

#if defined(CONFIG_BT_VOCS)
static void offset_state_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
Expand All @@ -34,7 +34,7 @@ static void offset_state_cfg_changed(const struct bt_gatt_attr *attr, uint16_t v
static ssize_t read_offset_state(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct vocs_server *inst = attr->user_data;
struct bt_vocs_server *inst = attr->user_data;

BT_DBG("offset %d, counter %u", inst->state.offset, inst->state.change_counter);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &inst->state, sizeof(inst->state));
Expand All @@ -50,9 +50,13 @@ static void location_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value
static ssize_t write_location(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
struct vocs_server *inst = attr->user_data;
struct bt_vocs_server *inst = attr->user_data;
uint32_t old_location = inst->location;

if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}

if (len != sizeof(inst->location)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}
Expand All @@ -61,8 +65,10 @@ static ssize_t write_location(struct bt_conn *conn, const struct bt_gatt_attr *a
BT_DBG("%02x", inst->location);

if (old_location != inst->location) {
bt_gatt_notify_uuid(NULL, BT_UUID_VOCS_LOCATION, inst->service_p->attrs,
&inst->location, sizeof(inst->location));
(void)bt_gatt_notify_uuid(NULL, BT_UUID_VOCS_LOCATION,
inst->service_p->attrs,
&inst->location,
sizeof(inst->location));

if (inst->cb && inst->cb->location) {
inst->cb->location(NULL, (struct bt_vocs *)inst, 0, inst->location);
Expand All @@ -76,7 +82,7 @@ static ssize_t write_location(struct bt_conn *conn, const struct bt_gatt_attr *a
static ssize_t read_location(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct vocs_server *inst = attr->user_data;
struct bt_vocs_server *inst = attr->user_data;

BT_DBG("0x%02x", inst->location);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &inst->location,
Expand All @@ -87,10 +93,14 @@ static ssize_t read_location(struct bt_conn *conn, const struct bt_gatt_attr *at
static ssize_t write_vocs_control(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
struct vocs_server *inst = attr->user_data;
const struct vocs_control_t *cp = buf;
struct bt_vocs_server *inst = attr->user_data;
const struct bt_vocs_control *cp = buf;
bool notify = false;

if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}

if (!len || !buf) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}
Expand All @@ -101,11 +111,7 @@ static ssize_t write_vocs_control(struct bt_conn *conn, const struct bt_gatt_att
return BT_GATT_ERR(BT_VOCS_ERR_OP_NOT_SUPPORTED);
}

if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}

if (len != sizeof(struct vocs_control_t)) {
if (len != sizeof(struct bt_vocs_control)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}

Expand All @@ -117,7 +123,7 @@ static ssize_t write_vocs_control(struct bt_conn *conn, const struct bt_gatt_att
}

switch (cp->opcode) {
case VOCS_OPCODE_SET_OFFSET:
case BT_VOCS_OPCODE_SET_OFFSET:
BT_DBG("Set offset %d", cp->offset);
if (cp->offset > BT_VOCS_MAX_OFFSET || cp->offset < BT_VOCS_MIN_OFFSET) {
return BT_GATT_ERR(BT_VOCS_ERR_OUT_OF_RANGE);
Expand All @@ -136,8 +142,9 @@ static ssize_t write_vocs_control(struct bt_conn *conn, const struct bt_gatt_att
inst->state.change_counter++;
BT_DBG("New state: offset %d, counter %u",
inst->state.offset, inst->state.change_counter);
bt_gatt_notify_uuid(NULL, BT_UUID_VOCS_STATE, inst->service_p->attrs,
&inst->state, sizeof(inst->state));
(void)bt_gatt_notify_uuid(NULL, BT_UUID_VOCS_STATE,
inst->service_p->attrs,
&inst->state, sizeof(inst->state));

if (inst->cb && inst->cb->state) {
inst->cb->state(NULL, (struct bt_vocs *)inst, 0, inst->state.offset);
Expand All @@ -158,7 +165,11 @@ static void output_desc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t va
static ssize_t write_output_desc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
struct vocs_server *inst = attr->user_data;
struct bt_vocs_server *inst = attr->user_data;

if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}

if (len >= sizeof(inst->output_desc)) {
BT_DBG("Output desc was clipped from length %u to %zu",
Expand All @@ -171,8 +182,10 @@ static ssize_t write_output_desc(struct bt_conn *conn, const struct bt_gatt_attr
memcpy(inst->output_desc, buf, len);
inst->output_desc[len] = '\0';

bt_gatt_notify_uuid(NULL, BT_UUID_VOCS_DESCRIPTION, inst->service_p->attrs,
&inst->output_desc, strlen(inst->output_desc));
(void)bt_gatt_notify_uuid(NULL, BT_UUID_VOCS_DESCRIPTION,
inst->service_p->attrs,
&inst->output_desc,
strlen(inst->output_desc));

if (inst->cb && inst->cb->description) {
inst->cb->description(NULL, (struct bt_vocs *)inst, 0, inst->output_desc);
Expand All @@ -188,7 +201,7 @@ static ssize_t write_output_desc(struct bt_conn *conn, const struct bt_gatt_attr
static ssize_t read_output_desc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct vocs_server *inst = attr->user_data;
struct bt_vocs_server *inst = attr->user_data;

BT_DBG("%s", log_strdup(inst->output_desc));
return bt_gatt_attr_read(conn, attr, buf, len, offset, &inst->output_desc,
Expand Down Expand Up @@ -221,7 +234,7 @@ static ssize_t read_output_desc(struct bt_conn *conn, const struct bt_gatt_attr
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT) \
}

static struct vocs_server vocs_insts[CONFIG_BT_VOCS_MAX_INSTANCE_COUNT];
static struct bt_vocs_server vocs_insts[CONFIG_BT_VOCS_MAX_INSTANCE_COUNT];
BT_GATT_SERVICE_INSTANCE_DEFINE(vocs_service_list, vocs_insts, CONFIG_BT_VOCS_MAX_INSTANCE_COUNT,
BT_VOCS_SERVICE_DEFINITION);

Expand Down Expand Up @@ -404,10 +417,10 @@ int bt_vocs_state_set(struct bt_conn *conn, struct bt_vocs *inst, int16_t offset
return bt_vocs_client_state_set(conn, inst, offset);
} else if (IS_ENABLED(CONFIG_BT_VOCS) && !conn) {
struct bt_gatt_attr attr;
struct vocs_control_t cp;
struct bt_vocs_control cp;
int err;

cp.opcode = VOCS_OPCODE_SET_OFFSET;
cp.opcode = BT_VOCS_OPCODE_SET_OFFSET;
cp.counter = inst->srv.state.change_counter;
cp.offset = sys_cpu_to_le16(offset);

Expand Down
4 changes: 2 additions & 2 deletions subsys/bluetooth/audio/vocs_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ static uint8_t vocs_discover_func(struct bt_conn *conn, const struct bt_gatt_att
struct bt_gatt_discover_params *params)
{
struct bt_vocs *inst = (struct bt_vocs *)CONTAINER_OF(
params, struct vocs_client, discover_params);
params, struct bt_vocs_client, discover_params);

if (!attr) {
BT_DBG("Discovery complete for VOCS %p", inst);
Expand Down Expand Up @@ -514,7 +514,7 @@ int bt_vocs_client_state_set(struct bt_conn *conn, struct bt_vocs *inst, int16_t
return -EBUSY;
}

inst->cli.cp.opcode = VOCS_OPCODE_SET_OFFSET;
inst->cli.cp.opcode = BT_VOCS_OPCODE_SET_OFFSET;
inst->cli.cp.counter = inst->cli.state.change_counter;
inst->cli.cp.offset = offset;

Expand Down
26 changes: 13 additions & 13 deletions subsys/bluetooth/audio/vocs_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
#include <zephyr/types.h>

#if defined(CONFIG_BT_VOCS)
#define VOCS_MAX_DESC_SIZE CONFIG_BT_VOCS_MAX_OUTPUT_DESCRIPTION_SIZE
#define BT_VOCS_MAX_DESC_SIZE CONFIG_BT_VOCS_MAX_OUTPUT_DESCRIPTION_SIZE
#else
#define VOCS_MAX_DESC_SIZE 1
#define BT_VOCS_MAX_DESC_SIZE 1
#endif /* CONFIG_BT_VOCS */

/* VOCS opcodes */
#define VOCS_OPCODE_SET_OFFSET 0x01
#define BT_VOCS_OPCODE_SET_OFFSET 0x01

struct vocs_control_t {
struct bt_vocs_control {
uint8_t opcode;
uint8_t counter;
int16_t offset;
} __packed;

struct vocs_state_t {
struct bt_vocs_state {
int16_t offset;
uint8_t change_counter;
} __packed;

struct vocs_client {
struct vocs_state_t state;
struct bt_vocs_client {
struct bt_vocs_state state;
bool location_writable;
uint32_t location;
bool desc_writable;
Expand All @@ -49,28 +49,28 @@ struct vocs_client {
uint8_t subscribe_cnt;

bool busy;
struct vocs_control_t cp;
struct bt_vocs_control cp;
struct bt_gatt_write_params write_params;
struct bt_gatt_read_params read_params;
struct bt_vocs_cb *cb;
struct bt_gatt_discover_params discover_params;
struct bt_conn *conn;
};

struct vocs_server {
struct vocs_state_t state;
struct bt_vocs_server {
struct bt_vocs_state state;
uint32_t location;
bool initialized;
char output_desc[VOCS_MAX_DESC_SIZE];
char output_desc[BT_VOCS_MAX_DESC_SIZE];
struct bt_vocs_cb *cb;

struct bt_gatt_service *service_p;
};

struct bt_vocs {
union {
struct vocs_server srv;
struct vocs_client cli;
struct bt_vocs_server srv;
struct bt_vocs_client cli;
};
};

Expand Down