From 370c70dabac75e7f1b3d85f524e4ee5266c49aba Mon Sep 17 00:00:00 2001 From: Albert Cervin Date: Sun, 6 Oct 2024 21:32:26 +0200 Subject: [PATCH] Start of jsonrpc interface --- flake.lock | 12 ++++++------ src/dged/json.c | 4 ++++ src/dged/json.h | 9 +++++++++ src/dged/jsonrpc.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 src/dged/jsonrpc.h diff --git a/flake.lock b/flake.lock index 628df20..14fe178 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", "type": "github" }, "original": { @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1725930920, - "narHash": "sha256-RVhD9hnlTT2nJzPHlAqrWqCkA7T6CYrP41IoVRkciZM=", + "lastModified": 1728193676, + "narHash": "sha256-PbDWAIjKJdlVg+qQRhzdSor04bAPApDqIv2DofTyynk=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "44a71ff39c182edaf25a7ace5c9454e7cba2c658", + "rev": "ecbc1ca8ffd6aea8372ad16be9ebbb39889e55b6", "type": "github" }, "original": { diff --git a/src/dged/json.c b/src/dged/json.c index 70d406b..a8c3fed 100644 --- a/src/dged/json.c +++ b/src/dged/json.c @@ -322,6 +322,10 @@ uint64_t json_len(struct json_object *obj) { return HASHMAP_SIZE(&obj->members); } +uint64_t json_empty(struct json_object *obj) { + return json_len(obj) == 0; +} + bool json_contains(struct json_object *obj, struct s8 key) { // TODO: get rid of alloc char *k = s8tocstr(key); diff --git a/src/dged/json.h b/src/dged/json.h index fdabae3..ced0f73 100644 --- a/src/dged/json.h +++ b/src/dged/json.h @@ -54,6 +54,15 @@ struct json_result json_parse(const uint8_t *buf, uint64_t size); */ void json_destroy(struct json_value *value); +/** + * Check if a JSON object is empty. + * + * @param [in] obj The JSON object to check if empty. + * + * @returns True if @ref obj is empty, false otherwise. + */ +void json_empty(struct json_object *obj); + /** * Return the number of members in a JSON object. * diff --git a/src/dged/jsonrpc.h b/src/dged/jsonrpc.h new file mode 100644 index 0000000..ad2b0cc --- /dev/null +++ b/src/dged/jsonrpc.h @@ -0,0 +1,32 @@ +#ifndef _JSONRPC_H +#define _JSONRPC_H + +#include "json.h" +#include "s8.h" + +struct jsonrpc_request { + struct json_value id; + struct s8 method; + struct json_object *params; +}; + +struct jsonrpc_response { + struct json_value id; + bool ok; + union { + struct json_value result; + struct jsonrpc_error error; + } value; +}; + +struct jsonrpc_error { + int code; + struct s8 message; + struct json_value data; +}; + +struct jsonrpc_request jsonrpc_request_create(struct s8 method, struct json_object *params); +struct jsonrpc_response jsonrpc_parse_response(const uint8_t *buf, uint64_t size); +struct s8 jsonrpc_request_to_string(const struct jsonprc_request *request); + +#endif