skipstone/examples/test.c
2020-09-21 21:21:57 -05:00

172 lines
4.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <skipstone/link.h>
#include <skipstone/message.h>
#include <skipstone/service.h>
#include <skipstone/system.h>
#include "../src/util.h"
struct context {
skipstone_link *link;
};
static void usage(int argc, char **argv) {
fprintf(stderr, "usage: %s /dev/rfcommX\n", argv[0]);
exit(1);
}
static int answer_phone_version_message(skipstone_service *service,
void *buf,
uint16_t size,
uint16_t id,
void *ctx) {
const uint32_t flags = SKIPSTONE_SYSTEM_CLIENT_TELEPHONY
| SKIPSTONE_SYSTEM_CLIENT_SMS
| SKIPSTONE_SYSTEM_CLIENT_ANDROID;
struct context *context = ctx;
return skipstone_system_send_client_version(context->link, flags);
}
static int answer_music_message(skipstone_service *service,
void *buf,
uint16_t size,
uint16_t id,
void *ctx) {
struct context *context = ctx;
uint8_t message[256];
ssize_t len;
if (((uint8_t *)buf)[0] == 0x08) {
if ((len = skipstone_message_pack(message,
sizeof(message),
"CzzzLSS",
0x10,
"KMFDM",
"Nihil",
"Ultra")) < 0) {
goto error_message_pack;
}
if (skipstone_link_send(context->link, message, 32, len) < 0) {
goto error_link_send;
}
if ((len = skipstone_message_pack(message,
sizeof(message),
"CCLLCC",
0x11,
1,
htole32(120),
htole32(120),
htole32(320),
1,
1)) < 0) {
goto error_message_pack;
}
if (skipstone_link_send(context->link, message, 32, len) < 0) {
goto error_link_send;
}
if ((len = skipstone_message_pack(message,
sizeof(message),
"Czz",
0x13,
"DeaDBeeF",
"DeaDBeeF")) < 0) {
goto error_message_pack;
}
if (skipstone_link_send(context->link, message, 32, len) < 0) {
goto error_link_send;
}
} else {
printf("Got playback command %02x\n", ((uint8_t *)buf)[0]);
}
return 0;
error_link_send:
error_message_pack:
return -1;
}
static int answer_phone_message(skipstone_service *service,
void *buf,
uint16_t size,
uint16_t id,
void *context) {
printf("Got phone command %02x\n", ((uint8_t *)buf)[0]);
return 0;
}
struct responder {
uint16_t id;
int (*responder)(skipstone_service *, void *, uint16_t, uint16_t, void *);
};
static struct responder responders[] = {
{ 17, answer_phone_version_message },
{ 32, answer_music_message },
{ 33, answer_phone_message },
{ 0, NULL }
};
int main(int argc, char **argv) {
skipstone_link *link;
skipstone_service *service;
struct context context;
int i;
if (argc != 2) {
usage(argc, argv);
}
if ((link = skipstone_link_open(argv[1])) == NULL) {
perror("skipstone_link_open()");
goto error_link_open;
}
if ((service = skipstone_service_new(link)) == NULL) {
perror("skipstone_service_new()");
goto error_service_new;
}
context.link = link;
for (i=0; responders[i].responder != NULL; i++) {
skipstone_service_responder_add(service,
responders[i].responder,
responders[i].id,
&context);
}
while (1) {
if (skipstone_service_event_handle(service) < 0) {
perror("skipstone_service_event_handle()");
goto error_io;
}
}
skipstone_link_destroy(link);
return 0;
error_io:
error_service_new:
skipstone_link_destroy(link);
error_link_open:
return 1;
}