skipstone/examples/test.c

156 lines
4.5 KiB
C
Raw Normal View History

2017-11-23 18:24:16 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2017-11-23 18:24:16 +00:00
#include <skipstone/link.h>
#include <skipstone/message.h>
#include <skipstone/service.h>
2017-11-26 16:22:00 -06:00
#include <skipstone/system.h>
2017-11-23 18:24:16 +00:00
2017-11-26 18:31:13 -06:00
#include "../src/util.h"
2020-09-21 15:14:50 -05:00
struct context {
skipstone_link *link;
};
2017-11-23 18:24:16 +00:00
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,
2020-09-21 15:14:50 -05:00
void *buf,
uint16_t size,
uint16_t id,
void *ctx) {
2017-11-29 21:42:01 -06:00
const uint32_t flags = SKIPSTONE_SYSTEM_CLIENT_TELEPHONY
| SKIPSTONE_SYSTEM_CLIENT_SMS
| SKIPSTONE_SYSTEM_CLIENT_ANDROID;
2017-11-24 11:57:55 -06:00
struct context *context = ctx;
return skipstone_system_send_client_version(context->link, flags);
2017-11-24 11:57:55 -06:00
}
static int answer_music_message(skipstone_service *service,
2020-09-21 15:14:50 -05:00
void *buf,
uint16_t size,
uint16_t id,
void *ctx) {
struct context *context = ctx;
uint8_t message[256];
ssize_t len;
2017-11-26 18:31:13 -06:00
if (((uint8_t *)buf)[0] == 0x08) {
if ((len = skipstone_message_pack(message,
sizeof(message),
"CzzzLSS",
0x10,
"KMFDM",
"Nihil",
"Ultra")) < 0) {
goto error_message_pack;
}
2020-09-21 15:14:50 -05:00
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;
}
2020-09-21 15:14:50 -05:00
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;
}
2020-09-21 15:14:50 -05:00
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]);
2017-11-26 18:31:13 -06:00
}
2017-11-23 18:24:16 +00:00
return 0;
2020-09-21 15:14:50 -05:00
error_link_send:
error_message_pack:
return -1;
2017-11-23 18:24:16 +00:00
}
static int answer_phone_message(skipstone_service *service,
2020-09-21 15:14:50 -05:00
void *buf,
uint16_t size,
uint16_t id,
void *context) {
2017-11-29 21:42:01 -06:00
printf("Got phone command %02x\n", ((uint8_t *)buf)[0]);
return 0;
}
2017-11-23 18:24:16 +00:00
int main(int argc, char **argv) {
skipstone_link *link;
skipstone_service *service;
2017-11-23 18:24:16 +00:00
2020-09-21 15:14:50 -05:00
struct context context;
2017-11-23 18:24:16 +00:00
if (argc != 2) {
usage(argc, argv);
}
2020-09-21 15:14:50 -05:00
if ((link = skipstone_link_open(argv[1])) == NULL) {
perror("skipstone_link_open()");
2017-11-23 18:24:16 +00:00
goto error_link_open;
}
if ((service = skipstone_service_new(link)) == NULL) {
perror("skipstone_service_new()");
goto error_service_new;
2017-11-23 18:24:16 +00:00
}
2020-09-21 15:14:50 -05:00
context.link = link;
2017-11-23 18:24:16 +00:00
skipstone_service_responder_add(service, answer_phone_version_message, 17, &context);
skipstone_service_responder_add(service, answer_music_message, 32, &context);
skipstone_service_responder_add(service, answer_phone_message, 33, &context);
2017-11-23 18:24:16 +00:00
2020-09-21 15:14:50 -05:00
while (1) {
if (skipstone_service_event_handle(service) < 0) {
perror("skipstone_service_event_handle()");
goto error_io;
}
}
2017-11-23 18:24:16 +00:00
skipstone_link_close(link);
return 0;
error_io:
error_service_new:
2017-11-23 18:24:16 +00:00
skipstone_link_close(link);
error_link_open:
return 1;
}