ble_owned_c.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef BLE_OWNED_C_H__
  2. #define BLE_OWNED_C_H__
  3. #include "sdk_common.h"
  4. #include "ble_config.h"
  5. #include "ble_owned_c.h"
  6. #include "ble_db_discovery.h"
  7. #include "ble_types.h"
  8. #include "ble_srv_common.h"
  9. #include "ble_gattc.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**@brief Macro for defining a ble_owned_c instance.
  14. *
  15. * @param _name Name of the instance.
  16. * @hideinitializer
  17. */
  18. #define BLE_OWNED_C_DEF(_name) \
  19. static ble_owned_c_t _name; \
  20. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  21. BLE_OWNED_C_BLE_OBSERVER_PRIO, \
  22. ble_owned_c_on_ble_evt, &_name)
  23. /** @brief Macro for defining multiple ble_owned_c instances.
  24. *
  25. * @param _name Name of the array of instances.
  26. * @param _cnt Number of instances to define.
  27. * @hideinitializer
  28. */
  29. #define BLE_OWNED_C_ARRAY_DEF(_name, _cnt) \
  30. static ble_owned_c_t _name[_cnt]; \
  31. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  32. BLE_OWNED_C_BLE_OBSERVER_PRIO, \
  33. ble_owned_c_on_ble_evt, &_name, _cnt)
  34. /**
  35. * @defgroup wechat_c_enums Enumerations
  36. * @{
  37. */
  38. #ifndef BLE_UUID_OWNED_BASE
  39. #ifdef BLE_UUID_WECHAT_SERVICE
  40. #define BLE_UUID_OWNED_BASE BLE_UUID_WECHAT_SERVICE
  41. #else
  42. #define BLE_UUID_OWNED_BASE 0xFEE7
  43. #endif
  44. #endif
  45. #ifndef BLE_UUID_INDICATE_CHARACTERISTICS
  46. #define BLE_UUID_INDICATE_CHARACTERISTICS 0xFECA
  47. #endif
  48. #ifndef BLE_OWNED_MAX_DATA_LEN
  49. #define BLE_OWNED_MAX_DATA_LEN 64
  50. #endif
  51. /**@brief OWNED Client event type. */
  52. typedef enum
  53. {
  54. BLE_OWNED_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the Owned service and its characteristics was found. */
  55. BLE_OWNED_C_EVT_OWNED_WR_EVT, /**< Event indicating that the central has received something from a peer. */
  56. BLE_OWNED_C_EVT_DISCONNECTED, /**< Event indicating that the OWNED server has disconnected. */
  57. }ble_owned_c_evt_type_t;
  58. /**@brief Handles on the connected peer device needed to interact with it. */
  59. typedef struct
  60. {
  61. uint16_t owned_wr_handle; /**< Handle of the write characteristic as provided by a discovery. */
  62. uint16_t owned_wr_cccd_handle; /**< Handle of the CCCD of the OWNED TX characteristic as provided by a discovery. */
  63. }ble_owned_c_handles_t;
  64. /**@brief Structure containing the OWNED event data received from the peer. */
  65. typedef struct
  66. {
  67. ble_owned_c_evt_type_t evt_type;
  68. uint16_t conn_handle;
  69. uint16_t max_data_len;
  70. uint8_t *p_data;
  71. uint16_t data_len;
  72. ble_owned_c_handles_t handles; /**< Handles on which the Nordic Uart service characteristics was discovered on the peer device. This will be filled if the evt_type is @ref ble_owned_C_EVT_DISCOVERY_COMPLETE.*/
  73. } ble_owned_c_evt_t;
  74. // Forward declaration of the ble_owned_t type.
  75. typedef struct ble_owned_c_s ble_owned_c_t;
  76. /**@brief Event handler type.
  77. *
  78. * @details This is the type of the event handler that should be provided by the application
  79. * of this module to receive events.
  80. */
  81. typedef void (* ble_owned_c_evt_handler_t)(ble_owned_c_t * p_ble_owned_c, ble_owned_c_evt_t const * p_evt);
  82. /**@brief OWNED Client structure. */
  83. struct ble_owned_c_s
  84. {
  85. uint8_t uuid_type; /**< UUID type. */
  86. uint16_t conn_handle; /**< Handle of the current connection. Set with @ref ble_owned_c_handles_assign when connected. */
  87. ble_owned_c_handles_t handles; /**< Handles on the connected peer device needed to interact with it. */
  88. ble_owned_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the OWNED. */
  89. };
  90. /**@brief OWNED Client initialization structure. */
  91. typedef struct
  92. {
  93. ble_owned_c_evt_handler_t evt_handler;
  94. } ble_owned_c_init_t;
  95. /**@brief Function for initializing the Nordic UART client module.
  96. *
  97. * @details This function registers with the Database Discovery module
  98. * for the OWNED. Doing so will make the Database Discovery
  99. * module look for the presence of a OWNED instance at the peer when a
  100. * discovery is started.
  101. *
  102. * @param[in] p_ble_owned_c Pointer to the OWNED client structure.
  103. * @param[in] p_ble_owned_c_init Pointer to the OWNED initialization structure containing the
  104. * initialization information.
  105. *
  106. * @retval NRF_SUCCESS If the module was initialized successfully. Otherwise, an error
  107. * code is returned. This function
  108. * propagates the error code returned by the Database Discovery module API
  109. * @ref ble_db_discovery_evt_register.
  110. */
  111. uint32_t ble_owned_c_init(ble_owned_c_t * p_ble_owned_c, ble_owned_c_init_t * p_ble_owned_c_init);
  112. /**@brief Function for handling events from the database discovery module.
  113. *
  114. * @details This function will handle an event from the database discovery module, and determine
  115. * if it relates to the discovery of OWNED at the peer. If so, it will
  116. * call the application's event handler indicating that OWNED has been
  117. * discovered at the peer. It also populates the event with the service related
  118. * information before providing it to the application.
  119. *
  120. * @param[in] p_ble_owned_c Pointer to the OWNED client structure.
  121. * @param[in] p_evt Pointer to the event received from the database discovery module.
  122. */
  123. void ble_owned_c_on_db_disc_evt(ble_owned_c_t * p_ble_owned_c, ble_db_discovery_evt_t * p_evt);
  124. /**@brief Function for handling BLE events from the SoftDevice.
  125. *
  126. * @details This function handles the BLE events received from the SoftDevice. If a BLE
  127. * event is relevant to the OWNED module, it is used to update
  128. * internal variables and, if necessary, send events to the application.
  129. *
  130. * @param[in] p_ble_evt Pointer to the BLE event.
  131. * @param[in] p_context Pointer to the OWNED client structure.
  132. */
  133. void ble_owned_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  134. /**@brief Function for requesting the peer to start sending notification of TX characteristic.
  135. *
  136. * @details This function enables notifications of the OWNED TX characteristic at the peer
  137. * by writing to the CCCD of the OWNED TX characteristic.
  138. *
  139. * @param p_ble_owned_c Pointer to the OWNED client structure.
  140. *
  141. * @retval NRF_SUCCESS If the SoftDevice has been requested to write to the CCCD of the peer.
  142. * Otherwise, an error code is returned. This function propagates the error
  143. * code returned by the SoftDevice API @ref sd_ble_gattc_write.
  144. */
  145. uint32_t ble_owned_c_wr_notif_enable(ble_owned_c_t * p_ble_owned_c);
  146. /**@brief Function for sending a string to the server.
  147. *
  148. * @details This function writes the RX characteristic of the server.
  149. *
  150. * @param[in] p_ble_owned_c Pointer to the OWNED client structure.
  151. * @param[in] p_string String to be sent.
  152. * @param[in] length Length of the string.
  153. *
  154. * @retval NRF_SUCCESS If the string was sent successfully. Otherwise, an error code is returned.
  155. */
  156. uint32_t ble_owned_c_data_send(ble_owned_c_t * p_ble_owned_c, uint8_t * p_byte, uint16_t length);
  157. /**@brief Function for assigning handles to a this instance of OWNED_c.
  158. *
  159. * @details Call this function when a link has been established with a peer to
  160. * associate this link to this instance of the module. This makes it
  161. * possible to handle several link and associate each link to a particular
  162. * instance of this module. The connection handle and attribute handles will be
  163. * provided from the discovery event @ref ble_owned_C_EVT_DISCOVERY_COMPLETE.
  164. *
  165. * @param[in] p_ble_owned_c Pointer to the OWNED client structure instance to associate with these
  166. * handles.
  167. * @param[in] conn_handle Connection handle to associated with the given OWNED Instance.
  168. * @param[in] p_peer_handles Attribute handles on the OWNED server that you want this OWNED client to
  169. * interact with.
  170. *
  171. * @retval NRF_SUCCESS If the operation was successful.
  172. * @retval NRF_ERROR_NULL If a p_OWNED was a NULL pointer.
  173. */
  174. uint32_t ble_owned_c_handles_assign(ble_owned_c_t * p_ble_owned_c,
  175. uint16_t conn_handle,
  176. ble_owned_c_handles_t const * p_peer_handles);
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif // ble_owned_C_H__
  181. /** @} */ // End tag for the file.