for bbc microbit library
Fork of nRF51822 by
source/nRF5xGattServer.h@624:b3082031b43e, 2018-01-11 (annotated)
- Committer:
- euxton
- Date:
- Thu Jan 11 21:53:39 2018 +0000
- Revision:
- 624:b3082031b43e
- Parent:
- 616:a8f9b022d8fd
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LancasterUniversity | 615:65ea2acfc6a2 | 1 | /* mbed Microcontroller Library |
LancasterUniversity | 615:65ea2acfc6a2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
LancasterUniversity | 615:65ea2acfc6a2 | 3 | * |
LancasterUniversity | 615:65ea2acfc6a2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
LancasterUniversity | 615:65ea2acfc6a2 | 5 | * you may not use this file except in compliance with the License. |
LancasterUniversity | 615:65ea2acfc6a2 | 6 | * You may obtain a copy of the License at |
LancasterUniversity | 615:65ea2acfc6a2 | 7 | * |
LancasterUniversity | 615:65ea2acfc6a2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
LancasterUniversity | 615:65ea2acfc6a2 | 9 | * |
LancasterUniversity | 615:65ea2acfc6a2 | 10 | * Unless required by applicable law or agreed to in writing, software |
LancasterUniversity | 615:65ea2acfc6a2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
LancasterUniversity | 615:65ea2acfc6a2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
LancasterUniversity | 615:65ea2acfc6a2 | 13 | * See the License for the specific language governing permissions and |
LancasterUniversity | 615:65ea2acfc6a2 | 14 | * limitations under the License. |
LancasterUniversity | 615:65ea2acfc6a2 | 15 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 16 | |
LancasterUniversity | 615:65ea2acfc6a2 | 17 | #ifndef __NRF51822_GATT_SERVER_H__ |
LancasterUniversity | 615:65ea2acfc6a2 | 18 | #define __NRF51822_GATT_SERVER_H__ |
LancasterUniversity | 615:65ea2acfc6a2 | 19 | |
LancasterUniversity | 615:65ea2acfc6a2 | 20 | #include <stddef.h> |
LancasterUniversity | 615:65ea2acfc6a2 | 21 | |
LancasterUniversity | 615:65ea2acfc6a2 | 22 | #include "ble/blecommon.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 23 | #include "ble.h" /* nordic ble */ |
LancasterUniversity | 615:65ea2acfc6a2 | 24 | #include "ble/Gap.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 25 | #include "ble/GattServer.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 26 | |
LancasterUniversity | 615:65ea2acfc6a2 | 27 | class nRF5xGattServer : public GattServer |
LancasterUniversity | 615:65ea2acfc6a2 | 28 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 29 | public: |
LancasterUniversity | 615:65ea2acfc6a2 | 30 | /* Functions that must be implemented from GattServer */ |
LancasterUniversity | 615:65ea2acfc6a2 | 31 | virtual ble_error_t addService(GattService &); |
LancasterUniversity | 615:65ea2acfc6a2 | 32 | virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); |
LancasterUniversity | 615:65ea2acfc6a2 | 33 | virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); |
LancasterUniversity | 615:65ea2acfc6a2 | 34 | virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); |
LancasterUniversity | 615:65ea2acfc6a2 | 35 | virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); |
LancasterUniversity | 616:a8f9b022d8fd | 36 | virtual ble_error_t notify(GattAttribute::Handle_t, const uint8_t[], uint16_t); |
LancasterUniversity | 615:65ea2acfc6a2 | 37 | virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP); |
LancasterUniversity | 615:65ea2acfc6a2 | 38 | virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP); |
LancasterUniversity | 616:a8f9b022d8fd | 39 | virtual ble_error_t reset(void); |
LancasterUniversity | 615:65ea2acfc6a2 | 40 | |
LancasterUniversity | 615:65ea2acfc6a2 | 41 | /* nRF51 Functions */ |
LancasterUniversity | 615:65ea2acfc6a2 | 42 | void eventCallback(void); |
LancasterUniversity | 615:65ea2acfc6a2 | 43 | void hwCallback(ble_evt_t *p_ble_evt); |
LancasterUniversity | 615:65ea2acfc6a2 | 44 | |
LancasterUniversity | 616:a8f9b022d8fd | 45 | |
LancasterUniversity | 615:65ea2acfc6a2 | 46 | private: |
LancasterUniversity | 616:a8f9b022d8fd | 47 | const static unsigned BLE_TOTAL_CHARACTERISTICS = 30; |
LancasterUniversity | 616:a8f9b022d8fd | 48 | const static unsigned BLE_TOTAL_DESCRIPTORS = 20; |
LancasterUniversity | 615:65ea2acfc6a2 | 49 | |
LancasterUniversity | 615:65ea2acfc6a2 | 50 | private: |
LancasterUniversity | 615:65ea2acfc6a2 | 51 | /** |
LancasterUniversity | 615:65ea2acfc6a2 | 52 | * resolve a value attribute to its owning characteristic. |
LancasterUniversity | 615:65ea2acfc6a2 | 53 | * @param valueHandle the value handle to be resolved. |
LancasterUniversity | 615:65ea2acfc6a2 | 54 | * @return characteristic index if a resolution is found, else -1. |
LancasterUniversity | 615:65ea2acfc6a2 | 55 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 56 | int resolveValueHandleToCharIndex(GattAttribute::Handle_t valueHandle) const { |
LancasterUniversity | 615:65ea2acfc6a2 | 57 | unsigned charIndex; |
LancasterUniversity | 615:65ea2acfc6a2 | 58 | for (charIndex = 0; charIndex < characteristicCount; charIndex++) { |
LancasterUniversity | 615:65ea2acfc6a2 | 59 | if (nrfCharacteristicHandles[charIndex].value_handle == valueHandle) { |
LancasterUniversity | 615:65ea2acfc6a2 | 60 | return charIndex; |
LancasterUniversity | 615:65ea2acfc6a2 | 61 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 62 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 63 | |
LancasterUniversity | 615:65ea2acfc6a2 | 64 | return -1; |
LancasterUniversity | 615:65ea2acfc6a2 | 65 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 66 | |
LancasterUniversity | 615:65ea2acfc6a2 | 67 | /** |
LancasterUniversity | 615:65ea2acfc6a2 | 68 | * resolve a CCCD attribute handle to its owning characteristic. |
LancasterUniversity | 615:65ea2acfc6a2 | 69 | * @param cccdHandle the CCCD handle to be resolved. |
LancasterUniversity | 615:65ea2acfc6a2 | 70 | * @return characteristic index if a resolution is found, else -1. |
LancasterUniversity | 615:65ea2acfc6a2 | 71 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 72 | int resolveCCCDHandleToCharIndex(GattAttribute::Handle_t cccdHandle) const { |
LancasterUniversity | 615:65ea2acfc6a2 | 73 | unsigned charIndex; |
LancasterUniversity | 615:65ea2acfc6a2 | 74 | for (charIndex = 0; charIndex < characteristicCount; charIndex++) { |
LancasterUniversity | 615:65ea2acfc6a2 | 75 | if (nrfCharacteristicHandles[charIndex].cccd_handle == cccdHandle) { |
LancasterUniversity | 615:65ea2acfc6a2 | 76 | return charIndex; |
LancasterUniversity | 615:65ea2acfc6a2 | 77 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 78 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 79 | |
LancasterUniversity | 615:65ea2acfc6a2 | 80 | return -1; |
LancasterUniversity | 615:65ea2acfc6a2 | 81 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 82 | |
LancasterUniversity | 615:65ea2acfc6a2 | 83 | private: |
LancasterUniversity | 615:65ea2acfc6a2 | 84 | GattCharacteristic *p_characteristics[BLE_TOTAL_CHARACTERISTICS]; |
LancasterUniversity | 615:65ea2acfc6a2 | 85 | ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS]; |
LancasterUniversity | 615:65ea2acfc6a2 | 86 | GattAttribute *p_descriptors[BLE_TOTAL_DESCRIPTORS]; |
LancasterUniversity | 615:65ea2acfc6a2 | 87 | uint8_t descriptorCount; |
LancasterUniversity | 615:65ea2acfc6a2 | 88 | uint16_t nrfDescriptorHandles[BLE_TOTAL_DESCRIPTORS]; |
LancasterUniversity | 615:65ea2acfc6a2 | 89 | |
LancasterUniversity | 616:a8f9b022d8fd | 90 | /* |
LancasterUniversity | 616:a8f9b022d8fd | 91 | * Allow instantiation from nRF5xn when required. |
LancasterUniversity | 616:a8f9b022d8fd | 92 | */ |
LancasterUniversity | 616:a8f9b022d8fd | 93 | friend class nRF5xn; |
LancasterUniversity | 616:a8f9b022d8fd | 94 | |
LancasterUniversity | 615:65ea2acfc6a2 | 95 | nRF5xGattServer() : GattServer(), p_characteristics(), nrfCharacteristicHandles(), p_descriptors(), descriptorCount(0), nrfDescriptorHandles() { |
LancasterUniversity | 615:65ea2acfc6a2 | 96 | /* empty */ |
LancasterUniversity | 615:65ea2acfc6a2 | 97 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 98 | |
LancasterUniversity | 615:65ea2acfc6a2 | 99 | private: |
LancasterUniversity | 615:65ea2acfc6a2 | 100 | nRF5xGattServer(const nRF5xGattServer &); |
LancasterUniversity | 615:65ea2acfc6a2 | 101 | const nRF5xGattServer& operator=(const nRF5xGattServer &); |
LancasterUniversity | 615:65ea2acfc6a2 | 102 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 103 | |
rgrover1 | 388:db85a09c27ef | 104 | #endif // ifndef __NRF51822_GATT_SERVER_H__ |