1/*-
2 * send_recv.c
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 *
30 * $Id: send_recv.c,v 1.2 2003/05/21 22:40:30 max Exp $
31 * $FreeBSD$
32 */
33
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <sys/time.h>
37#include <sys/endian.h>
38#include <assert.h>
39#include <errno.h>
40#include <netgraph/bluetooth/include/ng_hci.h>
41#include <string.h>
42#include <unistd.h>
43#include "hccontrol.h"
44
45/* Send HCI request to the unit */
46int
47hci_request(int s, int opcode, char const *cp, int cp_size, char *rp, int *rp_size)
48{
49	char			 buffer[512];
50	int			 n;
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;
53
54	assert(rp != NULL);
55	assert(rp_size != NULL);
56	assert(*rp_size > 0);
57
58	c->type = NG_HCI_CMD_PKT;
59	c->opcode = (uint16_t) opcode;
60	c->opcode = htole16(c->opcode);
61
62	if (cp != NULL) {
63		assert(0 < cp_size && cp_size <= NG_HCI_CMD_PKT_SIZE);
64
65		c->length = (uint8_t) cp_size;
66		memcpy(buffer + sizeof(*c), cp, cp_size);
67	} else
68		c->length = 0;
69
70	if (hci_send(s, buffer, sizeof(*c) + cp_size) == ERROR)
71		return (ERROR);
72
73again:
74	n = sizeof(buffer);
75	if (hci_recv(s, buffer, &n) == ERROR)
76		return (ERROR);
77
78	if (n < sizeof(*e)) {
79		errno = EMSGSIZE;
80		return (ERROR);
81	}
82
83	if (e->type != NG_HCI_EVENT_PKT) {
84		errno = EIO;
85		return (ERROR);
86	}
87
88	switch (e->event) {
89	case NG_HCI_EVENT_COMMAND_COMPL: {
90		ng_hci_command_compl_ep	*cc =
91				(ng_hci_command_compl_ep *)(e + 1);
92
93		cc->opcode = le16toh(cc->opcode);
94
95		if (cc->opcode == 0x0000 || cc->opcode != opcode)
96			goto again;
97
98		n -= (sizeof(*e) + sizeof(*cc));
99		if (n < *rp_size)
100			*rp_size = n;
101
102		memcpy(rp, buffer + sizeof(*e) + sizeof(*cc), *rp_size);
103		} break;
104
105	case NG_HCI_EVENT_COMMAND_STATUS: {
106		ng_hci_command_status_ep	*cs =
107				(ng_hci_command_status_ep *)(e + 1);
108
109		cs->opcode = le16toh(cs->opcode);
110
111		if (cs->opcode == 0x0000 || cs->opcode != opcode)
112			goto again;
113
114		*rp_size = 1;
115		*rp = cs->status;
116		} break;
117
118	default:
119		goto again;
120	}
121
122	return (OK);
123} /* hci_request */
124
125/* Send simple HCI request - Just HCI command packet (no parameters) */
126int
127hci_simple_request(int s, int opcode, char *rp, int *rp_size)
128{
129	return (hci_request(s, opcode, NULL, 0, rp, rp_size));
130} /* hci_simple_request */
131
132/* Send HCI data to the unit */
133int
134hci_send(int s, char const *buffer, int size)
135{
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);
139
140	if (send(s, buffer, size, 0) < 0)
141		return (ERROR);
142
143	return (OK);
144} /* hci_send */
145
146/* Receive HCI data from the unit */
147int
148hci_recv(int s, char *buffer, int *size)
149{
150	struct timeval	tv;
151	fd_set		rfd;
152	int		n;
153
154	assert(buffer != NULL);
155	assert(size != NULL);
156	assert(*size > sizeof(ng_hci_event_pkt_t));
157
158again:
159	FD_ZERO(&rfd);
160	FD_SET(s, &rfd);
161
162	tv.tv_sec = timeout;
163	tv.tv_usec = 0;
164
165	n = select(s + 1, &rfd, NULL, NULL, &tv);
166	if (n <= 0) {
167		if (n < 0) {
168			if (errno == EINTR)
169				goto again;
170		} else
171			errno = ETIMEDOUT;
172
173		return (ERROR);
174	}
175
176	assert(FD_ISSET(s, &rfd));
177
178	n = recv(s, buffer, *size, 0);
179	if (n < 0)
180		return (ERROR);
181
182	*size = n;
183
184	return (OK);
185} /* hci_recv */
186
187