Files
osmo-upf/tests/libosmo-tlv/test_tlv_gen/myproto_ies_custom.c
Neels Hofmeyr 63cba988d3 libosmo-tlv: add C code generator for IE structs and arrays
Defining a protocol of message types with lists of IEs bears a lot of
repetitive, copy-paste-error-prone writing out of data structures.
Add a third layer to libosmo-tlv, which allows helpful code generation.

By non-repetitive data structures that briefly describe the protocol's
messages and IEs, generate possibly repetitive IE list arrays and
decoded-struct definitions automatically, avoiding grunt work errors.

I tried C macros for this at first, but it became too convoluted.
Generating C code that can be read and grepped makes things easier.

A usage example is found in tests/libosmo-tlv/test_tlv_gen/.

Related: SYS#5599
Change-Id: Ifb3ea54d2797ce060b95834aa117725ec2d6c4cf
2022-02-25 01:53:07 +01:00

180 lines
5.2 KiB
C

/* Example for defining custom IES for tlv_gen.
*
* (C) 2021-2022 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* All Rights Reserved.
*
* Author: Neels Janosch Hofmeyr <nhofmeyr@sysmocom.de>
*
* SPDX-License-Identifier: GPL-2.0+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <errno.h>
#include <inttypes.h>
#include <osmocom/core/bits.h>
#include <osmocom/core/msgb.h>
#include <osmocom/tlv/tlv.h>
#include <myproto_ies_custom.h>
int myproto_dec_u16(void *decoded_struct, void *decode_to, const struct osmo_tlv_load *tlv)
{
int *foo = decode_to;
if (tlv->len != 2)
return -EINVAL;
*foo = osmo_load16be(tlv->val);
return 0;
}
int myproto_enc_u16(struct osmo_tlv_put *tlv, void *decoded_struct, void *encode_from)
{
int *foo = encode_from;
if (*foo > INT16_MAX)
return -EINVAL;
msgb_put_u16(tlv->dst, *foo);
return 0;
}
int myproto_enc_to_str_u16(char *buf, size_t buflen, void *encode_from)
{
int *foo = encode_from;
return snprintf(buf, buflen, "%d", *foo);
}
int myproto_dec_u64(void *decoded_struct, void *decode_to, const struct osmo_tlv_load *tlv)
{
uint64_t *val = decode_to;
if (tlv->len != sizeof(uint64_t))
return -EINVAL;
*val = osmo_load64be(tlv->val);
return 0;
}
int myproto_enc_u64(struct osmo_tlv_put *tlv, void *decoded_struct, void *encode_from)
{
uint64_t *val = encode_from;
osmo_store64be(*val, msgb_put(tlv->dst, sizeof(*val)));
return 0;
}
int myproto_enc_to_str_u64(char *buf, size_t buflen, void *encode_from)
{
uint64_t *val = encode_from;
return snprintf(buf, buflen, "0x%"PRIx64, *val);
}
int myproto_dec_bar(void *decoded_struct, void *decode_to, const struct osmo_tlv_load *tlv)
{
struct myproto_ie_bar *bar = decode_to;
if (tlv->len > sizeof(bar->str) - 1)
return -EINVAL;
osmo_strlcpy(bar->str, (const char*)tlv->val, OSMO_MIN(tlv->len + 1, sizeof(bar->str)));
return 0;
}
int myproto_enc_bar(struct osmo_tlv_put *tlv, void *decoded_struct, void *encode_from)
{
struct myproto_ie_bar *bar = encode_from;
int len = strnlen(bar->str, sizeof(bar->str));
memcpy(msgb_put(tlv->dst, len), bar, len);
return 0;
}
int myproto_enc_to_str_bar(char *buf, size_t buflen, void *encode_from)
{
struct myproto_ie_bar *bar = encode_from;
return osmo_quote_str_buf3(buf, buflen, bar->str, -1);
}
int myproto_dec_baz(void *decoded_struct, void *decode_to, const struct osmo_tlv_load *tlv)
{
struct myproto_ie_baz *baz = decode_to;
uint16_t l;
if (tlv->len != 2)
return -EINVAL;
l = osmo_load16be(tlv->val);
baz->v_int = l & 0x7fff;
baz->v_bool = (l & 0x8000) ? true : false;
return 0;
}
int myproto_enc_baz(struct osmo_tlv_put *tlv, void *decoded_struct, void *encode_from)
{
struct myproto_ie_baz *baz = encode_from;
if (baz->v_int > 0x7fff)
return -EINVAL;
msgb_put_u16(tlv->dst, (baz->v_bool ? 0x8000 : 0) + (baz->v_int & 0x7fff));
return 0;
}
int myproto_enc_to_str_baz(char *buf, size_t buflen, void *encode_from)
{
struct myproto_ie_baz *baz = encode_from;
return snprintf(buf, buflen, "{%d,%s}", baz->v_int, baz->v_bool ? "true" : "false");
}
int myproto_dec_repeat_struct(void *decoded_struct, void *decode_to, const struct osmo_tlv_load *tlv)
{
struct myproto_ie_repeat_struct *repeat_struct = decode_to;
if (tlv->len != 3)
return -EINVAL;
repeat_struct->v_int = osmo_load16be(tlv->val);
repeat_struct->v_bool = tlv->val[2] & 0x80;
repeat_struct->v_enum = tlv->val[2] & 0x7f;
return 0;
}
int myproto_enc_repeat_struct(struct osmo_tlv_put *tlv, void *decoded_struct, void *encode_from)
{
struct myproto_ie_repeat_struct *repeat_struct = encode_from;
msgb_put_u16(tlv->dst, repeat_struct->v_int);
msgb_put_u8(tlv->dst, (repeat_struct->v_bool ? 0x80 : 0) + (repeat_struct->v_enum & 0x7f));
return 0;
}
int myproto_enc_to_str_repeat_struct(char *buf, size_t buflen, void *encode_from)
{
struct myproto_ie_repeat_struct *repeat_struct = encode_from;
return snprintf(buf, buflen, "{%d,%s,%s}",
repeat_struct->v_int, repeat_struct->v_bool ? "true" : "false",
get_value_string(myproto_repeat_enum_names, repeat_struct->v_enum));
}
const struct value_string myproto_msg_type_names[] = {
{ MYPROTO_MSGT_MOO, "MOO" },
{ MYPROTO_MSGT_GOO, "GOO" },
{}
};
const struct value_string myproto_iei_names[] = {
{ MYPROTO_IEI_FOO, "FOO" },
{ MYPROTO_IEI_BAR, "BAR" },
{ MYPROTO_IEI_BAZ, "BAZ" },
{ MYPROTO_IEI_REPEAT_INT, "REPEAT_INT" },
{ MYPROTO_IEI_REPEAT_STRUCT, "REPEAT_STRUCT" },
{ MYPROTO_IEI_MOO_NEST, "MOO_NEST" },
{ MYPROTO_IEI_VAL, "VAL" },
{ MYPROTO_IEI_GOO_NEST, "GOO_NEST" },
{}
};
const struct value_string myproto_repeat_enum_names[] = {
OSMO_VALUE_STRING(R_A),
OSMO_VALUE_STRING(R_B),
OSMO_VALUE_STRING(R_C),
{}
};