Skip to content

Commit 48b8201

Browse files
committed
bluetooth: signal devd on netgraph device events
Currently, devd emits events for external adapters only. Send Netgraph init/disconnect events to devd so the internal adapter's state could be asserted from userland. Signed-off-by: Kirill Orlov (-k) <slowdive@me.com>
1 parent 96f262d commit 48b8201

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

sbin/devd/devd.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,19 @@ notify 100 {
159159
action "/sbin/init q";
160160
};
161161

162+
# Track the bluetooth adapter's arrival/departure. This is useful
163+
# for internal adapters that do not emit regular device events.
164+
notify 100 {
165+
match "system" "BLUETOOTH";
166+
match "subsystem" "HCI";
167+
match "type" "INITIALIZED";
168+
action "logger 'Bluetooth adapter initialized: $*'";
169+
};
170+
notify 100 {
171+
match "system" "BLUETOOTH";
172+
match "subsystem" "HCI";
173+
match "type" "POWERED_OFF";
174+
action "logger 'Bluetooth adapter powered off: $*'";
175+
};
176+
162177
*/

sys/netgraph/bluetooth/hci/ng_hci_main.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434

3535
#include <sys/param.h>
3636
#include <sys/systm.h>
37+
#include <sys/types.h>
3738
#include <sys/kernel.h>
3839
#include <sys/endian.h>
3940
#include <sys/malloc.h>
4041
#include <sys/mbuf.h>
42+
#include <sys/sbuf.h>
4143
#include <sys/queue.h>
44+
#include <sys/devctl.h>
4245
#include <netgraph/ng_message.h>
4346
#include <netgraph/netgraph.h>
4447
#include <netgraph/ng_parse.h>
@@ -110,6 +113,19 @@ static int ng_hci_linktype_to_addrtype(int linktype)
110113
}
111114
return BDADDR_BREDR;
112115
}
116+
117+
/*
118+
* Format bdaddr_t as "xx:xx:xx:xx:xx:xx" into buf.
119+
*/
120+
121+
static void
122+
ng_hci_bdaddr_to_str(const bdaddr_t *ba, char buf[18])
123+
{
124+
snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
125+
ba->b[5], ba->b[4], ba->b[3],
126+
ba->b[2], ba->b[1], ba->b[0]);
127+
} /* ng_hci_bdaddr_to_str */
128+
113129
/*****************************************************************************
114130
*****************************************************************************
115131
** Netgraph methods implementation
@@ -268,6 +284,25 @@ ng_hci_disconnect(hook_p hook)
268284
/* Connection terminated by local host */
269285
ng_hci_unit_clean(unit, 0x16);
270286
unit->state &= ~(NG_HCI_UNIT_CONNECTED|NG_HCI_UNIT_INITED);
287+
288+
/* Signal power off to devd */
289+
{
290+
char bdstr[18];
291+
struct sbuf *sb;
292+
293+
sb = sbuf_new_auto();
294+
ng_hci_bdaddr_to_str(&unit->bdaddr, bdstr);
295+
sbuf_sprintf(sb, "node=%s bdaddr=%s\n",
296+
NG_NODE_NAME(NG_HOOK_NODE(hook)), bdstr);
297+
298+
if (sbuf_finish(sb) != 0) err(1, "Could not generate sbuf message");
299+
300+
transmit_msg(sbuf_data(sb), sbuf_len(sb));
301+
devctl_notify("BLUETOOTH", "HCI", "POWERED_OFF", sb->buf);
302+
sbuf_delete(sb);
303+
}
304+
305+
271306
} else
272307
return (EINVAL);
273308

@@ -371,6 +406,24 @@ ng_hci_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
371406

372407
ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
373408
ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
409+
410+
/* Signal init to devd */
411+
{
412+
char bdstr[18];
413+
struct sbuf *sb;
414+
415+
sb = sbuf_new_auto();
416+
ng_hci_bdaddr_to_str(&unit->bdaddr, bdstr);
417+
sbuf_sprintf(sb, "node=%s bdaddr=%s\n",
418+
NG_NODE_NAME(node), bdstr);
419+
420+
if (sbuf_finish(sb) != 0) err(1, "Could not generate sbuf message");
421+
422+
transmit_msg(sbuf_data(sb), sbuf_len(sb));
423+
devctl_notify("BLUETOOTH", "HCI", "INITIALIZED", sb->buf);
424+
sbuf_delete(sb);
425+
}
426+
374427
break;
375428

376429
/* Get node debug level */

0 commit comments

Comments
 (0)