Changed URIBeaconConfigService.h to work with ST board

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DFUService.h Source File

DFUService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_DFU_SERVICE_H__
00018 #define __BLE_DFU_SERVICE_H__
00019 
00020 #include "ble/BLE.h"
00021 #include "ble/UUID.h"
00022 
00023 extern "C" void bootloader_start(void);
00024 
00025 extern const uint8_t  DFUServiceBaseUUID[];
00026 extern const uint16_t DFUServiceShortUUID;
00027 extern const uint16_t DFUServiceControlCharacteristicShortUUID;
00028 
00029 extern const uint8_t  DFUServiceUUID[];
00030 extern const uint8_t  DFUServiceControlCharacteristicUUID[];
00031 extern const uint8_t  DFUServicePacketCharacteristicUUID[];
00032 
00033 /**
00034 * @class DFUService
00035 * @brief Device Firmware Update Service.
00036 */
00037 class DFUService {
00038 public:
00039     /**
00040      * @brief Signature for the handover callback. The application may provide such a
00041      * callback when setting up the DFU service, in which case it will be
00042      * invoked before handing control over to the bootloader.
00043      */
00044     typedef void (*ResetPrepare_t)(void);
00045 
00046 public:
00047     /**
00048     * @brief Adds Device Firmware Update service to an existing ble object.
00049     *
00050     * @param[ref] _ble
00051     *               BLE object for the underlying controller.
00052     * @param[in] _handoverCallback
00053     *                Application specific handover callback.
00054     */
00055     DFUService(BLE &_ble, ResetPrepare_t _handoverCallback = NULL) :
00056         ble(_ble),
00057         controlPoint(DFUServiceControlCharacteristicUUID, controlBytes, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00058         packet(DFUServicePacketCharacteristicUUID, packetBytes, SIZEOF_PACKET_BYTES, SIZEOF_PACKET_BYTES,
00059                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
00060         controlBytes(),
00061         packetBytes() {
00062         static bool serviceAdded = false; /* We should only ever need to add the DFU service once. */
00063         if (serviceAdded) {
00064             return;
00065         }
00066 
00067         /* Set an initial value for control bytes so that the application's DFUService can
00068          * be distinguished from the real DFU service provided by the bootloader. */
00069         controlBytes[0] = 0xFF;
00070         controlBytes[1] = 0xFF;
00071 
00072         GattCharacteristic *dfuChars[] = {&controlPoint, &packet};
00073         GattService         dfuService(DFUServiceUUID, dfuChars, sizeof(dfuChars) / sizeof(GattCharacteristic *));
00074 
00075         ble.addService(dfuService);
00076         handoverCallback = _handoverCallback;
00077         serviceAdded     = true;
00078 
00079         ble.onDataWritten(this, &DFUService::onDataWritten);
00080     }
00081 
00082     /**
00083     * @brief get the handle for the value attribute of the control characteristic.
00084     */
00085     uint16_t getControlHandle(void) const {
00086         return controlPoint.getValueHandle();
00087     }
00088 
00089     /**
00090      * @brief This callback allows the DFU service to receive the initial trigger to
00091      * handover control to the bootloader; but first the application is given a
00092      * chance to clean up.
00093      *
00094      * @param[in] params
00095      *     Information about the characterisitc being updated.
00096      */
00097     virtual void onDataWritten(const GattWriteCallbackParams *params) {
00098         if (params->handle == controlPoint.getValueHandle()) {
00099             /* At present, writing anything will do the trick--this needs to be improved. */
00100             if (handoverCallback) {
00101                 handoverCallback();
00102             }
00103 
00104             bootloader_start();
00105         }
00106     }
00107 
00108 protected:
00109     static const unsigned SIZEOF_CONTROL_BYTES = 2;
00110     static const unsigned SIZEOF_PACKET_BYTES  = 20;
00111 
00112 protected:
00113     BLE          &ble;
00114 
00115     /**< Writing to the control characteristic triggers the handover to dfu-
00116       *  bootloader. At present, writing anything will do the trick--this needs
00117       *  to be improved. */
00118     WriteOnlyArrayGattCharacteristic<uint8_t, SIZEOF_CONTROL_BYTES> controlPoint;
00119 
00120     /**< The packet characteristic in this service doesn't do anything meaningful, but
00121       *  is only a placeholder to mimic the corresponding characteristic in the
00122       *  actual DFU service implemented by the bootloader. Without this, some
00123       *  FOTA clients might get confused as service definitions change after
00124       *  handing control over to the bootloader. */
00125     GattCharacteristic  packet;
00126 
00127     uint8_t             controlBytes[SIZEOF_CONTROL_BYTES];
00128     uint8_t             packetBytes[SIZEOF_PACKET_BYTES];
00129 
00130     static ResetPrepare_t handoverCallback;  /**< application specific handover callback. */
00131 };
00132 
00133 #endif /* #ifndef __BLE_DFU_SERVICE_H__*/