2018-10-05 15:13:34 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define __BTSTACK_FILE__ "mesh.c"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-06-26 11:42:25 +02:00
|
|
|
#include "mesh/adv_bearer.h"
|
|
|
|
#include "mesh/gatt_bearer.h"
|
|
|
|
#include "mesh/beacon.h"
|
|
|
|
#include "mesh/mesh_crypto.h"
|
|
|
|
#include "mesh/mesh_lower_transport.h"
|
|
|
|
#include "mesh/mesh_upper_transport.h"
|
|
|
|
#include "mesh/pb_adv.h"
|
|
|
|
#include "mesh/pb_gatt.h"
|
2018-11-20 11:22:40 +01:00
|
|
|
#include "ble/gatt-service/mesh_provisioning_service_server.h"
|
2018-10-05 15:13:34 +02:00
|
|
|
#include "provisioning.h"
|
|
|
|
#include "provisioning_device.h"
|
2019-04-22 18:18:40 +02:00
|
|
|
#include "mesh_foundation.h"
|
2019-07-10 15:58:51 +02:00
|
|
|
#include "mesh_iv_index_seq_number.h"
|
2019-06-10 22:29:44 +02:00
|
|
|
#include "mesh_configuration_server.h"
|
2019-06-12 11:46:36 +02:00
|
|
|
#include "mesh_generic_server.h"
|
2019-06-10 20:05:39 +02:00
|
|
|
#include "mesh_access.h"
|
2019-06-07 12:10:09 +02:00
|
|
|
#include "mesh_virtual_addresses.h"
|
2019-06-25 17:43:44 +02:00
|
|
|
#include "mesh_peer.h"
|
2019-06-14 12:16:16 +02:00
|
|
|
#include "mesh_proxy.h"
|
2019-06-25 12:23:47 +02:00
|
|
|
#include "mesh_generic_model.h"
|
2019-05-08 15:54:18 +02:00
|
|
|
#include "mesh.h"
|
2018-10-05 15:13:34 +02:00
|
|
|
#include "btstack.h"
|
|
|
|
#include "btstack_tlv.h"
|
|
|
|
|
|
|
|
#define BEACON_TYPE_SECURE_NETWORK 1
|
2019-04-16 17:05:14 +02:00
|
|
|
#define PTS_DEFAULT_TTL 10
|
2018-10-05 15:13:34 +02:00
|
|
|
|
2019-05-08 17:38:18 +02:00
|
|
|
static void show_usage(void);
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
const static uint8_t device_uuid[] = { 0x00, 0x1B, 0xDC, 0x08, 0x10, 0x21, 0x0B, 0x0E, 0x0A, 0x0C, 0x00, 0x0B, 0x0E, 0x0A, 0x0C, 0x00 };
|
|
|
|
|
2019-05-08 16:23:08 +02:00
|
|
|
// Mesh Provisioning
|
2019-06-03 15:37:51 +02:00
|
|
|
static uint8_t adv_data_unprovisioned[] = {
|
2019-05-08 16:23:08 +02:00
|
|
|
// Flags general discoverable, BR/EDR not supported
|
|
|
|
0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
|
|
|
|
// 16-bit Service UUIDs
|
|
|
|
0x03, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, ORG_BLUETOOTH_SERVICE_MESH_PROVISIONING & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROVISIONING >> 8,
|
|
|
|
// Service Data (22)
|
|
|
|
0x15, BLUETOOTH_DATA_TYPE_SERVICE_DATA, ORG_BLUETOOTH_SERVICE_MESH_PROVISIONING & 0xff, ORG_BLUETOOTH_SERVICE_MESH_PROVISIONING >> 8,
|
|
|
|
// UUID
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
// OOB information
|
|
|
|
0x00, 0x00
|
|
|
|
};
|
2019-06-03 15:37:51 +02:00
|
|
|
const uint8_t adv_data_unprovisioned_len = sizeof(adv_data_unprovisioned);
|
2019-05-08 16:23:08 +02:00
|
|
|
|
2019-06-04 11:44:15 +02:00
|
|
|
// Mesh Proxy, advertise with node id
|
2019-06-03 15:37:51 +02:00
|
|
|
static adv_bearer_connectable_advertisement_data_item_t connectable_advertisement_item;
|
2019-05-08 16:23:08 +02:00
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
|
|
|
|
|
|
|
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
|
|
|
|
|
|
|
static uint8_t mesh_flags;
|
|
|
|
|
2018-11-16 09:36:34 +01:00
|
|
|
static uint16_t pb_transport_cid = MESH_PB_TRANSPORT_INVALID_CID;
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
// pin entry
|
|
|
|
static int ui_chars_for_pin;
|
|
|
|
static uint8_t ui_pin[17];
|
|
|
|
static int ui_pin_offset;
|
|
|
|
|
|
|
|
static const btstack_tlv_t * btstack_tlv_singleton_impl;
|
|
|
|
static void * btstack_tlv_singleton_context;
|
|
|
|
|
2018-11-13 15:05:16 +01:00
|
|
|
static uint16_t primary_element_address;
|
2019-05-22 15:32:12 +02:00
|
|
|
|
|
|
|
static int provisioned;
|
2018-11-02 18:04:05 +01:00
|
|
|
|
2019-06-14 11:57:46 +02:00
|
|
|
// Test configuration
|
|
|
|
|
|
|
|
#define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER 0x0000u
|
|
|
|
|
2019-06-17 14:14:49 +02:00
|
|
|
static mesh_configuration_server_model_context_t mesh_configuration_server_model_context;
|
2019-06-17 11:49:41 +02:00
|
|
|
|
2019-06-14 11:57:46 +02:00
|
|
|
static mesh_model_t mesh_configuration_server_model;
|
|
|
|
static mesh_model_t mesh_health_server_model;
|
|
|
|
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;
|
|
|
|
|
2018-11-16 17:25:30 +01:00
|
|
|
// static void mesh_print_x(const char * name, uint32_t value){
|
|
|
|
// printf("%20s: 0x%x", name, (int) value);
|
|
|
|
// }
|
|
|
|
|
2019-07-02 16:23:31 +02:00
|
|
|
static void mesh_network_key_dump(const mesh_network_key_t * key){
|
|
|
|
printf("NetKey: "); printf_hexdump(key->net_key, 16);
|
|
|
|
printf("-- Derived from NetKey --\n");
|
|
|
|
printf("NID: 0x%02x\n", key->nid);
|
|
|
|
printf("NetworkID: "); printf_hexdump(key->network_id, 8);
|
|
|
|
printf("BeaconKey: "); printf_hexdump(key->beacon_key, 16);
|
|
|
|
printf("EncryptionKey: "); printf_hexdump(key->encryption_key, 16);
|
|
|
|
printf("PrivacyKey: "); printf_hexdump(key->privacy_key, 16);
|
|
|
|
printf("IdentityKey: "); printf_hexdump(key->identity_key, 16);
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:46:14 +01:00
|
|
|
static void mesh_provisioning_dump(const mesh_provisioning_data_t * data){
|
2018-11-05 16:58:54 +01:00
|
|
|
printf("UnicastAddr: 0x%02x\n", data->unicast_address);
|
2019-04-24 11:22:35 +02:00
|
|
|
printf("DevKey: "); printf_hexdump(data->device_key, 16);
|
2019-07-02 16:23:31 +02:00
|
|
|
printf("Flags: 0x%02x\n", data->flags);
|
2019-06-03 16:28:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:46:14 +01:00
|
|
|
static void mesh_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){
|
2019-05-22 15:32:12 +02:00
|
|
|
provisioned = 1;
|
|
|
|
|
2018-11-06 17:46:14 +01:00
|
|
|
// set unicast address
|
|
|
|
mesh_network_set_primary_element_address(provisioning_data->unicast_address);
|
2019-04-09 17:51:25 +02:00
|
|
|
mesh_lower_transport_set_primary_element_address(provisioning_data->unicast_address);
|
2018-12-04 17:39:48 +01:00
|
|
|
mesh_upper_transport_set_primary_element_address(provisioning_data->unicast_address);
|
2019-06-06 14:36:08 +02:00
|
|
|
mesh_access_set_primary_element_address(provisioning_data->unicast_address);
|
2018-11-13 15:05:16 +01:00
|
|
|
primary_element_address = provisioning_data->unicast_address;
|
2018-11-07 18:07:09 +01:00
|
|
|
// set device_key
|
|
|
|
mesh_transport_set_device_key(provisioning_data->device_key);
|
2019-07-02 16:23:31 +02:00
|
|
|
|
2018-11-06 17:46:14 +01:00
|
|
|
// for secure beacon
|
|
|
|
mesh_flags = provisioning_data->flags;
|
2019-07-02 16:23:31 +02:00
|
|
|
|
2018-11-06 17:46:14 +01:00
|
|
|
// dump data
|
|
|
|
mesh_provisioning_dump(provisioning_data);
|
2019-06-03 15:37:51 +02:00
|
|
|
|
2019-05-08 17:38:18 +02:00
|
|
|
// Mesh Proxy
|
2019-06-03 17:06:43 +02:00
|
|
|
#ifdef ENABLE_MESH_PROXY_SERVER
|
2019-06-14 12:16:16 +02:00
|
|
|
// Setup Proxy
|
2019-06-14 15:43:15 +02:00
|
|
|
mesh_proxy_init(provisioning_data->unicast_address);
|
2019-06-14 12:16:16 +02:00
|
|
|
|
2019-05-08 17:38:18 +02:00
|
|
|
printf("Advertise Mesh Proxy Service with Network ID\n");
|
2019-06-03 17:06:43 +02:00
|
|
|
mesh_proxy_start_advertising_with_network_id();
|
2019-05-08 17:38:18 +02:00
|
|
|
#endif
|
2018-11-06 17:46:14 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 18:31:29 +02:00
|
|
|
#ifdef ENABLE_MESH_PB_GATT
|
2019-05-22 15:32:12 +02:00
|
|
|
static void setup_advertising_unprovisioned(void) {
|
2019-05-08 16:23:08 +02:00
|
|
|
printf("Advertise Mesh Provisioning Service\n");
|
2019-05-22 15:32:12 +02:00
|
|
|
// dynamically store device uuid into adv data
|
2019-06-03 15:37:51 +02:00
|
|
|
memcpy(&adv_data_unprovisioned[11], device_uuid, sizeof(device_uuid));
|
|
|
|
// store in advertisement item
|
|
|
|
memset(&connectable_advertisement_item, 0, sizeof(connectable_advertisement_item));
|
|
|
|
connectable_advertisement_item.adv_length = adv_data_unprovisioned_len;
|
|
|
|
memcpy(connectable_advertisement_item.adv_data, (uint8_t*) adv_data_unprovisioned, adv_data_unprovisioned_len);
|
2019-05-08 16:23:08 +02:00
|
|
|
|
|
|
|
// setup advertisements
|
2019-06-03 15:37:51 +02:00
|
|
|
adv_bearer_advertisements_add_item(&connectable_advertisement_item);
|
2019-05-08 16:23:08 +02:00
|
|
|
adv_bearer_advertisements_enable(1);
|
2019-05-22 15:32:12 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static void mesh_setup_without_provisiong_data(void){
|
|
|
|
provisioned = 0;
|
|
|
|
|
|
|
|
#ifdef ENABLE_MESH_PB_ADV
|
|
|
|
// PB-ADV
|
|
|
|
printf("Starting Unprovisioned Device Beacon\n");
|
|
|
|
beacon_unprovisioned_device_start(device_uuid, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_MESH_PB_GATT
|
|
|
|
// PB_GATT
|
|
|
|
setup_advertising_unprovisioned();
|
2019-05-08 18:31:29 +02:00
|
|
|
#endif
|
2019-05-08 15:54:18 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 18:01:53 +01:00
|
|
|
// helper network layer, temp
|
|
|
|
static uint8_t mesh_network_send(uint16_t netkey_index, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dest, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len){
|
|
|
|
|
|
|
|
// "3.4.5.2: The output filter of the interface connected to advertising or GATT bearers shall drop all messages with TTL value set to 1."
|
|
|
|
// if (ttl <= 1) return 0;
|
|
|
|
|
|
|
|
// TODO: check transport_pdu_len depending on ctl
|
|
|
|
|
|
|
|
// lookup network by netkey_index
|
|
|
|
const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
|
|
|
|
if (!network_key) return 0;
|
|
|
|
|
|
|
|
// allocate network_pdu
|
2019-04-04 10:24:19 +02:00
|
|
|
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
|
2018-11-28 18:01:53 +01:00
|
|
|
if (!network_pdu) return 0;
|
|
|
|
|
|
|
|
// setup network_pdu
|
|
|
|
mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, ctl, ttl, seq, src, dest, transport_pdu_data, transport_pdu_len);
|
|
|
|
|
|
|
|
// send network_pdu
|
2019-04-22 18:32:26 +02:00
|
|
|
mesh_lower_transport_send_pdu((mesh_pdu_t *) network_pdu);
|
2018-11-28 18:01:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-12 17:43:59 +02:00
|
|
|
static void printf_hex(const uint8_t * data, uint16_t len){
|
|
|
|
while (len){
|
|
|
|
printf("%02x", *data);
|
|
|
|
data++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mesh_pts_dump_mesh_options(void){
|
|
|
|
printf("\nMeshOptions.ini\n");
|
|
|
|
|
|
|
|
printf("[mesh]\n");
|
|
|
|
|
|
|
|
printf("{IVindex}\n");
|
|
|
|
printf("%08x\n", mesh_get_iv_index());
|
|
|
|
|
|
|
|
mesh_network_key_t * network_key = mesh_network_key_list_get(0);
|
|
|
|
if (network_key){
|
|
|
|
printf("{NetKey}\n");
|
|
|
|
printf_hex(network_key->net_key, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh_transport_key_t * transport_key = mesh_transport_key_get(0);
|
|
|
|
if (transport_key){
|
|
|
|
printf("{AppKey}\n");
|
|
|
|
printf_hex(transport_key->key, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh_transport_key_t * device_key = mesh_transport_key_get(MESH_DEVICE_KEY_INDEX);
|
|
|
|
if (device_key){
|
|
|
|
printf("{DevKey}\n");
|
|
|
|
printf_hex(device_key->key, 16);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
|
|
|
UNUSED(channel);
|
|
|
|
UNUSED(size);
|
|
|
|
bd_addr_t addr;
|
|
|
|
int i;
|
|
|
|
int prov_len;
|
2018-11-02 15:18:56 +01:00
|
|
|
mesh_provisioning_data_t provisioning_data;
|
2018-10-05 15:13:34 +02:00
|
|
|
|
|
|
|
switch (packet_type) {
|
|
|
|
case HCI_EVENT_PACKET:
|
|
|
|
switch (hci_event_packet_get_type(packet)) {
|
|
|
|
case BTSTACK_EVENT_STATE:
|
|
|
|
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
|
|
|
|
// dump bd_addr in pts format
|
|
|
|
gap_local_bd_addr(addr);
|
|
|
|
printf("Local addr: %s - ", bd_addr_to_str(addr));
|
|
|
|
for (i=0;i<6;i++) {
|
|
|
|
printf("%02x", addr[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
// get tlv
|
|
|
|
btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context);
|
|
|
|
// load provisioning data
|
2018-10-23 18:43:10 +02:00
|
|
|
prov_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, 'PROV', (uint8_t *) &provisioning_data, sizeof(mesh_provisioning_data_t));
|
2018-10-05 15:13:34 +02:00
|
|
|
printf("Provisioning data available: %u\n", prov_len ? 1 : 0);
|
2018-10-23 19:20:53 +02:00
|
|
|
if (prov_len){
|
2018-11-06 17:46:14 +01:00
|
|
|
mesh_setup_from_provisioning_data(&provisioning_data);
|
2019-07-09 15:14:56 +02:00
|
|
|
// load iv index
|
2019-07-10 14:22:44 +02:00
|
|
|
mesh_restore_iv_index_and_sequence_number();
|
2019-07-02 16:23:31 +02:00
|
|
|
// load network keys
|
|
|
|
mesh_load_network_keys();
|
|
|
|
// load app keys
|
|
|
|
mesh_load_app_keys();
|
|
|
|
// load model to appkey bindings
|
|
|
|
mesh_load_appkey_lists();
|
|
|
|
// load virtual addresses
|
|
|
|
mesh_load_virtual_addresses();
|
|
|
|
// load model subscriptions
|
|
|
|
mesh_load_subscriptions();
|
|
|
|
// load model publications
|
|
|
|
mesh_load_publications();
|
|
|
|
// load foundation state
|
|
|
|
mesh_foundation_state_load();
|
|
|
|
|
|
|
|
// dump PTS MeshOptions.ini
|
|
|
|
mesh_pts_dump_mesh_options();
|
|
|
|
|
2019-03-27 21:23:47 +01:00
|
|
|
} else {
|
2019-05-08 15:54:18 +02:00
|
|
|
mesh_setup_without_provisiong_data();
|
2018-10-23 19:20:53 +02:00
|
|
|
}
|
2019-06-12 17:43:59 +02:00
|
|
|
|
|
|
|
#if defined(ENABLE_MESH_ADV_BEARER) || defined(ENABLE_MESH_PB_ADV)
|
|
|
|
|
2019-06-25 16:45:58 +02:00
|
|
|
// start sending Secure Network Beacon
|
2019-07-02 17:39:24 +02:00
|
|
|
mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(0);
|
|
|
|
if (subnet){
|
|
|
|
beacon_secure_network_start(subnet);
|
2019-06-25 16:45:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
// setup scanning
|
|
|
|
gap_set_scan_parameters(0, 0x300, 0x300);
|
|
|
|
gap_start_scan();
|
2019-05-22 15:25:05 +02:00
|
|
|
#endif
|
2019-01-11 14:57:03 +01:00
|
|
|
//
|
|
|
|
show_usage();
|
2018-10-05 15:13:34 +02:00
|
|
|
break;
|
2019-05-22 15:32:12 +02:00
|
|
|
|
|
|
|
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
|
|
|
// enable PB_GATT
|
|
|
|
if (provisioned == 0){
|
2019-05-22 16:57:07 +02:00
|
|
|
setup_advertising_unprovisioned();
|
2019-06-03 15:37:51 +02:00
|
|
|
} else {
|
2019-06-03 17:06:43 +02:00
|
|
|
#ifdef ENABLE_MESH_PROXY_SERVER
|
2019-06-03 15:37:51 +02:00
|
|
|
printf("Advertise Mesh Proxy Service with Network ID\n");
|
2019-06-03 17:06:43 +02:00
|
|
|
mesh_proxy_start_advertising_with_network_id();
|
2019-06-03 15:37:51 +02:00
|
|
|
#endif
|
2019-05-22 15:32:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-05-22 15:25:05 +02:00
|
|
|
case HCI_EVENT_LE_META:
|
|
|
|
if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
|
|
|
|
// disable PB_GATT
|
2019-06-03 15:37:51 +02:00
|
|
|
printf("Connected, stop advertising gatt service\n");
|
|
|
|
adv_bearer_advertisements_remove_item(&connectable_advertisement_item);
|
2019-05-22 15:25:05 +02:00
|
|
|
break;
|
2018-10-05 15:13:34 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:16:18 +02:00
|
|
|
static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2018-10-05 15:13:34 +02:00
|
|
|
if (packet_type != HCI_EVENT_PACKET) return;
|
2018-11-02 15:18:56 +01:00
|
|
|
mesh_provisioning_data_t provisioning_data;
|
2019-07-02 16:23:31 +02:00
|
|
|
mesh_network_key_t * primary_network_key;
|
2019-07-02 17:39:24 +02:00
|
|
|
mesh_subnet_t * primary_subnet;
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
switch(packet[0]){
|
|
|
|
case HCI_EVENT_MESH_META:
|
|
|
|
switch(packet[2]){
|
2019-06-03 11:30:28 +02:00
|
|
|
case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
|
2018-10-05 15:13:34 +02:00
|
|
|
printf("Provisioner link opened");
|
2019-06-03 11:30:28 +02:00
|
|
|
pb_transport_cid = mesh_subevent_pb_transport_link_open_get_pb_transport_cid(packet);
|
2018-11-16 09:36:34 +01:00
|
|
|
break;
|
2019-06-03 11:30:28 +02:00
|
|
|
case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
|
2018-11-16 09:36:34 +01:00
|
|
|
pb_transport_cid = MESH_PB_TRANSPORT_INVALID_CID;
|
2018-10-05 15:13:34 +02:00
|
|
|
break;
|
2019-06-03 11:30:28 +02:00
|
|
|
case MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER:
|
2018-10-05 15:13:34 +02:00
|
|
|
printf("Attention Timer: %u\n", packet[3]);
|
|
|
|
break;
|
2019-06-03 11:30:28 +02:00
|
|
|
case MESH_SUBEVENT_PB_PROV_INPUT_OOB_REQUEST:
|
2018-10-05 15:13:34 +02:00
|
|
|
printf("Enter passphrase: ");
|
|
|
|
fflush(stdout);
|
|
|
|
ui_chars_for_pin = 1;
|
|
|
|
ui_pin_offset = 0;
|
|
|
|
break;
|
2019-06-03 11:30:28 +02:00
|
|
|
case MESH_SUBEVENT_PB_PROV_COMPLETE:
|
2018-10-05 15:13:34 +02:00
|
|
|
printf("Provisioning complete\n");
|
2019-04-24 15:55:34 +02:00
|
|
|
|
2019-07-02 16:23:31 +02:00
|
|
|
// delete old data
|
|
|
|
mesh_delete_network_keys();
|
|
|
|
mesh_delete_app_keys();
|
|
|
|
mesh_delete_appkey_lists();
|
|
|
|
|
|
|
|
// get provisioning data
|
2018-11-07 18:07:09 +01:00
|
|
|
memcpy(provisioning_data.device_key, provisioning_device_data_get_device_key(), 16);
|
2018-11-05 16:58:54 +01:00
|
|
|
provisioning_data.flags = provisioning_device_data_get_flags();
|
|
|
|
provisioning_data.unicast_address = provisioning_device_data_get_unicast_address();
|
2019-04-24 15:55:34 +02:00
|
|
|
|
2019-07-09 15:14:56 +02:00
|
|
|
// get iv_index
|
|
|
|
mesh_set_iv_index(provisioning_device_data_get_iv_index());
|
|
|
|
|
2019-07-02 16:23:31 +02:00
|
|
|
// get primary netkey
|
|
|
|
primary_network_key = provisioning_device_data_get_network_key();
|
|
|
|
mesh_network_key_dump(primary_network_key);
|
2019-07-02 17:09:36 +02:00
|
|
|
|
2019-07-02 16:23:31 +02:00
|
|
|
// add to network keys
|
|
|
|
mesh_network_key_add(primary_network_key);
|
2019-06-06 18:10:08 +02:00
|
|
|
|
2019-07-02 17:09:36 +02:00
|
|
|
// setup primary network
|
2019-07-04 12:24:56 +02:00
|
|
|
mesh_subnet_setup_for_netkey_index(primary_network_key->netkey_index);
|
2019-07-02 17:09:36 +02:00
|
|
|
|
2019-07-02 16:23:31 +02:00
|
|
|
// store provisioning data and primary network key in TLV
|
2018-10-23 18:43:10 +02:00
|
|
|
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, 'PROV', (uint8_t *) &provisioning_data, sizeof(mesh_provisioning_data_t));
|
2019-07-02 16:23:31 +02:00
|
|
|
mesh_store_network_key(primary_network_key);
|
2019-06-03 15:37:51 +02:00
|
|
|
|
2019-07-10 11:40:52 +02:00
|
|
|
// store IV Index and sequence number
|
|
|
|
mesh_store_iv_index_and_sequence_number();
|
2019-07-09 15:14:56 +02:00
|
|
|
|
2019-06-03 15:37:51 +02:00
|
|
|
// setup after provisioned
|
2018-11-06 17:46:14 +01:00
|
|
|
mesh_setup_from_provisioning_data(&provisioning_data);
|
2019-06-04 11:44:15 +02:00
|
|
|
|
|
|
|
// start advertising with node id after provisioning
|
2019-07-02 16:23:31 +02:00
|
|
|
mesh_proxy_set_advertising_with_node_id(primary_network_key->netkey_index, MESH_NODE_IDENTITY_STATE_ADVERTISING_RUNNING);
|
2019-06-25 16:45:58 +02:00
|
|
|
|
|
|
|
// start sending Secure Network Beacons
|
2019-07-02 17:39:24 +02:00
|
|
|
primary_subnet = mesh_subnet_get_by_netkey_index(0);
|
|
|
|
beacon_secure_network_start(primary_subnet);
|
2019-06-25 16:45:58 +02:00
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:16:02 +02:00
|
|
|
static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t 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:
|
2019-06-14 11:57:46 +02:00
|
|
|
printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n",
|
2019-06-13 16:02:19 +02:00
|
|
|
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));
|
2019-06-13 15:16:02 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
static void mesh_unprovisioned_beacon_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2018-11-21 17:10:36 +01:00
|
|
|
if (packet_type != MESH_BEACON_PACKET) return;
|
|
|
|
uint8_t device_uuid[16];
|
2018-10-05 15:13:34 +02:00
|
|
|
uint16_t oob;
|
2018-11-21 17:10:36 +01:00
|
|
|
memcpy(device_uuid, &packet[1], 16);
|
|
|
|
oob = big_endian_read_16(packet, 17);
|
|
|
|
printf("received unprovisioned device beacon, oob data %x, device uuid: ", oob);
|
|
|
|
printf_hexdump(device_uuid, 16);
|
|
|
|
pb_adv_create_link(device_uuid);
|
2018-10-05 15:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pts_device_uuid[16];
|
|
|
|
const char * pts_device_uuid_string = "001BDC0810210B0E0A0C000B0E0A0C00";
|
|
|
|
|
|
|
|
static int scan_hex_byte(const char * byte_string){
|
|
|
|
int upper_nibble = nibble_for_char(*byte_string++);
|
|
|
|
if (upper_nibble < 0) return -1;
|
|
|
|
int lower_nibble = nibble_for_char(*byte_string);
|
|
|
|
if (lower_nibble < 0) return -1;
|
|
|
|
return (upper_nibble << 4) | lower_nibble;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
int single_byte = scan_hex_byte(string);
|
|
|
|
if (single_byte < 0) return 0;
|
|
|
|
string += 2;
|
|
|
|
buffer[i] = (uint8_t)single_byte;
|
|
|
|
// don't check seperator after last byte
|
|
|
|
if (i == len - 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// optional seperator
|
|
|
|
char separator = *string;
|
|
|
|
if (separator == ':' && separator == '-' && separator == ' ') {
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btstack_print_hex(const uint8_t * data, uint16_t len, char separator){
|
|
|
|
int i;
|
|
|
|
for (i=0;i<len;i++){
|
|
|
|
printf("%02x", data[i]);
|
|
|
|
if (separator){
|
|
|
|
printf("%c", separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2019-01-15 17:22:12 +01:00
|
|
|
static uint16_t pts_proxy_dst;
|
|
|
|
static int pts_type;
|
2018-10-05 15:13:34 +02:00
|
|
|
|
|
|
|
static uint8_t prov_static_oob_data[16];
|
|
|
|
static const char * prov_static_oob_string = "00000000000000000102030405060708";
|
|
|
|
|
|
|
|
static uint8_t prov_public_key_data[64];
|
|
|
|
static const char * prov_public_key_string = "F465E43FF23D3F1B9DC7DFC04DA8758184DBC966204796ECCF0D6CF5E16500CC0201D048BCBBD899EEEFC424164E33C201C2B010CA6B4D43A8A155CAD8ECB279";
|
|
|
|
static uint8_t prov_private_key_data[32];
|
|
|
|
static const char * prov_private_key_string = "529AA0670D72CD6497502ED473502B037E8803B5C60829A5A3CAA219505530BA";
|
|
|
|
|
2019-06-06 17:20:45 +02:00
|
|
|
static mesh_transport_key_t pts_application_key;
|
|
|
|
|
2019-01-29 10:24:44 +01:00
|
|
|
static void load_pts_app_key(void){
|
|
|
|
// PTS app key
|
2019-06-06 17:20:45 +02:00
|
|
|
btstack_parse_hex("3216D1509884B533248541792B877F98", 16, pts_application_key.key);
|
|
|
|
pts_application_key.aid = 0x38;
|
2019-06-11 14:07:07 +02:00
|
|
|
pts_application_key.internal_index = mesh_transport_key_get_free_index();
|
2019-06-06 17:20:45 +02:00
|
|
|
mesh_transport_key_add(&pts_application_key);
|
2019-03-27 21:23:47 +01:00
|
|
|
printf("PTS Application Key (AID %02x): ", 0x38);
|
2019-06-06 17:20:45 +02:00
|
|
|
printf_hexdump(pts_application_key.key, 16);
|
2019-01-29 10:24:44 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 15:21:42 +01:00
|
|
|
static void send_pts_network_messsage(int type){
|
|
|
|
uint8_t lower_transport_pdu_data[16];
|
2018-11-02 15:18:56 +01:00
|
|
|
|
|
|
|
uint16_t src = 0x0028;
|
|
|
|
uint16_t dst = 0x0001;
|
|
|
|
uint32_t seq = 0x00;
|
|
|
|
uint8_t ttl = 0;
|
|
|
|
uint8_t ctl = 0;
|
|
|
|
|
|
|
|
switch (type){
|
|
|
|
case 0:
|
|
|
|
ttl = 0;
|
|
|
|
dst = 0x001;
|
|
|
|
printf("unicast ttl=0\n");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
dst = 0x001;
|
2019-04-16 17:05:14 +02:00
|
|
|
ttl = PTS_DEFAULT_TTL;
|
2018-11-02 15:18:56 +01:00
|
|
|
printf("unicast ttl=10\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dst = 0x001;
|
|
|
|
ttl = 0x7f;
|
|
|
|
printf("unicast ttl=0x7f\n");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
printf("virtual\n");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
printf("group\n");
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
printf("all-proxies\n");
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
printf("all-friends\n");
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
printf("all-relays\n");
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
printf("all-nodes\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 15:21:42 +01:00
|
|
|
int lower_transport_pdu_len = 16;
|
|
|
|
memset(lower_transport_pdu_data, 0x55, lower_transport_pdu_len);
|
|
|
|
mesh_network_send(0, ctl, ttl, seq, src, dst, lower_transport_pdu_data, lower_transport_pdu_len);
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:57:03 +01:00
|
|
|
static void send_pts_unsegmented_access_messsage(void){
|
2018-11-16 15:21:42 +01:00
|
|
|
uint8_t access_pdu_data[16];
|
|
|
|
|
2019-01-29 10:24:44 +01:00
|
|
|
load_pts_app_key();
|
|
|
|
|
2018-11-16 15:21:42 +01:00
|
|
|
uint16_t src = primary_element_address;
|
2018-12-04 14:48:26 +01:00
|
|
|
uint16_t dest = 0x0001;
|
2019-04-16 17:05:14 +02:00
|
|
|
uint8_t ttl = PTS_DEFAULT_TTL;
|
2018-11-16 15:21:42 +01:00
|
|
|
|
|
|
|
int access_pdu_len = 1;
|
|
|
|
memset(access_pdu_data, 0x55, access_pdu_len);
|
2018-12-04 14:48:26 +01:00
|
|
|
uint16_t netkey_index = 0;
|
2019-01-11 14:57:03 +01:00
|
|
|
uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX;
|
2018-12-04 14:48:26 +01:00
|
|
|
|
|
|
|
// send as unsegmented access pdu
|
2019-04-22 22:48:59 +02:00
|
|
|
mesh_pdu_t * pdu = (mesh_pdu_t*) mesh_network_pdu_get();
|
|
|
|
int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
|
2018-12-10 14:58:54 +01:00
|
|
|
if (status) return;
|
2019-04-22 22:48:59 +02:00
|
|
|
mesh_upper_transport_send_access_pdu(pdu);
|
2018-11-02 15:18:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 14:57:03 +01:00
|
|
|
static void send_pts_segmented_access_messsage_unicast(void){
|
2018-12-10 14:58:54 +01:00
|
|
|
uint8_t access_pdu_data[20];
|
|
|
|
|
2019-01-29 10:24:44 +01:00
|
|
|
load_pts_app_key();
|
|
|
|
|
2018-12-10 14:58:54 +01:00
|
|
|
uint16_t src = primary_element_address;
|
|
|
|
uint16_t dest = 0x0001;
|
2019-04-16 17:05:14 +02:00
|
|
|
uint8_t ttl = PTS_DEFAULT_TTL;
|
2018-12-10 14:58:54 +01:00
|
|
|
|
|
|
|
int access_pdu_len = 20;
|
|
|
|
memset(access_pdu_data, 0x55, access_pdu_len);
|
|
|
|
uint16_t netkey_index = 0;
|
|
|
|
uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX;
|
|
|
|
|
|
|
|
// send as segmented access pdu
|
2019-04-22 22:48:59 +02:00
|
|
|
mesh_pdu_t * pdu = (mesh_pdu_t *) mesh_transport_pdu_get();
|
|
|
|
int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
|
2018-12-10 14:58:54 +01:00
|
|
|
if (status) return;
|
2019-04-22 22:48:59 +02:00
|
|
|
mesh_upper_transport_send_access_pdu(pdu);
|
2018-12-10 14:58:54 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:22:12 +01:00
|
|
|
static void send_pts_segmented_access_messsage_group(void){
|
2019-01-11 16:53:20 +01:00
|
|
|
uint8_t access_pdu_data[20];
|
|
|
|
|
2019-01-29 10:24:44 +01:00
|
|
|
load_pts_app_key();
|
|
|
|
|
2019-01-11 16:53:20 +01:00
|
|
|
uint16_t src = primary_element_address;
|
|
|
|
uint16_t dest = 0xd000;
|
2019-04-16 17:05:14 +02:00
|
|
|
uint8_t ttl = PTS_DEFAULT_TTL;
|
2019-01-11 16:53:20 +01:00
|
|
|
|
2019-01-15 17:22:12 +01:00
|
|
|
int access_pdu_len = 20;
|
|
|
|
memset(access_pdu_data, 0x55, access_pdu_len);
|
|
|
|
uint16_t netkey_index = 0;
|
2019-01-29 10:24:44 +01:00
|
|
|
uint16_t appkey_index = 0;
|
2019-01-15 17:22:12 +01:00
|
|
|
|
|
|
|
// send as segmented access pdu
|
2019-04-22 22:48:59 +02:00
|
|
|
mesh_pdu_t * pdu = (mesh_pdu_t *) mesh_transport_pdu_get();
|
|
|
|
int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
|
2019-01-15 17:22:12 +01:00
|
|
|
if (status) return;
|
2019-04-22 22:48:59 +02:00
|
|
|
mesh_upper_transport_send_access_pdu(pdu);
|
2019-01-15 17:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void send_pts_segmented_access_messsage_virtual(void){
|
|
|
|
uint8_t access_pdu_data[20];
|
|
|
|
|
2019-01-29 10:24:44 +01:00
|
|
|
load_pts_app_key();
|
|
|
|
|
2019-01-15 17:22:12 +01:00
|
|
|
uint16_t src = primary_element_address;
|
|
|
|
uint16_t dest = pts_proxy_dst;
|
2019-04-16 17:05:14 +02:00
|
|
|
uint8_t ttl = PTS_DEFAULT_TTL;
|
2019-01-11 16:53:20 +01:00
|
|
|
|
|
|
|
int access_pdu_len = 20;
|
|
|
|
memset(access_pdu_data, 0x55, access_pdu_len);
|
|
|
|
uint16_t netkey_index = 0;
|
2019-01-29 10:24:44 +01:00
|
|
|
uint16_t appkey_index = 0;
|
2019-01-11 16:53:20 +01:00
|
|
|
|
|
|
|
// send as segmented access pdu
|
2019-04-04 10:24:19 +02:00
|
|
|
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
|
2019-04-22 22:48:59 +02:00
|
|
|
int status = mesh_upper_transport_setup_access_pdu((mesh_pdu_t*) transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
|
2019-01-11 16:53:20 +01:00
|
|
|
if (status) return;
|
2019-04-22 22:32:01 +02:00
|
|
|
mesh_upper_transport_send_access_pdu((mesh_pdu_t*) transport_pdu);
|
2019-01-11 16:53:20 +01:00
|
|
|
}
|
2018-12-10 14:58:54 +01:00
|
|
|
|
2019-01-11 14:57:03 +01:00
|
|
|
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));
|
2019-06-14 11:57:46 +02:00
|
|
|
printf("0 - Send Network Message\n");
|
2019-01-11 14:57:03 +01:00
|
|
|
printf("1 - Send Unsegmented Access Message\n");
|
2019-06-14 11:57:46 +02:00
|
|
|
printf("2 - Send Segmented Access Message - Unicast\n");
|
|
|
|
printf("3 - Send Segmented Access Message - Group D000\n");
|
|
|
|
printf("4 - Send Segmented Access Message - Virtual 9779\n");
|
2019-03-29 19:12:01 +01:00
|
|
|
printf("6 - Clear Replay Protection List\n");
|
2019-03-27 21:23:47 +01:00
|
|
|
printf("7 - Load PTS App key\n");
|
2019-05-22 16:57:07 +02:00
|
|
|
printf("8 - Delete provisioning data\n");
|
2019-06-14 11:57:46 +02:00
|
|
|
printf("p - Enable Public Key OOB \n");
|
|
|
|
printf("o - Enable Output OOB \n");
|
|
|
|
printf("i - Input Output OOB \n");
|
|
|
|
printf("s - Static Output OOB \n");
|
|
|
|
printf("g - Generic ON/OFF Server Toggle Value\n");
|
2019-01-11 14:57:03 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
static void stdin_process(char cmd){
|
|
|
|
if (ui_chars_for_pin){
|
|
|
|
printf("%c", cmd);
|
|
|
|
fflush(stdout);
|
|
|
|
if (cmd == '\n'){
|
|
|
|
printf("\nSending Pin '%s'\n", ui_pin);
|
|
|
|
provisioning_device_input_oob_complete_alphanumeric(1, ui_pin, ui_pin_offset);
|
|
|
|
ui_chars_for_pin = 0;
|
|
|
|
} else {
|
|
|
|
ui_pin[ui_pin_offset++] = cmd;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (cmd){
|
2018-11-02 15:18:56 +01:00
|
|
|
case '0':
|
2018-11-16 15:21:42 +01:00
|
|
|
send_pts_network_messsage(pts_type++);
|
2018-11-02 15:18:56 +01:00
|
|
|
break;
|
2018-10-05 15:13:34 +02:00
|
|
|
case '1':
|
2019-01-11 14:57:03 +01:00
|
|
|
send_pts_unsegmented_access_messsage();
|
2018-11-16 15:21:42 +01:00
|
|
|
break;
|
2018-10-05 15:13:34 +02:00
|
|
|
case '2':
|
2019-01-11 14:57:03 +01:00
|
|
|
send_pts_segmented_access_messsage_unicast();
|
2018-12-10 14:58:54 +01:00
|
|
|
break;
|
2019-01-11 16:53:20 +01:00
|
|
|
case '3':
|
2019-01-15 17:22:12 +01:00
|
|
|
send_pts_segmented_access_messsage_group();
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
send_pts_segmented_access_messsage_virtual();
|
2019-01-11 16:53:20 +01:00
|
|
|
break;
|
2019-03-29 19:12:01 +01:00
|
|
|
case '6':
|
|
|
|
printf("Clearing Replay Protection List\n");
|
|
|
|
mesh_seq_auth_reset();
|
|
|
|
break;
|
2019-03-27 21:23:47 +01:00
|
|
|
case '7':
|
|
|
|
load_pts_app_key();
|
|
|
|
break;
|
2018-12-10 14:58:54 +01:00
|
|
|
case '8':
|
2019-06-14 16:03:52 +02:00
|
|
|
mesh_node_reset();
|
|
|
|
printf("Mesh Node Reset!\n");
|
2019-05-22 17:18:41 +02:00
|
|
|
setup_advertising_unprovisioned();
|
2018-10-05 15:13:34 +02:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
printf("+ Public Key OOB Enabled\n");
|
|
|
|
btstack_parse_hex(prov_public_key_string, 64, prov_public_key_data);
|
|
|
|
btstack_parse_hex(prov_private_key_string, 32, prov_private_key_data);
|
|
|
|
provisioning_device_set_public_key_oob(prov_public_key_data, prov_private_key_data);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
printf("+ Output OOB Enabled\n");
|
|
|
|
provisioning_device_set_output_oob_actions(0x08, 0x08);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
printf("+ Input OOB Enabled\n");
|
|
|
|
provisioning_device_set_input_oob_actions(0x08, 0x08);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
printf("+ Static OOB Enabled\n");
|
|
|
|
btstack_parse_hex(prov_static_oob_string, 16, prov_static_oob_data);
|
|
|
|
provisioning_device_set_static_oob(16, prov_static_oob_data);
|
|
|
|
break;
|
2019-06-14 11:57:46 +02:00
|
|
|
case 'g':
|
|
|
|
printf("Generic ON/OFF Server Toggle Value\n");
|
2019-06-17 15:10:34 +02:00
|
|
|
mesh_generic_on_off_server_set_value(&mesh_generic_on_off_server_model, 1-mesh_generic_on_off_server_get_value(&mesh_generic_on_off_server_model), 0, 0);
|
2019-06-14 11:57:46 +02:00
|
|
|
break;
|
2019-01-11 14:57:03 +01:00
|
|
|
case ' ':
|
|
|
|
show_usage();
|
|
|
|
break;
|
2018-10-05 15:13:34 +02:00
|
|
|
default:
|
2019-01-11 14:57:03 +01:00
|
|
|
printf("Command: '%c' not implemented\n", cmd);
|
|
|
|
show_usage();
|
2018-10-05 15:13:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int btstack_main(void);
|
|
|
|
int btstack_main(void)
|
|
|
|
{
|
|
|
|
// register for HCI events
|
|
|
|
hci_event_callback_registration.callback = &packet_handler;
|
|
|
|
hci_add_event_handler(&hci_event_callback_registration);
|
|
|
|
|
|
|
|
// console
|
|
|
|
btstack_stdin_setup(stdin_process);
|
|
|
|
|
|
|
|
// crypto
|
|
|
|
btstack_crypto_init();
|
|
|
|
|
2019-05-08 16:23:08 +02:00
|
|
|
// l2cap
|
|
|
|
l2cap_init();
|
|
|
|
|
|
|
|
// setup le device db
|
|
|
|
le_device_db_init();
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
//
|
|
|
|
sm_init();
|
|
|
|
|
|
|
|
// mesh
|
|
|
|
adv_bearer_init();
|
|
|
|
|
2019-06-03 17:06:43 +02:00
|
|
|
// 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);
|
|
|
|
|
2019-05-08 16:23:08 +02:00
|
|
|
// setup ATT server
|
|
|
|
att_server_init(profile_data, NULL, NULL);
|
|
|
|
|
2019-05-08 17:38:18 +02:00
|
|
|
// Setup GATT bearer
|
|
|
|
gatt_bearer_init();
|
|
|
|
|
2019-05-08 18:31:29 +02:00
|
|
|
#ifdef ENABLE_MESH_ADV_BEARER
|
2019-05-08 17:38:18 +02:00
|
|
|
// Setup Unprovisioned Device Beacon
|
2019-03-27 21:23:47 +01:00
|
|
|
beacon_init();
|
2019-06-28 14:35:12 +02:00
|
|
|
// Register for Unprovisioned Device Beacons provisioner
|
2018-10-05 15:13:34 +02:00
|
|
|
beacon_register_for_unprovisioned_device_beacons(&mesh_unprovisioned_beacon_handler);
|
2019-05-08 18:31:29 +02:00
|
|
|
#endif
|
|
|
|
|
2018-10-05 15:13:34 +02:00
|
|
|
// Provisioning in device role
|
|
|
|
provisioning_device_init(device_uuid);
|
2019-05-23 18:16:18 +02:00
|
|
|
provisioning_device_register_packet_handler(&mesh_provisioning_message_handler);
|
2018-10-05 15:13:34 +02:00
|
|
|
|
2018-11-02 15:50:26 +01:00
|
|
|
// Network layer
|
|
|
|
mesh_network_init();
|
2018-12-04 11:03:09 +01:00
|
|
|
|
|
|
|
// Transport layers (lower + upper))
|
2019-07-08 16:21:15 +02:00
|
|
|
mesh_lower_transport_init();
|
|
|
|
mesh_upper_transport_init();
|
2018-11-16 10:12:06 +01:00
|
|
|
|
2019-06-10 20:05:39 +02:00
|
|
|
// Access layer
|
|
|
|
mesh_access_init();
|
2019-06-06 11:33:26 +02:00
|
|
|
// Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
|
2019-06-10 21:40:37 +02:00
|
|
|
mesh_access_set_primary_element_location(0x103);
|
2019-01-15 17:22:12 +01:00
|
|
|
|
2019-06-06 11:33:26 +02:00
|
|
|
// Setup models
|
|
|
|
mesh_configuration_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER);
|
2019-04-28 09:33:29 +02:00
|
|
|
mesh_model_reset_appkeys(&mesh_configuration_server_model);
|
2019-06-17 14:14:49 +02:00
|
|
|
mesh_configuration_server_model.model_data = &mesh_configuration_server_model_context;
|
2019-06-10 22:29:44 +02:00
|
|
|
mesh_configuration_server_model.operations = mesh_configuration_server_get_operations();
|
2019-06-10 21:40:37 +02:00
|
|
|
mesh_element_add_model(mesh_primary_element(), &mesh_configuration_server_model);
|
2019-04-28 09:33:29 +02:00
|
|
|
|
2019-06-06 12:17:37 +02:00
|
|
|
mesh_health_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_HEALTH_SERVER);
|
2019-04-28 21:59:26 +02:00
|
|
|
mesh_model_reset_appkeys(&mesh_health_server_model);
|
2019-06-10 21:40:37 +02:00
|
|
|
mesh_element_add_model(mesh_primary_element(), &mesh_health_server_model);
|
2019-04-28 21:59:26 +02:00
|
|
|
|
2019-06-06 12:45:19 +02:00
|
|
|
mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER);
|
|
|
|
mesh_model_reset_appkeys(&mesh_generic_on_off_server_model);
|
2019-06-12 11:46:36 +02:00
|
|
|
mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations();
|
2019-06-10 21:40:37 +02:00
|
|
|
mesh_element_add_model(mesh_primary_element(), &mesh_generic_on_off_server_model);
|
2019-06-12 11:46:36 +02:00
|
|
|
mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state;
|
2019-06-18 10:41:50 +02:00
|
|
|
mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler);
|
2019-06-06 12:45:19 +02:00
|
|
|
|
2019-06-06 12:17:37 +02:00
|
|
|
mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER);
|
2019-04-28 21:59:26 +02:00
|
|
|
mesh_model_reset_appkeys(&mesh_vendor_model);
|
2019-06-10 21:40:37 +02:00
|
|
|
mesh_element_add_model(mesh_primary_element(), &mesh_vendor_model);
|
2019-06-06 11:33:26 +02:00
|
|
|
|
2019-06-27 12:31:02 +02:00
|
|
|
// Enable PROXY
|
|
|
|
mesh_foundation_gatt_proxy_set(1);
|
|
|
|
|
2019-04-23 17:13:07 +02:00
|
|
|
|
2019-06-10 21:40:37 +02:00
|
|
|
// PTS Virtual Address Label UUID - without Config Model, PTS uses our device uuid
|
2019-07-10 15:58:51 +02:00
|
|
|
uint8_t label_uuid[16];
|
2019-06-10 21:40:37 +02:00
|
|
|
btstack_parse_hex("001BDC0810210B0E0A0C000B0E0A0C00", 16, label_uuid);
|
2019-06-11 17:42:28 +02:00
|
|
|
mesh_virtual_address_t * virtual_addresss = mesh_virtual_address_register(label_uuid, 0x9779);
|
|
|
|
pts_proxy_dst = virtual_addresss->pseudo_dst;
|
2019-06-10 21:40:37 +02:00
|
|
|
|
|
|
|
// PTS Device UUID
|
2018-10-05 15:13:34 +02:00
|
|
|
btstack_parse_hex(pts_device_uuid_string, 16, pts_device_uuid);
|
|
|
|
btstack_print_hex(pts_device_uuid, 16, 0);
|
|
|
|
|
|
|
|
// turn on!
|
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* EXAMPLE_END */
|