attr_container.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 = 1,
  20. ATTR_TYPE_SHORT = ATTR_TYPE_BEGIN,
  21. ATTR_TYPE_INT,
  22. ATTR_TYPE_INT64,
  23. ATTR_TYPE_BYTE,
  24. ATTR_TYPE_UINT16,
  25. ATTR_TYPE_FLOAT,
  26. ATTR_TYPE_DOUBLE,
  27. ATTR_TYPE_BOOLEAN,
  28. ATTR_TYPE_STRING,
  29. ATTR_TYPE_BYTEARRAY,
  30. ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY
  31. };
  32. #define ATTR_CONT_READONLY_SHIFT 2
  33. typedef struct attr_container {
  34. /* container flag:
  35. * bit0, bit1 denote the implemenation algorithm, 00: buffer, 01: link list
  36. * bit2 denotes the readonly flag: 1 is readonly and attr cannot be set
  37. */
  38. char flags[2];
  39. /**
  40. * Buffer format
  41. * for buffer implementation:
  42. * buf length (4 bytes)
  43. * tag length (2 bytes)
  44. * tag
  45. * attr num (2bytes)
  46. * attr[0..n-1]:
  47. * attr key length (2 bytes)
  48. * attr type (1byte)
  49. * attr value (length depends on attr type)
  50. */
  51. char buf[1];
  52. } attr_container_t;
  53. /**
  54. * Create attribute container
  55. *
  56. * @param tag tag of current attribute container
  57. *
  58. * @return the created attribute container, NULL if failed
  59. */
  60. attr_container_t *
  61. attr_container_create(const char *tag);
  62. /**
  63. * Destroy attribute container
  64. *
  65. * @param attr_cont the attribute container to destroy
  66. */
  67. void
  68. attr_container_destroy(const attr_container_t *attr_cont);
  69. /**
  70. * Set short attribute in attribute container
  71. *
  72. * @param p_attr_cont pointer to attribute container to set attribute, and
  73. * return the new attribute container if it is re-created
  74. * @param key the attribute key
  75. * @param value the attribute value
  76. *
  77. * @return true if success, false otherwise
  78. */
  79. bool
  80. attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
  81. short value);
  82. /**
  83. * Set int attribute in attribute container
  84. *
  85. * @param p_attr_cont pointer to attribute container to set attribute, and
  86. * return the new attribute container if it is re-created
  87. * @param key the attribute key
  88. * @param value the attribute value
  89. *
  90. * @return true if success, false otherwise
  91. */
  92. bool
  93. attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
  94. int value);
  95. /**
  96. * Set int64 attribute in attribute container
  97. *
  98. * @param p_attr_cont pointer to attribute container to set attribute, and
  99. * return the new attribute container if it is re-created
  100. * @param key the attribute key
  101. * @param value the attribute value
  102. *
  103. * @return true if success, false otherwise
  104. */
  105. bool
  106. attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
  107. int64_t value);
  108. /**
  109. * Set byte attribute in attribute container
  110. *
  111. * @param p_attr_cont pointer to attribute container to set attribute, and
  112. * return the new attribute container if it is re-created
  113. * @param key the attribute key
  114. * @param value the attribute value
  115. *
  116. * @return true if success, false otherwise
  117. */
  118. bool
  119. attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
  120. int8_t value);
  121. /**
  122. * Set uint16 attribute in attribute container
  123. *
  124. * @param p_attr_cont pointer to attribute container to set attribute, and
  125. * return the new attribute container if it is re-created
  126. * @param key the attribute key
  127. * @param value the attribute value
  128. *
  129. * @return true if success, false otherwise
  130. */
  131. bool
  132. attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
  133. uint16_t value);
  134. /**
  135. * Set float attribute in attribute container
  136. *
  137. * @param p_attr_cont pointer to attribute container to set attribute, and
  138. * return the new attribute container if it is re-created
  139. * @param key the attribute key
  140. * @param value the attribute value
  141. *
  142. * @return true if success, false otherwise
  143. */
  144. bool
  145. attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
  146. float value);
  147. /**
  148. * Set double attribute in attribute container
  149. *
  150. * @param p_attr_cont pointer to attribute container to set attribute, and
  151. * return the new attribute container if it is re-created
  152. * @param key the attribute key
  153. * @param value the attribute value
  154. *
  155. * @return true if success, false otherwise
  156. */
  157. bool
  158. attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
  159. double value);
  160. /**
  161. * Set bool attribute in attribute container
  162. *
  163. * @param p_attr_cont pointer to attribute container to set attribute, and
  164. * return the new attribute container if it is re-created
  165. * @param key the attribute key
  166. * @param value the attribute value
  167. *
  168. * @return true if success, false otherwise
  169. */
  170. bool
  171. attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
  172. bool value);
  173. /**
  174. * Set string attribute in attribute container
  175. *
  176. * @param p_attr_cont pointer to attribute container to set attribute, and
  177. * return the new attribute container if it is re-created
  178. * @param key the attribute key
  179. * @param value the attribute value
  180. *
  181. * @return true if success, false otherwise
  182. */
  183. bool
  184. attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
  185. const char *value);
  186. /**
  187. * Set bytearray attribute in attribute container
  188. *
  189. * @param p_attr_cont pointer to attribute container to set attribute, and
  190. * return the new attribute container if it is re-created
  191. * @param key the attribute key
  192. * @param value the bytearray buffer
  193. * @param length the bytearray length
  194. *
  195. * @return true if success, false otherwise
  196. */
  197. bool
  198. attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
  199. const int8_t *value, unsigned length);
  200. /**
  201. * Get tag of current attribute container
  202. *
  203. * @param attr_cont the attribute container
  204. *
  205. * @return tag of current attribute container
  206. */
  207. const char *
  208. attr_container_get_tag(const attr_container_t *attr_cont);
  209. /**
  210. * Get attribute number of current attribute container
  211. *
  212. * @param attr_cont the attribute container
  213. *
  214. * @return attribute number of current attribute container
  215. */
  216. uint16_t
  217. attr_container_get_attr_num(const attr_container_t *attr_cont);
  218. /**
  219. * Whether the attribute container contains an attribute key.
  220. *
  221. * @param attr_cont the attribute container
  222. * @param key the attribute key
  223. *
  224. * @return true if key is contained in message, false otherwise
  225. */
  226. bool
  227. attr_container_contain_key(const attr_container_t *attr_cont, const char *key);
  228. /**
  229. * Get attribute from attribute container and return it as short value,
  230. * return 0 if attribute isn't found in message.
  231. *
  232. * @param attr_cont the attribute container
  233. * @param key the attribute key
  234. *
  235. * @return the short value of the attribute, 0 if key isn't found
  236. */
  237. short
  238. attr_container_get_as_short(const attr_container_t *attr_cont, const char *key);
  239. /**
  240. * Get attribute from attribute container and return it as int value,
  241. * return 0 if attribute isn't found in message.
  242. *
  243. * @param attr_cont the attribute container
  244. * @param key the attribute key
  245. *
  246. * @return the int value of the attribute, 0 if key isn't found
  247. */
  248. int
  249. attr_container_get_as_int(const attr_container_t *attr_cont, const char *key);
  250. /**
  251. * Get attribute from attribute container and return it as int64 value,
  252. * return 0 if attribute isn't found in attribute container.
  253. *
  254. * @param attr_cont the attribute container
  255. * @param key the attribute key
  256. *
  257. * @return the long value of the attribute, 0 if key isn't found
  258. */
  259. int64_t
  260. attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key);
  261. /**
  262. * Get attribute from attribute container and return it as byte value,
  263. * return 0 if attribute isn't found in attribute container.
  264. *
  265. * @param attr_cont the attribute container
  266. * @param key the attribute key
  267. *
  268. * @return the byte value of the attribute, 0 if key isn't found
  269. */
  270. int8_t
  271. attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key);
  272. /**
  273. * Get attribute from attribute container and return it as uint16 value,
  274. * return 0 if attribute isn't found in attribute container.
  275. *
  276. * @param attr_cont the attribute container
  277. * @param key the attribute key
  278. *
  279. * @return the char value of the attribute, 0 if key isn't found
  280. */
  281. uint16_t
  282. attr_container_get_as_uint16(const attr_container_t *attr_cont,
  283. const char *key);
  284. /**
  285. * Get attribute from attribute container and return it as float value,
  286. * return 0 if attribute isn't found in attribute container.
  287. *
  288. * @param attr_cont the attribute container
  289. * @param key the attribute key
  290. *
  291. * @return the float value of the attribute, 0 if key isn't found
  292. */
  293. float
  294. attr_container_get_as_float(const attr_container_t *attr_cont, const char *key);
  295. /**
  296. * Get attribute from attribute container and return it as double value,
  297. * return 0 if attribute isn't found in attribute container.
  298. *
  299. * @param attr_cont the attribute container
  300. * @param key the attribute key
  301. *
  302. * @return the double value of the attribute, 0 if key isn't found
  303. */
  304. double
  305. attr_container_get_as_double(const attr_container_t *attr_cont,
  306. const char *key);
  307. /**
  308. * Get attribute from attribute container and return it as bool value,
  309. * return false if attribute isn't found in attribute container.
  310. *
  311. * @param attr_cont the attribute container
  312. * @param key the attribute key
  313. *
  314. * @return the bool value of the attribute, 0 if key isn't found
  315. */
  316. bool
  317. attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key);
  318. /**
  319. * Get attribute from attribute container and return it as string value,
  320. * return NULL if attribute isn't found in attribute container.
  321. *
  322. * @param attr_cont the attribute container
  323. * @param key the attribute key
  324. *
  325. * @return the string value of the attribute, NULL if key isn't found
  326. */
  327. char *
  328. attr_container_get_as_string(const attr_container_t *attr_cont,
  329. const char *key);
  330. /**
  331. * Get attribute from attribute container and return it as bytearray value,
  332. * return 0 if attribute isn't found in attribute container.
  333. *
  334. * @param attr_cont the attribute container
  335. * @param key the attribute key
  336. *
  337. * @return the bytearray value of the attribute, NULL if key isn't found
  338. */
  339. const int8_t *
  340. attr_container_get_as_bytearray(const attr_container_t *attr_cont,
  341. const char *key, unsigned *array_length);
  342. /**
  343. * Get the buffer size of attribute container
  344. *
  345. * @param attr_cont the attribute container
  346. *
  347. * @return the buffer size of attribute container
  348. */
  349. unsigned
  350. attr_container_get_serialize_length(const attr_container_t *attr_cont);
  351. /**
  352. * Serialize attribute container to a buffer
  353. *
  354. * @param buf the buffer to receive the serialized data
  355. * @param attr_cont the attribute container to be serialized
  356. *
  357. * @return true if success, false otherwise
  358. */
  359. bool
  360. attr_container_serialize(char *buf, const attr_container_t *attr_cont);
  361. /**
  362. * Whether the attribute container is const, or set attribute isn't supported
  363. *
  364. * @param attr_cont the attribute container
  365. *
  366. * @return true if const, false otherwise
  367. */
  368. bool
  369. attr_container_is_constant(const attr_container_t *attr_cont);
  370. void
  371. attr_container_dump(const attr_container_t *attr_cont);
  372. #ifndef attr_container_malloc
  373. #define attr_container_malloc WA_MALLOC
  374. #endif
  375. #ifndef attr_container_free
  376. #define attr_container_free WA_FREE
  377. #endif
  378. #ifndef attr_container_printf
  379. #define attr_container_printf printf
  380. #endif
  381. #ifdef __cplusplus
  382. } /* end of extern "C" */
  383. #endif
  384. #endif /* end of _ATTR_CONTAINER_H_ */