summaryrefslogtreecommitdiff
path: root/usr.sbin/btd/btd.h
blob: 335333e246cc5fef7da66da4b40c9f3d03506e36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*	$OpenBSD: btd.h,v 1.6 2008/11/27 00:51:17 uwe Exp $	*/

/*
 * Copyright (c) 2008 Uwe Stuehler <uwe@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>

#include <dev/bluetooth/btdev.h>
#include <netbt/bluetooth.h>
#include <netbt/hci.h>

#include <db.h>
#include <event.h>
#include <pwd.h>
#include <stdarg.h>

#define DEFAULT_INTERFACE_NAME "OpenBSD"

#define BTD_DB		"/var/db/btd.db"
#define BTD_SOCKET	"/var/run/btd.sock"
#define BTD_USER	"_btd"

/* XXX there is no need for more than one bthub device! */
#define BTHUB_PATH	"/dev/bthub0"

#define	READ_BUF_SIZE		8192

struct btd;

struct btd_db {
	DB *dbh;
};

struct bt_interface {
	TAILQ_ENTRY(bt_interface) entry;
	struct btd *env;
	struct hci_physif *physif;
	bdaddr_t addr;
	char name[HCI_UNIT_NAME_SIZE];
	int disabled;
	int flags;
#define BTIF_EXPLICIT		0x0001 /* listed in config */
	int changes;
#define BTIF_DELETED		0x0001
#define BTIF_NAME_CHANGED	0x0002
};

struct bt_devinfo {
	/* XXX don't tie it to a kernel structure */
	struct btdev_attach_args baa;
};

struct bt_device {
	TAILQ_ENTRY(bt_device) entry;
	struct btd *env;
	bdaddr_t addr;
	uint16_t type;
	uint8_t pin[HCI_PIN_SIZE];
	uint8_t pin_size;
	int flags;
	struct bt_devinfo info;	/* filled in from database or SDP */
};

#define BTDF_ATTACH		0x0001	/* try attaching a driver */
#define BTDF_ATTACH_DONE	0x0002	/* driver is attached */
#define BTDF_SDP_STARTED	0x0004	/* SDP query is running */
#define BTDF_SDP_DONE		0x0008	/* SDP query is done */
#define BTDF_DEVINFO_VALID	0x0010	/* got device information */
#define BTDF_DELETED		0x0020	/* device deleted in config */

struct btd {
	int debug;
	struct btd_db db;
	struct hci_state *hci;
	struct sdp_state *sdp;
	TAILQ_HEAD(interfaces, bt_interface) interfaces;
	TAILQ_HEAD(devices, bt_device) devices;
};

/* ipc messages */

enum imsg_type {
	IMSG_NONE,
	IMSG_CONFIG_BEGIN,
	IMSG_CONFIG_INTERFACE,
	IMSG_CONFIG_DEVICE,
	IMSG_CONFIG_COMMIT,
	IMSG_CONFIG_ROLLBACK,
	IMSG_OPEN_HCI,
	IMSG_SET_LINK_POLICY,
	IMSG_SET_INTERFACE_FLAGS,
	IMSG_ATTACH,
	IMSG_DETACH
};

/* prototypes */

/* atomic.c */
int atomic_read(int, void *, size_t);
int atomic_write(int, const void *, size_t);

/* bt.c */
extern int priv_fd;
pid_t bt_main(int[2], struct btd *, struct passwd *);
void bt_priv_msg(enum imsg_type);
void bt_priv_send(const void *, size_t);
void bt_priv_recv(void *, size_t);
void bt_devices_changed(void);
int bt_set_interface_flags(const struct btreq *);

/* bt_subr.c */
char const *bt_ntoa(bdaddr_t const *, char[18]);
int bt_aton(char const *, bdaddr_t *);

/* conf.c */
struct btd *conf_new(void);
void conf_delete(struct btd *);

struct bt_interface *conf_add_interface(struct btd *, const bdaddr_t *);
struct bt_interface *conf_find_interface(const struct btd *, const bdaddr_t *);
void conf_delete_interface(struct bt_interface *);

struct bt_device *conf_add_device(struct btd *, const bdaddr_t *);
struct bt_device *conf_find_device(const struct btd *, const bdaddr_t *);
void conf_delete_device(struct bt_device *);

void conf_lookup_pin(const struct btd *, const bdaddr_t *,
    uint8_t[HCI_PIN_SIZE], uint8_t *);

void conf_dump(const struct btd *);

/* control.c */
void control_init(struct btd *);
void control_cleanup(void);

/* db.c */
void db_open(const char *, struct btd_db *);
int db_put_link_key(struct btd_db *, const bdaddr_t *, const uint8_t *);
int db_get_link_key(struct btd_db *, const bdaddr_t *, uint8_t *);
int db_put_devinfo(struct btd_db *, const bdaddr_t *,
    const struct bt_devinfo *);
int db_get_devinfo(struct btd_db *, const bdaddr_t *, struct bt_devinfo *);
void db_dump(struct btd_db *);

/* devinfo.c */
bdaddr_t *devinfo_laddr(struct bt_devinfo *);
bdaddr_t *devinfo_raddr(struct bt_devinfo *);
int devinfo_load(struct bt_devinfo *, void *, size_t);
void devinfo_unload(struct bt_devinfo *);
int devinfo_store(const struct bt_devinfo *, void **, size_t *);
int devinfo_load_attach_args(struct btdev_attach_args *, void *, size_t);
void devinfo_unload_attach_args(struct btdev_attach_args *);
void devinfo_dump(const struct bt_devinfo *);

/* fdpass.c */
void send_fd(int, int);
int receive_fd(int);

/* hci.c */
void hci_init(struct btd *);
int hci_reinit(struct btd *, const struct btd *);

/* log.c */
extern int debug;
void log_init(int);
void vlog(int, const char *, va_list);
void log_warn(const char *, ...);
void log_warnx(const char *, ...);
void log_info(const char *, ...);
void log_debug(const char *, ...);
void log_packet(const bdaddr_t *, const bdaddr_t *, const char *, ...);
void fatal(const char *);
void fatalx(const char *);

/* sdp.c */
void sdp_init(struct btd *);
int sdp_get_devinfo(struct bt_interface *, struct bt_device *);

/* util.c */
time_t getmonotime(void);