diff --git a/examples/map.c b/examples/map.c index 57f63ae..1d1813a 100644 --- a/examples/map.c +++ b/examples/map.c @@ -13,8 +13,8 @@ int main(int argc, char **argv) { return 1; } - skipstone_map_set(map, 0xdead, 0xdeadbeef); - skipstone_map_set(map, 0xbeef, 0xcafebabe); + skipstone_map_set(map, 0xdead, (void *)0xdeadbeef); + skipstone_map_set(map, 0xbeef, (void *)0xcafebabe); printf("%p\n", skipstone_map_get(map, 0xdead)); printf("%p\n", skipstone_map_get(map, 0xbeef)); diff --git a/examples/map.c-e b/examples/map.c-e index 57f63ae..1d1813a 100644 --- a/examples/map.c-e +++ b/examples/map.c-e @@ -13,8 +13,8 @@ int main(int argc, char **argv) { return 1; } - skipstone_map_set(map, 0xdead, 0xdeadbeef); - skipstone_map_set(map, 0xbeef, 0xcafebabe); + skipstone_map_set(map, 0xdead, (void *)0xdeadbeef); + skipstone_map_set(map, 0xbeef, (void *)0xcafebabe); printf("%p\n", skipstone_map_get(map, 0xdead)); printf("%p\n", skipstone_map_get(map, 0xbeef)); diff --git a/examples/music.c b/examples/music.c index 3c8d793..5ef4d5a 100644 --- a/examples/music.c +++ b/examples/music.c @@ -2,7 +2,8 @@ #include #include -#include +#include +#include static void usage(int argc, char **argv) { fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); @@ -10,7 +11,7 @@ static void usage(int argc, char **argv) { } int main(int argc, char **argv) { - skipstone_watch_link *link; + skipstone_link *link; void *buf; uint16_t len, endpoint; @@ -60,20 +61,20 @@ int main(int argc, char **argv) { goto error_malloc_buf; } - if ((link = skipstone_watch_link_open_serial(argv[1])) == NULL) { + if ((link = skipstone_link_open_serial(argv[1])) == NULL) { goto error_watch_link_open; } - while (skipstone_recv(link, buf, &len, &endpoint) >= 0) { + while (skipstone_link_recv(link, buf, &len, &endpoint) >= 0) { printf("Received message %hu bytes for endpoint %hu\n", len, endpoint); - skipstone_send(link, &play_track, sizeof(play_track), 0x20); + skipstone_link_send(link, &play_track, sizeof(play_track), 0x20); } - perror("skipstone_recv()"); + perror("skipstone_link_recv()"); - skipstone_watch_link_close(link); + skipstone_link_close(link); return 0; diff --git a/examples/music.c-e b/examples/music.c-e index 3c8d793..5ef4d5a 100644 --- a/examples/music.c-e +++ b/examples/music.c-e @@ -2,7 +2,8 @@ #include #include -#include +#include +#include static void usage(int argc, char **argv) { fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); @@ -10,7 +11,7 @@ static void usage(int argc, char **argv) { } int main(int argc, char **argv) { - skipstone_watch_link *link; + skipstone_link *link; void *buf; uint16_t len, endpoint; @@ -60,20 +61,20 @@ int main(int argc, char **argv) { goto error_malloc_buf; } - if ((link = skipstone_watch_link_open_serial(argv[1])) == NULL) { + if ((link = skipstone_link_open_serial(argv[1])) == NULL) { goto error_watch_link_open; } - while (skipstone_recv(link, buf, &len, &endpoint) >= 0) { + while (skipstone_link_recv(link, buf, &len, &endpoint) >= 0) { printf("Received message %hu bytes for endpoint %hu\n", len, endpoint); - skipstone_send(link, &play_track, sizeof(play_track), 0x20); + skipstone_link_send(link, &play_track, sizeof(play_track), 0x20); } - perror("skipstone_recv()"); + perror("skipstone_link_recv()"); - skipstone_watch_link_close(link); + skipstone_link_close(link); return 0; diff --git a/examples/read.c b/examples/read.c index 207d23e..e5ef18e 100644 --- a/examples/read.c +++ b/examples/read.c @@ -2,7 +2,8 @@ #include #include -#include +#include +#include static void usage(int argc, char **argv) { fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); @@ -10,7 +11,7 @@ static void usage(int argc, char **argv) { } int main(int argc, char **argv) { - skipstone_watch_link *link; + skipstone_link *link; void *buf; uint16_t len, endpoint; @@ -22,22 +23,22 @@ int main(int argc, char **argv) { goto error_malloc_buf; } - if ((link = skipstone_watch_link_open_serial(argv[1])) == NULL) { - goto error_watch_link_open; + if ((link = skipstone_link_open_serial(argv[1])) == NULL) { + goto error_link_open; } - while (skipstone_recv(link, buf, &len, &endpoint) >= 0) { + while (skipstone_link_recv(link, buf, &len, &endpoint) >= 0) { printf("Received message %hu bytes for endpoint %hu\n", len, endpoint); } - perror("skipstone_recv()"); + perror("skipstone_link_recv()"); - skipstone_watch_link_close(link); + skipstone_link_close(link); return 0; -error_watch_link_open: +error_link_open: free(buf); error_malloc_buf: diff --git a/examples/read.c-e b/examples/read.c-e index 207d23e..e5ef18e 100644 --- a/examples/read.c-e +++ b/examples/read.c-e @@ -2,7 +2,8 @@ #include #include -#include +#include +#include static void usage(int argc, char **argv) { fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); @@ -10,7 +11,7 @@ static void usage(int argc, char **argv) { } int main(int argc, char **argv) { - skipstone_watch_link *link; + skipstone_link *link; void *buf; uint16_t len, endpoint; @@ -22,22 +23,22 @@ int main(int argc, char **argv) { goto error_malloc_buf; } - if ((link = skipstone_watch_link_open_serial(argv[1])) == NULL) { - goto error_watch_link_open; + if ((link = skipstone_link_open_serial(argv[1])) == NULL) { + goto error_link_open; } - while (skipstone_recv(link, buf, &len, &endpoint) >= 0) { + while (skipstone_link_recv(link, buf, &len, &endpoint) >= 0) { printf("Received message %hu bytes for endpoint %hu\n", len, endpoint); } - perror("skipstone_recv()"); + perror("skipstone_link_recv()"); - skipstone_watch_link_close(link); + skipstone_link_close(link); return 0; -error_watch_link_open: +error_link_open: free(buf); error_malloc_buf: diff --git a/include/skipstone/link.h b/include/skipstone/link.h new file mode 100644 index 0000000..5361749 --- /dev/null +++ b/include/skipstone/link.h @@ -0,0 +1,18 @@ +#ifndef _SKIPSTONE_LINK_H +#define _SKIPSTONE_LINK_H + +#include + +typedef struct _skipstone_link skipstone_link; + +skipstone_link *skipstone_link_open_serial(const char *device); + +int skipstone_link_close(skipstone_link *link); + +int skipstone_link_send(skipstone_link *link, + void *buf, uint16_t size, uint16_t endpoint); + +int skipstone_link_recv(skipstone_link *link, + void *buf, uint16_t *size, uint16_t *endpoint); + +#endif /* _SKIPSTONE_LINK_H */ diff --git a/include/skipstone/map.h b/include/skipstone/map.h index cba2b85..4b44577 100644 --- a/include/skipstone/map.h +++ b/include/skipstone/map.h @@ -19,6 +19,6 @@ 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); +int skipstone_map_set(skipstone_map *map, uint16_t index, void *value); #endif /* _SKIPSTONE_MAP_H */ diff --git a/include/skipstone/message.h b/include/skipstone/message.h new file mode 100644 index 0000000..261c0d0 --- /dev/null +++ b/include/skipstone/message.h @@ -0,0 +1,38 @@ +#ifndef _SKIPSTONE_MESSAGE_H +#define _SKIPSTONE_MESSAGE_H + +#include + +#include +#include + +#define SKIPSTONE_MESSAGE_MAX_PAYLOAD 4096 + +enum { + SKIPSTONE_MESSAGE_ENDPOINT_NONE = 0, + SKIPSTONE_MESSAGE_ENDPOINT_FIRMWARE = 1, + SKIPSTONE_MESSAGE_ENDPOINT_TIME = 11, + SKIPSTONE_MESSAGE_ENDPOINT_VERSIONS = 16, + SKIPSTONE_MESSAGE_ENDPOINT_PHONE_VERSION = 17, + SKIPSTONE_MESSAGE_ENDPOINT_SYSTEM_MESSAGE = 18, + SKIPSTONE_MESSAGE_ENDPOINT_MUSIC_CONTROL = 32, + SKIPSTONE_MESSAGE_ENDPOINT_PHONE_CONTROL = 33 +}; + +typedef struct _skipstone_message_service skipstone_message_service; + +typedef int (*skipstone_message_handler)(skipstone_message_service *service, + void *buf, uint16_t size, uint16_t endpoint); + +skipstone_message_service *skipstone_message_service_new(); + +int skipstone_message_service_register(skipstone_message_service *service, + uint16_t endpoint, skipstone_message_handler *handler, void *context); + +int skipstone_message_service_deregister(skipstone_message_service *service, + uint16_t endpoint); + +int skipstone_message_service_run(skipstone_message_service *service, + skipstone_link *link); + +#endif /* _SKIPSTONE_MESSAGE_H */ diff --git a/include/skipstone/queue.h b/include/skipstone/queue.h new file mode 100644 index 0000000..825067f --- /dev/null +++ b/include/skipstone/queue.h @@ -0,0 +1,16 @@ +#ifndef _SKIPSTONE_QUEUE_H +#define _SKIPSTONE_QUEUE_H + +#include + +typedef struct _skipstone_queue skipstone_queue; + +skipstone_queue *skipstone_queue_new(); + +size_t skipstone_queue_count(skipstone_queue *queue); + +int skipstone_queue_push(skipstone_queue *queue, void *value); + +int skipstone_queue_pop(skipstone_queue *queue, void **value); + +#endif /* _SKIPSTONE_QUEUE_H */ diff --git a/include/skipstone/skipstone.h b/include/skipstone/skipstone.h deleted file mode 100644 index a33fefd..0000000 --- a/include/skipstone/skipstone.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _SKIPSTONE_H -#define _SKIPSTONE_H - -#include -#include - -#define SKIPSTONE_MESSAGE_MAX_PAYLOAD 4096 - -enum skipstone_message_endpoint { - SKIPSTONE_MESSAGE_ENDPOINT_NONE = 0, - SKIPSTONE_MESSAGE_ENDPOINT_FIRMWARE = 1, - SKIPSTONE_MESSAGE_ENDPOINT_TIME = 11, - SKIPSTONE_MESSAGE_ENDPOINT_VERSIONS = 16, - SKIPSTONE_MESSAGE_ENDPOINT_PHONE_VERSION = 17, - SKIPSTONE_MESSAGE_ENDPOINT_SYSTEM_MESSAGE = 18, - SKIPSTONE_MESSAGE_ENDPOINT_MUSIC_CONTROL = 32, - 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); - -skipstone_watch *skipstone_watch_new(skipstone_watch_link *link); - -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/include/skipstone/watch.h b/include/skipstone/watch.h new file mode 100644 index 0000000..137c516 --- /dev/null +++ b/include/skipstone/watch.h @@ -0,0 +1,13 @@ +#ifndef _SKIPSTONE_WATCH_H +#define _SKIPSTONE_WATCH_H + +#include +#include + +typedef struct _skipstone_watch skipstone_watch; + +skipstone_watch *skipstone_watch_new(skipstone_link *link); + +void skipstone_watch_destroy(skipstone_watch *watch); + +#endif /* _SKIPSTONE_WATCH_H */ diff --git a/src/Makefile b/src/Makefile index 2b673ce..f443df6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,9 +7,9 @@ CC = $(CROSS)cc CFLAGS += -fPIC -I$(INCLUDE_PATH) LDFLAGS = -HEADERS = skipstone.h map.h +HEADERS = watch.h link.h map.h message.h -OBJS = skipstone.o map.o +OBJS = watch.o link.o map.o message.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/skipstone.c b/src/link.c similarity index 76% rename from src/skipstone.c rename to src/link.c index c3a87c2..280043d 100644 --- a/src/skipstone.c +++ b/src/link.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,21 +8,20 @@ #include #include -#include -#include +#include +#include -enum skipstone_watch_link_type { +enum skipstone_link_type { SKIPSTONE_WATCH_LINK_SERIAL = 1 }; -struct _skipstone_message_endpoint { - uint16_t id; - skipstone_message_handler *handler; - void *context; +struct message_header { + uint16_t size, + endpoint; }; -struct _skipstone_watch_link { - enum skipstone_watch_link_type type; +struct _skipstone_link { + enum skipstone_link_type type; union { struct { @@ -33,18 +33,8 @@ struct _skipstone_watch_link { }; }; -struct _skipstone_watch { - skipstone_watch_link *link; - skipstone_map *endpoints; -}; - -typedef struct _skipstone_message_header { - uint16_t size, - endpoint; -} skipstone_message_header; - -skipstone_watch_link *skipstone_watch_link_open_serial(const char *device) { - skipstone_watch_link *link; +skipstone_link *skipstone_link_open_serial(const char *device) { + skipstone_link *link; if ((link = malloc(sizeof(*link))) == NULL) { goto error_malloc_link; @@ -91,7 +81,7 @@ error_malloc_link: return NULL; } -static int _watch_link_close_serial(skipstone_watch_link *link) { +static int _watch_link_close_serial(skipstone_link *link) { if (tcsetattr(link->serial.fd, TCSANOW, &link->serial.attr_old) < 0) { goto error_io; } @@ -102,7 +92,7 @@ error_io: return -1; } -int skipstone_watch_link_close(skipstone_watch_link *link) { +int skipstone_link_close(skipstone_link *link) { switch (link->type) { case SKIPSTONE_WATCH_LINK_SERIAL: { return _watch_link_close_serial(link); @@ -114,8 +104,8 @@ int skipstone_watch_link_close(skipstone_watch_link *link) { return -1; } -int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { - skipstone_message_header header; +int skipstone_link_send(skipstone_link *link, void *buf, uint16_t size, uint16_t endpoint) { + struct message_header header; ssize_t wrlen; @@ -149,8 +139,8 @@ error_toobig: return -1; } -int skipstone_watch_link_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { - skipstone_message_header header; +int skipstone_link_recv(skipstone_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { + struct message_header header; ssize_t len_read; size_t len_wanted, offset = 0; diff --git a/src/skipstone.c-e b/src/link.c-e similarity index 76% rename from src/skipstone.c-e rename to src/link.c-e index c3a87c2..280043d 100644 --- a/src/skipstone.c-e +++ b/src/link.c-e @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,21 +8,20 @@ #include #include -#include -#include +#include +#include -enum skipstone_watch_link_type { +enum skipstone_link_type { SKIPSTONE_WATCH_LINK_SERIAL = 1 }; -struct _skipstone_message_endpoint { - uint16_t id; - skipstone_message_handler *handler; - void *context; +struct message_header { + uint16_t size, + endpoint; }; -struct _skipstone_watch_link { - enum skipstone_watch_link_type type; +struct _skipstone_link { + enum skipstone_link_type type; union { struct { @@ -33,18 +33,8 @@ struct _skipstone_watch_link { }; }; -struct _skipstone_watch { - skipstone_watch_link *link; - skipstone_map *endpoints; -}; - -typedef struct _skipstone_message_header { - uint16_t size, - endpoint; -} skipstone_message_header; - -skipstone_watch_link *skipstone_watch_link_open_serial(const char *device) { - skipstone_watch_link *link; +skipstone_link *skipstone_link_open_serial(const char *device) { + skipstone_link *link; if ((link = malloc(sizeof(*link))) == NULL) { goto error_malloc_link; @@ -91,7 +81,7 @@ error_malloc_link: return NULL; } -static int _watch_link_close_serial(skipstone_watch_link *link) { +static int _watch_link_close_serial(skipstone_link *link) { if (tcsetattr(link->serial.fd, TCSANOW, &link->serial.attr_old) < 0) { goto error_io; } @@ -102,7 +92,7 @@ error_io: return -1; } -int skipstone_watch_link_close(skipstone_watch_link *link) { +int skipstone_link_close(skipstone_link *link) { switch (link->type) { case SKIPSTONE_WATCH_LINK_SERIAL: { return _watch_link_close_serial(link); @@ -114,8 +104,8 @@ int skipstone_watch_link_close(skipstone_watch_link *link) { return -1; } -int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { - skipstone_message_header header; +int skipstone_link_send(skipstone_link *link, void *buf, uint16_t size, uint16_t endpoint) { + struct message_header header; ssize_t wrlen; @@ -149,8 +139,8 @@ error_toobig: return -1; } -int skipstone_watch_link_recv(skipstone_watch_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { - skipstone_message_header header; +int skipstone_link_recv(skipstone_link *link, void *buf, uint16_t *size, uint16_t *endpoint) { + struct message_header header; ssize_t len_read; size_t len_wanted, offset = 0; diff --git a/src/map.c b/src/map.c index f005250..3d0ce2d 100644 --- a/src/map.c +++ b/src/map.c @@ -54,7 +54,7 @@ void *skipstone_map_get(skipstone_map *map, uint16_t index) { return map[index & 0x0f].value; } -void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value) { +int 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; + map[index & 0x0f].value = value; + + return 0; error_new: - return NULL; + return -1; } diff --git a/src/map.c-e b/src/map.c-e index f005250..3d0ce2d 100644 --- a/src/map.c-e +++ b/src/map.c-e @@ -54,7 +54,7 @@ void *skipstone_map_get(skipstone_map *map, uint16_t index) { return map[index & 0x0f].value; } -void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value) { +int 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; + map[index & 0x0f].value = value; + + return 0; error_new: - return NULL; + return -1; } diff --git a/src/message.c b/src/message.c new file mode 100644 index 0000000..8ee7372 --- /dev/null +++ b/src/message.c @@ -0,0 +1,64 @@ +#include +#include + +#include +#include +#include +#include + +struct endpoint { + uint16_t id; + skipstone_message_handler *handler; + void *context; +}; + +struct _skipstone_message_service { + skipstone_map *endpoints; + skipstone_queue *pending; +}; + +skipstone_message_service *skipstone_message_service_new() { + return (skipstone_message_service *)skipstone_map_new(); +} + +int skipstone_message_service_register(skipstone_message_service *service, + uint16_t id, skipstone_message_handler *handler, void *context) { + struct endpoint *endpoint; + + if ((endpoint = malloc(sizeof(*endpoint))) == NULL) { + goto error_malloc_endpoint; + } + + endpoint->id = id; + endpoint->handler = handler; + endpoint->context = context; + + return skipstone_map_set(service->endpoints, id, endpoint); + +error_malloc_endpoint: + return -1; +} + +int skipstone_message_service_deregister(skipstone_message_service *service, uint16_t id) { + return skipstone_map_set((skipstone_map *)service, id, NULL); +} + +int skipstone_message_service_run(skipstone_message_service *service, + skipstone_link *link) { + void *buf; + uint16_t len, id; + + if ((buf = malloc(SKIPSTONE_MESSAGE_MAX_PAYLOAD)) == NULL) { + goto error_malloc_buf; + } + + while (skipstone_link_recv(link, buf, &len, &id) >= 0) { + printf("Received message %hu bytes for endpoint %hu\n", + len, id); + } + + return 0; + +error_malloc_buf: + return -1; +} diff --git a/src/message.c-e b/src/message.c-e new file mode 100644 index 0000000..8ee7372 --- /dev/null +++ b/src/message.c-e @@ -0,0 +1,64 @@ +#include +#include + +#include +#include +#include +#include + +struct endpoint { + uint16_t id; + skipstone_message_handler *handler; + void *context; +}; + +struct _skipstone_message_service { + skipstone_map *endpoints; + skipstone_queue *pending; +}; + +skipstone_message_service *skipstone_message_service_new() { + return (skipstone_message_service *)skipstone_map_new(); +} + +int skipstone_message_service_register(skipstone_message_service *service, + uint16_t id, skipstone_message_handler *handler, void *context) { + struct endpoint *endpoint; + + if ((endpoint = malloc(sizeof(*endpoint))) == NULL) { + goto error_malloc_endpoint; + } + + endpoint->id = id; + endpoint->handler = handler; + endpoint->context = context; + + return skipstone_map_set(service->endpoints, id, endpoint); + +error_malloc_endpoint: + return -1; +} + +int skipstone_message_service_deregister(skipstone_message_service *service, uint16_t id) { + return skipstone_map_set((skipstone_map *)service, id, NULL); +} + +int skipstone_message_service_run(skipstone_message_service *service, + skipstone_link *link) { + void *buf; + uint16_t len, id; + + if ((buf = malloc(SKIPSTONE_MESSAGE_MAX_PAYLOAD)) == NULL) { + goto error_malloc_buf; + } + + while (skipstone_link_recv(link, buf, &len, &id) >= 0) { + printf("Received message %hu bytes for endpoint %hu\n", + len, id); + } + + return 0; + +error_malloc_buf: + return -1; +} diff --git a/src/queue.c b/src/queue.c new file mode 100644 index 0000000..6d98d51 --- /dev/null +++ b/src/queue.c @@ -0,0 +1,67 @@ +#include + +#include + +struct entry { + void *value; + struct entry *next, *prev; +}; + +struct _skipstone_queue { + size_t count; + struct entry *last; +}; + +skipstone_queue *skipstone_queue_new() { + skipstone_queue *queue; + + if ((queue = malloc(sizeof(*queue))) == NULL) { + goto error_malloc_queue; + } + + queue->count = 0; + queue->last = NULL; + + return queue; + +error_malloc_queue: + return NULL; +} + +size_t skipstone_queue_count(skipstone_queue *queue) { + return queue->count; +} + +int skipstone_queue_push(skipstone_queue *queue, void *value) { + struct entry *next; + + if ((next = malloc(sizeof(*entry))) == NULL) { + goto error_malloc_next; + } + + next->value = value; + next->next = NULL; + next->prev = NULL; + + queue->last->next = next; + queue->last = next; + + return 0; + +error_malloc_next: + return -1; +} + +int skipstone_queue_pop(skipstone_queue *queue, void **value) { + struct entry *last = queue->last; + + if (last == NULL) { + return 0; + } + + queue->last = last->prev; + + free(last); + + return 0; +} diff --git a/src/queue.c-e b/src/queue.c-e new file mode 100644 index 0000000..6d98d51 --- /dev/null +++ b/src/queue.c-e @@ -0,0 +1,67 @@ +#include + +#include + +struct entry { + void *value; + struct entry *next, *prev; +}; + +struct _skipstone_queue { + size_t count; + struct entry *last; +}; + +skipstone_queue *skipstone_queue_new() { + skipstone_queue *queue; + + if ((queue = malloc(sizeof(*queue))) == NULL) { + goto error_malloc_queue; + } + + queue->count = 0; + queue->last = NULL; + + return queue; + +error_malloc_queue: + return NULL; +} + +size_t skipstone_queue_count(skipstone_queue *queue) { + return queue->count; +} + +int skipstone_queue_push(skipstone_queue *queue, void *value) { + struct entry *next; + + if ((next = malloc(sizeof(*entry))) == NULL) { + goto error_malloc_next; + } + + next->value = value; + next->next = NULL; + next->prev = NULL; + + queue->last->next = next; + queue->last = next; + + return 0; + +error_malloc_next: + return -1; +} + +int skipstone_queue_pop(skipstone_queue *queue, void **value) { + struct entry *last = queue->last; + + if (last == NULL) { + return 0; + } + + queue->last = last->prev; + + free(last); + + return 0; +} diff --git a/src/watch.c b/src/watch.c new file mode 100644 index 0000000..6be21df --- /dev/null +++ b/src/watch.c @@ -0,0 +1,41 @@ +#include + +#include +#include + +struct _skipstone_watch { + skipstone_link *link; + skipstone_map *endpoints; +}; + +struct endpoint { + +}; + +skipstone_watch *skipstone_watch_new(skipstone_link *link) { + skipstone_watch *watch; + + if ((watch = malloc(sizeof(*watch))) == NULL) { + goto error_malloc_watch; + } + + if ((watch->endpoints = skipstone_map_new()) == NULL) { + goto error_map_new_endpoints; + } + + watch->link = link; + + return watch; + +error_map_new_endpoints: + free(watch); + +error_malloc_watch: + return NULL; +} + +void skipstone_watch_destroy(skipstone_watch *watch) { + skipstone_map_destroy(watch->endpoints); + + free(watch); +} diff --git a/src/watch.c-e b/src/watch.c-e new file mode 100644 index 0000000..6be21df --- /dev/null +++ b/src/watch.c-e @@ -0,0 +1,41 @@ +#include + +#include +#include + +struct _skipstone_watch { + skipstone_link *link; + skipstone_map *endpoints; +}; + +struct endpoint { + +}; + +skipstone_watch *skipstone_watch_new(skipstone_link *link) { + skipstone_watch *watch; + + if ((watch = malloc(sizeof(*watch))) == NULL) { + goto error_malloc_watch; + } + + if ((watch->endpoints = skipstone_map_new()) == NULL) { + goto error_map_new_endpoints; + } + + watch->link = link; + + return watch; + +error_map_new_endpoints: + free(watch); + +error_malloc_watch: + return NULL; +} + +void skipstone_watch_destroy(skipstone_watch *watch) { + skipstone_map_destroy(watch->endpoints); + + free(watch); +}