4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: send_recv.c,v 1.2 2003/05/21 22:40:30 max Exp $
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <sys/endian.h>
38 #include <netgraph/bluetooth/include/ng_hci.h>
41 #include "hccontrol.h"
43 /* Send HCI request to the unit */
45 hci_request(int s, int opcode, char const *cp, int cp_size, char *rp, int *rp_size)
49 ng_hci_cmd_pkt_t *c = (ng_hci_cmd_pkt_t *) buffer;
50 ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t *) buffer;
53 assert(rp_size != NULL);
56 c->type = NG_HCI_CMD_PKT;
57 c->opcode = (uint16_t) opcode;
58 c->opcode = htole16(c->opcode);
61 assert(0 < cp_size && cp_size <= NG_HCI_CMD_PKT_SIZE);
63 c->length = (uint8_t) cp_size;
64 memcpy(buffer + sizeof(*c), cp, cp_size);
68 if (hci_send(s, buffer, sizeof(*c) + cp_size) == ERROR)
73 if (hci_recv(s, buffer, &n) == ERROR)
81 if (e->type != NG_HCI_EVENT_PKT) {
87 case NG_HCI_EVENT_COMMAND_COMPL: {
88 ng_hci_command_compl_ep *cc =
89 (ng_hci_command_compl_ep *)(e + 1);
91 cc->opcode = le16toh(cc->opcode);
93 if (cc->opcode == 0x0000 || cc->opcode != opcode)
96 n -= (sizeof(*e) + sizeof(*cc));
100 memcpy(rp, buffer + sizeof(*e) + sizeof(*cc), *rp_size);
103 case NG_HCI_EVENT_COMMAND_STATUS: {
104 ng_hci_command_status_ep *cs =
105 (ng_hci_command_status_ep *)(e + 1);
107 cs->opcode = le16toh(cs->opcode);
109 if (cs->opcode == 0x0000 || cs->opcode != opcode)
123 /* Send simple HCI request - Just HCI command packet (no parameters) */
125 hci_simple_request(int s, int opcode, char *rp, int *rp_size)
127 return (hci_request(s, opcode, NULL, 0, rp, rp_size));
128 } /* hci_simple_request */
130 /* Send HCI data to the unit */
132 hci_send(int s, char const *buffer, int size)
134 assert(buffer != NULL);
135 assert(size >= sizeof(ng_hci_cmd_pkt_t));
136 assert(size <= sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE);
138 if (send(s, buffer, size, 0) < 0)
144 /* Receive HCI data from the unit */
146 hci_recv(int s, char *buffer, int *size)
152 assert(buffer != NULL);
153 assert(size != NULL);
154 assert(*size > sizeof(ng_hci_event_pkt_t));
163 n = select(s + 1, &rfd, NULL, NULL, &tv);
174 assert(FD_ISSET(s, &rfd));
176 n = recv(s, buffer, *size, 0);