app_util_bds.h 15 KB

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