command.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2018 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #pragma once
  23. /* Obfuscate command internals */
  24. typedef struct swclt_cmd_ctx swclt_cmd_ctx_t;
  25. typedef ks_handle_t swclt_cmd_t;
  26. /* A command is one of three types, request, result, error */
  27. typedef enum {
  28. SWCLT_CMD_TYPE_INVALID,
  29. SWCLT_CMD_TYPE_ERROR, /* Remote responded with an error */
  30. SWCLT_CMD_TYPE_REQUEST, /* Initial state, constructed from a request, ready to submit */
  31. SWCLT_CMD_TYPE_RESULT, /* Remote responded with a non error result */
  32. SWCLT_CMD_TYPE_FAILURE, /* A failure ocurred that prevented remote from responding */
  33. } SWCLT_CMD_TYPE;
  34. static inline const char * swclt_cmd_type_str(SWCLT_CMD_TYPE type)
  35. {
  36. switch (type) {
  37. case SWCLT_CMD_TYPE_INVALID:
  38. return "Invalid";
  39. case SWCLT_CMD_TYPE_ERROR:
  40. return "Error";
  41. case SWCLT_CMD_TYPE_REQUEST:
  42. return "Request";
  43. case SWCLT_CMD_TYPE_RESULT:
  44. return "Result";
  45. case SWCLT_CMD_TYPE_FAILURE:
  46. return "Failure";
  47. default:
  48. ks_debug_break();
  49. return "Unknown";
  50. }
  51. }
  52. /* Flags control aspects of the command such as whether a reply is expected */
  53. #define SWCLT_CMD_FLAG_NOREPLY KS_BIT_FLAG(0)
  54. #define SWCLT_CMD_FLAG_MAX 1
  55. typedef ks_status_t (*swclt_cmd_parse_cb_t)(ks_pool_t *pool, ks_json_t * const payload, void **structure);
  56. typedef void (*swclt_cmd_cb_t)(swclt_cmd_t cmd, void *cb_data);
  57. KS_BEGIN_EXTERN_C
  58. SWCLT_DECLARE(ks_status_t) __swclt_cmd_set_cb(swclt_cmd_t cmd, swclt_cmd_cb_t cb, void *cb_data,
  59. const char *file, int line, const char *tag);
  60. SWCLT_DECLARE(ks_status_t) __swclt_cmd_cb(swclt_cmd_t cmd, swclt_cmd_cb_t *cb, void **cb_data,
  61. const char *file, int line, const char *tag);
  62. /* Create a command from a request, optional caller pool */
  63. SWCLT_DECLARE(ks_status_t) __swclt_cmd_create(swclt_cmd_t *cmd, const char * const method, ks_json_t **request,
  64. uint32_t response_ttl_ms, uint32_t flags, const char *file, int line, const char *tag);
  65. #define swclt_cmd_create(cmd, method, request, response_ttl_ms, flags) \
  66. __swclt_cmd_create(cmd, method, request, response_ttl_ms, flags, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  67. /* Create a command from a request, and set a callback for async handling, optional caller pool */
  68. SWCLT_DECLARE(ks_status_t) __swclt_cmd_create_ex(swclt_cmd_t *cmd, ks_pool_t **pool, swclt_cmd_cb_t cb, void *cb_data,
  69. const char * const method, ks_json_t **request, uint32_t response_ttl_ms, uint32_t flags, ks_uuid_t id,
  70. const char *file, int line, const char *tag);
  71. #define swclt_cmd_create_ex(cmd, pool, cb, cb_data, method, request,response_ttl_ms, flags, id) \
  72. __swclt_cmd_create_ex(cmd, pool, cb, cb_data, method, request, response_ttl_ms, flags, id, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  73. /* Create a request from a frame w/optional callback */
  74. SWCLT_DECLARE(ks_status_t) __swclt_cmd_create_frame(
  75. swclt_cmd_t *cmd,
  76. swclt_cmd_cb_t cb,
  77. void *cb_data,
  78. swclt_frame_t *frame,
  79. uint32_t response_ttl_ms,
  80. uint32_t flags,
  81. const char *file,
  82. int line,
  83. const char *tag);
  84. #define swclt_cmd_create_frame(cmd, cb, cb_data, frame, frame_response_ttl_ms, flags) \
  85. __swclt_cmd_create_frame(cmd, cb, cb_data, frame, frame_response_ttl_ms, flags, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  86. SWCLT_DECLARE(ks_status_t) __swclt_cmd_print(swclt_cmd_t cmd, ks_pool_t *pool, char **string, const char *file, int line, const char *tag);
  87. SWCLT_DECLARE(ks_status_t) __swclt_cmd_type(swclt_cmd_t cmd, SWCLT_CMD_TYPE *type, const char *file, int line, const char *tag);
  88. SWCLT_DECLARE(ks_status_t) __swclt_cmd_set_result(swclt_cmd_t cmd, ks_json_t **result, const char *file, int line, const char *tag);
  89. SWCLT_DECLARE(ks_status_t) __swclt_cmd_set_error(swclt_cmd_t cmd, ks_json_t **result, const char *file, int line, const char *tag);
  90. SWCLT_DECLARE(ks_status_t) __swclt_cmd_parse(const char *file, int line, const char *tag, swclt_cmd_t cmd, ks_pool_t *pool, swclt_cmd_parse_cb_t parse_cb, void **structure);
  91. SWCLT_DECLARE(ks_status_t) __swclt_cmd_error(swclt_cmd_t cmd, ks_json_t **error, const char *file, int line, const char *tag);
  92. SWCLT_DECLARE(ks_status_t) __swclt_cmd_result(swclt_cmd_t cmd, ks_json_t **result, const char *file, int line, const char *tag);
  93. SWCLT_DECLARE(ks_status_t) __swclt_cmd_request(swclt_cmd_t cmd, ks_json_t **request, const char *file, int line, const char *tag);
  94. SWCLT_DECLARE(ks_status_t) __swclt_cmd_id(swclt_cmd_t cmd, ks_uuid_t *id, const char *file, int line, const char *tag);
  95. SWCLT_DECLARE(ks_status_t) __swclt_cmd_ttl(swclt_cmd_t cmd, uint32_t *response_ttl_ms, const char *file, int line, const char *tag);
  96. SWCLT_DECLARE(ks_status_t) __swclt_cmd_set_ttl(swclt_cmd_t cmd, uint32_t response_ttl_ms, const char *file, int line, const char *tag);
  97. SWCLT_DECLARE(ks_status_t) __swclt_cmd_failure_info(swclt_cmd_t cmd, ks_status_t *failure_status, const char **failure_reason, const char *file, int line, const char *tag);
  98. SWCLT_DECLARE(ks_status_t) __swclt_cmd_flags(swclt_cmd_t cmd, uint32_t *flags, const char *file, int line, const char *tag);
  99. SWCLT_DECLARE(ks_status_t) __swclt_cmd_method(swclt_cmd_t cmd, const char **method, const char *file, int line, const char *tag);
  100. SWCLT_DECLARE(ks_status_t) __swclt_cmd_parse_reply_frame(swclt_cmd_t cmd, swclt_frame_t *frame, ks_bool_t *async, const char *file, int line, const char *tag);
  101. SWCLT_DECLARE(ks_status_t) swclt_cmd_wait_result(swclt_cmd_t cmd);
  102. #define swclt_cmd_set_cb(cmd, cb, cb_data) __swclt_cmd_set_cb(cmd, cb, cb_data, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  103. #define swclt_cmd_cb(cmd, cb, cb_data) __swclt_cmd_cb(cmd, cb, cb_data, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  104. #define swclt_cmd_print(cmd, pool, string) __swclt_cmd_print(cmd, pool, string, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  105. #define swclt_cmd_type(cmd, type) __swclt_cmd_type(cmd, type, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  106. #define swclt_cmd_result(cmd, result) __swclt_cmd_result(cmd, result, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  107. #define swclt_cmd_set_result(cmd, result) __swclt_cmd_set_result(cmd, result, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  108. #define swclt_cmd_set_error(cmd, error) __swclt_cmd_set_error(cmd, error, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  109. #define swclt_cmd_parse(cmd, pool, parse_cb, structure) __swclt_cmd_parse(__FILE__, __LINE__, __PRETTY_FUNCTION__, cmd, pool, parse_cb, structure)
  110. #define swclt_cmd_error(cmd, error) __swclt_cmd_error(cmd, error, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  111. #define swclt_cmd_request(cmd, request) __swclt_cmd_request(cmd, request, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  112. #define swclt_cmd_id(cmd, id) __swclt_cmd_id(cmd, id, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  113. #define swclt_cmd_ttl(cmd, response_ttl_ms) __swclt_cmd_ttl(cmd, response_ttl_ms, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  114. #define swclt_cmd_set_ttl(cmd, response_ttl_ms) __swclt_cmd_set_ttl(cmd, response_ttl_ms, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  115. #define swclt_cmd_failure_info(cmd, failure_status, failure_reason) __swclt_cmd_failure_info(cmd, failure_status, failure_reason, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  116. #define swclt_cmd_flags(cmd, flags) __swclt_cmd_flags(cmd, flags, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  117. #define swclt_cmd_method(cmd, method) __swclt_cmd_method(cmd, method, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  118. #define swclt_cmd_parse_reply_frame(cmd, frame, async) __swclt_cmd_parse_reply_frame(cmd, frame, async, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  119. SWCLT_DECLARE(ks_status_t) __swclt_cmd_report_failure_fmt(const char *file, int line, const char *tag, swclt_cmd_t cmd, ks_status_t failure_status, const char *failure_fmt, ...);
  120. #define swclt_cmd_report_failure_fmt_s(cmd, status, error_fmt) __swclt_cmd_report_failure_fmt(__FILE__, __LINE__, __PRETTY_FUNCTION__, cmd, status, error_fmt)
  121. #define swclt_cmd_report_failure_fmt_m(cmd, status, error_fmt, ...) __swclt_cmd_report_failure_fmt(__FILE__, __LINE__, __PRETTY_FUNCTION__, cmd, status, error_fmt, __VA_ARGS__)
  122. SWCLT_DECLARE(ks_status_t) __swclt_cmd_report_failure(swclt_cmd_t cmd, ks_status_t failure_status, const char *failure_description, const char *file, int line, const char *tag);
  123. #define swclt_cmd_report_failure(cmd, status, description) __swclt_cmd_report_failure(cmd, status, description, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  124. #define swclt_cmd_get(cmd, contextP) __ks_handle_get(SWCLT_HTYPE_CMD, cmd, (ks_handle_base_t **)contextP, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  125. #define swclt_cmd_put(contextP) __ks_handle_put(SWCLT_HTYPE_CMD, (ks_handle_base_t **)contextP, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  126. KS_END_EXTERN_C
  127. /* For Emacs:
  128. * Local Variables:
  129. * mode:c
  130. * indent-tabs-mode:t
  131. * tab-width:4
  132. * c-basic-offset:4
  133. * End:
  134. * For VIM:
  135. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  136. */