From 68124701432af3fdbabac7c132877312b0425011 Mon Sep 17 00:00:00 2001 From: Martin Turon Date: Thu, 3 Oct 2019 13:27:28 -0700 Subject: [PATCH] [toble] Enable ToBLE feature when third_party/openthread supports it. - Add logic to disable WoBLE when ToBLE is provisioned. - Extend default logging to include OpenThread platform and debug. - Increase GATT table size. - Make woble and toble use same ble config tag. --- main/include/OpenThreadConfig.h | 34 +++++++++++++++++++++++- main/include/WeaveProjectConfig.h | 13 ++++++++++ main/include/ble_config.h | 26 +++++++++++++++++++ main/main.cpp | 43 +++++++++++++++++++++++++++++++ third_party/openweave-core | 2 +- 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 main/include/ble_config.h diff --git a/main/include/OpenThreadConfig.h b/main/include/OpenThreadConfig.h index 276669c0..119958d9 100644 --- a/main/include/OpenThreadConfig.h +++ b/main/include/OpenThreadConfig.h @@ -25,6 +25,8 @@ #ifndef OPENTHREAD_PLATFORM_CONFIG_H #define OPENTHREAD_PLATFORM_CONFIG_H +#include "ble_config.h" + // Disable the Nordic-supplied OpenThread logging facilities and use // the facilities provided by the OpenWeave Device Layer (see // openweave/src/adaptations/device-layer/nRF5/Logging.cpp). @@ -33,6 +35,9 @@ // Turn on a moderate level of logging in OpenThread #define OPENTHREAD_CONFIG_LOG_LEVEL OT_LOG_LEVEL_NOTE +// To turn on Nordic platform logs set this to 1 +#define OPENTHREAD_CONFIG_LOG_PLATFORM 0 + // Use the Nordic-supplied default platform configuration for remainder // of OpenThread config options. // @@ -41,6 +46,33 @@ // #include "openthread-core-nrf52840-config.h" -#endif // OPENTHREAD_PLATFORM_CONFIG_H +// ============================================================== +// CONFIG TOBLE +// ============================================================== + +/** + * To enable the Thread-over-BLE feature set this to 1. + */ +#define OPENTHREAD_CONFIG_ENABLE_TOBLE 0 +#define OPENTHREAD_CONFIG_ENABLE_BLE 1 +#define OPENTHREAD_CONFIG_ENABLE_TOBLE_TO_BLE 1 +#define OPENTHREAD_CONFIG_TOBLE_CENTRAL_ENABLE 0 +#define OPENTHREAD_CONFIG_TOBLE_PERIPHERAL_ENABLE 1 +#define OPENTHREAD_CONFIG_TOBLE_MULTI_RADIO_ENABLE 1 +#define OPENTHREAD_CONFIG_TOBLE_154_ATTACH_ATTEMPT_RATIO 2 +#define OPENTHREAD_DISABLE_TOBLE_GATT_ACKNOWLEDGEMENTS 0 + +#define OPENTHREAD_CONFIG_MAC_DATA_POLL_RESPONSE_TIMEOUT 500 +#define OPENTHREAD_CONFIG_6LOWPAN_REASSEMBLY_TIMEOUT 60 + +#define OPENTHREAD_CONFIG_MLE_PARENT_REQUEST_ROUTER_TIMEOUT 1500 +#define OPENTHREAD_CONFIG_MLE_PARENT_REQUEST_REED_TIMEOUT 3000 +#define OPENTHREAD_CONFIG_MLE_UNICAST_RETRANSMISSION_DELAY 1500 +#define OPENTHREAD_CONFIG_MLE_CHILD_ID_REQUEST_TIMEOUT 10000 + +#define OPENTHREAD_CONFIG_NORDIC_BLE_CFG_TAG NRF_BLE_CFG_TAG + + +#endif // OPENTHREAD_PLATFORM_CONFIG_H diff --git a/main/include/WeaveProjectConfig.h b/main/include/WeaveProjectConfig.h index 9647452f..44018619 100644 --- a/main/include/WeaveProjectConfig.h +++ b/main/include/WeaveProjectConfig.h @@ -28,6 +28,11 @@ #ifndef WEAVE_PROJECT_CONFIG_H #define WEAVE_PROJECT_CONFIG_H +#include "ble_config.h" + + +#define WEAVE_PROGRESS_LOGGING 1 + /** * WEAVE_DEVICE_CONFIG_ENABLE_TEST_DEVICE_IDENTITY * @@ -146,4 +151,12 @@ */ #define WEAVE_CONFIG_EVENT_LOGGING_DEFAULT_IMPORTANCE nl::Weave::Profiles::DataManagement::Debug +/** + * WEAVE_DEVICE_LAYER_BLE_CONN_CFG_TAG + * + * The SoftDevice BLE connection configuration tag must be the same + * across both OpenWeave and OpenThread. + */ +#define WEAVE_DEVICE_LAYER_BLE_CONN_CFG_TAG NRF_BLE_CFG_TAG + #endif // WEAVE_PROJECT_CONFIG_H diff --git a/main/include/ble_config.h b/main/include/ble_config.h new file mode 100644 index 00000000..f5f9781e --- /dev/null +++ b/main/include/ble_config.h @@ -0,0 +1,26 @@ +/* + * + * Copyright (c) 2019 Google LLC. + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BLE_CONFIG_H +#define BLE_CONFIG_H + +// ---- BLE Config ---- +#define NRF_BLE_CFG_TAG 1 + + +#endif // BLE_CONFIG_H \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index b323d566..bda73dfc 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -72,6 +72,8 @@ using namespace ::nl::Weave::DeviceLayer; extern "C" size_t GetHeapTotalSize(void); +static void DeviceEventHandler(const WeaveDeviceEvent * event, intptr_t arg); + // ================================================================================ // Logging Support // ================================================================================ @@ -213,12 +215,19 @@ int main(void) { uint32_t appRAMStart = 0; + ble_cfg_t bleCfg; // Configure the BLE stack using the default settings. // Fetch the start address of the application RAM. ret = nrf_sdh_ble_default_cfg_set(WEAVE_DEVICE_LAYER_BLE_CONN_CFG_TAG, &appRAMStart); APP_ERROR_CHECK(ret); + // Increase the GATT table size to allow room for both WoBLE and ToBLE services. + memset(&bleCfg, 0, sizeof(bleCfg)); + bleCfg.gatts_cfg.attr_tab_size.attr_tab_size = NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE * 2; + ret = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &bleCfg, appRAMStart); + APP_ERROR_CHECK(ret); + // Enable BLE stack. ret = nrf_sdh_ble_enable(&appRAMStart); APP_ERROR_CHECK(ret); @@ -239,6 +248,10 @@ int main(void) APP_ERROR_HANDLER(ret); } + // Register a function to receive events from the Weave device layer. Note that calls to + // this function will happen on the Weave event loop thread, not the app_main thread. + PlatformMgr().AddEventHandler(DeviceEventHandler, 0); + NRF_LOG_INFO("Initializing OpenThread stack"); otSysInit(0, NULL); @@ -285,6 +298,21 @@ int main(void) } } +#if OPENTHREAD_CONFIG_ENABLE_TOBLE + // Disable Weave Pairing if Thread network has already been provisioned. + // TODO: Change this to only disable WoBLE when *full provisioning* has completed, + // i.e. ConfigurationManager.IsPairedToAccount() + if (ConnectivityMgr().IsThreadProvisioned()) + { + ConnectivityMgr().SetBLEAdvertisingEnabled(false); + NRF_LOG_INFO("WoBLE pairing is disabled, as Thread is provisioned"); + } + else +#endif // OPENTHREAD_CONFIG_ENABLE_TOBLE + { + NRF_LOG_INFO("WoBLE pairing is enabled"); + } + NRF_LOG_INFO("Starting Weave task"); ret = PlatformMgr().StartEventLoopTask(); @@ -329,3 +357,18 @@ int main(void) NRF_LOG_INFO("vTaskStartScheduler() failed"); APP_ERROR_HANDLER(0); } + +/* Handle events from the Weave Device layer. + * + * NOTE: This function runs on the Weave event loop task. + */ +void DeviceEventHandler(const WeaveDeviceEvent * event, intptr_t arg) +{ + if (event->Type == DeviceEventType::kSessionEstablished && + event->SessionEstablished.IsCommissioner) + { + // Disable advertising when Commissioner connected over WoBLE + // so ToBLE connectivity checks can occur. + ConnectivityMgr().SetBLEAdvertisingEnabled(false); + } +} diff --git a/third_party/openweave-core b/third_party/openweave-core index 645007fb..1f530c46 160000 --- a/third_party/openweave-core +++ b/third_party/openweave-core @@ -1 +1 @@ -Subproject commit 645007fb66e08a85e030d5487732f3f9394ee403 +Subproject commit 1f530c460194328a45e9dd27944db43770dabe1f