For me it is confusing how the Nordic's exercise and sample apps handle the state of CCC descriptor value.
Adding notification and indication support
Why do we need to manage a user app level notify_enabled variable at all?
static void mylbsbc_ccc_mysensor_cfg_changed(const struct bt_gatt_attr *attr,
uint16_t value)
{
notify_mysensor_enabled = (value == BT_GATT_CCC_NOTIFY);
}
int my_lbs_send_sensor_notify(uint32_t sensor_value)
{
if (!notify_mysensor_enabled) {
return -EACCES;
}
return bt_gatt_notify(NULL, &my_lbs_svc.attrs[7],
&sensor_value,
sizeof(sensor_value));
}
This is probably a harmful data redundancy. This data have to be present somewhere else too (in the service's attribute list) and can be read back.
Obviously when the central reads the CCC descriptor value, the SDK won't call back me to access my variable.
We need some SDK function similar to what bt_gatt_attr_read_ccc do. Maybe could be named to bt_gatt_ccc_get_value.
ssize_t bt_gatt_attr_read_ccc(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
struct _bt_gatt_ccc *ccc = attr->user_data;
const struct bt_gatt_ccc_cfg *cfg;
uint16_t value;
cfg = find_ccc_cfg(conn, ccc);
if (cfg) {
value = sys_cpu_to_le16(cfg->value);
} else {
/* Default to disable if there is no cfg for the peer */
value = 0x0000;
}
return bt_gatt_attr_read(conn, attr, buf, len, offset, &value,
sizeof(value));
}
I could copy and use this code, but don't know how to access the conn object?
Should I memoize it from the connected callback?
void (*connected)(struct bt_conn *conn, uint8_t err);
Thanks in advance,
Attila