skipstone/examples/music.c
2017-11-23 17:28:47 +00:00

86 lines
1.8 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;
uint16_t len, endpoint;
struct {
uint8_t command;
uint8_t state;
uint32_t track_position;
uint32_t play_rate;
uint8_t shuffle;
uint8_t repeat;
} play_state = {
0x11, 0x01, 0, 44100, 0x01, 0x01
};
struct {
uint8_t command;
uint8_t artist_len;
char artist[5];
uint8_t album_len;
char album[5];
uint8_t title_len;
char title[5];
} play_track = {
0x10,
5, { 'K', 'M', 'F', 'D', 'M' },
5, { 'N', 'i', 'h', 'i', 'l' },
5, { 'U', 'l', 't', 'r', 'a' }
};
struct {
uint8_t command;
uint8_t package_len;
char package[8];
uint8_t name_len;
char name[8];
} play_info = {
0x13,
8, { 'D', 'e', 'a', 'D', 'B', 'e', 'e', 'F' },
8, { 'R', 'e', 'a', 'L', 'D', 'e', 'a', 'D' }
};
if (argc != 2) {
usage(argc, argv);
}
if ((buf = malloc(SKIPSTONE_MESSAGE_MAX_PAYLOAD)) == NULL) {
goto error_malloc_buf;
}
if ((link = skipstone_link_open_serial(argv[1])) == NULL) {
goto error_watch_link_open;
}
while (skipstone_link_recv(link, buf, &len, &endpoint) >= 0) {
printf("Received message %hu bytes for endpoint %hu\n",
len, endpoint);
skipstone_link_send(link, &play_track, sizeof(play_track), 0x20);
}
perror("skipstone_link_recv()");
skipstone_link_close(link);
return 0;
error_watch_link_open:
free(buf);
error_malloc_buf:
return 1;
}