1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2005-2010  Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <stdio.h>
29#include <errno.h>
30#include <stdlib.h>
31#include <getopt.h>
32#include <signal.h>
33#include <sys/socket.h>
34
35#include <bluetooth/bluetooth.h>
36#include <bluetooth/hci.h>
37#include <bluetooth/hci_lib.h>
38#include <bluetooth/sdp.h>
39#include <bluetooth/sdp_lib.h>
40
41static volatile sig_atomic_t __io_finished = 0;
42
43static void callback(uint8_t type, uint16_t status,
44				uint8_t *rsp, size_t size, void *udata)
45{
46	unsigned int i;
47
48	for (i = 0; i < size; i++) {
49		printf("%02x ", rsp[i]);
50		if ((i + 1) % 8 == 0)
51			printf(" ");
52		if ((i + 1) % 16 == 0)
53			printf("\n");
54	}
55	printf("\n");
56
57	__io_finished = 1;
58}
59
60static void cmd_search(bdaddr_t *src, bdaddr_t *dst)
61{
62	sdp_session_t *session;
63	sdp_list_t *search, *attrids;
64	uint32_t range = 0x0000ffff;
65	uuid_t uuid;
66
67	session = sdp_connect(src, dst, 0);
68	if (!session) {
69		perror("Can't connect to SDP service");
70		exit(1);
71	}
72
73	sdp_set_notify(session, callback, NULL);
74
75	sdp_uuid16_create(&uuid, PUBLIC_BROWSE_GROUP);
76
77	search = sdp_list_append(NULL, &uuid);
78
79	attrids = sdp_list_append(NULL, &range);
80
81	//sdp_service_search_attr_async(session, search,
82	//				SDP_ATTR_REQ_RANGE, attrids);
83
84	sdp_service_search_async(session, search, 0xffff);
85
86	sdp_list_free(attrids, NULL);
87
88	sdp_list_free(search, NULL);
89
90	while (!__io_finished)
91		sdp_process(session);
92
93	sdp_close(session);
94}
95
96static void usage(void)
97{
98	printf("sdptest - Utility for SDP testing\n\n");
99	printf("Usage:\n"
100		"\tsdptest [-i <dev>] <bdaddr>\n");
101}
102
103static struct option main_options[] = {
104	{ "device",	1, 0, 'i' },
105	{ "help",	0, 0, 'h' },
106	{ 0, 0, 0, 0 }
107};
108
109int main(int argc, char *argv[])
110{
111	bdaddr_t src, dst;
112	int opt;
113
114	bacpy(&src, BDADDR_ANY);
115
116	while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
117		switch (opt) {
118		case 'i':
119			if (!strncasecmp(optarg, "hci", 3))
120				hci_devba(atoi(optarg + 3), &src);
121			else
122				str2ba(optarg, &dst);
123			break;
124
125		case 'h':
126		default:
127			usage();
128			exit(0);
129		}
130	}
131
132	argc -= optind;
133	argv += optind;
134	optind = 0;
135
136	if (argc < 1) {
137		usage();
138		exit(1);
139	}
140
141	str2ba(argv[0], &dst);
142
143	cmd_search(&src, &dst);
144
145	return 0;
146}
147