fork
Fork of nRF51822 by
nRF51GattClient.h@337:3d8b3bfe22e1, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:33 2015 +0100
- Revision:
- 337:3d8b3bfe22e1
- Parent:
- 334:5dcd58729c5b
- Child:
- 339:e5e2157e8b44
Synchronized with git rev 16137b58
Author: Rohit Grover
we now have a working nRFGattClient::write()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 328:f3aa0fddaa79 | 1 | /* mbed Microcontroller Library |
rgrover1 | 328:f3aa0fddaa79 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 328:f3aa0fddaa79 | 3 | * |
rgrover1 | 328:f3aa0fddaa79 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 328:f3aa0fddaa79 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 328:f3aa0fddaa79 | 6 | * You may obtain a copy of the License at |
rgrover1 | 328:f3aa0fddaa79 | 7 | * |
rgrover1 | 328:f3aa0fddaa79 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 328:f3aa0fddaa79 | 9 | * |
rgrover1 | 328:f3aa0fddaa79 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 328:f3aa0fddaa79 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 328:f3aa0fddaa79 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 328:f3aa0fddaa79 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 328:f3aa0fddaa79 | 14 | * limitations under the License. |
rgrover1 | 328:f3aa0fddaa79 | 15 | */ |
rgrover1 | 328:f3aa0fddaa79 | 16 | |
rgrover1 | 328:f3aa0fddaa79 | 17 | #ifndef __NRF51822_GATT_CLIENT_H__ |
rgrover1 | 328:f3aa0fddaa79 | 18 | #define __NRF51822_GATT_CLIENT_H__ |
rgrover1 | 328:f3aa0fddaa79 | 19 | |
rgrover1 | 328:f3aa0fddaa79 | 20 | #include "GattClient.h" |
rgrover1 | 331:352bc4a36ea0 | 21 | #include "nRFServiceDiscovery.h" |
rgrover1 | 328:f3aa0fddaa79 | 22 | |
rgrover1 | 328:f3aa0fddaa79 | 23 | class nRF51GattClient : public GattClient |
rgrover1 | 328:f3aa0fddaa79 | 24 | { |
rgrover1 | 328:f3aa0fddaa79 | 25 | public: |
rgrover1 | 328:f3aa0fddaa79 | 26 | static nRF51GattClient &getInstance(); |
rgrover1 | 328:f3aa0fddaa79 | 27 | |
rgrover1 | 332:b054000833d4 | 28 | /** |
rgrover1 | 332:b054000833d4 | 29 | * Launch service discovery. Once launched, service discovery will remain |
rgrover1 | 332:b054000833d4 | 30 | * active with callbacks being issued back into the application for matching |
rgrover1 | 332:b054000833d4 | 31 | * services/characteristics. isActive() can be used to determine status; and |
rgrover1 | 332:b054000833d4 | 32 | * a termination callback (if setup) will be invoked at the end. Service |
rgrover1 | 332:b054000833d4 | 33 | * discovery can be terminated prematurely if needed using terminate(). |
rgrover1 | 332:b054000833d4 | 34 | * |
rgrover1 | 332:b054000833d4 | 35 | * @param connectionHandle |
rgrover1 | 332:b054000833d4 | 36 | * Handle for the connection with the peer. |
rgrover1 | 332:b054000833d4 | 37 | * @param sc |
rgrover1 | 332:b054000833d4 | 38 | * This is the application callback for matching service. Taken as |
rgrover1 | 332:b054000833d4 | 39 | * NULL by default. Note: service discovery may still be active |
rgrover1 | 332:b054000833d4 | 40 | * when this callback is issued; calling asynchronous BLE-stack |
rgrover1 | 332:b054000833d4 | 41 | * APIs from within this application callback might cause the |
rgrover1 | 332:b054000833d4 | 42 | * stack to abort service discovery. If this becomes an issue, it |
rgrover1 | 332:b054000833d4 | 43 | * may be better to make local copy of the discoveredService and |
rgrover1 | 332:b054000833d4 | 44 | * wait for service discovery to terminate before operating on the |
rgrover1 | 332:b054000833d4 | 45 | * service. |
rgrover1 | 332:b054000833d4 | 46 | * @param cc |
rgrover1 | 332:b054000833d4 | 47 | * This is the application callback for matching characteristic. |
rgrover1 | 332:b054000833d4 | 48 | * Taken as NULL by default. Note: service discovery may still be |
rgrover1 | 332:b054000833d4 | 49 | * active when this callback is issued; calling asynchronous |
rgrover1 | 332:b054000833d4 | 50 | * BLE-stack APIs from within this application callback might cause |
rgrover1 | 332:b054000833d4 | 51 | * the stack to abort service discovery. If this becomes an issue, |
rgrover1 | 332:b054000833d4 | 52 | * it may be better to make local copy of the discoveredCharacteristic |
rgrover1 | 332:b054000833d4 | 53 | * and wait for service discovery to terminate before operating on the |
rgrover1 | 332:b054000833d4 | 54 | * characteristic. |
rgrover1 | 332:b054000833d4 | 55 | * @param matchingServiceUUID |
rgrover1 | 332:b054000833d4 | 56 | * UUID based filter for specifying a service in which the application is |
rgrover1 | 332:b054000833d4 | 57 | * interested. By default it is set as the wildcard UUID_UNKNOWN, |
rgrover1 | 332:b054000833d4 | 58 | * in which case it matches all services. If characteristic-UUID |
rgrover1 | 332:b054000833d4 | 59 | * filter (below) is set to the wildcard value, then a service |
rgrover1 | 332:b054000833d4 | 60 | * callback will be invoked for the matching service (or for every |
rgrover1 | 332:b054000833d4 | 61 | * service if the service filter is a wildcard). |
rgrover1 | 332:b054000833d4 | 62 | * @param matchingCharacteristicUUIDIn |
rgrover1 | 332:b054000833d4 | 63 | * UUID based filter for specifying characteristic in which the application |
rgrover1 | 332:b054000833d4 | 64 | * is interested. By default it is set as the wildcard UUID_UKNOWN |
rgrover1 | 332:b054000833d4 | 65 | * to match against any characteristic. If both service-UUID |
rgrover1 | 332:b054000833d4 | 66 | * filter and characteristic-UUID filter are used with non- wildcard |
rgrover1 | 332:b054000833d4 | 67 | * values, then only a single characteristic callback is |
rgrover1 | 332:b054000833d4 | 68 | * invoked for the matching characteristic. |
rgrover1 | 332:b054000833d4 | 69 | * |
rgrover1 | 332:b054000833d4 | 70 | * @Note Using wildcard values for both service-UUID and characteristic- |
rgrover1 | 332:b054000833d4 | 71 | * UUID will result in complete service discovery--callbacks being |
rgrover1 | 332:b054000833d4 | 72 | * called for every service and characteristic. |
rgrover1 | 332:b054000833d4 | 73 | * |
rgrover1 | 332:b054000833d4 | 74 | * @return |
rgrover1 | 332:b054000833d4 | 75 | * BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error. |
rgrover1 | 332:b054000833d4 | 76 | */ |
rgrover1 | 332:b054000833d4 | 77 | virtual ble_error_t launchServiceDiscovery(Gap::Handle_t connectionHandle, |
rgrover1 | 332:b054000833d4 | 78 | ServiceDiscovery::ServiceCallback_t sc = NULL, |
rgrover1 | 332:b054000833d4 | 79 | ServiceDiscovery::CharacteristicCallback_t cc = NULL, |
rgrover1 | 332:b054000833d4 | 80 | const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN), |
rgrover1 | 332:b054000833d4 | 81 | const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)); |
rgrover1 | 332:b054000833d4 | 82 | |
rgrover1 | 332:b054000833d4 | 83 | virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) { |
rgrover1 | 332:b054000833d4 | 84 | discovery.onTermination(callback); |
rgrover1 | 332:b054000833d4 | 85 | } |
rgrover1 | 332:b054000833d4 | 86 | |
rgrover1 | 332:b054000833d4 | 87 | /** |
rgrover1 | 332:b054000833d4 | 88 | * Is service-discovery currently active? |
rgrover1 | 332:b054000833d4 | 89 | */ |
rgrover1 | 332:b054000833d4 | 90 | virtual bool isServiceDiscoveryActive(void) const { |
rgrover1 | 332:b054000833d4 | 91 | return discovery.isActive(); |
rgrover1 | 332:b054000833d4 | 92 | } |
rgrover1 | 332:b054000833d4 | 93 | |
rgrover1 | 332:b054000833d4 | 94 | /** |
rgrover1 | 332:b054000833d4 | 95 | * Terminate an ongoing service-discovery. This should result in an |
rgrover1 | 332:b054000833d4 | 96 | * invocation of the TerminationCallback if service-discovery is active. |
rgrover1 | 332:b054000833d4 | 97 | */ |
rgrover1 | 332:b054000833d4 | 98 | virtual void terminateServiceDiscovery(void) { |
rgrover1 | 332:b054000833d4 | 99 | discovery.terminate(); |
rgrover1 | 332:b054000833d4 | 100 | } |
rgrover1 | 332:b054000833d4 | 101 | |
rgrover1 | 329:d11ef9cc041b | 102 | virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const { |
rgrover1 | 329:d11ef9cc041b | 103 | uint32_t rc = sd_ble_gattc_read(connHandle, attributeHandle, offset); |
rgrover1 | 329:d11ef9cc041b | 104 | if (rc == NRF_SUCCESS) { |
rgrover1 | 329:d11ef9cc041b | 105 | return BLE_ERROR_NONE; |
rgrover1 | 329:d11ef9cc041b | 106 | } |
rgrover1 | 329:d11ef9cc041b | 107 | switch (rc) { |
rgrover1 | 329:d11ef9cc041b | 108 | case NRF_ERROR_BUSY: |
rgrover1 | 329:d11ef9cc041b | 109 | return BLE_STACK_BUSY; |
rgrover1 | 329:d11ef9cc041b | 110 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 329:d11ef9cc041b | 111 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 329:d11ef9cc041b | 112 | case NRF_ERROR_INVALID_ADDR: |
rgrover1 | 329:d11ef9cc041b | 113 | default: |
rgrover1 | 329:d11ef9cc041b | 114 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 329:d11ef9cc041b | 115 | } |
rgrover1 | 329:d11ef9cc041b | 116 | } |
rgrover1 | 329:d11ef9cc041b | 117 | |
rgrover1 | 337:3d8b3bfe22e1 | 118 | virtual ble_error_t write(GattClient::WriteOp_t cmd, Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, size_t length, const uint8_t *value) const { |
rgrover1 | 337:3d8b3bfe22e1 | 119 | ble_gattc_write_params_t writeParams = { |
rgrover1 | 337:3d8b3bfe22e1 | 120 | .write_op = cmd, |
rgrover1 | 337:3d8b3bfe22e1 | 121 | // .flags = 0, |
rgrover1 | 337:3d8b3bfe22e1 | 122 | .handle = attributeHandle, |
rgrover1 | 337:3d8b3bfe22e1 | 123 | .offset = 0, |
rgrover1 | 337:3d8b3bfe22e1 | 124 | .len = length, |
rgrover1 | 337:3d8b3bfe22e1 | 125 | .p_value = const_cast<uint8_t *>(value), |
rgrover1 | 337:3d8b3bfe22e1 | 126 | }; |
rgrover1 | 337:3d8b3bfe22e1 | 127 | |
rgrover1 | 337:3d8b3bfe22e1 | 128 | uint32_t rc = sd_ble_gattc_write(connHandle, &writeParams); |
rgrover1 | 337:3d8b3bfe22e1 | 129 | if (rc == NRF_SUCCESS) { |
rgrover1 | 337:3d8b3bfe22e1 | 130 | return BLE_ERROR_NONE; |
rgrover1 | 337:3d8b3bfe22e1 | 131 | } |
rgrover1 | 337:3d8b3bfe22e1 | 132 | switch (rc) { |
rgrover1 | 337:3d8b3bfe22e1 | 133 | case NRF_ERROR_BUSY: |
rgrover1 | 337:3d8b3bfe22e1 | 134 | return BLE_STACK_BUSY; |
rgrover1 | 337:3d8b3bfe22e1 | 135 | case BLE_ERROR_NO_TX_BUFFERS: |
rgrover1 | 337:3d8b3bfe22e1 | 136 | return BLE_ERROR_NO_MEM; |
rgrover1 | 337:3d8b3bfe22e1 | 137 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 337:3d8b3bfe22e1 | 138 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 337:3d8b3bfe22e1 | 139 | case NRF_ERROR_INVALID_ADDR: |
rgrover1 | 337:3d8b3bfe22e1 | 140 | default: |
rgrover1 | 337:3d8b3bfe22e1 | 141 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 337:3d8b3bfe22e1 | 142 | } |
rgrover1 | 332:b054000833d4 | 143 | } |
rgrover1 | 328:f3aa0fddaa79 | 144 | |
rgrover1 | 328:f3aa0fddaa79 | 145 | public: |
rgrover1 | 333:8eedcd324853 | 146 | nRF51GattClient() : discovery(this) { |
rgrover1 | 328:f3aa0fddaa79 | 147 | /* empty */ |
rgrover1 | 328:f3aa0fddaa79 | 148 | } |
rgrover1 | 328:f3aa0fddaa79 | 149 | |
rgrover1 | 332:b054000833d4 | 150 | friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt); |
rgrover1 | 332:b054000833d4 | 151 | |
rgrover1 | 328:f3aa0fddaa79 | 152 | private: |
rgrover1 | 328:f3aa0fddaa79 | 153 | nRF51GattClient(const nRF51GattClient &); |
rgrover1 | 328:f3aa0fddaa79 | 154 | const nRF51GattClient& operator=(const nRF51GattClient &); |
rgrover1 | 331:352bc4a36ea0 | 155 | |
rgrover1 | 331:352bc4a36ea0 | 156 | private: |
rgrover1 | 331:352bc4a36ea0 | 157 | nRFServiceDiscovery discovery; |
rgrover1 | 328:f3aa0fddaa79 | 158 | }; |
rgrover1 | 328:f3aa0fddaa79 | 159 | |
rgrover1 | 328:f3aa0fddaa79 | 160 | #endif // ifndef __NRF51822_GATT_CLIENT_H__ |