nordic
Fork of nRF51822 by
btle/btle_discovery.cpp@248:df37e7bb3f71, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:22 2015 +0100
- Revision:
- 248:df37e7bb3f71
- Parent:
- 244:a966506d1e5b
- Child:
- 249:71ef03789dd3
Synchronized with git rev 75938af2
Author: Rohit Grover
remove btle_gattc.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 244:a966506d1e5b | 1 | /* mbed Microcontroller Library |
rgrover1 | 244:a966506d1e5b | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 244:a966506d1e5b | 3 | * |
rgrover1 | 244:a966506d1e5b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 244:a966506d1e5b | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 244:a966506d1e5b | 6 | * You may obtain a copy of the License at |
rgrover1 | 244:a966506d1e5b | 7 | * |
rgrover1 | 244:a966506d1e5b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 244:a966506d1e5b | 9 | * |
rgrover1 | 244:a966506d1e5b | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 244:a966506d1e5b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 244:a966506d1e5b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 244:a966506d1e5b | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 244:a966506d1e5b | 14 | * limitations under the License. |
rgrover1 | 244:a966506d1e5b | 15 | */ |
rgrover1 | 244:a966506d1e5b | 16 | |
rgrover1 | 240:693a1f145b5a | 17 | #include "blecommon.h" |
rgrover1 | 240:693a1f145b5a | 18 | #include "UUID.h" |
rgrover1 | 240:693a1f145b5a | 19 | #include "Gap.h" |
rgrover1 | 240:693a1f145b5a | 20 | #include "nrf_error.h" |
rgrover1 | 240:693a1f145b5a | 21 | #include "btle_discovery.h" |
rgrover1 | 240:693a1f145b5a | 22 | #include "ble_err.h" |
rgrover1 | 244:a966506d1e5b | 23 | |
rgrover1 | 244:a966506d1e5b | 24 | static NordicServiceDiscovery discoverySingleton; |
rgrover1 | 244:a966506d1e5b | 25 | ServiceDiscovery *ServiceDiscovery::getSingleton(void) { |
rgrover1 | 244:a966506d1e5b | 26 | return &discoverySingleton; |
rgrover1 | 244:a966506d1e5b | 27 | } |
rgrover1 | 240:693a1f145b5a | 28 | |
rgrover1 | 240:693a1f145b5a | 29 | ble_error_t |
rgrover1 | 240:693a1f145b5a | 30 | ServiceDiscovery::launch(Gap::Handle_t connectionHandle, ServiceCallback_t sc, CharacteristicCallback_t cc) |
rgrover1 | 240:693a1f145b5a | 31 | { |
rgrover1 | 240:693a1f145b5a | 32 | ServiceDiscovery *singleton = getSingleton(); |
rgrover1 | 244:a966506d1e5b | 33 | discoverySingleton.serviceDiscoveryStarted(connectionHandle); |
rgrover1 | 240:693a1f145b5a | 34 | |
rgrover1 | 240:693a1f145b5a | 35 | uint32_t rc; |
rgrover1 | 240:693a1f145b5a | 36 | if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) { |
rgrover1 | 240:693a1f145b5a | 37 | singleton->terminate(); |
rgrover1 | 240:693a1f145b5a | 38 | switch (rc) { |
rgrover1 | 240:693a1f145b5a | 39 | case NRF_ERROR_INVALID_PARAM: |
rgrover1 | 240:693a1f145b5a | 40 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 240:693a1f145b5a | 41 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 240:693a1f145b5a | 42 | case NRF_ERROR_BUSY: |
rgrover1 | 240:693a1f145b5a | 43 | return BLE_STACK_BUSY; |
rgrover1 | 240:693a1f145b5a | 44 | default: |
rgrover1 | 240:693a1f145b5a | 45 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 240:693a1f145b5a | 46 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 240:693a1f145b5a | 47 | } |
rgrover1 | 240:693a1f145b5a | 48 | } |
rgrover1 | 240:693a1f145b5a | 49 | |
rgrover1 | 240:693a1f145b5a | 50 | return BLE_ERROR_NONE; |
rgrover1 | 240:693a1f145b5a | 51 | } |
rgrover1 | 240:693a1f145b5a | 52 | |
rgrover1 | 244:a966506d1e5b | 53 | ble_error_t |
rgrover1 | 244:a966506d1e5b | 54 | NordicServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle) { |
rgrover1 | 244:a966506d1e5b | 55 | discoverySingleton.characteristicDiscoveryStarted(connectionHandle); |
rgrover1 | 240:693a1f145b5a | 56 | |
rgrover1 | 240:693a1f145b5a | 57 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 240:693a1f145b5a | 58 | .start_handle = startHandle, |
rgrover1 | 240:693a1f145b5a | 59 | .end_handle = endHandle |
rgrover1 | 240:693a1f145b5a | 60 | }; |
rgrover1 | 240:693a1f145b5a | 61 | uint32_t rc; |
rgrover1 | 240:693a1f145b5a | 62 | if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) { |
rgrover1 | 244:a966506d1e5b | 63 | discoverySingleton.terminateCharacteristicDiscovery(); |
rgrover1 | 240:693a1f145b5a | 64 | switch (rc) { |
rgrover1 | 240:693a1f145b5a | 65 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 240:693a1f145b5a | 66 | case NRF_ERROR_INVALID_ADDR: |
rgrover1 | 240:693a1f145b5a | 67 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 240:693a1f145b5a | 68 | case NRF_ERROR_BUSY: |
rgrover1 | 240:693a1f145b5a | 69 | return BLE_STACK_BUSY; |
rgrover1 | 240:693a1f145b5a | 70 | default: |
rgrover1 | 240:693a1f145b5a | 71 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 240:693a1f145b5a | 72 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 240:693a1f145b5a | 73 | } |
rgrover1 | 240:693a1f145b5a | 74 | } |
rgrover1 | 240:693a1f145b5a | 75 | |
rgrover1 | 240:693a1f145b5a | 76 | return BLE_ERROR_NONE; |
rgrover1 | 240:693a1f145b5a | 77 | } |
rgrover1 | 240:693a1f145b5a | 78 | |
rgrover1 | 240:693a1f145b5a | 79 | void |
rgrover1 | 241:75b69581d1dd | 80 | NordicServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response) |
rgrover1 | 240:693a1f145b5a | 81 | { |
rgrover1 | 243:73fc02cc20b1 | 82 | serviceIndex = 0; |
rgrover1 | 243:73fc02cc20b1 | 83 | numServices = response->count; |
rgrover1 | 240:693a1f145b5a | 84 | |
rgrover1 | 240:693a1f145b5a | 85 | /* Account for the limitation on the number of discovered services we can handle at a time. */ |
rgrover1 | 243:73fc02cc20b1 | 86 | if (numServices > BLE_DB_DISCOVERY_MAX_SRV) { |
rgrover1 | 243:73fc02cc20b1 | 87 | numServices = BLE_DB_DISCOVERY_MAX_SRV; |
rgrover1 | 240:693a1f145b5a | 88 | } |
rgrover1 | 240:693a1f145b5a | 89 | |
rgrover1 | 243:73fc02cc20b1 | 90 | for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) { |
rgrover1 | 240:693a1f145b5a | 91 | services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid, |
rgrover1 | 240:693a1f145b5a | 92 | response->services[serviceIndex].handle_range.start_handle, |
rgrover1 | 240:693a1f145b5a | 93 | response->services[serviceIndex].handle_range.end_handle); |
rgrover1 | 240:693a1f145b5a | 94 | } |
rgrover1 | 240:693a1f145b5a | 95 | } |
rgrover1 | 240:693a1f145b5a | 96 | |
rgrover1 | 240:693a1f145b5a | 97 | void |
rgrover1 | 241:75b69581d1dd | 98 | NordicServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response) |
rgrover1 | 240:693a1f145b5a | 99 | { |
rgrover1 | 243:73fc02cc20b1 | 100 | characteristicIndex = 0; |
rgrover1 | 243:73fc02cc20b1 | 101 | numCharacteristics = response->count; |
rgrover1 | 240:693a1f145b5a | 102 | |
rgrover1 | 240:693a1f145b5a | 103 | /* Account for the limitation on the number of discovered characteristics we can handle at a time. */ |
rgrover1 | 243:73fc02cc20b1 | 104 | if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) { |
rgrover1 | 243:73fc02cc20b1 | 105 | numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV; |
rgrover1 | 240:693a1f145b5a | 106 | } |
rgrover1 | 240:693a1f145b5a | 107 | |
rgrover1 | 243:73fc02cc20b1 | 108 | for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) { |
rgrover1 | 240:693a1f145b5a | 109 | characteristics[charIndex].setup(response->chars[charIndex].uuid.uuid, |
rgrover1 | 240:693a1f145b5a | 110 | *(const uint8_t *)(&response->chars[charIndex].char_props), |
rgrover1 | 240:693a1f145b5a | 111 | response->chars[charIndex].handle_decl, |
rgrover1 | 240:693a1f145b5a | 112 | response->chars[charIndex].handle_value); |
rgrover1 | 240:693a1f145b5a | 113 | } |
rgrover1 | 244:a966506d1e5b | 114 | } |
rgrover1 | 244:a966506d1e5b | 115 | |
rgrover1 | 244:a966506d1e5b | 116 | void bleGattcEventHandler(const ble_evt_t *p_ble_evt) |
rgrover1 | 244:a966506d1e5b | 117 | { |
rgrover1 | 244:a966506d1e5b | 118 | switch (p_ble_evt->header.evt_id) { |
rgrover1 | 244:a966506d1e5b | 119 | case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: |
rgrover1 | 244:a966506d1e5b | 120 | switch (p_ble_evt->evt.gattc_evt.gatt_status) { |
rgrover1 | 244:a966506d1e5b | 121 | case BLE_GATT_STATUS_SUCCESS: |
rgrover1 | 244:a966506d1e5b | 122 | discoverySingleton.setupDiscoveredServices(&p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp); |
rgrover1 | 244:a966506d1e5b | 123 | break; |
rgrover1 | 244:a966506d1e5b | 124 | |
rgrover1 | 244:a966506d1e5b | 125 | case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: |
rgrover1 | 244:a966506d1e5b | 126 | default: |
rgrover1 | 244:a966506d1e5b | 127 | discoverySingleton.terminate(); |
rgrover1 | 244:a966506d1e5b | 128 | break; |
rgrover1 | 244:a966506d1e5b | 129 | } |
rgrover1 | 244:a966506d1e5b | 130 | break; |
rgrover1 | 244:a966506d1e5b | 131 | |
rgrover1 | 244:a966506d1e5b | 132 | case BLE_GATTC_EVT_CHAR_DISC_RSP: |
rgrover1 | 244:a966506d1e5b | 133 | switch (p_ble_evt->evt.gattc_evt.gatt_status) { |
rgrover1 | 244:a966506d1e5b | 134 | case BLE_GATT_STATUS_SUCCESS: |
rgrover1 | 244:a966506d1e5b | 135 | discoverySingleton.setupDiscoveredCharacteristics(&p_ble_evt->evt.gattc_evt.params.char_disc_rsp); |
rgrover1 | 244:a966506d1e5b | 136 | break; |
rgrover1 | 244:a966506d1e5b | 137 | |
rgrover1 | 244:a966506d1e5b | 138 | case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: |
rgrover1 | 244:a966506d1e5b | 139 | default: |
rgrover1 | 244:a966506d1e5b | 140 | discoverySingleton.terminateCharacteristicDiscovery(); |
rgrover1 | 244:a966506d1e5b | 141 | break; |
rgrover1 | 244:a966506d1e5b | 142 | } |
rgrover1 | 244:a966506d1e5b | 143 | break; |
rgrover1 | 244:a966506d1e5b | 144 | } |
rgrover1 | 244:a966506d1e5b | 145 | |
rgrover1 | 244:a966506d1e5b | 146 | discoverySingleton.progressCharacteristicDiscovery(); |
rgrover1 | 244:a966506d1e5b | 147 | discoverySingleton.progressServiceDiscovery(); |
rgrover1 | 240:693a1f145b5a | 148 | } |