shared_utils.h 3.8 KB

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