Refactor the link state machine to switch statements for clarity
This commit is contained in:
parent
e0854ae1c9
commit
10382d6fd6
79
src/main.c
79
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,
|
DEVICE_INIT(hci_uart, "hci_uart", &h5_open, NULL, NULL,
|
||||||
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|
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) {
|
bool _link_ctrl_memcmp(struct net_buf const * buf, u8_t const * const ref) {
|
||||||
return !memcmp(buf->data, ref, 2);
|
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);
|
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) {
|
static void h5_send_sync(void) {
|
||||||
_send_link_control(sync_req, sizeof(sync_req));
|
_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) {
|
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();
|
h5_send_sync_response();
|
||||||
return UNINIT;
|
return UNINIT;
|
||||||
}
|
case H5_SYNC_RESPONSE:
|
||||||
if (packet_is_sync_response(buf)) {
|
|
||||||
h5_send_config();
|
h5_send_config();
|
||||||
LOG_DBG("RX'd SYNC RESPONSE: UNINIT -> INIT");
|
LOG_DBG("RX'd SYNC RESPONSE: UNINIT -> INIT");
|
||||||
return INIT;
|
return INIT;
|
||||||
} else {
|
default:
|
||||||
LOG_DBG("RX'd Incorrect Type: UNINIT -> UNINIT");
|
LOG_DBG("RX'd Incorrect Type: UNINIT -> UNINIT");
|
||||||
/* SYNC is the answer to any non-SYNC_RESP packets in UNINIT state */
|
/* SYNC is the answer to any non-SYNC_RESP packets in UNINIT state */
|
||||||
h5_send_sync();
|
h5_send_sync();
|
||||||
|
@ -722,49 +743,44 @@ static linkstate_t do_uninit(struct net_buf const *buf) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static linkstate_t do_init(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_win = conf_rsp[2] & 0x7;
|
||||||
h5.tx_seq = 0;
|
h5.tx_seq = 0;
|
||||||
h5.tx_ack = 0;
|
h5.tx_ack = 0;
|
||||||
LOG_DBG("Finished H5 configuration, tx_win %u: INIT -> ACTIVE", h5.tx_win);
|
LOG_DBG("Finished H5 configuration, tx_win %u: INIT -> ACTIVE", h5.tx_win);
|
||||||
return ACTIVE;
|
return ACTIVE;
|
||||||
}
|
default:
|
||||||
|
|
||||||
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 {
|
|
||||||
LOG_DBG("RX'd Other: INIT -> INIT");
|
LOG_DBG("RX'd Other: INIT -> INIT");
|
||||||
}
|
|
||||||
return INIT;
|
return INIT;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static linkstate_t do_active(struct net_buf *buf) {
|
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");
|
LOG_DBG("Bad packet for ACTIVE state: ACTIVE -> UNINIT");
|
||||||
h5_send_sync();
|
h5_send_sync();
|
||||||
return UNINIT;
|
return UNINIT;
|
||||||
}
|
case H5_CONFIG:
|
||||||
|
|
||||||
if (packet_is_sync(buf)){
|
|
||||||
LOG_DBG("SYNC in ACTIVE state: ACTIVE -> UNINIT");
|
|
||||||
return UNINIT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (packet_is_config(buf)) {
|
|
||||||
h5_send_config_response();
|
h5_send_config_response();
|
||||||
LOG_DBG("RX'd CONFIG: ACTIVE -> ACTIVE");
|
LOG_DBG("RX'd CONFIG: ACTIVE -> ACTIVE");
|
||||||
}
|
return ACTIVE;
|
||||||
|
case H5_CONFIG_RESPONSE:
|
||||||
if (packet_is_config_response(buf)) {
|
|
||||||
LOG_DBG("RX'd CONFIG RESPONSE: ACTIVE -> ACTIVE");
|
LOG_DBG("RX'd CONFIG RESPONSE: ACTIVE -> ACTIVE");
|
||||||
}
|
return ACTIVE;
|
||||||
|
default:
|
||||||
// Presumably something from the controller
|
// Presumably something from the controller
|
||||||
if (bt_buf_get_type(buf) == BT_BUF_EVT) {
|
if (bt_buf_get_type(buf) == BT_BUF_EVT) {
|
||||||
LOG_DBG("RX'd CONTROLLER EVENT: ACTIVE -> ACTIVE");
|
LOG_DBG("RX'd CONTROLLER EVENT: ACTIVE -> ACTIVE");
|
||||||
|
@ -774,6 +790,7 @@ static linkstate_t do_active(struct net_buf *buf) {
|
||||||
}
|
}
|
||||||
return ACTIVE;
|
return ACTIVE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue