Default_Castum_AppFOTA4_Working

Dependencies:   mbed BLE_API nRF51822

Revision:
0:aa7314f3fa84
Child:
1:832e6e27cc49
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jan 24 00:41:45 2015 +0000
@@ -0,0 +1,102 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * 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.
+ */
+
+#include "mbed.h"
+#include "BLEDevice.h"
+
+#include "DFUService.h"
+#include "UARTService.h"
+#include "DeviceInformationService.h"
+
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+                               * it will have an impact on code-size and power consumption. */
+
+#if NEED_CONSOLE_OUTPUT
+#define DEBUG(...) { printf(__VA_ARGS__); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
+
+BLEDevice ble;
+UARTService *uartServicePtr;
+
+//const char *deviceName = "DefaultApp";
+const char *pcDeviceName = "FOTA4";
+const char *pcPing = "f4\n";    // Need "\n" to for BLE to Tx
+
+void periodicCallback(void)
+{
+    //DEBUG("ping\r\n");
+    DEBUG("p4\n");
+
+    ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t *)pcPing, strlen(pcPing)); // BLE UART = Ping Message
+}
+
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    //DEBUG("Disconnected!\n\r");
+    DEBUG("Disconnected!\n");
+    //DEBUG("Restarting the advertising process\n\r");
+    DEBUG("Restart Advertising\n");
+    ble.startAdvertising();
+}
+
+void onDataWritten(const GattCharacteristicWriteCBParams *params)
+{
+    //if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->txCharacteristic.getValueAttribute().getHandle())) 
+    if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
+        uint16_t bytesRead = params->len;
+        DEBUG("BLE-UART Echo Rx[%u]=[%*s]\n", bytesRead, params->len, params->data);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+    } else {
+        DEBUG("Unknown Characteristic Written, Handle:%d\n", params->charHandle);
+    }
+}
+
+int main(void)
+{
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+
+    //DEBUG("Initialising BTLE transport\n\r");
+    DEBUG("BLE Init\n");
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)pcDeviceName, strlen(pcDeviceName));
+
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */
+    ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
+    ble.startAdvertising();
+
+    DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1");
+
+    /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
+     * control characteristic which can be used to trigger the application to
+     * handover control to a resident bootloader. */
+    DFUService dfu(ble);
+
+    /* Setup a BLE service for console output. Redirect stdout to BLE-UART. */
+    UARTService uartService(ble);
+    uartServicePtr = &uartService;
+   //    uartService.retargetStdout(); //PR: Doesn't work with online libs BLE_API and nRF51822 updated 2015Jan23
+
+    for (;;) {
+        ble.waitForEvent();
+    }
+}