sa

Fork of nRF51822 by Nordic Semiconductor

Committer:
nakamae
Date:
Thu Dec 29 07:05:48 2016 +0000
Revision:
639:fdeb2820ef26
Parent:
638:c90ae1400bf2
new;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 638:c90ae1400bf2 1 /* mbed Microcontroller Library
Vincent Coubard 638:c90ae1400bf2 2 * Copyright (c) 2006-2013 ARM Limited
Vincent Coubard 638:c90ae1400bf2 3 *
Vincent Coubard 638:c90ae1400bf2 4 * Licensed under the Apache License, Version 2.0 (the "License");
Vincent Coubard 638:c90ae1400bf2 5 * you may not use this file except in compliance with the License.
Vincent Coubard 638:c90ae1400bf2 6 * You may obtain a copy of the License at
Vincent Coubard 638:c90ae1400bf2 7 *
Vincent Coubard 638:c90ae1400bf2 8 * http://www.apache.org/licenses/LICENSE-2.0
Vincent Coubard 638:c90ae1400bf2 9 *
Vincent Coubard 638:c90ae1400bf2 10 * Unless required by applicable law or agreed to in writing, software
Vincent Coubard 638:c90ae1400bf2 11 * distributed under the License is distributed on an "AS IS" BASIS,
Vincent Coubard 638:c90ae1400bf2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Vincent Coubard 638:c90ae1400bf2 13 * See the License for the specific language governing permissions and
Vincent Coubard 638:c90ae1400bf2 14 * limitations under the License.
Vincent Coubard 638:c90ae1400bf2 15 */
Vincent Coubard 638:c90ae1400bf2 16
Vincent Coubard 638:c90ae1400bf2 17 #include "nRF5xServiceDiscovery.h"
Vincent Coubard 638:c90ae1400bf2 18 #include "nRF5xCharacteristicDescriptorDiscoverer.h"
Vincent Coubard 638:c90ae1400bf2 19 #include "nRF5xGattClient.h"
Vincent Coubard 638:c90ae1400bf2 20 #include "nRF5xn.h"
Vincent Coubard 638:c90ae1400bf2 21
Vincent Coubard 638:c90ae1400bf2 22 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
Vincent Coubard 638:c90ae1400bf2 23 void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
Vincent Coubard 638:c90ae1400bf2 24 {
Vincent Coubard 638:c90ae1400bf2 25 nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
Vincent Coubard 638:c90ae1400bf2 26 nRF5xGap &gap = (nRF5xGap &) ble.getGap();
Vincent Coubard 638:c90ae1400bf2 27 nRF5xGattClient &gattClient = (nRF5xGattClient &) ble.getGattClient();
Vincent Coubard 638:c90ae1400bf2 28 nRF5xServiceDiscovery &sdSingleton = gattClient.discovery();
Vincent Coubard 638:c90ae1400bf2 29 nRF5xCharacteristicDescriptorDiscoverer &characteristicDescriptorDiscoverer =
Vincent Coubard 638:c90ae1400bf2 30 gattClient.characteristicDescriptorDiscoverer();
Vincent Coubard 638:c90ae1400bf2 31
Vincent Coubard 638:c90ae1400bf2 32 switch (p_ble_evt->header.evt_id) {
Vincent Coubard 638:c90ae1400bf2 33 case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
Vincent Coubard 638:c90ae1400bf2 34 switch (p_ble_evt->evt.gattc_evt.gatt_status) {
Vincent Coubard 638:c90ae1400bf2 35 case BLE_GATT_STATUS_SUCCESS:
Vincent Coubard 638:c90ae1400bf2 36 sdSingleton.setupDiscoveredServices(&p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp);
Vincent Coubard 638:c90ae1400bf2 37 break;
Vincent Coubard 638:c90ae1400bf2 38
Vincent Coubard 638:c90ae1400bf2 39 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
Vincent Coubard 638:c90ae1400bf2 40 default:
Vincent Coubard 638:c90ae1400bf2 41 sdSingleton.terminate();
Vincent Coubard 638:c90ae1400bf2 42 break;
Vincent Coubard 638:c90ae1400bf2 43 }
Vincent Coubard 638:c90ae1400bf2 44 break;
Vincent Coubard 638:c90ae1400bf2 45
Vincent Coubard 638:c90ae1400bf2 46 case BLE_GATTC_EVT_CHAR_DISC_RSP:
Vincent Coubard 638:c90ae1400bf2 47 switch (p_ble_evt->evt.gattc_evt.gatt_status) {
Vincent Coubard 638:c90ae1400bf2 48 case BLE_GATT_STATUS_SUCCESS:
Vincent Coubard 638:c90ae1400bf2 49 sdSingleton.setupDiscoveredCharacteristics(&p_ble_evt->evt.gattc_evt.params.char_disc_rsp);
Vincent Coubard 638:c90ae1400bf2 50 break;
Vincent Coubard 638:c90ae1400bf2 51
Vincent Coubard 638:c90ae1400bf2 52 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
Vincent Coubard 638:c90ae1400bf2 53 default:
Vincent Coubard 638:c90ae1400bf2 54 sdSingleton.terminateCharacteristicDiscovery(BLE_ERROR_NONE);
Vincent Coubard 638:c90ae1400bf2 55 break;
Vincent Coubard 638:c90ae1400bf2 56 }
Vincent Coubard 638:c90ae1400bf2 57 break;
Vincent Coubard 638:c90ae1400bf2 58
Vincent Coubard 638:c90ae1400bf2 59 case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
Vincent Coubard 638:c90ae1400bf2 60 if (sdSingleton.isActive()) {
Vincent Coubard 638:c90ae1400bf2 61 sdSingleton.processDiscoverUUIDResponse(&p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp);
Vincent Coubard 638:c90ae1400bf2 62 }
Vincent Coubard 638:c90ae1400bf2 63 break;
Vincent Coubard 638:c90ae1400bf2 64
Vincent Coubard 638:c90ae1400bf2 65 case BLE_GATTC_EVT_READ_RSP: {
Vincent Coubard 638:c90ae1400bf2 66 GattReadCallbackParams response = {
Vincent Coubard 638:c90ae1400bf2 67 .connHandle = p_ble_evt->evt.gattc_evt.conn_handle,
Vincent Coubard 638:c90ae1400bf2 68 .handle = p_ble_evt->evt.gattc_evt.params.read_rsp.handle,
Vincent Coubard 638:c90ae1400bf2 69 .offset = p_ble_evt->evt.gattc_evt.params.read_rsp.offset,
Vincent Coubard 638:c90ae1400bf2 70 .len = p_ble_evt->evt.gattc_evt.params.read_rsp.len,
Vincent Coubard 638:c90ae1400bf2 71 .data = p_ble_evt->evt.gattc_evt.params.read_rsp.data,
Vincent Coubard 638:c90ae1400bf2 72 };
Vincent Coubard 638:c90ae1400bf2 73 gattClient.processReadResponse(&response);
Vincent Coubard 638:c90ae1400bf2 74 }
Vincent Coubard 638:c90ae1400bf2 75 break;
Vincent Coubard 638:c90ae1400bf2 76
Vincent Coubard 638:c90ae1400bf2 77 case BLE_GATTC_EVT_WRITE_RSP: {
Vincent Coubard 638:c90ae1400bf2 78 GattWriteCallbackParams response = {
Vincent Coubard 638:c90ae1400bf2 79 .connHandle = p_ble_evt->evt.gattc_evt.conn_handle,
Vincent Coubard 638:c90ae1400bf2 80 .handle = p_ble_evt->evt.gattc_evt.params.write_rsp.handle,
Vincent Coubard 638:c90ae1400bf2 81 .writeOp = (GattWriteCallbackParams::WriteOp_t)(p_ble_evt->evt.gattc_evt.params.write_rsp.write_op),
Vincent Coubard 638:c90ae1400bf2 82 .offset = p_ble_evt->evt.gattc_evt.params.write_rsp.offset,
Vincent Coubard 638:c90ae1400bf2 83 .len = p_ble_evt->evt.gattc_evt.params.write_rsp.len,
Vincent Coubard 638:c90ae1400bf2 84 .data = p_ble_evt->evt.gattc_evt.params.write_rsp.data,
Vincent Coubard 638:c90ae1400bf2 85 };
Vincent Coubard 638:c90ae1400bf2 86 gattClient.processWriteResponse(&response);
Vincent Coubard 638:c90ae1400bf2 87 }
Vincent Coubard 638:c90ae1400bf2 88 break;
Vincent Coubard 638:c90ae1400bf2 89
Vincent Coubard 638:c90ae1400bf2 90 case BLE_GATTC_EVT_HVX: {
Vincent Coubard 638:c90ae1400bf2 91 GattHVXCallbackParams params;
Vincent Coubard 638:c90ae1400bf2 92 params.connHandle = p_ble_evt->evt.gattc_evt.conn_handle;
Vincent Coubard 638:c90ae1400bf2 93 params.handle = p_ble_evt->evt.gattc_evt.params.hvx.handle;
Vincent Coubard 638:c90ae1400bf2 94 params.type = static_cast<HVXType_t>(p_ble_evt->evt.gattc_evt.params.hvx.type);
Vincent Coubard 638:c90ae1400bf2 95 params.len = p_ble_evt->evt.gattc_evt.params.hvx.len;
Vincent Coubard 638:c90ae1400bf2 96 params.data = p_ble_evt->evt.gattc_evt.params.hvx.data;
Vincent Coubard 638:c90ae1400bf2 97
Vincent Coubard 638:c90ae1400bf2 98 gattClient.processHVXEvent(&params);
Vincent Coubard 638:c90ae1400bf2 99 }
Vincent Coubard 638:c90ae1400bf2 100 break;
Vincent Coubard 638:c90ae1400bf2 101
Vincent Coubard 638:c90ae1400bf2 102 case BLE_GATTC_EVT_DESC_DISC_RSP: {
Vincent Coubard 638:c90ae1400bf2 103 uint16_t conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
Vincent Coubard 638:c90ae1400bf2 104 uint16_t status = p_ble_evt->evt.gattc_evt.gatt_status;
Vincent Coubard 638:c90ae1400bf2 105 const ble_gattc_evt_desc_disc_rsp_t& discovered_descriptors = p_ble_evt->evt.gattc_evt.params.desc_disc_rsp;
Vincent Coubard 638:c90ae1400bf2 106
Vincent Coubard 638:c90ae1400bf2 107 switch(status) {
Vincent Coubard 638:c90ae1400bf2 108 case BLE_GATT_STATUS_SUCCESS:
Vincent Coubard 638:c90ae1400bf2 109 characteristicDescriptorDiscoverer.process(
Vincent Coubard 638:c90ae1400bf2 110 conn_handle,
Vincent Coubard 638:c90ae1400bf2 111 discovered_descriptors
Vincent Coubard 638:c90ae1400bf2 112 );
Vincent Coubard 638:c90ae1400bf2 113 break;
Vincent Coubard 638:c90ae1400bf2 114 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
Vincent Coubard 638:c90ae1400bf2 115 // end of discovery
Vincent Coubard 638:c90ae1400bf2 116 characteristicDescriptorDiscoverer.terminate(conn_handle, BLE_ERROR_NONE);
Vincent Coubard 638:c90ae1400bf2 117 break;
Vincent Coubard 638:c90ae1400bf2 118 default:
Vincent Coubard 638:c90ae1400bf2 119 characteristicDescriptorDiscoverer.terminate(conn_handle, BLE_ERROR_UNSPECIFIED);
Vincent Coubard 638:c90ae1400bf2 120 break;
Vincent Coubard 638:c90ae1400bf2 121 }
Vincent Coubard 638:c90ae1400bf2 122 } break;
Vincent Coubard 638:c90ae1400bf2 123
Vincent Coubard 638:c90ae1400bf2 124 case BLE_GATTC_EVT_ATTR_INFO_DISC_RSP : {
Vincent Coubard 638:c90ae1400bf2 125 uint16_t conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
Vincent Coubard 638:c90ae1400bf2 126 uint16_t status = p_ble_evt->evt.gattc_evt.gatt_status;
Vincent Coubard 638:c90ae1400bf2 127 const ble_gattc_evt_attr_info_disc_rsp_t& infos = p_ble_evt->evt.gattc_evt.params.attr_info_disc_rsp;
Vincent Coubard 638:c90ae1400bf2 128
Vincent Coubard 638:c90ae1400bf2 129 switch(status) {
Vincent Coubard 638:c90ae1400bf2 130 case BLE_GATT_STATUS_SUCCESS:
Vincent Coubard 638:c90ae1400bf2 131 characteristicDescriptorDiscoverer.processAttributeInformation(
Vincent Coubard 638:c90ae1400bf2 132 conn_handle,
Vincent Coubard 638:c90ae1400bf2 133 infos
Vincent Coubard 638:c90ae1400bf2 134 );
Vincent Coubard 638:c90ae1400bf2 135 break;
Vincent Coubard 638:c90ae1400bf2 136 default:
Vincent Coubard 638:c90ae1400bf2 137 characteristicDescriptorDiscoverer.terminate(conn_handle, BLE_ERROR_UNSPECIFIED);
Vincent Coubard 638:c90ae1400bf2 138 break;
Vincent Coubard 638:c90ae1400bf2 139 }
Vincent Coubard 638:c90ae1400bf2 140
Vincent Coubard 638:c90ae1400bf2 141 } break;
Vincent Coubard 638:c90ae1400bf2 142 }
Vincent Coubard 638:c90ae1400bf2 143
Vincent Coubard 638:c90ae1400bf2 144 sdSingleton.progressCharacteristicDiscovery();
Vincent Coubard 638:c90ae1400bf2 145 sdSingleton.progressServiceDiscovery();
Vincent Coubard 638:c90ae1400bf2 146 }
Vincent Coubard 638:c90ae1400bf2 147 #endif