cc3000 hostdriver with the mbed socket interface. Hacked TCP Classes for better control of non-blocking sockets.

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
mfurseman
Date:
Fri Oct 31 19:44:52 2014 +0000
Revision:
49:f2ffd6301ec5
Parent:
48:1e6068103f0b
Hacked _closed_sockets to public and used it for socket

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
mfurseman 48:1e6068103f0b 303 // Without this wait the select commmand blocks when called frequently
mfurseman 48:1e6068103f0b 304 wait_ms(6);
mfurseman 48:1e6068103f0b 305
Kojto 20:30b6ed7bf8fd 306 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 307 _event.simplelink_wait_event(HCI_EVNT_SELECT, &tParams);
mfurseman 48:1e6068103f0b 308
Kojto 20:30b6ed7bf8fd 309 // Update actually read FD
Kojto 45:50ab13d8f2dc 310 if (tParams.iStatus >= 0) {
Kojto 45:50ab13d8f2dc 311 if (readsds) {
Kojto 20:30b6ed7bf8fd 312 memcpy(readsds, &tParams.uiRdfd, sizeof(tParams.uiRdfd));
Kojto 20:30b6ed7bf8fd 313 }
Kojto 20:30b6ed7bf8fd 314
Kojto 45:50ab13d8f2dc 315 if (writesds) {
Kojto 20:30b6ed7bf8fd 316 memcpy(writesds, &tParams.uiWrfd, sizeof(tParams.uiWrfd));
Kojto 20:30b6ed7bf8fd 317 }
Kojto 20:30b6ed7bf8fd 318
Kojto 45:50ab13d8f2dc 319 if (exceptsds) {
Kojto 20:30b6ed7bf8fd 320 memcpy(exceptsds, &tParams.uiExfd, sizeof(tParams.uiExfd));
Kojto 20:30b6ed7bf8fd 321 }
Kojto 20:30b6ed7bf8fd 322
Kojto 20:30b6ed7bf8fd 323 return(tParams.iStatus);
Kojto 20:30b6ed7bf8fd 324
Kojto 45:50ab13d8f2dc 325 } else {
Kojto 20:30b6ed7bf8fd 326 errno = tParams.iStatus;
Kojto 45:50ab13d8f2dc 327 return -1;
Kojto 20:30b6ed7bf8fd 328 }
Kojto 20:30b6ed7bf8fd 329 }
Kojto 20:30b6ed7bf8fd 330
Kojto 20:30b6ed7bf8fd 331 int32_t cc3000_socket::getsockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen) {
Kojto 20:30b6ed7bf8fd 332 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 333 tBsdGetSockOptReturnParams tRetParams;
Kojto 20:30b6ed7bf8fd 334
Kojto 20:30b6ed7bf8fd 335 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 336 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 337
Kojto 20:30b6ed7bf8fd 338 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 339 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 340 args = UINT32_TO_STREAM(args, level);
Kojto 20:30b6ed7bf8fd 341 args = UINT32_TO_STREAM(args, optname);
Kojto 20:30b6ed7bf8fd 342
Kojto 20:30b6ed7bf8fd 343 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 344 _hci.command_send(HCI_CMND_GETSOCKOPT, ptr, SOCKET_GET_SOCK_OPT_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 345
Kojto 20:30b6ed7bf8fd 346 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 347 _event.simplelink_wait_event(HCI_CMND_GETSOCKOPT, &tRetParams);
Kojto 20:30b6ed7bf8fd 348
Kojto 45:50ab13d8f2dc 349 if (((int8_t)tRetParams.iStatus) >= 0) {
Kojto 20:30b6ed7bf8fd 350 *optlen = 4;
Kojto 20:30b6ed7bf8fd 351 memcpy(optval, tRetParams.ucOptValue, 4);
Kojto 20:30b6ed7bf8fd 352 return (0);
Kojto 45:50ab13d8f2dc 353 } else {
Kojto 20:30b6ed7bf8fd 354 errno = tRetParams.iStatus;
Kojto 20:30b6ed7bf8fd 355 return errno;
Kojto 20:30b6ed7bf8fd 356 }
Kojto 20:30b6ed7bf8fd 357 }
Kojto 20:30b6ed7bf8fd 358
Kojto 20:30b6ed7bf8fd 359 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 360 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 361 tBsdReadReturnParams tSocketReadEvent;
Kojto 20:30b6ed7bf8fd 362
Kojto 20:30b6ed7bf8fd 363 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 364 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 365
Kojto 20:30b6ed7bf8fd 366 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 367 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 368 args = UINT32_TO_STREAM(args, len);
Kojto 20:30b6ed7bf8fd 369 args = UINT32_TO_STREAM(args, flags);
Kojto 20:30b6ed7bf8fd 370
Kojto 20:30b6ed7bf8fd 371 // Generate the read command, and wait for the
Kojto 20:30b6ed7bf8fd 372 _hci.command_send(opcode, ptr, SOCKET_RECV_FROM_PARAMS_LEN);
Kojto 20:30b6ed7bf8fd 373
Kojto 20:30b6ed7bf8fd 374 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 375 _event.simplelink_wait_event(opcode, &tSocketReadEvent);
Kojto 20:30b6ed7bf8fd 376
Kojto 20:30b6ed7bf8fd 377 // In case the number of bytes is more then zero - read data
Kojto 45:50ab13d8f2dc 378 if (tSocketReadEvent.iNumberOfBytes > 0) {
Kojto 20:30b6ed7bf8fd 379 // Wait for the data in a synchronous way. Here we assume that the bug is
Kojto 20:30b6ed7bf8fd 380 // big enough to store also parameters of receive from too....
Kojto 20:30b6ed7bf8fd 381 _event.simplelink_wait_data((uint8_t *)buf, (uint8_t *)from, (uint8_t *)fromlen);
Kojto 20:30b6ed7bf8fd 382 }
Kojto 20:30b6ed7bf8fd 383
Kojto 20:30b6ed7bf8fd 384 errno = tSocketReadEvent.iNumberOfBytes;
Kojto 20:30b6ed7bf8fd 385
Kojto 20:30b6ed7bf8fd 386 return(tSocketReadEvent.iNumberOfBytes);
Kojto 20:30b6ed7bf8fd 387 }
Kojto 20:30b6ed7bf8fd 388
Kojto 20:30b6ed7bf8fd 389 int32_t cc3000_socket::recv(int32_t sd, void *buf, int32_t len, int32_t flags) {
Kojto 20:30b6ed7bf8fd 390 return(simple_link_recv(sd, buf, len, flags, NULL, NULL, HCI_CMND_RECV));
Kojto 20:30b6ed7bf8fd 391 }
Kojto 20:30b6ed7bf8fd 392
Kojto 20:30b6ed7bf8fd 393 int32_t cc3000_socket::recvfrom(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen) {
Kojto 20:30b6ed7bf8fd 394 return(simple_link_recv(sd, buf, len, flags, from, fromlen, HCI_CMND_RECVFROM));
Kojto 20:30b6ed7bf8fd 395 }
Kojto 20:30b6ed7bf8fd 396
Kojto 20:30b6ed7bf8fd 397 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 398 uint8_t uArgSize = 0x00, addrlen = 0x00;
Kojto 20:30b6ed7bf8fd 399 uint8_t *ptr, *pDataPtr = NULL, *args;
Kojto 20:30b6ed7bf8fd 400 uint32_t addr_offset = 0x00;
Kojto 20:30b6ed7bf8fd 401 int32_t res;
Kojto 20:30b6ed7bf8fd 402 tBsdReadReturnParams tSocketSendEvent;
Kojto 20:30b6ed7bf8fd 403
Kojto 20:30b6ed7bf8fd 404 // Check the bsd_arguments
Kojto 45:50ab13d8f2dc 405 if (0 != (res = HostFlowControlConsumeBuff(sd))) {
Kojto 20:30b6ed7bf8fd 406 return res;
Kojto 20:30b6ed7bf8fd 407 }
Kojto 20:30b6ed7bf8fd 408
Kojto 20:30b6ed7bf8fd 409 //Update the number of sent packets
Kojto 20:30b6ed7bf8fd 410 uint16_t sent_packets = _simple_link.get_sent_packets();
Kojto 20:30b6ed7bf8fd 411 sent_packets++;
Kojto 20:30b6ed7bf8fd 412 _simple_link.set_sent_packets(sent_packets);
Kojto 20:30b6ed7bf8fd 413
Kojto 20:30b6ed7bf8fd 414 // Allocate a buffer and construct a packet and send it over spi
Kojto 20:30b6ed7bf8fd 415 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 416 args = (ptr + HEADERS_SIZE_DATA);
Kojto 20:30b6ed7bf8fd 417
Kojto 20:30b6ed7bf8fd 418 // Update the offset of data and parameters according to the command
Kojto 20:30b6ed7bf8fd 419 switch(opcode)
Kojto 20:30b6ed7bf8fd 420 {
Kojto 45:50ab13d8f2dc 421 case HCI_CMND_SENDTO:
Kojto 20:30b6ed7bf8fd 422 {
Kojto 20:30b6ed7bf8fd 423 addr_offset = len + sizeof(len) + sizeof(len);
Kojto 20:30b6ed7bf8fd 424 addrlen = 8;
Kojto 20:30b6ed7bf8fd 425 uArgSize = SOCKET_SENDTO_PARAMS_LEN;
Kojto 20:30b6ed7bf8fd 426 pDataPtr = ptr + HEADERS_SIZE_DATA + SOCKET_SENDTO_PARAMS_LEN;
Kojto 20:30b6ed7bf8fd 427 break;
Kojto 20:30b6ed7bf8fd 428 }
Kojto 20:30b6ed7bf8fd 429
Kojto 45:50ab13d8f2dc 430 case HCI_CMND_SEND:
Kojto 20:30b6ed7bf8fd 431 {
Kojto 20:30b6ed7bf8fd 432 tolen = 0;
Kojto 20:30b6ed7bf8fd 433 to = NULL;
Kojto 20:30b6ed7bf8fd 434 uArgSize = HCI_CMND_SEND_ARG_LENGTH;
Kojto 20:30b6ed7bf8fd 435 pDataPtr = ptr + HEADERS_SIZE_DATA + HCI_CMND_SEND_ARG_LENGTH;
Kojto 20:30b6ed7bf8fd 436 break;
Kojto 20:30b6ed7bf8fd 437 }
Kojto 20:30b6ed7bf8fd 438
Kojto 45:50ab13d8f2dc 439 default:
Kojto 20:30b6ed7bf8fd 440 {
Kojto 20:30b6ed7bf8fd 441 break;
Kojto 20:30b6ed7bf8fd 442 }
Kojto 20:30b6ed7bf8fd 443 }
Kojto 20:30b6ed7bf8fd 444
Kojto 20:30b6ed7bf8fd 445 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 446 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 447 args = UINT32_TO_STREAM(args, uArgSize - sizeof(sd));
Kojto 20:30b6ed7bf8fd 448 args = UINT32_TO_STREAM(args, len);
Kojto 20:30b6ed7bf8fd 449 args = UINT32_TO_STREAM(args, flags);
Kojto 20:30b6ed7bf8fd 450
Kojto 45:50ab13d8f2dc 451 if (opcode == HCI_CMND_SENDTO) {
Kojto 20:30b6ed7bf8fd 452 args = UINT32_TO_STREAM(args, addr_offset);
Kojto 20:30b6ed7bf8fd 453 args = UINT32_TO_STREAM(args, addrlen);
Kojto 20:30b6ed7bf8fd 454 }
Kojto 20:30b6ed7bf8fd 455
Kojto 20:30b6ed7bf8fd 456 // Copy the data received from user into the TX Buffer
Kojto 20:30b6ed7bf8fd 457 ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)buf), len);
Kojto 20:30b6ed7bf8fd 458
Kojto 20:30b6ed7bf8fd 459 // In case we are using SendTo, copy the to parameters
Kojto 45:50ab13d8f2dc 460 if (opcode == HCI_CMND_SENDTO) {
Kojto 20:30b6ed7bf8fd 461 ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)to), tolen);
Kojto 20:30b6ed7bf8fd 462 }
Kojto 20:30b6ed7bf8fd 463
Kojto 20:30b6ed7bf8fd 464 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 465 _hci.data_send(opcode, ptr, uArgSize, len,(uint8_t*)to, tolen);
Kojto 20:30b6ed7bf8fd 466 if (opcode == HCI_CMND_SENDTO)
Kojto 20:30b6ed7bf8fd 467 _event.simplelink_wait_event(HCI_EVNT_SENDTO, &tSocketSendEvent);
Kojto 20:30b6ed7bf8fd 468 else
Kojto 20:30b6ed7bf8fd 469 _event.simplelink_wait_event(HCI_EVNT_SEND, &tSocketSendEvent);
Kojto 20:30b6ed7bf8fd 470
Kojto 20:30b6ed7bf8fd 471 return (len);
Kojto 20:30b6ed7bf8fd 472 }
Kojto 20:30b6ed7bf8fd 473
Kojto 20:30b6ed7bf8fd 474 int32_t cc3000_socket::send(int32_t sd, const void *buf, int32_t len, int32_t flags) {
Kojto 20:30b6ed7bf8fd 475 return(simple_link_send(sd, buf, len, flags, NULL, 0, HCI_CMND_SEND));
Kojto 20:30b6ed7bf8fd 476 }
Kojto 20:30b6ed7bf8fd 477
Kojto 20:30b6ed7bf8fd 478 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 479 return(simple_link_send(sd, buf, len, flags, to, tolen, HCI_CMND_SENDTO));
Kojto 20:30b6ed7bf8fd 480 }
Kojto 20:30b6ed7bf8fd 481
Kojto 20:30b6ed7bf8fd 482 int32_t cc3000_socket::mdns_advertiser(uint16_t mdns_enabled, uint8_t *device_service_name, uint16_t device_service_name_length) {
Kojto 20:30b6ed7bf8fd 483 int32_t ret;
Kojto 20:30b6ed7bf8fd 484 uint8_t *pTxBuffer, *pArgs;
Kojto 20:30b6ed7bf8fd 485
Kojto 45:50ab13d8f2dc 486 if (device_service_name_length > MDNS_DEVICE_SERVICE_MAX_LENGTH) {
Kojto 20:30b6ed7bf8fd 487 return EFAIL;
Kojto 20:30b6ed7bf8fd 488 }
Kojto 20:30b6ed7bf8fd 489
Kojto 20:30b6ed7bf8fd 490 pTxBuffer = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 491 pArgs = (pTxBuffer + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 492
Kojto 20:30b6ed7bf8fd 493 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 494 pArgs = UINT32_TO_STREAM(pArgs, mdns_enabled);
Kojto 20:30b6ed7bf8fd 495 pArgs = UINT32_TO_STREAM(pArgs, 8);
Kojto 20:30b6ed7bf8fd 496 pArgs = UINT32_TO_STREAM(pArgs, device_service_name_length);
Kojto 20:30b6ed7bf8fd 497 ARRAY_TO_STREAM(pArgs, device_service_name, device_service_name_length);
Kojto 20:30b6ed7bf8fd 498
Kojto 20:30b6ed7bf8fd 499 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 500 _hci.command_send(HCI_CMND_MDNS_ADVERTISE, pTxBuffer, SOCKET_MDNS_ADVERTISE_PARAMS_LEN + device_service_name_length);
Kojto 20:30b6ed7bf8fd 501
Kojto 20:30b6ed7bf8fd 502 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 503 _event.simplelink_wait_event(HCI_EVNT_MDNS_ADVERTISE, &ret);
Kojto 20:30b6ed7bf8fd 504
Kojto 20:30b6ed7bf8fd 505 return ret;
Kojto 20:30b6ed7bf8fd 506 }
Kojto 20:30b6ed7bf8fd 507
Kojto 20:30b6ed7bf8fd 508
Kojto 20:30b6ed7bf8fd 509 #ifndef CC3000_TINY_DRIVER
Kojto 20:30b6ed7bf8fd 510 int32_t cc3000_socket::gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) {
Kojto 20:30b6ed7bf8fd 511 tBsdGethostbynameParams ret;
Kojto 20:30b6ed7bf8fd 512 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 513
Kojto 20:30b6ed7bf8fd 514 errno = EFAIL;
Kojto 20:30b6ed7bf8fd 515
Kojto 45:50ab13d8f2dc 516 if (name_length > HOSTNAME_MAX_LENGTH) {
Kojto 20:30b6ed7bf8fd 517 return errno;
Kojto 20:30b6ed7bf8fd 518 }
Kojto 20:30b6ed7bf8fd 519
Kojto 20:30b6ed7bf8fd 520 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 521 args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 522
Kojto 20:30b6ed7bf8fd 523 // Fill in HCI packet structure
Kojto 20:30b6ed7bf8fd 524 args = UINT32_TO_STREAM(args, 8);
Kojto 20:30b6ed7bf8fd 525 args = UINT32_TO_STREAM(args, name_length);
Kojto 20:30b6ed7bf8fd 526 ARRAY_TO_STREAM(args, hostname, name_length);
Kojto 20:30b6ed7bf8fd 527
Kojto 20:30b6ed7bf8fd 528 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 529 _hci.command_send(HCI_CMND_GETHOSTNAME, ptr, SOCKET_GET_HOST_BY_NAME_PARAMS_LEN + name_length - 1);
Kojto 20:30b6ed7bf8fd 530
Kojto 20:30b6ed7bf8fd 531 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 532 _event.simplelink_wait_event(HCI_EVNT_BSD_GETHOSTBYNAME, &ret);
Kojto 20:30b6ed7bf8fd 533
Kojto 20:30b6ed7bf8fd 534 errno = ret.retVal;
Kojto 20:30b6ed7bf8fd 535
Kojto 20:30b6ed7bf8fd 536 (*((int32_t*)out_ip_addr)) = ret.outputAddress;
Kojto 20:30b6ed7bf8fd 537
Kojto 20:30b6ed7bf8fd 538 return (errno);
Kojto 20:30b6ed7bf8fd 539 }
Kojto 20:30b6ed7bf8fd 540
Kojto 20:30b6ed7bf8fd 541 int32_t cc3000_socket::setsockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen) {
Kojto 20:30b6ed7bf8fd 542 int32_t ret;
Kojto 20:30b6ed7bf8fd 543 uint8_t *ptr, *args;
Kojto 20:30b6ed7bf8fd 544
Kojto 20:30b6ed7bf8fd 545 ptr = _simple_link.get_transmit_buffer();
Kojto 20:30b6ed7bf8fd 546 args = (ptr + HEADERS_SIZE_CMD);
Kojto 20:30b6ed7bf8fd 547
Kojto 20:30b6ed7bf8fd 548 // Fill in temporary command buffer
Kojto 20:30b6ed7bf8fd 549 args = UINT32_TO_STREAM(args, sd);
Kojto 20:30b6ed7bf8fd 550 args = UINT32_TO_STREAM(args, level);
Kojto 20:30b6ed7bf8fd 551 args = UINT32_TO_STREAM(args, optname);
Kojto 20:30b6ed7bf8fd 552 args = UINT32_TO_STREAM(args, 0x00000008);
Kojto 20:30b6ed7bf8fd 553 args = UINT32_TO_STREAM(args, optlen);
Kojto 20:30b6ed7bf8fd 554 ARRAY_TO_STREAM(args, ((uint8_t *)optval), optlen);
Kojto 20:30b6ed7bf8fd 555
Kojto 20:30b6ed7bf8fd 556 // Initiate a HCI command
Kojto 20:30b6ed7bf8fd 557 _hci.command_send(HCI_CMND_SETSOCKOPT, ptr, SOCKET_SET_SOCK_OPT_PARAMS_LEN + optlen);
Kojto 20:30b6ed7bf8fd 558
Kojto 20:30b6ed7bf8fd 559 // Since we are in blocking state - wait for event complete
Kojto 20:30b6ed7bf8fd 560 _event.simplelink_wait_event(HCI_CMND_SETSOCKOPT, &ret);
Kojto 20:30b6ed7bf8fd 561
Kojto 45:50ab13d8f2dc 562 if (ret >= 0) {
Kojto 20:30b6ed7bf8fd 563 return (0);
Kojto 45:50ab13d8f2dc 564 } else {
Kojto 20:30b6ed7bf8fd 565 errno = ret;
Kojto 20:30b6ed7bf8fd 566 return ret;
Kojto 20:30b6ed7bf8fd 567 }
Kojto 20:30b6ed7bf8fd 568 }
Kojto 20:30b6ed7bf8fd 569
Kojto 20:30b6ed7bf8fd 570 #endif
Kojto 20:30b6ed7bf8fd 571
Kojto 45:50ab13d8f2dc 572 char* cc3000_socket::inet_ntoa_r(uint32_t s_addr, char *buf, int buflen)
SolderSplashLabs 42:bd2c631a031a 573 {
Kojto 45:50ab13d8f2dc 574 char inv[3];
Kojto 45:50ab13d8f2dc 575 char *rp;
Kojto 45:50ab13d8f2dc 576 uint8_t *ap;
Kojto 45:50ab13d8f2dc 577 uint8_t rem;
Kojto 45:50ab13d8f2dc 578 uint8_t n;
Kojto 45:50ab13d8f2dc 579 uint8_t i;
Kojto 45:50ab13d8f2dc 580 int len = 0;
SolderSplashLabs 42:bd2c631a031a 581
Kojto 45:50ab13d8f2dc 582 rp = buf;
Kojto 45:50ab13d8f2dc 583 ap = (uint8_t *)&s_addr;
Kojto 45:50ab13d8f2dc 584 for (n = 0; n < 4; n++) {
Kojto 45:50ab13d8f2dc 585 i = 0;
Kojto 45:50ab13d8f2dc 586 do {
Kojto 45:50ab13d8f2dc 587 rem = *ap % (uint8_t)10;
Kojto 45:50ab13d8f2dc 588 *ap /= (uint8_t)10;
Kojto 45:50ab13d8f2dc 589 inv[i++] = '0' + rem;
Kojto 45:50ab13d8f2dc 590 } while(*ap);
Kojto 45:50ab13d8f2dc 591 while(i--) {
Kojto 45:50ab13d8f2dc 592 if (len++ >= buflen) {
Kojto 45:50ab13d8f2dc 593 return NULL;
Kojto 45:50ab13d8f2dc 594 }
Kojto 45:50ab13d8f2dc 595 *rp++ = inv[i];
Kojto 45:50ab13d8f2dc 596 }
Kojto 45:50ab13d8f2dc 597 if (len++ >= buflen) {
Kojto 45:50ab13d8f2dc 598 return NULL;
Kojto 45:50ab13d8f2dc 599 }
Kojto 45:50ab13d8f2dc 600 *rp++ = '.';
Kojto 45:50ab13d8f2dc 601 ap++;
SolderSplashLabs 42:bd2c631a031a 602 }
Kojto 45:50ab13d8f2dc 603 *--rp = 0;
Kojto 45:50ab13d8f2dc 604 return buf;
SolderSplashLabs 42:bd2c631a031a 605 }
SolderSplashLabs 42:bd2c631a031a 606
Kojto 45:50ab13d8f2dc 607 } // mbed_cc3000 namespace