Refactor the link state machine to switch statements for clarity
This commit is contained in:
parent
e0854ae1c9
commit
10382d6fd6
93
src/main.c
93
src/main.c
|
@ -630,6 +630,14 @@ void bt_ctlr_assert_handle(char *file, u32_t line)
|
|||
DEVICE_INIT(hci_uart, "hci_uart", &h5_open, NULL, NULL,
|
||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|
||||
|
||||
enum h5_pkt_types {
|
||||
H5_UNKNOWN = -1,
|
||||
H5_SYNC,
|
||||
H5_SYNC_RESPONSE,
|
||||
H5_CONFIG,
|
||||
H5_CONFIG_RESPONSE,
|
||||
};
|
||||
|
||||
bool _link_ctrl_memcmp(struct net_buf const * buf, u8_t const * const ref) {
|
||||
return !memcmp(buf->data, ref, 2);
|
||||
}
|
||||
|
@ -654,6 +662,19 @@ static void _send_link_control(u8_t const * const buf, u8_t len) {
|
|||
h5_send(buf, HCI_3WIRE_LINK_PKT, len);
|
||||
}
|
||||
|
||||
int packet_get_type(struct net_buf const * buf) {
|
||||
if (packet_is_sync(buf)){
|
||||
return H5_SYNC;
|
||||
} else if (packet_is_sync_response(buf)) {
|
||||
return H5_SYNC_RESPONSE;
|
||||
} else if (packet_is_config(buf)) {
|
||||
return H5_CONFIG;
|
||||
} else if (packet_is_config_response(buf)) {
|
||||
return H5_CONFIG_RESPONSE;
|
||||
}
|
||||
return H5_UNKNOWN;
|
||||
}
|
||||
|
||||
static void h5_send_sync(void) {
|
||||
_send_link_control(sync_req, sizeof(sync_req));
|
||||
}
|
||||
|
@ -705,15 +726,15 @@ void gpio_init() {
|
|||
}
|
||||
|
||||
static linkstate_t do_uninit(struct net_buf const *buf) {
|
||||
if (packet_is_sync(buf)) {
|
||||
switch (packet_get_type(buf)) {
|
||||
case H5_SYNC:
|
||||
h5_send_sync_response();
|
||||
return UNINIT;
|
||||
}
|
||||
if (packet_is_sync_response(buf)) {
|
||||
case H5_SYNC_RESPONSE:
|
||||
h5_send_config();
|
||||
LOG_DBG("RX'd SYNC RESPONSE: UNINIT -> INIT");
|
||||
return INIT;
|
||||
} else {
|
||||
default:
|
||||
LOG_DBG("RX'd Incorrect Type: UNINIT -> UNINIT");
|
||||
/* SYNC is the answer to any non-SYNC_RESP packets in UNINIT state */
|
||||
h5_send_sync();
|
||||
|
@ -722,57 +743,53 @@ static linkstate_t do_uninit(struct net_buf const *buf) {
|
|||
}
|
||||
|
||||
static linkstate_t do_init(struct net_buf const *buf) {
|
||||
if (packet_is_config_response(buf)) {
|
||||
switch (packet_get_type(buf)) {
|
||||
case H5_SYNC:
|
||||
LOG_DBG("RX'd SYNC: INIT -> INIT");
|
||||
h5_send_sync_response();
|
||||
return INIT;
|
||||
case H5_CONFIG:
|
||||
LOG_DBG("RX'd CONFIG: INIT -> INIT");
|
||||
h5_send_config_response();
|
||||
return INIT;
|
||||
case H5_CONFIG_RESPONSE:
|
||||
h5.tx_win = conf_rsp[2] & 0x7;
|
||||
h5.tx_seq = 0;
|
||||
h5.tx_ack = 0;
|
||||
LOG_DBG("Finished H5 configuration, tx_win %u: INIT -> ACTIVE", h5.tx_win);
|
||||
return ACTIVE;
|
||||
}
|
||||
|
||||
if (packet_is_sync(buf)) {
|
||||
LOG_DBG("RX'd SYNC: INIT -> INIT");
|
||||
h5_send_sync_response();
|
||||
}
|
||||
|
||||
if (packet_is_config(buf)) {
|
||||
LOG_DBG("RX'd CONFIG: INIT -> INIT");
|
||||
h5_send_config_response();
|
||||
} else {
|
||||
default:
|
||||
LOG_DBG("RX'd Other: INIT -> INIT");
|
||||
return INIT;
|
||||
}
|
||||
return INIT;
|
||||
}
|
||||
|
||||
static linkstate_t do_active(struct net_buf *buf) {
|
||||
if (packet_is_sync_response(buf)) {
|
||||
switch (packet_get_type(buf)) {
|
||||
case H5_SYNC:
|
||||
LOG_DBG("SYNC in ACTIVE state: ACTIVE -> UNINIT");
|
||||
return UNINIT;
|
||||
case H5_SYNC_RESPONSE:
|
||||
LOG_DBG("Bad packet for ACTIVE state: ACTIVE -> UNINIT");
|
||||
h5_send_sync();
|
||||
return UNINIT;
|
||||
}
|
||||
|
||||
if (packet_is_sync(buf)){
|
||||
LOG_DBG("SYNC in ACTIVE state: ACTIVE -> UNINIT");
|
||||
return UNINIT;
|
||||
}
|
||||
|
||||
if (packet_is_config(buf)) {
|
||||
case H5_CONFIG:
|
||||
h5_send_config_response();
|
||||
LOG_DBG("RX'd CONFIG: ACTIVE -> ACTIVE");
|
||||
}
|
||||
|
||||
if (packet_is_config_response(buf)) {
|
||||
return ACTIVE;
|
||||
case H5_CONFIG_RESPONSE:
|
||||
LOG_DBG("RX'd CONFIG RESPONSE: ACTIVE -> ACTIVE");
|
||||
return ACTIVE;
|
||||
default:
|
||||
// Presumably something from the controller
|
||||
if (bt_buf_get_type(buf) == BT_BUF_EVT) {
|
||||
LOG_DBG("RX'd CONTROLLER EVENT: ACTIVE -> ACTIVE");
|
||||
h5_send(buf->data, HCI_EVENT_PKT, buf->len);
|
||||
} else {
|
||||
LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue");
|
||||
}
|
||||
return ACTIVE;
|
||||
}
|
||||
|
||||
// Presumably something from the controller
|
||||
if (bt_buf_get_type(buf) == BT_BUF_EVT) {
|
||||
LOG_DBG("RX'd CONTROLLER EVENT: ACTIVE -> ACTIVE");
|
||||
h5_send(buf->data, HCI_EVENT_PKT, buf->len);
|
||||
} else {
|
||||
LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue");
|
||||
}
|
||||
return ACTIVE;
|
||||
}
|
||||
|
||||
void main(void)
|
||||
|
|
Loading…
Reference in New Issue