static void att_find_type_rsp(struct bt_conn *conn, uint8_t err, const void *pdu, uint16_t length, void *user_data) { const struct bt_att_find_type_rsp *rsp = pdu; struct bt_gatt_discover_params *params = user_data; struct bt_gatt_service value; uint8_t i; uint16_t end_handle = 0, start_handle; BT_DBG("err 0x%02x", err); if (err) { goto done; } /* Parse attributes found */ for (i = 0; length >= sizeof(rsp->list[i]); i++, length -= sizeof(rsp->list[i])) { struct bt_gatt_attr *attr; start_handle = sys_le16_to_cpu(rsp->list[i].start_handle); end_handle = sys_le16_to_cpu(rsp->list[i].end_handle); BT_DBG("start_handle 0x%04x end_handle 0x%04x", start_handle, end_handle); value.end_handle = end_handle; value.uuid = params->uuid; if (params->type == BT_GATT_DISCOVER_PRIMARY) { attr = (&(struct bt_gatt_attr) BT_GATT_PRIMARY_SERVICE(&value)); } else { attr = (&(struct bt_gatt_attr) BT_GATT_SECONDARY_SERVICE(&value)); } attr->handle = start_handle; if (params->func(conn, attr, params) == BT_GATT_ITER_STOP) { return; } } /* Stop if could not parse the whole PDU */ if (length > 0) { goto done; } /* Stop if over the range or the requests */ if (end_handle >= params->end_handle) { goto done; } /* Continue for the last found handle */ params->start_handle = end_handle; if (!bt_gatt_discover(conn, params)) { return; } done: params->func(conn, NULL, params); }
return bt_gatt_attr_read(conn, attr, buf, len, offset, gap_name, strlen(gap_name)); } static ssize_t read_appearance(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { uint16_t appearance = sys_cpu_to_le16(gap_appearance); return bt_gatt_attr_read(conn, attr, buf, len, offset, &appearance, sizeof(appearance)); } static struct bt_gatt_attr attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP), BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_GAP_DEVICE_NAME, BT_GATT_PERM_READ, read_name, NULL, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_GAP_APPEARANCE, BT_GATT_PERM_READ, read_appearance, NULL, NULL), }; void gap_init(const char *name, uint16_t appearance) { gap_name = name; gap_appearance = appearance; bt_gatt_register(attrs, ARRAY_SIZE(attrs)); }
__weak const enum ble_hrs_location ble_hrs_sensor_location = BLE_HRS_WRIST; static ssize_t read_body_sensor_location(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { uint8_t value = ble_hrs_sensor_location; return bt_gatt_attr_read(conn, attr, buf, len, offset, &value, sizeof(value)); } /* HRS Service Declaration */ static const struct bt_gatt_attr hrs_attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_HRS), /* Heart Rate Measurement */ BT_GATT_CHARACTERISTIC(BT_UUID_HRS_MEASUREMENT, BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_HRS_MEASUREMENT, 0, NULL, NULL, NULL), BT_GATT_CCC(hrs_measurement_ccc_cfg, hrs_measurement_ccc_cfg_changed), /* Body Sensor Location */ BT_GATT_CHARACTERISTIC(BT_UUID_HRS_BODY_SENSOR, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_HRS_BODY_SENSOR, BT_GATT_PERM_READ, read_body_sensor_location, NULL, NULL), BT_GATT_CCC(hrs_measurement_ccc_cfg, hrs_measurement_ccc_cfg_changed), }; /* Pointer to the HRS value attribute in the above table */ static struct bt_gatt_attr const *const hrs_value = &hrs_attrs[2];
{ if (value & BT_GATT_CCC_NOTIFY) { /* Start running speed and cadence sensor */ pr_debug(LOG_MODULE_BLE, "RSCS enabled"); on_ble_rscs_enabled(); } else { /* Stop running speed and cadence sensor */ pr_debug(LOG_MODULE_BLE, "RSCS disabled"); on_ble_rscs_disabled(); } } /* RSC Service Declaration */ static const struct bt_gatt_attr rscs_attrs[] = { /* Running Speed and Cadence Service */ BT_GATT_PRIMARY_SERVICE(BT_UUID_RSCS), /* RSC Measurement */ BT_GATT_CHARACTERISTIC(BT_UUID_RSC_MEASUREMENT, BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_RSC_MEASUREMENT, 0, NULL, NULL, NULL), BT_GATT_CCC(rsc_measurement_ccc_cfg, rsc_measurement_ccc_cfg_changed), /* RSC Feature */ BT_GATT_CHARACTERISTIC(BT_UUID_RSC_FEATURE, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_RSC_FEATURE, BT_GATT_PERM_READ, read_stepcadence_feature, NULL, NULL), #ifdef CONFIG_BLE_RSCS_SENSOR_LOCATION_SUPPORT /* Sensor Location*/ BT_GATT_CHARACTERISTIC(BT_UUID_SENSOR_LOCATION, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_SENSOR_LOCATION, BT_GATT_PERM_READ, read_sensor_location, NULL, NULL),
ctrl_point_ind(conn, req->op, SC_CP_RSP_SUCCESS, &supported_locations, sizeof(supported_locations)); return len; default: status = SC_CP_RSP_OP_NOT_SUPP; } ctrl_point_ind(conn, req->op, status, NULL, 0); return len; } static struct bt_gatt_attr csc_attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_CSC), BT_GATT_CHARACTERISTIC(BT_UUID_CSC_MEASUREMENT, BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_CSC_MEASUREMENT, 0x00, NULL, NULL, NULL), BT_GATT_CCC(csc_meas_ccc_cfg, csc_meas_ccc_cfg_changed), BT_GATT_CHARACTERISTIC(BT_UUID_SENSOR_LOCATION, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_SENSOR_LOCATION, BT_GATT_PERM_READ, read_location, NULL, &sensor_location), BT_GATT_CHARACTERISTIC(BT_UUID_CSC_FEATURE, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_CSC_FEATURE, BT_GATT_PERM_READ, read_csc_feature, NULL, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_SC_CONTROL_POINT, BT_GATT_CHRC_WRITE | BT_GATT_CHRC_INDICATE), BT_GATT_DESCRIPTOR(BT_UUID_SC_CONTROL_POINT, BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &sensor_location), BT_GATT_CCC(ctrl_point_ccc_cfg, ctrl_point_ccc_cfg_changed), };
static struct bt_uuid_128 power_uuid = BT_UUID_INIT_128( 0xE5, 0x0B, 0x07, 0x6C, 0x26, 0x08, 0x3D, 0x85, 0xCD, 0x44, 0x82, 0x30, 0x48, 0xC4, 0x10, 0x9C); /* Characteristic UUID of consumption power 71C9E918-8302-47AA-89D3-7BF67152237A */ static struct bt_uuid_128 consumption_power_uuid = BT_UUID_INIT_128( 0x7A, 0x23, 0x52, 0x71, 0xF6, 0x7B, 0xD3, 0x89, 0xAA, 0x47, 0x02, 0x83, 0x18, 0xE9, 0xC9, 0x71); /* Characteristic UUID of solar power 0609E802-AFD2-4D56-B61C-12BA1F80CCB6 */ static struct bt_uuid_128 solar_power_uuid = BT_UUID_INIT_128( 0xB6, 0xCC, 0x80, 0x1F, 0xBA, 0x12, 0x1C, 0xB6, 0x56, 0x4D, 0xD2, 0xAF, 0x02, 0xE8, 0x09, 0x06); static struct bt_gatt_attr attrs[] = { BT_GATT_PRIMARY_SERVICE(&power_uuid), BT_GATT_CHARACTERISTIC(&consumption_power_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(&consumption_power_uuid.uuid, BT_GATT_PERM_READ, read_u32, NULL, &consumption_value), BT_GATT_CUD(CONSUMPTION_CUD, BT_GATT_PERM_READ), BT_GATT_CCC(consumption_ccc_cfg, sensor_ccc_cfg_changed), BT_GATT_CHARACTERISTIC(&solar_power_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(&solar_power_uuid.uuid, BT_GATT_PERM_READ, read_u32, NULL, &solar_value), BT_GATT_CUD(SOLAR_CUD, BT_GATT_PERM_READ), BT_GATT_CCC(solar_ccc_cfg, sensor_ccc_cfg_changed) };
static ssize_t write_rgb(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset) { memcpy(rgb, buf, sizeof(rgb)); printk("write_rgb: %x %x %x\n", rgb[0], rgb[1], rgb[2]); pwm_write(); return sizeof(rgb); } /* WebBT Service Declaration */ static struct bt_gatt_attr attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_WEBBT), /* Temperature */ BT_GATT_CHARACTERISTIC(BT_UUID_TEMP, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_TEMP, BT_GATT_PERM_READ, read_temperature, NULL, &temperature), BT_GATT_CUD(SENSOR_1_NAME, BT_GATT_PERM_READ), BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), /* RGB Led */ BT_GATT_CHARACTERISTIC(BT_UUID_RGB, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE), BT_GATT_DESCRIPTOR(BT_UUID_RGB, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_rgb, write_rgb, rgb), BT_GATT_CUD(SENSOR_2_NAME, BT_GATT_PERM_READ),
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } memcpy(value + offset, buf, len); return len; } static const struct bt_uuid_128 vnd_signed_uuid = BT_UUID_INIT_128( 0xf3, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x13, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x13); /* Vendor Primary Service Declaration */ static struct bt_gatt_attr vnd_attrs[] = { /* Vendor Primary Service Declaration */ BT_GATT_PRIMARY_SERVICE(&vnd_uuid), BT_GATT_CHARACTERISTIC(&vnd_enc_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_INDICATE), BT_GATT_DESCRIPTOR(&vnd_enc_uuid.uuid, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT, read_vnd, write_vnd, vnd_value), BT_GATT_CCC(vnd_ccc_cfg, vnd_ccc_cfg_changed), BT_GATT_CHARACTERISTIC(&vnd_auth_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE), BT_GATT_DESCRIPTOR(&vnd_auth_uuid.uuid, BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN, read_vnd, write_vnd, vnd_value), BT_GATT_CHARACTERISTIC(&vnd_long_uuid.uuid, BT_GATT_CHRC_READ |
{ simulate_blvl = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0; } static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { const char *value = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, value, sizeof(*value)); } /* Battery Service Declaration */ static struct bt_gatt_attr attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS), BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ, read_blvl, NULL, &battery), BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed), }; void bas_init(void) { bt_gatt_register(attrs, ARRAY_SIZE(attrs)); } void bas_notify(void) { if (!simulate_blvl) {
return bt_gatt_attr_read(conn, attr, buf, len, offset, &value, sizeof(value)); } static ssize_t read_u32(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { const uint32_t *u32 = attr->user_data; uint32_t value = sys_cpu_to_le32(*u32); return bt_gatt_attr_read(conn, attr, buf, len, offset, &value, sizeof(value)); } static struct bt_gatt_attr attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_ESS), BT_GATT_CHARACTERISTIC(BT_UUID_TEMPERATURE, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_TEMPERATURE, BT_GATT_PERM_READ, read_u16, NULL, &temp_value), BT_GATT_CUD(TEMPERATURE_CUD, BT_GATT_PERM_READ), BT_GATT_CCC(temp_ccc_cfg, sensor_ccc_cfg_changed), BT_GATT_CHARACTERISTIC(BT_UUID_HUMIDITY, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY), BT_GATT_DESCRIPTOR(BT_UUID_HUMIDITY, BT_GATT_PERM_READ, read_u16, NULL, &humidity_value), BT_GATT_CUD(HUMIDITY_CUD, BT_GATT_PERM_READ), BT_GATT_CCC(humidity_ccc_cfg, sensor_ccc_cfg_changed),
return bt_gatt_attr_read(conn, attr, buf, len, offset, name, strlen(name)); } static ssize_t read_appearance(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { uint16_t appearance = sys_cpu_to_le16(CSC_APPEARANCE); return bt_gatt_attr_read(conn, attr, buf, len, offset, &appearance, sizeof(appearance)); } static struct bt_gatt_attr gap_attrs[] = { BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP), BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_GAP_DEVICE_NAME, BT_GATT_PERM_READ, read_device_name, NULL, DEVICE_NAME), BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ), BT_GATT_DESCRIPTOR(BT_UUID_GAP_APPEARANCE, BT_GATT_PERM_READ, read_appearance, NULL, NULL), }; /* Device Information Service declaration */ static ssize_t read_model(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { const char *value = attr->user_data;