attr_container.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _ATTR_CONTAINER_H_
  6. #define _ATTR_CONTAINER_H_
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stddef.h>
  12. #include <stdbool.h>
  13. #include "bh_platform.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* Attribute type */
  18. enum {
  19. ATTR_TYPE_BEGIN = 0,
  20. ATTR_TYPE_BYTE = ATTR_TYPE_BEGIN,
  21. ATTR_TYPE_INT8 = ATTR_TYPE_BYTE,
  22. ATTR_TYPE_SHORT,
  23. ATTR_TYPE_INT16 = ATTR_TYPE_SHORT,
  24. ATTR_TYPE_INT,
  25. ATTR_TYPE_INT32 = ATTR_TYPE_INT,
  26. ATTR_TYPE_INT64,
  27. ATTR_TYPE_UINT8,
  28. ATTR_TYPE_UINT16,
  29. ATTR_TYPE_UINT32,
  30. ATTR_TYPE_UINT64,
  31. /**
  32. * Why ATTR_TYPE_FLOAT = 10?
  33. * We determine the number of bytes that should be copied through 1<<(type &
  34. * 3). ATTR_TYPE_BYTE = 0, so the number of bytes is 1 << 0 = 1.
  35. * ATTR_TYPE_UINT64 = 7, so the number of bytes is 1 << 3 = 8.
  36. * Since the float type takes up 4 bytes, ATTR_TYPE_FLOAT should be 10.
  37. * Calculation: (1 << (10&3)) = (1 << 2) = 4
  38. */
  39. ATTR_TYPE_FLOAT = 10,
  40. ATTR_TYPE_DOUBLE,
  41. ATTR_TYPE_BOOLEAN,
  42. ATTR_TYPE_STRING,
  43. ATTR_TYPE_BYTEARRAY,
  44. ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY
  45. };
  46. #define ATTR_CONT_READONLY_SHIFT 2
  47. typedef struct attr_container {
  48. /* container flag:
  49. * bit0, bit1 denote the implemenation algorithm, 00: buffer, 01: link list
  50. * bit2 denotes the readonly flag: 1 is readonly and attr cannot be set
  51. */
  52. char flags[2];
  53. /**
  54. * Buffer format
  55. * for buffer implementation:
  56. * buf length (4 bytes)
  57. * tag length (2 bytes)
  58. * tag
  59. * attr num (2bytes)
  60. * attr[0..n-1]:
  61. * attr key length (2 bytes)
  62. * attr type (1byte)
  63. * attr value (length depends on attr type)
  64. */
  65. char buf[1];
  66. } attr_container_t;
  67. /**
  68. * Create attribute container
  69. *
  70. * @param tag tag of current attribute container
  71. *
  72. * @return the created attribute container, NULL if failed
  73. */
  74. attr_container_t *
  75. attr_container_create(const char *tag);
  76. /**
  77. * Destroy attribute container
  78. *
  79. * @param attr_cont the attribute container to destroy
  80. */
  81. void
  82. attr_container_destroy(const attr_container_t *attr_cont);
  83. /**
  84. * Set short attribute in attribute container
  85. *
  86. * @param p_attr_cont pointer to attribute container to set attribute, and
  87. * return the new attribute container if it is re-created
  88. * @param key the attribute key
  89. * @param value the attribute value
  90. *
  91. * @return true if success, false otherwise
  92. */
  93. bool
  94. attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
  95. short value);
  96. /**
  97. * Set int16 attribute in attribute container
  98. *
  99. * @param p_attr_cont pointer to attribute container to set attribute, and
  100. * return the new attribute container if it is re-created
  101. * @param key the attribute key
  102. * @param value the attribute value
  103. *
  104. * @return true if success, false otherwise
  105. */
  106. bool
  107. attr_container_set_int16(attr_container_t **p_attr_cont, const char *key,
  108. int16_t value);
  109. /**
  110. * Set int attribute in attribute container
  111. *
  112. * @param p_attr_cont pointer to attribute container to set attribute, and
  113. * return the new attribute container if it is re-created
  114. * @param key the attribute key
  115. * @param value the attribute value
  116. *
  117. * @return true if success, false otherwise
  118. */
  119. bool
  120. attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
  121. int value);
  122. /**
  123. * Set int32 attribute in attribute container
  124. *
  125. * @param p_attr_cont pointer to attribute container to set attribute, and
  126. * return the new attribute container if it is re-created
  127. * @param key the attribute key
  128. * @param value the attribute value
  129. *
  130. * @return true if success, false otherwise
  131. */
  132. bool
  133. attr_container_set_int32(attr_container_t **p_attr_cont, const char *key,
  134. int32_t value);
  135. /**
  136. * Set uint32 attribute in attribute container
  137. *
  138. * @param p_attr_cont pointer to attribute container to set attribute, and
  139. * return the new attribute container if it is re-created
  140. * @param key the attribute key
  141. * @param value the attribute value
  142. *
  143. * @return true if success, false otherwise
  144. */
  145. bool
  146. attr_container_set_uint32(attr_container_t **p_attr_cont, const char *key,
  147. uint32_t value);
  148. /**
  149. * Set int64 attribute in attribute container
  150. *
  151. * @param p_attr_cont pointer to attribute container to set attribute, and
  152. * return the new attribute container if it is re-created
  153. * @param key the attribute key
  154. * @param value the attribute value
  155. *
  156. * @return true if success, false otherwise
  157. */
  158. bool
  159. attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
  160. int64_t value);
  161. /**
  162. * Set uint64 attribute in attribute container
  163. *
  164. * @param p_attr_cont pointer to attribute container to set attribute, and
  165. * return the new attribute container if it is re-created
  166. * @param key the attribute key
  167. * @param value the attribute value
  168. *
  169. * @return true if success, false otherwise
  170. */
  171. bool
  172. attr_container_set_uint64(attr_container_t **p_attr_cont, const char *key,
  173. uint64_t value);
  174. /**
  175. * Set byte attribute in attribute container
  176. *
  177. * @param p_attr_cont pointer to attribute container to set attribute, and
  178. * return the new attribute container if it is re-created
  179. * @param key the attribute key
  180. * @param value the attribute value
  181. *
  182. * @return true if success, false otherwise
  183. */
  184. bool
  185. attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
  186. int8_t value);
  187. /**
  188. * Set int8 attribute in attribute container
  189. *
  190. * @param p_attr_cont pointer to attribute container to set attribute, and
  191. * return the new attribute container if it is re-created
  192. * @param key the attribute key
  193. * @param value the attribute value
  194. *
  195. * @return true if success, false otherwise
  196. */
  197. bool
  198. attr_container_set_int8(attr_container_t **p_attr_cont, const char *key,
  199. int8_t value);
  200. /**
  201. * Set uint8 attribute in attribute container
  202. *
  203. * @param p_attr_cont pointer to attribute container to set attribute, and
  204. * return the new attribute container if it is re-created
  205. * @param key the attribute key
  206. * @param value the attribute value
  207. *
  208. * @return true if success, false otherwise
  209. */
  210. bool
  211. attr_container_set_uint8(attr_container_t **p_attr_cont, const char *key,
  212. uint8_t value);
  213. /**
  214. * Set uint16 attribute in attribute container
  215. *
  216. * @param p_attr_cont pointer to attribute container to set attribute, and
  217. * return the new attribute container if it is re-created
  218. * @param key the attribute key
  219. * @param value the attribute value
  220. *
  221. * @return true if success, false otherwise
  222. */
  223. bool
  224. attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
  225. uint16_t value);
  226. /**
  227. * Set float attribute in attribute container
  228. *
  229. * @param p_attr_cont pointer to attribute container to set attribute, and
  230. * return the new attribute container if it is re-created
  231. * @param key the attribute key
  232. * @param value the attribute value
  233. *
  234. * @return true if success, false otherwise
  235. */
  236. bool
  237. attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
  238. float value);
  239. /**
  240. * Set double attribute in attribute container
  241. *
  242. * @param p_attr_cont pointer to attribute container to set attribute, and
  243. * return the new attribute container if it is re-created
  244. * @param key the attribute key
  245. * @param value the attribute value
  246. *
  247. * @return true if success, false otherwise
  248. */
  249. bool
  250. attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
  251. double value);
  252. /**
  253. * Set bool attribute in attribute container
  254. *
  255. * @param p_attr_cont pointer to attribute container to set attribute, and
  256. * return the new attribute container if it is re-created
  257. * @param key the attribute key
  258. * @param value the attribute value
  259. *
  260. * @return true if success, false otherwise
  261. */
  262. bool
  263. attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
  264. bool value);
  265. /**
  266. * Set string attribute in attribute container
  267. *
  268. * @param p_attr_cont pointer to attribute container to set attribute, and
  269. * return the new attribute container if it is re-created
  270. * @param key the attribute key
  271. * @param value the attribute value
  272. *
  273. * @return true if success, false otherwise
  274. */
  275. bool
  276. attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
  277. const char *value);
  278. /**
  279. * Set bytearray attribute in attribute container
  280. *
  281. * @param p_attr_cont pointer to attribute container to set attribute, and
  282. * return the new attribute container if it is re-created
  283. * @param key the attribute key
  284. * @param value the bytearray buffer
  285. * @param length the bytearray length
  286. *
  287. * @return true if success, false otherwise
  288. */
  289. bool
  290. attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
  291. const int8_t *value, unsigned length);
  292. /**
  293. * Get tag of current attribute container
  294. *
  295. * @param attr_cont the attribute container
  296. *
  297. * @return tag of current attribute container
  298. */
  299. const char *
  300. attr_container_get_tag(const attr_container_t *attr_cont);
  301. /**
  302. * Get attribute number of current attribute container
  303. *
  304. * @param attr_cont the attribute container
  305. *
  306. * @return attribute number of current attribute container
  307. */
  308. uint16_t
  309. attr_container_get_attr_num(const attr_container_t *attr_cont);
  310. /**
  311. * Whether the attribute container contains an attribute key.
  312. *
  313. * @param attr_cont the attribute container
  314. * @param key the attribute key
  315. *
  316. * @return true if key is contained in message, false otherwise
  317. */
  318. bool
  319. attr_container_contain_key(const attr_container_t *attr_cont, const char *key);
  320. /**
  321. * Get attribute from attribute container and return it as short value,
  322. * return 0 if attribute isn't found in message.
  323. *
  324. * @param attr_cont the attribute container
  325. * @param key the attribute key
  326. *
  327. * @return the short value of the attribute, 0 if key isn't found
  328. */
  329. short
  330. attr_container_get_as_short(const attr_container_t *attr_cont, const char *key);
  331. /**
  332. * Get attribute from attribute container and return it as int16 value,
  333. * return 0 if attribute isn't found in message.
  334. *
  335. * @param attr_cont the attribute container
  336. * @param key the attribute key
  337. *
  338. * @return the short value of the attribute, 0 if key isn't found
  339. */
  340. int16_t
  341. attr_container_get_as_int16(const attr_container_t *attr_cont, const char *key);
  342. /**
  343. * Get attribute from attribute container and return it as int value,
  344. * return 0 if attribute isn't found in message.
  345. *
  346. * @param attr_cont the attribute container
  347. * @param key the attribute key
  348. *
  349. * @return the int value of the attribute, 0 if key isn't found
  350. */
  351. int
  352. attr_container_get_as_int(const attr_container_t *attr_cont, const char *key);
  353. /**
  354. * Get attribute from attribute container and return it as int32 value,
  355. * return 0 if attribute isn't found in message.
  356. *
  357. * @param attr_cont the attribute container
  358. * @param key the attribute key
  359. *
  360. * @return the int value of the attribute, 0 if key isn't found
  361. */
  362. int32_t
  363. attr_container_get_as_int32(const attr_container_t *attr_cont, const char *key);
  364. /**
  365. * Get attribute from attribute container and return it as uint32 value,
  366. * return 0 if attribute isn't found in message.
  367. *
  368. * @param attr_cont the attribute container
  369. * @param key the attribute key
  370. *
  371. * @return the unsigned int value of the attribute, 0 if key isn't found
  372. */
  373. uint32_t
  374. attr_container_get_as_uint32(const attr_container_t *attr_cont,
  375. const char *key);
  376. /**
  377. * Get attribute from attribute container and return it as int64 value,
  378. * return 0 if attribute isn't found in attribute container.
  379. *
  380. * @param attr_cont the attribute container
  381. * @param key the attribute key
  382. *
  383. * @return the long value of the attribute, 0 if key isn't found
  384. */
  385. int64_t
  386. attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key);
  387. /**
  388. * Get attribute from attribute container and return it as uint64 value,
  389. * return 0 if attribute isn't found in attribute container.
  390. *
  391. * @param attr_cont the attribute container
  392. * @param key the attribute key
  393. *
  394. * @return the unsigned long value of the attribute, 0 if key isn't found
  395. */
  396. uint64_t
  397. attr_container_get_as_uint64(const attr_container_t *attr_cont,
  398. const char *key);
  399. /**
  400. * Get attribute from attribute container and return it as byte value,
  401. * return 0 if attribute isn't found in attribute container.
  402. *
  403. * @param attr_cont the attribute container
  404. * @param key the attribute key
  405. *
  406. * @return the byte value of the attribute, 0 if key isn't found
  407. */
  408. int8_t
  409. attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key);
  410. /**
  411. * Get attribute from attribute container and return it as int8 value,
  412. * return 0 if attribute isn't found in attribute container.
  413. *
  414. * @param attr_cont the attribute container
  415. * @param key the attribute key
  416. *
  417. * @return the byte value of the attribute, 0 if key isn't found
  418. */
  419. int8_t
  420. attr_container_get_as_int8(const attr_container_t *attr_cont, const char *key);
  421. /**
  422. * Get attribute from attribute container and return it as uint8 value,
  423. * return 0 if attribute isn't found in attribute container.
  424. *
  425. * @param attr_cont the attribute container
  426. * @param key the attribute key
  427. *
  428. * @return the uint8 value of the attribute, 0 if key isn't found
  429. */
  430. uint8_t
  431. attr_container_get_as_uint8(const attr_container_t *attr_cont, const char *key);
  432. /**
  433. * Get attribute from attribute container and return it as uint16 value,
  434. * return 0 if attribute isn't found in attribute container.
  435. *
  436. * @param attr_cont the attribute container
  437. * @param key the attribute key
  438. *
  439. * @return the char value of the attribute, 0 if key isn't found
  440. */
  441. uint16_t
  442. attr_container_get_as_uint16(const attr_container_t *attr_cont,
  443. const char *key);
  444. /**
  445. * Get attribute from attribute container and return it as float value,
  446. * return 0 if attribute isn't found in attribute container.
  447. *
  448. * @param attr_cont the attribute container
  449. * @param key the attribute key
  450. *
  451. * @return the float value of the attribute, 0 if key isn't found
  452. */
  453. float
  454. attr_container_get_as_float(const attr_container_t *attr_cont, const char *key);
  455. /**
  456. * Get attribute from attribute container and return it as double value,
  457. * return 0 if attribute isn't found in attribute container.
  458. *
  459. * @param attr_cont the attribute container
  460. * @param key the attribute key
  461. *
  462. * @return the double value of the attribute, 0 if key isn't found
  463. */
  464. double
  465. attr_container_get_as_double(const attr_container_t *attr_cont,
  466. const char *key);
  467. /**
  468. * Get attribute from attribute container and return it as bool value,
  469. * return false if attribute isn't found in attribute container.
  470. *
  471. * @param attr_cont the attribute container
  472. * @param key the attribute key
  473. *
  474. * @return the bool value of the attribute, 0 if key isn't found
  475. */
  476. bool
  477. attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key);
  478. /**
  479. * Get attribute from attribute container and return it as string value,
  480. * return NULL if attribute isn't found in attribute container.
  481. *
  482. * @param attr_cont the attribute container
  483. * @param key the attribute key
  484. *
  485. * @return the string value of the attribute, NULL if key isn't found
  486. */
  487. char *
  488. attr_container_get_as_string(const attr_container_t *attr_cont,
  489. const char *key);
  490. /**
  491. * Get attribute from attribute container and return it as bytearray value,
  492. * return 0 if attribute isn't found in attribute container.
  493. *
  494. * @param attr_cont the attribute container
  495. * @param key the attribute key
  496. *
  497. * @return the bytearray value of the attribute, NULL if key isn't found
  498. */
  499. const int8_t *
  500. attr_container_get_as_bytearray(const attr_container_t *attr_cont,
  501. const char *key, unsigned *array_length);
  502. /**
  503. * Get the buffer size of attribute container
  504. *
  505. * @param attr_cont the attribute container
  506. *
  507. * @return the buffer size of attribute container
  508. */
  509. unsigned
  510. attr_container_get_serialize_length(const attr_container_t *attr_cont);
  511. /**
  512. * Serialize attribute container to a buffer
  513. *
  514. * @param buf the buffer to receive the serialized data
  515. * @param attr_cont the attribute container to be serialized
  516. *
  517. * @return true if success, false otherwise
  518. */
  519. bool
  520. attr_container_serialize(char *buf, const attr_container_t *attr_cont);
  521. /**
  522. * Whether the attribute container is const, or set attribute isn't supported
  523. *
  524. * @param attr_cont the attribute container
  525. *
  526. * @return true if const, false otherwise
  527. */
  528. bool
  529. attr_container_is_constant(const attr_container_t *attr_cont);
  530. void
  531. attr_container_dump(const attr_container_t *attr_cont);
  532. #ifndef attr_container_malloc
  533. #define attr_container_malloc WA_MALLOC
  534. #endif
  535. #ifndef attr_container_free
  536. #define attr_container_free WA_FREE
  537. #endif
  538. #ifndef attr_container_printf
  539. #define attr_container_printf printf
  540. #endif
  541. #ifdef __cplusplus
  542. } /* end of extern "C" */
  543. #endif
  544. #endif /* end of _ATTR_CONTAINER_H_ */