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