library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF5xGattServer.h Source File

nRF5xGattServer.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 __NRF51822_GATT_SERVER_H__
00018 #define __NRF51822_GATT_SERVER_H__
00019 
00020 #include <stddef.h>
00021 
00022 #include "ble/blecommon.h"
00023 #include "nrf_ble.h" /* nordic ble */
00024 #include "ble/Gap.h"
00025 #include "ble/GattServer.h"
00026 
00027 class nRF5xGattServer : public GattServer
00028 {
00029 public:
00030     /* Functions that must be implemented from GattServer */
00031     virtual ble_error_t addService(GattService &);
00032     virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
00033     virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
00034     virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
00035     virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
00036     virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP);
00037     virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP);
00038     virtual ble_error_t reset(void);
00039 
00040     /* nRF51 Functions */
00041     void eventCallback(void);
00042     void hwCallback(ble_evt_t *p_ble_evt);
00043 
00044 
00045 private:
00046     const static unsigned BLE_TOTAL_CHARACTERISTICS = 20;
00047     const static unsigned BLE_TOTAL_DESCRIPTORS     = 8;
00048 
00049 private:
00050     /**
00051      * resolve a value attribute to its owning characteristic.
00052      * @param  valueHandle the value handle to be resolved.
00053      * @return             characteristic index if a resolution is found, else -1.
00054      */
00055     int resolveValueHandleToCharIndex(GattAttribute::Handle_t valueHandle) const {
00056         unsigned charIndex;
00057         for (charIndex = 0; charIndex < characteristicCount; charIndex++) {
00058             if (nrfCharacteristicHandles[charIndex].value_handle == valueHandle) {
00059                 return charIndex;
00060             }
00061         }
00062 
00063         return -1;
00064     }
00065 
00066     /**
00067      * resolve a CCCD attribute handle to its owning characteristic.
00068      * @param  cccdHandle the CCCD handle to be resolved.
00069      * @return             characteristic index if a resolution is found, else -1.
00070      */
00071     int resolveCCCDHandleToCharIndex(GattAttribute::Handle_t cccdHandle) const {
00072         unsigned charIndex;
00073         for (charIndex = 0; charIndex < characteristicCount; charIndex++) {
00074             if (nrfCharacteristicHandles[charIndex].cccd_handle == cccdHandle) {
00075                 return charIndex;
00076             }
00077         }
00078 
00079         return -1;
00080     }
00081 
00082 private:
00083     GattCharacteristic       *p_characteristics[BLE_TOTAL_CHARACTERISTICS];
00084     ble_gatts_char_handles_t  nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS];
00085     GattAttribute            *p_descriptors[BLE_TOTAL_DESCRIPTORS];
00086     uint8_t                   descriptorCount;
00087     uint16_t                  nrfDescriptorHandles[BLE_TOTAL_DESCRIPTORS];
00088 
00089     /*
00090      * Allow instantiation from nRF5xn when required.
00091      */
00092     friend class nRF5xn;
00093 
00094     nRF5xGattServer() : GattServer(), p_characteristics(), nrfCharacteristicHandles(), p_descriptors(), descriptorCount(0), nrfDescriptorHandles() {
00095         /* empty */
00096     }
00097 
00098 private:
00099     nRF5xGattServer(const nRF5xGattServer &);
00100     const nRF5xGattServer& operator=(const nRF5xGattServer &);
00101 };
00102 
00103 #endif // ifndef __NRF51822_GATT_SERVER_H__