|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef __DISCOVERED_CHARACTERISTIC_H__ |
| 18 | +#define __DISCOVERED_CHARACTERISTIC_H__ |
| 19 | + |
| 20 | +#include "UUID.h" |
| 21 | +#include "Gap.h" |
| 22 | +#include "GattAttribute.h" |
| 23 | +#include "GattClient.h" |
| 24 | + |
| 25 | +/** |
| 26 | + * Structure for holding information about the service and the characteristics |
| 27 | + * found during the discovery process. |
| 28 | + */ |
| 29 | +class DiscoveredCharacteristic { |
| 30 | +public: |
| 31 | + struct Properties_t { |
| 32 | + uint8_t _broadcast :1; /**< Broadcasting of the value permitted. */ |
| 33 | + uint8_t _read :1; /**< Reading the value permitted. */ |
| 34 | + uint8_t _writeWoResp :1; /**< Writing the value with Write Command permitted. */ |
| 35 | + uint8_t _write :1; /**< Writing the value with Write Request permitted. */ |
| 36 | + uint8_t _notify :1; /**< Notications of the value permitted. */ |
| 37 | + uint8_t _indicate :1; /**< Indications of the value permitted. */ |
| 38 | + uint8_t _authSignedWrite :1; /**< Writing the value with Signed Write Command permitted. */ |
| 39 | + |
| 40 | + public: |
| 41 | + bool broadcast(void) const {return _broadcast; } |
| 42 | + bool read(void) const {return _read; } |
| 43 | + bool writeWoResp(void) const {return _writeWoResp; } |
| 44 | + bool write(void) const {return _write; } |
| 45 | + bool notify(void) const {return _notify; } |
| 46 | + bool indicate(void) const {return _indicate; } |
| 47 | + bool authSignedWrite(void) const {return _authSignedWrite;} |
| 48 | + |
| 49 | + private: |
| 50 | + operator uint8_t() const; /* disallow implicit conversion into an integer */ |
| 51 | + operator unsigned() const; /* disallow implicit conversion into an integer */ |
| 52 | + }; |
| 53 | + |
| 54 | + /** |
| 55 | + * Structure for holding information about the service and the characteristics |
| 56 | + * found during the discovery process. |
| 57 | + */ |
| 58 | + struct DiscoveredDescriptor { |
| 59 | + GattAttribute::Handle_t handle; /**< Descriptor Handle. */ |
| 60 | + UUID uuid; /**< Descriptor UUID. */ |
| 61 | + }; |
| 62 | + |
| 63 | + /** |
| 64 | + * Callback type for when a characteristic descriptor is found during descriptor- |
| 65 | + * discovery. The receiving function is passed in a pointer to a |
| 66 | + * DiscoveredDescriptor object which will remain valid for the lifetime |
| 67 | + * of the callback. Memory for this object is owned by the BLE_API eventing |
| 68 | + * framework. The application can safely make a persistent shallow-copy of |
| 69 | + * this object in order to work with the characteristic beyond the callback. |
| 70 | + */ |
| 71 | + typedef void (*DescriptorCallback_t)(const DiscoveredDescriptor *); |
| 72 | + |
| 73 | + /** |
| 74 | + * Initiate (or continue) a read for the value attribute, optionally at a |
| 75 | + * given offset. If the Characteristic or Descriptor to be read is longer |
| 76 | + * than ATT_MTU - 1, this function must be called multiple times with |
| 77 | + * appropriate offset to read the complete value. |
| 78 | + * |
| 79 | + * @return BLE_ERROR_NONE if a read has been initiated, else |
| 80 | + * BLE_ERROR_INVALID_STATE if some internal state about the connection is invalid, or |
| 81 | + * BLE_STACK_BUSY if some client procedure already in progress, or |
| 82 | + * BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties. |
| 83 | + */ |
| 84 | + ble_error_t read(uint16_t offset = 0) const; |
| 85 | + |
| 86 | + /** |
| 87 | + * Perform a write without response procedure. |
| 88 | + * |
| 89 | + * @param length |
| 90 | + * The amount of data being written. |
| 91 | + * @param value |
| 92 | + * The bytes being written. |
| 93 | + * |
| 94 | + * @note It is important to note that a write without response will generate |
| 95 | + * an onDataSent() callback when the packet has been transmitted. There |
| 96 | + * will be a BLE-stack specific limit to the number of pending |
| 97 | + * writeWoResponse operations; the user may want to use the onDataSent() |
| 98 | + * callback for flow-control. |
| 99 | + * |
| 100 | + * @retval BLE_ERROR_NONE Successfully started the Write procedure, else |
| 101 | + * BLE_ERROR_INVALID_STATE if some internal state about the connection is invalid, or |
| 102 | + * BLE_STACK_BUSY if some client procedure already in progress, or |
| 103 | + * BLE_ERROR_NO_MEM if there are no available buffers left to process the request, or |
| 104 | + * BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties. |
| 105 | + */ |
| 106 | + ble_error_t writeWoResponse(uint16_t length, const uint8_t *value) const; |
| 107 | + |
| 108 | + /** |
| 109 | + * Initiate a GATT Characteristic Descriptor Discovery procedure for descriptors within this characteristic. |
| 110 | + * |
| 111 | + * @param callback |
| 112 | + * @param matchingUUID |
| 113 | + * filter for descriptors. Defaults to wildcard which will discover all descriptors. |
| 114 | + * |
| 115 | + * @return BLE_ERROR_NONE if descriptor discovery is launched successfully; else an appropriate error. |
| 116 | + */ |
| 117 | + ble_error_t discoverDescriptors(DescriptorCallback_t callback, const UUID &matchingUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) const; |
| 118 | + |
| 119 | + /** |
| 120 | + * Perform a write procedure. |
| 121 | + * |
| 122 | + * @param length |
| 123 | + * The amount of data being written. |
| 124 | + * @param value |
| 125 | + * The bytes being written. |
| 126 | + * |
| 127 | + * @note It is important to note that a write will generate |
| 128 | + * an onDataWritten() callback when the peer acknowledges the request. |
| 129 | + * |
| 130 | + * @retval BLE_ERROR_NONE Successfully started the Write procedure, else |
| 131 | + * BLE_ERROR_INVALID_STATE if some internal state about the connection is invalid, or |
| 132 | + * BLE_STACK_BUSY if some client procedure already in progress, or |
| 133 | + * BLE_ERROR_NO_MEM if there are no available buffers left to process the request, or |
| 134 | + * BLE_ERROR_OPERATION_NOT_PERMITTED due to the characteristic's properties. |
| 135 | + */ |
| 136 | + ble_error_t write(uint16_t length, const uint8_t *value) const; |
| 137 | + |
| 138 | + static void setupOnDataRead(GattClient::ReadCallback_t callback) { |
| 139 | + onDataReadCallback = callback; |
| 140 | + } |
| 141 | + |
| 142 | + static void setupOnDataWrite(GattClient::WriteCallback_t callback) { |
| 143 | + onDataWriteCallback = callback; |
| 144 | + } |
| 145 | + |
| 146 | + void setupLongUUID(UUID::LongUUIDBytes_t longUUID) { |
| 147 | + uuid.setupLong(longUUID); |
| 148 | + } |
| 149 | + |
| 150 | +public: |
| 151 | + UUID::ShortUUIDBytes_t getShortUUID(void) const { |
| 152 | + return uuid.getShortUUID(); |
| 153 | + } |
| 154 | + |
| 155 | + const Properties_t& getProperties(void) const { |
| 156 | + return props; |
| 157 | + } |
| 158 | + |
| 159 | + const GattAttribute::Handle_t& getDeclHandle(void) const { |
| 160 | + return declHandle; |
| 161 | + } |
| 162 | + const GattAttribute::Handle_t& getValueHandle(void) const { |
| 163 | + return valueHandle; |
| 164 | + } |
| 165 | + |
| 166 | +public: |
| 167 | + DiscoveredCharacteristic() : gattc(NULL), |
| 168 | + uuid(UUID::ShortUUIDBytes_t(0)), |
| 169 | + props(), |
| 170 | + declHandle(GattAttribute::INVALID_HANDLE), |
| 171 | + valueHandle(GattAttribute::INVALID_HANDLE) { |
| 172 | + /* empty */ |
| 173 | + } |
| 174 | + |
| 175 | +protected: |
| 176 | + GattClient *gattc; |
| 177 | + |
| 178 | +protected: |
| 179 | + UUID uuid; |
| 180 | + Properties_t props; |
| 181 | + GattAttribute::Handle_t declHandle; |
| 182 | + GattAttribute::Handle_t valueHandle; |
| 183 | + |
| 184 | + Gap::Handle_t connHandle; |
| 185 | + |
| 186 | +public: |
| 187 | + static GattClient::ReadCallback_t onDataReadCallback; |
| 188 | + static GattClient::WriteCallback_t onDataWriteCallback; |
| 189 | +}; |
| 190 | + |
| 191 | +#endif /*__DISCOVERED_CHARACTERISTIC_H__*/ |
0 commit comments