#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/bluetooth/include/ng_bluetooth.h>
#include <netgraph/bluetooth/include/ng_hci.h>
#include <netgraph/bluetooth/hci/ng_hci_var.h>
#include <netgraph/bluetooth/hci/ng_hci_cmds.h>
#include <netgraph/bluetooth/hci/ng_hci_evnt.h>
#include <netgraph/bluetooth/hci/ng_hci_ulpi.h>
#include <netgraph/bluetooth/hci/ng_hci_misc.h>
void
ng_hci_mtap(ng_hci_unit_p unit, struct mbuf *m0)
{
struct mbuf *m = NULL;
int error = 0;
if (unit->raw != NULL && NG_HOOK_IS_VALID(unit->raw)) {
m = m_dup(m0, M_NOWAIT);
if (m != NULL)
NG_SEND_DATA_ONLY(error, unit->raw, m);
if (error != 0)
NG_HCI_INFO(
"%s: %s - Could not forward packet, error=%d\n",
__func__, NG_NODE_NAME(unit->node), error);
}
}
void
ng_hci_node_is_up(node_p node, hook_p hook, void *arg1, int arg2)
{
ng_hci_unit_p unit = NULL;
struct ng_mesg *msg = NULL;
ng_hci_node_up_ep *ep = NULL;
int error;
if (node == NULL || NG_NODE_NOT_VALID(node) ||
hook == NULL || NG_HOOK_NOT_VALID(hook))
return;
unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY)
return;
if (hook != unit->acl && hook != unit->sco)
return;
NG_MKMESSAGE(msg,NGM_HCI_COOKIE,NGM_HCI_NODE_UP,sizeof(*ep),M_NOWAIT);
if (msg != NULL) {
ep = (ng_hci_node_up_ep *)(msg->data);
if (hook == unit->acl) {
NG_HCI_BUFF_ACL_SIZE(unit->buffer, ep->pkt_size);
NG_HCI_BUFF_ACL_TOTAL(unit->buffer, ep->num_pkts);
} else {
NG_HCI_BUFF_SCO_SIZE(unit->buffer, ep->pkt_size);
NG_HCI_BUFF_SCO_TOTAL(unit->buffer, ep->num_pkts);
}
bcopy(&unit->bdaddr, &ep->bdaddr, sizeof(ep->bdaddr));
NG_SEND_MSG_HOOK(error, node, msg, hook, 0);
} else
error = ENOMEM;
if (error != 0)
NG_HCI_INFO(
"%s: %s - failed to send NODE_UP message to hook \"%s\", error=%d\n",
__func__, NG_NODE_NAME(unit->node),
NG_HOOK_NAME(hook), error);
}
void
ng_hci_unit_clean(ng_hci_unit_p unit, int reason)
{
int size;
if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)
ng_hci_command_untimeout(unit);
NG_BT_MBUFQ_DRAIN(&unit->cmdq);
NG_HCI_BUFF_CMD_SET(unit->buffer, 1);
while (!LIST_EMPTY(&unit->con_list)) {
ng_hci_unit_con_p con = LIST_FIRST(&unit->con_list);
if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
ng_hci_con_untimeout(con);
ng_hci_lp_discon_ind(con, reason);
ng_hci_free_con(con);
}
NG_HCI_BUFF_ACL_TOTAL(unit->buffer, size);
NG_HCI_BUFF_ACL_FREE(unit->buffer, size);
NG_HCI_BUFF_SCO_TOTAL(unit->buffer, size);
NG_HCI_BUFF_SCO_FREE(unit->buffer, size);
ng_hci_flush_neighbor_cache(unit);
}
ng_hci_neighbor_p
ng_hci_new_neighbor(ng_hci_unit_p unit)
{
ng_hci_neighbor_p n = NULL;
n = malloc(sizeof(*n), M_NETGRAPH_HCI,
M_NOWAIT | M_ZERO);
if (n != NULL) {
getmicrotime(&n->updated);
LIST_INSERT_HEAD(&unit->neighbors, n, next);
}
return (n);
}
void
ng_hci_free_neighbor(ng_hci_neighbor_p n)
{
LIST_REMOVE(n, next);
bzero(n, sizeof(*n));
free(n, M_NETGRAPH_HCI);
}
void
ng_hci_flush_neighbor_cache(ng_hci_unit_p unit)
{
while (!LIST_EMPTY(&unit->neighbors))
ng_hci_free_neighbor(LIST_FIRST(&unit->neighbors));
}
ng_hci_neighbor_p
ng_hci_get_neighbor(ng_hci_unit_p unit, bdaddr_p bdaddr,int link_type)
{
ng_hci_neighbor_p n = NULL;
for (n = LIST_FIRST(&unit->neighbors); n != NULL; ) {
ng_hci_neighbor_p nn = LIST_NEXT(n, next);
if (!ng_hci_neighbor_stale(n)) {
if (n->addrtype == link_type &&
bcmp(&n->bdaddr, bdaddr, sizeof(*bdaddr)) == 0)
break;
} else
ng_hci_free_neighbor(n);
n = nn;
}
return (n);
}
int
ng_hci_neighbor_stale(ng_hci_neighbor_p n)
{
struct timeval now;
getmicrotime(&now);
return (now.tv_sec - n->updated.tv_sec > bluetooth_hci_max_neighbor_age());
}
ng_hci_unit_con_p
ng_hci_new_con(ng_hci_unit_p unit, int link_type)
{
ng_hci_unit_con_p con = NULL;
int num_pkts;
static int fake_con_handle = 0x0f00;
con = malloc(sizeof(*con), M_NETGRAPH_HCI,
M_NOWAIT | M_ZERO);
if (con != NULL) {
con->unit = unit;
con->state = NG_HCI_CON_CLOSED;
con->con_handle = fake_con_handle ++;
if (fake_con_handle > 0x0fff)
fake_con_handle = 0x0f00;
con->link_type = link_type;
if (con->link_type != NG_HCI_LINK_SCO)
NG_HCI_BUFF_ACL_TOTAL(unit->buffer, num_pkts);
else
NG_HCI_BUFF_SCO_TOTAL(unit->buffer, num_pkts);
NG_BT_ITEMQ_INIT(&con->conq, num_pkts);
ng_callout_init(&con->con_timo);
LIST_INSERT_HEAD(&unit->con_list, con, next);
}
return (con);
}
void
ng_hci_free_con(ng_hci_unit_con_p con)
{
LIST_REMOVE(con, next);
if (con->link_type != NG_HCI_LINK_SCO)
NG_HCI_BUFF_ACL_FREE(con->unit->buffer, con->pending);
else
NG_HCI_BUFF_SCO_FREE(con->unit->buffer, con->pending);
NG_BT_ITEMQ_DESTROY(&con->conq);
bzero(con, sizeof(*con));
free(con, M_NETGRAPH_HCI);
}
ng_hci_unit_con_p
ng_hci_con_by_handle(ng_hci_unit_p unit, int con_handle)
{
ng_hci_unit_con_p con = NULL;
LIST_FOREACH(con, &unit->con_list, next)
if (con->con_handle == con_handle)
break;
return (con);
}
ng_hci_unit_con_p
ng_hci_con_by_bdaddr(ng_hci_unit_p unit, bdaddr_p bdaddr, int link_type)
{
ng_hci_unit_con_p con = NULL;
LIST_FOREACH(con, &unit->con_list, next)
if (con->link_type == link_type &&
bcmp(&con->bdaddr, bdaddr, sizeof(bdaddr_t)) == 0)
break;
return (con);
}
int
ng_hci_command_timeout(ng_hci_unit_p unit)
{
if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)
panic(
"%s: %s - Duplicated command timeout!\n", __func__, NG_NODE_NAME(unit->node));
unit->state |= NG_HCI_UNIT_COMMAND_PENDING;
ng_callout(&unit->cmd_timo, unit->node, NULL,
bluetooth_hci_command_timeout(),
ng_hci_process_command_timeout, NULL, 0);
return (0);
}
int
ng_hci_command_untimeout(ng_hci_unit_p unit)
{
if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
panic(
"%s: %s - No command timeout!\n", __func__, NG_NODE_NAME(unit->node));
if (ng_uncallout(&unit->cmd_timo, unit->node) < 1)
return (ETIMEDOUT);
unit->state &= ~NG_HCI_UNIT_COMMAND_PENDING;
return (0);
}
int
ng_hci_con_timeout(ng_hci_unit_con_p con)
{
if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
panic(
"%s: %s - Duplicated connection timeout!\n",
__func__, NG_NODE_NAME(con->unit->node));
con->flags |= NG_HCI_CON_TIMEOUT_PENDING;
ng_callout(&con->con_timo, con->unit->node, NULL,
bluetooth_hci_connect_timeout(),
ng_hci_process_con_timeout, NULL,
con->con_handle);
return (0);
}
int
ng_hci_con_untimeout(ng_hci_unit_con_p con)
{
if (!(con->flags & NG_HCI_CON_TIMEOUT_PENDING))
panic(
"%s: %s - No connection timeout!\n", __func__, NG_NODE_NAME(con->unit->node));
if (ng_uncallout(&con->con_timo, con->unit->node) < 1)
return (ETIMEDOUT);
con->flags &= ~NG_HCI_CON_TIMEOUT_PENDING;
return (0);
}
#if 0
char const * const
ng_hci_str_error(u_int16_t code)
{
#define LAST_ERROR_CODE ((sizeof(s)/sizeof(s[0]))-1)
static char const * const s[] = {
"No error",
"Unknown HCI command",
"No connection",
"Hardware failure",
"Page timeout",
"Authentication failure",
"Key missing",
"Memory full",
"Connection timeout",
"Max number of connections",
"Max number of SCO connections to a unit",
"ACL connection already exists",
"Command disallowed",
"Host rejected due to limited resources",
"Host rejected due to securiity reasons",
"Host rejected due to remote unit is a personal unit",
"Host timeout",
"Unsupported feature or parameter value",
"Invalid HCI command parameter",
"Other end terminated connection: User ended connection",
"Other end terminated connection: Low resources",
"Other end terminated connection: About to power off",
"Connection terminated by local host",
"Repeated attempts",
"Pairing not allowed",
"Unknown LMP PDU",
"Unsupported remote feature",
"SCO offset rejected",
"SCO interval rejected",
"SCO air mode rejected",
"Invalid LMP parameters",
"Unspecified error",
"Unsupported LMP parameter value",
"Role change not allowed",
"LMP response timeout",
"LMP error transaction collision",
"LMP PSU not allowed",
"Encryption mode not acceptable",
"Unit key used",
"QoS is not supported",
"Instant passed",
"Pairing with unit key not supported",
"Different Transaction Collision",
"Unknown error (Reserved for future use)",
"QoS Unacceptable Parameter",
"QoS Rejected",
"Channel Classification Not Supported",
"Insufficient Security",
"Parameter Out Of Mandatory Range",
"Unknown error (Reserved for future use)",
"Role Switch Pending",
"Unknown error (Reserved for future use)",
"Reserved Slot Violation",
"Role Switch Failed",
"Extended Inquiry Response Too Large",
"Secure Simple Pairing Not Supported By Host",
"Host Busy - Pairing",
"Connection Rejected due to No Suitable Channel Found",
"Controller Busy",
"Unacceptable Connection Parameters",
"Advertising Timeout",
"Connection Terminated due to MIC Failure",
"Connection Failed to be Established / Synchronization Timeout",
"MAC Connection Failed",
"Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging",
"Type0 Submap Not Defined",
"Unknown Advertising Identifier",
"Limit Reached",
"Operation Cancelled by Host",
"Packet Too Long",
"Unknown error"
};
return ((code >= LAST_ERROR_CODE)? s[LAST_ERROR_CODE] : s[code]);
}
#endif