es.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) 2016 - 2018, 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. #ifndef ES_H__
  41. #define ES_H__
  42. #include <stdint.h>
  43. #include "app_util_platform.h"
  44. /**
  45. * @file
  46. *
  47. * @defgroup eddystone_types Frame types and data formats
  48. * @brief Definitions specific to Eddystone frame types and data formats.
  49. * @ingroup eddystone
  50. * @{
  51. */
  52. /** @brief Swap 2 bytes.
  53. */
  54. #define BYTES_SWAP_16BIT(x) (x << 8 | x >> 8)
  55. /** @brief Reverse 4 bytes.
  56. */
  57. #define BYTES_REVERSE_32BIT(x) \
  58. ((x << 24 | ((x << 8) & 0x00FF0000)) | (((x >> 8) & 0x0000FF00) | x >> 24))
  59. /** @brief Check if the error code is equal to NRF_SUCCESS. If it is not, return the error code.
  60. */
  61. #define RETURN_IF_ERROR(PARAM) \
  62. if ((PARAM) != NRF_SUCCESS) \
  63. { \
  64. return (PARAM); \
  65. }
  66. #define ES_UUID 0xFEAA //!< UUID for Eddystone beacons according to specification.
  67. #define ES_UID_FRAME_TYPE 0x00 //!< UID frame type (fixed at 0x00).
  68. #define ES_UID_RFU 0x00, 0x00 //!< Reserved for future use according to specification.
  69. #define ES_URL_FRAME_TYPE 0x10 //!< URL frame type (fixed at 0x10).
  70. #define ES_URL_SCHEME 0x00 //!< URL prefix scheme according to specification (0x00 = "http://www").
  71. #define ES_TLM_FRAME_TYPE 0x20 //!< TLM frame type (fixed at 0x20).
  72. #define ES_EID_FRAME_TYPE 0x30 //!< EID frame type (fixed at 0x30).
  73. #define ES_FRAME_TYPE_LENGTH (1) //!< Length of a frame type field.
  74. #define ES_UID_LENGTH (20) //!< Length of a UID frame.
  75. #define ES_UID_NAMESPACE_LENGTH (10) //!< Length of a UID frame namespace field.
  76. #define ES_UID_INSTANCE_LENGTH (6) //!< Length of a UID frame instance field.
  77. #define ES_UID_RFU_LENGTH (2) //!< Length of a UID frame RFU field.
  78. #define ES_URL_LENGTH (20) //!< Length of a URL frame.
  79. #define ES_URL_URL_SCHEME_LENGTH (1) //!< Length of a URL frame URL scheme field.
  80. #define ES_URL_ENCODED_URL_LENGTH (17) //!< Maximum length of a URL frame encoded URL field.
  81. #define ES_TLM_LENGTH (14) //!< Length of a TLM frame.
  82. #define ES_TLM_VBATT_LENGTH (2) //!< Length of a TLM frame VBATT field.
  83. #define ES_TLM_TEMP_LENGTH (2) //!< Length of a TLM frame TEMP field.
  84. #define ES_TLM_ADV_CNT_LENGTH (4) //!< Length of a TLM frame ADV count field.
  85. #define ES_TLM_SEC_CNT_LENGTH (4) //!< Length of a TLM frame seconds field.
  86. #define ES_EID_LENGTH (10) //!< Length of an EID frame.
  87. #define ES_EID_ID_LENGTH (8) //!< Length of an EID frame ephemeral ID field.
  88. #define ES_EID_GATTS_READ_LENGTH (14)
  89. #define ES_EID_GATTS_READ_FRAME_TYPE_IDX (0)
  90. #define ES_EID_GATTS_READ_EXPONENT_IDX (1)
  91. #define ES_EID_GATTS_READ_CLCK_VALUE_IDX (2)
  92. #define ES_EID_GATTS_READ_EID_IDX (6)
  93. #define ES_ETLM_LENGTH (18) //!< Length of an eTLM frame.
  94. #define ES_ETLM_ECRYPTED_LENGTH (ES_TLM_VBATT_LENGTH + \
  95. ES_TLM_TEMP_LENGTH + \
  96. ES_TLM_ADV_CNT_LENGTH + \
  97. ES_TLM_SEC_CNT_LENGTH) //!< Length of an eTLM frame encrypted TLM field.
  98. #define ES_ETLM_RFU (0x00) //!< eTLM frame RFU field value.
  99. #define ES_SPEC_VERSION_BYTE (0x00) //!< eTLM frame specification version field value.
  100. /** @brief Eddystone frame type values. These values are advertised as frame types. */
  101. typedef enum
  102. {
  103. ES_FRAME_TYPE_UID = ES_UID_FRAME_TYPE, /**< UID frame type. */
  104. ES_FRAME_TYPE_URL = ES_URL_FRAME_TYPE, /**< URL frame type. */
  105. ES_FRAME_TYPE_TLM = ES_TLM_FRAME_TYPE, /**< TLM frame type. */
  106. ES_FRAME_TYPE_EID = ES_EID_FRAME_TYPE /**< EID frame type. */
  107. } es_frame_type_t;
  108. /** @brief TLM version values. */
  109. typedef enum
  110. {
  111. ES_TLM_VERSION_TLM = 0x00, /**< TLM. */
  112. ES_TLM_VERSION_ETLM = 0x01 /**< Encrypted TLM (eTLM). */
  113. } es_tlm_version_t;
  114. /** @brief UID frame data representation.
  115. * @note This is a packed structure. Therefore, you should not change it.
  116. */
  117. typedef PACKED_STRUCT
  118. {
  119. es_frame_type_t frame_type; //!< Frame type (see @ref es_frame_type_t).
  120. int8_t ranging_data; //!< Calibrated TX power at 0 m.
  121. int8_t namespace[ES_UID_NAMESPACE_LENGTH]; //!< UID namespace.
  122. int8_t instance[ES_UID_INSTANCE_LENGTH]; //!< UID instance.
  123. int8_t rfu[ES_UID_RFU_LENGTH]; //!< RFU.
  124. } es_uid_frame_t;
  125. /** @brief URL frame data representation.
  126. * @note This is a packed structure. Therefore, you should not change it.
  127. */
  128. typedef PACKED_STRUCT
  129. {
  130. es_frame_type_t frame_type; //!< Frame type (see @ref es_frame_type_t).
  131. int8_t ranging_data; //!< Calibrated TX power at 0 m.
  132. uint8_t url_scheme; //!< URL scheme.
  133. uint8_t encoded_url[ES_URL_ENCODED_URL_LENGTH]; //!< Encoded URL (variable length).
  134. } es_url_frame_t;
  135. /** @brief TLM frame data representation.
  136. * @note This is a packed structure. Therefore, you should not change it.
  137. */
  138. typedef PACKED_STRUCT
  139. {
  140. es_frame_type_t frame_type; //!< Frame type (see @ref es_frame_type_t).
  141. es_tlm_version_t version; //!< TLM version (see @ref es_tlm_version_t).
  142. int8_t vbatt[ES_TLM_VBATT_LENGTH]; //!< Battery voltage (in 1 mV units).
  143. int8_t temp[ES_TLM_TEMP_LENGTH]; //!< Beacon temperature.
  144. int8_t adv_cnt[ES_TLM_ADV_CNT_LENGTH]; //!< Advertising PDU count.
  145. int8_t sec_cnt[ES_TLM_SEC_CNT_LENGTH]; //!< Time since power-on or reboot.
  146. } es_tlm_frame_t;
  147. /** @brief EID frame data representation.
  148. * @note This is a packed structure. Therefore, you should not change it.
  149. */
  150. typedef PACKED_STRUCT
  151. {
  152. es_frame_type_t frame_type; //!< Frame type (see @ref es_frame_type_t).
  153. int8_t ranging_data; //!< Calibrated TX power at 0 m.
  154. int8_t eid[ES_EID_ID_LENGTH]; //!< 8-byte ephemeral identifier.
  155. } es_eid_frame_t;
  156. /** @brief eTLM frame data representation.
  157. * @note This is a packed structure. Therefore, you should not change it.
  158. */
  159. typedef PACKED_STRUCT
  160. {
  161. es_frame_type_t frame_type; //!< Frame type (see @ref es_frame_type_t).
  162. es_tlm_version_t version; //!< TLM version (see @ref es_tlm_version_t).
  163. int8_t encrypted_tlm[ES_ETLM_ECRYPTED_LENGTH]; //!< Encrypted TLM data.
  164. int16_t random_salt; //!< Salt
  165. int16_t msg_integrity_check; //!< Message integrity check.
  166. } es_etlm_frame_t;
  167. /**
  168. * @}
  169. */
  170. #endif // ES_H__