-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoldudp64.h
144 lines (122 loc) · 3.59 KB
/
moldudp64.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef MOLDUDP64_H
#define MOLDUDP64_H
/* Copyright (c) 2023, Julia Desmazes. All rights reserved.
*
* This work is licensed under the Creative Commons Attribution-NonCommercial
* 4.0 International License.
*
* This code is provided "as is" without any express or implied warranties. */
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MOLDUDP64_MSG_CNT_MAX 100
typedef struct __attribute__((__packed__)){
uint16_t len;
uint8_t *data;
}moldudp64_msg_s;
typedef struct __attribute__((__packed__)){
uint8_t sid[10];// session id
uint64_t seq; // sequence number
uint16_t cnt; // message count
moldudp64_msg_s *msg[MOLDUDP64_MSG_CNT_MAX]; // messages
} moldudp64_s;
/* allocate a new mold structure */
moldudp64_s * moldudp64_alloc();
/* add new message to existing mold structure */
void moldudp64_add_msg(moldudp64_s *p, void *msg_data, size_t msg_len);
/* free's all alloced messages
* does not destroy structure, can still be reused */
void moldudp64_clear(moldudp64_s *p);
/* free's allocated structure
* cannot be reused */
void moldudp64_free(moldudp64_s *p);
/* flattens the structure into a byte array, applies correct endianness
* to flattened version */
size_t moldudp64_flatten(moldudp64_s *p, uint8_t **flat);
/* set mold session id and sequence number */
void moldudp64_set_ids(moldudp64_s* p,const uint8_t sid[10],const uint64_t seq);
/* print structure content, used for debug */
void moldudp64_print(const moldudp64_s *p);
/* get the session id and sequence number for a given message */
void moldudp64_get_ids(moldudp64_s *p,uint16_t msg_cnt_offset,uint8_t *sid[10],uint64_t *seq);
/* calculate the debug id for a message
* debug id given based on { sid. seq, message offset }
* used for RTL debug */
void moldudp64_get_debug_id(
const uint8_t sid[10],
const uint64_t seq,
const uint16_t msg_cnt_offset,
uint8_t debug_id[18]);
#define malloc_(x) (x *) malloc(sizeof(x))
typedef uint8_t u8;
#ifdef DEBUG
#define info(...) printf(__VA_ARGS__)
#else
#define info(...)
#endif
#endif // MOLDUDP64_H
/*
* Abort if the contents @a and @b,
* two blocks of size @nb differ.
*/
static inline void mcmp_(
const char *a_name,
const char *b_name,
void *a,
void *b,
size_t nb
)
{
uint8_t *x =(uint8_t*) a;
uint8_t *y =(uint8_t*) b;
for (size_t i = 0; i < nb; i++) {
if (x[i] != y[i]) {
printf("mcmp fail : ('%s'[%ld] = '%hhx') != ('%s'[%ld] = '%hhx').\n", a_name, i, x[i], b_name, i, y[i]);
abort();
}
}
}
#define mcmp(a, b, nb) mcmp_(#a, #b, a, b, nb)
static inline void mlog_(
const char *name_a,
void *a,
size_t nb
)
{
uint8_t *x =(uint8_t*) a;
printf("array of '%ld' bytes : '%s(%p)'.\n", nb, name_a, a);
for(size_t i = 0; i < nb; i++) {
uint8_t c = x[i];
printf("- %02ld (%02hhx : %c)\n", i, c, isalpha(c) ? c : ' ');
}
printf("\n");
}
static inline void mlogs_(
const char *name_a,
const char *name_b,
void *a,
void *b,
size_t nb
)
{
uint8_t *x = (uint8_t*)a;
uint8_t *y = (uint8_t*)b;
printf("arrays of '%ld' bytes : '%s(%p)' - '%s(%p)'.\n", nb, name_a, a, name_b, b);
for(size_t i = 0; i < nb; i++) {
uint8_t c0 = x[i];
uint8_t c1 = y[i];
printf("- %02ld (%02hhx : %c) - (%02hhx : %c)\n", i, c0, isalpha(c0) ? c0 : ' ', c1, isalpha(c1) ? c1 : ' ');
}
printf("\n");
}
#ifdef DEBUG
#define mlog(a, nb, ...) printf(__VA_ARGS__); mlog_(#a, a, nb);
#define mlogs(a, b, nb, ...) printf(__VA_ARGS__); mlogs_(#a, #b, a, b, nb);
#define mdbg(a, b, nb, ...) printf(__VA_ARGS__); mlogs_(#a, #b, a, b, nb); mcmp(a, b, nb);
#else
#define mlog(a, nb, ...)
#define mlogs(a, b, nb, ...)
#define mdbg(a, b, nb, ...)
#endif