es_slot_reg.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_slot_reg.h"
  41. #include "es_security.h"
  42. #define ES_SLOT_NOT_CONFIGURED 0xab /** Value set in configured lists to indicate not configured slot. */
  43. /**@brief Function updating 'tlm_configured' property of slot registry when slot is being cleared.
  44. *
  45. * @param[in] p_reg Pointer to slot registry.
  46. * @param[in] slot_no Slot number to be used.
  47. */
  48. static void update_tlm_configured_on_clearing(es_slot_reg_t * p_reg, uint8_t slot_no)
  49. {
  50. if (p_reg->tlm_configured && slot_no == p_reg->tlm_slot)
  51. {
  52. p_reg->tlm_configured = false;
  53. }
  54. }
  55. /**@brief Function updating 'num_configured_slots' and 'slots_configured' properties of slot registry when slot is being cleared.
  56. *
  57. * @param[in] p_configured Pointer to list of configured slots.
  58. * @param[in] p_num_configured_slots Pointer to number of configured slots.
  59. * @param[in] slot_no Slot number to clear.
  60. */
  61. static void configured_slots_on_clear_update(uint8_t * p_configured, uint8_t * p_num_configured_slots, uint8_t slot_no)
  62. {
  63. uint8_t index_of_last_configured_slot = *p_num_configured_slots - 1;
  64. for (uint32_t i = 0; i < APP_MAX_ADV_SLOTS; ++i)
  65. {
  66. if (p_configured[i] == slot_no)
  67. {
  68. // Copy all values 'to the right' of the cleared slot one step to the left.
  69. if (i < index_of_last_configured_slot)
  70. {
  71. for (uint32_t j = i; j < index_of_last_configured_slot; ++j)
  72. {
  73. p_configured[j] = p_configured[j + 1];
  74. }
  75. // Write ES_SLOT_NOT_CONFIGURED to all rightmost not configured indexes.
  76. memset(&p_configured[index_of_last_configured_slot],
  77. ES_SLOT_NOT_CONFIGURED,
  78. APP_MAX_ADV_SLOTS - index_of_last_configured_slot);
  79. }
  80. else
  81. {
  82. // There are no values 'to the right', simply overwrite with ES_SLOT_NOT_CONFIGURED
  83. p_configured[i] = ES_SLOT_NOT_CONFIGURED;
  84. }
  85. *p_num_configured_slots -= 1;
  86. return;
  87. }
  88. }
  89. }
  90. bool es_slot_reg_etlm_required(const es_slot_reg_t * p_reg)
  91. {
  92. return (p_reg->num_configured_eid_slots > 0 && p_reg->tlm_configured);
  93. }
  94. bool es_slot_reg_clear_slot(es_slot_reg_t * p_reg, uint8_t slot_no)
  95. {
  96. bool eid_has_been_cleared = false;
  97. if (p_reg->slots[slot_no].configured)
  98. {
  99. update_tlm_configured_on_clearing(p_reg, slot_no);
  100. configured_slots_on_clear_update(p_reg->slots_configured,
  101. &p_reg->num_configured_slots,
  102. slot_no);
  103. if (p_reg->slots[slot_no].adv_frame.type == ES_FRAME_TYPE_EID)
  104. {
  105. configured_slots_on_clear_update(p_reg->eid_slots_configured,
  106. &p_reg->num_configured_eid_slots,
  107. slot_no);
  108. eid_has_been_cleared = true;
  109. }
  110. p_reg->slots[slot_no].configured = false;
  111. }
  112. memset(&p_reg->slots[slot_no], 0, sizeof(p_reg->slots[slot_no]));
  113. if (eid_has_been_cleared)
  114. {
  115. es_security_eid_slot_destroy(slot_no);
  116. }
  117. return eid_has_been_cleared;
  118. }
  119. void es_slot_reg_update_slot_list_info_on_add(es_slot_reg_t * p_reg,
  120. uint8_t slot_no,
  121. es_frame_type_t frame_type,
  122. bool init)
  123. {
  124. if (frame_type == ES_FRAME_TYPE_TLM)
  125. {
  126. p_reg->tlm_configured = true;
  127. p_reg->tlm_slot = slot_no;
  128. }
  129. if (!p_reg->slots[slot_no].configured || init)
  130. {
  131. p_reg->slots[slot_no].configured = true;
  132. // Note, we use 'num_configured_slots' before incrementing it, so it is pointing to the correct index.
  133. p_reg->slots_configured[p_reg->num_configured_slots] = slot_no;
  134. p_reg->num_configured_slots++;
  135. if (frame_type == ES_FRAME_TYPE_EID)
  136. {
  137. p_reg->eid_slots_configured[p_reg->num_configured_eid_slots] = slot_no;
  138. p_reg->num_configured_eid_slots++;
  139. }
  140. }
  141. // If an already configured slot has changed from anything TO an EID slot.
  142. else if (frame_type == ES_FRAME_TYPE_EID &&
  143. p_reg->slots[slot_no].adv_frame.type != ES_FRAME_TYPE_EID)
  144. {
  145. p_reg->eid_slots_configured[p_reg->num_configured_eid_slots] = slot_no;
  146. p_reg->num_configured_eid_slots++;
  147. }
  148. }
  149. void es_slot_reg_init(es_slot_reg_t * p_reg)
  150. {
  151. p_reg->tlm_configured = false;
  152. memset(p_reg->slots_configured, ES_SLOT_NOT_CONFIGURED, APP_MAX_ADV_SLOTS);
  153. memset(p_reg->eid_slots_configured, ES_SLOT_NOT_CONFIGURED, APP_MAX_EID_SLOTS);
  154. p_reg->num_configured_eid_slots = 0;
  155. p_reg->num_configured_slots = 0;
  156. }