shared_utils.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _SHARED_UTILS_H_
  6. #define _SHARED_UTILS_H_
  7. #include "bh_platform.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #define FMT_ATTR_CONTAINER 99
  12. #define FMT_APP_RAW_BINARY 98
  13. /* the request structure */
  14. typedef struct request {
  15. // message id
  16. uint32 mid;
  17. // url of the request
  18. char *url;
  19. // action of the request, can be PUT/GET/POST/DELETE
  20. int action;
  21. // payload format, currently only support attr_container_t type
  22. int fmt;
  23. // payload of the request, currently only support attr_container_t type
  24. void *payload;
  25. // length in bytes of the payload
  26. int payload_len;
  27. // sender of the request
  28. unsigned long sender;
  29. } request_t;
  30. /* the response structure */
  31. typedef struct response {
  32. // message id
  33. uint32 mid;
  34. // status of the response
  35. int status;
  36. // payload format
  37. int fmt;
  38. // payload of the response,
  39. void *payload;
  40. // length in bytes of the payload
  41. int payload_len;
  42. // receiver of the response
  43. unsigned long reciever;
  44. } response_t;
  45. int
  46. check_url_start(const char *url, int url_len, const char *leading_str);
  47. bool
  48. match_url(char *pattern, char *matched);
  49. char *
  50. find_key_value(char *buffer, int buffer_len, char *key, char *value,
  51. int value_len, char delimiter);
  52. request_t *
  53. clone_request(request_t *request);
  54. void
  55. request_cleaner(request_t *request);
  56. response_t *
  57. clone_response(response_t *response);
  58. void
  59. response_cleaner(response_t *response);
  60. /**
  61. * @brief Set fields of response.
  62. *
  63. * @param response pointer of the response to be set
  64. * @param status status of response
  65. * @param fmt format of the response payload
  66. * @param payload payload of the response
  67. * @param payload_len length in bytes of the response payload
  68. *
  69. * @return pointer to the response
  70. *
  71. * @warning the response pointer MUST NOT be NULL
  72. */
  73. response_t *
  74. set_response(response_t *response, int status, int fmt, const char *payload,
  75. int payload_len);
  76. /**
  77. * @brief Make a response for a request.
  78. *
  79. * @param request pointer of the request
  80. * @param response pointer of the response to be made
  81. *
  82. * @return pointer to the response
  83. *
  84. * @warning the request and response pointers MUST NOT be NULL
  85. */
  86. response_t *
  87. make_response_for_request(request_t *request, response_t *response);
  88. /**
  89. * @brief Initialize a request.
  90. *
  91. * @param request pointer of the request to be initialized
  92. * @param url url of the request
  93. * @param action action of the request
  94. * @param fmt format of the request payload
  95. * @param payload payload of the request
  96. * @param payload_len length in bytes of the request payload
  97. *
  98. * @return pointer to the request
  99. *
  100. * @warning the request pointer MUST NOT be NULL
  101. */
  102. request_t *
  103. init_request(request_t *request, char *url, int action, int fmt, void *payload,
  104. int payload_len);
  105. char *
  106. pack_request(request_t *request, int *size);
  107. request_t *
  108. unpack_request(char *packet, int size, request_t *request);
  109. char *
  110. pack_response(response_t *response, int *size);
  111. response_t *
  112. unpack_response(char *packet, int size, response_t *response);
  113. void
  114. free_req_resp_packet(char *packet);
  115. char *
  116. wa_strdup(const char *str);
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* end of _SHARED_UTILS_H_ */