test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

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