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