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, 3 months ago) by misho
Branches: MAIN
CVS tags: elwix5_9, elwix5_8, elwix5_7, elwix5_6, elwix5_5, elwix5_4, elwix5_12, elwix5_11, elwix5_10, HEAD, ELWIX5_9, ELWIX5_8, ELWIX5_7, ELWIX5_6, ELWIX5_5, ELWIX5_4, ELWIX5_3, ELWIX5_11, ELWIX5_10
version 5.3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <elwix.h>


static void
display(array_t *arr)
{
	register int i;

	for (i = 0; i < array_Size(arr); i++) {
		if (AIT_TYPE(array(arr, i, ait_val_t*)) == string)
			printf("arr[%d]=%s (%d) type=%d\n", i, AIT_ADDR(array(arr, i, ait_val_t*)), 
					AIT_LEN(array(arr, i, ait_val_t*)), 
					AIT_TYPE(array(arr, i, ait_val_t*)));
		else if (AIT_TYPE(array(arr, i, ait_val_t*)) == f32) 
			printf("arr[%d]#%f (%d) type=%d\n", i, AIT_GET_F32(array(arr, i, ait_val_t*)), 
					AIT_LEN(array(arr, i, ait_val_t*)), 
					AIT_TYPE(array(arr, i, ait_val_t*)));
		else if (AIT_TYPE(array(arr, i, ait_val_t*)) == f64) 
			printf("arr[%d]#%lf (%d) type=%d\n", i, AIT_GET_F64(array(arr, i, ait_val_t*)), 
					AIT_LEN(array(arr, i, ait_val_t*)), 
					AIT_TYPE(array(arr, i, ait_val_t*)));
		else
			printf("arr[%d]#%ld (%d) type=%d\n", i, AIT_RAW(array(arr, i, ait_val_t*)), 
					AIT_LEN(array(arr, i, ait_val_t*)), 
					AIT_TYPE(array(arr, i, ait_val_t*)));
	}
}

int
main(int argc, char **argv)
{
	array_t *arr;
	u_char buf[BUFSIZ], buf2[BUFSIZ];
	int f, f2, len, len2, ret = 0;
	ait_val_t *v;
	off_t nt;

	arr = ait_array2vars((const char**) argv, 42);
	if (!arr)
		return 1;

	display(arr);

	printf("\nNeeded buffer size = %d\n", ait_vars2tlv(NULL, 0, arr));
	printf("Serialized data to buffer = %d\n", (len = ait_vars2tlv(buf, sizeof buf, arr)));
	printf("Serialized var to buffer = %d\n", (len2 = ait_var2tlv(buf2, sizeof buf2, array(arr, 0, ait_val_t*))));

	f = open("test_tlv.bin", O_CREAT | O_WRONLY, 0644);
	if (f == -1) {
		ret = 2;
		goto err;
	}
	if ((len = write(f, buf, len)) == -1) {
		ret = 3;
		goto err;
	} else
		printf("Wrote %d bytes to file test_tlv.bin\n", len);

	f2 = open("test_var_tlv.bin", O_CREAT | O_WRONLY, 0644);
	if (f2 == -1) {
		ret = 2;
		goto err;
	}
	if ((len2 = write(f2, buf2, len2)) == -1) {
		ret = 3;
		goto err;
	} else
		printf("Wrote %d bytes to file test_var_tlv.bin\n", len2);

	ait_freeVars(&arr);

	arr = ait_tlv2vars(buf, len);
	if (!arr) {
		ret = 4;
		goto err;
	}
	printf("\n>>> Built vars array from TLV buffer\n\n");
	display(arr);

	arr = ait_tlv2vars(buf, sizeof buf);
	if (!arr) {
		ret = 5;
		goto err;
	}
	printf("\n>>> Built vars array from TLV buffer with zeros at the end\n\n");
	display(arr);

	v = ait_tlv2var(buf, sizeof buf, &nt);
	if (!v) {
		ret = 6;
		goto err;
	}
	printf("\nV=%s (%d) type=%d next=%ld\n", AIT_ADDR(v), AIT_LEN(v), AIT_TYPE(v), nt);
	ait_freeVar(&v);

	v = ait_tlv2var(buf + nt, sizeof buf - nt, NULL);
	if (!v) {
		ret = 7;
		goto err;
	}
	printf("\nV=%s (%d) type=%d\n", AIT_ADDR(v), AIT_LEN(v), AIT_TYPE(v));
	ait_freeVar(&v);

err:
	if (f2 > 2)
		close(f2);
	if (f > 2)
		close(f);
	ait_freeVars(&arr);
	return ret;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>