Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /*
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2013 Nordic Semiconductor ASA
sahilmgandhi 18:6a4db94011d3 3 * All rights reserved.
sahilmgandhi 18:6a4db94011d3 4 *
sahilmgandhi 18:6a4db94011d3 5 * Redistribution and use in source and binary forms, with or without modification,
sahilmgandhi 18:6a4db94011d3 6 * are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * 1. Redistributions of source code must retain the above copyright notice, this list
sahilmgandhi 18:6a4db94011d3 9 * of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 10 *
sahilmgandhi 18:6a4db94011d3 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
sahilmgandhi 18:6a4db94011d3 12 * integrated circuit in a product or a software update for such product, must reproduce
sahilmgandhi 18:6a4db94011d3 13 * the above copyright notice, this list of conditions and the following disclaimer in
sahilmgandhi 18:6a4db94011d3 14 * the documentation and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 15 *
sahilmgandhi 18:6a4db94011d3 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
sahilmgandhi 18:6a4db94011d3 17 * used to endorse or promote products derived from this software without specific prior
sahilmgandhi 18:6a4db94011d3 18 * written permission.
sahilmgandhi 18:6a4db94011d3 19 *
sahilmgandhi 18:6a4db94011d3 20 * 4. This software, with or without modification, must only be used with a
sahilmgandhi 18:6a4db94011d3 21 * Nordic Semiconductor ASA integrated circuit.
sahilmgandhi 18:6a4db94011d3 22 *
sahilmgandhi 18:6a4db94011d3 23 * 5. Any software provided in binary or object form under this license must not be reverse
sahilmgandhi 18:6a4db94011d3 24 * engineered, decompiled, modified and/or disassembled.
sahilmgandhi 18:6a4db94011d3 25 *
sahilmgandhi 18:6a4db94011d3 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
sahilmgandhi 18:6a4db94011d3 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sahilmgandhi 18:6a4db94011d3 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sahilmgandhi 18:6a4db94011d3 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
sahilmgandhi 18:6a4db94011d3 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sahilmgandhi 18:6a4db94011d3 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sahilmgandhi 18:6a4db94011d3 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
sahilmgandhi 18:6a4db94011d3 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
sahilmgandhi 18:6a4db94011d3 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sahilmgandhi 18:6a4db94011d3 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 36 *
sahilmgandhi 18:6a4db94011d3 37 */
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 #include "ble_db_discovery.h"
sahilmgandhi 18:6a4db94011d3 41 #include <stdlib.h>
sahilmgandhi 18:6a4db94011d3 42 #include "nrf_ble.h"
sahilmgandhi 18:6a4db94011d3 43 #include "nrf_log.h"
sahilmgandhi 18:6a4db94011d3 44
sahilmgandhi 18:6a4db94011d3 45 #include "sdk_common.h"
sahilmgandhi 18:6a4db94011d3 46
sahilmgandhi 18:6a4db94011d3 47 #define SRV_DISC_START_HANDLE 0x0001 /**< The start handle value used during service discovery. */
sahilmgandhi 18:6a4db94011d3 48 #define DB_DISCOVERY_MAX_USERS BLE_DB_DISCOVERY_MAX_SRV /**< The maximum number of users/registrations allowed by this module. */
sahilmgandhi 18:6a4db94011d3 49 #define DB_LOG NRF_LOG_PRINTF_DEBUG /**< A debug logger macro that can be used in this file to do logging information over UART. */
sahilmgandhi 18:6a4db94011d3 50
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 /**@brief Array of structures containing information about the registered application modules. */
sahilmgandhi 18:6a4db94011d3 53 static ble_uuid_t m_registered_handlers[DB_DISCOVERY_MAX_USERS];
sahilmgandhi 18:6a4db94011d3 54
sahilmgandhi 18:6a4db94011d3 55
sahilmgandhi 18:6a4db94011d3 56 /**@brief Array of structures containing pending events to be sent to the application modules.
sahilmgandhi 18:6a4db94011d3 57 *
sahilmgandhi 18:6a4db94011d3 58 * @details Whenever a discovery related event is to be raised to a user module, it will be stored
sahilmgandhi 18:6a4db94011d3 59 * in this array first. When all services needed to be discovered have been
sahilmgandhi 18:6a4db94011d3 60 * discovered, all pending events will be sent to the corresponding user modules.
sahilmgandhi 18:6a4db94011d3 61 **/
sahilmgandhi 18:6a4db94011d3 62 static struct
sahilmgandhi 18:6a4db94011d3 63 {
sahilmgandhi 18:6a4db94011d3 64 ble_db_discovery_evt_t evt; /**< The pending event. */
sahilmgandhi 18:6a4db94011d3 65 ble_db_discovery_evt_handler_t evt_handler; /**< The event handler which should be called to raise this event. */
sahilmgandhi 18:6a4db94011d3 66 } m_pending_user_evts[DB_DISCOVERY_MAX_USERS];
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68 static ble_db_discovery_evt_handler_t m_evt_handler;
sahilmgandhi 18:6a4db94011d3 69 static uint32_t m_pending_usr_evt_index; /**< The index to the pending user event array, pointing to the last added pending user event. */
sahilmgandhi 18:6a4db94011d3 70 static uint32_t m_num_of_handlers_reg; /**< The number of handlers registered with the DB Discovery module. */
sahilmgandhi 18:6a4db94011d3 71 static bool m_initialized = false; /**< This variable Indicates if the module is initialized or not. */
sahilmgandhi 18:6a4db94011d3 72
sahilmgandhi 18:6a4db94011d3 73 #define MODULE_INITIALIZED (m_initialized == true)
sahilmgandhi 18:6a4db94011d3 74 #include "sdk_macros.h"
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 /**@brief Function for fetching the event handler provided by a registered application module.
sahilmgandhi 18:6a4db94011d3 77 *
sahilmgandhi 18:6a4db94011d3 78 * @param[in] srv_uuid UUID of the service.
sahilmgandhi 18:6a4db94011d3 79 *
sahilmgandhi 18:6a4db94011d3 80 * @retval evt_handler Event handler of the module, registered for the given service UUID.
sahilmgandhi 18:6a4db94011d3 81 * @retval NULL If no event handler is found.
sahilmgandhi 18:6a4db94011d3 82 */
sahilmgandhi 18:6a4db94011d3 83 static ble_db_discovery_evt_handler_t registered_handler_get(const ble_uuid_t * const p_srv_uuid)
sahilmgandhi 18:6a4db94011d3 84 {
sahilmgandhi 18:6a4db94011d3 85 uint32_t i;
sahilmgandhi 18:6a4db94011d3 86
sahilmgandhi 18:6a4db94011d3 87 for (i = 0; i < m_num_of_handlers_reg; i++)
sahilmgandhi 18:6a4db94011d3 88 {
sahilmgandhi 18:6a4db94011d3 89 if (BLE_UUID_EQ(&(m_registered_handlers[i]), p_srv_uuid))
sahilmgandhi 18:6a4db94011d3 90 {
sahilmgandhi 18:6a4db94011d3 91 return (m_evt_handler);
sahilmgandhi 18:6a4db94011d3 92 }
sahilmgandhi 18:6a4db94011d3 93 }
sahilmgandhi 18:6a4db94011d3 94
sahilmgandhi 18:6a4db94011d3 95 return NULL;
sahilmgandhi 18:6a4db94011d3 96 }
sahilmgandhi 18:6a4db94011d3 97
sahilmgandhi 18:6a4db94011d3 98
sahilmgandhi 18:6a4db94011d3 99 /**@brief Function for storing the event handler provided by a registered application module.
sahilmgandhi 18:6a4db94011d3 100 *
sahilmgandhi 18:6a4db94011d3 101 * @param[in] p_srv_uuid The UUID of the service.
sahilmgandhi 18:6a4db94011d3 102 * @param[in] p_evt_handler The event handler provided by the application.
sahilmgandhi 18:6a4db94011d3 103 *
sahilmgandhi 18:6a4db94011d3 104 * @retval NRF_SUCCESS If the handler was stored or already present in the list.
sahilmgandhi 18:6a4db94011d3 105 * @retval NRF_ERROR_NO_MEM If there is no space left to store the handler.
sahilmgandhi 18:6a4db94011d3 106 */
sahilmgandhi 18:6a4db94011d3 107 static uint32_t registered_handler_set(const ble_uuid_t * const p_srv_uuid,
sahilmgandhi 18:6a4db94011d3 108 ble_db_discovery_evt_handler_t p_evt_handler)
sahilmgandhi 18:6a4db94011d3 109 {
sahilmgandhi 18:6a4db94011d3 110 if (registered_handler_get(p_srv_uuid) != NULL)
sahilmgandhi 18:6a4db94011d3 111 {
sahilmgandhi 18:6a4db94011d3 112 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 113 }
sahilmgandhi 18:6a4db94011d3 114 if (m_num_of_handlers_reg < DB_DISCOVERY_MAX_USERS)
sahilmgandhi 18:6a4db94011d3 115 {
sahilmgandhi 18:6a4db94011d3 116 m_registered_handlers[m_num_of_handlers_reg] = *p_srv_uuid;
sahilmgandhi 18:6a4db94011d3 117
sahilmgandhi 18:6a4db94011d3 118 m_num_of_handlers_reg++;
sahilmgandhi 18:6a4db94011d3 119
sahilmgandhi 18:6a4db94011d3 120 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 121 }
sahilmgandhi 18:6a4db94011d3 122 else
sahilmgandhi 18:6a4db94011d3 123 {
sahilmgandhi 18:6a4db94011d3 124 return NRF_ERROR_NO_MEM;
sahilmgandhi 18:6a4db94011d3 125 }
sahilmgandhi 18:6a4db94011d3 126 }
sahilmgandhi 18:6a4db94011d3 127
sahilmgandhi 18:6a4db94011d3 128
sahilmgandhi 18:6a4db94011d3 129 /**@brief Function for sending all pending discovery events to the corresponding user modules.
sahilmgandhi 18:6a4db94011d3 130 */
sahilmgandhi 18:6a4db94011d3 131 static void pending_user_evts_send(void)
sahilmgandhi 18:6a4db94011d3 132 {
sahilmgandhi 18:6a4db94011d3 133 uint32_t i = 0;
sahilmgandhi 18:6a4db94011d3 134
sahilmgandhi 18:6a4db94011d3 135 for (i = 0; i < m_num_of_handlers_reg; i++)
sahilmgandhi 18:6a4db94011d3 136 {
sahilmgandhi 18:6a4db94011d3 137 // Pass the event to the corresponding event handler.
sahilmgandhi 18:6a4db94011d3 138 m_pending_user_evts[i].evt_handler(&(m_pending_user_evts[i].evt));
sahilmgandhi 18:6a4db94011d3 139 }
sahilmgandhi 18:6a4db94011d3 140 m_pending_usr_evt_index = 0;
sahilmgandhi 18:6a4db94011d3 141 }
sahilmgandhi 18:6a4db94011d3 142
sahilmgandhi 18:6a4db94011d3 143
sahilmgandhi 18:6a4db94011d3 144 /**@brief Function for indicating error to the application.
sahilmgandhi 18:6a4db94011d3 145 *
sahilmgandhi 18:6a4db94011d3 146 * @details This function will fetch the event handler based on the UUID of the service being
sahilmgandhi 18:6a4db94011d3 147 * discovered. (The event handler is registered by the application beforehand).
sahilmgandhi 18:6a4db94011d3 148 * The error code is added to the pending events together with the event handler.
sahilmgandhi 18:6a4db94011d3 149 * If no event handler was found, then this function will do nothing.
sahilmgandhi 18:6a4db94011d3 150 *
sahilmgandhi 18:6a4db94011d3 151 * @param[in] p_db_discovery Pointer to the DB discovery structure.
sahilmgandhi 18:6a4db94011d3 152 * @param[in] err_code Error code that should be provided to the application.
sahilmgandhi 18:6a4db94011d3 153 * @param[in] conn_handle Connection Handle.
sahilmgandhi 18:6a4db94011d3 154 *
sahilmgandhi 18:6a4db94011d3 155 */
sahilmgandhi 18:6a4db94011d3 156 static void discovery_error_evt_trigger(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 157 uint32_t err_code,
sahilmgandhi 18:6a4db94011d3 158 uint16_t const conn_handle)
sahilmgandhi 18:6a4db94011d3 159 {
sahilmgandhi 18:6a4db94011d3 160 ble_db_discovery_evt_handler_t p_evt_handler;
sahilmgandhi 18:6a4db94011d3 161 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 162
sahilmgandhi 18:6a4db94011d3 163 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 164
sahilmgandhi 18:6a4db94011d3 165 p_evt_handler = registered_handler_get(&(p_srv_being_discovered->srv_uuid));
sahilmgandhi 18:6a4db94011d3 166
sahilmgandhi 18:6a4db94011d3 167 if (p_evt_handler != NULL)
sahilmgandhi 18:6a4db94011d3 168 {
sahilmgandhi 18:6a4db94011d3 169 ble_db_discovery_evt_t evt;
sahilmgandhi 18:6a4db94011d3 170
sahilmgandhi 18:6a4db94011d3 171 evt.conn_handle = conn_handle;
sahilmgandhi 18:6a4db94011d3 172 evt.evt_type = BLE_DB_DISCOVERY_ERROR;
sahilmgandhi 18:6a4db94011d3 173 evt.params.err_code = err_code;
sahilmgandhi 18:6a4db94011d3 174
sahilmgandhi 18:6a4db94011d3 175 p_evt_handler(&evt);
sahilmgandhi 18:6a4db94011d3 176 }
sahilmgandhi 18:6a4db94011d3 177 }
sahilmgandhi 18:6a4db94011d3 178
sahilmgandhi 18:6a4db94011d3 179
sahilmgandhi 18:6a4db94011d3 180 /**@brief Function for triggering a Discovery Complete or Service Not Found event to the
sahilmgandhi 18:6a4db94011d3 181 * application.
sahilmgandhi 18:6a4db94011d3 182 *
sahilmgandhi 18:6a4db94011d3 183 * @details This function will fetch the event handler based on the UUID of the service being
sahilmgandhi 18:6a4db94011d3 184 * discovered. (The event handler is registered by the application beforehand).
sahilmgandhi 18:6a4db94011d3 185 * It then triggers an event indicating the completion of the service discovery.
sahilmgandhi 18:6a4db94011d3 186 * If no event handler was found, then this function will do nothing.
sahilmgandhi 18:6a4db94011d3 187 *
sahilmgandhi 18:6a4db94011d3 188 * @param[in] p_db_discovery Pointer to the DB discovery structure.
sahilmgandhi 18:6a4db94011d3 189 * @param[in] is_srv_found Variable to indicate if the service was found at the peer.
sahilmgandhi 18:6a4db94011d3 190 * @param[in] conn_handle Connection Handle.
sahilmgandhi 18:6a4db94011d3 191 */
sahilmgandhi 18:6a4db94011d3 192 static void discovery_complete_evt_trigger(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 193 bool is_srv_found,
sahilmgandhi 18:6a4db94011d3 194 uint16_t const conn_handle)
sahilmgandhi 18:6a4db94011d3 195 {
sahilmgandhi 18:6a4db94011d3 196 ble_db_discovery_evt_handler_t p_evt_handler;
sahilmgandhi 18:6a4db94011d3 197 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 198
sahilmgandhi 18:6a4db94011d3 199 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 200
sahilmgandhi 18:6a4db94011d3 201 p_evt_handler = registered_handler_get(&(p_srv_being_discovered->srv_uuid));
sahilmgandhi 18:6a4db94011d3 202
sahilmgandhi 18:6a4db94011d3 203 if (p_evt_handler != NULL)
sahilmgandhi 18:6a4db94011d3 204 {
sahilmgandhi 18:6a4db94011d3 205 if (m_pending_usr_evt_index < DB_DISCOVERY_MAX_USERS)
sahilmgandhi 18:6a4db94011d3 206 {
sahilmgandhi 18:6a4db94011d3 207 // Insert an event into the pending event list.
sahilmgandhi 18:6a4db94011d3 208 m_pending_user_evts[m_pending_usr_evt_index].evt.conn_handle = conn_handle;
sahilmgandhi 18:6a4db94011d3 209
sahilmgandhi 18:6a4db94011d3 210 m_pending_user_evts[m_pending_usr_evt_index].evt.params.discovered_db =
sahilmgandhi 18:6a4db94011d3 211 *p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 212 if (is_srv_found)
sahilmgandhi 18:6a4db94011d3 213 {
sahilmgandhi 18:6a4db94011d3 214 m_pending_user_evts[m_pending_usr_evt_index].evt.evt_type =
sahilmgandhi 18:6a4db94011d3 215 BLE_DB_DISCOVERY_COMPLETE;
sahilmgandhi 18:6a4db94011d3 216 }
sahilmgandhi 18:6a4db94011d3 217 else
sahilmgandhi 18:6a4db94011d3 218 {
sahilmgandhi 18:6a4db94011d3 219 m_pending_user_evts[m_pending_usr_evt_index].evt.evt_type =
sahilmgandhi 18:6a4db94011d3 220 BLE_DB_DISCOVERY_SRV_NOT_FOUND;
sahilmgandhi 18:6a4db94011d3 221 }
sahilmgandhi 18:6a4db94011d3 222 m_pending_user_evts[m_pending_usr_evt_index].evt_handler = p_evt_handler;
sahilmgandhi 18:6a4db94011d3 223
sahilmgandhi 18:6a4db94011d3 224 m_pending_usr_evt_index++;
sahilmgandhi 18:6a4db94011d3 225
sahilmgandhi 18:6a4db94011d3 226 if (m_pending_usr_evt_index == m_num_of_handlers_reg)
sahilmgandhi 18:6a4db94011d3 227 {
sahilmgandhi 18:6a4db94011d3 228 // All registered modules have pending events. Send all pending events to the user
sahilmgandhi 18:6a4db94011d3 229 // modules.
sahilmgandhi 18:6a4db94011d3 230 pending_user_evts_send();
sahilmgandhi 18:6a4db94011d3 231 }
sahilmgandhi 18:6a4db94011d3 232 else
sahilmgandhi 18:6a4db94011d3 233 {
sahilmgandhi 18:6a4db94011d3 234 // Too many events pending. Do nothing. (Ideally this should not happen.)
sahilmgandhi 18:6a4db94011d3 235 }
sahilmgandhi 18:6a4db94011d3 236 }
sahilmgandhi 18:6a4db94011d3 237 }
sahilmgandhi 18:6a4db94011d3 238 }
sahilmgandhi 18:6a4db94011d3 239
sahilmgandhi 18:6a4db94011d3 240
sahilmgandhi 18:6a4db94011d3 241 /**@brief Function for handling service discovery completion.
sahilmgandhi 18:6a4db94011d3 242 *
sahilmgandhi 18:6a4db94011d3 243 * @details This function will be used to determine if there are more services to be discovered,
sahilmgandhi 18:6a4db94011d3 244 * and if so, initiate the discovery of the next service.
sahilmgandhi 18:6a4db94011d3 245 *
sahilmgandhi 18:6a4db94011d3 246 * @param[in] p_db_discovery Pointer to the DB Discovery Structure.
sahilmgandhi 18:6a4db94011d3 247 * @param[in] conn_handle Connection Handle.
sahilmgandhi 18:6a4db94011d3 248 */
sahilmgandhi 18:6a4db94011d3 249 static void on_srv_disc_completion(ble_db_discovery_t * p_db_discovery,
sahilmgandhi 18:6a4db94011d3 250 uint16_t const conn_handle)
sahilmgandhi 18:6a4db94011d3 251 {
sahilmgandhi 18:6a4db94011d3 252 p_db_discovery->discoveries_count++;
sahilmgandhi 18:6a4db94011d3 253
sahilmgandhi 18:6a4db94011d3 254 // Check if more services need to be discovered.
sahilmgandhi 18:6a4db94011d3 255 if (p_db_discovery->discoveries_count < m_num_of_handlers_reg)
sahilmgandhi 18:6a4db94011d3 256 {
sahilmgandhi 18:6a4db94011d3 257 // Reset the current characteristic index since a new service discovery is about to start.
sahilmgandhi 18:6a4db94011d3 258 p_db_discovery->curr_char_ind = 0;
sahilmgandhi 18:6a4db94011d3 259
sahilmgandhi 18:6a4db94011d3 260 // Initiate discovery of the next service.
sahilmgandhi 18:6a4db94011d3 261 p_db_discovery->curr_srv_ind++;
sahilmgandhi 18:6a4db94011d3 262
sahilmgandhi 18:6a4db94011d3 263 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 264
sahilmgandhi 18:6a4db94011d3 265 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 266
sahilmgandhi 18:6a4db94011d3 267 p_srv_being_discovered->srv_uuid = m_registered_handlers[p_db_discovery->curr_srv_ind];
sahilmgandhi 18:6a4db94011d3 268
sahilmgandhi 18:6a4db94011d3 269 // Reset the characteristic count in the current service to zero since a new service
sahilmgandhi 18:6a4db94011d3 270 // discovery is about to start.
sahilmgandhi 18:6a4db94011d3 271 p_srv_being_discovered->char_count = 0;
sahilmgandhi 18:6a4db94011d3 272
sahilmgandhi 18:6a4db94011d3 273 DB_LOG("[DB]: Starting discovery of service with UUID 0x%x for Connection handle %d\r\n",
sahilmgandhi 18:6a4db94011d3 274 p_srv_being_discovered->srv_uuid.uuid, conn_handle);
sahilmgandhi 18:6a4db94011d3 275
sahilmgandhi 18:6a4db94011d3 276 uint32_t err_code;
sahilmgandhi 18:6a4db94011d3 277
sahilmgandhi 18:6a4db94011d3 278 err_code = sd_ble_gattc_primary_services_discover
sahilmgandhi 18:6a4db94011d3 279 (
sahilmgandhi 18:6a4db94011d3 280 conn_handle,
sahilmgandhi 18:6a4db94011d3 281 SRV_DISC_START_HANDLE,
sahilmgandhi 18:6a4db94011d3 282 &(p_srv_being_discovered->srv_uuid)
sahilmgandhi 18:6a4db94011d3 283 );
sahilmgandhi 18:6a4db94011d3 284 if (err_code != NRF_SUCCESS)
sahilmgandhi 18:6a4db94011d3 285 {
sahilmgandhi 18:6a4db94011d3 286 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 287
sahilmgandhi 18:6a4db94011d3 288 // Error with discovering the service.
sahilmgandhi 18:6a4db94011d3 289 // Indicate the error to the registered user application.
sahilmgandhi 18:6a4db94011d3 290 discovery_error_evt_trigger(p_db_discovery, err_code, conn_handle);
sahilmgandhi 18:6a4db94011d3 291
sahilmgandhi 18:6a4db94011d3 292 m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
sahilmgandhi 18:6a4db94011d3 293 m_pending_user_evts[0].evt.conn_handle = conn_handle;
sahilmgandhi 18:6a4db94011d3 294 // m_evt_handler(&m_pending_user_evts[0].evt);
sahilmgandhi 18:6a4db94011d3 295
sahilmgandhi 18:6a4db94011d3 296 return;
sahilmgandhi 18:6a4db94011d3 297 }
sahilmgandhi 18:6a4db94011d3 298 }
sahilmgandhi 18:6a4db94011d3 299 else
sahilmgandhi 18:6a4db94011d3 300 {
sahilmgandhi 18:6a4db94011d3 301 // No more service discovery is needed.
sahilmgandhi 18:6a4db94011d3 302 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 303 m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
sahilmgandhi 18:6a4db94011d3 304 m_pending_user_evts[0].evt.conn_handle = conn_handle;
sahilmgandhi 18:6a4db94011d3 305 //m_evt_handler(&m_pending_user_evts[0].evt);
sahilmgandhi 18:6a4db94011d3 306 }
sahilmgandhi 18:6a4db94011d3 307 }
sahilmgandhi 18:6a4db94011d3 308
sahilmgandhi 18:6a4db94011d3 309
sahilmgandhi 18:6a4db94011d3 310 /**@brief Function for finding out if a characteristic discovery should be performed after the
sahilmgandhi 18:6a4db94011d3 311 * last discovered characteristic.
sahilmgandhi 18:6a4db94011d3 312 *
sahilmgandhi 18:6a4db94011d3 313 * @details This function is used during the time of database discovery to find out if there is
sahilmgandhi 18:6a4db94011d3 314 * a need to do more characteristic discoveries. The value handles of the
sahilmgandhi 18:6a4db94011d3 315 * last discovered characteristic is compared with the end handle of the service.
sahilmgandhi 18:6a4db94011d3 316 * If the service handle is greater than one of the former characteristic handles,
sahilmgandhi 18:6a4db94011d3 317 * it means that a characteristic discovery is required.
sahilmgandhi 18:6a4db94011d3 318 *
sahilmgandhi 18:6a4db94011d3 319 * @param[in] p_db_discovery The pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 320 * @param[in] p_after_char The pointer to the last discovered characteristic.
sahilmgandhi 18:6a4db94011d3 321 *
sahilmgandhi 18:6a4db94011d3 322 * @retval True if a characteristic discovery is required.
sahilmgandhi 18:6a4db94011d3 323 * @retval False if a characteristic discovery is NOT required.
sahilmgandhi 18:6a4db94011d3 324 */
sahilmgandhi 18:6a4db94011d3 325 static bool is_char_discovery_reqd(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 326 ble_gattc_char_t * p_after_char)
sahilmgandhi 18:6a4db94011d3 327 {
sahilmgandhi 18:6a4db94011d3 328 if (
sahilmgandhi 18:6a4db94011d3 329 p_after_char->handle_value <
sahilmgandhi 18:6a4db94011d3 330 p_db_discovery->services[p_db_discovery->curr_srv_ind].handle_range.end_handle
sahilmgandhi 18:6a4db94011d3 331 )
sahilmgandhi 18:6a4db94011d3 332 {
sahilmgandhi 18:6a4db94011d3 333 // Handle value of the characteristic being discovered is less than the end handle of
sahilmgandhi 18:6a4db94011d3 334 // the service being discovered. There is a possibility of more characteristics being
sahilmgandhi 18:6a4db94011d3 335 // present. Hence a characteristic discovery is required.
sahilmgandhi 18:6a4db94011d3 336 return true;
sahilmgandhi 18:6a4db94011d3 337 }
sahilmgandhi 18:6a4db94011d3 338
sahilmgandhi 18:6a4db94011d3 339 return false;
sahilmgandhi 18:6a4db94011d3 340 }
sahilmgandhi 18:6a4db94011d3 341
sahilmgandhi 18:6a4db94011d3 342
sahilmgandhi 18:6a4db94011d3 343 /**@brief Function to find out if a descriptor discovery is required.
sahilmgandhi 18:6a4db94011d3 344 *
sahilmgandhi 18:6a4db94011d3 345 * @details This function finds out if there is a possibility of existence of descriptors between
sahilmgandhi 18:6a4db94011d3 346 * current characteristic and the next characteristic. If so, this function will compute
sahilmgandhi 18:6a4db94011d3 347 * the handle range on which the descriptors may be present and will return it.
sahilmgandhi 18:6a4db94011d3 348 * If the current characteristic is the last known characteristic, then this function
sahilmgandhi 18:6a4db94011d3 349 * will use the service end handle to find out if the current characteristic can have
sahilmgandhi 18:6a4db94011d3 350 * descriptors.
sahilmgandhi 18:6a4db94011d3 351 *
sahilmgandhi 18:6a4db94011d3 352 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 353 * @param[in] p_curr_char Pointer to the current characteristic.
sahilmgandhi 18:6a4db94011d3 354 * @param[in] p_next_char Pointer to the next characteristic. This should be NULL if the
sahilmgandhi 18:6a4db94011d3 355 * caller knows that there is no characteristic after the current
sahilmgandhi 18:6a4db94011d3 356 * characteristic at the peer.
sahilmgandhi 18:6a4db94011d3 357 * @param[out] p_handle_range Pointer to the handle range in which descriptors may exist at the
sahilmgandhi 18:6a4db94011d3 358 * the peer.
sahilmgandhi 18:6a4db94011d3 359 *
sahilmgandhi 18:6a4db94011d3 360 * @retval True If a descriptor discovery is required.
sahilmgandhi 18:6a4db94011d3 361 * @retval False If a descriptor discovery is NOT required.
sahilmgandhi 18:6a4db94011d3 362 */
sahilmgandhi 18:6a4db94011d3 363 static bool is_desc_discovery_reqd(ble_db_discovery_t * p_db_discovery,
sahilmgandhi 18:6a4db94011d3 364 ble_gatt_db_char_t * p_curr_char,
sahilmgandhi 18:6a4db94011d3 365 ble_gatt_db_char_t * p_next_char,
sahilmgandhi 18:6a4db94011d3 366 ble_gattc_handle_range_t * p_handle_range)
sahilmgandhi 18:6a4db94011d3 367 {
sahilmgandhi 18:6a4db94011d3 368 if (p_next_char == NULL)
sahilmgandhi 18:6a4db94011d3 369 {
sahilmgandhi 18:6a4db94011d3 370 // Current characteristic is the last characteristic in the service. Check if the value
sahilmgandhi 18:6a4db94011d3 371 // handle of the current characteristic is equal to the service end handle.
sahilmgandhi 18:6a4db94011d3 372 if (
sahilmgandhi 18:6a4db94011d3 373 p_curr_char->characteristic.handle_value ==
sahilmgandhi 18:6a4db94011d3 374 p_db_discovery->services[p_db_discovery->curr_srv_ind].handle_range.end_handle
sahilmgandhi 18:6a4db94011d3 375 )
sahilmgandhi 18:6a4db94011d3 376 {
sahilmgandhi 18:6a4db94011d3 377 // No descriptors can be present for the current characteristic. p_curr_char is the last
sahilmgandhi 18:6a4db94011d3 378 // characteristic with no descriptors.
sahilmgandhi 18:6a4db94011d3 379 return false;
sahilmgandhi 18:6a4db94011d3 380 }
sahilmgandhi 18:6a4db94011d3 381
sahilmgandhi 18:6a4db94011d3 382 p_handle_range->start_handle = p_curr_char->characteristic.handle_value + 1;
sahilmgandhi 18:6a4db94011d3 383
sahilmgandhi 18:6a4db94011d3 384 // Since the current characteristic is the last characteristic in the service, the end
sahilmgandhi 18:6a4db94011d3 385 // handle should be the end handle of the service.
sahilmgandhi 18:6a4db94011d3 386 p_handle_range->end_handle =
sahilmgandhi 18:6a4db94011d3 387 p_db_discovery->services[p_db_discovery->curr_srv_ind].handle_range.end_handle;
sahilmgandhi 18:6a4db94011d3 388
sahilmgandhi 18:6a4db94011d3 389 return true;
sahilmgandhi 18:6a4db94011d3 390 }
sahilmgandhi 18:6a4db94011d3 391
sahilmgandhi 18:6a4db94011d3 392 // p_next_char != NULL. Check for existence of descriptors between the current and the next
sahilmgandhi 18:6a4db94011d3 393 // characteristic.
sahilmgandhi 18:6a4db94011d3 394 if ((p_curr_char->characteristic.handle_value + 1) == p_next_char->characteristic.handle_decl)
sahilmgandhi 18:6a4db94011d3 395 {
sahilmgandhi 18:6a4db94011d3 396 // No descriptors can exist between the two characteristic.
sahilmgandhi 18:6a4db94011d3 397 return false;
sahilmgandhi 18:6a4db94011d3 398 }
sahilmgandhi 18:6a4db94011d3 399
sahilmgandhi 18:6a4db94011d3 400 p_handle_range->start_handle = p_curr_char->characteristic.handle_value + 1;
sahilmgandhi 18:6a4db94011d3 401 p_handle_range->end_handle = p_next_char->characteristic.handle_decl - 1;
sahilmgandhi 18:6a4db94011d3 402
sahilmgandhi 18:6a4db94011d3 403 return true;
sahilmgandhi 18:6a4db94011d3 404 }
sahilmgandhi 18:6a4db94011d3 405
sahilmgandhi 18:6a4db94011d3 406
sahilmgandhi 18:6a4db94011d3 407 /**@brief Function for performing characteristic discovery.
sahilmgandhi 18:6a4db94011d3 408 *
sahilmgandhi 18:6a4db94011d3 409 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 410 * @param[in] conn_handle Connection Handle.
sahilmgandhi 18:6a4db94011d3 411 *
sahilmgandhi 18:6a4db94011d3 412 * @return NRF_SUCCESS if the SoftDevice was successfully requested to perform the characteristic
sahilmgandhi 18:6a4db94011d3 413 * discovery. Otherwise an error code. This function returns the error code returned
sahilmgandhi 18:6a4db94011d3 414 * by the SoftDevice API @ref sd_ble_gattc_characteristics_discover.
sahilmgandhi 18:6a4db94011d3 415 */
sahilmgandhi 18:6a4db94011d3 416 static uint32_t characteristics_discover(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 417 uint16_t const conn_handle)
sahilmgandhi 18:6a4db94011d3 418 {
sahilmgandhi 18:6a4db94011d3 419 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 420 ble_gattc_handle_range_t handle_range;
sahilmgandhi 18:6a4db94011d3 421
sahilmgandhi 18:6a4db94011d3 422 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 423
sahilmgandhi 18:6a4db94011d3 424 if (p_db_discovery->curr_char_ind != 0)
sahilmgandhi 18:6a4db94011d3 425 {
sahilmgandhi 18:6a4db94011d3 426 // This is not the first characteristic being discovered. Hence the 'start handle' to be
sahilmgandhi 18:6a4db94011d3 427 // used must be computed using the handle_value of the previous characteristic.
sahilmgandhi 18:6a4db94011d3 428 ble_gattc_char_t * p_prev_char;
sahilmgandhi 18:6a4db94011d3 429 uint8_t prev_char_ind = p_db_discovery->curr_char_ind - 1;
sahilmgandhi 18:6a4db94011d3 430
sahilmgandhi 18:6a4db94011d3 431 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 432
sahilmgandhi 18:6a4db94011d3 433 p_prev_char = &(p_srv_being_discovered->charateristics[prev_char_ind].characteristic);
sahilmgandhi 18:6a4db94011d3 434
sahilmgandhi 18:6a4db94011d3 435 handle_range.start_handle = p_prev_char->handle_value + 1;
sahilmgandhi 18:6a4db94011d3 436 }
sahilmgandhi 18:6a4db94011d3 437 else
sahilmgandhi 18:6a4db94011d3 438 {
sahilmgandhi 18:6a4db94011d3 439 // This is the first characteristic of this service being discovered.
sahilmgandhi 18:6a4db94011d3 440 handle_range.start_handle = p_srv_being_discovered->handle_range.start_handle;
sahilmgandhi 18:6a4db94011d3 441 }
sahilmgandhi 18:6a4db94011d3 442
sahilmgandhi 18:6a4db94011d3 443 handle_range.end_handle = p_srv_being_discovered->handle_range.end_handle;
sahilmgandhi 18:6a4db94011d3 444
sahilmgandhi 18:6a4db94011d3 445 return sd_ble_gattc_characteristics_discover(conn_handle, &handle_range);
sahilmgandhi 18:6a4db94011d3 446 }
sahilmgandhi 18:6a4db94011d3 447
sahilmgandhi 18:6a4db94011d3 448
sahilmgandhi 18:6a4db94011d3 449 /**@brief Function for performing descriptor discovery, if required.
sahilmgandhi 18:6a4db94011d3 450 *
sahilmgandhi 18:6a4db94011d3 451 * @details This function will check if descriptor discovery is required and then perform it if
sahilmgandhi 18:6a4db94011d3 452 * needed. If no more descriptor discovery is required for the service, then the output
sahilmgandhi 18:6a4db94011d3 453 * parameter p_raise_discov_complete is set to true, indicating to the caller that a
sahilmgandhi 18:6a4db94011d3 454 * discovery complete event can be triggered to the application.
sahilmgandhi 18:6a4db94011d3 455 *
sahilmgandhi 18:6a4db94011d3 456 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 457 * @param[out] p_raise_discov_complete The value pointed to by this pointer will be set to true if
sahilmgandhi 18:6a4db94011d3 458 * the Discovery Complete event can be triggered to the
sahilmgandhi 18:6a4db94011d3 459 * application.
sahilmgandhi 18:6a4db94011d3 460 * @param[in] conn_handle Connection Handle.
sahilmgandhi 18:6a4db94011d3 461 *
sahilmgandhi 18:6a4db94011d3 462 * @return NRF_SUCCESS if the SoftDevice was successfully requested to perform the descriptor
sahilmgandhi 18:6a4db94011d3 463 * discovery, or if no more descriptor discovery is required. Otherwise an error code.
sahilmgandhi 18:6a4db94011d3 464 * This function returns the error code returned by the SoftDevice API @ref
sahilmgandhi 18:6a4db94011d3 465 * sd_ble_gattc_descriptors_discover.
sahilmgandhi 18:6a4db94011d3 466 */
sahilmgandhi 18:6a4db94011d3 467 static uint32_t descriptors_discover(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 468 bool * p_raise_discov_complete,
sahilmgandhi 18:6a4db94011d3 469 uint16_t const conn_handle)
sahilmgandhi 18:6a4db94011d3 470 {
sahilmgandhi 18:6a4db94011d3 471 ble_gattc_handle_range_t handle_range;
sahilmgandhi 18:6a4db94011d3 472 ble_gatt_db_char_t * p_curr_char_being_discovered;
sahilmgandhi 18:6a4db94011d3 473 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 474 bool is_discovery_reqd = false;
sahilmgandhi 18:6a4db94011d3 475
sahilmgandhi 18:6a4db94011d3 476 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 477
sahilmgandhi 18:6a4db94011d3 478 p_curr_char_being_discovered =
sahilmgandhi 18:6a4db94011d3 479 &(p_srv_being_discovered->charateristics[p_db_discovery->curr_char_ind]);
sahilmgandhi 18:6a4db94011d3 480
sahilmgandhi 18:6a4db94011d3 481 if ((p_db_discovery->curr_char_ind + 1) == p_srv_being_discovered->char_count)
sahilmgandhi 18:6a4db94011d3 482 {
sahilmgandhi 18:6a4db94011d3 483 // This is the last characteristic of this service.
sahilmgandhi 18:6a4db94011d3 484 is_discovery_reqd = is_desc_discovery_reqd(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 485 p_curr_char_being_discovered,
sahilmgandhi 18:6a4db94011d3 486 NULL,
sahilmgandhi 18:6a4db94011d3 487 &handle_range);
sahilmgandhi 18:6a4db94011d3 488 }
sahilmgandhi 18:6a4db94011d3 489 else
sahilmgandhi 18:6a4db94011d3 490 {
sahilmgandhi 18:6a4db94011d3 491 uint8_t i;
sahilmgandhi 18:6a4db94011d3 492 ble_gatt_db_char_t * p_next_char;
sahilmgandhi 18:6a4db94011d3 493
sahilmgandhi 18:6a4db94011d3 494 for (i = p_db_discovery->curr_char_ind;
sahilmgandhi 18:6a4db94011d3 495 i < p_srv_being_discovered->char_count;
sahilmgandhi 18:6a4db94011d3 496 i++)
sahilmgandhi 18:6a4db94011d3 497 {
sahilmgandhi 18:6a4db94011d3 498
sahilmgandhi 18:6a4db94011d3 499 if (i == (p_srv_being_discovered->char_count - 1))
sahilmgandhi 18:6a4db94011d3 500 {
sahilmgandhi 18:6a4db94011d3 501 // The current characteristic is the last characteristic in the service.
sahilmgandhi 18:6a4db94011d3 502 p_next_char = NULL;
sahilmgandhi 18:6a4db94011d3 503 }
sahilmgandhi 18:6a4db94011d3 504 else
sahilmgandhi 18:6a4db94011d3 505 {
sahilmgandhi 18:6a4db94011d3 506 p_next_char = &(p_srv_being_discovered->charateristics[i + 1]);
sahilmgandhi 18:6a4db94011d3 507 }
sahilmgandhi 18:6a4db94011d3 508
sahilmgandhi 18:6a4db94011d3 509 // Check if it is possible for the current characteristic to have a descriptor.
sahilmgandhi 18:6a4db94011d3 510 if (is_desc_discovery_reqd(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 511 p_curr_char_being_discovered,
sahilmgandhi 18:6a4db94011d3 512 p_next_char,
sahilmgandhi 18:6a4db94011d3 513 &handle_range))
sahilmgandhi 18:6a4db94011d3 514 {
sahilmgandhi 18:6a4db94011d3 515 is_discovery_reqd = true;
sahilmgandhi 18:6a4db94011d3 516 break;
sahilmgandhi 18:6a4db94011d3 517 }
sahilmgandhi 18:6a4db94011d3 518 else
sahilmgandhi 18:6a4db94011d3 519 {
sahilmgandhi 18:6a4db94011d3 520 // No descriptors can exist.
sahilmgandhi 18:6a4db94011d3 521 p_curr_char_being_discovered = p_next_char;
sahilmgandhi 18:6a4db94011d3 522 p_db_discovery->curr_char_ind++;
sahilmgandhi 18:6a4db94011d3 523 }
sahilmgandhi 18:6a4db94011d3 524 }
sahilmgandhi 18:6a4db94011d3 525 }
sahilmgandhi 18:6a4db94011d3 526
sahilmgandhi 18:6a4db94011d3 527 if (!is_discovery_reqd)
sahilmgandhi 18:6a4db94011d3 528 {
sahilmgandhi 18:6a4db94011d3 529 // No more descriptor discovery required. Discovery is complete.
sahilmgandhi 18:6a4db94011d3 530 // This informs the caller that a discovery complete event can be triggered.
sahilmgandhi 18:6a4db94011d3 531 *p_raise_discov_complete = true;
sahilmgandhi 18:6a4db94011d3 532
sahilmgandhi 18:6a4db94011d3 533 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 534 }
sahilmgandhi 18:6a4db94011d3 535
sahilmgandhi 18:6a4db94011d3 536 *p_raise_discov_complete = false;
sahilmgandhi 18:6a4db94011d3 537
sahilmgandhi 18:6a4db94011d3 538 return sd_ble_gattc_descriptors_discover(conn_handle, &handle_range);
sahilmgandhi 18:6a4db94011d3 539 }
sahilmgandhi 18:6a4db94011d3 540
sahilmgandhi 18:6a4db94011d3 541
sahilmgandhi 18:6a4db94011d3 542 /**@brief Function for handling primary service discovery response.
sahilmgandhi 18:6a4db94011d3 543 *
sahilmgandhi 18:6a4db94011d3 544 * @details This function will handle the primary service discovery response and start the
sahilmgandhi 18:6a4db94011d3 545 * discovery of characteristics within that service.
sahilmgandhi 18:6a4db94011d3 546 *
sahilmgandhi 18:6a4db94011d3 547 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 548 * @param[in] p_ble_gattc_evt Pointer to the GATT Client event.
sahilmgandhi 18:6a4db94011d3 549 */
sahilmgandhi 18:6a4db94011d3 550 static void on_primary_srv_discovery_rsp(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 551 const ble_gattc_evt_t * const p_ble_gattc_evt)
sahilmgandhi 18:6a4db94011d3 552 {
sahilmgandhi 18:6a4db94011d3 553 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 554 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 555
sahilmgandhi 18:6a4db94011d3 556 if (p_ble_gattc_evt->conn_handle != p_db_discovery->conn_handle)
sahilmgandhi 18:6a4db94011d3 557 {
sahilmgandhi 18:6a4db94011d3 558 return;
sahilmgandhi 18:6a4db94011d3 559 }
sahilmgandhi 18:6a4db94011d3 560 if (p_ble_gattc_evt->gatt_status == BLE_GATT_STATUS_SUCCESS)
sahilmgandhi 18:6a4db94011d3 561 {
sahilmgandhi 18:6a4db94011d3 562 uint32_t err_code;
sahilmgandhi 18:6a4db94011d3 563 const ble_gattc_evt_prim_srvc_disc_rsp_t * p_prim_srvc_disc_rsp_evt;
sahilmgandhi 18:6a4db94011d3 564
sahilmgandhi 18:6a4db94011d3 565 DB_LOG("Found service UUID 0x%x\r\n", p_srv_being_discovered->srv_uuid.uuid);
sahilmgandhi 18:6a4db94011d3 566
sahilmgandhi 18:6a4db94011d3 567 p_prim_srvc_disc_rsp_evt = &(p_ble_gattc_evt->params.prim_srvc_disc_rsp);
sahilmgandhi 18:6a4db94011d3 568
sahilmgandhi 18:6a4db94011d3 569 p_srv_being_discovered->srv_uuid = p_prim_srvc_disc_rsp_evt->services[0].uuid;
sahilmgandhi 18:6a4db94011d3 570 p_srv_being_discovered->handle_range = p_prim_srvc_disc_rsp_evt->services[0].handle_range;
sahilmgandhi 18:6a4db94011d3 571
sahilmgandhi 18:6a4db94011d3 572 err_code = characteristics_discover(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 573 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 574
sahilmgandhi 18:6a4db94011d3 575 if (err_code != NRF_SUCCESS)
sahilmgandhi 18:6a4db94011d3 576 {
sahilmgandhi 18:6a4db94011d3 577 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 578
sahilmgandhi 18:6a4db94011d3 579 // Error with discovering the service.
sahilmgandhi 18:6a4db94011d3 580 // Indicate the error to the registered user application.
sahilmgandhi 18:6a4db94011d3 581 discovery_error_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 582 err_code,
sahilmgandhi 18:6a4db94011d3 583 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 584
sahilmgandhi 18:6a4db94011d3 585 m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
sahilmgandhi 18:6a4db94011d3 586 m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
sahilmgandhi 18:6a4db94011d3 587 //m_evt_handler(&m_pending_user_evts[0].evt);
sahilmgandhi 18:6a4db94011d3 588 }
sahilmgandhi 18:6a4db94011d3 589 }
sahilmgandhi 18:6a4db94011d3 590 else
sahilmgandhi 18:6a4db94011d3 591 {
sahilmgandhi 18:6a4db94011d3 592 DB_LOG("Service UUID 0x%x Not found\r\n", p_srv_being_discovered->srv_uuid.uuid);
sahilmgandhi 18:6a4db94011d3 593 // Trigger Service Not Found event to the application.
sahilmgandhi 18:6a4db94011d3 594 discovery_complete_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 595 false,
sahilmgandhi 18:6a4db94011d3 596 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 597
sahilmgandhi 18:6a4db94011d3 598 on_srv_disc_completion(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 599 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 600 }
sahilmgandhi 18:6a4db94011d3 601 }
sahilmgandhi 18:6a4db94011d3 602
sahilmgandhi 18:6a4db94011d3 603
sahilmgandhi 18:6a4db94011d3 604 /**@brief Function for handling characteristic discovery response.
sahilmgandhi 18:6a4db94011d3 605 *
sahilmgandhi 18:6a4db94011d3 606 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 607 * @param[in] p_ble_gattc_evt Pointer to the GATT Client event.
sahilmgandhi 18:6a4db94011d3 608 */
sahilmgandhi 18:6a4db94011d3 609 static void on_characteristic_discovery_rsp(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 610 const ble_gattc_evt_t * const p_ble_gattc_evt)
sahilmgandhi 18:6a4db94011d3 611 {
sahilmgandhi 18:6a4db94011d3 612 uint32_t err_code;
sahilmgandhi 18:6a4db94011d3 613 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 614 bool perform_desc_discov = false;
sahilmgandhi 18:6a4db94011d3 615
sahilmgandhi 18:6a4db94011d3 616 if (p_ble_gattc_evt->conn_handle != p_db_discovery->conn_handle)
sahilmgandhi 18:6a4db94011d3 617 {
sahilmgandhi 18:6a4db94011d3 618 return;
sahilmgandhi 18:6a4db94011d3 619 }
sahilmgandhi 18:6a4db94011d3 620 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 621
sahilmgandhi 18:6a4db94011d3 622 if (p_ble_gattc_evt->gatt_status == BLE_GATT_STATUS_SUCCESS)
sahilmgandhi 18:6a4db94011d3 623 {
sahilmgandhi 18:6a4db94011d3 624 const ble_gattc_evt_char_disc_rsp_t * p_char_disc_rsp_evt;
sahilmgandhi 18:6a4db94011d3 625
sahilmgandhi 18:6a4db94011d3 626 p_char_disc_rsp_evt = &(p_ble_gattc_evt->params.char_disc_rsp);
sahilmgandhi 18:6a4db94011d3 627
sahilmgandhi 18:6a4db94011d3 628 // Find out the number of characteristics that were previously discovered (in earlier
sahilmgandhi 18:6a4db94011d3 629 // characteristic discovery responses, if any).
sahilmgandhi 18:6a4db94011d3 630 uint8_t num_chars_prev_disc = p_srv_being_discovered->char_count;
sahilmgandhi 18:6a4db94011d3 631
sahilmgandhi 18:6a4db94011d3 632 // Find out the number of characteristics that are currently discovered (in the
sahilmgandhi 18:6a4db94011d3 633 // characteristic discovery response being handled).
sahilmgandhi 18:6a4db94011d3 634 uint8_t num_chars_curr_disc = p_char_disc_rsp_evt->count;
sahilmgandhi 18:6a4db94011d3 635
sahilmgandhi 18:6a4db94011d3 636 // Check if the total number of discovered characteristics are supported by this module.
sahilmgandhi 18:6a4db94011d3 637 if ((num_chars_prev_disc + num_chars_curr_disc) <= BLE_GATT_DB_MAX_CHARS)
sahilmgandhi 18:6a4db94011d3 638 {
sahilmgandhi 18:6a4db94011d3 639 // Update the characteristics count.
sahilmgandhi 18:6a4db94011d3 640 p_srv_being_discovered->char_count += num_chars_curr_disc;
sahilmgandhi 18:6a4db94011d3 641 }
sahilmgandhi 18:6a4db94011d3 642 else
sahilmgandhi 18:6a4db94011d3 643 {
sahilmgandhi 18:6a4db94011d3 644 // The number of characteristics discovered at the peer is more than the supported
sahilmgandhi 18:6a4db94011d3 645 // maximum. This module will store only the characteristics found up to this point.
sahilmgandhi 18:6a4db94011d3 646 p_srv_being_discovered->char_count = BLE_GATT_DB_MAX_CHARS;
sahilmgandhi 18:6a4db94011d3 647 }
sahilmgandhi 18:6a4db94011d3 648
sahilmgandhi 18:6a4db94011d3 649 uint32_t i;
sahilmgandhi 18:6a4db94011d3 650 uint32_t j;
sahilmgandhi 18:6a4db94011d3 651
sahilmgandhi 18:6a4db94011d3 652 for (i = num_chars_prev_disc, j = 0; i < p_srv_being_discovered->char_count; i++, j++)
sahilmgandhi 18:6a4db94011d3 653 {
sahilmgandhi 18:6a4db94011d3 654 p_srv_being_discovered->charateristics[i].characteristic =
sahilmgandhi 18:6a4db94011d3 655 p_char_disc_rsp_evt->chars[j];
sahilmgandhi 18:6a4db94011d3 656
sahilmgandhi 18:6a4db94011d3 657 p_srv_being_discovered->charateristics[i].cccd_handle = BLE_GATT_HANDLE_INVALID;
sahilmgandhi 18:6a4db94011d3 658 }
sahilmgandhi 18:6a4db94011d3 659
sahilmgandhi 18:6a4db94011d3 660 ble_gattc_char_t * p_last_known_char;
sahilmgandhi 18:6a4db94011d3 661
sahilmgandhi 18:6a4db94011d3 662 p_last_known_char = &(p_srv_being_discovered->charateristics[i - 1].characteristic);
sahilmgandhi 18:6a4db94011d3 663
sahilmgandhi 18:6a4db94011d3 664 // If no more characteristic discovery is required, or if the maximum number of supported
sahilmgandhi 18:6a4db94011d3 665 // characteristic per service has been reached, descriptor discovery will be performed.
sahilmgandhi 18:6a4db94011d3 666 if (
sahilmgandhi 18:6a4db94011d3 667 !is_char_discovery_reqd(p_db_discovery, p_last_known_char) ||
sahilmgandhi 18:6a4db94011d3 668 (p_srv_being_discovered->char_count == BLE_GATT_DB_MAX_CHARS)
sahilmgandhi 18:6a4db94011d3 669 )
sahilmgandhi 18:6a4db94011d3 670 {
sahilmgandhi 18:6a4db94011d3 671 perform_desc_discov = true;
sahilmgandhi 18:6a4db94011d3 672 }
sahilmgandhi 18:6a4db94011d3 673 else
sahilmgandhi 18:6a4db94011d3 674 {
sahilmgandhi 18:6a4db94011d3 675 // Update the current characteristic index.
sahilmgandhi 18:6a4db94011d3 676 p_db_discovery->curr_char_ind = p_srv_being_discovered->char_count;
sahilmgandhi 18:6a4db94011d3 677
sahilmgandhi 18:6a4db94011d3 678 // Perform another round of characteristic discovery.
sahilmgandhi 18:6a4db94011d3 679 err_code = characteristics_discover(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 680 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 681
sahilmgandhi 18:6a4db94011d3 682 if (err_code != NRF_SUCCESS)
sahilmgandhi 18:6a4db94011d3 683 {
sahilmgandhi 18:6a4db94011d3 684 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 685
sahilmgandhi 18:6a4db94011d3 686 discovery_error_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 687 err_code,
sahilmgandhi 18:6a4db94011d3 688 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 689
sahilmgandhi 18:6a4db94011d3 690 m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
sahilmgandhi 18:6a4db94011d3 691 m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
sahilmgandhi 18:6a4db94011d3 692 //m_evt_handler(&m_pending_user_evts[0].evt);
sahilmgandhi 18:6a4db94011d3 693
sahilmgandhi 18:6a4db94011d3 694 return;
sahilmgandhi 18:6a4db94011d3 695 }
sahilmgandhi 18:6a4db94011d3 696 }
sahilmgandhi 18:6a4db94011d3 697 }
sahilmgandhi 18:6a4db94011d3 698 else
sahilmgandhi 18:6a4db94011d3 699 {
sahilmgandhi 18:6a4db94011d3 700 // The previous characteristic discovery resulted in no characteristics.
sahilmgandhi 18:6a4db94011d3 701 // descriptor discovery should be performed.
sahilmgandhi 18:6a4db94011d3 702 perform_desc_discov = true;
sahilmgandhi 18:6a4db94011d3 703 }
sahilmgandhi 18:6a4db94011d3 704
sahilmgandhi 18:6a4db94011d3 705 if (perform_desc_discov)
sahilmgandhi 18:6a4db94011d3 706 {
sahilmgandhi 18:6a4db94011d3 707 bool raise_discov_complete;
sahilmgandhi 18:6a4db94011d3 708
sahilmgandhi 18:6a4db94011d3 709 p_db_discovery->curr_char_ind = 0;
sahilmgandhi 18:6a4db94011d3 710
sahilmgandhi 18:6a4db94011d3 711 err_code = descriptors_discover(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 712 &raise_discov_complete,
sahilmgandhi 18:6a4db94011d3 713 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 714
sahilmgandhi 18:6a4db94011d3 715 if (err_code != NRF_SUCCESS)
sahilmgandhi 18:6a4db94011d3 716 {
sahilmgandhi 18:6a4db94011d3 717 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 718
sahilmgandhi 18:6a4db94011d3 719 discovery_error_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 720 err_code,
sahilmgandhi 18:6a4db94011d3 721 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 722
sahilmgandhi 18:6a4db94011d3 723 m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
sahilmgandhi 18:6a4db94011d3 724 m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
sahilmgandhi 18:6a4db94011d3 725 //m_evt_handler(&m_pending_user_evts[0].evt);
sahilmgandhi 18:6a4db94011d3 726
sahilmgandhi 18:6a4db94011d3 727 return;
sahilmgandhi 18:6a4db94011d3 728 }
sahilmgandhi 18:6a4db94011d3 729 if (raise_discov_complete)
sahilmgandhi 18:6a4db94011d3 730 {
sahilmgandhi 18:6a4db94011d3 731 // No more characteristics and descriptors need to be discovered. Discovery is complete.
sahilmgandhi 18:6a4db94011d3 732 // Send a discovery complete event to the user application.
sahilmgandhi 18:6a4db94011d3 733 DB_LOG("[DB]: Discovery of service with UUID 0x%x completed with success for Connection"
sahilmgandhi 18:6a4db94011d3 734 " handle %d\r\n", p_srv_being_discovered->srv_uuid.uuid,
sahilmgandhi 18:6a4db94011d3 735 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 736
sahilmgandhi 18:6a4db94011d3 737 discovery_complete_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 738 true,
sahilmgandhi 18:6a4db94011d3 739 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 740
sahilmgandhi 18:6a4db94011d3 741 on_srv_disc_completion(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 742 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 743 }
sahilmgandhi 18:6a4db94011d3 744 }
sahilmgandhi 18:6a4db94011d3 745 }
sahilmgandhi 18:6a4db94011d3 746
sahilmgandhi 18:6a4db94011d3 747
sahilmgandhi 18:6a4db94011d3 748 /**@brief Function for handling descriptor discovery response.
sahilmgandhi 18:6a4db94011d3 749 *
sahilmgandhi 18:6a4db94011d3 750 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 751 * @param[in] p_ble_gattc_evt Pointer to the GATT Client event.
sahilmgandhi 18:6a4db94011d3 752 */
sahilmgandhi 18:6a4db94011d3 753 static void on_descriptor_discovery_rsp(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 754 const ble_gattc_evt_t * const p_ble_gattc_evt)
sahilmgandhi 18:6a4db94011d3 755 {
sahilmgandhi 18:6a4db94011d3 756 const ble_gattc_evt_desc_disc_rsp_t * p_desc_disc_rsp_evt;
sahilmgandhi 18:6a4db94011d3 757 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 758
sahilmgandhi 18:6a4db94011d3 759 if (p_ble_gattc_evt->conn_handle != p_db_discovery->conn_handle)
sahilmgandhi 18:6a4db94011d3 760 {
sahilmgandhi 18:6a4db94011d3 761 return;
sahilmgandhi 18:6a4db94011d3 762 }
sahilmgandhi 18:6a4db94011d3 763
sahilmgandhi 18:6a4db94011d3 764 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 765
sahilmgandhi 18:6a4db94011d3 766 p_desc_disc_rsp_evt = &(p_ble_gattc_evt->params.desc_disc_rsp);
sahilmgandhi 18:6a4db94011d3 767
sahilmgandhi 18:6a4db94011d3 768 ble_gatt_db_char_t * p_char_being_discovered =
sahilmgandhi 18:6a4db94011d3 769 &(p_srv_being_discovered->charateristics[p_db_discovery->curr_char_ind]);
sahilmgandhi 18:6a4db94011d3 770
sahilmgandhi 18:6a4db94011d3 771 if (p_ble_gattc_evt->gatt_status == BLE_GATT_STATUS_SUCCESS)
sahilmgandhi 18:6a4db94011d3 772 {
sahilmgandhi 18:6a4db94011d3 773 // The descriptor was found at the peer.
sahilmgandhi 18:6a4db94011d3 774 // If the descriptor was a CCCD, then the cccd_handle needs to be populated.
sahilmgandhi 18:6a4db94011d3 775
sahilmgandhi 18:6a4db94011d3 776 uint32_t i;
sahilmgandhi 18:6a4db94011d3 777
sahilmgandhi 18:6a4db94011d3 778 // Loop through all the descriptors to find the CCCD.
sahilmgandhi 18:6a4db94011d3 779 for (i = 0; i < p_desc_disc_rsp_evt->count; i++)
sahilmgandhi 18:6a4db94011d3 780 {
sahilmgandhi 18:6a4db94011d3 781 if (
sahilmgandhi 18:6a4db94011d3 782 p_desc_disc_rsp_evt->descs[i].uuid.uuid ==
sahilmgandhi 18:6a4db94011d3 783 BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG
sahilmgandhi 18:6a4db94011d3 784 )
sahilmgandhi 18:6a4db94011d3 785 {
sahilmgandhi 18:6a4db94011d3 786 p_char_being_discovered->cccd_handle = p_desc_disc_rsp_evt->descs[i].handle;
sahilmgandhi 18:6a4db94011d3 787
sahilmgandhi 18:6a4db94011d3 788 break;
sahilmgandhi 18:6a4db94011d3 789 }
sahilmgandhi 18:6a4db94011d3 790 }
sahilmgandhi 18:6a4db94011d3 791 }
sahilmgandhi 18:6a4db94011d3 792
sahilmgandhi 18:6a4db94011d3 793 bool raise_discov_complete = false;
sahilmgandhi 18:6a4db94011d3 794
sahilmgandhi 18:6a4db94011d3 795 if ((p_db_discovery->curr_char_ind + 1) == p_srv_being_discovered->char_count)
sahilmgandhi 18:6a4db94011d3 796 {
sahilmgandhi 18:6a4db94011d3 797 // No more characteristics and descriptors need to be discovered. Discovery is complete.
sahilmgandhi 18:6a4db94011d3 798 // Send a discovery complete event to the user application.
sahilmgandhi 18:6a4db94011d3 799
sahilmgandhi 18:6a4db94011d3 800 raise_discov_complete = true;
sahilmgandhi 18:6a4db94011d3 801 }
sahilmgandhi 18:6a4db94011d3 802 else
sahilmgandhi 18:6a4db94011d3 803 {
sahilmgandhi 18:6a4db94011d3 804 // Begin discovery of descriptors for the next characteristic.
sahilmgandhi 18:6a4db94011d3 805 uint32_t err_code;
sahilmgandhi 18:6a4db94011d3 806
sahilmgandhi 18:6a4db94011d3 807 p_db_discovery->curr_char_ind++;
sahilmgandhi 18:6a4db94011d3 808
sahilmgandhi 18:6a4db94011d3 809 err_code = descriptors_discover(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 810 &raise_discov_complete,
sahilmgandhi 18:6a4db94011d3 811 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 812
sahilmgandhi 18:6a4db94011d3 813 if (err_code != NRF_SUCCESS)
sahilmgandhi 18:6a4db94011d3 814 {
sahilmgandhi 18:6a4db94011d3 815 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 816
sahilmgandhi 18:6a4db94011d3 817 // Error with discovering the service.
sahilmgandhi 18:6a4db94011d3 818 // Indicate the error to the registered user application.
sahilmgandhi 18:6a4db94011d3 819 discovery_error_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 820 err_code,
sahilmgandhi 18:6a4db94011d3 821 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 822
sahilmgandhi 18:6a4db94011d3 823 m_pending_user_evts[0].evt.evt_type = BLE_DB_DISCOVERY_AVAILABLE;
sahilmgandhi 18:6a4db94011d3 824 m_pending_user_evts[0].evt.conn_handle = p_ble_gattc_evt->conn_handle;
sahilmgandhi 18:6a4db94011d3 825
sahilmgandhi 18:6a4db94011d3 826 return;
sahilmgandhi 18:6a4db94011d3 827 }
sahilmgandhi 18:6a4db94011d3 828 }
sahilmgandhi 18:6a4db94011d3 829
sahilmgandhi 18:6a4db94011d3 830 if (raise_discov_complete)
sahilmgandhi 18:6a4db94011d3 831 {
sahilmgandhi 18:6a4db94011d3 832 DB_LOG("[DB]: Discovery of service with UUID 0x%x completed with success for Connection"
sahilmgandhi 18:6a4db94011d3 833 "handle %d\r\n", p_srv_being_discovered->srv_uuid.uuid,
sahilmgandhi 18:6a4db94011d3 834 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 835
sahilmgandhi 18:6a4db94011d3 836 discovery_complete_evt_trigger(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 837 true,
sahilmgandhi 18:6a4db94011d3 838 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 839
sahilmgandhi 18:6a4db94011d3 840 on_srv_disc_completion(p_db_discovery,
sahilmgandhi 18:6a4db94011d3 841 p_ble_gattc_evt->conn_handle);
sahilmgandhi 18:6a4db94011d3 842 }
sahilmgandhi 18:6a4db94011d3 843 }
sahilmgandhi 18:6a4db94011d3 844
sahilmgandhi 18:6a4db94011d3 845
sahilmgandhi 18:6a4db94011d3 846 uint32_t ble_db_discovery_init(const ble_db_discovery_evt_handler_t evt_handler)
sahilmgandhi 18:6a4db94011d3 847 {
sahilmgandhi 18:6a4db94011d3 848 uint32_t err_code = NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 849 VERIFY_PARAM_NOT_NULL(evt_handler);
sahilmgandhi 18:6a4db94011d3 850
sahilmgandhi 18:6a4db94011d3 851 m_num_of_handlers_reg = 0;
sahilmgandhi 18:6a4db94011d3 852 m_initialized = true;
sahilmgandhi 18:6a4db94011d3 853 m_pending_usr_evt_index = 0;
sahilmgandhi 18:6a4db94011d3 854 m_evt_handler = evt_handler;
sahilmgandhi 18:6a4db94011d3 855
sahilmgandhi 18:6a4db94011d3 856 return err_code;
sahilmgandhi 18:6a4db94011d3 857
sahilmgandhi 18:6a4db94011d3 858 }
sahilmgandhi 18:6a4db94011d3 859
sahilmgandhi 18:6a4db94011d3 860
sahilmgandhi 18:6a4db94011d3 861 uint32_t ble_db_discovery_close()
sahilmgandhi 18:6a4db94011d3 862 {
sahilmgandhi 18:6a4db94011d3 863 m_num_of_handlers_reg = 0;
sahilmgandhi 18:6a4db94011d3 864 m_initialized = false;
sahilmgandhi 18:6a4db94011d3 865 m_pending_usr_evt_index = 0;
sahilmgandhi 18:6a4db94011d3 866
sahilmgandhi 18:6a4db94011d3 867 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 868 }
sahilmgandhi 18:6a4db94011d3 869
sahilmgandhi 18:6a4db94011d3 870
sahilmgandhi 18:6a4db94011d3 871 uint32_t ble_db_discovery_evt_register(const ble_uuid_t * const p_uuid)
sahilmgandhi 18:6a4db94011d3 872 {
sahilmgandhi 18:6a4db94011d3 873 VERIFY_PARAM_NOT_NULL(p_uuid);
sahilmgandhi 18:6a4db94011d3 874 VERIFY_MODULE_INITIALIZED();
sahilmgandhi 18:6a4db94011d3 875
sahilmgandhi 18:6a4db94011d3 876 return registered_handler_set(p_uuid, m_evt_handler);
sahilmgandhi 18:6a4db94011d3 877 }
sahilmgandhi 18:6a4db94011d3 878
sahilmgandhi 18:6a4db94011d3 879
sahilmgandhi 18:6a4db94011d3 880 uint32_t ble_db_discovery_start(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 881 uint16_t conn_handle)
sahilmgandhi 18:6a4db94011d3 882 {
sahilmgandhi 18:6a4db94011d3 883 VERIFY_PARAM_NOT_NULL(p_db_discovery);
sahilmgandhi 18:6a4db94011d3 884 VERIFY_MODULE_INITIALIZED();
sahilmgandhi 18:6a4db94011d3 885
sahilmgandhi 18:6a4db94011d3 886 if (m_num_of_handlers_reg == 0)
sahilmgandhi 18:6a4db94011d3 887 {
sahilmgandhi 18:6a4db94011d3 888 // No user modules were registered. There are no services to discover.
sahilmgandhi 18:6a4db94011d3 889 return NRF_ERROR_INVALID_STATE;
sahilmgandhi 18:6a4db94011d3 890 }
sahilmgandhi 18:6a4db94011d3 891
sahilmgandhi 18:6a4db94011d3 892 if (p_db_discovery->discovery_in_progress)
sahilmgandhi 18:6a4db94011d3 893 {
sahilmgandhi 18:6a4db94011d3 894 return NRF_ERROR_BUSY;
sahilmgandhi 18:6a4db94011d3 895 }
sahilmgandhi 18:6a4db94011d3 896
sahilmgandhi 18:6a4db94011d3 897 p_db_discovery->conn_handle = conn_handle;
sahilmgandhi 18:6a4db94011d3 898 ble_gatt_db_srv_t * p_srv_being_discovered;
sahilmgandhi 18:6a4db94011d3 899
sahilmgandhi 18:6a4db94011d3 900 m_pending_usr_evt_index = 0;
sahilmgandhi 18:6a4db94011d3 901
sahilmgandhi 18:6a4db94011d3 902 p_db_discovery->discoveries_count = 0;
sahilmgandhi 18:6a4db94011d3 903 p_db_discovery->curr_srv_ind = 0;
sahilmgandhi 18:6a4db94011d3 904
sahilmgandhi 18:6a4db94011d3 905 p_srv_being_discovered = &(p_db_discovery->services[p_db_discovery->curr_srv_ind]);
sahilmgandhi 18:6a4db94011d3 906
sahilmgandhi 18:6a4db94011d3 907 p_srv_being_discovered->srv_uuid = m_registered_handlers[p_db_discovery->curr_srv_ind];
sahilmgandhi 18:6a4db94011d3 908
sahilmgandhi 18:6a4db94011d3 909 DB_LOG("[DB]: Starting discovery of service with UUID 0x%x for Connection handle %d\r\n",
sahilmgandhi 18:6a4db94011d3 910 p_srv_being_discovered->srv_uuid.uuid, conn_handle);
sahilmgandhi 18:6a4db94011d3 911
sahilmgandhi 18:6a4db94011d3 912 uint32_t err_code;
sahilmgandhi 18:6a4db94011d3 913
sahilmgandhi 18:6a4db94011d3 914 err_code = sd_ble_gattc_primary_services_discover(conn_handle,
sahilmgandhi 18:6a4db94011d3 915 SRV_DISC_START_HANDLE,
sahilmgandhi 18:6a4db94011d3 916 &(p_srv_being_discovered->srv_uuid));
sahilmgandhi 18:6a4db94011d3 917 VERIFY_SUCCESS(err_code);
sahilmgandhi 18:6a4db94011d3 918 p_db_discovery->discovery_in_progress = true;
sahilmgandhi 18:6a4db94011d3 919
sahilmgandhi 18:6a4db94011d3 920 return NRF_SUCCESS;
sahilmgandhi 18:6a4db94011d3 921 }
sahilmgandhi 18:6a4db94011d3 922
sahilmgandhi 18:6a4db94011d3 923
sahilmgandhi 18:6a4db94011d3 924 /**@brief Function for handling disconnected event.
sahilmgandhi 18:6a4db94011d3 925 *
sahilmgandhi 18:6a4db94011d3 926 * @param[in] p_db_discovery Pointer to the DB Discovery structure.
sahilmgandhi 18:6a4db94011d3 927 * @param[in] p_ble_gattc_evt Pointer to the GAP event.
sahilmgandhi 18:6a4db94011d3 928 */
sahilmgandhi 18:6a4db94011d3 929 static void on_disconnected(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 930 const ble_gap_evt_t * const p_evt)
sahilmgandhi 18:6a4db94011d3 931 {
sahilmgandhi 18:6a4db94011d3 932 if (p_evt->conn_handle == p_db_discovery->conn_handle)
sahilmgandhi 18:6a4db94011d3 933 {
sahilmgandhi 18:6a4db94011d3 934 p_db_discovery->discovery_in_progress = false;
sahilmgandhi 18:6a4db94011d3 935 }
sahilmgandhi 18:6a4db94011d3 936 }
sahilmgandhi 18:6a4db94011d3 937
sahilmgandhi 18:6a4db94011d3 938
sahilmgandhi 18:6a4db94011d3 939 void ble_db_discovery_on_ble_evt(ble_db_discovery_t * const p_db_discovery,
sahilmgandhi 18:6a4db94011d3 940 const ble_evt_t * const p_ble_evt)
sahilmgandhi 18:6a4db94011d3 941 {
sahilmgandhi 18:6a4db94011d3 942 VERIFY_PARAM_NOT_NULL_VOID(p_db_discovery);
sahilmgandhi 18:6a4db94011d3 943 VERIFY_PARAM_NOT_NULL_VOID(p_ble_evt);
sahilmgandhi 18:6a4db94011d3 944 VERIFY_MODULE_INITIALIZED_VOID();
sahilmgandhi 18:6a4db94011d3 945
sahilmgandhi 18:6a4db94011d3 946 switch (p_ble_evt->header.evt_id)
sahilmgandhi 18:6a4db94011d3 947 {
sahilmgandhi 18:6a4db94011d3 948 case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
sahilmgandhi 18:6a4db94011d3 949 on_primary_srv_discovery_rsp(p_db_discovery, &(p_ble_evt->evt.gattc_evt));
sahilmgandhi 18:6a4db94011d3 950 break;
sahilmgandhi 18:6a4db94011d3 951
sahilmgandhi 18:6a4db94011d3 952 case BLE_GATTC_EVT_CHAR_DISC_RSP:
sahilmgandhi 18:6a4db94011d3 953 on_characteristic_discovery_rsp(p_db_discovery, &(p_ble_evt->evt.gattc_evt));
sahilmgandhi 18:6a4db94011d3 954 break;
sahilmgandhi 18:6a4db94011d3 955
sahilmgandhi 18:6a4db94011d3 956 case BLE_GATTC_EVT_DESC_DISC_RSP:
sahilmgandhi 18:6a4db94011d3 957 on_descriptor_discovery_rsp(p_db_discovery, &(p_ble_evt->evt.gattc_evt));
sahilmgandhi 18:6a4db94011d3 958 break;
sahilmgandhi 18:6a4db94011d3 959
sahilmgandhi 18:6a4db94011d3 960 case BLE_GAP_EVT_DISCONNECTED:
sahilmgandhi 18:6a4db94011d3 961 on_disconnected(p_db_discovery, &(p_ble_evt->evt.gap_evt));
sahilmgandhi 18:6a4db94011d3 962 break;
sahilmgandhi 18:6a4db94011d3 963
sahilmgandhi 18:6a4db94011d3 964 default:
sahilmgandhi 18:6a4db94011d3 965 break;
sahilmgandhi 18:6a4db94011d3 966 }
sahilmgandhi 18:6a4db94011d3 967 }