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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-01-29 10:04:16 +10:30
parent 031524fab5
commit 654a45dad6
3 changed files with 50 additions and 0 deletions

View file

@ -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 \

View file

@ -0,0 +1,32 @@
#include "config.h"
#include <common/channel_type.h>
#include <common/json_channel_type.h>
#include <common/json_command.h>
#include <common/json_param.h>
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;
}

View file

@ -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 <common/json_parse_simple.h>
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 */