ble_lbs.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (c) 2015 - 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 LED Button Service Server
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. *
  46. * @brief LED Button Service Server module.
  47. *
  48. * @details This module implements a custom LED Button Service with an LED and Button Characteristics.
  49. * During initialization, the module adds the LED Button Service and Characteristics
  50. * to the BLE stack database.
  51. *
  52. * The application must supply an event handler for receiving LED Button Service
  53. * events. Using this handler, the service notifies the application when the
  54. * LED value changes.
  55. *
  56. * The service also provides a function for letting the application notify
  57. * the state of the Button Characteristic to connected peers.
  58. *
  59. * @note The application must register this module as BLE event observer using the
  60. * NRF_SDH_BLE_OBSERVER macro. Example:
  61. * @code
  62. * ble_hids_t instance;
  63. * NRF_SDH_BLE_OBSERVER(anything, BLE_HIDS_BLE_OBSERVER_PRIO,
  64. * ble_hids_on_ble_evt, &instance);
  65. * @endcode
  66. */
  67. #ifndef BLE_LBS_H__
  68. #define BLE_LBS_H__
  69. #include <stdint.h>
  70. #include <stdbool.h>
  71. #include "ble.h"
  72. #include "ble_srv_common.h"
  73. #include "nrf_sdh_ble.h"
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. /**@brief Macro for defining a ble_lbs instance.
  78. *
  79. * @param _name Name of the instance.
  80. * @hideinitializer
  81. */
  82. #define BLE_LBS_DEF(_name) \
  83. static ble_lbs_t _name; \
  84. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  85. BLE_LBS_BLE_OBSERVER_PRIO, \
  86. ble_lbs_on_ble_evt, &_name)
  87. #define LBS_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, \
  88. 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00}
  89. #define LBS_UUID_SERVICE 0x1523
  90. #define LBS_UUID_BUTTON_CHAR 0x1524
  91. #define LBS_UUID_LED_CHAR 0x1525
  92. // Forward declaration of the ble_lbs_t type.
  93. typedef struct ble_lbs_s ble_lbs_t;
  94. typedef void (*ble_lbs_led_write_handler_t) (uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t new_state);
  95. /** @brief LED Button Service init structure. This structure contains all options and data needed for
  96. * initialization of the service.*/
  97. typedef struct
  98. {
  99. ble_lbs_led_write_handler_t led_write_handler; /**< Event handler to be called when the LED Characteristic is written. */
  100. } ble_lbs_init_t;
  101. /**@brief LED Button Service structure. This structure contains various status information for the service. */
  102. struct ble_lbs_s
  103. {
  104. uint16_t service_handle; /**< Handle of LED Button Service (as provided by the BLE stack). */
  105. ble_gatts_char_handles_t led_char_handles; /**< Handles related to the LED Characteristic. */
  106. ble_gatts_char_handles_t button_char_handles; /**< Handles related to the Button Characteristic. */
  107. uint8_t uuid_type; /**< UUID type for the LED Button Service. */
  108. ble_lbs_led_write_handler_t led_write_handler; /**< Event handler to be called when the LED Characteristic is written. */
  109. };
  110. /**@brief Function for initializing the LED Button Service.
  111. *
  112. * @param[out] p_lbs LED Button Service structure. This structure must be supplied by
  113. * the application. It is initialized by this function and will later
  114. * be used to identify this particular service instance.
  115. * @param[in] p_lbs_init Information needed to initialize the service.
  116. *
  117. * @retval NRF_SUCCESS If the service was initialized successfully. Otherwise, an error code is returned.
  118. */
  119. uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init);
  120. /**@brief Function for handling the application's BLE stack events.
  121. *
  122. * @details This function handles all events from the BLE stack that are of interest to the LED Button Service.
  123. *
  124. * @param[in] p_ble_evt Event received from the BLE stack.
  125. * @param[in] p_context LED Button Service structure.
  126. */
  127. void ble_lbs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  128. /**@brief Function for sending a button state notification.
  129. *
  130. ' @param[in] conn_handle Handle of the peripheral connection to which the button state notification will be sent.
  131. * @param[in] p_lbs LED Button Service structure.
  132. * @param[in] button_state New button state.
  133. *
  134. * @retval NRF_SUCCESS If the notification was sent successfully. Otherwise, an error code is returned.
  135. */
  136. uint32_t ble_lbs_on_button_change(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t button_state);
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif // BLE_LBS_H__
  141. /** @} */