request.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "bi-inc/shared_utils.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* CoAP request method codes */
  12. typedef enum {
  13. COAP_GET = 1,
  14. COAP_POST,
  15. COAP_PUT,
  16. COAP_DELETE,
  17. COAP_EVENT = (COAP_DELETE + 2)
  18. } coap_method_t;
  19. /* CoAP response codes */
  20. typedef enum {
  21. NO_ERROR = 0,
  22. CREATED_2_01 = 65, /* CREATED */
  23. DELETED_2_02 = 66, /* DELETED */
  24. VALID_2_03 = 67, /* NOT_MODIFIED */
  25. CHANGED_2_04 = 68, /* CHANGED */
  26. CONTENT_2_05 = 69, /* OK */
  27. CONTINUE_2_31 = 95, /* CONTINUE */
  28. BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
  29. UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
  30. BAD_OPTION_4_02 = 130, /* BAD_OPTION */
  31. FORBIDDEN_4_03 = 131, /* FORBIDDEN */
  32. NOT_FOUND_4_04 = 132, /* NOT_FOUND */
  33. METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
  34. NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
  35. PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
  36. REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
  37. UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
  38. INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
  39. NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
  40. BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
  41. SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
  42. GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
  43. PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
  44. /* Erbium errors */
  45. MEMORY_ALLOCATION_ERROR = 192,
  46. PACKET_SERIALIZATION_ERROR,
  47. /* Erbium hooks */
  48. MANUAL_RESPONSE,
  49. PING_RESPONSE
  50. } coap_status_t;
  51. /**
  52. * @typedef request_handler_f
  53. *
  54. * @brief Define the signature of callback function for API
  55. * api_register_resource_handler() to handle request or for API
  56. * api_subscribe_event() to handle event.
  57. *
  58. * @param request pointer of the request to be handled
  59. *
  60. * @see api_register_resource_handler
  61. * @see api_subscribe_event
  62. */
  63. typedef void (*request_handler_f)(request_t *request);
  64. /**
  65. * @typedef response_handler_f
  66. *
  67. * @brief Define the signature of callback function for API
  68. * api_send_request() to handle response of a request.
  69. *
  70. * @param response pointer of the response to be handled
  71. * @param user_data user data associated with the request which is set when
  72. * calling api_send_request().
  73. *
  74. * @see api_send_request
  75. */
  76. typedef void (*response_handler_f)(response_t *response, void *user_data);
  77. /*
  78. *****************
  79. * Request APIs
  80. *****************
  81. */
  82. /**
  83. * @brief Register resource.
  84. *
  85. * @param url url of the resource
  86. * @param handler callback function to handle the request to the resource
  87. *
  88. * @return true if success, false otherwise
  89. */
  90. bool
  91. api_register_resource_handler(const char *url, request_handler_f handler);
  92. /**
  93. * @brief Send request asynchronously.
  94. *
  95. * @param request pointer of the request to be sent
  96. * @param response_handler callback function to handle the response
  97. * @param user_data user data
  98. */
  99. void
  100. api_send_request(request_t *request, response_handler_f response_handler,
  101. void *user_data);
  102. /**
  103. * @brief Send response.
  104. *
  105. * @param response pointer of the response to be sent
  106. *
  107. * @par
  108. * @code
  109. * void res1_handler(request_t *request)
  110. * {
  111. * response_t response[1];
  112. * make_response_for_request(request, response);
  113. * set_response(response, DELETED_2_02, 0, NULL, 0);
  114. * api_response_send(response);
  115. * }
  116. * @endcode
  117. */
  118. void
  119. api_response_send(response_t *response);
  120. /*
  121. *****************
  122. * Event APIs
  123. *****************
  124. */
  125. /**
  126. * @brief Publish an event.
  127. *
  128. * @param url url of the event
  129. * @param fmt format of the event payload
  130. * @param payload payload of the event
  131. * @param payload_len length in bytes of the event payload
  132. *
  133. * @return true if success, false otherwise
  134. */
  135. bool
  136. api_publish_event(const char *url, int fmt, void *payload, int payload_len);
  137. /**
  138. * @brief Subscribe an event.
  139. *
  140. * @param url url of the event
  141. * @param handler callback function to handle the event.
  142. *
  143. * @return true if success, false otherwise
  144. */
  145. bool
  146. api_subscribe_event(const char *url, request_handler_f handler);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif