mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-26 03:35:20 +00:00
mesh: start pb gatt
This commit is contained in:
parent
d3a9936bfc
commit
119c1fd49c
@ -37,7 +37,6 @@
|
||||
|
||||
#define __BTSTACK_FILE__ "mesh_provisioning_service_server.c"
|
||||
|
||||
|
||||
#include "bluetooth.h"
|
||||
#include "btstack_defines.h"
|
||||
#include "ble/att_db.h"
|
||||
@ -46,6 +45,8 @@
|
||||
#include "bluetooth_gatt.h"
|
||||
#include "btstack_debug.h"
|
||||
#include "l2cap.h"
|
||||
#include "hci.h"
|
||||
#include "btstack_event.h"
|
||||
|
||||
#include "ble/gatt-service/mesh_provisioning_service_server.h"
|
||||
#include "provisioning.h"
|
||||
@ -65,8 +66,11 @@ typedef struct {
|
||||
uint16_t data_out_client_configuration_descriptor_handle;
|
||||
uint16_t data_out_client_configuration_descriptor_value;
|
||||
btstack_context_callback_registration_t data_out_notify_callback;
|
||||
|
||||
btstack_context_callback_registration_t pdu_response_callback;
|
||||
} mesh_provisioning_t;
|
||||
|
||||
static btstack_packet_handler_t mesh_provisioning_service_packet_handler;
|
||||
static att_service_handler_t mesh_provisioning_service;
|
||||
static mesh_provisioning_t mesh_provisioning;
|
||||
|
||||
@ -77,6 +81,22 @@ static mesh_provisioning_t * mesh_provisioning_service_get_instance_for_con_hand
|
||||
return instance;
|
||||
}
|
||||
|
||||
static hci_con_handle_t get_con_handle(void){
|
||||
return mesh_provisioning.con_handle;
|
||||
}
|
||||
|
||||
static void pb_adv_emit_link_open(uint8_t status, uint16_t pb_adv_cid){
|
||||
uint8_t event[6] = { HCI_EVENT_MESH_META, 6, MESH_PB_ADV_LINK_OPEN, status};
|
||||
little_endian_store_16(event, 4, pb_adv_cid);
|
||||
mesh_provisioning_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
||||
}
|
||||
|
||||
static void pb_adv_emit_link_close(uint16_t pb_adv_cid, uint8_t reason){
|
||||
uint8_t event[5] = { HCI_EVENT_MESH_META, 6, MESH_PB_ADV_LINK_CLOSED};
|
||||
little_endian_store_16(event, 4, pb_adv_cid);
|
||||
mesh_provisioning_service_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
||||
}
|
||||
|
||||
static uint16_t mesh_provisioning_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
|
||||
UNUSED(con_handle);
|
||||
UNUSED(attribute_handle);
|
||||
@ -104,7 +124,9 @@ static int mesh_provisioning_service_write_callback(hci_con_handle_t con_handle,
|
||||
}
|
||||
|
||||
if (attribute_handle == instance->data_in_client_value_handle){
|
||||
printf("mesh_provisioning_service_write_callback, handle write on 0x%02x\n", attribute_handle);
|
||||
printf("mesh_provisioning_service_write_callback, handle write on 0x%02x, len %u\n", attribute_handle, buffer_size);
|
||||
if (!mesh_provisioning_service_packet_handler) return 0;
|
||||
(*mesh_provisioning_service_packet_handler)(PROVISIONING_DATA_PACKET, 0, buffer, buffer_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -114,6 +136,11 @@ static int mesh_provisioning_service_write_callback(hci_con_handle_t con_handle,
|
||||
}
|
||||
instance->data_out_client_configuration_descriptor_value = little_endian_read_16(buffer, 0);
|
||||
printf("mesh_provisioning_service_write_callback: data out notify enabled %d\n", instance->data_out_client_configuration_descriptor_value);
|
||||
if (instance->data_out_client_configuration_descriptor_value){
|
||||
pb_adv_emit_link_close(con_handle, 0);
|
||||
} else {
|
||||
pb_adv_emit_link_open(0, con_handle);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
printf("mesh_provisioning_service_write_callback, not handeled write on handle 0x%02x, buffer size %d\n", attribute_handle, buffer_size);
|
||||
@ -150,28 +177,177 @@ void mesh_provisioning_service_server_init(void){
|
||||
att_server_register_service_handler(&mesh_provisioning_service);
|
||||
}
|
||||
|
||||
static void mesh_provisioning_service_server_data_out_can_send_now(void * context){
|
||||
mesh_provisioning_t * instance = (mesh_provisioning_t *) instance->data_out_notify_callback.context;
|
||||
void mesh_provisioning_service_server_send_proxy_pdu(uint16_t con_handle, const uint8_t * proxy_pdu, uint16_t proxy_pdu_size){
|
||||
mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
|
||||
if (!instance){
|
||||
log_error("mesh_provisioning_service_server_data_out_can_send_now: instance is null");
|
||||
return;
|
||||
}
|
||||
att_server_notify(instance->con_handle, instance->data_out_client_value_handle, &instance->data_out_proxy_pdu[0], instance->data_out_proxy_pdu_size);
|
||||
att_server_notify(instance->con_handle, instance->data_out_client_value_handle, proxy_pdu, proxy_pdu_size);
|
||||
}
|
||||
|
||||
void mesh_provisioning_service_server_send_proxy_pdu(uint16_t con_handle, const uint8_t * proxy_pdu, uint16_t proxy_pdu_size){
|
||||
static void mesh_provisioning_service_can_send_now(void * context){
|
||||
hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
|
||||
// notify client
|
||||
mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
|
||||
if (!instance){
|
||||
log_error("mesh_provisioning_service_server_send_proxy_pdu: instance is null");
|
||||
log_error("no instance for handle 0x%02x", con_handle);
|
||||
return;
|
||||
}
|
||||
if (proxy_pdu_size && proxy_pdu_size > MESH_PROV_MAX_PROXY_PDU) return;
|
||||
|
||||
if (instance->data_out_client_configuration_descriptor_value){
|
||||
memcpy(instance->data_out_proxy_pdu, proxy_pdu, proxy_pdu_size);
|
||||
instance->data_out_proxy_pdu_size = proxy_pdu_size;
|
||||
instance->data_out_notify_callback.callback = &mesh_provisioning_service_server_data_out_can_send_now;
|
||||
instance->data_out_notify_callback.context = (void*) instance;
|
||||
att_server_register_can_send_now_callback(&instance->data_out_notify_callback, instance->con_handle);
|
||||
if (!mesh_provisioning_service_packet_handler) return;
|
||||
uint8_t buffer[5];
|
||||
buffer[0] = HCI_EVENT_MESH_META;
|
||||
buffer[1] = 3;
|
||||
buffer[2] = MESH_SUBEVENT_CAN_SEND_NOW;
|
||||
little_endian_store_16(buffer, 3, (uint16_t) con_handle);
|
||||
(*mesh_provisioning_service_packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
void mesh_provisioning_service_server_request_can_send_now(hci_con_handle_t con_handle){
|
||||
printf("mesh_provisioning_service_server_request_can_send_now, con handle 0x%02x\n", con_handle);
|
||||
|
||||
mesh_provisioning_t * instance = mesh_provisioning_service_get_instance_for_con_handle(con_handle);
|
||||
if (!instance){
|
||||
printf("mesh_provisioning_service_server_request_can_send_now: instance is null, 0x%2x\n", con_handle);
|
||||
return;
|
||||
}
|
||||
|
||||
instance->pdu_response_callback.callback = &mesh_provisioning_service_can_send_now;
|
||||
instance->pdu_response_callback.context = (void*) (uintptr_t) con_handle;
|
||||
att_server_register_can_send_now_callback(&instance->pdu_response_callback, con_handle);
|
||||
}
|
||||
|
||||
void mesh_provisioning_service_server_register_packet_handler(btstack_packet_handler_t callback){
|
||||
mesh_provisioning_service_packet_handler = callback;
|
||||
}
|
||||
|
||||
/************** PB ADV Impl for GATT Mesh Provisioning ********************/
|
||||
typedef enum {
|
||||
MESH_MSG_SAR_FIELD_COMPLETE_MSG = 0,
|
||||
MESH_MSG_SAR_FIELD_FIRST_SEGMENT,
|
||||
MESH_MSG_SAR_FIELD_CONTINUE,
|
||||
MESH_MSG_SAR_FIELD_LAST_SEGMENT
|
||||
} mesh_msg_sar_field_t; // Message segmentation and reassembly information
|
||||
|
||||
typedef enum {
|
||||
MESH_MSG_TYPE_NETWORK_PDU = 0,
|
||||
MESH_MSG_TYPE_BEACON,
|
||||
MESH_MSG_TYPE_PROXY_CONFIGURATION,
|
||||
MESH_MSG_TYPE_PROVISIONING_PDU
|
||||
} mesh_msg_type_t;
|
||||
|
||||
static const uint8_t * pb_adv_own_device_uuid;
|
||||
static btstack_packet_handler_t pb_adv_packet_handler;
|
||||
static const uint8_t * proxy_pdu;
|
||||
static uint16_t proxy_pdu_size;
|
||||
static uint16_t con_handle;
|
||||
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
|
||||
/**
|
||||
* Initialize Provisioning Bearer using Advertisement Bearer
|
||||
* @param DeviceUUID
|
||||
*/
|
||||
void pb_adv_init(const uint8_t * device_uuid){
|
||||
pb_adv_own_device_uuid = device_uuid;
|
||||
// setup mesh provisioning service
|
||||
mesh_provisioning_service_server_init();
|
||||
mesh_provisioning_service_server_register_packet_handler(packet_handler);
|
||||
}
|
||||
|
||||
static void pb_adv_emit_pdu_sent(uint8_t status){
|
||||
uint8_t event[] = {HCI_EVENT_MESH_META, 2, MESH_PB_ADV_PDU_SENT, status};
|
||||
pb_adv_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register listener for Provisioning PDUs and MESH_PBV_ADV_SEND_COMPLETE
|
||||
*/
|
||||
void pb_adv_register_packet_handler(btstack_packet_handler_t _packet_handler){
|
||||
pb_adv_packet_handler = _packet_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send Provisioning PDU
|
||||
*/
|
||||
static uint8_t buffer[100];
|
||||
|
||||
void pb_adv_send_pdu(const uint8_t * pdu, uint16_t size){
|
||||
if (!pdu || size <= 0) return;
|
||||
// store pdu, request to send
|
||||
printf("pb_adv_send_pdu: ");
|
||||
printf_hexdump(pdu, size);
|
||||
proxy_pdu = pdu;
|
||||
proxy_pdu_size = size;
|
||||
mesh_provisioning_service_server_request_can_send_now(get_con_handle());
|
||||
}
|
||||
|
||||
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
mesh_msg_sar_field_t msg_sar_field;
|
||||
mesh_msg_type_t msg_type;
|
||||
|
||||
switch (packet_type) {
|
||||
case PROVISIONING_DATA_PACKET:
|
||||
printf("PROVISIONING_DATA_PACKET, len %u\n", size);
|
||||
|
||||
// on provisioning PDU call packet handler with PROVISIONG_DATA type
|
||||
msg_sar_field = packet[0] >> 6;
|
||||
msg_type = packet[0] & 0x3F;
|
||||
printf("received PROVISIONING_DATA_PACKET, segmentation %d, type %d\n", msg_sar_field, msg_type);
|
||||
if (msg_sar_field != MESH_MSG_SAR_FIELD_COMPLETE_MSG) {
|
||||
printf("segmntation not yet implemented\n");
|
||||
return;
|
||||
}
|
||||
if (msg_type != MESH_MSG_TYPE_PROVISIONING_PDU) return;
|
||||
if (!pb_adv_packet_handler) return;
|
||||
// send to provisioning device
|
||||
pb_adv_packet_handler(PROVISIONING_DATA_PACKET, 0, packet+1, size-1);
|
||||
break;
|
||||
|
||||
case HCI_EVENT_PACKET:
|
||||
switch (hci_event_packet_get_type(packet)) {
|
||||
case HCI_EVENT_MESH_META:
|
||||
switch (hci_event_mesh_meta_get_subevent_code(packet)){
|
||||
case MESH_PB_ADV_LINK_OPEN:
|
||||
case MESH_PB_ADV_LINK_CLOSED:
|
||||
printf("Forward link open/close\n");
|
||||
pb_adv_packet_handler(HCI_EVENT_PACKET, 0, packet, size);
|
||||
break;
|
||||
case MESH_SUBEVENT_CAN_SEND_NOW:
|
||||
con_handle = little_endian_read_16(packet, 3);
|
||||
if (con_handle == HCI_CON_HANDLE_INVALID) return;
|
||||
buffer[0] = (MESH_MSG_SAR_FIELD_COMPLETE_MSG << 6) | MESH_MSG_TYPE_PROVISIONING_PDU;
|
||||
memcpy(&buffer[1], proxy_pdu, proxy_pdu_size);
|
||||
mesh_provisioning_service_server_send_proxy_pdu(con_handle, buffer, proxy_pdu_size+1);
|
||||
// TODO: emit MESH_PBV_ADV_SEND_COMPLETE
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Close Link
|
||||
* @param pb_adv_cid
|
||||
* @param reason 0 = success, 1 = timeout, 2 = fail
|
||||
*/
|
||||
void pb_adv_close_link(uint16_t pb_adv_cid, uint8_t reason){
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup Link with unprovisioned device
|
||||
* @param DeviceUUID
|
||||
* @returns pb_adv_cid or 0
|
||||
*/
|
||||
uint16_t pb_adv_create_link(const uint8_t * device_uuid){
|
||||
return 0;
|
||||
}
|
@ -38,6 +38,7 @@
|
||||
#define __MESH_PROVISIONING_SERVICE_SERVER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hci.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
@ -81,6 +82,18 @@ void mesh_provisioning_service_server_init(void);
|
||||
*/
|
||||
void mesh_provisioning_service_server_send_proxy_pdu(uint16_t con_handle, const uint8_t * proxy_pdu, uint16_t proxy_pdu_size);
|
||||
|
||||
/**
|
||||
* @brief Register callback for the PB-GATT.
|
||||
* @param callback
|
||||
*/
|
||||
void mesh_provisioning_service_server_register_packet_handler(btstack_packet_handler_t callback);
|
||||
|
||||
/**
|
||||
* @brief Request can send now event to send PDU
|
||||
* Generates an MESH_SUBEVENT_CAN_SEND_NOW subevent
|
||||
* @param con_handle
|
||||
*/
|
||||
void mesh_provisioning_service_server_request_can_send_now_event(hci_con_handle_t con_handle);
|
||||
/* API_END */
|
||||
|
||||
#if defined __cplusplus
|
||||
|
@ -13,7 +13,6 @@ CORE += \
|
||||
btstack_tlv_posix.c \
|
||||
le_device_db_fs.c \
|
||||
l2cap.c \
|
||||
pb_adv.c \
|
||||
uECC.c \
|
||||
rijndael.c \
|
||||
main.c \
|
||||
@ -43,10 +42,10 @@ CC_UNIT = g++
|
||||
CFLAGS += $(shell pkg-config libusb-1.0 --cflags)
|
||||
LDFLAGS += $(shell pkg-config libusb-1.0 --libs)
|
||||
|
||||
mesh: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} mesh_crypto.o provisioning_device.o mesh_network.o mesh.o
|
||||
mesh: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} pb_adv.o mesh_crypto.o provisioning_device.o mesh_network.o mesh.o
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
provisioner: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} mesh_crypto.o provisioning_provisioner.o provisioner.o
|
||||
provisioner: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} pb_adv.o mesh_crypto.o provisioning_provisioner.o provisioner.o
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
sniffer: ${CORE_OBJ} ${COMMON_OBJ} ${SM_OBJ} sniffer.c
|
||||
@ -61,7 +60,7 @@ provisioning_provisioner_test: provisioning_provisioner_test.cpp uECC.o mesh_cry
|
||||
mesh_provisioning_device.h: mesh_provisioning_device.gatt
|
||||
python ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@
|
||||
|
||||
mesh_provisioning_device: mesh_provisioning_device.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} ${SM_OBJ} mesh_provisioning_service_server.o mesh_provisioning_device.o
|
||||
mesh_provisioning_device: mesh_provisioning_device.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} ${SM_OBJ} mesh_crypto.o provisioning_device.o mesh_provisioning_service_server.o mesh_provisioning_device.o
|
||||
${CC} $(filter-out mesh_provisioning_device.h,$^) ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
EXAMPLES = mesh provisioner sniffer provisioning_device_test provisioning_provisioner_test mesh_provisioning_device
|
||||
|
@ -46,6 +46,9 @@
|
||||
#include "mesh_provisioning_device.h"
|
||||
#include "btstack_config.h"
|
||||
#include "btstack.h"
|
||||
#include "provisioning.h"
|
||||
#include "provisioning_device.h"
|
||||
#include "ble/mesh/pb_adv.h"
|
||||
|
||||
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml
|
||||
// cycling / cycling power sensor
|
||||
@ -57,7 +60,7 @@ static uint16_t adv_int_max = 0x0030;
|
||||
|
||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
|
||||
const uint8_t adv_data[] = {
|
||||
static uint8_t adv_data[] = {
|
||||
// Flags general discoverable, BR/EDR not supported
|
||||
0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
|
||||
// 16-bit Service UUIDs
|
||||
@ -114,18 +117,13 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case HCI_EVENT_GATTSERVICE_META:
|
||||
switch (packet[2]){
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const static uint8_t device_uuid[] = { 0x00, 0x1B, 0xDC, 0x08, 0x10, 0x21, 0x0B, 0x0E, 0x0A, 0x0C, 0x00, 0x0B, 0x0E, 0x0A, 0x0C, 0x00 };
|
||||
|
||||
int btstack_main(void);
|
||||
int btstack_main(void){
|
||||
hci_event_callback_registration.callback = &packet_handler;
|
||||
@ -142,8 +140,15 @@ int btstack_main(void){
|
||||
// setup ATT server
|
||||
att_server_init(profile_data, NULL, NULL);
|
||||
|
||||
// setup mesh provisioning service
|
||||
mesh_provisioning_service_server_init();
|
||||
// Provisioning in device role
|
||||
provisioning_device_init(device_uuid);
|
||||
provisioning_device_register_packet_handler(&packet_handler);
|
||||
|
||||
// dynamically store device uuid into adv data - probably offset 7 - (see btstack_util to flip)
|
||||
uint8_t uuid_flipped[16];
|
||||
reverse_128(device_uuid, uuid_flipped);
|
||||
memcpy(&adv_data[11], uuid_flipped, 16);
|
||||
|
||||
// setup advertisements
|
||||
uint8_t adv_type = 0; // AFV_IND
|
||||
bd_addr_t null_addr;
|
||||
|
Loading…
x
Reference in New Issue
Block a user