jrpc.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef JRPC_H
  2. #define JRPC_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "cJSON.h"
  7. #include <stdint.h>
  8. #include <stdarg.h>
  9. // Define string constants
  10. #define STR_JSON_RPC_VERSION "1.0"
  11. #define STR_JSON_RPC_FIELD "jsonrpc"
  12. #define STR_METHOD_FIELD "method"
  13. #define STR_PARAMS_FIELD "params"
  14. #define STR_ID_FIELD "id"
  15. #define STR_STATUS_FIELD "status"
  16. #define STR_RESULT_FIELD "result"
  17. #define STR_RECEIVED_STATUS "received"
  18. #define STR_METHOD_NOT_FOUND_STATUS "method not found"
  19. #define STR_INVALID_PARAMS_STATUS "invalid params"
  20. #define STR_UNKNOWN_STATUS "unknown"
  21. #define RPC_MAP_END \
  22. { NULL, NULL, 0 }
  23. #define PARAM_COUNT_NO_CHECK -1
  24. // Timeout definitions
  25. #define ACK_TIMEOUT 200
  26. #define BLOCKING_TIMEOUT 1000
  27. #define RETRY_COUNT 5
  28. // Packet type definitions
  29. #define TYPE_REQUEST 0
  30. #define TYPE_ACK 1
  31. #define TYPE_RESULT 2
  32. #define STR_TYPE_REQUEST "REQ"
  33. #define STR_TYPE_ACK "ACK"
  34. #define STR_TYPE_RESULT "RES"
  35. #define STR_TYPE_FIELD "type"
  36. #define JRPC_USING_DOUBLE_ACK 1
  37. // Cache array size
  38. #define CACHE_SIZE 16
  39. typedef enum {
  40. ACK_SUCCESS = 0,
  41. ACK_METHOD_NOT_FOUND = -1,
  42. ACK_INVALID_PARAMS = -2,
  43. ACK_MEMORY_ERROR = -3
  44. } ack_status;
  45. typedef struct JRPC_ JRPC;
  46. typedef cJSON* (*rpc_function)(cJSON* params[], int param_count);
  47. typedef int (*rpc_function_nonblocking)(int id,
  48. cJSON* params[],
  49. int param_count,
  50. JRPC* self);
  51. typedef struct {
  52. const char* name;
  53. rpc_function func;
  54. int param_count; // Number of parameters
  55. } rpc_mapping;
  56. typedef struct {
  57. const char* name;
  58. rpc_function_nonblocking func;
  59. int param_count; // Number of parameters
  60. } rpc_mapping_nonblocking;
  61. typedef void (*rpc_callback)(cJSON* result);
  62. typedef void (*send_function)(const char* message);
  63. typedef char* (*receive_function)(void);
  64. typedef void (*yield_function)(void);
  65. typedef unsigned long (*tick_function)(void);
  66. struct JRPC_ {
  67. rpc_mapping* map;
  68. rpc_mapping_nonblocking* nonblocking_map;
  69. send_function send;
  70. receive_function receive;
  71. uint8_t receive_need_free;
  72. yield_function yield;
  73. tick_function tick;
  74. int current_id;
  75. cJSON* cache[CACHE_SIZE]; // Add cache
  76. int cache_count;
  77. };
  78. // Function declarations
  79. void JRPC_server_handle_string(JRPC* self, char* json_str);
  80. void JRPC_server_handle(JRPC* self);
  81. rpc_function JRPC_find_rpc_function(JRPC* self,
  82. const char* name,
  83. int* param_count);
  84. rpc_function_nonblocking JRPC_find_nonblocking_rpc_function(JRPC* self,
  85. const char* name,
  86. int* param_count);
  87. void JRPC_send_response(JRPC* self, int id, cJSON* result);
  88. void JRPC_send_request_no_blocking(JRPC* self,
  89. const char* method,
  90. cJSON* params[],
  91. int param_count,
  92. rpc_callback callback);
  93. cJSON* JRPC_send_request_blocking(JRPC* self,
  94. const char* method,
  95. cJSON* params[],
  96. int param_count);
  97. cJSON* JRPC_receive_with_id_and_type(JRPC* self, int id, int type);
  98. int jrpc_compare_json_strings(const char* json_str1, const char* json_str2);
  99. int jrpc_validate_response(const char* expected_response);
  100. void set_jrpc_memory_functions(void* (*malloc_func)(size_t),
  101. void (*free_func)(void*));
  102. void set_jrpc_vprintf_function(int (*vprintf_func)(const char*, va_list));
  103. int jrpc_test_client();
  104. int jrpc_test_server();
  105. char* JRPC_cmd(JRPC* jrpc, const char* cmd);
  106. void JRPC_init(JRPC* jrpc,
  107. rpc_mapping* rpc_map,
  108. rpc_mapping_nonblocking* nonblocking_rpc_map,
  109. void (*send_func)(const char* message),
  110. char* (*receive_func)(void),
  111. int receive_need_free,
  112. void (*yield_func)(void),
  113. unsigned long (*tick_func)(void));
  114. void JRPC_deinit(JRPC* jrpc);
  115. void* jrpc_malloc(size_t size);
  116. void jrpc_free(void* ptr);
  117. char* jrpc_strdup(const char* str);
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif // JRPC_H