Zephyr Bluetooth peripheral callbacks

We currently have a nRF52840 central device written with the Bluetooth Nordic SoftDevice API. From what I have read, the Zephyr RTOS is the best way going forward. I started creating a peripheral Bluetooth sensor that would connect to our central using Zephyr using the samples available. In order to configure the sensor, I will have to add quite a few read|write characteristics. I do not want to have to write separate read and write callbacks for every characteristic which may increase the  code size considerably. Instead I want to use the "const struct bt_gatt_attr *attr" parameter to figure out which attribute is being read or write. I cannot figure out how to resolve the attribute handle to the specific characteristic.

This has to be very simple but I have overlooked how to do it.

static ssize_t on_write_characteristic_cb(struct bt_conn *conn,
			  const struct bt_gatt_attr *attr,
			  const void *buf,
			  uint16_t len,
			  uint16_t offset,
			  uint8_t flags)
{
	LOG_INF("Received data, handle %d, conn %p",
		attr->handle, (void *)conn);
	if (ami_cm_cb.written_cb) {
	  ... which charateristic
        }
	return len;
}

static ssize_t read_characteristic_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr,
			 void *buf, uint16_t len, uint16_t offset)
{
	uint8_t button_value = 12;
	if (ami_cm_cb.toberead_cb){
	  ... which charateristic
	}
	return bt_gatt_attr_read(conn, attr, buf, len, offset, &button_value,
				 sizeof(button_value));
}

BT_GATT_SERVICE_DEFINE(ami_setup_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_AMI_SETUP_SERVICE),
	BT_GATT_CHARACTERISTIC(BT_UUID_AMI_LOG_TYPE_CHR,
			       BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
			       BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_characteristic_cb, on_write_characteristic_cb,NULL),
	BT_GATT_CHARACTERISTIC(BT_UUID_AMI_LOG_INTERVAL_CHR,
			       BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
			       BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_characteristic_cb, on_write_characteristic_cb,NULL),
);

Related