Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API LoRaWAN-lib SX1276Lib mbed nRF51822 HCSR04Lib
Fork of LoRa by
Revision 2:ce7cea075e95, committed 2016-06-01
- Comitter:
- haaspors
- Date:
- Wed Jun 01 14:07:25 2016 +0000
- Parent:
- 1:ca647cbbe4fb
- Child:
- 3:70d40f678f37
- Commit message:
- BLE custom GATT and DFU service.
Changed in this revision
--- a/Comissioning.h Mon May 09 08:07:23 2016 +0000
+++ b/Comissioning.h Wed Jun 01 14:07:25 2016 +0000
@@ -19,7 +19,7 @@
* When set to 1 the application uses the Over-the-Air activation procedure
* When set to 0 the application uses the Personalization activation procedure
*/
-#define OVER_THE_AIR_ACTIVATION 0
+#define OVER_THE_AIR_ACTIVATION 1
/*!
* Indicates if the end-device is to be connected to a private or public network
@@ -41,7 +41,7 @@
/*!
* AES encryption/decryption cipher application key
*/
-#define LORAWAN_APPLICATION_KEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
+#define LORAWAN_APPLICATION_KEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D }
#else
--- a/main.cpp Mon May 09 08:07:23 2016 +0000
+++ b/main.cpp Wed Jun 01 14:07:25 2016 +0000
@@ -5,6 +5,9 @@
#include "LoRaMac.h"
#include "Comissioning.h"
+#include "ble/BLE.h"
+#include "ble/services/DFUService.h"
+
/*!
* Join requests trials duty cycle.
*/
@@ -147,6 +150,36 @@
MibRequestConfirm_t mibReq;
+
+/* BLE stuff */
+const uint16_t customServiceUUID = 0xA000;
+const static char DEVICE_NAME[] = "TD Test Device";
+
+#if( OVER_THE_AIR_ACTIVATION != 0 )
+const uint16_t DevEuiUUID = 0xA001;
+const uint16_t AppEuiUUID = 0xA002;
+const uint16_t AppKeyUUID = 0xA003;
+
+ReadWriteArrayGattCharacteristic<uint8_t, sizeof(DevEui)> DevEuiChar(DevEuiUUID, DevEui);
+ReadWriteArrayGattCharacteristic<uint8_t, sizeof(AppEui)> AppEuiChar(AppEuiUUID, AppEui);
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(AppKey)> AppKeyChar(AppKeyUUID, AppKey);
+
+GattCharacteristic *characteristics[] = {&DevEuiChar, &AppEuiChar, &AppKeyChar};
+#else
+const uint16_t DevAddrUUID = 0xA004;
+const uint16_t NwkSKeyUUID = 0xA005;
+const uint16_t AppSKeyUUID = 0xA006;
+
+ReadWriteGattCharacteristic<uint32_t> DevAddrChar(DevAddrUUID, &DevAddr);
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(NwkSKey)> NwkSKeyChar(NwkSKeyUUID, NwkSKey);
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(AppSKey)> AppSKeyChar(AppSKeyUUID, AppSKey);
+
+GattCharacteristic *characteristics[] = {&DevAddrChar, &NwkSKeyChar, &AppSKeyChar};
+#endif
+GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+
+
+/* I/O stuff */
InterruptIn button(BUTTON3);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
@@ -410,6 +443,67 @@
}
}
+/*
+ * Restart advertising when phone app disconnects
+ */
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
+{
+ BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
+}
+
+/*
+ * Handle writes to writeCharacteristic
+ */
+void writeCharCallback(const GattWriteCallbackParams *params)
+{
+ /* Restart the lora stack */
+#if( OVER_THE_AIR_ACTIVATION != 0 )
+ if (params->handle == AppKeyChar.getValueHandle()) {
+ printf("Value written, device state set to JOIN\r\n");
+ memcpy(AppKey, params->data, params->len);
+ DeviceState = DEVICE_STATE_JOIN;
+ } else if (params->handle == AppEuiChar.getValueHandle()) {
+ memcpy(AppEui, params->data, params->len);
+ } else if (params->handle == DevEuiChar.getValueHandle()) {
+ memcpy(DevEui, params->data, params->len);
+ }
+#else
+ printf("Value written, device state set to JOIN\r\n");
+ DeviceState = DEVICE_STATE_JOIN;
+#endif
+ BLE::Instance(BLE::DEFAULT_INSTANCE).updateCharacteristicValue(params->handle, params->data, params->len);
+}
+
+/*
+ * Initialization callback
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext * params)
+{
+ const uint16_t uuid16_list[] = { customServiceUUID, DFUServiceShortUUID };
+ BLE &ble = params->ble;
+ ble_error_t error = params->error;
+
+ if (error != BLE_ERROR_NONE) {
+ return;
+ }
+
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.gattServer().onDataWritten(writeCharCallback);
+
+ /* Setup advertising, BLE only */
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+ ble.gap().setAdvertisingInterval(100);
+
+ ble.addService(customService);
+ static DFUService dfu(ble);
+
+ /* Start advertising */
+ ble.gap().startAdvertising();
+}
+
void buttonPressedCallback(void)
{
printf("Button pressed \r\n");
@@ -420,17 +514,23 @@
int main( void )
{
-
- button.fall(buttonPressedCallback);
-
printf("Hello world! \r\n");
+
LoRaMacPrimitives_t LoRaMacPrimitives;
LoRaMacCallback_t LoRaMacCallbacks;
+ BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
BoardInit( );
+
+ ble.init(bleInitComplete);
+
+ button.fall(buttonPressedCallback);
+ /* SpinWait for initialization to complete. This is necessary because the
+ * BLE object is used in the main loop below. */
+ while (ble.hasInitialized() == false) { /* spin loop */ }
+
DeviceState = DEVICE_STATE_INIT;
-
while( 1 )
{
if (DeviceState != DEVICE_STATE_SLEEP)
@@ -543,6 +643,7 @@
case DEVICE_STATE_SLEEP:
{
// Wake up through events
+ ble.waitForEvent();
break;
}
default:
--- a/mbed.bld Mon May 09 08:07:23 2016 +0000 +++ b/mbed.bld Wed Jun 01 14:07:25 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/082adc85693f \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb \ No newline at end of file
