Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /* mbed Microcontroller Library
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2006-2013 ARM Limited
nexpaq 0:6c56fb4bc5f0 3 *
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the "License");
nexpaq 0:6c56fb4bc5f0 5 * you may not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an "AS IS" BASIS,
nexpaq 0:6c56fb4bc5f0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16
nexpaq 0:6c56fb4bc5f0 17 #ifdef TARGET_NRF51822 /* DFU only supported on nrf51 platforms */
nexpaq 0:6c56fb4bc5f0 18
nexpaq 0:6c56fb4bc5f0 19 #ifndef __BLE_DFU_SERVICE_H__
nexpaq 0:6c56fb4bc5f0 20 #define __BLE_DFU_SERVICE_H__
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 #include "ble/BLE.h"
nexpaq 0:6c56fb4bc5f0 23 #include "ble/UUID.h"
nexpaq 0:6c56fb4bc5f0 24
nexpaq 0:6c56fb4bc5f0 25 extern "C" {
nexpaq 0:6c56fb4bc5f0 26 #include "dfu_app_handler.h"
nexpaq 0:6c56fb4bc5f0 27 }
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 extern const uint8_t DFUServiceBaseUUID[];
nexpaq 0:6c56fb4bc5f0 30 extern const uint16_t DFUServiceShortUUID;
nexpaq 0:6c56fb4bc5f0 31 extern const uint16_t DFUServiceControlCharacteristicShortUUID;
nexpaq 0:6c56fb4bc5f0 32
nexpaq 0:6c56fb4bc5f0 33 extern const uint8_t DFUServiceUUID[];
nexpaq 0:6c56fb4bc5f0 34 extern const uint8_t DFUServiceControlCharacteristicUUID[];
nexpaq 0:6c56fb4bc5f0 35 extern const uint8_t DFUServicePacketCharacteristicUUID[];
nexpaq 0:6c56fb4bc5f0 36
nexpaq 0:6c56fb4bc5f0 37 /**
nexpaq 0:6c56fb4bc5f0 38 * @class DFUService
nexpaq 0:6c56fb4bc5f0 39 * @brief Device Firmware Update Service.
nexpaq 0:6c56fb4bc5f0 40 */
nexpaq 0:6c56fb4bc5f0 41 class DFUService {
nexpaq 0:6c56fb4bc5f0 42 public:
nexpaq 0:6c56fb4bc5f0 43 /**
nexpaq 0:6c56fb4bc5f0 44 * @brief Signature for the handover callback. The application may provide this
nexpaq 0:6c56fb4bc5f0 45 * callback when setting up the DFU service. The callback is then
nexpaq 0:6c56fb4bc5f0 46 * invoked before handing control over to the bootloader.
nexpaq 0:6c56fb4bc5f0 47 */
nexpaq 0:6c56fb4bc5f0 48 typedef void (*ResetPrepare_t)(void);
nexpaq 0:6c56fb4bc5f0 49
nexpaq 0:6c56fb4bc5f0 50 public:
nexpaq 0:6c56fb4bc5f0 51 /**
nexpaq 0:6c56fb4bc5f0 52 * @brief Adds Device Firmware Update Service to an existing BLE object.
nexpaq 0:6c56fb4bc5f0 53 *
nexpaq 0:6c56fb4bc5f0 54 * @param[ref] _ble
nexpaq 0:6c56fb4bc5f0 55 * BLE object for the underlying controller.
nexpaq 0:6c56fb4bc5f0 56 * @param[in] _handoverCallback
nexpaq 0:6c56fb4bc5f0 57 * Application-specific handover callback.
nexpaq 0:6c56fb4bc5f0 58 */
nexpaq 0:6c56fb4bc5f0 59 DFUService(BLE &_ble, ResetPrepare_t _handoverCallback = NULL) :
nexpaq 0:6c56fb4bc5f0 60 ble(_ble),
nexpaq 0:6c56fb4bc5f0 61 controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
nexpaq 0:6c56fb4bc5f0 62 packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES,
nexpaq 0:6c56fb4bc5f0 63 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
nexpaq 0:6c56fb4bc5f0 64 controlBytes(),
nexpaq 0:6c56fb4bc5f0 65 packetBytes() {
nexpaq 0:6c56fb4bc5f0 66 static bool serviceAdded = false; /* We only add the DFU service once. */
nexpaq 0:6c56fb4bc5f0 67 if (serviceAdded) {
nexpaq 0:6c56fb4bc5f0 68 return;
nexpaq 0:6c56fb4bc5f0 69 }
nexpaq 0:6c56fb4bc5f0 70
nexpaq 0:6c56fb4bc5f0 71 /* Set an initial value for control bytes, so that the application's DFU service can
nexpaq 0:6c56fb4bc5f0 72 * be distinguished from the real DFU service provided by the bootloader. */
nexpaq 0:6c56fb4bc5f0 73 controlBytes[0] = 0xFF;
nexpaq 0:6c56fb4bc5f0 74 controlBytes[1] = 0xFF;
nexpaq 0:6c56fb4bc5f0 75
nexpaq 0:6c56fb4bc5f0 76 GattCharacteristic *dfuChars[] = {&controlPoint, &packet};
nexpaq 0:6c56fb4bc5f0 77 GattService dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *));
nexpaq 0:6c56fb4bc5f0 78
nexpaq 0:6c56fb4bc5f0 79 ble.addService(dfuService);
nexpaq 0:6c56fb4bc5f0 80 handoverCallback = _handoverCallback;
nexpaq 0:6c56fb4bc5f0 81 serviceAdded = true;
nexpaq 0:6c56fb4bc5f0 82
nexpaq 0:6c56fb4bc5f0 83 ble.onDataWritten(this, &DFUService::onDataWritten);
nexpaq 0:6c56fb4bc5f0 84 }
nexpaq 0:6c56fb4bc5f0 85
nexpaq 0:6c56fb4bc5f0 86 /**
nexpaq 0:6c56fb4bc5f0 87 * @brief Get the handle for the value attribute of the control characteristic.
nexpaq 0:6c56fb4bc5f0 88 */
nexpaq 0:6c56fb4bc5f0 89 uint16_t getControlHandle(void) const {
nexpaq 0:6c56fb4bc5f0 90 return controlPoint.getValueHandle();
nexpaq 0:6c56fb4bc5f0 91 }
nexpaq 0:6c56fb4bc5f0 92
nexpaq 0:6c56fb4bc5f0 93 /**
nexpaq 0:6c56fb4bc5f0 94 * @brief This callback allows the DFU service to receive the initial trigger to
nexpaq 0:6c56fb4bc5f0 95 * hand control over to the bootloader. First, the application is given a
nexpaq 0:6c56fb4bc5f0 96 * chance to clean up.
nexpaq 0:6c56fb4bc5f0 97 *
nexpaq 0:6c56fb4bc5f0 98 * @param[in] params
nexpaq 0:6c56fb4bc5f0 99 * Information about the characterisitc being updated.
nexpaq 0:6c56fb4bc5f0 100 */
nexpaq 0:6c56fb4bc5f0 101 virtual void onDataWritten(const GattWriteCallbackParams *params) {
nexpaq 0:6c56fb4bc5f0 102 if (params->handle == controlPoint.getValueHandle()) {
nexpaq 0:6c56fb4bc5f0 103 /* At present, writing anything will do the trick - this needs to be improved. */
nexpaq 0:6c56fb4bc5f0 104 if (handoverCallback) {
nexpaq 0:6c56fb4bc5f0 105 handoverCallback();
nexpaq 0:6c56fb4bc5f0 106 }
nexpaq 0:6c56fb4bc5f0 107
nexpaq 0:6c56fb4bc5f0 108 // Call bootloader_start implicitly trough a event handler call
nexpaq 0:6c56fb4bc5f0 109 // it is a work around for bootloader_start not being public in sdk 8.1
nexpaq 0:6c56fb4bc5f0 110 ble_dfu_t p_dfu;
nexpaq 0:6c56fb4bc5f0 111 ble_dfu_evt_t p_evt;
nexpaq 0:6c56fb4bc5f0 112
nexpaq 0:6c56fb4bc5f0 113 p_dfu.conn_handle = params->connHandle;
nexpaq 0:6c56fb4bc5f0 114 p_evt.ble_dfu_evt_type = BLE_DFU_START;
nexpaq 0:6c56fb4bc5f0 115
nexpaq 0:6c56fb4bc5f0 116 dfu_app_on_dfu_evt(&p_dfu, &p_evt);
nexpaq 0:6c56fb4bc5f0 117 }
nexpaq 0:6c56fb4bc5f0 118 }
nexpaq 0:6c56fb4bc5f0 119
nexpaq 0:6c56fb4bc5f0 120 protected:
nexpaq 0:6c56fb4bc5f0 121 static const unsigned SIZEOF_CONTROL_BYTES = 2;
nexpaq 0:6c56fb4bc5f0 122 static const unsigned SIZEOF_PACKET_BYTES = 20;
nexpaq 0:6c56fb4bc5f0 123
nexpaq 0:6c56fb4bc5f0 124 protected:
nexpaq 0:6c56fb4bc5f0 125 BLE &ble;
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127 /**< Writing to the control characteristic triggers the handover to DFU
nexpaq 0:6c56fb4bc5f0 128 * bootloader. At present, writing anything will do the trick - this needs
nexpaq 0:6c56fb4bc5f0 129 * to be improved. */
nexpaq 0:6c56fb4bc5f0 130 WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_CONTROL_BYTES> controlPoint;
nexpaq 0:6c56fb4bc5f0 131
nexpaq 0:6c56fb4bc5f0 132 /**< The packet characteristic in this service doesn't do anything meaningful;
nexpaq 0:6c56fb4bc5f0 133 * it is only a placeholder to mimic the corresponding characteristic in the
nexpaq 0:6c56fb4bc5f0 134 * actual DFU service implemented by the bootloader. Without this, some
nexpaq 0:6c56fb4bc5f0 135 * FOTA clients might get confused, because service definitions change after
nexpaq 0:6c56fb4bc5f0 136 * handing control over to the bootloader. */
nexpaq 0:6c56fb4bc5f0 137 GattCharacteristic packet;
nexpaq 0:6c56fb4bc5f0 138
nexpaq 0:6c56fb4bc5f0 139 uint8_t controlBytes[SIZEOF_CONTROL_BYTES];
nexpaq 0:6c56fb4bc5f0 140 uint8_t packetBytes[SIZEOF_PACKET_BYTES];
nexpaq 0:6c56fb4bc5f0 141
nexpaq 0:6c56fb4bc5f0 142 static ResetPrepare_t handoverCallback; /**< Application-specific handover callback. */
nexpaq 0:6c56fb4bc5f0 143 };
nexpaq 0:6c56fb4bc5f0 144
nexpaq 0:6c56fb4bc5f0 145 #endif /* #ifndef __BLE_DFU_SERVICE_H__*/
nexpaq 0:6c56fb4bc5f0 146 #endif /* #ifdef TARGET_NRF51822 */