request.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 _AEE_REQUEST_H_
  17. #define _AEE_REQUEST_H_
  18. #include "native_interface.h"
  19. #include "shared_utils.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * @typedef request_handler_f
  25. *
  26. * @brief Define the signature of callback function for API
  27. * api_register_resource_handler() to handle request or for API
  28. * api_subscribe_event() to handle event.
  29. *
  30. * @param request pointer of the request to be handled
  31. *
  32. * @see api_register_resource_handler
  33. * @see api_subscribe_event
  34. */
  35. typedef void (*request_handler_f)(request_t *request);
  36. /**
  37. * @typedef response_handler_f
  38. *
  39. * @brief Define the signature of callback function for API
  40. * api_send_request() to handle response of a request.
  41. *
  42. * @param response pointer of the response to be handled
  43. * @param user_data user data associated with the request which is set when
  44. * calling api_send_request().
  45. *
  46. * @see api_send_request
  47. */
  48. typedef void (*response_handler_f)(response_t *response, void *user_data);
  49. /*
  50. *****************
  51. * Request APIs
  52. *****************
  53. */
  54. /**
  55. * @brief Register resource.
  56. *
  57. * @param url url of the resource
  58. * @param handler callback function to handle the request to the resource
  59. *
  60. * @return true if success, false otherwise
  61. */
  62. bool api_register_resource_handler(const char *url, request_handler_f handler);
  63. /**
  64. * @brief Send request asynchronously.
  65. *
  66. * @param request pointer of the request to be sent
  67. * @param response_handler callback function to handle the response
  68. * @param user_data user data
  69. */
  70. void api_send_request(request_t * request, response_handler_f response_handler,
  71. void * user_data);
  72. /**
  73. * @brief Send response.
  74. *
  75. * @param response pointer of the response to be sent
  76. *
  77. * @par
  78. * @code
  79. * void res1_handler(request_t *request)
  80. * {
  81. * response_t response[1];
  82. * make_response_for_request(request, response);
  83. * set_response(response, DELETED_2_02, 0, NULL, 0);
  84. * api_response_send(response);
  85. * }
  86. * @endcode
  87. */
  88. void api_response_send(response_t *response);
  89. /*
  90. *****************
  91. * Event APIs
  92. *****************
  93. */
  94. /**
  95. * @brief Publish an event.
  96. *
  97. * @param url url of the event
  98. * @param fmt format of the event payload
  99. * @param payload payload of the event
  100. * @param payload_len length in bytes of the event payload
  101. *
  102. * @return true if success, false otherwise
  103. */
  104. bool api_publish_event(const char *url, int fmt, void *payload,
  105. int payload_len);
  106. /**
  107. * @brief Subscribe an event.
  108. *
  109. * @param url url of the event
  110. * @param handler callback function to handle the event.
  111. *
  112. * @return true if success, false otherwise
  113. */
  114. bool api_subscribe_event(const char * url, request_handler_f handler);
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif