File:
[ELWIX - Embedded LightWeight unIX -] /
libelwix /
example /
test_tlv.c
Revision
1.4:
download - view:
text,
annotated -
select for diffs -
revision graph
Tue Feb 1 20:46:57 2022 UTC (2 years, 6 months ago) by
misho
Branches:
MAIN
CVS tags:
elwix6_1,
elwix5_9,
elwix5_8,
elwix5_7,
elwix5_6,
elwix5_5,
elwix5_4,
elwix5_12,
elwix5_11,
elwix5_10,
HEAD,
ELWIX6_0,
ELWIX5_9,
ELWIX5_8,
ELWIX5_7,
ELWIX5_6,
ELWIX5_5,
ELWIX5_4,
ELWIX5_3,
ELWIX5_11,
ELWIX5_10
version 5.3
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <string.h>
4: #include <unistd.h>
5: #include <fcntl.h>
6: #include <elwix.h>
7:
8:
9: static void
10: display(array_t *arr)
11: {
12: register int i;
13:
14: for (i = 0; i < array_Size(arr); i++) {
15: if (AIT_TYPE(array(arr, i, ait_val_t*)) == string)
16: printf("arr[%d]=%s (%d) type=%d\n", i, AIT_ADDR(array(arr, i, ait_val_t*)),
17: AIT_LEN(array(arr, i, ait_val_t*)),
18: AIT_TYPE(array(arr, i, ait_val_t*)));
19: else if (AIT_TYPE(array(arr, i, ait_val_t*)) == f32)
20: printf("arr[%d]#%f (%d) type=%d\n", i, AIT_GET_F32(array(arr, i, ait_val_t*)),
21: AIT_LEN(array(arr, i, ait_val_t*)),
22: AIT_TYPE(array(arr, i, ait_val_t*)));
23: else if (AIT_TYPE(array(arr, i, ait_val_t*)) == f64)
24: printf("arr[%d]#%lf (%d) type=%d\n", i, AIT_GET_F64(array(arr, i, ait_val_t*)),
25: AIT_LEN(array(arr, i, ait_val_t*)),
26: AIT_TYPE(array(arr, i, ait_val_t*)));
27: else
28: printf("arr[%d]#%ld (%d) type=%d\n", i, AIT_RAW(array(arr, i, ait_val_t*)),
29: AIT_LEN(array(arr, i, ait_val_t*)),
30: AIT_TYPE(array(arr, i, ait_val_t*)));
31: }
32: }
33:
34: int
35: main(int argc, char **argv)
36: {
37: array_t *arr;
38: u_char buf[BUFSIZ], buf2[BUFSIZ];
39: int f, f2, len, len2, ret = 0;
40: ait_val_t *v;
41: off_t nt;
42:
43: arr = ait_array2vars((const char**) argv, 42);
44: if (!arr)
45: return 1;
46:
47: display(arr);
48:
49: printf("\nNeeded buffer size = %d\n", ait_vars2tlv(NULL, 0, arr));
50: printf("Serialized data to buffer = %d\n", (len = ait_vars2tlv(buf, sizeof buf, arr)));
51: printf("Serialized var to buffer = %d\n", (len2 = ait_var2tlv(buf2, sizeof buf2, array(arr, 0, ait_val_t*))));
52:
53: f = open("test_tlv.bin", O_CREAT | O_WRONLY, 0644);
54: if (f == -1) {
55: ret = 2;
56: goto err;
57: }
58: if ((len = write(f, buf, len)) == -1) {
59: ret = 3;
60: goto err;
61: } else
62: printf("Wrote %d bytes to file test_tlv.bin\n", len);
63:
64: f2 = open("test_var_tlv.bin", O_CREAT | O_WRONLY, 0644);
65: if (f2 == -1) {
66: ret = 2;
67: goto err;
68: }
69: if ((len2 = write(f2, buf2, len2)) == -1) {
70: ret = 3;
71: goto err;
72: } else
73: printf("Wrote %d bytes to file test_var_tlv.bin\n", len2);
74:
75: ait_freeVars(&arr);
76:
77: arr = ait_tlv2vars(buf, len);
78: if (!arr) {
79: ret = 4;
80: goto err;
81: }
82: printf("\n>>> Built vars array from TLV buffer\n\n");
83: display(arr);
84:
85: arr = ait_tlv2vars(buf, sizeof buf);
86: if (!arr) {
87: ret = 5;
88: goto err;
89: }
90: printf("\n>>> Built vars array from TLV buffer with zeros at the end\n\n");
91: display(arr);
92:
93: v = ait_tlv2var(buf, sizeof buf, &nt);
94: if (!v) {
95: ret = 6;
96: goto err;
97: }
98: printf("\nV=%s (%d) type=%d next=%ld\n", AIT_ADDR(v), AIT_LEN(v), AIT_TYPE(v), nt);
99: ait_freeVar(&v);
100:
101: v = ait_tlv2var(buf + nt, sizeof buf - nt, NULL);
102: if (!v) {
103: ret = 7;
104: goto err;
105: }
106: printf("\nV=%s (%d) type=%d\n", AIT_ADDR(v), AIT_LEN(v), AIT_TYPE(v));
107: ait_freeVar(&v);
108:
109: err:
110: if (f2 > 2)
111: close(f2);
112: if (f > 2)
113: close(f);
114: ait_freeVars(&arr);
115: return ret;
116: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>