ble_lbs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Copyright (c) 2013 - 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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(BLE_LBS)
  42. #include "ble_lbs.h"
  43. #include "ble_srv_common.h"
  44. /**@brief Function for handling the Write event.
  45. *
  46. * @param[in] p_lbs LED Button Service structure.
  47. * @param[in] p_ble_evt Event received from the BLE stack.
  48. */
  49. static void on_write(ble_lbs_t * p_lbs, ble_evt_t const * p_ble_evt)
  50. {
  51. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  52. if ( (p_evt_write->handle == p_lbs->led_char_handles.value_handle)
  53. && (p_evt_write->len == 1)
  54. && (p_lbs->led_write_handler != NULL))
  55. {
  56. p_lbs->led_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_lbs, p_evt_write->data[0]);
  57. }
  58. }
  59. void ble_lbs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  60. {
  61. ble_lbs_t * p_lbs = (ble_lbs_t *)p_context;
  62. switch (p_ble_evt->header.evt_id)
  63. {
  64. case BLE_GATTS_EVT_WRITE:
  65. on_write(p_lbs, p_ble_evt);
  66. break;
  67. default:
  68. // No implementation needed.
  69. break;
  70. }
  71. }
  72. uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
  73. {
  74. uint32_t err_code;
  75. ble_uuid_t ble_uuid;
  76. ble_add_char_params_t add_char_params;
  77. // Initialize service structure.
  78. p_lbs->led_write_handler = p_lbs_init->led_write_handler;
  79. // Add service.
  80. ble_uuid128_t base_uuid = {LBS_UUID_BASE};
  81. err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);
  82. VERIFY_SUCCESS(err_code);
  83. ble_uuid.type = p_lbs->uuid_type;
  84. ble_uuid.uuid = LBS_UUID_SERVICE;
  85. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);
  86. VERIFY_SUCCESS(err_code);
  87. // Add Button characteristic.
  88. memset(&add_char_params, 0, sizeof(add_char_params));
  89. add_char_params.uuid = LBS_UUID_BUTTON_CHAR;
  90. add_char_params.uuid_type = p_lbs->uuid_type;
  91. add_char_params.init_len = sizeof(uint8_t);
  92. add_char_params.max_len = sizeof(uint8_t);
  93. add_char_params.char_props.read = 1;
  94. add_char_params.char_props.notify = 1;
  95. add_char_params.read_access = SEC_OPEN;
  96. add_char_params.cccd_write_access = SEC_OPEN;
  97. err_code = characteristic_add(p_lbs->service_handle,
  98. &add_char_params,
  99. &p_lbs->button_char_handles);
  100. if (err_code != NRF_SUCCESS)
  101. {
  102. return err_code;
  103. }
  104. // Add LED characteristic.
  105. memset(&add_char_params, 0, sizeof(add_char_params));
  106. add_char_params.uuid = LBS_UUID_LED_CHAR;
  107. add_char_params.uuid_type = p_lbs->uuid_type;
  108. add_char_params.init_len = sizeof(uint8_t);
  109. add_char_params.max_len = sizeof(uint8_t);
  110. add_char_params.char_props.read = 1;
  111. add_char_params.char_props.write = 1;
  112. add_char_params.read_access = SEC_OPEN;
  113. add_char_params.write_access = SEC_OPEN;
  114. return characteristic_add(p_lbs->service_handle, &add_char_params, &p_lbs->led_char_handles);
  115. }
  116. uint32_t ble_lbs_on_button_change(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t button_state)
  117. {
  118. ble_gatts_hvx_params_t params;
  119. uint16_t len = sizeof(button_state);
  120. memset(&params, 0, sizeof(params));
  121. params.type = BLE_GATT_HVX_NOTIFICATION;
  122. params.handle = p_lbs->button_char_handles.value_handle;
  123. params.p_data = &button_state;
  124. params.p_len = &len;
  125. return sd_ble_gatts_hvx(conn_handle, &params);
  126. }
  127. #endif // NRF_MODULE_ENABLED(BLE_LBS)