IOTIO

Fork of Nucleo_BLE_API by ST Americas mbed Team

Committer:
16038618
Date:
Sat Oct 29 14:16:12 2016 +0000
Revision:
2:584d5bedac75
Parent:
0:289fd2dae405
IOTIO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sjallouli 0:289fd2dae405 1 /* mbed Microcontroller Library
sjallouli 0:289fd2dae405 2 * Copyright (c) 2006-2013 ARM Limited
sjallouli 0:289fd2dae405 3 *
sjallouli 0:289fd2dae405 4 * Licensed under the Apache License, Version 2.0 (the "License");
sjallouli 0:289fd2dae405 5 * you may not use this file except in compliance with the License.
sjallouli 0:289fd2dae405 6 * You may obtain a copy of the License at
sjallouli 0:289fd2dae405 7 *
sjallouli 0:289fd2dae405 8 * http://www.apache.org/licenses/LICENSE-2.0
sjallouli 0:289fd2dae405 9 *
sjallouli 0:289fd2dae405 10 * Unless required by applicable law or agreed to in writing, software
sjallouli 0:289fd2dae405 11 * distributed under the License is distributed on an "AS IS" BASIS,
sjallouli 0:289fd2dae405 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sjallouli 0:289fd2dae405 13 * See the License for the specific language governing permissions and
sjallouli 0:289fd2dae405 14 * limitations under the License.
sjallouli 0:289fd2dae405 15 */
sjallouli 0:289fd2dae405 16
sjallouli 0:289fd2dae405 17 #ifndef __BLE_DFU_SERVICE_H__
sjallouli 0:289fd2dae405 18 #define __BLE_DFU_SERVICE_H__
sjallouli 0:289fd2dae405 19
sjallouli 0:289fd2dae405 20 #include "BLEDevice.h"
sjallouli 0:289fd2dae405 21 #include "UUID.h"
sjallouli 0:289fd2dae405 22
sjallouli 0:289fd2dae405 23 extern "C" void bootloader_start(void);
sjallouli 0:289fd2dae405 24
sjallouli 0:289fd2dae405 25 extern const uint8_t DFUServiceBaseUUID[];
sjallouli 0:289fd2dae405 26 extern const uint16_t DFUServiceShortUUID;
sjallouli 0:289fd2dae405 27 extern const uint16_t DFUServiceControlCharacteristicShortUUID;
sjallouli 0:289fd2dae405 28
sjallouli 0:289fd2dae405 29 extern const uint8_t DFUServiceUUID[];
sjallouli 0:289fd2dae405 30 extern const uint8_t DFUServiceControlCharacteristicUUID[];
sjallouli 0:289fd2dae405 31 extern const uint8_t DFUServicePacketCharacteristicUUID[];
sjallouli 0:289fd2dae405 32
sjallouli 0:289fd2dae405 33 class DFUService {
sjallouli 0:289fd2dae405 34 public:
sjallouli 0:289fd2dae405 35 /**
sjallouli 0:289fd2dae405 36 * Signature for the handover callback. The application may provide such a
sjallouli 0:289fd2dae405 37 * callback when setting up the DFU service, in which case it will be
sjallouli 0:289fd2dae405 38 * invoked before handing control over to the bootloader.
sjallouli 0:289fd2dae405 39 */
sjallouli 0:289fd2dae405 40 typedef void (*ResetPrepare_t)(void);
sjallouli 0:289fd2dae405 41
sjallouli 0:289fd2dae405 42 public:
sjallouli 0:289fd2dae405 43 DFUService(BLEDevice &_ble, ResetPrepare_t _handoverCallback = NULL) :
sjallouli 0:289fd2dae405 44 ble(_ble),
sjallouli 0:289fd2dae405 45 controlBytes(),
sjallouli 0:289fd2dae405 46 packetBytes(),
sjallouli 0:289fd2dae405 47 controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, SIZEOF_CONTROL_BYTES, SIZEOF_CONTROL_BYTES,
sjallouli 0:289fd2dae405 48 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
sjallouli 0:289fd2dae405 49 packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES,
sjallouli 0:289fd2dae405 50 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) {
sjallouli 0:289fd2dae405 51 static bool serviceAdded = false; /* We should only ever need to add the DFU service once. */
sjallouli 0:289fd2dae405 52 if (serviceAdded) {
sjallouli 0:289fd2dae405 53 return;
sjallouli 0:289fd2dae405 54 }
sjallouli 0:289fd2dae405 55
sjallouli 0:289fd2dae405 56 /* Set an initial value for control bytes so that the application's DFUService can
sjallouli 0:289fd2dae405 57 * be distinguished from the real DFU service provided by the bootloader. */
sjallouli 0:289fd2dae405 58 controlBytes[0] = 0xFF;
sjallouli 0:289fd2dae405 59 controlBytes[1] = 0xFF;
sjallouli 0:289fd2dae405 60
sjallouli 0:289fd2dae405 61 GattCharacteristic *dfuChars[] = {&controlPoint, &packet};
sjallouli 0:289fd2dae405 62 GattService dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *));
sjallouli 0:289fd2dae405 63
sjallouli 0:289fd2dae405 64 ble.addService(dfuService);
sjallouli 0:289fd2dae405 65 handoverCallback = _handoverCallback;
sjallouli 0:289fd2dae405 66 serviceAdded = true;
sjallouli 0:289fd2dae405 67
sjallouli 0:289fd2dae405 68 ble.onDataWritten(this, &DFUService::onDataWritten);
sjallouli 0:289fd2dae405 69 }
sjallouli 0:289fd2dae405 70
sjallouli 0:289fd2dae405 71 uint16_t getControlHandle(void) {
sjallouli 0:289fd2dae405 72 return controlPoint.getValueAttribute().getHandle();
sjallouli 0:289fd2dae405 73 }
sjallouli 0:289fd2dae405 74
sjallouli 0:289fd2dae405 75 /**
sjallouli 0:289fd2dae405 76 * This callback allows the DFU service to receive the initial trigger to
sjallouli 0:289fd2dae405 77 * handover control to the bootloader; but first the application is given a
sjallouli 0:289fd2dae405 78 * chance to clean up.
sjallouli 0:289fd2dae405 79 */
sjallouli 0:289fd2dae405 80 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
sjallouli 0:289fd2dae405 81 if (params->charHandle == controlPoint.getValueAttribute().getHandle()) {
sjallouli 0:289fd2dae405 82 /* At present, writing anything will do the trick--this needs to be improved. */
sjallouli 0:289fd2dae405 83 if (handoverCallback) {
sjallouli 0:289fd2dae405 84 handoverCallback();
sjallouli 0:289fd2dae405 85 }
sjallouli 0:289fd2dae405 86
sjallouli 0:289fd2dae405 87 bootloader_start();
sjallouli 0:289fd2dae405 88 }
sjallouli 0:289fd2dae405 89 }
sjallouli 0:289fd2dae405 90
sjallouli 0:289fd2dae405 91 private:
sjallouli 0:289fd2dae405 92 static const unsigned SIZEOF_CONTROL_BYTES = 2;
sjallouli 0:289fd2dae405 93 static const unsigned SIZEOF_PACKET_BYTES = 20;
sjallouli 0:289fd2dae405 94
sjallouli 0:289fd2dae405 95 static ResetPrepare_t handoverCallback; /**< application specific handover callback. */
sjallouli 0:289fd2dae405 96
sjallouli 0:289fd2dae405 97 private:
sjallouli 0:289fd2dae405 98 BLEDevice &ble;
sjallouli 0:289fd2dae405 99 uint8_t controlBytes[SIZEOF_CONTROL_BYTES];
sjallouli 0:289fd2dae405 100 uint8_t packetBytes[SIZEOF_PACKET_BYTES];
sjallouli 0:289fd2dae405 101
sjallouli 0:289fd2dae405 102 /**< Writing to the control characteristic triggers the handover to dfu-
sjallouli 0:289fd2dae405 103 * bootloader. At present, writing anything will do the trick--this needs
sjallouli 0:289fd2dae405 104 * to be improved. */
sjallouli 0:289fd2dae405 105 GattCharacteristic controlPoint;
sjallouli 0:289fd2dae405 106
sjallouli 0:289fd2dae405 107 /**< The packet characteristic in this service doesn't do anything meaningful, but
sjallouli 0:289fd2dae405 108 * is only a placeholder to mimic the corresponding characteristic in the
sjallouli 0:289fd2dae405 109 * actual DFU service implemented by the bootloader. Without this, some
sjallouli 0:289fd2dae405 110 * FOTA clients might get confused as service definitions change after
sjallouli 0:289fd2dae405 111 * handing control over to the bootloader. */
sjallouli 0:289fd2dae405 112 GattCharacteristic packet;
sjallouli 0:289fd2dae405 113 };
sjallouli 0:289fd2dae405 114
sjallouli 0:289fd2dae405 115 #endif /* #ifndef __BLE_DFU_SERVICE_H__*/