aa

Dependents:   Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:21 2015 +0100
Revision:
239:693a1f145b5a
Child:
240:75b69581d1dd
Synchronized with git rev 50a110cd
Author: Rohit Grover
partial separation of code related to discovery into files.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 239:693a1f145b5a 1 #include "blecommon.h"
rgrover1 239:693a1f145b5a 2 #include "UUID.h"
rgrover1 239:693a1f145b5a 3 #include "Gap.h"
rgrover1 239:693a1f145b5a 4 #include "nrf_error.h"
rgrover1 239:693a1f145b5a 5 #include "btle_discovery.h"
rgrover1 239:693a1f145b5a 6 #include "ble_err.h"
rgrover1 239:693a1f145b5a 7
rgrover1 239:693a1f145b5a 8 ServiceDiscovery *ServiceDiscovery::getSingleton(void) {
rgrover1 239:693a1f145b5a 9 static ServiceDiscovery discoverySingleton;
rgrover1 239:693a1f145b5a 10
rgrover1 239:693a1f145b5a 11 return &discoverySingleton;
rgrover1 239:693a1f145b5a 12 }
rgrover1 239:693a1f145b5a 13
rgrover1 239:693a1f145b5a 14 ble_error_t
rgrover1 239:693a1f145b5a 15 ServiceDiscovery::launch(Gap::Handle_t connectionHandle, ServiceCallback_t sc, CharacteristicCallback_t cc)
rgrover1 239:693a1f145b5a 16 {
rgrover1 239:693a1f145b5a 17 ServiceDiscovery *singleton = getSingleton();
rgrover1 239:693a1f145b5a 18 singleton->serviceDiscoveryStarted(connectionHandle);
rgrover1 239:693a1f145b5a 19
rgrover1 239:693a1f145b5a 20 uint32_t rc;
rgrover1 239:693a1f145b5a 21 if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) {
rgrover1 239:693a1f145b5a 22 singleton->terminate();
rgrover1 239:693a1f145b5a 23 switch (rc) {
rgrover1 239:693a1f145b5a 24 case NRF_ERROR_INVALID_PARAM:
rgrover1 239:693a1f145b5a 25 case BLE_ERROR_INVALID_CONN_HANDLE:
rgrover1 239:693a1f145b5a 26 return BLE_ERROR_INVALID_PARAM;
rgrover1 239:693a1f145b5a 27 case NRF_ERROR_BUSY:
rgrover1 239:693a1f145b5a 28 return BLE_STACK_BUSY;
rgrover1 239:693a1f145b5a 29 default:
rgrover1 239:693a1f145b5a 30 case NRF_ERROR_INVALID_STATE:
rgrover1 239:693a1f145b5a 31 return BLE_ERROR_INVALID_STATE;
rgrover1 239:693a1f145b5a 32 }
rgrover1 239:693a1f145b5a 33 }
rgrover1 239:693a1f145b5a 34
rgrover1 239:693a1f145b5a 35 return BLE_ERROR_NONE;
rgrover1 239:693a1f145b5a 36 }
rgrover1 239:693a1f145b5a 37
rgrover1 239:693a1f145b5a 38 ble_error_t ServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle) {
rgrover1 239:693a1f145b5a 39 ServiceDiscovery *singleton = getSingleton();
rgrover1 239:693a1f145b5a 40 singleton->characteristicDiscoveryStarted(connectionHandle);
rgrover1 239:693a1f145b5a 41
rgrover1 239:693a1f145b5a 42 ble_gattc_handle_range_t handleRange = {
rgrover1 239:693a1f145b5a 43 .start_handle = startHandle,
rgrover1 239:693a1f145b5a 44 .end_handle = endHandle
rgrover1 239:693a1f145b5a 45 };
rgrover1 239:693a1f145b5a 46 uint32_t rc;
rgrover1 239:693a1f145b5a 47 if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
rgrover1 239:693a1f145b5a 48 singleton->terminateCharacteristicDiscovery();
rgrover1 239:693a1f145b5a 49 switch (rc) {
rgrover1 239:693a1f145b5a 50 case BLE_ERROR_INVALID_CONN_HANDLE:
rgrover1 239:693a1f145b5a 51 case NRF_ERROR_INVALID_ADDR:
rgrover1 239:693a1f145b5a 52 return BLE_ERROR_INVALID_PARAM;
rgrover1 239:693a1f145b5a 53 case NRF_ERROR_BUSY:
rgrover1 239:693a1f145b5a 54 return BLE_STACK_BUSY;
rgrover1 239:693a1f145b5a 55 default:
rgrover1 239:693a1f145b5a 56 case NRF_ERROR_INVALID_STATE:
rgrover1 239:693a1f145b5a 57 return BLE_ERROR_INVALID_STATE;
rgrover1 239:693a1f145b5a 58 }
rgrover1 239:693a1f145b5a 59 }
rgrover1 239:693a1f145b5a 60
rgrover1 239:693a1f145b5a 61 return BLE_ERROR_NONE;
rgrover1 239:693a1f145b5a 62 }
rgrover1 239:693a1f145b5a 63
rgrover1 239:693a1f145b5a 64 void
rgrover1 239:693a1f145b5a 65 ServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response)
rgrover1 239:693a1f145b5a 66 {
rgrover1 239:693a1f145b5a 67 currSrvInd = 0;
rgrover1 239:693a1f145b5a 68 srvCount = response->count;
rgrover1 239:693a1f145b5a 69
rgrover1 239:693a1f145b5a 70 /* Account for the limitation on the number of discovered services we can handle at a time. */
rgrover1 239:693a1f145b5a 71 if (srvCount > BLE_DB_DISCOVERY_MAX_SRV) {
rgrover1 239:693a1f145b5a 72 srvCount = BLE_DB_DISCOVERY_MAX_SRV;
rgrover1 239:693a1f145b5a 73 }
rgrover1 239:693a1f145b5a 74
rgrover1 239:693a1f145b5a 75 for (unsigned serviceIndex = 0; serviceIndex < srvCount; serviceIndex++) {
rgrover1 239:693a1f145b5a 76 services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
rgrover1 239:693a1f145b5a 77 response->services[serviceIndex].handle_range.start_handle,
rgrover1 239:693a1f145b5a 78 response->services[serviceIndex].handle_range.end_handle);
rgrover1 239:693a1f145b5a 79 }
rgrover1 239:693a1f145b5a 80 }
rgrover1 239:693a1f145b5a 81
rgrover1 239:693a1f145b5a 82 void
rgrover1 239:693a1f145b5a 83 ServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
rgrover1 239:693a1f145b5a 84 {
rgrover1 239:693a1f145b5a 85 currCharInd = 0;
rgrover1 239:693a1f145b5a 86 charCount = response->count;
rgrover1 239:693a1f145b5a 87
rgrover1 239:693a1f145b5a 88 /* Account for the limitation on the number of discovered characteristics we can handle at a time. */
rgrover1 239:693a1f145b5a 89 if (charCount > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) {
rgrover1 239:693a1f145b5a 90 charCount = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV;
rgrover1 239:693a1f145b5a 91 }
rgrover1 239:693a1f145b5a 92
rgrover1 239:693a1f145b5a 93 for (unsigned charIndex = 0; charIndex < charCount; charIndex++) {
rgrover1 239:693a1f145b5a 94 characteristics[charIndex].setup(response->chars[charIndex].uuid.uuid,
rgrover1 239:693a1f145b5a 95 *(const uint8_t *)(&response->chars[charIndex].char_props),
rgrover1 239:693a1f145b5a 96 response->chars[charIndex].handle_decl,
rgrover1 239:693a1f145b5a 97 response->chars[charIndex].handle_value);
rgrover1 239:693a1f145b5a 98 }
rgrover1 239:693a1f145b5a 99 }