From 37a1d948f3a68610a211e114ae0ec3d6bc246a55 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 22 Nov 2017 20:29:00 +0000 Subject: [PATCH] why the hell not --- examples/Makefile | 2 +- examples/map.c | 25 +++++++++++ examples/map.c-e | 25 +++++++++++ examples/music.c | 13 +++--- examples/music.c-e | 13 +++--- include/skipstone/map.h | 24 +++++++++++ include/skipstone/skipstone.h | 26 ++++++++++-- src/Makefile | 4 +- src/map.c | 79 +++++++++++++++++++++++++++++++++++ src/map.c-e | 79 +++++++++++++++++++++++++++++++++++ src/skipstone.c | 66 ++++++++++++++++------------- src/skipstone.c-e | 66 ++++++++++++++++------------- 12 files changed, 349 insertions(+), 73 deletions(-) create mode 100644 examples/map.c create mode 100644 examples/map.c-e create mode 100644 include/skipstone/map.h create mode 100644 src/map.c create mode 100644 src/map.c-e diff --git a/examples/Makefile b/examples/Makefile index 2e3887b..4292cd6 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -7,7 +7,7 @@ INCLUDE_PATH = ../include CFLAGS += -I$(INCLUDE_PATH) LDFLAGS = -L../src -lskipstone -EXAMPLES = read music +EXAMPLES = read music map RM = /bin/rm diff --git a/examples/map.c b/examples/map.c new file mode 100644 index 0000000..57f63ae --- /dev/null +++ b/examples/map.c @@ -0,0 +1,25 @@ +#include +#include +#include + +#include + +int main(int argc, char **argv) { + skipstone_map *map; + + if ((map = skipstone_map_new()) == NULL) { + perror("skipstone_map_new()"); + + return 1; + } + + skipstone_map_set(map, 0xdead, 0xdeadbeef); + skipstone_map_set(map, 0xbeef, 0xcafebabe); + + printf("%p\n", skipstone_map_get(map, 0xdead)); + printf("%p\n", skipstone_map_get(map, 0xbeef)); + + skipstone_map_destroy(map); + + return 0; +} diff --git a/examples/map.c-e b/examples/map.c-e new file mode 100644 index 0000000..57f63ae --- /dev/null +++ b/examples/map.c-e @@ -0,0 +1,25 @@ +#include +#include +#include + +#include + +int main(int argc, char **argv) { + skipstone_map *map; + + if ((map = skipstone_map_new()) == NULL) { + perror("skipstone_map_new()"); + + return 1; + } + + skipstone_map_set(map, 0xdead, 0xdeadbeef); + skipstone_map_set(map, 0xbeef, 0xcafebabe); + + printf("%p\n", skipstone_map_get(map, 0xdead)); + printf("%p\n", skipstone_map_get(map, 0xbeef)); + + skipstone_map_destroy(map); + + return 0; +} diff --git a/examples/music.c b/examples/music.c index 884918a..3c8d793 100644 --- a/examples/music.c +++ b/examples/music.c @@ -34,7 +34,10 @@ int main(int argc, char **argv) { uint8_t title_len; char title[5]; } play_track = { - 0x10, 5, "KMFDM", 5, "Nihil", 5, "Ultra" + 0x10, + 5, { 'K', 'M', 'F', 'D', 'M' }, + 5, { 'N', 'i', 'h', 'i', 'l' }, + 5, { 'U', 'l', 't', 'r', 'a' } }; struct { @@ -44,7 +47,9 @@ int main(int argc, char **argv) { uint8_t name_len; char name[8]; } play_info = { - 0x13, 8, "DeaDBeeF", 8, "RealDead" + 0x13, + 8, { 'D', 'e', 'a', 'D', 'B', 'e', 'e', 'F' }, + 8, { 'R', 'e', 'a', 'L', 'D', 'e', 'a', 'D' } }; if (argc != 2) { @@ -63,9 +68,7 @@ int main(int argc, char **argv) { printf("Received message %hu bytes for endpoint %hu\n", len, endpoint); - if (skipstone_send(link, &play_track, sizeof(play_track), 0x20) < 0) { - printf("Balls\n"); - } + skipstone_send(link, &play_track, sizeof(play_track), 0x20); } perror("skipstone_recv()"); diff --git a/examples/music.c-e b/examples/music.c-e index 884918a..3c8d793 100644 --- a/examples/music.c-e +++ b/examples/music.c-e @@ -34,7 +34,10 @@ int main(int argc, char **argv) { uint8_t title_len; char title[5]; } play_track = { - 0x10, 5, "KMFDM", 5, "Nihil", 5, "Ultra" + 0x10, + 5, { 'K', 'M', 'F', 'D', 'M' }, + 5, { 'N', 'i', 'h', 'i', 'l' }, + 5, { 'U', 'l', 't', 'r', 'a' } }; struct { @@ -44,7 +47,9 @@ int main(int argc, char **argv) { uint8_t name_len; char name[8]; } play_info = { - 0x13, 8, "DeaDBeeF", 8, "RealDead" + 0x13, + 8, { 'D', 'e', 'a', 'D', 'B', 'e', 'e', 'F' }, + 8, { 'R', 'e', 'a', 'L', 'D', 'e', 'a', 'D' } }; if (argc != 2) { @@ -63,9 +68,7 @@ int main(int argc, char **argv) { printf("Received message %hu bytes for endpoint %hu\n", len, endpoint); - if (skipstone_send(link, &play_track, sizeof(play_track), 0x20) < 0) { - printf("Balls\n"); - } + skipstone_send(link, &play_track, sizeof(play_track), 0x20); } perror("skipstone_recv()"); diff --git a/include/skipstone/map.h b/include/skipstone/map.h new file mode 100644 index 0000000..cba2b85 --- /dev/null +++ b/include/skipstone/map.h @@ -0,0 +1,24 @@ +#ifndef _SKIPSTONE_MAP_H +#define _SKIPSTONE_MAP_H + +#include + +#define SKIPSTONE_MAP_SLOTS 16 +#define SKIPSTONE_MAP_DEPTH 3 + +typedef struct _skipstone_map { + union { + void *value; + struct _skipstone_map *next; + }; +} skipstone_map; + +skipstone_map *skipstone_map_new(); + +void skipstone_map_destroy(skipstone_map *map); + +void *skipstone_map_get(skipstone_map *map, uint16_t index); + +void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value); + +#endif /* _SKIPSTONE_MAP_H */ diff --git a/include/skipstone/skipstone.h b/include/skipstone/skipstone.h index 642e3af..a33fefd 100644 --- a/include/skipstone/skipstone.h +++ b/include/skipstone/skipstone.h @@ -14,18 +14,36 @@ enum skipstone_message_endpoint { SKIPSTONE_MESSAGE_ENDPOINT_PHONE_VERSION = 17, SKIPSTONE_MESSAGE_ENDPOINT_SYSTEM_MESSAGE = 18, SKIPSTONE_MESSAGE_ENDPOINT_MUSIC_CONTROL = 32, - SKIPSTONE_MESSAGE_ENDPOINT_PHONE_CONTROL = 33, - + SKIPSTONE_MESSAGE_ENDPOINT_PHONE_CONTROL = 33 }; +typedef struct _skipstone_message_endpoint skipstone_message_endpoint; + typedef struct _skipstone_watch_link skipstone_watch_link; +typedef struct _skipstone_watch skipstone_watch; + +typedef int (*skipstone_message_handler)(skipstone_watch *watch, + void *buf, uint16_t size, uint16_t endpoint); + skipstone_watch_link *skipstone_watch_link_open_serial(const char *device); int skipstone_watch_link_close(skipstone_watch_link *link); -int skipstone_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint); +skipstone_watch *skipstone_watch_new(skipstone_watch_link *link); -int skipstone_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint); +void skipstone_watch_destroy(skipstone_watch *watch); + +int skipstone_watch_main(); + +void skipstone_watch_register_message_handler(skipstone_watch *watch, + uint16_t endpoint, skipstone_message_handler *handler, void *context); + +void skipstone_watch_deregister_message_handler(skipstone_watch *watch, + uint16_t endpoint); + +int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint); + +int skipstone_watch_link_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint); #endif /* _SKIPSTONE_H */ diff --git a/src/Makefile b/src/Makefile index 2cb2a06..2b673ce 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,9 +7,9 @@ CC = $(CROSS)cc CFLAGS += -fPIC -I$(INCLUDE_PATH) LDFLAGS = -HEADERS = skipstone.h +HEADERS = skipstone.h map.h -OBJS = skipstone.o +OBJS = skipstone.o map.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/map.c b/src/map.c new file mode 100644 index 0000000..f005250 --- /dev/null +++ b/src/map.c @@ -0,0 +1,79 @@ +#include +#include + +#include + +skipstone_map *skipstone_map_new() { + skipstone_map *map; + + if ((map = malloc(SKIPSTONE_MAP_SLOTS * sizeof(*map))) == NULL) { + goto error_malloc_map; + } + + memset(map, 0, SKIPSTONE_MAP_SLOTS * sizeof(*map)); + + return map; + +error_malloc_map: + return NULL; +} + +static void _destroy(skipstone_map *map, int depth) { + uint16_t i; + + if (depth > SKIPSTONE_MAP_DEPTH) { + return; + } + + for (i=0; i>= 4; + } + + return map[index & 0x0f].value; +} + +void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value) { + uint16_t i; + + for (i=0; i>= 4; + } + + return map[index & 0x0f].value = value; + +error_new: + return NULL; +} diff --git a/src/map.c-e b/src/map.c-e new file mode 100644 index 0000000..f005250 --- /dev/null +++ b/src/map.c-e @@ -0,0 +1,79 @@ +#include +#include + +#include + +skipstone_map *skipstone_map_new() { + skipstone_map *map; + + if ((map = malloc(SKIPSTONE_MAP_SLOTS * sizeof(*map))) == NULL) { + goto error_malloc_map; + } + + memset(map, 0, SKIPSTONE_MAP_SLOTS * sizeof(*map)); + + return map; + +error_malloc_map: + return NULL; +} + +static void _destroy(skipstone_map *map, int depth) { + uint16_t i; + + if (depth > SKIPSTONE_MAP_DEPTH) { + return; + } + + for (i=0; i>= 4; + } + + return map[index & 0x0f].value; +} + +void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value) { + uint16_t i; + + for (i=0; i>= 4; + } + + return map[index & 0x0f].value = value; + +error_new: + return NULL; +} diff --git a/src/skipstone.c b/src/skipstone.c index a089746..c3a87c2 100644 --- a/src/skipstone.c +++ b/src/skipstone.c @@ -8,11 +8,18 @@ #include #include +#include enum skipstone_watch_link_type { SKIPSTONE_WATCH_LINK_SERIAL = 1 }; +struct _skipstone_message_endpoint { + uint16_t id; + skipstone_message_handler *handler; + void *context; +}; + struct _skipstone_watch_link { enum skipstone_watch_link_type type; @@ -26,6 +33,11 @@ struct _skipstone_watch_link { }; }; +struct _skipstone_watch { + skipstone_watch_link *link; + skipstone_map *endpoints; +}; + typedef struct _skipstone_message_header { uint16_t size, endpoint; @@ -102,7 +114,7 @@ int skipstone_watch_link_close(skipstone_watch_link *link) { return -1; } -int skipstone_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { +int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { skipstone_message_header header; ssize_t wrlen; @@ -137,38 +149,36 @@ error_toobig: return -1; } -int skipstone_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { +int skipstone_watch_link_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { skipstone_message_header header; - while (1) { - ssize_t len_read; - size_t len_wanted, offset = 0; + ssize_t len_read; + size_t len_wanted, offset = 0; - if (read(link->serial.fd, &header, sizeof(header)) < 0) { - goto error_io; - } - - len_wanted = (size_t)be16toh(header.size); - - if (len_wanted > SKIPSTONE_MESSAGE_MAX_PAYLOAD) { - goto error_io; - } - - while (len_wanted) { - if ((len_read = read(link->serial.fd, (uint8_t *)buf + offset, len_wanted)) < 0) { - goto error_io; - } - - len_wanted -= len_read; - offset += len_read; - } - - *size = be16toh(header.size); - *endpoint = be16toh(header.endpoint); - - return 0; + if (read(link->serial.fd, &header, sizeof(header)) < 0) { + goto error_io; } + len_wanted = (size_t)be16toh(header.size); + + if (len_wanted > SKIPSTONE_MESSAGE_MAX_PAYLOAD) { + goto error_io; + } + + while (len_wanted) { + if ((len_read = read(link->serial.fd, (uint8_t *)buf + offset, len_wanted)) < 0) { + goto error_io; + } + + len_wanted -= len_read; + offset += len_read; + } + + *size = be16toh(header.size); + *endpoint = be16toh(header.endpoint); + + return 0; + error_io: return -1; } diff --git a/src/skipstone.c-e b/src/skipstone.c-e index a089746..c3a87c2 100644 --- a/src/skipstone.c-e +++ b/src/skipstone.c-e @@ -8,11 +8,18 @@ #include #include +#include enum skipstone_watch_link_type { SKIPSTONE_WATCH_LINK_SERIAL = 1 }; +struct _skipstone_message_endpoint { + uint16_t id; + skipstone_message_handler *handler; + void *context; +}; + struct _skipstone_watch_link { enum skipstone_watch_link_type type; @@ -26,6 +33,11 @@ struct _skipstone_watch_link { }; }; +struct _skipstone_watch { + skipstone_watch_link *link; + skipstone_map *endpoints; +}; + typedef struct _skipstone_message_header { uint16_t size, endpoint; @@ -102,7 +114,7 @@ int skipstone_watch_link_close(skipstone_watch_link *link) { return -1; } -int skipstone_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { +int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { skipstone_message_header header; ssize_t wrlen; @@ -137,38 +149,36 @@ error_toobig: return -1; } -int skipstone_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { +int skipstone_watch_link_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { skipstone_message_header header; - while (1) { - ssize_t len_read; - size_t len_wanted, offset = 0; + ssize_t len_read; + size_t len_wanted, offset = 0; - if (read(link->serial.fd, &header, sizeof(header)) < 0) { - goto error_io; - } - - len_wanted = (size_t)be16toh(header.size); - - if (len_wanted > SKIPSTONE_MESSAGE_MAX_PAYLOAD) { - goto error_io; - } - - while (len_wanted) { - if ((len_read = read(link->serial.fd, (uint8_t *)buf + offset, len_wanted)) < 0) { - goto error_io; - } - - len_wanted -= len_read; - offset += len_read; - } - - *size = be16toh(header.size); - *endpoint = be16toh(header.endpoint); - - return 0; + if (read(link->serial.fd, &header, sizeof(header)) < 0) { + goto error_io; } + len_wanted = (size_t)be16toh(header.size); + + if (len_wanted > SKIPSTONE_MESSAGE_MAX_PAYLOAD) { + goto error_io; + } + + while (len_wanted) { + if ((len_read = read(link->serial.fd, (uint8_t *)buf + offset, len_wanted)) < 0) { + goto error_io; + } + + len_wanted -= len_read; + offset += len_read; + } + + *size = be16toh(header.size); + *endpoint = be16toh(header.endpoint); + + return 0; + error_io: return -1; }