fork

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF51GattClient.h Source File

nRF51GattClient.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 "nRF51ServiceDiscovery.h"
00022 
00023 class nRF51GattClient : public GattClient
00024 {
00025 public:
00026     static nRF51GattClient &getInstance();
00027 
00028     /**
00029      * Launch service discovery. Once launched, service discovery will remain
00030      * active with callbacks being issued back into the application for matching
00031      * services/characteristics. isActive() can be used to determine status; and
00032      * a termination callback (if setup) will be invoked at the end. Service
00033      * discovery can be terminated prematurely if needed using terminate().
00034      *
00035      * @param  connectionHandle
00036      *           Handle for the connection with the peer.
00037      * @param  sc
00038      *           This is the application callback for matching service. Taken as
00039      *           NULL by default. Note: service discovery may still be active
00040      *           when this callback is issued; calling asynchronous BLE-stack
00041      *           APIs from within this application callback might cause the
00042      *           stack to abort service discovery. If this becomes an issue, it
00043      *           may be better to make local copy of the discoveredService and
00044      *           wait for service discovery to terminate before operating on the
00045      *           service.
00046      * @param  cc
00047      *           This is the application callback for matching characteristic.
00048      *           Taken as NULL by default. Note: service discovery may still be
00049      *           active when this callback is issued; calling asynchronous
00050      *           BLE-stack APIs from within this application callback might cause
00051      *           the stack to abort service discovery. If this becomes an issue,
00052      *           it may be better to make local copy of the discoveredCharacteristic
00053      *           and wait for service discovery to terminate before operating on the
00054      *           characteristic.
00055      * @param  matchingServiceUUID
00056      *           UUID based filter for specifying a service in which the application is
00057      *           interested. By default it is set as the wildcard UUID_UNKNOWN,
00058      *           in which case it matches all services. If characteristic-UUID
00059      *           filter (below) is set to the wildcard value, then a service
00060      *           callback will be invoked for the matching service (or for every
00061      *           service if the service filter is a wildcard).
00062      * @param  matchingCharacteristicUUIDIn
00063      *           UUID based filter for specifying characteristic in which the application
00064      *           is interested. By default it is set as the wildcard UUID_UKNOWN
00065      *           to match against any characteristic. If both service-UUID
00066      *           filter and characteristic-UUID filter are used with non- wildcard
00067      *           values, then only a single characteristic callback is
00068      *           invoked for the matching characteristic.
00069      *
00070      * @Note     Using wildcard values for both service-UUID and characteristic-
00071      *           UUID will result in complete service discovery--callbacks being
00072      *           called for every service and characteristic.
00073      *
00074      * @return
00075      *           BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
00076      */
00077     virtual ble_error_t launchServiceDiscovery(Gap::Handle_t                               connectionHandle,
00078                                                ServiceDiscovery::ServiceCallback_t         sc = NULL,
00079                                                ServiceDiscovery::CharacteristicCallback_t  cc = NULL,
00080                                                const UUID                                 &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
00081                                                const UUID                                 &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
00082 
00083     virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) {
00084         discovery.onTermination(callback);
00085     }
00086 
00087     /**
00088      * Is service-discovery currently active?
00089      */
00090     virtual bool isServiceDiscoveryActive(void) const {
00091         return discovery.isActive();
00092     }
00093 
00094     /**
00095      * Terminate an ongoing service-discovery. This should result in an
00096      * invocation of the TerminationCallback if service-discovery is active.
00097      */
00098     virtual void terminateServiceDiscovery(void) {
00099         discovery.terminate();
00100     }
00101 
00102     virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const {
00103         uint32_t rc = sd_ble_gattc_read(connHandle, attributeHandle, offset);
00104         if (rc == NRF_SUCCESS) {
00105             return BLE_ERROR_NONE;
00106         }
00107         switch (rc) {
00108             case NRF_ERROR_BUSY:
00109                 return BLE_STACK_BUSY;
00110             case BLE_ERROR_INVALID_CONN_HANDLE:
00111             case NRF_ERROR_INVALID_STATE:
00112             case NRF_ERROR_INVALID_ADDR:
00113             default:
00114                 return BLE_ERROR_INVALID_STATE;
00115         }
00116     }
00117 
00118     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 {
00119         ble_gattc_write_params_t writeParams = { };
00120         writeParams.write_op = cmd;
00121         writeParams.handle   = attributeHandle;
00122         writeParams.len      = length;
00123         writeParams.p_value  = const_cast<uint8_t *>(value);
00124 
00125         uint32_t rc = sd_ble_gattc_write(connHandle, &writeParams);
00126         if (rc == NRF_SUCCESS) {
00127             return BLE_ERROR_NONE;
00128         }
00129         switch (rc) {
00130             case NRF_ERROR_BUSY:
00131                 return BLE_STACK_BUSY;
00132             case BLE_ERROR_NO_TX_BUFFERS:
00133                 return BLE_ERROR_NO_MEM;
00134             case BLE_ERROR_INVALID_CONN_HANDLE:
00135             case NRF_ERROR_INVALID_STATE:
00136             case NRF_ERROR_INVALID_ADDR:
00137             default:
00138                 return BLE_ERROR_INVALID_STATE;
00139         }
00140     }
00141 
00142 public:
00143     nRF51GattClient() : discovery(this) {
00144         /* empty */
00145     }
00146 
00147     friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
00148 
00149 private:
00150     nRF51GattClient(const nRF51GattClient &);
00151     const nRF51GattClient& operator=(const nRF51GattClient &);
00152 
00153 private:
00154     nRF51ServiceDiscovery discovery;
00155 };
00156 
00157 #endif // ifndef __NRF51822_GATT_CLIENT_H__