project for nrf51822 qfab

Dependencies:   eddystone_URL mbed

Fork of eddystone_URL by vo dung

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
????????

Who changed what in which revision?

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