Bluetooth: RFCOMM: Define enum and macros for role and CR

This replaces initiator flag of session and dlc to enum which has
two values INITIATOR and ACCEPTOR.

Also this defines macros for CRs in header. Basically there are
three types fo CRs. Frame header CR has different meaning for
UIH and non UIH packets. Also this renames the existing msg hdr
CR to make it consistent.

These changes are basically done to make it more readable

Change-Id: Ic15e93465b0afbd19d8805f27d7a43f34ef38689
Signed-off-by: Jaganath Kanakkassery <jaganathx.kanakkassery@intel.com>
This commit is contained in:
Jaganath Kanakkassery 2016-10-27 21:08:45 +05:30 committed by Johan Hedberg
commit 2eba256fd2
3 changed files with 49 additions and 19 deletions

View file

@ -75,6 +75,13 @@ struct bt_rfcomm_dlc_ops {
void (*recv)(struct bt_rfcomm_dlc *dlc, struct net_buf *buf);
};
/** @brief Role of RFCOMM session and dlc. Used only by internal APIs
*/
typedef enum bt_rfcomm_role {
BT_RFCOMM_ROLE_ACCEPTOR,
BT_RFCOMM_ROLE_INITIATOR
} __packed bt_rfcomm_role_t;
/** @brief RFCOMM DLC structure. */
struct bt_rfcomm_dlc {
/* Queue for outgoing data */
@ -88,12 +95,12 @@ struct bt_rfcomm_dlc {
struct bt_rfcomm_dlc *_next;
bt_security_t required_sec_level;
bt_rfcomm_role_t role;
uint16_t mtu;
uint8_t dlci;
uint8_t state;
uint8_t rx_credit;
bool initiator;
/* Stack for TX fiber */
BT_STACK(stack, 128);

View file

@ -317,7 +317,7 @@ static struct net_buf *rfcomm_make_uih_msg(struct bt_rfcomm_dlc *dlc,
}
hdr = net_buf_add(buf, sizeof(*hdr));
hdr_cr = dlc->session->initiator ? 1 : 0;
hdr_cr = BT_RFCOMM_UIH_CR(dlc->session->role);
hdr->address = BT_RFCOMM_SET_ADDR(0, hdr_cr);
hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UIH, 0);
hdr->length = BT_RFCOMM_SET_LEN_8(sizeof(*msg_hdr) + len);
@ -382,7 +382,7 @@ static struct bt_rfcomm_dlc *rfcomm_dlc_accept(struct bt_rfcomm_session *session
dlc->dlci = dlci;
dlc->session = session;
dlc->initiator = false;
dlc->role = BT_RFCOMM_ROLE_ACCEPTOR;
dlc->rx_credit = RFCOMM_DEFAULT_CREDIT;
dlc->state = BT_RFCOMM_STATE_INIT;
dlc->mtu = min(dlc->mtu, session->mtu);
@ -406,7 +406,7 @@ static int rfcomm_send_dm(struct bt_rfcomm_session *session, uint8_t dlci)
}
hdr = net_buf_add(buf, sizeof(*hdr));
cr = session->initiator ? 0 : 1;
cr = BT_RFCOMM_RESP_CR(session->role);
hdr->address = BT_RFCOMM_SET_ADDR(dlci, cr);
hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_DM, 1);
hdr->length = BT_RFCOMM_SET_LEN_8(0);
@ -472,7 +472,7 @@ static int rfcomm_send_ua(struct bt_rfcomm_session *session, uint8_t dlci)
}
hdr = net_buf_add(buf, sizeof(*hdr));
cr = session->initiator ? 0 : 1;
cr = BT_RFCOMM_RESP_CR(session->role);
hdr->address = BT_RFCOMM_SET_ADDR(dlci, cr);
hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UA, 1);
hdr->length = BT_RFCOMM_SET_LEN_8(0);
@ -509,7 +509,7 @@ static void rfcomm_dlc_connected(struct bt_rfcomm_dlc *dlc)
{
dlc->state = BT_RFCOMM_STATE_CONNECTED;
rfcomm_send_msc(dlc, BT_RFCOMM_MSG_CMD);
rfcomm_send_msc(dlc, BT_RFCOMM_MSG_CMD_CR);
nano_fifo_init(&dlc->tx_queue);
fiber_start(dlc->stack, sizeof(dlc->stack), rfcomm_dlc_tx_fiber,
@ -554,7 +554,7 @@ static void rfcomm_dlc_drop(struct bt_rfcomm_dlc *dlc)
{
BT_DBG("dlc %p", dlc);
if (!dlc->initiator) {
if (dlc->role == BT_RFCOMM_ROLE_ACCEPTOR) {
rfcomm_send_dm(dlc->session, dlc->dlci);
}
rfcomm_dlcs_remove_dlci(dlc->session->dlcs, dlc->dlci);
@ -564,7 +564,7 @@ static void rfcomm_dlc_drop(struct bt_rfcomm_dlc *dlc)
static void rfcomm_handle_sabm(struct bt_rfcomm_session *session, uint8_t dlci)
{
if (!dlci) {
session->initiator = false;
session->role = BT_RFCOMM_ROLE_ACCEPTOR;
if (rfcomm_send_ua(session, dlci) < 0) {
return;
@ -644,7 +644,7 @@ static int rfcomm_send_credit(struct bt_rfcomm_dlc *dlc, uint8_t credits)
{
struct bt_rfcomm_hdr *hdr;
struct net_buf *buf;
uint8_t fcs;
uint8_t fcs, cr;
BT_DBG("Dlc %p credits %d", dlc, credits);
@ -655,7 +655,8 @@ static int rfcomm_send_credit(struct bt_rfcomm_dlc *dlc, uint8_t credits)
}
hdr = net_buf_add(buf, sizeof(*hdr));
hdr->address = BT_RFCOMM_SET_ADDR(dlc->dlci, dlc->session->initiator);
cr = BT_RFCOMM_UIH_CR(dlc->session->role);
hdr->address = BT_RFCOMM_SET_ADDR(dlc->dlci, cr);
hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UIH, BT_RFCOMM_PF_CREDIT);
hdr->length = BT_RFCOMM_SET_LEN_8(0);
net_buf_add_u8(buf, credits);
@ -679,8 +680,8 @@ static void rfcomm_handle_msc(struct bt_rfcomm_session *session,
return;
}
if (cr == BT_RFCOMM_MSG_CMD) {
rfcomm_send_msc(dlc, BT_RFCOMM_MSG_RESP);
if (cr == BT_RFCOMM_MSG_CMD_CR) {
rfcomm_send_msc(dlc, BT_RFCOMM_MSG_RESP_CR);
}
}
@ -711,7 +712,7 @@ static void rfcomm_handle_pn(struct bt_rfcomm_session *session,
dlc->state = BT_RFCOMM_STATE_CONFIG;
}
rfcomm_send_pn(dlc, BT_RFCOMM_MSG_RESP);
rfcomm_send_pn(dlc, BT_RFCOMM_MSG_RESP_CR);
}
static void rfcomm_handle_disc(struct bt_rfcomm_session *session, uint8_t dlci)
@ -826,7 +827,7 @@ static void rfcomm_handle_data(struct bt_rfcomm_session *session,
int bt_rfcomm_dlc_send(struct bt_rfcomm_dlc *dlc, struct net_buf *buf)
{
struct bt_rfcomm_hdr *hdr;
uint8_t fcs;
uint8_t fcs, cr;
if (!buf) {
return -EINVAL;
@ -856,7 +857,8 @@ int bt_rfcomm_dlc_send(struct bt_rfcomm_dlc *dlc, struct net_buf *buf)
hdr->length = BT_RFCOMM_SET_LEN_8(buf->len - sizeof(*hdr));
}
hdr->address = BT_RFCOMM_SET_ADDR(dlc->dlci, dlc->session->initiator);
cr = BT_RFCOMM_UIH_CR(dlc->session->role);
hdr->address = BT_RFCOMM_SET_ADDR(dlc->dlci, cr);
hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UIH,
BT_RFCOMM_PF_NO_CREDIT);
@ -942,7 +944,7 @@ static void rfcomm_encrypt_change(struct bt_l2cap_chan *chan,
continue;
}
if (!dlc->initiator) {
if (dlc->role == BT_RFCOMM_ROLE_ACCEPTOR) {
rfcomm_send_ua(session, dlc->dlci);
rfcomm_dlc_connected(dlc);
}

View file

@ -27,7 +27,7 @@ struct bt_rfcomm_session {
struct bt_rfcomm_dlc *dlcs;
uint16_t mtu;
uint8_t state;
bool initiator;
bt_rfcomm_role_t role;
};
enum {
@ -110,8 +110,29 @@ struct bt_rfcomm_msc {
#define BT_RFCOMM_LEN_EXTENDED(len) (!((len) & 0x01))
#define BT_RFCOMM_MSG_CMD 1
#define BT_RFCOMM_MSG_RESP 0
/* For CR in UIH Packet header
* Initiating station have the C/R bit set to 1 and those sent by the
* responding station have the C/R bit set to 0
*/
#define BT_RFCOMM_UIH_CR(role) ((role) == BT_RFCOMM_ROLE_INITIATOR)
/* For CR in Non UIH Packet header
* Command
* Initiator --> Responder 1
* Responder --> Initiator 0
* Response
* Initiator --> Responder 0
* Responder --> Initiator 1
*/
#define BT_RFCOMM_CMD_CR(role) ((role) == BT_RFCOMM_ROLE_INITIATOR)
#define BT_RFCOMM_RESP_CR(role) ((role) == BT_RFCOMM_ROLE_ACCEPTOR)
/* For CR in MSG header
* If the C/R bit is set to 1 the message is a command,
* if it is set to 0 the message is a response.
*/
#define BT_RFCOMM_MSG_CMD_CR 1
#define BT_RFCOMM_MSG_RESP_CR 0
/* Excluding ext bit */
#define BT_RFCOMM_MAX_LEN_8 127