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