diff --git a/example/Makefile.inc b/example/Makefile.inc index f25333d02..9c18e6579 100644 --- a/example/Makefile.inc +++ b/example/Makefile.inc @@ -3,6 +3,7 @@ VPATH += ${BTSTACK_ROOT}/src VPATH += ${BTSTACK_ROOT}/src/ble VPATH += ${BTSTACK_ROOT}/src/ble/gatt-service VPATH += ${BTSTACK_ROOT}/src/classic +VPATH += ${BTSTACK_ROOT}/src/mesh VPATH += ${BTSTACK_ROOT}/example VPATH += ${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/srce VPATH += ${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/srce @@ -20,11 +21,12 @@ VPATH += ${BTSTACK_ROOT}/3rd-party/micro-ecc VPATH += ${BTSTACK_ROOT}/3rd-party/yxml VPATH += ${BTSTACK_ROOT}/platform/lwip VPATH += ${BTSTACK_ROOT}/platform/lwip/port - +VPATH += ${BTSTACK_ROOT}/test/mesh CFLAGS += -I. CFLAGS += -I${BTSTACK_ROOT}/src/ble CFLAGS += -I${BTSTACK_ROOT}/src/classic +CFLAGS += -I${BTSTACK_ROOT}/src/mesh CFLAGS += -I${BTSTACK_ROOT}/src CFLAGS += -I${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/include CFLAGS += -I${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/include @@ -36,7 +38,7 @@ CFLAGS += -I${BTSTACK_ROOT}/3rd-party/micro-ecc CFLAGS += -I${BTSTACK_ROOT}/3rd-party/yxml CFLAGS += -I${BTSTACK_ROOT}/platform/lwip CFLAGS += -I${BTSTACK_ROOT}/platform/lwip/port - +CFLAGS += -I${BTSTACK_ROOT}/test/mesh # for CVSD/SBC PLC LDFLAGS += -lm @@ -132,6 +134,33 @@ HXCMOD_PLAYER = \ hxcmod.c \ nao-deceased_by_disease.c \ +MESH = \ + mesh.c \ + mesh_access.c \ + mesh_configuration_server.c \ + mesh_foundation.c \ + mesh_keys.c \ + mesh_virtual_addresses.c \ + mesh_peer.c \ + mesh_proxy.c \ + provisioning.c \ + provisioning_device.c \ + provisioning_provisioner.c \ + mesh_generic_server.c \ + mesh_iv_index_seq_number.c \ + adv_bearer.c \ + beacon.c \ + gatt_bearer.c \ + mesh_crypto.c \ + mesh_lower_transport.c \ + mesh_network.c \ + mesh_node.c \ + mesh_upper_transport.c \ + pb_adv.c \ + pb_gatt.c \ + mesh_provisioning_service_server.c \ + mesh_proxy_service_server.c \ + # List of General Examples without Bluetooth EXAMPLES_GENERAL = \ @@ -164,6 +193,8 @@ EXAMPLES_CLASSIC_ONLY = \ spp_counter \ spp_streamer \ spp_streamer_client \ + ublox_spp_le_counter \ + mesh_node_demo \ # List of Examples that only use Bluetooth LE @@ -232,6 +263,7 @@ CVSD_PLC_OBJ = $(CVSD_PLC:.c=.o) AVDTP_OBJ = $(AVDTP:.c=.o) HXCMOD_PLAYER_OBJ = $(HXCMOD_PLAYER:.c=.o) LWIP_OBJ = $(LWIP_SRC:.c=.o) +MESH_OBJ = $(MESH:.c=.o) default_target: all @@ -387,6 +419,9 @@ nordic_spp_le_streamer: nordic_spp_le_streamer.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT ublox_spp_le_counter: ublox_spp_le_counter.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} device_information_service_server.o ublox_spp_service_server.o ublox_spp_le_counter.c ${CC} $(filter-out ublox_spp_le_counter.h,$^) ${CFLAGS} ${LDFLAGS} -o $@ +mesh_node_demo: mesh_node_demo.h ${CORE_OBJ} ${COMMON_OBJ} ${MESH_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} ${SM_OBJ} mesh_node_demo.o + ${CC} $(filter-out mesh_node_demo.h,$^) ${CFLAGS} ${LDFLAGS} -o $@ + clean: rm -f ${EXAMPLES} ${EXAMPLES_GATT_H_FILES} rm -f *.o *.out *.hex *.exe *.wav *.sbc *.dSYM diff --git a/example/mesh_node_demo.c b/example/mesh_node_demo.c new file mode 100644 index 000000000..7e2c6105e --- /dev/null +++ b/example/mesh_node_demo.c @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2019 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +/** + * Basic Mesh Node demo + */ + +#define __BTSTACK_FILE__ "mesh_node_demo.c" + +#include +#include +#include +#include + +#include "btstack.h" +#include "mesh_node_demo.h" + +// general +#define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER 0x0000u + +static mesh_model_t mesh_vendor_model; + +static mesh_model_t mesh_generic_on_off_server_model; +static mesh_generic_on_off_state_t mesh_generic_on_off_state; + + +static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ + UNUSED(packet_type); + UNUSED(channel); + UNUSED(size); + + if (packet_type != HCI_EVENT_PACKET) return; + + switch(packet[0]){ + case HCI_EVENT_MESH_META: + switch(packet[2]){ + case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN: + printf("Provisioner link opened"); + break; + case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED: + printf("Provisioner link close"); + break; + case MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER: + printf("Attention Timer: %u\n", packet[3]); + break; + case MESH_SUBEVENT_PB_PROV_COMPLETE: + printf("Provisioning complete\n"); + break; + default: + break; + } + break; + default: + break; + } +} + +static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ + UNUSED(channel); + UNUSED(size); + + if (packet_type != HCI_EVENT_PACKET) return; + + switch(packet[0]){ + case HCI_EVENT_MESH_META: + switch(packet[2]){ + case MESH_SUBEVENT_STATE_UPDATE_BOOL: + printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n", + mesh_subevent_state_update_bool_get_model_identifier(packet), + mesh_subevent_state_update_bool_get_state_identifier(packet), + mesh_subevent_state_update_bool_get_reason(packet), + mesh_subevent_state_update_bool_get_value(packet)); + break; + default: + break; + } + break; + default: + break; + } +} + +static void show_usage(void){ + bd_addr_t iut_address; + gap_local_bd_addr(iut_address); + printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address)); + printf("8 - Delete provisioning data\n"); + printf("g - Generic ON/OFF Server Toggle Value\n"); + printf("\n"); +} + +static void stdin_process(char cmd){ + switch (cmd){ + case '8': + mesh_node_reset(); + printf("Mesh Node Reset!\n"); + mesh_proxy_start_advertising_unprovisioned_device(); + break; + case 'g': + printf("Generic ON/OFF Server Toggle Value\n"); + mesh_generic_on_off_server_set(&mesh_generic_on_off_server_model, 1-mesh_generic_on_off_server_get(&mesh_generic_on_off_server_model), 0, 0); + break; + case ' ': + show_usage(); + break; + default: + printf("Command: '%c' not implemented\n", cmd); + show_usage(); + break; + } +} + +int btstack_main(void); +int btstack_main(void) +{ + // console + btstack_stdin_setup(stdin_process); + + // crypto + btstack_crypto_init(); + + // l2cap + l2cap_init(); + + // setup le device db + le_device_db_init(); + + // setup ATT server + att_server_init(profile_data, NULL, NULL); + + // + sm_init(); + + + // mesh + mesh_init(); + + // setup connectable advertisments + bd_addr_t null_addr; + memset(null_addr, 0, 6); + uint8_t adv_type = 0; // AFV_IND + uint16_t adv_int_min = 0x0030; + uint16_t adv_int_max = 0x0030; + adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); + + // Track Provisioning as device role + mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler); + + // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors + mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103); + + // Setup Generic On/Off model + mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER); + mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations(); + mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state; + mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler); + mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model); + + // Setup our custom model + mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER); + mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model); + + // Enable Output OOB + provisioning_device_set_output_oob_actions(0x08, 0x08); + + // Enable PROXY + mesh_foundation_gatt_proxy_set(1); + +#if defined(ENABLE_MESH_ADV_BEARER) + // setup scanning when supporting ADV Bearer + gap_set_scan_parameters(0, 0x300, 0x300); + gap_start_scan(); +#endif + + // turn on! + hci_power_control(HCI_POWER_ON); + + return 0; +} +/* EXAMPLE_END */ diff --git a/example/mesh_node_demo.gatt b/example/mesh_node_demo.gatt new file mode 100644 index 000000000..efc5b484f --- /dev/null +++ b/example/mesh_node_demo.gatt @@ -0,0 +1,5 @@ +PRIMARY_SERVICE, GAP_SERVICE +CHARACTERISTIC, GAP_DEVICE_NAME, READ, "Mesh Provisioning Test" + +#import +#import diff --git a/port/libusb/btstack_config.h b/port/libusb/btstack_config.h index 66d77ad64..9d916bb75 100644 --- a/port/libusb/btstack_config.h +++ b/port/libusb/btstack_config.h @@ -29,8 +29,27 @@ #define ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE +// Mesh Config +#define ENABLE_MESH_ADV_BEARER +#define ENABLE_MESH_GATT_BEARER +// #define ENABLE_MESH_PB_ADV +#define ENABLE_MESH_PB_GATT +#define ENABLE_MESH_PROXY_SERVER + +#define ENABLE_MESH +#define ENABLE_MESH_PROVISIONER + // BTstack configuration. buffers, sizes, ... #define HCI_ACL_PAYLOAD_SIZE (1691 + 4) #define HCI_INCOMING_PRE_BUFFER_SIZE 14 // sizeof BNEP header, avoid memcpy +#define MAX_NR_LE_DEVICE_DB_ENTRIES 4 +#define MAX_NR_MESH_TRANSPORT_KEYS 16 +#define MAX_NR_MESH_VIRTUAL_ADDRESSES 16 +#define MAX_NR_MESH_SUBNETS 2 + +// allow for one NetKey update +#define MAX_NR_MESH_NETWORK_KEYS (MAX_NR_MESH_SUBNETS+1) + #endif +