attr-container.h 12 KB

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