ble_tps.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2012 - 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. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "sdk_common.h"
  45. #if NRF_MODULE_ENABLED(BLE_TPS)
  46. #include "ble_tps.h"
  47. #include <string.h>
  48. #include "ble_srv_common.h"
  49. /**@brief Function for handling the Connect event.
  50. *
  51. * @param[in] p_tps TX Power Service structure.
  52. * @param[in] p_ble_evt Event received from the BLE stack.
  53. */
  54. static void on_connect(ble_tps_t * p_tps, ble_evt_t const * p_ble_evt)
  55. {
  56. p_tps->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  57. }
  58. void ble_tps_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  59. {
  60. ble_tps_t * p_tps = (ble_tps_t *)p_context;
  61. switch (p_ble_evt->header.evt_id)
  62. {
  63. case BLE_GAP_EVT_CONNECTED:
  64. on_connect(p_tps, p_ble_evt);
  65. break;
  66. default:
  67. // No implementation needed.
  68. break;
  69. }
  70. }
  71. uint32_t ble_tps_init(ble_tps_t * p_tps, const ble_tps_init_t * p_tps_init)
  72. {
  73. uint32_t err_code;
  74. ble_uuid_t ble_uuid;
  75. ble_add_char_params_t add_char_params;
  76. // Add service
  77. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_TX_POWER_SERVICE);
  78. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  79. &ble_uuid,
  80. &p_tps->service_handle);
  81. if (err_code != NRF_SUCCESS)
  82. {
  83. return err_code;
  84. }
  85. // Add TX Power Level characteristic
  86. memset(&add_char_params, 0, sizeof(add_char_params));
  87. add_char_params.uuid = BLE_UUID_TX_POWER_LEVEL_CHAR;
  88. add_char_params.max_len = sizeof(uint8_t);
  89. add_char_params.init_len = sizeof(uint8_t);
  90. add_char_params.p_init_value = (uint8_t *) &p_tps_init->initial_tx_power_level;
  91. add_char_params.char_props.read = 1;
  92. add_char_params.read_access = p_tps_init->tpl_rd_sec;
  93. return characteristic_add(p_tps->service_handle,
  94. &add_char_params,
  95. &p_tps->tx_power_level_handles);
  96. }
  97. uint32_t ble_tps_tx_power_level_set(ble_tps_t * p_tps, int8_t tx_power_level)
  98. {
  99. ble_gatts_value_t gatts_value;
  100. // Initialize value struct.
  101. memset(&gatts_value, 0, sizeof(gatts_value));
  102. gatts_value.len = sizeof(uint8_t);
  103. gatts_value.offset = 0;
  104. gatts_value.p_value = (uint8_t*)&tx_power_level;
  105. // Update database
  106. return sd_ble_gatts_value_set(p_tps->conn_handle,
  107. p_tps->tx_power_level_handles.value_handle,
  108. &gatts_value);
  109. }
  110. #endif // NRF_MODULE_ENABLED(BLE_TPS)