mgmt: Use a common structure for powered, discoverable and connectable
The events, commands and responses to these modes are represented by
identical management messages. By having a unified struct for all of
them quite a lot of code can simplified and reused.
diff --git a/lib/mgmt.h b/lib/mgmt.h
index 1af8279..3657f71 100644
--- a/lib/mgmt.h
+++ b/lib/mgmt.h
@@ -68,35 +68,16 @@
uint16_t hci_rev;
} __packed;
+struct mgmt_mode {
+ uint16_t index;
+ uint8_t val;
+} __packed;
+
#define MGMT_OP_SET_POWERED 0x0005
-struct mgmt_cp_set_powered {
- uint16_t index;
- uint8_t powered;
-} __packed;
-struct mgmt_rp_set_powered {
- uint16_t index;
- uint8_t powered;
-} __packed;
#define MGMT_OP_SET_DISCOVERABLE 0x0006
-struct mgmt_cp_set_discoverable {
- uint16_t index;
- uint8_t discoverable;
-} __packed;
-struct mgmt_rp_set_discoverable {
- uint16_t index;
- uint8_t discoverable;
-} __packed;
#define MGMT_OP_SET_CONNECTABLE 0x0007
-struct mgmt_cp_set_connectable {
- uint16_t index;
- uint8_t connectable;
-} __packed;
-struct mgmt_rp_set_connectable {
- uint16_t index;
- uint8_t connectable;
-} __packed;
#define MGMT_EV_CMD_COMPLETE 0x0001
struct mgmt_ev_cmd_complete {
@@ -127,19 +108,7 @@
} __packed;
#define MGMT_EV_POWERED 0x0006
-struct mgmt_ev_powered {
- uint16_t index;
- uint8_t powered;
-} __packed;
#define MGMT_EV_DISCOVERABLE 0x0007
-struct mgmt_ev_discoverable {
- uint16_t index;
- uint8_t discoverable;
-} __packed;
#define MGMT_EV_CONNECTABLE 0x0008
-struct mgmt_ev_connectable {
- uint16_t index;
- uint8_t connectable;
-} __packed;
diff --git a/plugins/mgmtops.c b/plugins/mgmtops.c
index 3064074..b3fb418 100644
--- a/plugins/mgmtops.c
+++ b/plugins/mgmtops.c
@@ -172,20 +172,18 @@
remove_controller(index);
}
-static int mgmt_set_connectable(int index, gboolean connectable)
+static int mgmt_set_mode(int index, uint16_t opcode, uint8_t val)
{
- char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_connectable)];
+ char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_mode)];
struct mgmt_hdr *hdr = (void *) buf;
- struct mgmt_cp_set_connectable *cp = (void *) &buf[sizeof(*hdr)];
-
- DBG("index %d connectable %d", index, connectable);
+ struct mgmt_mode *cp = (void *) &buf[sizeof(*hdr)];
memset(buf, 0, sizeof(buf));
- hdr->opcode = MGMT_OP_SET_CONNECTABLE;
+ hdr->opcode = opcode;
hdr->len = htobs(sizeof(*cp));
cp->index = htobs(index);
- cp->connectable = connectable;
+ cp->val = val;
if (write(mgmt_sock, buf, sizeof(buf)) < 0)
return -errno;
@@ -193,25 +191,16 @@
return 0;
}
+static int mgmt_set_connectable(int index, gboolean connectable)
+{
+ DBG("index %d connectable %d", index, connectable);
+ return mgmt_set_mode(index, MGMT_OP_SET_CONNECTABLE, connectable);
+}
+
static int mgmt_set_discoverable(int index, gboolean discoverable)
{
- char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_discoverable)];
- struct mgmt_hdr *hdr = (void *) buf;
- struct mgmt_cp_set_discoverable *cp = (void *) &buf[sizeof(*hdr)];
-
DBG("index %d discoverable %d", index, discoverable);
-
- memset(buf, 0, sizeof(buf));
- hdr->opcode = MGMT_OP_SET_DISCOVERABLE;
- hdr->len = htobs(sizeof(*cp));
-
- cp->index = htobs(index);
- cp->discoverable = discoverable;
-
- if (write(mgmt_sock, buf, sizeof(buf)) < 0)
- return -errno;
-
- return 0;
+ return mgmt_set_mode(index, MGMT_OP_SET_DISCOVERABLE, discoverable);
}
static int mgmt_set_pairable(int index, gboolean pairable)
@@ -279,7 +268,7 @@
static void mgmt_powered(int sk, void *buf, size_t len)
{
- struct mgmt_ev_powered *ev = buf;
+ struct mgmt_mode *ev = buf;
uint16_t index;
if (len < sizeof(*ev)) {
@@ -289,14 +278,14 @@
index = btohs(bt_get_unaligned(&ev->index));
- DBG("Controller %u powered %u", index, ev->powered);
+ DBG("Controller %u powered %u", index, ev->val);
- mgmt_update_mode(index, ev->powered);
+ mgmt_update_mode(index, ev->val);
}
static void mgmt_discoverable(int sk, void *buf, size_t len)
{
- struct mgmt_ev_discoverable *ev = buf;
+ struct mgmt_mode *ev = buf;
struct controller_info *info;
struct btd_adapter *adapter;
uint16_t index;
@@ -309,7 +298,7 @@
index = btohs(bt_get_unaligned(&ev->index));
- DBG("Controller %u discoverable %u", index, ev->discoverable);
+ DBG("Controller %u discoverable %u", index, ev->val);
if (index > max_index) {
error("Unexpected index %u in discoverable event", index);
@@ -318,7 +307,7 @@
info = &controllers[index];
- info->discoverable = ev->discoverable ? TRUE : FALSE;
+ info->discoverable = ev->val ? TRUE : FALSE;
adapter = manager_find_adapter(&info->bdaddr);
if (!adapter)
@@ -337,7 +326,7 @@
static void mgmt_connectable(int sk, void *buf, size_t len)
{
- struct mgmt_ev_connectable *ev = buf;
+ struct mgmt_mode *ev = buf;
struct controller_info *info;
struct btd_adapter *adapter;
uint16_t index;
@@ -350,7 +339,7 @@
index = btohs(bt_get_unaligned(&ev->index));
- DBG("Controller %u connectable %u", index, ev->connectable);
+ DBG("Controller %u connectable %u", index, ev->val);
if (index > max_index) {
error("Unexpected index %u in connectable event", index);
@@ -359,7 +348,7 @@
info = &controllers[index];
- info->connectable = ev->connectable ? TRUE : FALSE;
+ info->connectable = ev->val ? TRUE : FALSE;
adapter = manager_find_adapter(&info->bdaddr);
if (!adapter)
@@ -406,23 +395,8 @@
static int mgmt_set_powered(int index, gboolean powered)
{
- char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_set_powered)];
- struct mgmt_hdr *hdr = (void *) buf;
- struct mgmt_cp_set_powered *cp = (void *) &buf[sizeof(*hdr)];
-
DBG("index %d powered %d", index, powered);
-
- memset(buf, 0, sizeof(buf));
- hdr->opcode = MGMT_OP_SET_POWERED;
- hdr->len = htobs(sizeof(*cp));
-
- cp->index = htobs(index);
- cp->powered = powered;
-
- if (write(mgmt_sock, buf, sizeof(buf)) < 0)
- return -errno;
-
- return 0;
+ return mgmt_set_mode(index, MGMT_OP_SET_POWERED, powered);
}
static void read_info_complete(int sk, void *buf, size_t len)
@@ -491,7 +465,7 @@
static void set_powered_complete(int sk, void *buf, size_t len)
{
- struct mgmt_rp_set_powered *rp = buf;
+ struct mgmt_mode *rp = buf;
uint16_t index;
if (len < sizeof(*rp)) {
@@ -501,14 +475,14 @@
index = btohs(bt_get_unaligned(&rp->index));
- DBG("hci%d powered %u", index, rp->powered);
+ DBG("hci%d powered %u", index, rp->val);
- mgmt_update_mode(index, rp->powered);
+ mgmt_update_mode(index, rp->val);
}
static void set_discoverable_complete(int sk, void *buf, size_t len)
{
- struct mgmt_rp_set_discoverable *rp = buf;
+ struct mgmt_mode *rp = buf;
struct controller_info *info;
struct btd_adapter *adapter;
uint16_t index;
@@ -521,7 +495,7 @@
index = btohs(bt_get_unaligned(&rp->index));
- DBG("hci%d discoverable %u", index, rp->discoverable);
+ DBG("hci%d discoverable %u", index, rp->val);
if (index > max_index) {
error("Unexpected index %u in discoverable complete", index);
@@ -530,7 +504,7 @@
info = &controllers[index];
- info->discoverable = rp->discoverable ? TRUE : FALSE;
+ info->discoverable = rp->val ? TRUE : FALSE;
adapter = manager_find_adapter(&info->bdaddr);
if (!adapter)
@@ -547,7 +521,7 @@
static void set_connectable_complete(int sk, void *buf, size_t len)
{
- struct mgmt_rp_set_connectable *rp = buf;
+ struct mgmt_mode *rp = buf;
struct controller_info *info;
struct btd_adapter *adapter;
uint16_t index;
@@ -559,7 +533,7 @@
index = btohs(bt_get_unaligned(&rp->index));
- DBG("hci%d connectable %u", index, rp->connectable);
+ DBG("hci%d connectable %u", index, rp->val);
if (index > max_index) {
error("Unexpected index %u in connectable complete", index);
@@ -568,11 +542,11 @@
info = &controllers[index];
- info->connectable = rp->connectable ? TRUE : FALSE;
+ info->connectable = rp->val ? TRUE : FALSE;
adapter = manager_find_adapter(&info->bdaddr);
if (adapter)
- adapter_mode_changed(adapter, rp->connectable ? SCAN_PAGE : 0);
+ adapter_mode_changed(adapter, rp->val ? SCAN_PAGE : 0);
}
static void mgmt_cmd_complete(int sk, void *buf, size_t len)