From 654a45dad6615b765478f1a0c3e9facda8428804 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 29 Jan 2024 10:04:16 +1030 Subject: [PATCH] common: add param_channel_type helper. We use an array of bit numbers. We could use an array of names, but the JSON typing is then harder. Signed-off-by: Rusty Russell --- common/Makefile | 1 + common/json_channel_type.c | 32 ++++++++++++++++++++++++++++++++ common/json_channel_type.h | 17 +++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 common/json_channel_type.c create mode 100644 common/json_channel_type.h diff --git a/common/Makefile b/common/Makefile index 83820bf16..55da9a41b 100644 --- a/common/Makefile +++ b/common/Makefile @@ -54,6 +54,7 @@ COMMON_SRC_NOGEN := \ common/initial_commit_tx.c \ common/invoice_path_id.c \ common/iso4217.c \ + common/json_channel_type.c \ common/json_filter.c \ common/json_param.c \ common/json_parse.c \ diff --git a/common/json_channel_type.c b/common/json_channel_type.c new file mode 100644 index 000000000..d320591d5 --- /dev/null +++ b/common/json_channel_type.c @@ -0,0 +1,32 @@ +#include "config.h" +#include +#include +#include +#include + +struct command_result *param_channel_type(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + struct channel_type **ctype) +{ + u8 *features = tal_arr(NULL, u8, 0); + size_t i; + const jsmntok_t *t; + + if (tok->type != JSMN_ARRAY) { + return command_fail_badparam(cmd, name, buffer, tok, + "must be an array"); + } + + json_for_each_arr(i, t, tok) { + u16 fbit; + if (!json_to_u16(buffer, t, &fbit)) + return command_fail_badparam(cmd, name, buffer, t, + "must be a 16-bit integer"); + set_feature_bit(&features, fbit); + } + + *ctype = channel_type_from(cmd, take(features)); + return NULL; +} diff --git a/common/json_channel_type.h b/common/json_channel_type.h new file mode 100644 index 000000000..df4e81284 --- /dev/null +++ b/common/json_channel_type.h @@ -0,0 +1,17 @@ +/* Dealing with channel_type in JSON */ +#ifndef LIGHTNING_COMMON_JSON_CHANNEL_TYPE_H +#define LIGHTNING_COMMON_JSON_CHANNEL_TYPE_H +#include "config.h" +#include + +struct command; +struct channel_type; + +/* Parse [1,2] as a channel_type */ +struct command_result *param_channel_type(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + struct channel_type **ctype); + +#endif /* LIGHTNING_COMMON_JSON_CHANNEL_TYPE_H */