shared_utils.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_
  17. #define DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_
  18. #include "native_interface.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 check_url_start(const char* url, int url_len, const char * leading_str);
  57. bool match_url(char * pattern, char * matched);
  58. char * find_key_value(char * buffer, int buffer_len, char * key, char * value,
  59. int value_len, char delimiter);
  60. request_t *clone_request(request_t *request);
  61. void request_cleaner(request_t *request);
  62. response_t * clone_response(response_t * response);
  63. void response_cleaner(response_t * response);
  64. /**
  65. * @brief Set fields of response.
  66. *
  67. * @param response pointer of the response to be set
  68. * @param status status of response
  69. * @param fmt format of the response payload
  70. * @param payload payload of the response
  71. * @param payload_len length in bytes of the response payload
  72. *
  73. * @return pointer to the response
  74. *
  75. * @warning the response pointer MUST NOT be NULL
  76. */
  77. response_t * set_response(response_t * response, int status, int fmt,
  78. const char *payload, int payload_len);
  79. /**
  80. * @brief Make a response for a request.
  81. *
  82. * @param request pointer of the request
  83. * @param response pointer of the response to be made
  84. *
  85. * @return pointer to the response
  86. *
  87. * @warning the request and response pointers MUST NOT be NULL
  88. */
  89. response_t * make_response_for_request(request_t * request,
  90. response_t * response);
  91. /**
  92. * @brief Initialize a request.
  93. *
  94. * @param request pointer of the request to be initialized
  95. * @param url url of the request
  96. * @param action action of the request
  97. * @param fmt format of the request payload
  98. * @param payload payload of the request
  99. * @param payload_len length in bytes of the request payload
  100. *
  101. * @return pointer to the request
  102. *
  103. * @warning the request pointer MUST NOT be NULL
  104. */
  105. request_t * init_request(request_t * request, char *url, int action, int fmt,
  106. void *payload, int payload_len);
  107. char * pack_request(request_t *request, int * size);
  108. request_t * unpack_request(char * packet, int size, request_t * request);
  109. char * pack_response(response_t *response, int * size);
  110. response_t * unpack_response(char * packet, int size, response_t * response);
  111. void free_req_resp_packet(char * packet);
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. #endif /* DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_ */