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