4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: send_recv.c,v 1.2 2003/05/21 22:40:30 max Exp $
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <sys/endian.h>
40 #include <netgraph/bluetooth/include/ng_hci.h>
43 #include "hccontrol.h"
45 /* Send HCI request to the unit */
47 hci_request(int s, int opcode, char const *cp, int cp_size, char *rp, int *rp_size)
51 ng_hci_cmd_pkt_t *c = (ng_hci_cmd_pkt_t *) buffer;
52 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) buffer;
55 assert(rp_size != NULL);
58 c->type = NG_HCI_CMD_PKT;
59 c->opcode = (uint16_t) opcode;
60 c->opcode = htole16(c->opcode);
63 assert(0 < cp_size && cp_size <= NG_HCI_CMD_PKT_SIZE);
65 c->length = (uint8_t) cp_size;
66 memcpy(buffer + sizeof(*c), cp, cp_size);
70 if (hci_send(s, buffer, sizeof(*c) + cp_size) == ERROR)
75 if (hci_recv(s, buffer, &n) == ERROR)
83 if (e->type != NG_HCI_EVENT_PKT) {
89 case NG_HCI_EVENT_COMMAND_COMPL: {
90 ng_hci_command_compl_ep *cc =
91 (ng_hci_command_compl_ep *)(e + 1);
93 cc->opcode = le16toh(cc->opcode);
95 if (cc->opcode == 0x0000 || cc->opcode != opcode)
98 n -= (sizeof(*e) + sizeof(*cc));
102 memcpy(rp, buffer + sizeof(*e) + sizeof(*cc), *rp_size);
105 case NG_HCI_EVENT_COMMAND_STATUS: {
106 ng_hci_command_status_ep *cs =
107 (ng_hci_command_status_ep *)(e + 1);
109 cs->opcode = le16toh(cs->opcode);
111 if (cs->opcode == 0x0000 || cs->opcode != opcode)
125 /* Send simple HCI request - Just HCI command packet (no parameters) */
127 hci_simple_request(int s, int opcode, char *rp, int *rp_size)
129 return (hci_request(s, opcode, NULL, 0, rp, rp_size));
130 } /* hci_simple_request */
132 /* Send HCI data to the unit */
134 hci_send(int s, char const *buffer, int size)
136 assert(buffer != NULL);
137 assert(size >= sizeof(ng_hci_cmd_pkt_t));
138 assert(size <= sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE);
140 if (send(s, buffer, size, 0) < 0)
146 /* Receive HCI data from the unit */
148 hci_recv(int s, char *buffer, int *size)
154 assert(buffer != NULL);
155 assert(size != NULL);
156 assert(*size > sizeof(ng_hci_event_pkt_t));
165 n = select(s + 1, &rfd, NULL, NULL, &tv);
176 assert(FD_ISSET(s, &rfd));
178 n = recv(s, buffer, *size, 0);