request.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _AEE_REQUEST_H_
  6. #define _AEE_REQUEST_H_
  7. #include "shared_utils.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * @typedef request_handler_f
  13. *
  14. * @brief Define the signature of callback function for API
  15. * api_register_resource_handler() to handle request or for API
  16. * api_subscribe_event() to handle event.
  17. *
  18. * @param request pointer of the request to be handled
  19. *
  20. * @see api_register_resource_handler
  21. * @see api_subscribe_event
  22. */
  23. typedef void (*request_handler_f)(request_t *request);
  24. /**
  25. * @typedef response_handler_f
  26. *
  27. * @brief Define the signature of callback function for API
  28. * api_send_request() to handle response of a request.
  29. *
  30. * @param response pointer of the response to be handled
  31. * @param user_data user data associated with the request which is set when
  32. * calling api_send_request().
  33. *
  34. * @see api_send_request
  35. */
  36. typedef void (*response_handler_f)(response_t *response, void *user_data);
  37. /*
  38. *****************
  39. * Request APIs
  40. *****************
  41. */
  42. /**
  43. * @brief Register resource.
  44. *
  45. * @param url url of the resource
  46. * @param handler callback function to handle the request to the resource
  47. *
  48. * @return true if success, false otherwise
  49. */
  50. bool api_register_resource_handler(const char *url, request_handler_f handler);
  51. /**
  52. * @brief Send request asynchronously.
  53. *
  54. * @param request pointer of the request to be sent
  55. * @param response_handler callback function to handle the response
  56. * @param user_data user data
  57. */
  58. void api_send_request(request_t * request, response_handler_f response_handler,
  59. void * user_data);
  60. /**
  61. * @brief Send response.
  62. *
  63. * @param response pointer of the response to be sent
  64. *
  65. * @par
  66. * @code
  67. * void res1_handler(request_t *request)
  68. * {
  69. * response_t response[1];
  70. * make_response_for_request(request, response);
  71. * set_response(response, DELETED_2_02, 0, NULL, 0);
  72. * api_response_send(response);
  73. * }
  74. * @endcode
  75. */
  76. void api_response_send(response_t *response);
  77. /*
  78. *****************
  79. * Event APIs
  80. *****************
  81. */
  82. /**
  83. * @brief Publish an event.
  84. *
  85. * @param url url of the event
  86. * @param fmt format of the event payload
  87. * @param payload payload of the event
  88. * @param payload_len length in bytes of the event payload
  89. *
  90. * @return true if success, false otherwise
  91. */
  92. bool api_publish_event(const char *url, int fmt, void *payload,
  93. int payload_len);
  94. /**
  95. * @brief Subscribe an event.
  96. *
  97. * @param url url of the event
  98. * @param handler callback function to handle the event.
  99. *
  100. * @return true if success, false otherwise
  101. */
  102. bool api_subscribe_event(const char * url, request_handler_f handler);
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #endif