es_adv_frame.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "es_adv_frame.h"
  41. #include "es_slot.h"
  42. /**@brief Function for setting advertisement data, using 'ble_advdata_encode'.
  43. *
  44. * @param[in] p_scrsp_data Scan response data.
  45. * @param[in] p_es_data_array Eddystone service data array.
  46. */
  47. static void fill_adv_data(ble_advdata_t * p_scrsp_data, uint8_array_t * p_es_data_array, ble_gap_adv_data_t * const p_adv_data)
  48. {
  49. ble_advdata_t adv_data;
  50. ret_code_t err_code;
  51. ble_uuid_t adv_uuids[] = {{ES_UUID, BLE_UUID_TYPE_BLE}};
  52. uint8_array_t es_data_array = {0};
  53. ble_advdata_service_data_t service_data; // Structure to hold Service Data.
  54. service_data.service_uuid = APP_ES_UUID; // Eddystone UUID to allow discoverability on iOS devices.
  55. service_data.data = (p_es_data_array != NULL) ? *p_es_data_array : es_data_array;
  56. // Build and set advertising data.
  57. memset(&adv_data, 0, sizeof(ble_advdata_t));
  58. adv_data.name_type = BLE_ADVDATA_NO_NAME;
  59. adv_data.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
  60. adv_data.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
  61. adv_data.uuids_complete.p_uuids = adv_uuids;
  62. adv_data.p_service_data_array = &service_data;
  63. adv_data.service_data_count = (p_es_data_array != NULL) ? 1 : 0;
  64. err_code = ble_advdata_encode(&adv_data,
  65. p_adv_data->adv_data.p_data,
  66. &p_adv_data->adv_data.len);
  67. APP_ERROR_CHECK(err_code);
  68. if (p_scrsp_data != NULL)
  69. {
  70. err_code = ble_advdata_encode(p_scrsp_data,
  71. p_adv_data->scan_rsp_data.p_data,
  72. &p_adv_data->scan_rsp_data.len);
  73. APP_ERROR_CHECK(err_code);
  74. }
  75. else
  76. {
  77. p_adv_data->scan_rsp_data.p_data = NULL;
  78. p_adv_data->scan_rsp_data.len = 0;
  79. }
  80. }
  81. void es_adv_frame_fill_connectable_adv_data(ble_advdata_t * p_scrsp_data, ble_gap_adv_data_t * const p_adv_data)
  82. {
  83. fill_adv_data(p_scrsp_data, NULL, p_adv_data);
  84. }
  85. void es_adv_frame_fill_non_connectable_adv_data(uint8_t slot_no, bool etlm, ble_gap_adv_data_t * const p_adv_data)
  86. {
  87. uint8_array_t es_data_array = {0};
  88. const es_slot_reg_t * p_reg = es_slot_get_registry();
  89. if (etlm)
  90. {
  91. es_slot_etlm_update(slot_no);
  92. // If eTLM, the incoming slot_no points to the corresponding EID slot, update to point to TLM slot.
  93. slot_no = p_reg->tlm_slot;
  94. }
  95. // If TLM, update the TLM data.
  96. else if (p_reg->slots[slot_no].adv_frame.type == ES_FRAME_TYPE_TLM)
  97. {
  98. es_slot_tlm_update();
  99. }
  100. es_data_array.p_data = (uint8_t *)&p_reg->slots[slot_no].adv_frame.frame;
  101. es_data_array.size = p_reg->slots[slot_no].adv_frame.length;
  102. fill_adv_data(NULL, &es_data_array, p_adv_data);
  103. }