Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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