3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2014 Intel Corporation
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37
#include "lib/bluetooth.h"
39
#include "lib/hci_lib.h"
41
#include "btio/btio.h"
42
#include "src/shared/util.h"
43
#include "src/shared/queue.h"
46
static GMainLoop *mainloop = NULL;
47
static int dev_role = AVDTP_SEP_TYPE_SOURCE;
48
static bool preconf = false;
49
static struct avdtp *avdtp = NULL;
50
struct avdtp_stream *avdtp_stream = NULL;
51
struct avdtp_local_sep *local_sep = NULL;
52
struct avdtp_remote_sep *remote_sep = NULL;
53
static GIOChannel *io = NULL;
54
static bool reject = false;
57
static uint16_t version = 0x0103;
58
static guint media_player = 0;
59
static guint media_recorder = 0;
60
static guint idle_id = 0;
61
static struct queue *lseps = NULL;
63
static bool fragment = false;
76
static const char sbc_codec[] = {0x00, 0x00, 0x11, 0x15, 0x02, 0x40};
77
static const char sbc_media_frame[] = {
78
0x00, 0x60, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
79
0x01, 0x9c, 0xfd, 0x40, 0xbd, 0xde, 0xa9, 0x75, 0x43, 0x20, 0x87, 0x64,
80
0x44, 0x32, 0x7f, 0xbe, 0xf7, 0x76, 0xfe, 0xf7, 0xbb, 0xbb, 0x7f, 0xbe,
81
0xf7, 0x76, 0xfe, 0xf7, 0xbb, 0xbb, 0x7f, 0xbe, 0xf7, 0x76, 0xfe, 0xf7,
82
0xbb, 0xbb, 0x80, 0x3e, 0xf7, 0x76, 0xfe, 0xf7, 0xbb, 0xbb, 0x83, 0x41,
83
0x07, 0x77, 0x09, 0x07, 0x43, 0xb3, 0x81, 0xbc, 0xf8, 0x77, 0x02, 0xe5,
84
0xa4, 0x3a, 0xa0, 0xcb, 0x38, 0xbb, 0x57, 0x90, 0xd9, 0x08, 0x9c, 0x1d,
85
0x86, 0x59, 0x01, 0x0c, 0x21, 0x44, 0x68, 0x35, 0xa8, 0x57, 0x97, 0x0e,
86
0x9b, 0xbb, 0x62, 0xc4, 0xca, 0x57, 0x04, 0xa1, 0xca, 0x3b, 0xa3, 0x48,
87
0xd2, 0x66, 0x11, 0x33, 0x6a, 0x3b, 0xb4, 0xbb, 0x08, 0x77, 0x17, 0x03,
88
0xb4, 0x3b, 0x79, 0x3b, 0x46, 0x97, 0x0e, 0xf7, 0x3d, 0xbb, 0x3d, 0x49,
89
0x25, 0x86, 0x88, 0xb4, 0xad, 0x3b, 0x62, 0xbb, 0xa4, 0x47, 0x29, 0x99,
90
0x3b, 0x3b, 0xaf, 0xc6, 0xd4, 0x37, 0x68, 0x94, 0x0a, 0xbb
93
static void parse_command(const char *cmd)
95
if (!strncmp(cmd, "getconf", sizeof("getconf"))) {
96
command = CMD_GET_CONF;
97
} else if (!strncmp(cmd, "open", sizeof("open"))) {
99
} else if (!strncmp(cmd, "start", sizeof("start"))) {
101
} else if (!strncmp(cmd, "suspend", sizeof("suspend"))) {
102
command = CMD_SUSPEND;
103
} else if (!strncmp(cmd, "close", sizeof("close"))) {
105
} else if (!strncmp(cmd, "abort", sizeof("abort"))) {
107
} else if (!strncmp(cmd, "delay", sizeof("delay"))) {
110
printf("Unknown command '%s'\n", cmd);
111
printf("(getconf open start suspend close abort delay)\n");
116
static void send_command(void)
118
avdtp_state_t state = avdtp_sep_get_state(local_sep);
122
avdtp_get_configuration(avdtp, avdtp_stream);
125
if (state == AVDTP_STATE_CONFIGURED)
126
avdtp_open(avdtp, avdtp_stream);
129
if (state == AVDTP_STATE_OPEN)
130
avdtp_start(avdtp, avdtp_stream);
133
if (state == AVDTP_STATE_STREAMING)
134
avdtp_suspend(avdtp , avdtp_stream);
137
if (state == AVDTP_STATE_STREAMING)
138
avdtp_close(avdtp, avdtp_stream, FALSE);
141
avdtp_abort(avdtp , avdtp_stream);
144
avdtp_delay_report(avdtp , avdtp_stream , 250);
152
static gboolean media_writer(gpointer user_data)
158
if (!avdtp_stream_get_transport(avdtp_stream, &fd, NULL, &omtu, NULL))
161
if (omtu < sizeof(sbc_media_frame))
164
to_write = sizeof(sbc_media_frame);
166
if (write(fd, sbc_media_frame, to_write) < 0)
174
static bool start_media_player(void)
179
printf("Media streaming started\n");
181
if (media_player || !avdtp_stream)
184
if (!avdtp_stream_get_transport(avdtp_stream, &fd, NULL, &omtu, NULL))
187
media_player = g_timeout_add(200, media_writer, NULL);
194
static void stop_media_player(void)
199
printf("Media streaming stopped\n");
201
g_source_remove(media_player);
205
#if __BYTE_ORDER == __LITTLE_ENDIAN
216
uint16_t sequence_number;
220
} __attribute__ ((packed));
222
#elif __BYTE_ORDER == __BIG_ENDIAN
233
uint16_t sequence_number;
237
} __attribute__ ((packed));
240
#error "Unknown byte order"
243
static gboolean media_reader(GIOChannel *source, GIOCondition condition,
246
char buf[UINT16_MAX];
247
struct rtp_header *rtp = (void *) buf;
248
static bool decode = false;
252
if (!avdtp_stream_get_transport(avdtp_stream, &fd, &imtu, NULL, NULL))
255
ret = read(fd, buf, imtu);
257
printf("Reading failed (%s)\n", strerror(errno));
261
if (ret < (int) sizeof(*rtp)) {
262
printf("Not enough media data received (%u bytes)", ret);
267
printf("V=%u P=%u X=%u CC=%u M=%u PT=%u SeqNr=%d\n",
268
rtp->v, rtp->p, rtp->x, rtp->cc, rtp->m, rtp->pt,
269
be16_to_cpu(rtp->sequence_number));
278
static bool start_media_recorder(void)
284
printf("Media recording started\n");
286
if (media_recorder || !avdtp_stream)
289
if (!avdtp_stream_get_transport(avdtp_stream, &fd, NULL, &omtu, NULL))
292
chan = g_io_channel_unix_new(fd);
294
media_recorder = g_io_add_watch(chan, G_IO_IN, media_reader, NULL);
295
g_io_channel_unref(chan);
303
static void stop_media_recorder(void)
308
printf("Media recording stopped\n");
310
g_source_remove(media_recorder);
314
static void set_configuration_cfm(struct avdtp *session,
315
struct avdtp_local_sep *lsep,
316
struct avdtp_stream *stream,
317
struct avdtp_error *err,
320
printf("%s\n", __func__);
323
avdtp_open(avdtp, avdtp_stream);
326
static void get_configuration_cfm(struct avdtp *session,
327
struct avdtp_local_sep *lsep,
328
struct avdtp_stream *stream,
329
struct avdtp_error *err,
332
printf("%s\n", __func__);
335
static void disconnect_cb(void *user_data)
337
printf("Disconnected\n");
339
g_main_loop_quit(mainloop);
342
static void discover_cb(struct avdtp *session, GSList *seps,
343
struct avdtp_error *err, void *user_data)
345
struct avdtp_service_capability *service;
349
remote_sep = avdtp_find_remote_sep(avdtp, local_sep);
351
printf("Unable to find matching endpoint\n");
352
avdtp_shutdown(session);
356
printf("Matching endpoint found\n");
358
service = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0);
359
caps = g_slist_append(caps, service);
361
service = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, sbc_codec,
363
caps = g_slist_append(caps, service);
365
ret = avdtp_set_configuration(avdtp, remote_sep, local_sep, caps,
368
g_slist_free_full(caps, g_free);
371
printf("Failed to set configuration (%s)\n", strerror(-ret));
372
avdtp_shutdown(session);
376
static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
383
printf("%s\n", err->message);
384
g_main_loop_quit(mainloop);
388
bt_io_get(chan, &gerr,
389
BT_IO_OPT_IMTU, &imtu,
390
BT_IO_OPT_OMTU, &omtu,
391
BT_IO_OPT_DEST_BDADDR, &dst,
394
printf("%s\n", gerr->message);
395
g_main_loop_quit(mainloop);
399
printf("Connected (imtu=%d omtu=%d)\n", imtu, omtu);
401
fd = g_io_channel_unix_get_fd(chan);
403
if (avdtp && avdtp_stream) {
404
if (!avdtp_stream_set_transport(avdtp_stream, fd, imtu, omtu)) {
405
printf("avdtp_stream_set_transport: failed\n");
406
g_main_loop_quit(mainloop);
409
g_io_channel_set_close_on_unref(chan, FALSE);
416
avdtp = avdtp_new(fd, imtu, omtu, version, lseps);
418
printf("Failed to create avdtp instance\n");
419
g_main_loop_quit(mainloop);
423
avdtp_add_disconnect_cb(avdtp, disconnect_cb, NULL);
428
ret = avdtp_discover(avdtp, discover_cb, NULL);
430
printf("avdtp_discover failed: %s", strerror(-ret));
431
g_main_loop_quit(mainloop);
436
static GIOChannel *do_connect(GError **err)
439
return bt_io_connect(connect_cb, NULL, NULL, err,
440
BT_IO_OPT_SOURCE_BDADDR, &src,
441
BT_IO_OPT_DEST_BDADDR, &dst,
442
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
443
BT_IO_OPT_PSM, AVDTP_PSM,
447
return bt_io_connect(connect_cb, NULL, NULL, err,
448
BT_IO_OPT_SOURCE_BDADDR, &src,
449
BT_IO_OPT_DEST_BDADDR, &dst,
450
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
451
BT_IO_OPT_PSM, AVDTP_PSM,
455
static void open_cfm(struct avdtp *session, struct avdtp_local_sep *lsep,
456
struct avdtp_stream *stream, struct avdtp_error *err,
461
printf("%s\n", __func__);
465
printf("connect failed: %s\n", gerr->message);
467
g_main_loop_quit(mainloop);
471
static void start_cfm(struct avdtp *session, struct avdtp_local_sep *lsep,
472
struct avdtp_stream *stream, struct avdtp_error *err,
475
printf("%s\n", __func__);
477
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
478
start_media_player();
480
start_media_recorder();
483
static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *lsep,
484
struct avdtp_stream *stream,
485
struct avdtp_error *err, void *user_data)
487
printf("%s\n", __func__);
489
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
492
stop_media_recorder();
495
static void close_cfm(struct avdtp *session, struct avdtp_local_sep *lsep,
496
struct avdtp_stream *stream,
497
struct avdtp_error *err, void *user_data)
499
printf("%s\n", __func__);
501
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
504
stop_media_recorder();
509
static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *lsep,
510
struct avdtp_stream *stream,
511
struct avdtp_error *err, void *user_data)
513
printf("%s\n", __func__);
515
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
518
stop_media_recorder();
523
static void reconfigure_cfm(struct avdtp *session,
524
struct avdtp_local_sep *lsep,
525
struct avdtp_stream *stream,
526
struct avdtp_error *err, void *user_data)
528
printf("%s\n", __func__);
531
static void delay_report_cfm(struct avdtp *session,
532
struct avdtp_local_sep *lsep,
533
struct avdtp_stream *stream,
534
struct avdtp_error *err, void *user_data)
536
printf("%s\n", __func__);
539
static struct avdtp_sep_cfm sep_cfm = {
540
.set_configuration = set_configuration_cfm,
541
.get_configuration = get_configuration_cfm,
544
.suspend = suspend_cfm,
547
.reconfigure = reconfigure_cfm,
548
.delay_report = delay_report_cfm,
551
static gboolean get_capability_ind(struct avdtp *session,
552
struct avdtp_local_sep *sep,
553
GSList **caps, uint8_t *err,
556
struct avdtp_service_capability *service;
559
printf("%s\n", __func__);
562
g_source_remove(idle_id);
571
service = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0);
572
*caps = g_slist_append(*caps, service);
574
service = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, sbc_codec,
576
*caps = g_slist_append(*caps, service);
579
for (i = 0; i < 10; i++) {
580
service = avdtp_service_cap_new(AVDTP_MEDIA_CODEC,
583
*caps = g_slist_append(*caps, service);
589
static gboolean set_configuration_ind(struct avdtp *session,
590
struct avdtp_local_sep *lsep,
591
struct avdtp_stream *stream,
593
avdtp_set_configuration_cb cb,
596
printf("%s\n", __func__);
602
g_source_remove(idle_id);
606
avdtp_stream = stream;
608
cb(session, stream, NULL);
615
static gboolean get_configuration_ind(struct avdtp *session,
616
struct avdtp_local_sep *lsep,
617
uint8_t *err, void *user_data)
619
printf("%s\n", __func__);
627
static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *lsep,
628
struct avdtp_stream *stream, uint8_t *err,
631
printf("%s\n", __func__);
641
static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *lsep,
642
struct avdtp_stream *stream, uint8_t *err,
645
printf("%s\n", __func__);
650
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
651
start_media_player();
653
start_media_recorder();
660
static gboolean suspend_ind(struct avdtp *session,
661
struct avdtp_local_sep *sep,
662
struct avdtp_stream *stream, uint8_t *err,
665
printf("%s\n", __func__);
670
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
673
stop_media_recorder();
678
static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
679
struct avdtp_stream *stream, uint8_t *err,
682
printf("%s\n", __func__);
687
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
690
stop_media_recorder();
697
static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
698
struct avdtp_stream *stream, uint8_t *err,
701
printf("%s\n", __func__);
703
if (dev_role == AVDTP_SEP_TYPE_SOURCE)
706
stop_media_recorder();
711
static gboolean reconfigure_ind(struct avdtp *session,
712
struct avdtp_local_sep *lsep,
713
uint8_t *err, void *user_data)
715
printf("%s\n", __func__);
723
static gboolean delayreport_ind(struct avdtp *session,
724
struct avdtp_local_sep *lsep,
725
uint8_t rseid, uint16_t delay,
726
uint8_t *err, void *user_data)
728
printf("%s\n", __func__);
736
static struct avdtp_sep_ind sep_ind = {
737
.get_capability = get_capability_ind,
738
.set_configuration = set_configuration_ind,
739
.get_configuration = get_configuration_ind,
743
.suspend = suspend_ind,
745
.reconfigure = reconfigure_ind,
746
.delayreport = delayreport_ind,
749
static void usage(void)
751
printf("avdtptest - AVDTP testing ver %s\n", VERSION);
753
"\tavdtptest [options]\n");
755
"\t-d <device_role> SRC (source) or SINK (sink)\n"
756
"\t-i <hcidev> HCI adapter\n"
757
"\t-c <bdaddr> connect\n"
759
"\t-r reject commands\n"
761
"\t-p configure stream\n"
762
"\t-s <command> send command\n"
763
"\t-v <version> set version (0x0100, 0x0102, 0x0103\n");
766
static struct option main_options[] = {
767
{ "help", 0, 0, 'h' },
768
{ "device_role", 1, 0, 'd' },
769
{ "adapter", 1, 0, 'i' },
770
{ "connect", 1, 0, 'c' },
771
{ "listen", 0, 0, 'l' },
772
{ "reject", 0, 0, 'r' },
773
{ "fragment", 0, 0, 'f' },
774
{ "preconf", 0, 0, 'p' },
775
{ "send", 1, 0, 's' },
776
{ "version", 1, 0, 'v' },
780
static GIOChannel *do_listen(GError **err)
783
return bt_io_listen(connect_cb, NULL, NULL, NULL, err,
784
BT_IO_OPT_SOURCE_BDADDR, &src,
785
BT_IO_OPT_PSM, AVDTP_PSM,
786
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
790
return bt_io_listen(connect_cb, NULL, NULL, NULL, err,
791
BT_IO_OPT_SOURCE_BDADDR, &src,
792
BT_IO_OPT_PSM, AVDTP_PSM,
793
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
797
int main(int argc, char *argv[])
802
bacpy(&src, BDADDR_ANY);
803
bacpy(&dst, BDADDR_ANY);
805
mainloop = g_main_loop_new(NULL, FALSE);
807
printf("Failed to create main loop\n");
812
while ((opt = getopt_long(argc, argv, "d:hi:s:c:v:lrfp",
813
main_options, NULL)) != EOF) {
816
if (!strncmp(optarg, "hci", 3))
817
hci_devba(atoi(optarg + 3), &src);
819
str2ba(optarg, &src);
822
if (!strncasecmp(optarg, "SRC", sizeof("SRC"))) {
823
dev_role = AVDTP_SEP_TYPE_SOURCE;
824
} else if (!strncasecmp(optarg, "SINK",
826
dev_role = AVDTP_SEP_TYPE_SINK;
833
if (str2ba(optarg, &dst) < 0) {
839
bacpy(&dst, BDADDR_ANY);
851
parse_command(optarg);
854
version = strtol(optarg, NULL, 0);
855
if (version != 0x0100 && version != 0x0102 &&
857
printf("invalid version\n");
873
local_sep = avdtp_register_sep(lseps, dev_role, AVDTP_MEDIA_TYPE_AUDIO,
874
0x00, TRUE, &sep_ind, &sep_cfm, NULL);
876
printf("Failed to register sep\n");
880
queue_push_tail(lseps, local_sep);
882
if (!bacmp(&dst, BDADDR_ANY)) {
883
printf("Listening...\n");
884
io = do_listen(&err);
886
printf("Connecting...\n");
887
io = do_connect(&err);
891
printf("Failed: %s\n", err->message);
896
g_main_loop_run(mainloop);
900
queue_destroy(lseps, NULL);
905
g_main_loop_unref(mainloop);