app_util_bds.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * Copyright (c) 2012 - 2019, 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. /** @file
  41. *
  42. * @defgroup app_util Utility Functions and Definitions
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Various types and definitions available to all applications.
  47. */
  48. #ifndef APP_UTIL_BDS_H__
  49. #define APP_UTIL_BDS_H__
  50. #include <stdint.h>
  51. #include <string.h>
  52. #include <stdbool.h>
  53. #include "compiler_abstraction.h"
  54. #include "app_util.h"
  55. #include "ble_srv_common.h"
  56. #include "nordic_common.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. typedef uint8_t nibble_t;
  61. typedef uint32_t uint24_t;
  62. typedef uint64_t uint40_t;
  63. /**@brief IEEE 11073-20601 Regulatory Certification Data List Structure */
  64. typedef struct
  65. {
  66. uint8_t * p_list; /**< Pointer the byte array containing the encoded opaque structure based on IEEE 11073-20601 specification. */
  67. uint8_t list_len; /**< Length of the byte array. */
  68. } regcertdatalist_t;
  69. /**@brief SFLOAT format (IEEE-11073 16-bit FLOAT, meaning 4 bits for exponent (base 10) and 12 bits mantissa) */
  70. typedef struct
  71. {
  72. int8_t exponent; /**< Base 10 exponent, should be using only 4 bits */
  73. int16_t mantissa; /**< Mantissa, should be using only 12 bits */
  74. } sfloat_t;
  75. /**@brief Date and Time structure. */
  76. typedef struct
  77. {
  78. uint16_t year;
  79. uint8_t month;
  80. uint8_t day;
  81. uint8_t hours;
  82. uint8_t minutes;
  83. uint8_t seconds;
  84. } ble_date_time_t;
  85. /**@brief Function for encoding a uint16 value.
  86. *
  87. * @param[in] p_value Value to be encoded.
  88. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  89. *
  90. * @return Number of bytes written.
  91. */
  92. static __INLINE uint8_t bds_uint16_encode(const uint16_t * p_value, uint8_t * p_encoded_data)
  93. {
  94. p_encoded_data[0] = (uint8_t) ((*p_value & 0x00FF) >> 0);
  95. p_encoded_data[1] = (uint8_t) ((*p_value & 0xFF00) >> 8);
  96. return sizeof(uint16_t);
  97. }
  98. static __INLINE uint8_t bds_int16_encode(const int16_t * p_value, uint8_t * p_encoded_data)
  99. {
  100. uint16_t tmp = *p_value;
  101. return bds_uint16_encode(&tmp, p_encoded_data);
  102. }
  103. /**@brief Function for encoding a uint24 value.
  104. *
  105. * @param[in] p_value Value to be encoded.
  106. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  107. *
  108. * @return Number of bytes written.
  109. */
  110. static __INLINE uint8_t bds_uint24_encode(const uint32_t * p_value, uint8_t * p_encoded_data)
  111. {
  112. p_encoded_data[0] = (uint8_t) ((*p_value & 0x000000FF) >> 0);
  113. p_encoded_data[1] = (uint8_t) ((*p_value & 0x0000FF00) >> 8);
  114. p_encoded_data[2] = (uint8_t) ((*p_value & 0x00FF0000) >> 16);
  115. return (3);
  116. }
  117. /**@brief Function for encoding a uint32 value.
  118. *
  119. * @param[in] p_value Value to be encoded.
  120. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  121. *
  122. * @return Number of bytes written.
  123. */
  124. static __INLINE uint8_t bds_uint32_encode(const uint32_t * p_value, uint8_t * p_encoded_data)
  125. {
  126. p_encoded_data[0] = (uint8_t) ((*p_value & 0x000000FF) >> 0);
  127. p_encoded_data[1] = (uint8_t) ((*p_value & 0x0000FF00) >> 8);
  128. p_encoded_data[2] = (uint8_t) ((*p_value & 0x00FF0000) >> 16);
  129. p_encoded_data[3] = (uint8_t) ((*p_value & 0xFF000000) >> 24);
  130. return sizeof(uint32_t);
  131. }
  132. /**@brief Function for encoding a uint40 value.
  133. *
  134. * @param[in] p_value Value to be encoded.
  135. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  136. *
  137. * @return Number of bytes written.
  138. */
  139. static __INLINE uint8_t bds_uint40_encode(const uint64_t * p_value, uint8_t * p_encoded_data)
  140. {
  141. p_encoded_data[0] = (uint8_t) ((*p_value & 0x00000000000000FF) >> 0);
  142. p_encoded_data[1] = (uint8_t) ((*p_value & 0x000000000000FF00) >> 8);
  143. p_encoded_data[2] = (uint8_t) ((*p_value & 0x0000000000FF0000) >> 16);
  144. p_encoded_data[3] = (uint8_t) ((*p_value & 0x00000000FF000000) >> 24);
  145. p_encoded_data[4] = (uint8_t) ((*p_value & 0x000000FF00000000) >> 32);
  146. return 5;
  147. }
  148. /**@brief Function for encoding a sfloat value.
  149. *
  150. * @param[in] p_value Value to be encoded.
  151. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  152. *
  153. * @return Number of bytes written.
  154. */
  155. static __INLINE uint8_t bds_sfloat_encode(const sfloat_t * p_value, uint8_t * p_encoded_data)
  156. {
  157. uint16_t encoded_val;
  158. encoded_val = ((p_value->exponent << 12) & 0xF000) |
  159. ((p_value->mantissa << 0) & 0x0FFF);
  160. return(bds_uint16_encode(&encoded_val, p_encoded_data));
  161. }
  162. /**@brief Function for encoding a uint8_array value.
  163. *
  164. * @param[in] p_value Value to be encoded.
  165. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  166. */
  167. static __INLINE uint8_t bds_uint8_array_encode(const uint8_array_t * p_value,
  168. uint8_t * p_encoded_data)
  169. {
  170. memcpy(p_encoded_data, p_value->p_data, p_value->size);
  171. return p_value->size;
  172. }
  173. /**@brief Function for encoding a utf8_str value.
  174. *
  175. * @param[in] p_value Value to be encoded.
  176. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  177. */
  178. static __INLINE uint8_t bds_ble_srv_utf8_str_encode(const ble_srv_utf8_str_t * p_value,
  179. uint8_t * p_encoded_data)
  180. {
  181. memcpy(p_encoded_data, p_value->p_str, p_value->length);
  182. return p_value->length;
  183. }
  184. /**@brief Function for encoding a regcertdatalist value.
  185. *
  186. * @param[in] p_value Value to be encoded.
  187. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  188. */
  189. static __INLINE uint8_t bds_regcertdatalist_encode(const regcertdatalist_t * p_value,
  190. uint8_t * p_encoded_data)
  191. {
  192. memcpy(p_encoded_data, p_value->p_list, p_value->list_len);
  193. return p_value->list_len;
  194. }
  195. /**@brief Function for decoding a date_time value.
  196. *
  197. * @param[in] p_date_time pointer to the date_time structure to encode.
  198. * @param[in] p_encoded_data pointer to the encoded data
  199. * @return length of the encoded field.
  200. */
  201. static __INLINE uint8_t bds_ble_date_time_encode(const ble_date_time_t * p_date_time,
  202. uint8_t * p_encoded_data)
  203. {
  204. uint8_t len = bds_uint16_encode(&p_date_time->year, &p_encoded_data[0]);
  205. p_encoded_data[len++] = p_date_time->month;
  206. p_encoded_data[len++] = p_date_time->day;
  207. p_encoded_data[len++] = p_date_time->hours;
  208. p_encoded_data[len++] = p_date_time->minutes;
  209. p_encoded_data[len++] = p_date_time->seconds;
  210. return len;
  211. }
  212. /**@brief Function for decoding a uint16 value.
  213. *
  214. * @param[in] len length of the field to be decoded.
  215. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  216. * @param[in] p_decoded_val pointer to the decoded value
  217. * @return length of the decoded field.
  218. */
  219. static __INLINE uint8_t bds_uint16_decode(const uint8_t len,
  220. const uint8_t * p_encoded_data,
  221. uint16_t * p_decoded_val)
  222. {
  223. UNUSED_VARIABLE(len);
  224. *p_decoded_val = (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
  225. (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 );
  226. return (sizeof(uint16_t));
  227. }
  228. /**@brief Function for decoding a int16 value.
  229. *
  230. * @param[in] len length of the field to be decoded.
  231. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  232. * @param[in] p_decoded_val pointer to the decoded value
  233. * @return length of the decoded field.
  234. */
  235. static __INLINE uint8_t bds_int16_decode(const uint8_t len,
  236. const uint8_t * p_encoded_data,
  237. int16_t * p_decoded_val)
  238. {
  239. UNUSED_VARIABLE(len);
  240. uint16_t tmp = 0;
  241. uint8_t retval = bds_uint16_decode(len, p_encoded_data, &tmp);
  242. *p_decoded_val = (int16_t)tmp;
  243. return retval;
  244. }
  245. /**@brief Function for decoding a uint24 value.
  246. *
  247. * @param[in] len length of the field to be decoded.
  248. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  249. * @param[in] p_decoded_val pointer to the decoded value
  250. *
  251. * @return length of the decoded field.
  252. */
  253. static __INLINE uint8_t bds_uint24_decode(const uint8_t len,
  254. const uint8_t * p_encoded_data,
  255. uint32_t * p_decoded_val)
  256. {
  257. UNUSED_VARIABLE(len);
  258. *p_decoded_val = (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  259. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  260. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16);
  261. return (3);
  262. }
  263. /**@brief Function for decoding a uint32 value.
  264. *
  265. * @param[in] len length of the field to be decoded.
  266. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  267. * @param[in] p_decoded_val pointer to the decoded value
  268. *
  269. * @return length of the decoded field.
  270. */
  271. static __INLINE uint8_t bds_uint32_decode(const uint8_t len,
  272. const uint8_t * p_encoded_data,
  273. uint32_t * p_decoded_val)
  274. {
  275. UNUSED_VARIABLE(len);
  276. *p_decoded_val = (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  277. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  278. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  279. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 );
  280. return (sizeof(uint32_t));
  281. }
  282. /**@brief Function for decoding a uint40 value.
  283. *
  284. * @param[in] len length of the field to be decoded.
  285. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  286. * @param[in] p_decoded_val pointer to the decoded value
  287. *
  288. * @return length of the decoded field.
  289. */
  290. static __INLINE uint8_t bds_uint40_decode(const uint8_t len,
  291. const uint8_t * p_encoded_data,
  292. uint64_t * p_decoded_val)
  293. {
  294. UNUSED_VARIABLE(len);
  295. *p_decoded_val = (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  296. (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  297. (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  298. (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24 )|
  299. (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32 );
  300. return (40);
  301. }
  302. /**@brief Function for decoding a sfloat value.
  303. *
  304. * @param[in] len length of the field to be decoded.
  305. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  306. * @param[in] p_decoded_val pointer to the decoded value
  307. *
  308. * @return length of the decoded field.
  309. */
  310. static __INLINE uint8_t bds_sfloat_decode(const uint8_t len,
  311. const uint8_t * p_encoded_data,
  312. sfloat_t * p_decoded_val)
  313. {
  314. p_decoded_val->exponent = 0;
  315. bds_uint16_decode(len, p_encoded_data, (uint16_t*)&p_decoded_val->mantissa);
  316. p_decoded_val->exponent = (uint8_t)((p_decoded_val->mantissa & 0xF000) >> 12);
  317. p_decoded_val->mantissa &= 0x0FFF;
  318. return len;
  319. }
  320. /**@brief Function for decoding a uint8_array value.
  321. *
  322. * @param[in] len length of the field to be decoded.
  323. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  324. * @param[in] p_decoded_val pointer to the decoded value
  325. *
  326. * @return length of the decoded field.
  327. */
  328. static __INLINE uint8_t bds_uint8_array_decode(const uint8_t len,
  329. const uint8_t * p_encoded_data,
  330. uint8_array_t * p_decoded_val)
  331. {
  332. memcpy(p_decoded_val->p_data, p_encoded_data, len);
  333. p_decoded_val->size = len;
  334. return p_decoded_val->size;
  335. }
  336. /**@brief Function for decoding a utf8_str value.
  337. *
  338. * @param[in] len length of the field to be decoded.
  339. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  340. * @param[in] p_decoded_val pointer to the decoded value
  341. *
  342. * @return length of the decoded field.
  343. */
  344. static __INLINE uint8_t bds_ble_srv_utf8_str_decode(const uint8_t len,
  345. const uint8_t * p_encoded_data,
  346. ble_srv_utf8_str_t * p_decoded_val)
  347. {
  348. p_decoded_val->p_str = (uint8_t*)p_encoded_data;
  349. p_decoded_val->length = len;
  350. return p_decoded_val->length;
  351. }
  352. /**@brief Function for decoding a regcertdatalist value.
  353. *
  354. * @param[in] len length of the field to be decoded.
  355. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  356. * @param[in] p_decoded_val pointer to the decoded value
  357. *
  358. * @return length of the decoded field.
  359. */
  360. static __INLINE uint8_t bds_regcertdatalist_decode(const uint8_t len,
  361. const uint8_t * p_encoded_data,
  362. regcertdatalist_t * p_decoded_val)
  363. {
  364. memcpy(p_decoded_val->p_list, p_encoded_data, len);
  365. p_decoded_val->list_len = len;
  366. return p_decoded_val->list_len;
  367. }
  368. /**@brief Function for decoding a date_time value.
  369. *
  370. * @param[in] len length of the field to be decoded.
  371. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  372. * @param[in] p_date_time pointer to the decoded value
  373. *
  374. * @return length of the decoded field.
  375. */
  376. static __INLINE uint8_t bds_ble_date_time_decode(const uint8_t len,
  377. const uint8_t * p_encoded_data,
  378. ble_date_time_t * p_date_time)
  379. {
  380. UNUSED_VARIABLE(len);
  381. uint8_t pos = bds_uint16_decode(len, &p_encoded_data[0], &p_date_time->year);
  382. p_date_time->month = p_encoded_data[pos++];
  383. p_date_time->day = p_encoded_data[pos++];
  384. p_date_time->hours = p_encoded_data[pos++];
  385. p_date_time->minutes = p_encoded_data[pos++];
  386. p_date_time->seconds = p_encoded_data[pos++];
  387. return pos;
  388. }
  389. #ifdef __cplusplus
  390. }
  391. #endif
  392. #endif // APP_UTIL_BDS_H__
  393. /** @} */