first cbor test programs added
parent
74d7a87738
commit
3a95a6ecea
|
@ -3,3 +3,4 @@
|
|||
*.swp
|
||||
*.out
|
||||
*.o
|
||||
*.bin
|
||||
|
|
|
@ -24,6 +24,13 @@ First of all, the application will send a message to the service's pipe in **pla
|
|||
In order to communicate between the application and the service, we use the [CBOR format (RFC 7049)][cbor].
|
||||
This will be used with some conventions.
|
||||
|
||||
## CBOR install, programming, debug
|
||||
|
||||
[libcbor][libcbor] is used in the provided implementations.
|
||||
It is an [extensively documented][libcbor-doc] library, easy to install and to work with.
|
||||
|
||||
We also strongly encourage the use of the [cbor-diag][cbor-diag] to ensure that you send and receive correctly formatted messages.
|
||||
|
||||
## overview
|
||||
|
||||
The format will be "type : value".
|
||||
|
@ -40,5 +47,10 @@ The type will be a simple byte :
|
|||
|
||||
index | abbreviation | semantic
|
||||
0 | close | to close the communication between the application and the service
|
||||
1 | message | to send data
|
||||
2 | error | to send an error message
|
||||
|
||||
[cbor]: https://tools.ietf.org/html/rfc7049
|
||||
[cbor-diag]: https://github.com/cabo/cbor-diag
|
||||
[libcbor]: https://github.com/PJK/libcbor
|
||||
[libcbor-doc]: https://github.com/PJK/libcbor
|
||||
|
|
|
@ -10,7 +10,7 @@ TESTS=$(addsuffix .test, $(EXEC))
|
|||
all: $(SOURCES) $(EXEC)
|
||||
|
||||
$(EXEC): $(OBJECTS) $(CFILES)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
@ -19,4 +19,4 @@ clean:
|
|||
-rm $(OBJECTS)
|
||||
|
||||
mrproper: clean
|
||||
rm $(EXEC)
|
||||
rm *.bin
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
CC=gcc
|
||||
CFLAGS=-Wall -g -Wextra
|
||||
LDFLAGS= -pthread -lcbor
|
||||
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
|
||||
EXEC=$(basename $(wildcard *.c))
|
||||
SOURCES=$(wildcard ../lib/*.c)
|
||||
OBJECTS=$(SOURCES:.c=.o)
|
||||
TESTS=$(addsuffix .test, $(EXEC))
|
||||
|
||||
all: $(SOURCES) $(EXEC)
|
||||
|
||||
$(EXEC): $(OBJECTS) $(CFILES)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
mrproper:
|
||||
rm *.bin
|
|
@ -0,0 +1,36 @@
|
|||
#include "cbor.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* Reads data from a file. Example usage:
|
||||
* $ ./examples/readfile examples/data/nested_array.cbor
|
||||
*/
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf (stderr, "usage: %s file\n", argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
FILE * f = fopen(argv[1], "rb");
|
||||
fseek(f, 0, SEEK_END);
|
||||
size_t length = (size_t)ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
unsigned char * buffer = malloc(length);
|
||||
fread(buffer, length, 1, f);
|
||||
|
||||
/* Assuming `buffer` contains `info.st_size` bytes of input data */
|
||||
struct cbor_load_result result;
|
||||
cbor_item_t * item = cbor_load(buffer, length, &result);
|
||||
/* Pretty-print the result */
|
||||
cbor_describe(item, stdout);
|
||||
fflush(stdout);
|
||||
/* Deallocate the result */
|
||||
cbor_decref(&item);
|
||||
|
||||
fclose(f);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include "cbor.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
/* Preallocate the map structure */
|
||||
cbor_item_t * root = cbor_new_definite_map(2);
|
||||
/* Add the content */
|
||||
cbor_map_add(root, (struct cbor_pair) {
|
||||
.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
|
||||
.value = cbor_move(cbor_build_bool(true))
|
||||
});
|
||||
cbor_map_add(root, (struct cbor_pair) {
|
||||
.key = cbor_move(cbor_build_uint8(42)),
|
||||
.value = cbor_move(cbor_build_string("Is the answer"))
|
||||
});
|
||||
/* Output: `length` bytes of data in the `buffer` */
|
||||
unsigned char * buffer;
|
||||
size_t buffer_size, length = cbor_serialize_alloc(root, &buffer, &buffer_size);
|
||||
|
||||
fwrite(buffer, 1, length, stdout);
|
||||
free(buffer);
|
||||
|
||||
fflush(stdout);
|
||||
cbor_decref(&root);
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include "cbor.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* * Illustrates how one might skim through a map (which is assumed to have
|
||||
* * string keys and values only), looking for the value of a specific key
|
||||
* *
|
||||
* * Use the examples/data/map.cbor input to test this.
|
||||
* */
|
||||
|
||||
const char * key = "a secret key";
|
||||
bool key_found = false;
|
||||
|
||||
void find_string(void * _ctx, cbor_data buffer, size_t len)
|
||||
{
|
||||
(void) _ctx;
|
||||
if (key_found) {
|
||||
printf("Found the value: %*s\n", (int) len, buffer);
|
||||
key_found = false;
|
||||
} else if (len == strlen(key)) {
|
||||
key_found = (memcmp(key, buffer, len) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
FILE * f = fopen(argv[1], "rb");
|
||||
fseek(f, 0, SEEK_END);
|
||||
size_t length = (size_t)ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
unsigned char * buffer = malloc(length);
|
||||
fread(buffer, length, 1, f);
|
||||
|
||||
struct cbor_callbacks callbacks = cbor_empty_callbacks;
|
||||
struct cbor_decoder_result decode_result;
|
||||
size_t bytes_read = 0;
|
||||
callbacks.string = find_string;
|
||||
while (bytes_read < length) {
|
||||
decode_result = cbor_stream_decode(buffer + bytes_read,
|
||||
length - bytes_read,
|
||||
&callbacks, NULL);
|
||||
bytes_read += decode_result.read;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
|
@ -13,6 +13,9 @@ struct node {
|
|||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
// the list
|
||||
struct mlist *list;
|
||||
list = malloc (sizeof(struct mlist));
|
||||
|
|
37
misc/msg.c
37
misc/msg.c
|
@ -1,7 +1,12 @@
|
|||
#include "../lib/pubsubd.h"
|
||||
#include "cbor.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PKT_CLOSE 0
|
||||
#define PKT_MSG 1
|
||||
#define PKT_ERROR 2
|
||||
|
||||
void
|
||||
ohshit(int rvalue, const char* str) {
|
||||
fprintf (stderr, "\033[31merr: %s\033[00m\n", str);
|
||||
|
@ -16,16 +21,6 @@ void usage (char **argv)
|
|||
printf ( " This sends a CBOR msg [ 1, \"data\" ]\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* implemented types:
|
||||
* bstr_t (default)
|
||||
* tstr_t
|
||||
* int_t
|
||||
*
|
||||
* future types:
|
||||
* nint_t
|
||||
*/
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
|
@ -34,5 +29,27 @@ main(int argc, char **argv)
|
|||
exit (1);
|
||||
}
|
||||
|
||||
unsigned char buf[BUFSIZ];
|
||||
memset (buf, 0, BUFSIZ);
|
||||
|
||||
ssize_t buflen = read (0, buf, BUFSIZ);
|
||||
|
||||
/* Preallocate the map structure */
|
||||
cbor_item_t * root = cbor_new_definite_map(1);
|
||||
/* Add the content */
|
||||
cbor_map_add(root, (struct cbor_pair) {
|
||||
.key = cbor_move(cbor_build_uint8(PKT_MSG)),
|
||||
.value = cbor_move(cbor_build_bytestring(buf, buflen))
|
||||
});
|
||||
/* Output: `length` bytes of data in the `buffer` */
|
||||
unsigned char * buffer;
|
||||
size_t buffer_size, length = cbor_serialize_alloc (root, &buffer, &buffer_size);
|
||||
|
||||
fwrite(buffer, 1, length, stdout);
|
||||
free(buffer);
|
||||
|
||||
fflush(stdout);
|
||||
cbor_decref(&root);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,10 @@
|
|||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
char *fifopathin = "/tmp/123000-1-in";
|
||||
size_t msize = 100;
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
char *fifopathin = "/tmp/123000-1-in";
|
||||
size_t msize;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
CC=gcc
|
||||
CFLAGS=-Wall -g
|
||||
LDFLAGS=
|
||||
LDFLAGS= -pthread
|
||||
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
|
||||
EXEC=$(basename $(wildcard *.c))
|
||||
SOURCES=$(wildcard ../lib/*.c)
|
||||
|
@ -10,13 +10,13 @@ TESTS=$(addsuffix .test, $(EXEC))
|
|||
all: $(SOURCES) $(EXEC)
|
||||
|
||||
$(EXEC): $(OBJECTS) $(CFILES)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
-rm $(OBJECTS)
|
||||
@-rm $(OBJECTS)
|
||||
|
||||
mrproper: clean
|
||||
rm $(EXEC)
|
||||
@-rm *.bin
|
||||
|
|
|
@ -10,7 +10,7 @@ TESTS=$(addsuffix .test, $(EXEC))
|
|||
all: $(SOURCES) $(EXEC)
|
||||
|
||||
$(EXEC): $(OBJECTS) $(CFILES)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
@ -22,4 +22,4 @@ clean:
|
|||
@-rm $(OBJECTS)
|
||||
|
||||
mrproper: clean
|
||||
@-rm $(EXEC)
|
||||
@-rm *.bin
|
||||
|
|
Reference in New Issue