Heroic Robotics / cc3000_hostdriver_mbedsocket

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
heroic
Date:
Tue May 06 22:38:34 2014 +0000
Revision:
56:9ab991c1d2db
Parent:
45:50ab13d8f2dc
Stop hci event handler from locking up in the situation where the micro thinks there's an event about to happen and the cc3000 disagrees.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 20:30b6ed7bf8fd 1 /*****************************************************************************
Kojto 20:30b6ed7bf8fd 2 *
Kojto 20:30b6ed7bf8fd 3 * C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
Kojto 20:30b6ed7bf8fd 4 * Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
Kojto 20:30b6ed7bf8fd 5 * provided help.
Kojto 20:30b6ed7bf8fd 6 *
Kojto 20:30b6ed7bf8fd 7 * This version of "host driver" uses CC3000 Host Driver Implementation. Thus
Kojto 20:30b6ed7bf8fd 8 * read the following copyright:
Kojto 20:30b6ed7bf8fd 9 *
Kojto 20:30b6ed7bf8fd 10 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Kojto 20:30b6ed7bf8fd 11 *
Kojto 20:30b6ed7bf8fd 12 * Redistribution and use in source and binary forms, with or without
Kojto 20:30b6ed7bf8fd 13 * modification, are permitted provided that the following conditions
Kojto 20:30b6ed7bf8fd 14 * are met:
Kojto 20:30b6ed7bf8fd 15 *
Kojto 20:30b6ed7bf8fd 16 * Redistributions of source code must retain the above copyright
Kojto 20:30b6ed7bf8fd 17 * notice, this list of conditions and the following disclaimer.
Kojto 20:30b6ed7bf8fd 18 *
Kojto 20:30b6ed7bf8fd 19 * Redistributions in binary form must reproduce the above copyright
Kojto 20:30b6ed7bf8fd 20 * notice, this list of conditions and the following disclaimer in the
Kojto 20:30b6ed7bf8fd 21 * documentation and/or other materials provided with the
Kojto 20:30b6ed7bf8fd 22 * distribution.
Kojto 20:30b6ed7bf8fd 23 *
Kojto 20:30b6ed7bf8fd 24 * Neither the name of Texas Instruments Incorporated nor the names of
Kojto 20:30b6ed7bf8fd 25 * its contributors may be used to endorse or promote products derived
Kojto 20:30b6ed7bf8fd 26 * from this software without specific prior written permission.
Kojto 20:30b6ed7bf8fd 27 *
Kojto 20:30b6ed7bf8fd 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Kojto 20:30b6ed7bf8fd 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Kojto 20:30b6ed7bf8fd 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Kojto 20:30b6ed7bf8fd 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Kojto 20:30b6ed7bf8fd 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Kojto 20:30b6ed7bf8fd 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Kojto 20:30b6ed7bf8fd 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Kojto 20:30b6ed7bf8fd 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Kojto 20:30b6ed7bf8fd 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kojto 20:30b6ed7bf8fd 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Kojto 20:30b6ed7bf8fd 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 20:30b6ed7bf8fd 39 *
Kojto 20:30b6ed7bf8fd 40 *****************************************************************************/
Kojto 20:30b6ed7bf8fd 41 #include "cc3000.h"
Kojto 20:30b6ed7bf8fd 42 #include "cc3000_socket.h"
Kojto 20:30b6ed7bf8fd 43 #include "cc3000_event.h" //TODO - remove this
Kojto 20:30b6ed7bf8fd 44 #include "cc3000_common.h"
Kojto 20:30b6ed7bf8fd 45
Kojto 20:30b6ed7bf8fd 46 namespace mbed_cc3000 {
Kojto 20:30b6ed7bf8fd 47
Kojto 20:30b6ed7bf8fd 48 cc3000_socket::cc3000_socket(cc3000_simple_link &simplelink, cc3000_hci &hci, cc3000_event &event)
Kojto 45:50ab13d8f2dc 49 : _simple_link(simplelink), _hci(hci), _event(event) {
Kojto 20:30b6ed7bf8fd 50
Kojto 20:30b6ed7bf8fd 51 }
Kojto 20:30b6ed7bf8fd 52
Kojto 45:50ab13d8f2dc 53 cc3000_socket::~cc3000_socket() {
Kojto 20:30b6ed7bf8fd 54
Kojto 20:30b6ed7bf8fd 55 }
Kojto 20:30b6ed7bf8fd 56
Kojto 20:30b6ed7bf8fd 57 int32_t cc3000_socket::HostFlowControlConsumeBuff(int32_t sd) {
Kojto 20:30b6ed7bf8fd 58 #ifndef SEND_NON_BLOCKING
Kojto 20:30b6ed7bf8fd 59 /* wait in busy loop */
Kojto 45:50ab13d8f2dc 60 do {
Kojto 20:30b6ed7bf8fd 61 // When the last transmission failed, return the last failure reason.
Kojto 20:30b6ed7bf8fd 62 // Note that the buffer will not be allocated in this case
Kojto 45:50ab13d8f2dc 63 if (_simple_link.get_transmit_error() != 0) {
Kojto 20:30b6ed7bf8fd 64 errno = _simple_link.get_transmit_error();
Kojto 20:30b6ed7bf8fd 65 _simple_link.set_transmit_error(0);
Kojto 20:30b6ed7bf8fd 66 return errno;
Kojto 20:30b6ed7bf8fd 67 }
Kojto 20:30b6ed7bf8fd 68
Kojto 20:30b6ed7bf8fd 69 if(SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd))
Kojto 20:30b6ed7bf8fd 70 return -1;
Kojto 45:50ab13d8f2dc 71 } while (0 == _simple_link.get_number_free_buffers());
Kojto 20:30b6ed7bf8fd 72
Kojto 20:30b6ed7bf8fd 73 uint16_t free_buffer = _simple_link.get_number_free_buffers();
Kojto 20:30b6ed7bf8fd 74 free_buffer--;
Kojto 20:30b6ed7bf8fd 75 _simple_link.set_number_free_buffers(free_buffer);
Kojto 20:30b6ed7bf8fd 76
Kojto 20:30b6ed7bf8fd 77 return 0;
Kojto 20:30b6ed7bf8fd 78 #else
Kojto 20:30b6ed7bf8fd 79
Kojto 20:30b6ed7bf8fd 80 // When the last transmission failed, return the last failure reason.
Kojto 20:30b6ed7bf8fd 81 // Note that the buffer will not be allocated in this case
Kojto 45:50ab13d8f2dc 82 if (_simple_link.get_transmit_error() != 0) {
Kojto 20:30b6ed7bf8fd 83 errno = _simple_link.get_transmit_error();
Kojto 20:30b6ed7bf8fd 84 _simple_link.set_transmit_error(0);
Kojto 20:30b6ed7bf8fd 85 return errno;
Kojto 20:30b6ed7bf8fd 86 }
Kojto 45:50ab13d8f2dc 87 if (SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd))
Kojto 20:30b6ed7bf8fd 88 return -1;
Kojto 20:30b6ed7bf8fd 89
Kojto 20:30b6ed7bf8fd 90 // If there are no available buffers, return -2. It is recommended to use
Kojto 20:30b6ed7bf8fd 91 // select or receive to see if there is any buffer occupied with received data
Kojto 20:30b6ed7bf8fd 92 // If so, call receive() to release the buffer.
Kojto 45:50ab13d8f2dc 93 if (0 == _simple_link.get_number_free_buffers()) {
Kojto 20:30b6ed7bf8fd 94 return -2;
Kojto 45:50ab13d8f2dc 95 } else {
Kojto 20:30b6ed7bf8fd 96 uint16_t free_buffer = _simple_link.get_number_free_buffers();
Kojto 20:30b6ed7bf8fd 97 free_buffer--;
Kojto 20:30b6ed7bf8fd 98 _simple_link.set_number_free_buffers(free_buffer);
Kojto 20:30b6ed7bf8fd 99 return 0;
Kojto 20:30b6ed7bf8fd 100 }
Kojto 20:30b6ed7bf8fd 101 #endif
Kojto 20:30b6ed7bf8fd 102 }
Kojto 20:30b6ed7bf8fd 103
Kojto 20:30b6ed7bf8fd 104 int32_t cc3000_socket::socket(int32_t domain, int32_t type, int32_t protocol) {
Kojto 20:30b6ed7bf8fd 105 int32_t ret;
Kojto 20:30b6ed7bf8fd 106 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 107
Kojto 20:30b6ed7bf8fd 108 ret = EFAIL;
Kojto 20:30b6ed7bf8fd 109 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 110 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 111
Kojto 20:30b6ed7bf8fd 112 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 113 args = UINT32_TO_STREAM(args, domain);
Kojto 20:30b6ed7bf8fd 114 args = UINT32_TO_STREAM(args, type);
Kojto 20:30b6ed7bf8fd 115 args = UINT32_TO_STREAM(args, protocol);
Kojto 20:30b6ed7bf8fd 116
Kojto 20:30b6ed7bf8fd 117 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 118 _hci.command_send(HCI_CMND_SOCKET, ptr, SOCKET_OPEN_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 119
Kojto 20:30b6ed7bf8fd 120 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 121 _event.simplelink_wait_event(HCI_CMND_SOCKET, &ret);
Kojto 20:30b6ed7bf8fd 122
Kojto 20:30b6ed7bf8fd 123 // Process the event
Kojto 20:30b6ed7bf8fd 124 errno = ret;
Kojto 20:30b6ed7bf8fd 125
Kojto 20:30b6ed7bf8fd 126 _event.set_socket_active_status(ret, SOCKET_STATUS_ACTIVE);
Kojto 20:30b6ed7bf8fd 127
Kojto 45:50ab13d8f2dc 128 return ret;
Kojto 20:30b6ed7bf8fd 129 }
Kojto 20:30b6ed7bf8fd 130
Kojto 20:30b6ed7bf8fd 131 int32_t cc3000_socket::closesocket(int32_t sd) {
Kojto 20:30b6ed7bf8fd 132 int32_t ret;
Kojto 20:30b6ed7bf8fd 133 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 134
Kojto 45:50ab13d8f2dc 135 while (_simple_link.get_number_free_buffers() != SOCKET_MAX_FREE_BUFFERS);
Kojto 20:30b6ed7bf8fd 136 ret = EFAIL;
Kojto 20:30b6ed7bf8fd 137 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 138 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 139
Kojto 20:30b6ed7bf8fd 140 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 141 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 142
Kojto 20:30b6ed7bf8fd 143 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 144 _hci.command_send(HCI_CMND_CLOSE_SOCKET, ptr, SOCKET_CLOSE_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 145
Kojto 20:30b6ed7bf8fd 146 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 147 _event.simplelink_wait_event(HCI_CMND_CLOSE_SOCKET, &ret);
Kojto 20:30b6ed7bf8fd 148 errno = ret;
Kojto 20:30b6ed7bf8fd 149
Kojto 20:30b6ed7bf8fd 150 // since 'close' call may result in either OK (and then it closed) or error, mark this socket as invalid
Kojto 20:30b6ed7bf8fd 151 _event.set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
Kojto 20:30b6ed7bf8fd 152
Kojto 45:50ab13d8f2dc 153 return ret;
Kojto 20:30b6ed7bf8fd 154 }
Kojto 20:30b6ed7bf8fd 155
Kojto 20:30b6ed7bf8fd 156 int32_t cc3000_socket::accept(int32_t sd, sockaddr *addr, socklen_t *addrlen) {
Kojto 20:30b6ed7bf8fd 157 int32_t ret;
Kojto 20:30b6ed7bf8fd 158 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 159 tBsdReturnParams tAcceptReturnArguments;
Kojto 20:30b6ed7bf8fd 160
Kojto 20:30b6ed7bf8fd 161 ret = EFAIL;
Kojto 20:30b6ed7bf8fd 162 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 163 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 164
Kojto 20:30b6ed7bf8fd 165 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 166 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 167
Kojto 20:30b6ed7bf8fd 168 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 169 _hci.command_send(HCI_CMND_ACCEPT, ptr, SOCKET_ACCEPT_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 170
Kojto 20:30b6ed7bf8fd 171 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 172 _event.simplelink_wait_event(HCI_CMND_ACCEPT, &tAcceptReturnArguments);
Kojto 20:30b6ed7bf8fd 173
Kojto 20:30b6ed7bf8fd 174
Kojto 20:30b6ed7bf8fd 175 // need specify return parameters!!!
Kojto 20:30b6ed7bf8fd 176 memcpy(addr, &tAcceptReturnArguments.tSocketAddress, ASIC_ADDR_LEN);
Kojto 20:30b6ed7bf8fd 177 *addrlen = ASIC_ADDR_LEN;
Kojto 20:30b6ed7bf8fd 178 errno = tAcceptReturnArguments.iStatus;
Kojto 20:30b6ed7bf8fd 179 ret = errno;
Kojto 20:30b6ed7bf8fd 180
Kojto 20:30b6ed7bf8fd 181 // if succeeded, iStatus = new socket descriptor. otherwise - error number
Kojto 45:50ab13d8f2dc 182 if(M_IS_VALID_SD(ret)) {
Kojto 20:30b6ed7bf8fd 183 _event.set_socket_active_status(ret, SOCKET_STATUS_ACTIVE);
Kojto 45:50ab13d8f2dc 184 } else {
Kojto 20:30b6ed7bf8fd 185 _event.set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
Kojto 20:30b6ed7bf8fd 186 }
Kojto 20:30b6ed7bf8fd 187
Kojto 45:50ab13d8f2dc 188 return ret;
Kojto 20:30b6ed7bf8fd 189 }
Kojto 20:30b6ed7bf8fd 190
Kojto 20:30b6ed7bf8fd 191 int32_t cc3000_socket::bind(int32_t sd, const sockaddr *addr, int32_t addrlen) {
Kojto 20:30b6ed7bf8fd 192 int32_t ret;
Kojto 20:30b6ed7bf8fd 193 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 194
Kojto 20:30b6ed7bf8fd 195 ret = EFAIL;
Kojto 20:30b6ed7bf8fd 196 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 197 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 198
Kojto 20:30b6ed7bf8fd 199 addrlen = ASIC_ADDR_LEN;
Kojto 20:30b6ed7bf8fd 200
Kojto 20:30b6ed7bf8fd 201 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 202 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 203 args = UINT32_TO_STREAM(args, 0x00000008);
Kojto 20:30b6ed7bf8fd 204 args = UINT32_TO_STREAM(args, addrlen);
Kojto 20:30b6ed7bf8fd 205 ARRAY_TO_STREAM(args, ((uint8_t *)addr), addrlen);
Kojto 20:30b6ed7bf8fd 206
Kojto 20:30b6ed7bf8fd 207 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 208 _hci.command_send(HCI_CMND_BIND, ptr, SOCKET_BIND_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 209
Kojto 20:30b6ed7bf8fd 210 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 211 _event.simplelink_wait_event(HCI_CMND_BIND, &ret);
Kojto 20:30b6ed7bf8fd 212
Kojto 20:30b6ed7bf8fd 213 errno = ret;
Kojto 20:30b6ed7bf8fd 214
Kojto 45:50ab13d8f2dc 215 return ret;
Kojto 20:30b6ed7bf8fd 216 }
Kojto 20:30b6ed7bf8fd 217
Kojto 20:30b6ed7bf8fd 218 int32_t cc3000_socket::listen(int32_t sd, int32_t backlog) {
Kojto 20:30b6ed7bf8fd 219 int32_t ret;
Kojto 20:30b6ed7bf8fd 220 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 221
Kojto 20:30b6ed7bf8fd 222 ret = EFAIL;
Kojto 20:30b6ed7bf8fd 223 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 224 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 225
Kojto 20:30b6ed7bf8fd 226 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 227 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 228 args = UINT32_TO_STREAM(args, backlog);
Kojto 20:30b6ed7bf8fd 229
Kojto 20:30b6ed7bf8fd 230 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 231 _hci.command_send(HCI_CMND_LISTEN, ptr, SOCKET_LISTEN_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 232
Kojto 20:30b6ed7bf8fd 233 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 234 _event.simplelink_wait_event(HCI_CMND_LISTEN, &ret);
Kojto 20:30b6ed7bf8fd 235 errno = ret;
Kojto 20:30b6ed7bf8fd 236
Kojto 20:30b6ed7bf8fd 237 return(ret);
Kojto 20:30b6ed7bf8fd 238 }
Kojto 20:30b6ed7bf8fd 239
Kojto 20:30b6ed7bf8fd 240 int32_t cc3000_socket::connect(int32_t sd, const sockaddr *addr, int32_t addrlen) {
Kojto 20:30b6ed7bf8fd 241 int32_t ret;
Kojto 20:30b6ed7bf8fd 242 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 243
Kojto 20:30b6ed7bf8fd 244 ret = EFAIL;
Kojto 20:30b6ed7bf8fd 245 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 246 args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 247 addrlen = 8;
Kojto 20:30b6ed7bf8fd 248
Kojto 20:30b6ed7bf8fd 249 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 250 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 251 args = UINT32_TO_STREAM(args, 0x00000008);
Kojto 20:30b6ed7bf8fd 252 args = UINT32_TO_STREAM(args, addrlen);
Kojto 20:30b6ed7bf8fd 253 ARRAY_TO_STREAM(args, ((uint8_t *)addr), addrlen);
Kojto 20:30b6ed7bf8fd 254
Kojto 20:30b6ed7bf8fd 255 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 256 _hci.command_send(HCI_CMND_CONNECT, ptr, SOCKET_CONNECT_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 257
Kojto 20:30b6ed7bf8fd 258 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 259 _event.simplelink_wait_event(HCI_CMND_CONNECT, &ret);
Kojto 20:30b6ed7bf8fd 260
Kojto 20:30b6ed7bf8fd 261 errno = ret;
Kojto 20:30b6ed7bf8fd 262
Kojto 20:30b6ed7bf8fd 263 return((int32_t)ret);
Kojto 20:30b6ed7bf8fd 264 }
Kojto 20:30b6ed7bf8fd 265
Kojto 20:30b6ed7bf8fd 266 int32_t cc3000_socket::select(int32_t nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, struct timeval *timeout) {
Kojto 20:30b6ed7bf8fd 267 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 268 tBsdSelectRecvParams tParams;
Kojto 20:30b6ed7bf8fd 269 uint32_t is_blocking;
Kojto 20:30b6ed7bf8fd 270
Kojto 45:50ab13d8f2dc 271 if (timeout == NULL) {
Kojto 20:30b6ed7bf8fd 272 is_blocking = 1; /* blocking , infinity timeout */
Kojto 45:50ab13d8f2dc 273 } else {
Kojto 20:30b6ed7bf8fd 274 is_blocking = 0; /* no blocking, timeout */
Kojto 20:30b6ed7bf8fd 275 }
Kojto 20:30b6ed7bf8fd 276
Kojto 20:30b6ed7bf8fd 277 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 278 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 279 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 280
Kojto 20:30b6ed7bf8fd 281 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 282 args = UINT32_TO_STREAM(args, nfds);
Kojto 20:30b6ed7bf8fd 283 args = UINT32_TO_STREAM(args, 0x00000014);
Kojto 20:30b6ed7bf8fd 284 args = UINT32_TO_STREAM(args, 0x00000014);
Kojto 20:30b6ed7bf8fd 285 args = UINT32_TO_STREAM(args, 0x00000014);
Kojto 20:30b6ed7bf8fd 286 args = UINT32_TO_STREAM(args, 0x00000014);
Kojto 20:30b6ed7bf8fd 287 args = UINT32_TO_STREAM(args, is_blocking);
Kojto 20:30b6ed7bf8fd 288 args = UINT32_TO_STREAM(args, ((readsds) ? *(uint32_t*)readsds : 0));
Kojto 20:30b6ed7bf8fd 289 args = UINT32_TO_STREAM(args, ((writesds) ? *(uint32_t*)writesds : 0));
Kojto 20:30b6ed7bf8fd 290 args = UINT32_TO_STREAM(args, ((exceptsds) ? *(uint32_t*)exceptsds : 0));
Kojto 20:30b6ed7bf8fd 291
Kojto 45:50ab13d8f2dc 292 if (timeout) {
Kojto 45:50ab13d8f2dc 293 if ( 0 == timeout->tv_sec && timeout->tv_usec < SELECT_TIMEOUT_MIN_MICRO_SECONDS) {
Kojto 20:30b6ed7bf8fd 294 timeout->tv_usec = SELECT_TIMEOUT_MIN_MICRO_SECONDS;
Kojto 20:30b6ed7bf8fd 295 }
Kojto 20:30b6ed7bf8fd 296 args = UINT32_TO_STREAM(args, timeout->tv_sec);
Kojto 20:30b6ed7bf8fd 297 args = UINT32_TO_STREAM(args, timeout->tv_usec);
Kojto 20:30b6ed7bf8fd 298 }
Kojto 20:30b6ed7bf8fd 299
Kojto 20:30b6ed7bf8fd 300 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 301 _hci.command_send(HCI_CMND_BSD_SELECT, ptr, SOCKET_SELECT_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 302
Kojto 20:30b6ed7bf8fd 303 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 304 _event.simplelink_wait_event(HCI_EVNT_SELECT, &tParams);
Kojto 20:30b6ed7bf8fd 305
Kojto 20:30b6ed7bf8fd 306 // Update actually read FD
Kojto 45:50ab13d8f2dc 307 if (tParams.iStatus >= 0) {
Kojto 45:50ab13d8f2dc 308 if (readsds) {
Kojto 20:30b6ed7bf8fd 309 memcpy(readsds, &tParams.uiRdfd, sizeof(tParams.uiRdfd));
Kojto 20:30b6ed7bf8fd 310 }
Kojto 20:30b6ed7bf8fd 311
Kojto 45:50ab13d8f2dc 312 if (writesds) {
Kojto 20:30b6ed7bf8fd 313 memcpy(writesds, &tParams.uiWrfd, sizeof(tParams.uiWrfd));
Kojto 20:30b6ed7bf8fd 314 }
Kojto 20:30b6ed7bf8fd 315
Kojto 45:50ab13d8f2dc 316 if (exceptsds) {
Kojto 20:30b6ed7bf8fd 317 memcpy(exceptsds, &tParams.uiExfd, sizeof(tParams.uiExfd));
Kojto 20:30b6ed7bf8fd 318 }
Kojto 20:30b6ed7bf8fd 319
Kojto 20:30b6ed7bf8fd 320 return(tParams.iStatus);
Kojto 20:30b6ed7bf8fd 321
Kojto 45:50ab13d8f2dc 322 } else {
Kojto 20:30b6ed7bf8fd 323 errno = tParams.iStatus;
Kojto 45:50ab13d8f2dc 324 return -1;
Kojto 20:30b6ed7bf8fd 325 }
Kojto 20:30b6ed7bf8fd 326 }
Kojto 20:30b6ed7bf8fd 327
Kojto 20:30b6ed7bf8fd 328 int32_t cc3000_socket::getsockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen) {
Kojto 20:30b6ed7bf8fd 329 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 330 tBsdGetSockOptReturnParams tRetParams;
Kojto 20:30b6ed7bf8fd 331
Kojto 20:30b6ed7bf8fd 332 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 333 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 334
Kojto 20:30b6ed7bf8fd 335 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 336 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 337 args = UINT32_TO_STREAM(args, level);
Kojto 20:30b6ed7bf8fd 338 args = UINT32_TO_STREAM(args, optname);
Kojto 20:30b6ed7bf8fd 339
Kojto 20:30b6ed7bf8fd 340 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 341 _hci.command_send(HCI_CMND_GETSOCKOPT, ptr, SOCKET_GET_SOCK_OPT_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 342
Kojto 20:30b6ed7bf8fd 343 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 344 _event.simplelink_wait_event(HCI_CMND_GETSOCKOPT, &tRetParams);
Kojto 20:30b6ed7bf8fd 345
Kojto 45:50ab13d8f2dc 346 if (((int8_t)tRetParams.iStatus) >= 0) {
Kojto 20:30b6ed7bf8fd 347 *optlen = 4;
Kojto 20:30b6ed7bf8fd 348 memcpy(optval, tRetParams.ucOptValue, 4);
Kojto 20:30b6ed7bf8fd 349 return (0);
Kojto 45:50ab13d8f2dc 350 } else {
Kojto 20:30b6ed7bf8fd 351 errno = tRetParams.iStatus;
Kojto 20:30b6ed7bf8fd 352 return errno;
Kojto 20:30b6ed7bf8fd 353 }
Kojto 20:30b6ed7bf8fd 354 }
Kojto 20:30b6ed7bf8fd 355
Kojto 20:30b6ed7bf8fd 356 int32_t cc3000_socket::simple_link_recv(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen, int32_t opcode) {
Kojto 20:30b6ed7bf8fd 357 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 358 tBsdReadReturnParams tSocketReadEvent;
Kojto 20:30b6ed7bf8fd 359
Kojto 20:30b6ed7bf8fd 360 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 361 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 362
Kojto 20:30b6ed7bf8fd 363 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 364 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 365 args = UINT32_TO_STREAM(args, len);
Kojto 20:30b6ed7bf8fd 366 args = UINT32_TO_STREAM(args, flags);
Kojto 20:30b6ed7bf8fd 367
Kojto 20:30b6ed7bf8fd 368 // Generate the read command, and wait for the
Kojto 20:30b6ed7bf8fd 369 _hci.command_send(opcode, ptr, SOCKET_RECV_FROM_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 370
Kojto 20:30b6ed7bf8fd 371 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 372 _event.simplelink_wait_event(opcode, &tSocketReadEvent);
Kojto 20:30b6ed7bf8fd 373
Kojto 20:30b6ed7bf8fd 374 // In case the number of bytes is more then zero - read data
Kojto 45:50ab13d8f2dc 375 if (tSocketReadEvent.iNumberOfBytes > 0) {
Kojto 20:30b6ed7bf8fd 376 // Wait for the data in a synchronous way. Here we assume that the bug is
Kojto 20:30b6ed7bf8fd 377 // big enough to store also parameters of receive from too....
Kojto 20:30b6ed7bf8fd 378 _event.simplelink_wait_data((uint8_t *)buf, (uint8_t *)from, (uint8_t *)fromlen);
Kojto 20:30b6ed7bf8fd 379 }
Kojto 20:30b6ed7bf8fd 380
Kojto 20:30b6ed7bf8fd 381 errno = tSocketReadEvent.iNumberOfBytes;
Kojto 20:30b6ed7bf8fd 382
Kojto 20:30b6ed7bf8fd 383 return(tSocketReadEvent.iNumberOfBytes);
Kojto 20:30b6ed7bf8fd 384 }
Kojto 20:30b6ed7bf8fd 385
Kojto 20:30b6ed7bf8fd 386 int32_t cc3000_socket::recv(int32_t sd, void *buf, int32_t len, int32_t flags) {
Kojto 20:30b6ed7bf8fd 387 return(simple_link_recv(sd, buf, len, flags, NULL, NULL, HCI_CMND_RECV));
Kojto 20:30b6ed7bf8fd 388 }
Kojto 20:30b6ed7bf8fd 389
Kojto 20:30b6ed7bf8fd 390 int32_t cc3000_socket::recvfrom(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen) {
Kojto 20:30b6ed7bf8fd 391 return(simple_link_recv(sd, buf, len, flags, from, fromlen, HCI_CMND_RECVFROM));
Kojto 20:30b6ed7bf8fd 392 }
Kojto 20:30b6ed7bf8fd 393
Kojto 20:30b6ed7bf8fd 394 int32_t cc3000_socket::simple_link_send(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, int32_t tolen, int32_t opcode) {
Kojto 20:30b6ed7bf8fd 395 uint8_t uArgSize = 0x00, addrlen = 0x00;
Kojto 20:30b6ed7bf8fd 396 uint8_t *ptr, *pDataPtr = NULL, *args;
Kojto 20:30b6ed7bf8fd 397 uint32_t addr_offset = 0x00;
Kojto 20:30b6ed7bf8fd 398 int32_t res;
Kojto 20:30b6ed7bf8fd 399 tBsdReadReturnParams tSocketSendEvent;
Kojto 20:30b6ed7bf8fd 400
Kojto 20:30b6ed7bf8fd 401 // Check the bsd_arguments
Kojto 45:50ab13d8f2dc 402 if (0 != (res = HostFlowControlConsumeBuff(sd))) {
Kojto 20:30b6ed7bf8fd 403 return res;
Kojto 20:30b6ed7bf8fd 404 }
Kojto 20:30b6ed7bf8fd 405
Kojto 20:30b6ed7bf8fd 406 //Update the number of sent packets
Kojto 20:30b6ed7bf8fd 407 uint16_t sent_packets = _simple_link.get_sent_packets();
Kojto 20:30b6ed7bf8fd 408 sent_packets++;
Kojto 20:30b6ed7bf8fd 409 _simple_link.set_sent_packets(sent_packets);
Kojto 20:30b6ed7bf8fd 410
Kojto 20:30b6ed7bf8fd 411 // Allocate a buffer and construct a packet and send it over spi
Kojto 20:30b6ed7bf8fd 412 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 413 args = (ptr + HEADERS_SIZE_DATA);
Kojto 20:30b6ed7bf8fd 414
Kojto 20:30b6ed7bf8fd 415 // Update the offset of data and parameters according to the command
Kojto 20:30b6ed7bf8fd 416 switch(opcode)
Kojto 20:30b6ed7bf8fd 417 {
Kojto 45:50ab13d8f2dc 418 case HCI_CMND_SENDTO:
Kojto 20:30b6ed7bf8fd 419 {
Kojto 20:30b6ed7bf8fd 420 addr_offset = len + sizeof(len) + sizeof(len);
Kojto 20:30b6ed7bf8fd 421 addrlen = 8;
Kojto 20:30b6ed7bf8fd 422 uArgSize = SOCKET_SENDTO_PARAMS_LEN;
Kojto 20:30b6ed7bf8fd 423 pDataPtr = ptr + HEADERS_SIZE_DATA + SOCKET_SENDTO_PARAMS_LEN;
Kojto 20:30b6ed7bf8fd 424 break;
Kojto 20:30b6ed7bf8fd 425 }
Kojto 20:30b6ed7bf8fd 426
Kojto 45:50ab13d8f2dc 427 case HCI_CMND_SEND:
Kojto 20:30b6ed7bf8fd 428 {
Kojto 20:30b6ed7bf8fd 429 tolen = 0;
Kojto 20:30b6ed7bf8fd 430 to = NULL;
Kojto 20:30b6ed7bf8fd 431 uArgSize = HCI_CMND_SEND_ARG_LENGTH;
Kojto 20:30b6ed7bf8fd 432 pDataPtr = ptr + HEADERS_SIZE_DATA + HCI_CMND_SEND_ARG_LENGTH;
Kojto 20:30b6ed7bf8fd 433 break;
Kojto 20:30b6ed7bf8fd 434 }
Kojto 20:30b6ed7bf8fd 435
Kojto 45:50ab13d8f2dc 436 default:
Kojto 20:30b6ed7bf8fd 437 {
Kojto 20:30b6ed7bf8fd 438 break;
Kojto 20:30b6ed7bf8fd 439 }
Kojto 20:30b6ed7bf8fd 440 }
Kojto 20:30b6ed7bf8fd 441
Kojto 20:30b6ed7bf8fd 442 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 443 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 444 args = UINT32_TO_STREAM(args, uArgSize - sizeof(sd));
Kojto 20:30b6ed7bf8fd 445 args = UINT32_TO_STREAM(args, len);
Kojto 20:30b6ed7bf8fd 446 args = UINT32_TO_STREAM(args, flags);
Kojto 20:30b6ed7bf8fd 447
Kojto 45:50ab13d8f2dc 448 if (opcode == HCI_CMND_SENDTO) {
Kojto 20:30b6ed7bf8fd 449 args = UINT32_TO_STREAM(args, addr_offset);
Kojto 20:30b6ed7bf8fd 450 args = UINT32_TO_STREAM(args, addrlen);
Kojto 20:30b6ed7bf8fd 451 }
Kojto 20:30b6ed7bf8fd 452
Kojto 20:30b6ed7bf8fd 453 // Copy the data received from user into the TX Buffer
Kojto 20:30b6ed7bf8fd 454 ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)buf), len);
Kojto 20:30b6ed7bf8fd 455
Kojto 20:30b6ed7bf8fd 456 // In case we are using SendTo, copy the to parameters
Kojto 45:50ab13d8f2dc 457 if (opcode == HCI_CMND_SENDTO) {
Kojto 20:30b6ed7bf8fd 458 ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)to), tolen);
Kojto 20:30b6ed7bf8fd 459 }
Kojto 20:30b6ed7bf8fd 460
Kojto 20:30b6ed7bf8fd 461 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 462 _hci.data_send(opcode, ptr, uArgSize, len,(uint8_t*)to, tolen);
Kojto 20:30b6ed7bf8fd 463 if (opcode == HCI_CMND_SENDTO)
Kojto 20:30b6ed7bf8fd 464 _event.simplelink_wait_event(HCI_EVNT_SENDTO, &tSocketSendEvent);
Kojto 20:30b6ed7bf8fd 465 else
Kojto 20:30b6ed7bf8fd 466 _event.simplelink_wait_event(HCI_EVNT_SEND, &tSocketSendEvent);
Kojto 20:30b6ed7bf8fd 467
heroic 56:9ab991c1d2db 468 return (len);
Kojto 20:30b6ed7bf8fd 469 }
Kojto 20:30b6ed7bf8fd 470
Kojto 20:30b6ed7bf8fd 471 int32_t cc3000_socket::send(int32_t sd, const void *buf, int32_t len, int32_t flags) {
Kojto 20:30b6ed7bf8fd 472 return(simple_link_send(sd, buf, len, flags, NULL, 0, HCI_CMND_SEND));
Kojto 20:30b6ed7bf8fd 473 }
Kojto 20:30b6ed7bf8fd 474
Kojto 20:30b6ed7bf8fd 475 int32_t cc3000_socket::sendto(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, socklen_t tolen) {
Kojto 20:30b6ed7bf8fd 476 return(simple_link_send(sd, buf, len, flags, to, tolen, HCI_CMND_SENDTO));
Kojto 20:30b6ed7bf8fd 477 }
Kojto 20:30b6ed7bf8fd 478
Kojto 20:30b6ed7bf8fd 479 int32_t cc3000_socket::mdns_advertiser(uint16_t mdns_enabled, uint8_t *device_service_name, uint16_t device_service_name_length) {
Kojto 20:30b6ed7bf8fd 480 int32_t ret;
Kojto 20:30b6ed7bf8fd 481 uint8_t *pTxBuffer, *pArgs;
Kojto 20:30b6ed7bf8fd 482
Kojto 45:50ab13d8f2dc 483 if (device_service_name_length > MDNS_DEVICE_SERVICE_MAX_LENGTH) {
Kojto 20:30b6ed7bf8fd 484 return EFAIL;
Kojto 20:30b6ed7bf8fd 485 }
Kojto 20:30b6ed7bf8fd 486
Kojto 20:30b6ed7bf8fd 487 pTxBuffer = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 488 pArgs = (pTxBuffer + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 489
Kojto 20:30b6ed7bf8fd 490 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 491 pArgs = UINT32_TO_STREAM(pArgs, mdns_enabled);
Kojto 20:30b6ed7bf8fd 492 pArgs = UINT32_TO_STREAM(pArgs, 8);
Kojto 20:30b6ed7bf8fd 493 pArgs = UINT32_TO_STREAM(pArgs, device_service_name_length);
Kojto 20:30b6ed7bf8fd 494 ARRAY_TO_STREAM(pArgs, device_service_name, device_service_name_length);
Kojto 20:30b6ed7bf8fd 495
Kojto 20:30b6ed7bf8fd 496 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 497 _hci.command_send(HCI_CMND_MDNS_ADVERTISE, pTxBuffer, SOCKET_MDNS_ADVERTISE_PARAMS_LEN + device_service_name_length);
Kojto 20:30b6ed7bf8fd 498
Kojto 20:30b6ed7bf8fd 499 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 500 _event.simplelink_wait_event(HCI_EVNT_MDNS_ADVERTISE, &ret);
Kojto 20:30b6ed7bf8fd 501
Kojto 20:30b6ed7bf8fd 502 return ret;
Kojto 20:30b6ed7bf8fd 503 }
Kojto 20:30b6ed7bf8fd 504
Kojto 20:30b6ed7bf8fd 505
Kojto 20:30b6ed7bf8fd 506 #ifndef CC3000_TINY_DRIVER
Kojto 20:30b6ed7bf8fd 507 int32_t cc3000_socket::gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) {
Kojto 20:30b6ed7bf8fd 508 tBsdGethostbynameParams ret;
Kojto 20:30b6ed7bf8fd 509 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 510
Kojto 20:30b6ed7bf8fd 511 errno = EFAIL;
Kojto 20:30b6ed7bf8fd 512
Kojto 45:50ab13d8f2dc 513 if (name_length > HOSTNAME_MAX_LENGTH) {
Kojto 20:30b6ed7bf8fd 514 return errno;
Kojto 20:30b6ed7bf8fd 515 }
Kojto 20:30b6ed7bf8fd 516
Kojto 20:30b6ed7bf8fd 517 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 518 args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 519
Kojto 20:30b6ed7bf8fd 520 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 521 args = UINT32_TO_STREAM(args, 8);
Kojto 20:30b6ed7bf8fd 522 args = UINT32_TO_STREAM(args, name_length);
Kojto 20:30b6ed7bf8fd 523 ARRAY_TO_STREAM(args, hostname, name_length);
Kojto 20:30b6ed7bf8fd 524
Kojto 20:30b6ed7bf8fd 525 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 526 _hci.command_send(HCI_CMND_GETHOSTNAME, ptr, SOCKET_GET_HOST_BY_NAME_PARAMS_LEN + name_length - 1);
Kojto 20:30b6ed7bf8fd 527
Kojto 20:30b6ed7bf8fd 528 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 529 _event.simplelink_wait_event(HCI_EVNT_BSD_GETHOSTBYNAME, &ret);
Kojto 20:30b6ed7bf8fd 530
Kojto 20:30b6ed7bf8fd 531 errno = ret.retVal;
Kojto 20:30b6ed7bf8fd 532
Kojto 20:30b6ed7bf8fd 533 (*((int32_t*)out_ip_addr)) = ret.outputAddress;
Kojto 20:30b6ed7bf8fd 534
Kojto 20:30b6ed7bf8fd 535 return (errno);
Kojto 20:30b6ed7bf8fd 536 }
Kojto 20:30b6ed7bf8fd 537
Kojto 20:30b6ed7bf8fd 538 int32_t cc3000_socket::setsockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen) {
Kojto 20:30b6ed7bf8fd 539 int32_t ret;
Kojto 20:30b6ed7bf8fd 540 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 541
Kojto 20:30b6ed7bf8fd 542 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 543 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 544
Kojto 20:30b6ed7bf8fd 545 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 546 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 547 args = UINT32_TO_STREAM(args, level);
Kojto 20:30b6ed7bf8fd 548 args = UINT32_TO_STREAM(args, optname);
Kojto 20:30b6ed7bf8fd 549 args = UINT32_TO_STREAM(args, 0x00000008);
Kojto 20:30b6ed7bf8fd 550 args = UINT32_TO_STREAM(args, optlen);
Kojto 20:30b6ed7bf8fd 551 ARRAY_TO_STREAM(args, ((uint8_t *)optval), optlen);
Kojto 20:30b6ed7bf8fd 552
Kojto 20:30b6ed7bf8fd 553 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 554 _hci.command_send(HCI_CMND_SETSOCKOPT, ptr, SOCKET_SET_SOCK_OPT_PARAMS_LEN + optlen);
Kojto 20:30b6ed7bf8fd 555
Kojto 20:30b6ed7bf8fd 556 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 557 _event.simplelink_wait_event(HCI_CMND_SETSOCKOPT, &ret);
Kojto 20:30b6ed7bf8fd 558
Kojto 45:50ab13d8f2dc 559 if (ret >= 0) {
Kojto 20:30b6ed7bf8fd 560 return (0);
Kojto 45:50ab13d8f2dc 561 } else {
Kojto 20:30b6ed7bf8fd 562 errno = ret;
Kojto 20:30b6ed7bf8fd 563 return ret;
Kojto 20:30b6ed7bf8fd 564 }
Kojto 20:30b6ed7bf8fd 565 }
Kojto 20:30b6ed7bf8fd 566
Kojto 20:30b6ed7bf8fd 567 #endif
Kojto 20:30b6ed7bf8fd 568
Kojto 45:50ab13d8f2dc 569 char* cc3000_socket::inet_ntoa_r(uint32_t s_addr, char *buf, int buflen)
SolderSplashLabs 42:bd2c631a031a 570 {
Kojto 45:50ab13d8f2dc 571 char inv[3];
Kojto 45:50ab13d8f2dc 572 char *rp;
Kojto 45:50ab13d8f2dc 573 uint8_t *ap;
Kojto 45:50ab13d8f2dc 574 uint8_t rem;
Kojto 45:50ab13d8f2dc 575 uint8_t n;
Kojto 45:50ab13d8f2dc 576 uint8_t i;
Kojto 45:50ab13d8f2dc 577 int len = 0;
SolderSplashLabs 42:bd2c631a031a 578
Kojto 45:50ab13d8f2dc 579 rp = buf;
Kojto 45:50ab13d8f2dc 580 ap = (uint8_t *)&s_addr;
Kojto 45:50ab13d8f2dc 581 for (n = 0; n < 4; n++) {
Kojto 45:50ab13d8f2dc 582 i = 0;
Kojto 45:50ab13d8f2dc 583 do {
Kojto 45:50ab13d8f2dc 584 rem = *ap % (uint8_t)10;
Kojto 45:50ab13d8f2dc 585 *ap /= (uint8_t)10;
Kojto 45:50ab13d8f2dc 586 inv[i++] = '0' + rem;
Kojto 45:50ab13d8f2dc 587 } while(*ap);
Kojto 45:50ab13d8f2dc 588 while(i--) {
Kojto 45:50ab13d8f2dc 589 if (len++ >= buflen) {
Kojto 45:50ab13d8f2dc 590 return NULL;
Kojto 45:50ab13d8f2dc 591 }
Kojto 45:50ab13d8f2dc 592 *rp++ = inv[i];
Kojto 45:50ab13d8f2dc 593 }
Kojto 45:50ab13d8f2dc 594 if (len++ >= buflen) {
Kojto 45:50ab13d8f2dc 595 return NULL;
Kojto 45:50ab13d8f2dc 596 }
Kojto 45:50ab13d8f2dc 597 *rp++ = '.';
Kojto 45:50ab13d8f2dc 598 ap++;
SolderSplashLabs 42:bd2c631a031a 599 }
Kojto 45:50ab13d8f2dc 600 *--rp = 0;
Kojto 45:50ab13d8f2dc 601 return buf;
SolderSplashLabs 42:bd2c631a031a 602 }
SolderSplashLabs 42:bd2c631a031a 603
Kojto 45:50ab13d8f2dc 604 } // mbed_cc3000 namespace