/* THIS FILE IS GENERATED FROM gtlv_gen.c */
#include <stddef.h>
#include <errno.h>
#include <osmocom/core/utils.h>
#include <osmocom/gtlv/gtlv.h>
#include <osmocom/gtlv/gtlv_dec_enc.h>
#include <osmocom/gtlv/gtlv_gen.h>

#include <myproto_ies_auto.h>

extern int myproto_dec_u16(void *decoded_struct, void *decode_to, const struct osmo_gtlv_load *gtlv);
extern int myproto_enc_u16(struct osmo_gtlv_put *gtlv, const void *decoded_struct, const void *encode_from);
extern int myproto_enc_to_str_u16(char *buf, size_t buflen, const void *encode_from);
extern int myproto_dec_bar(void *decoded_struct, void *decode_to, const struct osmo_gtlv_load *gtlv);
extern int myproto_enc_bar(struct osmo_gtlv_put *gtlv, const void *decoded_struct, const void *encode_from);
extern int myproto_enc_to_str_bar(char *buf, size_t buflen, const void *encode_from);
extern int myproto_dec_baz(void *decoded_struct, void *decode_to, const struct osmo_gtlv_load *gtlv);
extern int myproto_enc_baz(struct osmo_gtlv_put *gtlv, const void *decoded_struct, const void *encode_from);
extern int myproto_enc_to_str_baz(char *buf, size_t buflen, const void *encode_from);
extern int myproto_dec_repeat_struct(void *decoded_struct, void *decode_to, const struct osmo_gtlv_load *gtlv);
extern int myproto_enc_repeat_struct(struct osmo_gtlv_put *gtlv, const void *decoded_struct, const void *encode_from);
extern int myproto_enc_to_str_repeat_struct(char *buf, size_t buflen, const void *encode_from);
extern int myproto_dec_u64(void *decoded_struct, void *decode_to, const struct osmo_gtlv_load *gtlv);
extern int myproto_enc_u64(struct osmo_gtlv_put *gtlv, const void *decoded_struct, const void *encode_from);
extern int myproto_enc_to_str_u64(char *buf, size_t buflen, const void *encode_from);

static const struct osmo_gtlv_coding ies_in_moo_nest[] = {
	{ { MYPROTO_IEI_FOO },
	  .memb_ofs = offsetof(struct myproto_ie_moo_nest, foo),
	  .dec_func = myproto_dec_u16,
	  .enc_func = myproto_enc_u16,
	  .enc_to_str_func = myproto_enc_to_str_u16,
	},
	{ { MYPROTO_IEI_BAR },
	  .memb_ofs = offsetof(struct myproto_ie_moo_nest, bar),
	  .dec_func = myproto_dec_bar,
	  .enc_func = myproto_enc_bar,
	  .enc_to_str_func = myproto_enc_to_str_bar,
	},
	{ { MYPROTO_IEI_BAZ },
	  .memb_ofs = offsetof(struct myproto_ie_moo_nest, baz),
	  .dec_func = myproto_dec_baz,
	  .enc_func = myproto_enc_baz,
	  .enc_to_str_func = myproto_enc_to_str_baz,
	},
	{}
};

static const struct osmo_gtlv_coding ies_in_goo_nest[] = {
	{ { MYPROTO_IEI_VAL },
	  .memb_ofs = offsetof(struct myproto_ie_goo_nest, val),
	  .dec_func = myproto_dec_u64,
	  .enc_func = myproto_enc_u64,
	  .enc_to_str_func = myproto_enc_to_str_u64,
	  .has_presence_flag = true,
	  .presence_flag_ofs = offsetof(struct myproto_ie_goo_nest, val_present),
	},
	{ { MYPROTO_IEI_MOO_NEST },
	  .memb_ofs = offsetof(struct myproto_ie_goo_nest, nest),
	  .nested_ies = ies_in_moo_nest,
	},
	{}
};

static const struct osmo_gtlv_coding ies_in_msg_moo[] = {
	{ { MYPROTO_IEI_FOO },
	  .memb_ofs = offsetof(union myproto_ies, moo.foo),
	  .dec_func = myproto_dec_u16,
	  .enc_func = myproto_enc_u16,
	  .enc_to_str_func = myproto_enc_to_str_u16,
	},
	{ { MYPROTO_IEI_BAR },
	  .memb_ofs = offsetof(union myproto_ies, moo.bar),
	  .dec_func = myproto_dec_bar,
	  .enc_func = myproto_enc_bar,
	  .enc_to_str_func = myproto_enc_to_str_bar,
	},
	{ { MYPROTO_IEI_BAZ },
	  .memb_ofs = offsetof(union myproto_ies, moo.baz),
	  .dec_func = myproto_dec_baz,
	  .enc_func = myproto_enc_baz,
	  .enc_to_str_func = myproto_enc_to_str_baz,
	  .has_presence_flag = true,
	  .presence_flag_ofs = offsetof(union myproto_ies, moo.baz_present),
	},
	{ { MYPROTO_IEI_REPEAT_INT },
	  .memb_ofs = offsetof(union myproto_ies, moo.repeat_int),
	  .dec_func = myproto_dec_u16,
	  .enc_func = myproto_enc_u16,
	  .enc_to_str_func = myproto_enc_to_str_u16,
	  .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(union myproto_ies, moo.repeat_int),
	  .has_count = true, .count_max = 32,
	  .count_mandatory = 0,
	  .count_ofs = offsetof(union myproto_ies, moo.repeat_int_count),
	},
	{ { MYPROTO_IEI_REPEAT_STRUCT },
	  .memb_ofs = offsetof(union myproto_ies, moo.repeat_struct),
	  .dec_func = myproto_dec_repeat_struct,
	  .enc_func = myproto_enc_repeat_struct,
	  .enc_to_str_func = myproto_enc_to_str_repeat_struct,
	  .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(union myproto_ies, moo.repeat_struct),
	  .has_count = true, .count_max = 32,
	  .count_mandatory = 0,
	  .count_ofs = offsetof(union myproto_ies, moo.repeat_struct_count),
	},
	{ { MYPROTO_IEI_MOO_NEST },
	  .memb_ofs = offsetof(union myproto_ies, moo.nest),
	  .nested_ies = ies_in_moo_nest,
	  .has_presence_flag = true,
	  .presence_flag_ofs = offsetof(union myproto_ies, moo.nest_present),
	},
	{}
};

static const struct osmo_gtlv_coding ies_in_msg_goo[] = {
	{ { MYPROTO_IEI_FOO },
	  .memb_ofs = offsetof(union myproto_ies, goo.foo),
	  .dec_func = myproto_dec_u16,
	  .enc_func = myproto_enc_u16,
	  .enc_to_str_func = myproto_enc_to_str_u16,
	},
	{ { MYPROTO_IEI_BAR },
	  .memb_ofs = offsetof(union myproto_ies, goo.bar),
	  .dec_func = myproto_dec_bar,
	  .enc_func = myproto_enc_bar,
	  .enc_to_str_func = myproto_enc_to_str_bar,
	  .has_presence_flag = true,
	  .presence_flag_ofs = offsetof(union myproto_ies, goo.bar_present),
	},
	{ { MYPROTO_IEI_GOO_NEST },
	  .memb_ofs = offsetof(union myproto_ies, goo.nest),
	  .nested_ies = ies_in_goo_nest,
	  .memb_array_pitch = OSMO_MEMB_ARRAY_PITCH(union myproto_ies, goo.nest),
	  .has_count = true, .count_max = 8,
	  .count_mandatory = 0,
	  .count_ofs = offsetof(union myproto_ies, goo.nest_count),
	},
	{}
};

static const struct osmo_gtlv_coding *msg_defs[] = {
	[MYPROTO_MSGT_MOO] = ies_in_msg_moo,
	[MYPROTO_MSGT_GOO] = ies_in_msg_goo,
};

const struct osmo_gtlv_coding *myproto_get_msg_coding(enum myproto_msg_type message_type)
{
	if (message_type >= ARRAY_SIZE(msg_defs))
		return NULL;
	return msg_defs[message_type];
}

int myproto_ies_decode(union myproto_ies *dst, struct osmo_gtlv_load *gtlv, bool tlv_ordered,
	enum myproto_msg_type message_type,
	osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs)
{
	return osmo_gtlvs_decode(dst, sizeof(*dst), 0, gtlv, tlv_ordered, myproto_get_msg_coding(message_type),
		err_cb, err_cb_data, iei_strs);
}

int myproto_ies_encode(struct osmo_gtlv_put *gtlv, const union myproto_ies *src,
	enum myproto_msg_type message_type, osmo_gtlv_err_cb err_cb, void *err_cb_data, const struct value_string *iei_strs)
{
	return osmo_gtlvs_encode(gtlv, src, sizeof(*src), 0, myproto_get_msg_coding(message_type),
		err_cb, err_cb_data, iei_strs);
}

int myproto_ies_encode_to_str(char *buf, size_t buflen, const union myproto_ies *src,
	enum myproto_msg_type message_type, const struct value_string *iei_strs)
{
	return osmo_gtlvs_encode_to_str_buf(buf, buflen, src, sizeof(*src), 0, myproto_get_msg_coding(message_type), iei_strs);
}
