Shuta Nakamae / nRF51822

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:15 2015 +0100
Revision:
194:c99fc3160091
Child:
195:061ed80ffbcf
Synchronized with git rev 5f5027e2
Author: Rohit Grover
separate out gatt-client functionality into btle_gattc.cpp
presently have event handler and lauchServiceDiscovery()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 194:c99fc3160091 1 /* mbed Microcontroller Library
rgrover1 194:c99fc3160091 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 194:c99fc3160091 3 *
rgrover1 194:c99fc3160091 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 194:c99fc3160091 5 * you may not use this file except in compliance with the License.
rgrover1 194:c99fc3160091 6 * You may obtain a copy of the License at
rgrover1 194:c99fc3160091 7 *
rgrover1 194:c99fc3160091 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 194:c99fc3160091 9 *
rgrover1 194:c99fc3160091 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 194:c99fc3160091 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 194:c99fc3160091 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 194:c99fc3160091 13 * See the License for the specific language governing permissions and
rgrover1 194:c99fc3160091 14 * limitations under the License.
rgrover1 194:c99fc3160091 15 */
rgrover1 194:c99fc3160091 16
rgrover1 194:c99fc3160091 17 #include "btle_gattc.h"
rgrover1 194:c99fc3160091 18
rgrover1 194:c99fc3160091 19 #define BLE_DB_DISCOVERY_MAX_SRV 4 /**< Maximum number of services supported by this module. This also indicates the maximum number of users allowed to be registered to this module. (one user per service). */
rgrover1 194:c99fc3160091 20 #define BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV 4 /**< Maximum number of characteristics per service supported by this module. */
rgrover1 194:c99fc3160091 21
rgrover1 194:c99fc3160091 22 #define SRV_DISC_START_HANDLE 0x0001 /**< The start handle value used during service discovery. */
rgrover1 194:c99fc3160091 23
rgrover1 194:c99fc3160091 24
rgrover1 194:c99fc3160091 25 /**@brief Structure for holding information about the service and the characteristics found during
rgrover1 194:c99fc3160091 26 * the discovery process.
rgrover1 194:c99fc3160091 27 */
rgrover1 194:c99fc3160091 28 typedef struct
rgrover1 194:c99fc3160091 29 {
rgrover1 194:c99fc3160091 30 ble_uuid_t srvUUID; /**< UUID of the service. */
rgrover1 194:c99fc3160091 31 // uint8_t char_count; /**< Number of characteristics present in the service. */
rgrover1 194:c99fc3160091 32 // ble_db_discovery_char_t charateristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; /**< Array of information related to the characteristics present in the service. */
rgrover1 194:c99fc3160091 33 ble_gattc_handle_range_t handleRange; /**< Service Handle Range. */
rgrover1 194:c99fc3160091 34 } ble_db_discovery_srv_t;
rgrover1 194:c99fc3160091 35
rgrover1 194:c99fc3160091 36 typedef struct
rgrover1 194:c99fc3160091 37 {
rgrover1 194:c99fc3160091 38 ble_db_discovery_srv_t services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. This is intended for internal use during service discovery.*/
rgrover1 194:c99fc3160091 39 uint16_t connHandle; /**< Connection handle as provided by the SoftDevice. */
rgrover1 194:c99fc3160091 40 uint8_t srvCount; /**< Number of services at the peers GATT database.*/
rgrover1 194:c99fc3160091 41 // uint8_t currCharInd; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
rgrover1 194:c99fc3160091 42 uint8_t currSrvInd; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
rgrover1 194:c99fc3160091 43 // bool discoveryInProgress; /**< Variable to indicate if there is a service discovery in progress. */
rgrover1 194:c99fc3160091 44 } ble_db_discovery_t;
rgrover1 194:c99fc3160091 45
rgrover1 194:c99fc3160091 46 void launchServiceDiscovery(Gap::Handle_t connectionHandle)
rgrover1 194:c99fc3160091 47 {
rgrover1 194:c99fc3160091 48 // printf("connectionHandle %u\r\n", connectionHandle);
rgrover1 194:c99fc3160091 49 printf("launch service discovery returned %u\r\n", sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL));
rgrover1 194:c99fc3160091 50 }
rgrover1 194:c99fc3160091 51
rgrover1 194:c99fc3160091 52 void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
rgrover1 194:c99fc3160091 53 {
rgrover1 194:c99fc3160091 54 switch (p_ble_evt->header.evt_id) {
rgrover1 194:c99fc3160091 55 case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
rgrover1 194:c99fc3160091 56 switch (p_ble_evt->evt.gattc_evt.gatt_status) {
rgrover1 194:c99fc3160091 57 case BLE_GATT_STATUS_SUCCESS: {
rgrover1 194:c99fc3160091 58 printf("count of primary services: %u\r\n", p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count);
rgrover1 194:c99fc3160091 59
rgrover1 194:c99fc3160091 60 unsigned index;
rgrover1 194:c99fc3160091 61 for (index = 0; index < p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.count; index++) {
rgrover1 194:c99fc3160091 62 printf("%x [%u %u]\r\n",
rgrover1 194:c99fc3160091 63 p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].uuid.uuid,
rgrover1 194:c99fc3160091 64 p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.start_handle,
rgrover1 194:c99fc3160091 65 p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.end_handle);
rgrover1 194:c99fc3160091 66
rgrover1 194:c99fc3160091 67 ble_gattc_handle_range_t handleRange = {
rgrover1 194:c99fc3160091 68 .start_handle = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.start_handle,
rgrover1 194:c99fc3160091 69 .end_handle = p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index].handle_range.end_handle
rgrover1 194:c99fc3160091 70 };
rgrover1 194:c99fc3160091 71 printf("characteristics_discover returned %u\r\n",
rgrover1 194:c99fc3160091 72 sd_ble_gattc_characteristics_discover(p_ble_evt->evt.gattc_evt.conn_handle, &handleRange));
rgrover1 194:c99fc3160091 73 }
rgrover1 194:c99fc3160091 74 printf("services discover returned %u\r\n",
rgrover1 194:c99fc3160091 75 sd_ble_gattc_primary_services_discover(p_ble_evt->evt.gattc_evt.conn_handle,
rgrover1 194:c99fc3160091 76 p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp.services[index -1].handle_range.end_handle,
rgrover1 194:c99fc3160091 77 NULL));
rgrover1 194:c99fc3160091 78 break;
rgrover1 194:c99fc3160091 79 }
rgrover1 194:c99fc3160091 80 case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: {
rgrover1 194:c99fc3160091 81 printf("end of service discovery\r\n");
rgrover1 194:c99fc3160091 82 break;
rgrover1 194:c99fc3160091 83 }
rgrover1 194:c99fc3160091 84 default: {
rgrover1 194:c99fc3160091 85 printf("gatt failure status: %u\r\n", p_ble_evt->evt.gattc_evt.gatt_status);
rgrover1 194:c99fc3160091 86 break;
rgrover1 194:c99fc3160091 87 }
rgrover1 194:c99fc3160091 88 }
rgrover1 194:c99fc3160091 89 break;
rgrover1 194:c99fc3160091 90
rgrover1 194:c99fc3160091 91 case BLE_GATTC_EVT_CHAR_DISC_RSP: {
rgrover1 194:c99fc3160091 92 switch (p_ble_evt->evt.gattc_evt.gatt_status) {
rgrover1 194:c99fc3160091 93 default:
rgrover1 194:c99fc3160091 94 printf("gatt failure status: %u\r\n", p_ble_evt->evt.gattc_evt.gatt_status);
rgrover1 194:c99fc3160091 95 break;
rgrover1 194:c99fc3160091 96 }
rgrover1 194:c99fc3160091 97 break;
rgrover1 194:c99fc3160091 98 }
rgrover1 194:c99fc3160091 99 }
rgrover1 194:c99fc3160091 100 }