Skip to content

Commit ccf4e9c

Browse files
Use TLV parser for generic parser array
1 parent 490ff54 commit ccf4e9c

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

src_features/generic_tx_parser/gtp_data_path.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ static bool handle_array(const s_tlv_data *data, s_data_path_context *context) {
4545

4646
ctx.args = &context->data_path->elements[context->data_path->size].array;
4747
explicit_bzero(ctx.args, sizeof(*ctx.args));
48-
if (!tlv_parse(data->value, data->length, (f_tlv_data_handler) &handle_array_struct, &ctx))
48+
if (!handle_array_struct(data, &ctx)) {
4949
return false;
50+
}
5051
context->data_path->elements[context->data_path->size].type = ELEMENT_TYPE_ARRAY;
5152
context->data_path->size += 1;
5253
return true;
@@ -78,7 +79,7 @@ static bool handle_slice(const s_tlv_data *data, s_data_path_context *context) {
7879

7980
ctx.args = &context->data_path->elements[context->data_path->size].slice;
8081
explicit_bzero(ctx.args, sizeof(*ctx.args));
81-
if (!handle_slice_struct(data, &ctx)){
82+
if (!handle_slice_struct(data, &ctx)) {
8283
return false;
8384
}
8485
context->data_path->elements[context->data_path->size].type = ELEMENT_TYPE_SLICE;

src_features/generic_tx_parser/gtp_path_array.c

+16-31
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44
#include <string.h>
55
#include "gtp_path_array.h"
66
#include "read.h"
7+
#include "tlv_library.h"
8+
#include "buffer.h"
79
#include "os_print.h"
810
#include "utils.h"
911

10-
enum {
11-
TAG_WEIGHT = 0x01,
12-
TAG_START = 0x02,
13-
TAG_END = 0x03,
14-
};
15-
16-
static bool handle_weight(const s_tlv_data *data, s_path_array_context *context) {
17-
if (data->length != sizeof(context->args->weight)) {
18-
return false;
19-
}
20-
context->args->weight = data->value[0];
21-
return true;
12+
static bool handle_weight(const tlv_data_t *data, s_path_array_context *context) {
13+
return get_uint8_t_from_tlv_data(data, &context->args->weight);
2214
}
2315

24-
static bool handle_start(const s_tlv_data *data, s_path_array_context *context) {
16+
static bool handle_start(const tlv_data_t *data, s_path_array_context *context) {
2517
if (data->length != sizeof(context->args->start)) {
2618
return false;
2719
}
@@ -30,7 +22,7 @@ static bool handle_start(const s_tlv_data *data, s_path_array_context *context)
3022
return true;
3123
}
3224

33-
static bool handle_end(const s_tlv_data *data, s_path_array_context *context) {
25+
static bool handle_end(const tlv_data_t *data, s_path_array_context *context) {
3426
if (data->length != sizeof(context->args->end)) {
3527
return false;
3628
}
@@ -39,24 +31,17 @@ static bool handle_end(const s_tlv_data *data, s_path_array_context *context) {
3931
return true;
4032
}
4133

34+
// clang-format off
35+
#define TLV_TAGS(X) \
36+
X(0x01, TAG_WEIGHT, handle_weight, ALLOW_MULTIPLE_TAG) \
37+
X(0x02, TAG_START, handle_start, ENFORCE_UNIQUE_TAG) \
38+
X(0x03, TAG_END, handle_end, ENFORCE_UNIQUE_TAG)
39+
40+
DEFINE_TLV_PARSER(TLV_TAGS, parse_tlv_array)
41+
4242
bool handle_array_struct(const s_tlv_data *data, s_path_array_context *context) {
43-
bool ret;
44-
45-
switch (data->tag) {
46-
case TAG_WEIGHT:
47-
ret = handle_weight(data, context);
48-
break;
49-
case TAG_START:
50-
ret = handle_start(data, context);
51-
break;
52-
case TAG_END:
53-
ret = handle_end(data, context);
54-
break;
55-
default:
56-
PRINTF(TLV_TAG_ERROR_MSG, data->tag);
57-
ret = false;
58-
}
59-
return ret;
43+
buffer_t payload_buffer = {.ptr = data->value, .size = data->length};
44+
return parse_tlv_array(&payload_buffer, context, NULL);
6045
}
6146

6247
#endif // HAVE_GENERIC_TX_PARSER

src_features/generic_tx_parser/gtp_path_slice.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ static bool handle_end(const s_tlv_data *data, s_path_slice_context *context) {
2828
X(0x01, TAG_START, handle_start, ENFORCE_UNIQUE_TAG) \
2929
X(0x02, TAG_END, handle_end, ENFORCE_UNIQUE_TAG)
3030

31-
DEFINE_TLV_PARSER(TLV_TAGS, parse_tlv_slice_struct)
31+
DEFINE_TLV_PARSER(TLV_TAGS, parse_tlv_slice)
3232

3333
bool handle_slice_struct(const s_tlv_data *data, s_path_slice_context *context) {
3434
buffer_t payload_buffer = {.ptr = data->value, .size = data->length};
35-
return parse_tlv_slice_struct(&payload_buffer, context, NULL);
35+
return parse_tlv_slice(&payload_buffer, context, NULL);
3636
}
3737

3838
#endif // HAVE_GENERIC_TX_PARSER

0 commit comments

Comments
 (0)