skipstone/examples/read.c

53 lines
1 KiB
C
Raw Permalink Normal View History

2017-11-21 22:24:52 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2017-11-23 17:22:36 +00:00
#include <skipstone/link.h>
#include <skipstone/message.h>
2017-11-21 22:24:52 +00:00
static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]);
exit(1);
}
int main(int argc, char **argv) {
2017-11-23 17:22:36 +00:00
skipstone_link *link;
2020-09-21 15:14:50 -05:00
2017-11-21 22:24:52 +00:00
void *buf;
2020-09-21 15:14:50 -05:00
ssize_t len;
uint16_t endpoint;
2017-11-21 22:24:52 +00:00
if (argc != 2) {
usage(argc, argv);
}
if ((buf = malloc(SKIPSTONE_MESSAGE_MAX_PAYLOAD)) == NULL) {
goto error_malloc_buf;
}
2020-09-21 15:14:50 -05:00
if ((link = skipstone_link_open(argv[1])) == NULL) {
2017-11-23 17:22:36 +00:00
goto error_link_open;
2017-11-21 22:24:52 +00:00
}
2020-09-21 15:14:50 -05:00
while ((len = skipstone_link_recv(link,
buf,
&endpoint,
SKIPSTONE_MESSAGE_MAX_PAYLOAD)) >= 0) {
printf("Received message %zd bytes for endpoint %hu\n",
2017-11-21 22:24:52 +00:00
len, endpoint);
}
2017-11-23 17:22:36 +00:00
perror("skipstone_link_recv()");
2017-11-21 22:24:52 +00:00
skipstone_link_destroy(link);
2017-11-21 22:24:52 +00:00
return 0;
2017-11-23 17:22:36 +00:00
error_link_open:
2017-11-21 22:24:52 +00:00
free(buf);
error_malloc_buf:
return 1;
}