Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF5xGattClient.h Source File

nRF5xGattClient.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_CLIENT_H__
00018 #define __NRF51822_GATT_CLIENT_H__
00019 
00020 #include "ble/GattClient.h"
00021 #include "nRF5xServiceDiscovery.h"
00022 #include "nRF5xCharacteristicDescriptorDiscoverer.h"
00023 
00024 class nRF5xGattClient : public GattClient
00025 {
00026 public:
00027     /**
00028      * When using S110, all Gatt client features will return
00029      * BLE_ERROR_NOT_IMPLEMENTED
00030      */
00031 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
00032 
00033     /**
00034      * Launch service discovery. Once launched, service discovery will remain
00035      * active with callbacks being issued back into the application for matching
00036      * services/characteristics. isActive() can be used to determine status; and
00037      * a termination callback (if setup) will be invoked at the end. Service
00038      * discovery can be terminated prematurely if needed using terminate().
00039      *
00040      * @param  connectionHandle
00041      *           Handle for the connection with the peer.
00042      * @param  sc
00043      *           This is the application callback for matching service. Taken as
00044      *           NULL by default. Note: service discovery may still be active
00045      *           when this callback is issued; calling asynchronous BLE-stack
00046      *           APIs from within this application callback might cause the
00047      *           stack to abort service discovery. If this becomes an issue, it
00048      *           may be better to make local copy of the discoveredService and
00049      *           wait for service discovery to terminate before operating on the
00050      *           service.
00051      * @param  cc
00052      *           This is the application callback for matching characteristic.
00053      *           Taken as NULL by default. Note: service discovery may still be
00054      *           active when this callback is issued; calling asynchronous
00055      *           BLE-stack APIs from within this application callback might cause
00056      *           the stack to abort service discovery. If this becomes an issue,
00057      *           it may be better to make local copy of the discoveredCharacteristic
00058      *           and wait for service discovery to terminate before operating on the
00059      *           characteristic.
00060      * @param  matchingServiceUUID
00061      *           UUID based filter for specifying a service in which the application is
00062      *           interested. By default it is set as the wildcard UUID_UNKNOWN,
00063      *           in which case it matches all services. If characteristic-UUID
00064      *           filter (below) is set to the wildcard value, then a service
00065      *           callback will be invoked for the matching service (or for every
00066      *           service if the service filter is a wildcard).
00067      * @param  matchingCharacteristicUUIDIn
00068      *           UUID based filter for specifying characteristic in which the application
00069      *           is interested. By default it is set as the wildcard UUID_UKNOWN
00070      *           to match against any characteristic. If both service-UUID
00071      *           filter and characteristic-UUID filter are used with non- wildcard
00072      *           values, then only a single characteristic callback is
00073      *           invoked for the matching characteristic.
00074      *
00075      * @Note     Using wildcard values for both service-UUID and characteristic-
00076      *           UUID will result in complete service discovery--callbacks being
00077      *           called for every service and characteristic.
00078      *
00079      * @return
00080      *           BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
00081      */
00082     virtual ble_error_t launchServiceDiscovery(Gap::Handle_t                               connectionHandle,
00083                                                ServiceDiscovery::ServiceCallback_t         sc = NULL,
00084                                                ServiceDiscovery::CharacteristicCallback_t  cc = NULL,
00085                                                const UUID                                 &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
00086                                                const UUID                                 &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
00087 
00088     virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) {
00089         _discovery.onTermination(callback);
00090     }
00091 
00092     /**
00093      * Is service-discovery currently active?
00094      */
00095     virtual bool isServiceDiscoveryActive(void) const {
00096         return _discovery.isActive();
00097     }
00098 
00099     /**
00100      * Terminate an ongoing service-discovery. This should result in an
00101      * invocation of the TerminationCallback if service-discovery is active.
00102      */
00103     virtual void terminateServiceDiscovery(void) {
00104         _discovery.terminate();
00105     }
00106 
00107     /**
00108      * @brief Implementation of GattClient::discoverCharacteristicDescriptors
00109      * @see GattClient::discoverCharacteristicDescriptors
00110      */
00111     virtual ble_error_t discoverCharacteristicDescriptors(
00112         const DiscoveredCharacteristic& characteristic,
00113         const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
00114         const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
00115     );
00116 
00117     /**
00118      * @brief Implementation of GattClient::isCharacteristicDiscoveryActive
00119      * @see GattClient::isCharacteristicDiscoveryActive
00120      */
00121     virtual bool isCharacteristicDescriptorsDiscoveryActive(const DiscoveredCharacteristic& characteristic) const;
00122 
00123     /**
00124      * @brief Implementation of GattClient::terminateCharacteristicDiscovery
00125      * @see GattClient::terminateCharacteristicDiscovery
00126      */
00127     virtual void terminateCharacteristicDescriptorsDiscovery(const DiscoveredCharacteristic& characteristic);
00128 
00129     virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const {
00130         uint32_t rc = sd_ble_gattc_read(connHandle, attributeHandle, offset);
00131         if (rc == NRF_SUCCESS) {
00132             return BLE_ERROR_NONE;
00133         }
00134         switch (rc) {
00135             case NRF_ERROR_BUSY:
00136                 return BLE_STACK_BUSY;
00137             case BLE_ERROR_INVALID_CONN_HANDLE:
00138             case NRF_ERROR_INVALID_STATE:
00139             case NRF_ERROR_INVALID_ADDR:
00140             default:
00141                 return BLE_ERROR_INVALID_STATE;
00142         }
00143     }
00144 
00145     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 {
00146         ble_gattc_write_params_t writeParams;
00147         writeParams.write_op = cmd;
00148         writeParams.flags    = 0; /* this is inconsequential */
00149         writeParams.handle   = attributeHandle;
00150         writeParams.offset   = 0;
00151         writeParams.len      = length;
00152         writeParams.p_value  = const_cast<uint8_t *>(value);
00153 
00154         uint32_t rc = sd_ble_gattc_write(connHandle, &writeParams);
00155         if (rc == NRF_SUCCESS) {
00156             return BLE_ERROR_NONE;
00157         }
00158         switch (rc) {
00159             case NRF_ERROR_BUSY:
00160                 return BLE_STACK_BUSY;
00161             case BLE_ERROR_NO_TX_BUFFERS:
00162                 return BLE_ERROR_NO_MEM;
00163             case BLE_ERROR_INVALID_CONN_HANDLE:
00164             case NRF_ERROR_INVALID_STATE:
00165             case NRF_ERROR_INVALID_ADDR:
00166             default:
00167                 return BLE_ERROR_INVALID_STATE;
00168         }
00169     }
00170 
00171     /**
00172      * @brief  Clear nRF5xGattClient's state.
00173      *
00174      * @return
00175      *           BLE_ERROR_NONE if successful.
00176      */
00177     virtual ble_error_t reset(void) {
00178         /* Clear all state that is from the parent, including private members */
00179         if (GattClient::reset() != BLE_ERROR_NONE) {
00180             return BLE_ERROR_INVALID_STATE;
00181         }
00182 
00183         /* Clear derived class members */
00184         _discovery.reset();
00185 
00186         return BLE_ERROR_NONE;
00187     }
00188 
00189 public:
00190     /*
00191      * Allow instantiation from nRF5xn when required.
00192      */
00193     friend class nRF5xn;
00194 
00195     nRF5xGattClient() : _discovery(this) {
00196         /* empty */
00197     }
00198 
00199     nRF5xServiceDiscovery& discovery() {
00200         return _discovery;
00201     }
00202 
00203     nRF5xCharacteristicDescriptorDiscoverer& characteristicDescriptorDiscoverer() {
00204         return _characteristicDescriptorDiscoverer;
00205     }
00206 
00207 private:
00208     nRF5xGattClient(const nRF5xGattClient &);
00209     const nRF5xGattClient& operator=(const nRF5xGattClient &);
00210 
00211 private:
00212     nRF5xServiceDiscovery _discovery;
00213     nRF5xCharacteristicDescriptorDiscoverer _characteristicDescriptorDiscoverer;
00214 
00215 #endif // if !S110
00216 };
00217 
00218 #endif // ifndef __NRF51822_GATT_CLIENT_H__