peanut parents in syrup

This commit is contained in:
XANTRONIX 2017-11-23 17:22:36 +00:00
parent 37a1d948f3
commit 5e8e29505b
23 changed files with 514 additions and 146 deletions

View file

@ -13,8 +13,8 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
skipstone_map_set(map, 0xdead, 0xdeadbeef); skipstone_map_set(map, 0xdead, (void *)0xdeadbeef);
skipstone_map_set(map, 0xbeef, 0xcafebabe); skipstone_map_set(map, 0xbeef, (void *)0xcafebabe);
printf("%p\n", skipstone_map_get(map, 0xdead)); printf("%p\n", skipstone_map_get(map, 0xdead));
printf("%p\n", skipstone_map_get(map, 0xbeef)); printf("%p\n", skipstone_map_get(map, 0xbeef));

View file

@ -13,8 +13,8 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
skipstone_map_set(map, 0xdead, 0xdeadbeef); skipstone_map_set(map, 0xdead, (void *)0xdeadbeef);
skipstone_map_set(map, 0xbeef, 0xcafebabe); skipstone_map_set(map, 0xbeef, (void *)0xcafebabe);
printf("%p\n", skipstone_map_get(map, 0xdead)); printf("%p\n", skipstone_map_get(map, 0xdead));
printf("%p\n", skipstone_map_get(map, 0xbeef)); printf("%p\n", skipstone_map_get(map, 0xbeef));

View file

@ -2,7 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <skipstone/skipstone.h> #include <skipstone/link.h>
#include <skipstone/message.h>
static void usage(int argc, char **argv) { static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); 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) { int main(int argc, char **argv) {
skipstone_watch_link *link; skipstone_link *link;
void *buf; void *buf;
uint16_t len, endpoint; uint16_t len, endpoint;
@ -60,20 +61,20 @@ int main(int argc, char **argv) {
goto error_malloc_buf; 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; 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", printf("Received message %hu bytes for endpoint %hu\n",
len, endpoint); 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; return 0;

View file

@ -2,7 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <skipstone/skipstone.h> #include <skipstone/link.h>
#include <skipstone/message.h>
static void usage(int argc, char **argv) { static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); 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) { int main(int argc, char **argv) {
skipstone_watch_link *link; skipstone_link *link;
void *buf; void *buf;
uint16_t len, endpoint; uint16_t len, endpoint;
@ -60,20 +61,20 @@ int main(int argc, char **argv) {
goto error_malloc_buf; 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; 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", printf("Received message %hu bytes for endpoint %hu\n",
len, endpoint); 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; return 0;

View file

@ -2,7 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <skipstone/skipstone.h> #include <skipstone/link.h>
#include <skipstone/message.h>
static void usage(int argc, char **argv) { static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); 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) { int main(int argc, char **argv) {
skipstone_watch_link *link; skipstone_link *link;
void *buf; void *buf;
uint16_t len, endpoint; uint16_t len, endpoint;
@ -22,22 +23,22 @@ int main(int argc, char **argv) {
goto error_malloc_buf; 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; 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", printf("Received message %hu bytes for endpoint %hu\n",
len, endpoint); len, endpoint);
} }
perror("skipstone_recv()"); perror("skipstone_link_recv()");
skipstone_watch_link_close(link); skipstone_link_close(link);
return 0; return 0;
error_watch_link_open: error_link_open:
free(buf); free(buf);
error_malloc_buf: error_malloc_buf:

View file

@ -2,7 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <skipstone/skipstone.h> #include <skipstone/link.h>
#include <skipstone/message.h>
static void usage(int argc, char **argv) { static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]); 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) { int main(int argc, char **argv) {
skipstone_watch_link *link; skipstone_link *link;
void *buf; void *buf;
uint16_t len, endpoint; uint16_t len, endpoint;
@ -22,22 +23,22 @@ int main(int argc, char **argv) {
goto error_malloc_buf; 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; 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", printf("Received message %hu bytes for endpoint %hu\n",
len, endpoint); len, endpoint);
} }
perror("skipstone_recv()"); perror("skipstone_link_recv()");
skipstone_watch_link_close(link); skipstone_link_close(link);
return 0; return 0;
error_watch_link_open: error_link_open:
free(buf); free(buf);
error_malloc_buf: error_malloc_buf:

18
include/skipstone/link.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef _SKIPSTONE_LINK_H
#define _SKIPSTONE_LINK_H
#include <stdint.h>
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 */

View file

@ -19,6 +19,6 @@ void skipstone_map_destroy(skipstone_map *map);
void *skipstone_map_get(skipstone_map *map, uint16_t index); 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 */ #endif /* _SKIPSTONE_MAP_H */

View file

@ -0,0 +1,38 @@
#ifndef _SKIPSTONE_MESSAGE_H
#define _SKIPSTONE_MESSAGE_H
#include <stdint.h>
#include <skipstone/link.h>
#include <skipstone/map.h>
#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 */

16
include/skipstone/queue.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef _SKIPSTONE_QUEUE_H
#define _SKIPSTONE_QUEUE_H
#include <sys/types.h>
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 */

View file

@ -1,49 +0,0 @@
#ifndef _SKIPSTONE_H
#define _SKIPSTONE_H
#include <stdlib.h>
#include <stdint.h>
#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 */

13
include/skipstone/watch.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef _SKIPSTONE_WATCH_H
#define _SKIPSTONE_WATCH_H
#include <skipstone/link.h>
#include <skipstone/message.h>
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 */

View file

@ -7,9 +7,9 @@ CC = $(CROSS)cc
CFLAGS += -fPIC -I$(INCLUDE_PATH) CFLAGS += -fPIC -I$(INCLUDE_PATH)
LDFLAGS = 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_MAJOR = 0
VERSION_MINOR = 0.1 VERSION_MINOR = 0.1

View file

@ -1,3 +1,4 @@
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
@ -7,21 +8,20 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <endian.h> #include <endian.h>
#include <skipstone/skipstone.h> #include <skipstone/link.h>
#include <skipstone/map.h> #include <skipstone/message.h>
enum skipstone_watch_link_type { enum skipstone_link_type {
SKIPSTONE_WATCH_LINK_SERIAL = 1 SKIPSTONE_WATCH_LINK_SERIAL = 1
}; };
struct _skipstone_message_endpoint { struct message_header {
uint16_t id; uint16_t size,
skipstone_message_handler *handler; endpoint;
void *context;
}; };
struct _skipstone_watch_link { struct _skipstone_link {
enum skipstone_watch_link_type type; enum skipstone_link_type type;
union { union {
struct { struct {
@ -33,18 +33,8 @@ struct _skipstone_watch_link {
}; };
}; };
struct _skipstone_watch { skipstone_link *skipstone_link_open_serial(const char *device) {
skipstone_watch_link *link; skipstone_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;
if ((link = malloc(sizeof(*link))) == NULL) { if ((link = malloc(sizeof(*link))) == NULL) {
goto error_malloc_link; goto error_malloc_link;
@ -91,7 +81,7 @@ error_malloc_link:
return NULL; 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) { if (tcsetattr(link->serial.fd, TCSANOW, &link->serial.attr_old) < 0) {
goto error_io; goto error_io;
} }
@ -102,7 +92,7 @@ error_io:
return -1; return -1;
} }
int skipstone_watch_link_close(skipstone_watch_link *link) { int skipstone_link_close(skipstone_link *link) {
switch (link->type) { switch (link->type) {
case SKIPSTONE_WATCH_LINK_SERIAL: { case SKIPSTONE_WATCH_LINK_SERIAL: {
return _watch_link_close_serial(link); return _watch_link_close_serial(link);
@ -114,8 +104,8 @@ int skipstone_watch_link_close(skipstone_watch_link *link) {
return -1; return -1;
} }
int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { int skipstone_link_send(skipstone_link *link, void *buf, uint16_t size, uint16_t endpoint) {
skipstone_message_header header; struct message_header header;
ssize_t wrlen; ssize_t wrlen;
@ -149,8 +139,8 @@ error_toobig:
return -1; return -1;
} }
int skipstone_watch_link_recv(skipstone_watch_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) {
skipstone_message_header header; struct message_header header;
ssize_t len_read; ssize_t len_read;
size_t len_wanted, offset = 0; size_t len_wanted, offset = 0;

View file

@ -1,3 +1,4 @@
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
@ -7,21 +8,20 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <endian.h> #include <endian.h>
#include <skipstone/skipstone.h> #include <skipstone/link.h>
#include <skipstone/map.h> #include <skipstone/message.h>
enum skipstone_watch_link_type { enum skipstone_link_type {
SKIPSTONE_WATCH_LINK_SERIAL = 1 SKIPSTONE_WATCH_LINK_SERIAL = 1
}; };
struct _skipstone_message_endpoint { struct message_header {
uint16_t id; uint16_t size,
skipstone_message_handler *handler; endpoint;
void *context;
}; };
struct _skipstone_watch_link { struct _skipstone_link {
enum skipstone_watch_link_type type; enum skipstone_link_type type;
union { union {
struct { struct {
@ -33,18 +33,8 @@ struct _skipstone_watch_link {
}; };
}; };
struct _skipstone_watch { skipstone_link *skipstone_link_open_serial(const char *device) {
skipstone_watch_link *link; skipstone_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;
if ((link = malloc(sizeof(*link))) == NULL) { if ((link = malloc(sizeof(*link))) == NULL) {
goto error_malloc_link; goto error_malloc_link;
@ -91,7 +81,7 @@ error_malloc_link:
return NULL; 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) { if (tcsetattr(link->serial.fd, TCSANOW, &link->serial.attr_old) < 0) {
goto error_io; goto error_io;
} }
@ -102,7 +92,7 @@ error_io:
return -1; return -1;
} }
int skipstone_watch_link_close(skipstone_watch_link *link) { int skipstone_link_close(skipstone_link *link) {
switch (link->type) { switch (link->type) {
case SKIPSTONE_WATCH_LINK_SERIAL: { case SKIPSTONE_WATCH_LINK_SERIAL: {
return _watch_link_close_serial(link); return _watch_link_close_serial(link);
@ -114,8 +104,8 @@ int skipstone_watch_link_close(skipstone_watch_link *link) {
return -1; return -1;
} }
int skipstone_watch_link_send(skipstone_watch_link *link, void *buf, uint16_t size, uint16_t endpoint) { int skipstone_link_send(skipstone_link *link, void *buf, uint16_t size, uint16_t endpoint) {
skipstone_message_header header; struct message_header header;
ssize_t wrlen; ssize_t wrlen;
@ -149,8 +139,8 @@ error_toobig:
return -1; return -1;
} }
int skipstone_watch_link_recv(skipstone_watch_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) {
skipstone_message_header header; struct message_header header;
ssize_t len_read; ssize_t len_read;
size_t len_wanted, offset = 0; size_t len_wanted, offset = 0;

View file

@ -54,7 +54,7 @@ void *skipstone_map_get(skipstone_map *map, uint16_t index) {
return map[index & 0x0f].value; 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; uint16_t i;
for (i=0; i<SKIPSTONE_MAP_DEPTH; i++) { for (i=0; i<SKIPSTONE_MAP_DEPTH; i++) {
@ -72,8 +72,10 @@ void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value) {
index >>= 4; index >>= 4;
} }
return map[index & 0x0f].value = value; map[index & 0x0f].value = value;
return 0;
error_new: error_new:
return NULL; return -1;
} }

View file

@ -54,7 +54,7 @@ void *skipstone_map_get(skipstone_map *map, uint16_t index) {
return map[index & 0x0f].value; 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; uint16_t i;
for (i=0; i<SKIPSTONE_MAP_DEPTH; i++) { for (i=0; i<SKIPSTONE_MAP_DEPTH; i++) {
@ -72,8 +72,10 @@ void *skipstone_map_set(skipstone_map *map, uint16_t index, void *value) {
index >>= 4; index >>= 4;
} }
return map[index & 0x0f].value = value; map[index & 0x0f].value = value;
return 0;
error_new: error_new:
return NULL; return -1;
} }

64
src/message.c Normal file
View file

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <skipstone/link.h>
#include <skipstone/map.h>
#include <skipstone/queue.h>
#include <skipstone/message.h>
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;
}

64
src/message.c-e Normal file
View file

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <skipstone/link.h>
#include <skipstone/map.h>
#include <skipstone/queue.h>
#include <skipstone/message.h>
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;
}

67
src/queue.c Normal file
View file

@ -0,0 +1,67 @@
#include <stdlib.h>
#include <skipstone/queue.h>
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;
}

67
src/queue.c-e Normal file
View file

@ -0,0 +1,67 @@
#include <stdlib.h>
#include <skipstone/queue.h>
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;
}

41
src/watch.c Normal file
View file

@ -0,0 +1,41 @@
#include <stdlib.h>
#include <skipstone/watch.h>
#include <skipstone/map.h>
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);
}

41
src/watch.c-e Normal file
View file

@ -0,0 +1,41 @@
#include <stdlib.h>
#include <skipstone/watch.h>
#include <skipstone/map.h>
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);
}