ble_lbs_c.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. *
  42. * @defgroup ble_lbs_c LED Button Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief The LED Button Service client can be used to set up a LED and read a button state on a
  46. * LED button service server.
  47. *
  48. * @details This module contains the APIs and types exposed by the LED Button Service Client
  49. * module. The application can use these APIs and types to perform the discovery of
  50. * LED Button Service at the peer and to interact with it.
  51. *
  52. * @note The application must register this module as the BLE event observer by using the
  53. * NRF_SDH_BLE_OBSERVER macro. Example:
  54. * @code
  55. * ble_lbs_c_t instance;
  56. * NRF_SDH_BLE_OBSERVER(anything, BLE_LBS_C_BLE_OBSERVER_PRIO,
  57. * ble_lbs_c_on_ble_evt, &instance);
  58. * @endcode
  59. */
  60. #ifndef BLE_LBS_C_H__
  61. #define BLE_LBS_C_H__
  62. #include <stdint.h>
  63. #include "ble.h"
  64. #include "ble_db_discovery.h"
  65. #include "ble_srv_common.h"
  66. #include "nrf_ble_gq.h"
  67. #include "nrf_sdh_ble.h"
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. /**@brief Macro for defining a ble_lbs_c instance.
  72. *
  73. * @param _name Name of the instance.
  74. * @hideinitializer
  75. */
  76. #define BLE_LBS_C_DEF(_name) \
  77. static ble_lbs_c_t _name; \
  78. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  79. BLE_LBS_C_BLE_OBSERVER_PRIO, \
  80. ble_lbs_c_on_ble_evt, &_name)
  81. /**@brief Macro for defining multiple ble_lbs_c instances.
  82. *
  83. * @param _name Name of the array of instances.
  84. * @param _cnt Number of instances to define.
  85. */
  86. #define BLE_LBS_C_ARRAY_DEF(_name, _cnt) \
  87. static ble_lbs_c_t _name[_cnt]; \
  88. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  89. BLE_LBS_C_BLE_OBSERVER_PRIO, \
  90. ble_lbs_c_on_ble_evt, &_name, _cnt)
  91. #define LBS_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, \
  92. 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00}
  93. #define LBS_UUID_SERVICE 0x1523
  94. #define LBS_UUID_BUTTON_CHAR 0x1524
  95. #define LBS_UUID_LED_CHAR 0x1525
  96. /**@brief LBS Client event type. */
  97. typedef enum
  98. {
  99. BLE_LBS_C_EVT_DISCOVERY_COMPLETE = 1, /**< Event indicating that the LED Button Service was discovered at the peer. */
  100. BLE_LBS_C_EVT_BUTTON_NOTIFICATION /**< Event indicating that a notification of the LED Button Button characteristic was received from the peer. */
  101. } ble_lbs_c_evt_type_t;
  102. /**@brief Structure containing the Button value received from the peer. */
  103. typedef struct
  104. {
  105. uint8_t button_state; /**< Button Value. */
  106. } ble_button_t;
  107. /**@brief Structure containing the handles related to the LED Button Service found on the peer. */
  108. typedef struct
  109. {
  110. uint16_t button_cccd_handle; /**< Handle of the CCCD of the Button characteristic. */
  111. uint16_t button_handle; /**< Handle of the Button characteristic as provided by the SoftDevice. */
  112. uint16_t led_handle; /**< Handle of the LED characteristic as provided by the SoftDevice. */
  113. } lbs_db_t;
  114. /**@brief LED Button Event structure. */
  115. typedef struct
  116. {
  117. ble_lbs_c_evt_type_t evt_type; /**< Type of the event. */
  118. uint16_t conn_handle; /**< Connection handle on which the event occured.*/
  119. union
  120. {
  121. ble_button_t button; /**< Button value received. This is filled if the evt_type is @ref BLE_LBS_C_EVT_BUTTON_NOTIFICATION. */
  122. lbs_db_t peer_db; /**< Handles related to the LED Button Service found on the peer device. This is filled if the evt_type is @ref BLE_LBS_C_EVT_DISCOVERY_COMPLETE.*/
  123. } params;
  124. } ble_lbs_c_evt_t;
  125. // Forward declaration of the ble_lbs_c_t type.
  126. typedef struct ble_lbs_c_s ble_lbs_c_t;
  127. /**@brief Event handler type.
  128. *
  129. * @details This is the type of the event handler that is to be provided by the application
  130. * of this module in order to receive events.
  131. */
  132. typedef void (* ble_lbs_c_evt_handler_t) (ble_lbs_c_t * p_ble_lbs_c, ble_lbs_c_evt_t * p_evt);
  133. /**@brief LED Button Client structure. */
  134. struct ble_lbs_c_s
  135. {
  136. uint16_t conn_handle; /**< Connection handle as provided by the SoftDevice. */
  137. lbs_db_t peer_lbs_db; /**< Handles related to LBS on the peer. */
  138. ble_lbs_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the LED Button service. */
  139. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  140. uint8_t uuid_type; /**< UUID type. */
  141. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  142. };
  143. /**@brief LED Button Client initialization structure. */
  144. typedef struct
  145. {
  146. ble_lbs_c_evt_handler_t evt_handler; /**< Event handler to be called by the LED Button Client module when there is an event related to the LED Button Service. */
  147. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  148. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  149. } ble_lbs_c_init_t;
  150. /**@brief Function for initializing the LED Button client module.
  151. *
  152. * @details This function registers with the Database Discovery module for the
  153. * LED Button Service. The module looks for the presence of a LED Button Service instance
  154. * at the peer when a discovery is started.
  155. *
  156. * @param[in] p_ble_lbs_c Pointer to the LED Button client structure.
  157. * @param[in] p_ble_lbs_c_init Pointer to the LED Button initialization structure that contains the
  158. * initialization information.
  159. *
  160. * @retval NRF_SUCCESS On successful initialization.
  161. * @retval err_code Otherwise, this function propagates the error code returned by the Database Discovery module API
  162. * @ref ble_db_discovery_evt_register.
  163. */
  164. uint32_t ble_lbs_c_init(ble_lbs_c_t * p_ble_lbs_c, ble_lbs_c_init_t * p_ble_lbs_c_init);
  165. /**@brief Function for handling BLE events from the SoftDevice.
  166. *
  167. * @details This function handles the BLE events received from the SoftDevice. If a BLE event
  168. * is relevant to the LED Button Client module, the function uses the event's data to update interval
  169. * variables and, if necessary, send events to the application.
  170. *
  171. * @param[in] p_ble_evt Pointer to the BLE event.
  172. * @param[in] p_context Pointer to the LED button client structure.
  173. */
  174. void ble_lbs_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  175. /**@brief Function for requesting the peer to start sending notification of the Button
  176. * Characteristic.
  177. *
  178. * @details This function enables to notification of the Button at the peer
  179. * by writing to the CCCD of the Button characteristic.
  180. *
  181. * @param[in] p_ble_lbs_c Pointer to the LED Button Client structure.
  182. *
  183. * @retval NRF_SUCCESS If the SoftDevice has been requested to write to the CCCD of the peer.
  184. * @retval NRF_ERROR_INVALID_STATE If no connection handle has been assigned (@ref ble_lbs_c_handles_assign).
  185. * @retval NRF_ERROR_NULL If the given parameter is NULL.
  186. * @retval err_code Otherwise, this API propagates the error code returned by function
  187. * @ref nrf_ble_gq_item_add.
  188. */
  189. uint32_t ble_lbs_c_button_notif_enable(ble_lbs_c_t * p_ble_lbs_c);
  190. /**@brief Function for handling events from the Database Discovery module.
  191. *
  192. * @details Call this function when you get a callback event from the Database Discovery module. This
  193. * function handles an event from the Database Discovery module, and determines whether it
  194. * relates to the discovery of LED Button service at the peer. If it does, this function calls the
  195. * application's event handler to indicate that the LED Button service was discovered
  196. * at the peer. The function also populates the event with service-related information before
  197. * providing it to the application.
  198. *
  199. * @param[in] p_ble_lbs_c Pointer to the LED Button client structure.
  200. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  201. */
  202. void ble_lbs_on_db_disc_evt(ble_lbs_c_t * p_ble_lbs_c, const ble_db_discovery_evt_t * p_evt);
  203. /**@brief Function for assigning handles to this instance of lbs_c.
  204. *
  205. * @details Call this function when a link has been established with a peer to associate the link
  206. * to this instance of the module. This makes it possible to handle several links and
  207. * associate each link to a particular instance of this module.
  208. *
  209. * @param[in] p_ble_lbs_c Pointer to the LED Button client structure instance for associating the link.
  210. * @param[in] conn_handle Connection handle to associate with the given LED Button Client Instance.
  211. * @param[in] p_peer_handles LED Button Service handles found on the peer (from @ref BLE_LBS_C_EVT_DISCOVERY_COMPLETE event).
  212. *
  213. * @retval NRF_SUCCESS If the status was sent successfully.
  214. * @retval err_code Otherwise, this API propagates the error code returned by function
  215. * @ref nrf_ble_gq_item_add.
  216. *
  217. */
  218. uint32_t ble_lbs_c_handles_assign(ble_lbs_c_t * p_ble_lbs_c,
  219. uint16_t conn_handle,
  220. const lbs_db_t * p_peer_handles);
  221. /**@brief Function for writing the LED status to the connected server.
  222. *
  223. * @param[in] p_ble_lbs_c Pointer to the LED Button client structure.
  224. * @param[in] status LED status to send.
  225. *
  226. * @retval NRF_SUCCESS If the status was sent successfully.
  227. * @retval err_code Otherwise, this API propagates the error code returned by function
  228. * @ref nrf_ble_gq_conn_handle_register.
  229. */
  230. uint32_t ble_lbs_led_status_send(ble_lbs_c_t * p_ble_lbs_c, uint8_t status);
  231. #ifdef __cplusplus
  232. }
  233. #endif
  234. #endif // BLE_LBS_C_H__
  235. /** @} */