skipstone/examples/read.c
XANTRONIX Development 7120e736f6 more refactoring
2020-09-21 15:14:50 -05:00

52 lines
1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <skipstone/link.h>
#include <skipstone/message.h>
static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]);
exit(1);
}
int main(int argc, char **argv) {
skipstone_link *link;
void *buf;
ssize_t len;
uint16_t endpoint;
if (argc != 2) {
usage(argc, argv);
}
if ((buf = malloc(SKIPSTONE_MESSAGE_MAX_PAYLOAD)) == NULL) {
goto error_malloc_buf;
}
if ((link = skipstone_link_open(argv[1])) == NULL) {
goto error_link_open;
}
while ((len = skipstone_link_recv(link,
buf,
&endpoint,
SKIPSTONE_MESSAGE_MAX_PAYLOAD)) >= 0) {
printf("Received message %zd bytes for endpoint %hu\n",
len, endpoint);
}
perror("skipstone_link_recv()");
skipstone_link_close(link);
return 0;
error_link_open:
free(buf);
error_malloc_buf:
return 1;
}