A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Committer:
dflet
Date:
Mon Sep 16 18:37:14 2013 +0000
Revision:
2:e6a185df9e4c
Parent:
0:6ad60d78b315
ADC and Leds now work on board and config.html page.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:6ad60d78b315 1 /*****************************************************************************
dflet 0:6ad60d78b315 2 *
dflet 0:6ad60d78b315 3 * socket.c - CC3000 Host Driver Implementation.
dflet 0:6ad60d78b315 4 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
dflet 0:6ad60d78b315 5 *
dflet 0:6ad60d78b315 6 * Redistribution and use in source and binary forms, with or without
dflet 0:6ad60d78b315 7 * modification, are permitted provided that the following conditions
dflet 0:6ad60d78b315 8 * are met:
dflet 0:6ad60d78b315 9 *
dflet 0:6ad60d78b315 10 * Redistributions of source code must retain the above copyright
dflet 0:6ad60d78b315 11 * notice, this list of conditions and the following disclaimer.
dflet 0:6ad60d78b315 12 *
dflet 0:6ad60d78b315 13 * Redistributions in binary form must reproduce the above copyright
dflet 0:6ad60d78b315 14 * notice, this list of conditions and the following disclaimer in the
dflet 0:6ad60d78b315 15 * documentation and/or other materials provided with the
dflet 0:6ad60d78b315 16 * distribution.
dflet 0:6ad60d78b315 17 *
dflet 0:6ad60d78b315 18 * Neither the name of Texas Instruments Incorporated nor the names of
dflet 0:6ad60d78b315 19 * its contributors may be used to endorse or promote products derived
dflet 0:6ad60d78b315 20 * from this software without specific prior written permission.
dflet 0:6ad60d78b315 21 *
dflet 0:6ad60d78b315 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dflet 0:6ad60d78b315 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dflet 0:6ad60d78b315 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dflet 0:6ad60d78b315 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dflet 0:6ad60d78b315 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dflet 0:6ad60d78b315 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dflet 0:6ad60d78b315 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dflet 0:6ad60d78b315 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dflet 0:6ad60d78b315 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dflet 0:6ad60d78b315 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dflet 0:6ad60d78b315 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dflet 0:6ad60d78b315 33 *
dflet 0:6ad60d78b315 34 *****************************************************************************/
dflet 0:6ad60d78b315 35
dflet 0:6ad60d78b315 36 //*****************************************************************************
dflet 0:6ad60d78b315 37 //
dflet 0:6ad60d78b315 38 //! \addtogroup socket_api
dflet 0:6ad60d78b315 39 //! @{
dflet 0:6ad60d78b315 40 //
dflet 0:6ad60d78b315 41 //*****************************************************************************
dflet 0:6ad60d78b315 42
dflet 0:6ad60d78b315 43 #include <stdio.h>
dflet 0:6ad60d78b315 44 #include <string.h>
dflet 0:6ad60d78b315 45 #include <stdlib.h>
dflet 0:6ad60d78b315 46 #include "hci.h"
dflet 0:6ad60d78b315 47 #include "socket.h"
dflet 0:6ad60d78b315 48 #include "evnt_handler.h"
dflet 0:6ad60d78b315 49 #include "netapp.h"
dflet 0:6ad60d78b315 50
dflet 0:6ad60d78b315 51
dflet 0:6ad60d78b315 52
dflet 0:6ad60d78b315 53 //Enable this flag if and only if you must comply with BSD socket
dflet 0:6ad60d78b315 54 //close() function
dflet 0:6ad60d78b315 55 #ifdef _API_USE_BSD_CLOSE
dflet 0:6ad60d78b315 56 #define close(sd) closesocket(sd)
dflet 0:6ad60d78b315 57 #endif
dflet 0:6ad60d78b315 58
dflet 0:6ad60d78b315 59 //Enable this flag if and only if you must comply with BSD socket read() and
dflet 0:6ad60d78b315 60 //write() functions
dflet 0:6ad60d78b315 61 #ifdef _API_USE_BSD_READ_WRITE
dflet 0:6ad60d78b315 62 #define read(sd, buf, len, flags) recv(sd, buf, len, flags)
dflet 0:6ad60d78b315 63 #define write(sd, buf, len, flags) send(sd, buf, len, flags)
dflet 0:6ad60d78b315 64 #endif
dflet 0:6ad60d78b315 65
dflet 0:6ad60d78b315 66 #define SOCKET_OPEN_PARAMS_LEN (12)
dflet 0:6ad60d78b315 67 #define SOCKET_CLOSE_PARAMS_LEN (4)
dflet 0:6ad60d78b315 68 #define SOCKET_ACCEPT_PARAMS_LEN (4)
dflet 0:6ad60d78b315 69 #define SOCKET_BIND_PARAMS_LEN (20)
dflet 0:6ad60d78b315 70 #define SOCKET_LISTEN_PARAMS_LEN (8)
dflet 0:6ad60d78b315 71 #define SOCKET_GET_HOST_BY_NAME_PARAMS_LEN (9)
dflet 0:6ad60d78b315 72 #define SOCKET_CONNECT_PARAMS_LEN (20)
dflet 0:6ad60d78b315 73 #define SOCKET_SELECT_PARAMS_LEN (44)
dflet 0:6ad60d78b315 74 #define SOCKET_SET_SOCK_OPT_PARAMS_LEN (20)
dflet 0:6ad60d78b315 75 #define SOCKET_GET_SOCK_OPT_PARAMS_LEN (12)
dflet 0:6ad60d78b315 76 #define SOCKET_RECV_FROM_PARAMS_LEN (12)
dflet 0:6ad60d78b315 77 #define SOCKET_SENDTO_PARAMS_LEN (24)
dflet 0:6ad60d78b315 78 #define SOCKET_MDNS_ADVERTISE_PARAMS_LEN (12)
dflet 0:6ad60d78b315 79
dflet 0:6ad60d78b315 80
dflet 0:6ad60d78b315 81 // The legnth of arguments for the SEND command: sd + buff_offset + len + flags,
dflet 0:6ad60d78b315 82 // while size of each parameter is 32 bit - so the total length is 16 bytes;
dflet 0:6ad60d78b315 83
dflet 0:6ad60d78b315 84 #define HCI_CMND_SEND_ARG_LENGTH (16)
dflet 0:6ad60d78b315 85
dflet 0:6ad60d78b315 86
dflet 0:6ad60d78b315 87 #define SELECT_TIMEOUT_MIN_MICRO_SECONDS 5000
dflet 0:6ad60d78b315 88
dflet 0:6ad60d78b315 89 #define HEADERS_SIZE_DATA (SPI_HEADER_SIZE + 5)
dflet 0:6ad60d78b315 90
dflet 0:6ad60d78b315 91 #define SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE)
dflet 0:6ad60d78b315 92
dflet 0:6ad60d78b315 93 #define MDNS_DEVICE_SERVICE_MAX_LENGTH (32)
dflet 0:6ad60d78b315 94
dflet 0:6ad60d78b315 95
dflet 0:6ad60d78b315 96 //*****************************************************************************
dflet 0:6ad60d78b315 97 //
dflet 0:6ad60d78b315 98 //! HostFlowControlConsumeBuff
dflet 0:6ad60d78b315 99 //!
dflet 0:6ad60d78b315 100 //! @param sd socket descriptor
dflet 0:6ad60d78b315 101 //!
dflet 0:6ad60d78b315 102 //! @return 0 in case there are buffers available,
dflet 0:6ad60d78b315 103 //! -1 in case of bad socket
dflet 0:6ad60d78b315 104 //! -2 if there are no free buffers present (only when
dflet 0:6ad60d78b315 105 //! SEND_NON_BLOCKING is enabled)
dflet 0:6ad60d78b315 106 //!
dflet 0:6ad60d78b315 107 //! @brief if SEND_NON_BLOCKING not define - block until have free buffer
dflet 0:6ad60d78b315 108 //! becomes available, else return immediately with correct status
dflet 0:6ad60d78b315 109 //! regarding the buffers available.
dflet 0:6ad60d78b315 110 //
dflet 0:6ad60d78b315 111 //*****************************************************************************
dflet 0:6ad60d78b315 112 int
dflet 0:6ad60d78b315 113 HostFlowControlConsumeBuff(int sd)
dflet 0:6ad60d78b315 114 {
dflet 0:6ad60d78b315 115 #ifndef SEND_NON_BLOCKING
dflet 0:6ad60d78b315 116 /* wait in busy loop */
dflet 0:6ad60d78b315 117 do
dflet 0:6ad60d78b315 118 {
dflet 0:6ad60d78b315 119 // In case last transmission failed then we will return the last failure
dflet 0:6ad60d78b315 120 // reason here.
dflet 0:6ad60d78b315 121 // Note that the buffer will not be allocated in this case
dflet 0:6ad60d78b315 122 if (tSLInformation.slTransmitDataError != 0)
dflet 0:6ad60d78b315 123 {
dflet 0:6ad60d78b315 124 errno = tSLInformation.slTransmitDataError;
dflet 0:6ad60d78b315 125 tSLInformation.slTransmitDataError = 0;
dflet 0:6ad60d78b315 126 return errno;
dflet 0:6ad60d78b315 127 }
dflet 0:6ad60d78b315 128
dflet 0:6ad60d78b315 129 if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd))
dflet 0:6ad60d78b315 130 return -1;
dflet 0:6ad60d78b315 131 } while(0 == tSLInformation.usNumberOfFreeBuffers);
dflet 0:6ad60d78b315 132
dflet 0:6ad60d78b315 133 tSLInformation.usNumberOfFreeBuffers--;
dflet 0:6ad60d78b315 134
dflet 0:6ad60d78b315 135 return 0;
dflet 0:6ad60d78b315 136 #else
dflet 0:6ad60d78b315 137
dflet 0:6ad60d78b315 138 // In case last transmission failed then we will return the last failure
dflet 0:6ad60d78b315 139 // reason here.
dflet 0:6ad60d78b315 140 // Note that the buffer will not be allocated in this case
dflet 0:6ad60d78b315 141 if (tSLInformation.slTransmitDataError != 0)
dflet 0:6ad60d78b315 142 {
dflet 0:6ad60d78b315 143 errno = tSLInformation.slTransmitDataError;
dflet 0:6ad60d78b315 144 tSLInformation.slTransmitDataError = 0;
dflet 0:6ad60d78b315 145 return errno;
dflet 0:6ad60d78b315 146 }
dflet 0:6ad60d78b315 147 if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd))
dflet 0:6ad60d78b315 148 return -1;
dflet 0:6ad60d78b315 149
dflet 0:6ad60d78b315 150 //If there are no available buffers, return -2. It is recommended to use
dflet 0:6ad60d78b315 151 // select or receive to see if there is any buffer occupied with received data
dflet 0:6ad60d78b315 152 // If so, call receive() to release the buffer.
dflet 0:6ad60d78b315 153 if(0 == tSLInformation.usNumberOfFreeBuffers)
dflet 0:6ad60d78b315 154 {
dflet 0:6ad60d78b315 155 return -2;
dflet 0:6ad60d78b315 156 }
dflet 0:6ad60d78b315 157 else
dflet 0:6ad60d78b315 158 {
dflet 0:6ad60d78b315 159 tSLInformation.usNumberOfFreeBuffers--;
dflet 0:6ad60d78b315 160 return 0;
dflet 0:6ad60d78b315 161 }
dflet 0:6ad60d78b315 162 #endif
dflet 0:6ad60d78b315 163 }
dflet 0:6ad60d78b315 164
dflet 0:6ad60d78b315 165 //*****************************************************************************
dflet 0:6ad60d78b315 166 //
dflet 0:6ad60d78b315 167 //! socket
dflet 0:6ad60d78b315 168 //!
dflet 0:6ad60d78b315 169 //! @param domain selects the protocol family which will be used for
dflet 0:6ad60d78b315 170 //! communication. On this version only AF_INET is supported
dflet 0:6ad60d78b315 171 //! @param type specifies the communication semantics. On this version
dflet 0:6ad60d78b315 172 //! only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW are supported
dflet 0:6ad60d78b315 173 //! @param protocol specifies a particular protocol to be used with the
dflet 0:6ad60d78b315 174 //! socket IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW are
dflet 0:6ad60d78b315 175 //! supported.
dflet 0:6ad60d78b315 176 //!
dflet 0:6ad60d78b315 177 //! @return On success, socket handle that is used for consequent socket
dflet 0:6ad60d78b315 178 //! operations. On error, -1 is returned.
dflet 0:6ad60d78b315 179 //!
dflet 0:6ad60d78b315 180 //! @brief create an endpoint for communication
dflet 0:6ad60d78b315 181 //! The socket function creates a socket that is bound to a specific
dflet 0:6ad60d78b315 182 //! transport service provider. This function is called by the
dflet 0:6ad60d78b315 183 //! application layer to obtain a socket handle.
dflet 0:6ad60d78b315 184 //
dflet 0:6ad60d78b315 185 //*****************************************************************************
dflet 0:6ad60d78b315 186
dflet 0:6ad60d78b315 187 int
dflet 0:6ad60d78b315 188 socket(long domain, long type, long protocol)
dflet 0:6ad60d78b315 189 {
dflet 0:6ad60d78b315 190 long ret;
dflet 0:6ad60d78b315 191 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 192
dflet 0:6ad60d78b315 193 ret = EFAIL;
dflet 0:6ad60d78b315 194 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 195 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 196
dflet 0:6ad60d78b315 197 // Fill in HCI packet structure
dflet 0:6ad60d78b315 198 args = UINT32_TO_STREAM(args, domain);
dflet 0:6ad60d78b315 199 args = UINT32_TO_STREAM(args, type);
dflet 0:6ad60d78b315 200 args = UINT32_TO_STREAM(args, protocol);
dflet 0:6ad60d78b315 201
dflet 0:6ad60d78b315 202 // Initiate a HCI command
dflet 0:6ad60d78b315 203 hci_command_send(HCI_CMND_SOCKET, ptr, SOCKET_OPEN_PARAMS_LEN);
dflet 0:6ad60d78b315 204
dflet 0:6ad60d78b315 205 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 206 SimpleLinkWaitEvent(HCI_CMND_SOCKET, &ret);
dflet 0:6ad60d78b315 207
dflet 0:6ad60d78b315 208 // Process the event
dflet 0:6ad60d78b315 209 errno = ret;
dflet 0:6ad60d78b315 210
dflet 0:6ad60d78b315 211 set_socket_active_status(ret, SOCKET_STATUS_ACTIVE);
dflet 0:6ad60d78b315 212
dflet 0:6ad60d78b315 213 return(ret);
dflet 0:6ad60d78b315 214 }
dflet 0:6ad60d78b315 215
dflet 0:6ad60d78b315 216 //*****************************************************************************
dflet 0:6ad60d78b315 217 //
dflet 0:6ad60d78b315 218 //! closesocket
dflet 0:6ad60d78b315 219 //!
dflet 0:6ad60d78b315 220 //! @param sd socket handle.
dflet 0:6ad60d78b315 221 //!
dflet 0:6ad60d78b315 222 //! @return On success, zero is returned. On error, -1 is returned.
dflet 0:6ad60d78b315 223 //!
dflet 0:6ad60d78b315 224 //! @brief The socket function closes a created socket.
dflet 0:6ad60d78b315 225 //
dflet 0:6ad60d78b315 226 //*****************************************************************************
dflet 0:6ad60d78b315 227
dflet 0:6ad60d78b315 228 long
dflet 0:6ad60d78b315 229 closesocket(long sd)
dflet 0:6ad60d78b315 230 {
dflet 0:6ad60d78b315 231 long ret;
dflet 0:6ad60d78b315 232 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 233
dflet 0:6ad60d78b315 234 ret = EFAIL;
dflet 0:6ad60d78b315 235 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 236 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 237
dflet 0:6ad60d78b315 238 // Fill in HCI packet structure
dflet 0:6ad60d78b315 239 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 240
dflet 0:6ad60d78b315 241 // Initiate a HCI command
dflet 0:6ad60d78b315 242 hci_command_send(HCI_CMND_CLOSE_SOCKET,
dflet 0:6ad60d78b315 243 ptr, SOCKET_CLOSE_PARAMS_LEN);
dflet 0:6ad60d78b315 244
dflet 0:6ad60d78b315 245 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 246 SimpleLinkWaitEvent(HCI_CMND_CLOSE_SOCKET, &ret);
dflet 0:6ad60d78b315 247 errno = ret;
dflet 0:6ad60d78b315 248
dflet 0:6ad60d78b315 249 // since 'close' call may result in either OK (and then it closed) or error
dflet 0:6ad60d78b315 250 // mark this socket as invalid
dflet 0:6ad60d78b315 251 set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
dflet 0:6ad60d78b315 252
dflet 0:6ad60d78b315 253 return(ret);
dflet 0:6ad60d78b315 254 }
dflet 0:6ad60d78b315 255
dflet 0:6ad60d78b315 256 //*****************************************************************************
dflet 0:6ad60d78b315 257 //
dflet 0:6ad60d78b315 258 //! accept
dflet 0:6ad60d78b315 259 //!
dflet 0:6ad60d78b315 260 //! @param[in] sd socket descriptor (handle)
dflet 0:6ad60d78b315 261 //! @param[out] addr the argument addr is a pointer to a sockaddr structure
dflet 0:6ad60d78b315 262 //! This structure is filled in with the address of the
dflet 0:6ad60d78b315 263 //! peer socket, as known to the communications layer.
dflet 0:6ad60d78b315 264 //! determined. The exact format of the address returned
dflet 0:6ad60d78b315 265 //! addr is by the socket's address sockaddr.
dflet 0:6ad60d78b315 266 //! On this version only AF_INET is supported.
dflet 0:6ad60d78b315 267 //! This argument returns in network order.
dflet 0:6ad60d78b315 268 //! @param[out] addrlen the addrlen argument is a value-result argument:
dflet 0:6ad60d78b315 269 //! it should initially contain the size of the structure
dflet 0:6ad60d78b315 270 //! pointed to by addr.
dflet 0:6ad60d78b315 271 //!
dflet 0:6ad60d78b315 272 //! @return For socket in blocking mode:
dflet 0:6ad60d78b315 273 //! On success, socket handle. on failure negative
dflet 0:6ad60d78b315 274 //! For socket in non-blocking mode:
dflet 0:6ad60d78b315 275 //! - On connection establishment, socket handle
dflet 0:6ad60d78b315 276 //! - On connection pending, SOC_IN_PROGRESS (-2)
dflet 0:6ad60d78b315 277 //! - On failure, SOC_ERROR (-1)
dflet 0:6ad60d78b315 278 //!
dflet 0:6ad60d78b315 279 //! @brief accept a connection on a socket:
dflet 0:6ad60d78b315 280 //! This function is used with connection-based socket types
dflet 0:6ad60d78b315 281 //! (SOCK_STREAM). It extracts the first connection request on the
dflet 0:6ad60d78b315 282 //! queue of pending connections, creates a new connected socket, and
dflet 0:6ad60d78b315 283 //! returns a new file descriptor referring to that socket.
dflet 0:6ad60d78b315 284 //! The newly created socket is not in the listening state.
dflet 0:6ad60d78b315 285 //! The original socket sd is unaffected by this call.
dflet 0:6ad60d78b315 286 //! The argument sd is a socket that has been created with socket(),
dflet 0:6ad60d78b315 287 //! bound to a local address with bind(), and is listening for
dflet 0:6ad60d78b315 288 //! connections after a listen(). The argument addr is a pointer
dflet 0:6ad60d78b315 289 //! to a sockaddr structure. This structure is filled in with the
dflet 0:6ad60d78b315 290 //! address of the peer socket, as known to the communications layer.
dflet 0:6ad60d78b315 291 //! The exact format of the address returned addr is determined by the
dflet 0:6ad60d78b315 292 //! socket's address family. The addrlen argument is a value-result
dflet 0:6ad60d78b315 293 //! argument: it should initially contain the size of the structure
dflet 0:6ad60d78b315 294 //! pointed to by addr, on return it will contain the actual
dflet 0:6ad60d78b315 295 //! length (in bytes) of the address returned.
dflet 0:6ad60d78b315 296 //!
dflet 0:6ad60d78b315 297 //! @sa socket ; bind ; listen
dflet 0:6ad60d78b315 298 //
dflet 0:6ad60d78b315 299 //*****************************************************************************
dflet 0:6ad60d78b315 300
dflet 0:6ad60d78b315 301 long
dflet 0:6ad60d78b315 302 accept(long sd, sockaddr *addr, socklen_t *addrlen)
dflet 0:6ad60d78b315 303 {
dflet 0:6ad60d78b315 304 long ret;
dflet 0:6ad60d78b315 305 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 306 tBsdReturnParams tAcceptReturnArguments;
dflet 0:6ad60d78b315 307
dflet 0:6ad60d78b315 308 ret = EFAIL;
dflet 0:6ad60d78b315 309 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 310 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 311
dflet 0:6ad60d78b315 312 // Fill in temporary command buffer
dflet 0:6ad60d78b315 313 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 314
dflet 0:6ad60d78b315 315 // Initiate a HCI command
dflet 0:6ad60d78b315 316 hci_command_send(HCI_CMND_ACCEPT,
dflet 0:6ad60d78b315 317 ptr, SOCKET_ACCEPT_PARAMS_LEN);
dflet 0:6ad60d78b315 318
dflet 0:6ad60d78b315 319 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 320 SimpleLinkWaitEvent(HCI_CMND_ACCEPT, &tAcceptReturnArguments);
dflet 0:6ad60d78b315 321
dflet 0:6ad60d78b315 322
dflet 0:6ad60d78b315 323 // need specify return parameters!!!
dflet 0:6ad60d78b315 324 memcpy(addr, &tAcceptReturnArguments.tSocketAddress, ASIC_ADDR_LEN);
dflet 0:6ad60d78b315 325 *addrlen = ASIC_ADDR_LEN;
dflet 0:6ad60d78b315 326 errno = tAcceptReturnArguments.iStatus;
dflet 0:6ad60d78b315 327 ret = errno;
dflet 0:6ad60d78b315 328
dflet 0:6ad60d78b315 329 // if succeeded, iStatus = new socket descriptor. otherwise - error number
dflet 0:6ad60d78b315 330 if(M_IS_VALID_SD(ret))
dflet 0:6ad60d78b315 331 {
dflet 0:6ad60d78b315 332 set_socket_active_status(ret, SOCKET_STATUS_ACTIVE);
dflet 0:6ad60d78b315 333 }
dflet 0:6ad60d78b315 334 else
dflet 0:6ad60d78b315 335 {
dflet 0:6ad60d78b315 336 set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
dflet 0:6ad60d78b315 337 }
dflet 0:6ad60d78b315 338
dflet 0:6ad60d78b315 339 return(ret);
dflet 0:6ad60d78b315 340 }
dflet 0:6ad60d78b315 341
dflet 0:6ad60d78b315 342 //*****************************************************************************
dflet 0:6ad60d78b315 343 //
dflet 0:6ad60d78b315 344 //! bind
dflet 0:6ad60d78b315 345 //!
dflet 0:6ad60d78b315 346 //! @param[in] sd socket descriptor (handle)
dflet 0:6ad60d78b315 347 //! @param[out] addr specifies the destination address. On this version
dflet 0:6ad60d78b315 348 //! only AF_INET is supported.
dflet 0:6ad60d78b315 349 //! @param[out] addrlen contains the size of the structure pointed to by addr.
dflet 0:6ad60d78b315 350 //!
dflet 0:6ad60d78b315 351 //! @return On success, zero is returned. On error, -1 is returned.
dflet 0:6ad60d78b315 352 //!
dflet 0:6ad60d78b315 353 //! @brief assign a name to a socket
dflet 0:6ad60d78b315 354 //! This function gives the socket the local address addr.
dflet 0:6ad60d78b315 355 //! addr is addrlen bytes long. Traditionally, this is called when a
dflet 0:6ad60d78b315 356 //! socket is created with socket, it exists in a name space (address
dflet 0:6ad60d78b315 357 //! family) but has no name assigned.
dflet 0:6ad60d78b315 358 //! It is necessary to assign a local address before a SOCK_STREAM
dflet 0:6ad60d78b315 359 //! socket may receive connections.
dflet 0:6ad60d78b315 360 //!
dflet 0:6ad60d78b315 361 //! @sa socket ; accept ; listen
dflet 0:6ad60d78b315 362 //
dflet 0:6ad60d78b315 363 //*****************************************************************************
dflet 0:6ad60d78b315 364
dflet 0:6ad60d78b315 365 long
dflet 0:6ad60d78b315 366 bind(long sd, const sockaddr *addr, long addrlen)
dflet 0:6ad60d78b315 367 {
dflet 0:6ad60d78b315 368 long ret;
dflet 0:6ad60d78b315 369 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 370
dflet 0:6ad60d78b315 371 ret = EFAIL;
dflet 0:6ad60d78b315 372 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 373 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 374
dflet 0:6ad60d78b315 375 addrlen = ASIC_ADDR_LEN;
dflet 0:6ad60d78b315 376
dflet 0:6ad60d78b315 377 // Fill in temporary command buffer
dflet 0:6ad60d78b315 378 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 379 args = UINT32_TO_STREAM(args, 0x00000008);
dflet 0:6ad60d78b315 380 args = UINT32_TO_STREAM(args, addrlen);
dflet 0:6ad60d78b315 381 ARRAY_TO_STREAM(args, ((unsigned char *)addr), addrlen);
dflet 0:6ad60d78b315 382
dflet 0:6ad60d78b315 383 // Initiate a HCI command
dflet 0:6ad60d78b315 384 hci_command_send(HCI_CMND_BIND,
dflet 0:6ad60d78b315 385 ptr, SOCKET_BIND_PARAMS_LEN);
dflet 0:6ad60d78b315 386
dflet 0:6ad60d78b315 387 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 388 SimpleLinkWaitEvent(HCI_CMND_BIND, &ret);
dflet 0:6ad60d78b315 389
dflet 0:6ad60d78b315 390 errno = ret;
dflet 0:6ad60d78b315 391
dflet 0:6ad60d78b315 392 return(ret);
dflet 0:6ad60d78b315 393 }
dflet 0:6ad60d78b315 394
dflet 0:6ad60d78b315 395 //*****************************************************************************
dflet 0:6ad60d78b315 396 //
dflet 0:6ad60d78b315 397 //! listen
dflet 0:6ad60d78b315 398 //!
dflet 0:6ad60d78b315 399 //! @param[in] sd socket descriptor (handle)
dflet 0:6ad60d78b315 400 //! @param[in] backlog specifies the listen queue depth. On this version
dflet 0:6ad60d78b315 401 //! backlog is not supported.
dflet 0:6ad60d78b315 402 //! @return On success, zero is returned. On error, -1 is returned.
dflet 0:6ad60d78b315 403 //!
dflet 0:6ad60d78b315 404 //! @brief listen for connections on a socket
dflet 0:6ad60d78b315 405 //! The willingness to accept incoming connections and a queue
dflet 0:6ad60d78b315 406 //! limit for incoming connections are specified with listen(),
dflet 0:6ad60d78b315 407 //! and then the connections are accepted with accept.
dflet 0:6ad60d78b315 408 //! The listen() call applies only to sockets of type SOCK_STREAM
dflet 0:6ad60d78b315 409 //! The backlog parameter defines the maximum length the queue of
dflet 0:6ad60d78b315 410 //! pending connections may grow to.
dflet 0:6ad60d78b315 411 //!
dflet 0:6ad60d78b315 412 //! @sa socket ; accept ; bind
dflet 0:6ad60d78b315 413 //!
dflet 0:6ad60d78b315 414 //! @note On this version, backlog is not supported
dflet 0:6ad60d78b315 415 //
dflet 0:6ad60d78b315 416 //*****************************************************************************
dflet 0:6ad60d78b315 417
dflet 0:6ad60d78b315 418 long
dflet 0:6ad60d78b315 419 listen(long sd, long backlog)
dflet 0:6ad60d78b315 420 {
dflet 0:6ad60d78b315 421 long ret;
dflet 0:6ad60d78b315 422 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 423
dflet 0:6ad60d78b315 424 ret = EFAIL;
dflet 0:6ad60d78b315 425 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 426 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 427
dflet 0:6ad60d78b315 428 // Fill in temporary command buffer
dflet 0:6ad60d78b315 429 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 430 args = UINT32_TO_STREAM(args, backlog);
dflet 0:6ad60d78b315 431
dflet 0:6ad60d78b315 432 // Initiate a HCI command
dflet 0:6ad60d78b315 433 hci_command_send(HCI_CMND_LISTEN,
dflet 0:6ad60d78b315 434 ptr, SOCKET_LISTEN_PARAMS_LEN);
dflet 0:6ad60d78b315 435
dflet 0:6ad60d78b315 436 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 437 SimpleLinkWaitEvent(HCI_CMND_LISTEN, &ret);
dflet 0:6ad60d78b315 438 errno = ret;
dflet 0:6ad60d78b315 439
dflet 0:6ad60d78b315 440 return(ret);
dflet 0:6ad60d78b315 441 }
dflet 0:6ad60d78b315 442
dflet 0:6ad60d78b315 443 //*****************************************************************************
dflet 0:6ad60d78b315 444 //
dflet 0:6ad60d78b315 445 //! gethostbyname
dflet 0:6ad60d78b315 446 //!
dflet 0:6ad60d78b315 447 //! @param[in] hostname host name
dflet 0:6ad60d78b315 448 //! @param[in] usNameLen name length
dflet 0:6ad60d78b315 449 //! @param[out] out_ip_addr This parameter is filled in with host IP address.
dflet 0:6ad60d78b315 450 //! In case that host name is not resolved,
dflet 0:6ad60d78b315 451 //! out_ip_addr is zero.
dflet 0:6ad60d78b315 452 //! @return On success, positive is returned. On error, negative is returned
dflet 0:6ad60d78b315 453 //!
dflet 0:6ad60d78b315 454 //! @brief Get host IP by name. Obtain the IP Address of machine on network,
dflet 0:6ad60d78b315 455 //! by its name.
dflet 0:6ad60d78b315 456 //!
dflet 0:6ad60d78b315 457 //! @note On this version, only blocking mode is supported. Also note that
dflet 0:6ad60d78b315 458 //! the function requires DNS server to be configured prior to its usage.
dflet 0:6ad60d78b315 459 //
dflet 0:6ad60d78b315 460 //*****************************************************************************
dflet 0:6ad60d78b315 461
dflet 0:6ad60d78b315 462 #ifndef CC3000_TINY_DRIVER
dflet 0:6ad60d78b315 463 int
dflet 0:6ad60d78b315 464 gethostbyname(char * hostname, unsigned short usNameLen,
dflet 0:6ad60d78b315 465 unsigned long* out_ip_addr)
dflet 0:6ad60d78b315 466 {
dflet 0:6ad60d78b315 467 tBsdGethostbynameParams ret;
dflet 0:6ad60d78b315 468 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 469
dflet 0:6ad60d78b315 470 errno = EFAIL;
dflet 0:6ad60d78b315 471
dflet 0:6ad60d78b315 472 if (usNameLen > HOSTNAME_MAX_LENGTH)
dflet 0:6ad60d78b315 473 {
dflet 0:6ad60d78b315 474 return errno;
dflet 0:6ad60d78b315 475 }
dflet 0:6ad60d78b315 476
dflet 0:6ad60d78b315 477 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 478 args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
dflet 0:6ad60d78b315 479
dflet 0:6ad60d78b315 480 // Fill in HCI packet structure
dflet 0:6ad60d78b315 481 args = UINT32_TO_STREAM(args, 8);
dflet 0:6ad60d78b315 482 args = UINT32_TO_STREAM(args, usNameLen);
dflet 0:6ad60d78b315 483 ARRAY_TO_STREAM(args, hostname, usNameLen);
dflet 0:6ad60d78b315 484
dflet 0:6ad60d78b315 485 // Initiate a HCI command
dflet 0:6ad60d78b315 486 hci_command_send(HCI_CMND_GETHOSTNAME, ptr, SOCKET_GET_HOST_BY_NAME_PARAMS_LEN
dflet 0:6ad60d78b315 487 + usNameLen - 1);
dflet 0:6ad60d78b315 488
dflet 0:6ad60d78b315 489 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 490 SimpleLinkWaitEvent(HCI_EVNT_BSD_GETHOSTBYNAME, &ret);
dflet 0:6ad60d78b315 491
dflet 0:6ad60d78b315 492 errno = ret.retVal;
dflet 0:6ad60d78b315 493
dflet 0:6ad60d78b315 494 (*((long*)out_ip_addr)) = ret.outputAddress;
dflet 0:6ad60d78b315 495
dflet 0:6ad60d78b315 496 return (errno);
dflet 0:6ad60d78b315 497
dflet 0:6ad60d78b315 498 }
dflet 0:6ad60d78b315 499 #endif
dflet 0:6ad60d78b315 500
dflet 0:6ad60d78b315 501 //*****************************************************************************
dflet 0:6ad60d78b315 502 //
dflet 0:6ad60d78b315 503 //! connect
dflet 0:6ad60d78b315 504 //!
dflet 0:6ad60d78b315 505 //! @param[in] sd socket descriptor (handle)
dflet 0:6ad60d78b315 506 //! @param[in] addr specifies the destination addr. On this version
dflet 0:6ad60d78b315 507 //! only AF_INET is supported.
dflet 0:6ad60d78b315 508 //! @param[out] addrlen contains the size of the structure pointed to by addr
dflet 0:6ad60d78b315 509 //! @return On success, zero is returned. On error, -1 is returned
dflet 0:6ad60d78b315 510 //!
dflet 0:6ad60d78b315 511 //! @brief initiate a connection on a socket
dflet 0:6ad60d78b315 512 //! Function connects the socket referred to by the socket descriptor
dflet 0:6ad60d78b315 513 //! sd, to the address specified by addr. The addrlen argument
dflet 0:6ad60d78b315 514 //! specifies the size of addr. The format of the address in addr is
dflet 0:6ad60d78b315 515 //! determined by the address space of the socket. If it is of type
dflet 0:6ad60d78b315 516 //! SOCK_DGRAM, this call specifies the peer with which the socket is
dflet 0:6ad60d78b315 517 //! to be associated; this address is that to which datagrams are to be
dflet 0:6ad60d78b315 518 //! sent, and the only address from which datagrams are to be received.
dflet 0:6ad60d78b315 519 //! If the socket is of type SOCK_STREAM, this call attempts to make a
dflet 0:6ad60d78b315 520 //! connection to another socket. The other socket is specified by
dflet 0:6ad60d78b315 521 //! address, which is an address in the communications space of the
dflet 0:6ad60d78b315 522 //! socket. Note that the function implements only blocking behavior
dflet 0:6ad60d78b315 523 //! thus the caller will be waiting either for the connection
dflet 0:6ad60d78b315 524 //! establishment or for the connection establishment failure.
dflet 0:6ad60d78b315 525 //!
dflet 0:6ad60d78b315 526 //! @sa socket
dflet 0:6ad60d78b315 527 //
dflet 0:6ad60d78b315 528 //*****************************************************************************
dflet 0:6ad60d78b315 529
dflet 0:6ad60d78b315 530 long
dflet 0:6ad60d78b315 531 connect(long sd, const sockaddr *addr, long addrlen)
dflet 0:6ad60d78b315 532 {
dflet 0:6ad60d78b315 533 long int ret;
dflet 0:6ad60d78b315 534 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 535
dflet 0:6ad60d78b315 536 ret = EFAIL;
dflet 0:6ad60d78b315 537 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 538 args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
dflet 0:6ad60d78b315 539 addrlen = 8;
dflet 0:6ad60d78b315 540
dflet 0:6ad60d78b315 541 // Fill in temporary command buffer
dflet 0:6ad60d78b315 542 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 543 args = UINT32_TO_STREAM(args, 0x00000008);
dflet 0:6ad60d78b315 544 args = UINT32_TO_STREAM(args, addrlen);
dflet 0:6ad60d78b315 545 ARRAY_TO_STREAM(args, ((unsigned char *)addr), addrlen);
dflet 0:6ad60d78b315 546
dflet 0:6ad60d78b315 547 // Initiate a HCI command
dflet 0:6ad60d78b315 548 hci_command_send(HCI_CMND_CONNECT,
dflet 0:6ad60d78b315 549 ptr, SOCKET_CONNECT_PARAMS_LEN);
dflet 0:6ad60d78b315 550
dflet 0:6ad60d78b315 551 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 552 SimpleLinkWaitEvent(HCI_CMND_CONNECT, &ret);
dflet 0:6ad60d78b315 553
dflet 0:6ad60d78b315 554 errno = ret;
dflet 0:6ad60d78b315 555
dflet 0:6ad60d78b315 556 return((long)ret);
dflet 0:6ad60d78b315 557 }
dflet 0:6ad60d78b315 558
dflet 0:6ad60d78b315 559
dflet 0:6ad60d78b315 560 //*****************************************************************************
dflet 0:6ad60d78b315 561 //
dflet 0:6ad60d78b315 562 //! select
dflet 0:6ad60d78b315 563 //!
dflet 0:6ad60d78b315 564 //! @param[in] nfds the highest-numbered file descriptor in any of the
dflet 0:6ad60d78b315 565 //! three sets, plus 1.
dflet 0:6ad60d78b315 566 //! @param[out] writesds socket descriptors list for write monitoring
dflet 0:6ad60d78b315 567 //! @param[out] readsds socket descriptors list for read monitoring
dflet 0:6ad60d78b315 568 //! @param[out] exceptsds socket descriptors list for exception monitoring
dflet 0:6ad60d78b315 569 //! @param[in] timeout is an upper bound on the amount of time elapsed
dflet 0:6ad60d78b315 570 //! before select() returns. Null means infinity
dflet 0:6ad60d78b315 571 //! timeout. The minimum timeout is 5 milliseconds,
dflet 0:6ad60d78b315 572 //! less than 5 milliseconds will be set
dflet 0:6ad60d78b315 573 //! automatically to 5 milliseconds.
dflet 0:6ad60d78b315 574 //! @return On success, select() returns the number of file descriptors
dflet 0:6ad60d78b315 575 //! contained in the three returned descriptor sets (that is, the
dflet 0:6ad60d78b315 576 //! total number of bits that are set in readfds, writefds,
dflet 0:6ad60d78b315 577 //! exceptfds) which may be zero if the timeout expires before
dflet 0:6ad60d78b315 578 //! anything interesting happens.
dflet 0:6ad60d78b315 579 //! On error, -1 is returned.
dflet 0:6ad60d78b315 580 //! *readsds - return the sockets on which Read request will
dflet 0:6ad60d78b315 581 //! return without delay with valid data.
dflet 0:6ad60d78b315 582 //! *writesds - return the sockets on which Write request
dflet 0:6ad60d78b315 583 //! will return without delay.
dflet 0:6ad60d78b315 584 //! *exceptsds - return the sockets which closed recently.
dflet 0:6ad60d78b315 585 //!
dflet 0:6ad60d78b315 586 //! @brief Monitor socket activity
dflet 0:6ad60d78b315 587 //! Select allow a program to monitor multiple file descriptors,
dflet 0:6ad60d78b315 588 //! waiting until one or more of the file descriptors become
dflet 0:6ad60d78b315 589 //! "ready" for some class of I/O operation
dflet 0:6ad60d78b315 590 //!
dflet 0:6ad60d78b315 591 //! @Note If the timeout value set to less than 5ms it will automatically set
dflet 0:6ad60d78b315 592 //! to 5ms to prevent overload of the system
dflet 0:6ad60d78b315 593 //!
dflet 0:6ad60d78b315 594 //! @sa socket
dflet 0:6ad60d78b315 595 //
dflet 0:6ad60d78b315 596 //*****************************************************************************
dflet 0:6ad60d78b315 597
dflet 0:6ad60d78b315 598 int
dflet 0:6ad60d78b315 599 select(long nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds,
dflet 0:6ad60d78b315 600 struct timeval *timeout)
dflet 0:6ad60d78b315 601 {
dflet 0:6ad60d78b315 602 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 603 tBsdSelectRecvParams tParams;
dflet 0:6ad60d78b315 604 unsigned long is_blocking;
dflet 0:6ad60d78b315 605
dflet 0:6ad60d78b315 606 if( timeout == NULL)
dflet 0:6ad60d78b315 607 {
dflet 0:6ad60d78b315 608 is_blocking = 1; /* blocking , infinity timeout */
dflet 0:6ad60d78b315 609 }
dflet 0:6ad60d78b315 610 else
dflet 0:6ad60d78b315 611 {
dflet 0:6ad60d78b315 612 is_blocking = 0; /* no blocking, timeout */
dflet 0:6ad60d78b315 613 }
dflet 0:6ad60d78b315 614
dflet 0:6ad60d78b315 615 // Fill in HCI packet structure
dflet 0:6ad60d78b315 616 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 617 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 618
dflet 0:6ad60d78b315 619 // Fill in temporary command buffer
dflet 0:6ad60d78b315 620 args = UINT32_TO_STREAM(args, nfds);
dflet 0:6ad60d78b315 621 args = UINT32_TO_STREAM(args, 0x00000014);
dflet 0:6ad60d78b315 622 args = UINT32_TO_STREAM(args, 0x00000014);
dflet 0:6ad60d78b315 623 args = UINT32_TO_STREAM(args, 0x00000014);
dflet 0:6ad60d78b315 624 args = UINT32_TO_STREAM(args, 0x00000014);
dflet 0:6ad60d78b315 625 args = UINT32_TO_STREAM(args, is_blocking);
dflet 0:6ad60d78b315 626 args = UINT32_TO_STREAM(args, ((readsds) ? *(unsigned long*)readsds : 0));
dflet 0:6ad60d78b315 627 args = UINT32_TO_STREAM(args, ((writesds) ? *(unsigned long*)writesds : 0));
dflet 0:6ad60d78b315 628 args = UINT32_TO_STREAM(args, ((exceptsds) ? *(unsigned long*)exceptsds : 0));
dflet 0:6ad60d78b315 629
dflet 0:6ad60d78b315 630 if (timeout)
dflet 0:6ad60d78b315 631 {
dflet 0:6ad60d78b315 632 if ( 0 == timeout->tv_sec && timeout->tv_usec <
dflet 0:6ad60d78b315 633 SELECT_TIMEOUT_MIN_MICRO_SECONDS)
dflet 0:6ad60d78b315 634 {
dflet 0:6ad60d78b315 635 timeout->tv_usec = SELECT_TIMEOUT_MIN_MICRO_SECONDS;
dflet 0:6ad60d78b315 636 }
dflet 0:6ad60d78b315 637 args = UINT32_TO_STREAM(args, timeout->tv_sec);
dflet 0:6ad60d78b315 638 args = UINT32_TO_STREAM(args, timeout->tv_usec);
dflet 0:6ad60d78b315 639 }
dflet 0:6ad60d78b315 640
dflet 0:6ad60d78b315 641 // Initiate a HCI command
dflet 0:6ad60d78b315 642 hci_command_send(HCI_CMND_BSD_SELECT, ptr, SOCKET_SELECT_PARAMS_LEN);
dflet 0:6ad60d78b315 643
dflet 0:6ad60d78b315 644 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 645 SimpleLinkWaitEvent(HCI_EVNT_SELECT, &tParams);
dflet 0:6ad60d78b315 646
dflet 0:6ad60d78b315 647 // Update actually read FD
dflet 0:6ad60d78b315 648 if (tParams.iStatus >= 0)
dflet 0:6ad60d78b315 649 {
dflet 0:6ad60d78b315 650 if (readsds)
dflet 0:6ad60d78b315 651 {
dflet 0:6ad60d78b315 652 memcpy(readsds, &tParams.uiRdfd, sizeof(tParams.uiRdfd));
dflet 0:6ad60d78b315 653 }
dflet 0:6ad60d78b315 654
dflet 0:6ad60d78b315 655 if (writesds)
dflet 0:6ad60d78b315 656 {
dflet 0:6ad60d78b315 657 memcpy(writesds, &tParams.uiWrfd, sizeof(tParams.uiWrfd));
dflet 0:6ad60d78b315 658 }
dflet 0:6ad60d78b315 659
dflet 0:6ad60d78b315 660 if (exceptsds)
dflet 0:6ad60d78b315 661 {
dflet 0:6ad60d78b315 662 memcpy(exceptsds, &tParams.uiExfd, sizeof(tParams.uiExfd));
dflet 0:6ad60d78b315 663 }
dflet 0:6ad60d78b315 664
dflet 0:6ad60d78b315 665 return(tParams.iStatus);
dflet 0:6ad60d78b315 666
dflet 0:6ad60d78b315 667 }
dflet 0:6ad60d78b315 668 else
dflet 0:6ad60d78b315 669 {
dflet 0:6ad60d78b315 670 errno = tParams.iStatus;
dflet 0:6ad60d78b315 671 return(-1);
dflet 0:6ad60d78b315 672 }
dflet 0:6ad60d78b315 673 }
dflet 0:6ad60d78b315 674
dflet 0:6ad60d78b315 675 //*****************************************************************************
dflet 0:6ad60d78b315 676 //
dflet 0:6ad60d78b315 677 //! setsockopt
dflet 0:6ad60d78b315 678 //!
dflet 0:6ad60d78b315 679 //! @param[in] sd socket handle
dflet 0:6ad60d78b315 680 //! @param[in] level defines the protocol level for this option
dflet 0:6ad60d78b315 681 //! @param[in] optname defines the option name to Interrogate
dflet 0:6ad60d78b315 682 //! @param[in] optval specifies a value for the option
dflet 0:6ad60d78b315 683 //! @param[in] optlen specifies the length of the option value
dflet 0:6ad60d78b315 684 //! @return On success, zero is returned. On error, -1 is returned
dflet 0:6ad60d78b315 685 //!
dflet 0:6ad60d78b315 686 //! @brief set socket options
dflet 0:6ad60d78b315 687 //! This function manipulate the options associated with a socket.
dflet 0:6ad60d78b315 688 //! Options may exist at multiple protocol levels; they are always
dflet 0:6ad60d78b315 689 //! present at the uppermost socket level.
dflet 0:6ad60d78b315 690 //! When manipulating socket options the level at which the option
dflet 0:6ad60d78b315 691 //! resides and the name of the option must be specified.
dflet 0:6ad60d78b315 692 //! To manipulate options at the socket level, level is specified as
dflet 0:6ad60d78b315 693 //! SOL_SOCKET. To manipulate options at any other level the protocol
dflet 0:6ad60d78b315 694 //! number of the appropriate protocol controlling the option is
dflet 0:6ad60d78b315 695 //! supplied. For example, to indicate that an option is to be
dflet 0:6ad60d78b315 696 //! interpreted by the TCP protocol, level should be set to the
dflet 0:6ad60d78b315 697 //! protocol number of TCP;
dflet 0:6ad60d78b315 698 //! The parameters optval and optlen are used to access optval -
dflet 0:6ad60d78b315 699 //! use for setsockopt(). For getsockopt() they identify a buffer
dflet 0:6ad60d78b315 700 //! in which the value for the requested option(s) are to
dflet 0:6ad60d78b315 701 //! be returned. For getsockopt(), optlen is a value-result
dflet 0:6ad60d78b315 702 //! parameter, initially containing the size of the buffer
dflet 0:6ad60d78b315 703 //! pointed to by option_value, and modified on return to
dflet 0:6ad60d78b315 704 //! indicate the actual size of the value returned. If no option
dflet 0:6ad60d78b315 705 //! value is to be supplied or returned, option_value may be NULL.
dflet 0:6ad60d78b315 706 //!
dflet 0:6ad60d78b315 707 //! @Note On this version the following two socket options are enabled:
dflet 0:6ad60d78b315 708 //! The only protocol level supported in this version
dflet 0:6ad60d78b315 709 //! is SOL_SOCKET (level).
dflet 0:6ad60d78b315 710 //! 1. SOCKOPT_RECV_TIMEOUT (optname)
dflet 0:6ad60d78b315 711 //! SOCKOPT_RECV_TIMEOUT configures recv and recvfrom timeout
dflet 0:6ad60d78b315 712 //! in milliseconds.
dflet 0:6ad60d78b315 713 //! In that case optval should be pointer to unsigned long.
dflet 0:6ad60d78b315 714 //! 2. SOCKOPT_NONBLOCK (optname). sets the socket non-blocking mode on
dflet 0:6ad60d78b315 715 //! or off.
dflet 0:6ad60d78b315 716 //! In that case optval should be SOCK_ON or SOCK_OFF (optval).
dflet 0:6ad60d78b315 717 //!
dflet 0:6ad60d78b315 718 //! @sa getsockopt
dflet 0:6ad60d78b315 719 //
dflet 0:6ad60d78b315 720 //*****************************************************************************
dflet 0:6ad60d78b315 721
dflet 0:6ad60d78b315 722 #ifndef CC3000_TINY_DRIVER
dflet 0:6ad60d78b315 723 int
dflet 0:6ad60d78b315 724 setsockopt(long sd, long level, long optname, const void *optval,
dflet 0:6ad60d78b315 725 socklen_t optlen)
dflet 0:6ad60d78b315 726 {
dflet 0:6ad60d78b315 727 int ret;
dflet 0:6ad60d78b315 728 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 729
dflet 0:6ad60d78b315 730 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 731 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 732
dflet 0:6ad60d78b315 733 // Fill in temporary command buffer
dflet 0:6ad60d78b315 734 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 735 args = UINT32_TO_STREAM(args, level);
dflet 0:6ad60d78b315 736 args = UINT32_TO_STREAM(args, optname);
dflet 0:6ad60d78b315 737 args = UINT32_TO_STREAM(args, 0x00000008);
dflet 0:6ad60d78b315 738 args = UINT32_TO_STREAM(args, optlen);
dflet 0:6ad60d78b315 739 ARRAY_TO_STREAM(args, ((unsigned char *)optval), optlen);
dflet 0:6ad60d78b315 740
dflet 0:6ad60d78b315 741 // Initiate a HCI command
dflet 0:6ad60d78b315 742 hci_command_send(HCI_CMND_SETSOCKOPT,
dflet 0:6ad60d78b315 743 ptr, SOCKET_SET_SOCK_OPT_PARAMS_LEN + optlen);
dflet 0:6ad60d78b315 744
dflet 0:6ad60d78b315 745 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 746 SimpleLinkWaitEvent(HCI_CMND_SETSOCKOPT, &ret);
dflet 0:6ad60d78b315 747
dflet 0:6ad60d78b315 748 if (ret >= 0)
dflet 0:6ad60d78b315 749 {
dflet 0:6ad60d78b315 750 return (0);
dflet 0:6ad60d78b315 751 }
dflet 0:6ad60d78b315 752 else
dflet 0:6ad60d78b315 753 {
dflet 0:6ad60d78b315 754 errno = ret;
dflet 0:6ad60d78b315 755 return (errno);
dflet 0:6ad60d78b315 756 }
dflet 0:6ad60d78b315 757 }
dflet 0:6ad60d78b315 758 #endif
dflet 0:6ad60d78b315 759
dflet 0:6ad60d78b315 760 //*****************************************************************************
dflet 0:6ad60d78b315 761 //
dflet 0:6ad60d78b315 762 //! getsockopt
dflet 0:6ad60d78b315 763 //!
dflet 0:6ad60d78b315 764 //! @param[in] sd socket handle
dflet 0:6ad60d78b315 765 //! @param[in] level defines the protocol level for this option
dflet 0:6ad60d78b315 766 //! @param[in] optname defines the option name to Interrogate
dflet 0:6ad60d78b315 767 //! @param[out] optval specifies a value for the option
dflet 0:6ad60d78b315 768 //! @param[out] optlen specifies the length of the option value
dflet 0:6ad60d78b315 769 //! @return On success, zero is returned. On error, -1 is returned
dflet 0:6ad60d78b315 770 //!
dflet 0:6ad60d78b315 771 //! @brief set socket options
dflet 0:6ad60d78b315 772 //! This function manipulate the options associated with a socket.
dflet 0:6ad60d78b315 773 //! Options may exist at multiple protocol levels; they are always
dflet 0:6ad60d78b315 774 //! present at the uppermost socket level.
dflet 0:6ad60d78b315 775 //! When manipulating socket options the level at which the option
dflet 0:6ad60d78b315 776 //! resides and the name of the option must be specified.
dflet 0:6ad60d78b315 777 //! To manipulate options at the socket level, level is specified as
dflet 0:6ad60d78b315 778 //! SOL_SOCKET. To manipulate options at any other level the protocol
dflet 0:6ad60d78b315 779 //! number of the appropriate protocol controlling the option is
dflet 0:6ad60d78b315 780 //! supplied. For example, to indicate that an option is to be
dflet 0:6ad60d78b315 781 //! interpreted by the TCP protocol, level should be set to the
dflet 0:6ad60d78b315 782 //! protocol number of TCP;
dflet 0:6ad60d78b315 783 //! The parameters optval and optlen are used to access optval -
dflet 0:6ad60d78b315 784 //! use for setsockopt(). For getsockopt() they identify a buffer
dflet 0:6ad60d78b315 785 //! in which the value for the requested option(s) are to
dflet 0:6ad60d78b315 786 //! be returned. For getsockopt(), optlen is a value-result
dflet 0:6ad60d78b315 787 //! parameter, initially containing the size of the buffer
dflet 0:6ad60d78b315 788 //! pointed to by option_value, and modified on return to
dflet 0:6ad60d78b315 789 //! indicate the actual size of the value returned. If no option
dflet 0:6ad60d78b315 790 //! value is to be supplied or returned, option_value may be NULL.
dflet 0:6ad60d78b315 791 //!
dflet 0:6ad60d78b315 792 //! @Note On this version the following two socket options are enabled:
dflet 0:6ad60d78b315 793 //! The only protocol level supported in this version
dflet 0:6ad60d78b315 794 //! is SOL_SOCKET (level).
dflet 0:6ad60d78b315 795 //! 1. SOCKOPT_RECV_TIMEOUT (optname)
dflet 0:6ad60d78b315 796 //! SOCKOPT_RECV_TIMEOUT configures recv and recvfrom timeout
dflet 0:6ad60d78b315 797 //! in milliseconds.
dflet 0:6ad60d78b315 798 //! In that case optval should be pointer to unsigned long.
dflet 0:6ad60d78b315 799 //! 2. SOCKOPT_NONBLOCK (optname). sets the socket non-blocking mode on
dflet 0:6ad60d78b315 800 //! or off.
dflet 0:6ad60d78b315 801 //! In that case optval should be SOCK_ON or SOCK_OFF (optval).
dflet 0:6ad60d78b315 802 //!
dflet 0:6ad60d78b315 803 //! @sa setsockopt
dflet 0:6ad60d78b315 804 //
dflet 0:6ad60d78b315 805 //*****************************************************************************
dflet 0:6ad60d78b315 806
dflet 0:6ad60d78b315 807 int
dflet 0:6ad60d78b315 808 getsockopt (long sd, long level, long optname, void *optval, socklen_t *optlen)
dflet 0:6ad60d78b315 809 {
dflet 0:6ad60d78b315 810 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 811 tBsdGetSockOptReturnParams tRetParams;
dflet 0:6ad60d78b315 812
dflet 0:6ad60d78b315 813 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 814 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 815
dflet 0:6ad60d78b315 816 // Fill in temporary command buffer
dflet 0:6ad60d78b315 817 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 818 args = UINT32_TO_STREAM(args, level);
dflet 0:6ad60d78b315 819 args = UINT32_TO_STREAM(args, optname);
dflet 0:6ad60d78b315 820
dflet 0:6ad60d78b315 821 // Initiate a HCI command
dflet 0:6ad60d78b315 822 hci_command_send(HCI_CMND_GETSOCKOPT,
dflet 0:6ad60d78b315 823 ptr, SOCKET_GET_SOCK_OPT_PARAMS_LEN);
dflet 0:6ad60d78b315 824
dflet 0:6ad60d78b315 825 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 826 SimpleLinkWaitEvent(HCI_CMND_GETSOCKOPT, &tRetParams);
dflet 0:6ad60d78b315 827
dflet 0:6ad60d78b315 828 if (((signed char)tRetParams.iStatus) >= 0)
dflet 0:6ad60d78b315 829 {
dflet 0:6ad60d78b315 830 *optlen = 4;
dflet 0:6ad60d78b315 831 memcpy(optval, tRetParams.ucOptValue, 4);
dflet 0:6ad60d78b315 832 return (0);
dflet 0:6ad60d78b315 833 }
dflet 0:6ad60d78b315 834 else
dflet 0:6ad60d78b315 835 {
dflet 0:6ad60d78b315 836 errno = tRetParams.iStatus;
dflet 0:6ad60d78b315 837 return (errno);
dflet 0:6ad60d78b315 838 }
dflet 0:6ad60d78b315 839 }
dflet 0:6ad60d78b315 840
dflet 0:6ad60d78b315 841 //*****************************************************************************
dflet 0:6ad60d78b315 842 //
dflet 0:6ad60d78b315 843 //! simple_link_recv
dflet 0:6ad60d78b315 844 //!
dflet 0:6ad60d78b315 845 //! @param sd socket handle
dflet 0:6ad60d78b315 846 //! @param buf read buffer
dflet 0:6ad60d78b315 847 //! @param len buffer length
dflet 0:6ad60d78b315 848 //! @param flags indicates blocking or non-blocking operation
dflet 0:6ad60d78b315 849 //! @param from pointer to an address structure indicating source address
dflet 0:6ad60d78b315 850 //! @param fromlen source address structure size
dflet 0:6ad60d78b315 851 //!
dflet 0:6ad60d78b315 852 //! @return Return the number of bytes received, or -1 if an error
dflet 0:6ad60d78b315 853 //! occurred
dflet 0:6ad60d78b315 854 //!
dflet 0:6ad60d78b315 855 //! @brief Read data from socket
dflet 0:6ad60d78b315 856 //! Return the length of the message on successful completion.
dflet 0:6ad60d78b315 857 //! If a message is too long to fit in the supplied buffer,
dflet 0:6ad60d78b315 858 //! excess bytes may be discarded depending on the type of
dflet 0:6ad60d78b315 859 //! socket the message is received from
dflet 0:6ad60d78b315 860 //
dflet 0:6ad60d78b315 861 //*****************************************************************************
dflet 0:6ad60d78b315 862 int
dflet 0:6ad60d78b315 863 simple_link_recv(long sd, void *buf, long len, long flags, sockaddr *from,
dflet 0:6ad60d78b315 864 socklen_t *fromlen, long opcode)
dflet 0:6ad60d78b315 865 {
dflet 0:6ad60d78b315 866 unsigned char *ptr, *args;
dflet 0:6ad60d78b315 867 tBsdReadReturnParams tSocketReadEvent;
dflet 0:6ad60d78b315 868
dflet 0:6ad60d78b315 869 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 870 args = (ptr + HEADERS_SIZE_CMD);
dflet 0:6ad60d78b315 871
dflet 0:6ad60d78b315 872 // Fill in HCI packet structure
dflet 0:6ad60d78b315 873 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 874 args = UINT32_TO_STREAM(args, len);
dflet 0:6ad60d78b315 875 args = UINT32_TO_STREAM(args, flags);
dflet 0:6ad60d78b315 876
dflet 0:6ad60d78b315 877 // Generate the read command, and wait for the
dflet 0:6ad60d78b315 878 hci_command_send(opcode, ptr, SOCKET_RECV_FROM_PARAMS_LEN);
dflet 0:6ad60d78b315 879
dflet 0:6ad60d78b315 880 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 881 SimpleLinkWaitEvent(opcode, &tSocketReadEvent);
dflet 0:6ad60d78b315 882
dflet 0:6ad60d78b315 883 // In case the number of bytes is more then zero - read data
dflet 0:6ad60d78b315 884 if (tSocketReadEvent.iNumberOfBytes > 0)
dflet 0:6ad60d78b315 885 {
dflet 0:6ad60d78b315 886 // Wait for the data in a synchronous way. Here we assume that the bug is
dflet 0:6ad60d78b315 887 // big enough to store also parameters of receive from too....
dflet 0:6ad60d78b315 888 SimpleLinkWaitData((unsigned char *)buf, (unsigned char *)from, (unsigned char *)fromlen);
dflet 0:6ad60d78b315 889 }
dflet 0:6ad60d78b315 890
dflet 0:6ad60d78b315 891 errno = tSocketReadEvent.iNumberOfBytes;
dflet 0:6ad60d78b315 892
dflet 0:6ad60d78b315 893 return(tSocketReadEvent.iNumberOfBytes);
dflet 0:6ad60d78b315 894 }
dflet 0:6ad60d78b315 895
dflet 0:6ad60d78b315 896 //*****************************************************************************
dflet 0:6ad60d78b315 897 //
dflet 0:6ad60d78b315 898 //! recv
dflet 0:6ad60d78b315 899 //!
dflet 0:6ad60d78b315 900 //! @param[in] sd socket handle
dflet 0:6ad60d78b315 901 //! @param[out] buf Points to the buffer where the message should be stored
dflet 0:6ad60d78b315 902 //! @param[in] len Specifies the length in bytes of the buffer pointed to
dflet 0:6ad60d78b315 903 //! by the buffer argument.
dflet 0:6ad60d78b315 904 //! @param[in] flags Specifies the type of message reception.
dflet 0:6ad60d78b315 905 //! On this version, this parameter is not supported.
dflet 0:6ad60d78b315 906 //!
dflet 0:6ad60d78b315 907 //! @return Return the number of bytes received, or -1 if an error
dflet 0:6ad60d78b315 908 //! occurred
dflet 0:6ad60d78b315 909 //!
dflet 0:6ad60d78b315 910 //! @brief function receives a message from a connection-mode socket
dflet 0:6ad60d78b315 911 //!
dflet 0:6ad60d78b315 912 //! @sa recvfrom
dflet 0:6ad60d78b315 913 //!
dflet 0:6ad60d78b315 914 //! @Note On this version, only blocking mode is supported.
dflet 0:6ad60d78b315 915 //
dflet 0:6ad60d78b315 916 //*****************************************************************************
dflet 0:6ad60d78b315 917
dflet 0:6ad60d78b315 918 int
dflet 0:6ad60d78b315 919 recv(long sd, void *buf, long len, long flags)
dflet 0:6ad60d78b315 920 {
dflet 0:6ad60d78b315 921 return(simple_link_recv(sd, buf, len, flags, NULL, NULL, HCI_CMND_RECV));
dflet 0:6ad60d78b315 922 }
dflet 0:6ad60d78b315 923
dflet 0:6ad60d78b315 924 //*****************************************************************************
dflet 0:6ad60d78b315 925 //
dflet 0:6ad60d78b315 926 //! recvfrom
dflet 0:6ad60d78b315 927 //!
dflet 0:6ad60d78b315 928 //! @param[in] sd socket handle
dflet 0:6ad60d78b315 929 //! @param[out] buf Points to the buffer where the message should be stored
dflet 0:6ad60d78b315 930 //! @param[in] len Specifies the length in bytes of the buffer pointed to
dflet 0:6ad60d78b315 931 //! by the buffer argument.
dflet 0:6ad60d78b315 932 //! @param[in] flags Specifies the type of message reception.
dflet 0:6ad60d78b315 933 //! On this version, this parameter is not supported.
dflet 0:6ad60d78b315 934 //! @param[in] from pointer to an address structure indicating the source
dflet 0:6ad60d78b315 935 //! address: sockaddr. On this version only AF_INET is
dflet 0:6ad60d78b315 936 //! supported.
dflet 0:6ad60d78b315 937 //! @param[in] fromlen source address tructure size
dflet 0:6ad60d78b315 938 //!
dflet 0:6ad60d78b315 939 //! @return Return the number of bytes received, or -1 if an error
dflet 0:6ad60d78b315 940 //! occurred
dflet 0:6ad60d78b315 941 //!
dflet 0:6ad60d78b315 942 //! @brief read data from socket
dflet 0:6ad60d78b315 943 //! function receives a message from a connection-mode or
dflet 0:6ad60d78b315 944 //! connectionless-mode socket. Note that raw sockets are not
dflet 0:6ad60d78b315 945 //! supported.
dflet 0:6ad60d78b315 946 //!
dflet 0:6ad60d78b315 947 //! @sa recv
dflet 0:6ad60d78b315 948 //!
dflet 0:6ad60d78b315 949 //! @Note On this version, only blocking mode is supported.
dflet 0:6ad60d78b315 950 //
dflet 0:6ad60d78b315 951 //*****************************************************************************
dflet 0:6ad60d78b315 952 int
dflet 0:6ad60d78b315 953 recvfrom(long sd, void *buf, long len, long flags, sockaddr *from,
dflet 0:6ad60d78b315 954 socklen_t *fromlen)
dflet 0:6ad60d78b315 955 {
dflet 0:6ad60d78b315 956 return(simple_link_recv(sd, buf, len, flags, from, fromlen,
dflet 0:6ad60d78b315 957 HCI_CMND_RECVFROM));
dflet 0:6ad60d78b315 958 }
dflet 0:6ad60d78b315 959
dflet 0:6ad60d78b315 960 //*****************************************************************************
dflet 0:6ad60d78b315 961 //
dflet 0:6ad60d78b315 962 //! simple_link_send
dflet 0:6ad60d78b315 963 //!
dflet 0:6ad60d78b315 964 //! @param sd socket handle
dflet 0:6ad60d78b315 965 //! @param buf write buffer
dflet 0:6ad60d78b315 966 //! @param len buffer length
dflet 0:6ad60d78b315 967 //! @param flags On this version, this parameter is not supported
dflet 0:6ad60d78b315 968 //! @param to pointer to an address structure indicating destination
dflet 0:6ad60d78b315 969 //! address
dflet 0:6ad60d78b315 970 //! @param tolen destination address structure size
dflet 0:6ad60d78b315 971 //!
dflet 0:6ad60d78b315 972 //! @return Return the number of bytes transmitted, or -1 if an error
dflet 0:6ad60d78b315 973 //! occurred, or -2 in case there are no free buffers available
dflet 0:6ad60d78b315 974 //! (only when SEND_NON_BLOCKING is enabled)
dflet 0:6ad60d78b315 975 //!
dflet 0:6ad60d78b315 976 //! @brief This function is used to transmit a message to another
dflet 0:6ad60d78b315 977 //! socket
dflet 0:6ad60d78b315 978 //
dflet 0:6ad60d78b315 979 //*****************************************************************************
dflet 0:6ad60d78b315 980 int
dflet 0:6ad60d78b315 981 simple_link_send(long sd, const void *buf, long len, long flags,
dflet 0:6ad60d78b315 982 const sockaddr *to, long tolen, long opcode)
dflet 0:6ad60d78b315 983 {
dflet 0:6ad60d78b315 984 unsigned char uArgSize, addrlen;
dflet 0:6ad60d78b315 985 unsigned char *ptr, *pDataPtr, *args;
dflet 0:6ad60d78b315 986 unsigned long addr_offset;
dflet 0:6ad60d78b315 987 int res;
dflet 0:6ad60d78b315 988 tBsdReadReturnParams tSocketSendEvent;
dflet 0:6ad60d78b315 989
dflet 0:6ad60d78b315 990 // Check the bsd_arguments
dflet 0:6ad60d78b315 991 if (0 != (res = HostFlowControlConsumeBuff(sd)))
dflet 0:6ad60d78b315 992 {
dflet 0:6ad60d78b315 993 return res;
dflet 0:6ad60d78b315 994 }
dflet 0:6ad60d78b315 995
dflet 0:6ad60d78b315 996 //Update the number of sent packets
dflet 0:6ad60d78b315 997 tSLInformation.NumberOfSentPackets++;
dflet 0:6ad60d78b315 998
dflet 0:6ad60d78b315 999 // Allocate a buffer and construct a packet and send it over spi
dflet 0:6ad60d78b315 1000 ptr = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 1001 args = (ptr + HEADERS_SIZE_DATA);
dflet 0:6ad60d78b315 1002
dflet 0:6ad60d78b315 1003 // Update the offset of data and parameters according to the command
dflet 0:6ad60d78b315 1004 switch(opcode)
dflet 0:6ad60d78b315 1005 {
dflet 0:6ad60d78b315 1006 case HCI_CMND_SENDTO:
dflet 0:6ad60d78b315 1007 {
dflet 0:6ad60d78b315 1008 addr_offset = len + sizeof(len) + sizeof(len);
dflet 0:6ad60d78b315 1009 addrlen = 8;
dflet 0:6ad60d78b315 1010 uArgSize = SOCKET_SENDTO_PARAMS_LEN;
dflet 0:6ad60d78b315 1011 pDataPtr = ptr + HEADERS_SIZE_DATA + SOCKET_SENDTO_PARAMS_LEN;
dflet 0:6ad60d78b315 1012 break;
dflet 0:6ad60d78b315 1013 }
dflet 0:6ad60d78b315 1014
dflet 0:6ad60d78b315 1015 case HCI_CMND_SEND:
dflet 0:6ad60d78b315 1016 {
dflet 0:6ad60d78b315 1017 tolen = 0;
dflet 0:6ad60d78b315 1018 to = NULL;
dflet 0:6ad60d78b315 1019 uArgSize = HCI_CMND_SEND_ARG_LENGTH;
dflet 0:6ad60d78b315 1020 pDataPtr = ptr + HEADERS_SIZE_DATA + HCI_CMND_SEND_ARG_LENGTH;
dflet 0:6ad60d78b315 1021 break;
dflet 0:6ad60d78b315 1022 }
dflet 0:6ad60d78b315 1023
dflet 0:6ad60d78b315 1024 default:
dflet 0:6ad60d78b315 1025 {
dflet 0:6ad60d78b315 1026 break;
dflet 0:6ad60d78b315 1027 }
dflet 0:6ad60d78b315 1028 }
dflet 0:6ad60d78b315 1029
dflet 0:6ad60d78b315 1030 // Fill in temporary command buffer
dflet 0:6ad60d78b315 1031 args = UINT32_TO_STREAM(args, sd);
dflet 0:6ad60d78b315 1032 args = UINT32_TO_STREAM(args, uArgSize - sizeof(sd));
dflet 0:6ad60d78b315 1033 args = UINT32_TO_STREAM(args, len);
dflet 0:6ad60d78b315 1034 args = UINT32_TO_STREAM(args, flags);
dflet 0:6ad60d78b315 1035
dflet 0:6ad60d78b315 1036 if (opcode == HCI_CMND_SENDTO)
dflet 0:6ad60d78b315 1037 {
dflet 0:6ad60d78b315 1038 args = UINT32_TO_STREAM(args, addr_offset);
dflet 0:6ad60d78b315 1039 args = UINT32_TO_STREAM(args, addrlen);
dflet 0:6ad60d78b315 1040 }
dflet 0:6ad60d78b315 1041
dflet 0:6ad60d78b315 1042 // Copy the data received from user into the TX Buffer
dflet 0:6ad60d78b315 1043 ARRAY_TO_STREAM(pDataPtr, ((unsigned char *)buf), len);
dflet 0:6ad60d78b315 1044
dflet 0:6ad60d78b315 1045 // In case we are using SendTo, copy the to parameters
dflet 0:6ad60d78b315 1046 if (opcode == HCI_CMND_SENDTO)
dflet 0:6ad60d78b315 1047 {
dflet 0:6ad60d78b315 1048 ARRAY_TO_STREAM(pDataPtr, ((unsigned char *)to), tolen);
dflet 0:6ad60d78b315 1049 }
dflet 0:6ad60d78b315 1050
dflet 0:6ad60d78b315 1051 // Initiate a HCI command
dflet 0:6ad60d78b315 1052 hci_data_send(opcode, ptr, uArgSize, len,(unsigned char*)to, tolen);
dflet 0:6ad60d78b315 1053
dflet 0:6ad60d78b315 1054 if (opcode == HCI_CMND_SENDTO)
dflet 0:6ad60d78b315 1055 SimpleLinkWaitEvent(HCI_EVNT_SENDTO, &tSocketSendEvent);
dflet 0:6ad60d78b315 1056 else
dflet 0:6ad60d78b315 1057 SimpleLinkWaitEvent(HCI_EVNT_SEND, &tSocketSendEvent);
dflet 0:6ad60d78b315 1058
dflet 0:6ad60d78b315 1059 return (len);
dflet 0:6ad60d78b315 1060 }
dflet 0:6ad60d78b315 1061
dflet 0:6ad60d78b315 1062
dflet 0:6ad60d78b315 1063 //*****************************************************************************
dflet 0:6ad60d78b315 1064 //
dflet 0:6ad60d78b315 1065 //! send
dflet 0:6ad60d78b315 1066 //!
dflet 0:6ad60d78b315 1067 //! @param sd socket handle
dflet 0:6ad60d78b315 1068 //! @param buf Points to a buffer containing the message to be sent
dflet 0:6ad60d78b315 1069 //! @param len message size in bytes
dflet 0:6ad60d78b315 1070 //! @param flags On this version, this parameter is not supported
dflet 0:6ad60d78b315 1071 //!
dflet 0:6ad60d78b315 1072 //! @return Return the number of bytes transmitted, or -1 if an
dflet 0:6ad60d78b315 1073 //! error occurred
dflet 0:6ad60d78b315 1074 //!
dflet 0:6ad60d78b315 1075 //! @brief Write data to TCP socket
dflet 0:6ad60d78b315 1076 //! This function is used to transmit a message to another
dflet 0:6ad60d78b315 1077 //! socket.
dflet 0:6ad60d78b315 1078 //!
dflet 0:6ad60d78b315 1079 //! @Note On this version, only blocking mode is supported.
dflet 0:6ad60d78b315 1080 //!
dflet 0:6ad60d78b315 1081 //! @sa sendto
dflet 0:6ad60d78b315 1082 //
dflet 0:6ad60d78b315 1083 //*****************************************************************************
dflet 0:6ad60d78b315 1084
dflet 0:6ad60d78b315 1085 int
dflet 0:6ad60d78b315 1086 send(long sd, const void *buf, long len, long flags)
dflet 0:6ad60d78b315 1087 {
dflet 0:6ad60d78b315 1088 return(simple_link_send(sd, buf, len, flags, NULL, 0, HCI_CMND_SEND));
dflet 0:6ad60d78b315 1089 }
dflet 0:6ad60d78b315 1090
dflet 0:6ad60d78b315 1091 //*****************************************************************************
dflet 0:6ad60d78b315 1092 //
dflet 0:6ad60d78b315 1093 //! sendto
dflet 0:6ad60d78b315 1094 //!
dflet 0:6ad60d78b315 1095 //! @param sd socket handle
dflet 0:6ad60d78b315 1096 //! @param buf Points to a buffer containing the message to be sent
dflet 0:6ad60d78b315 1097 //! @param len message size in bytes
dflet 0:6ad60d78b315 1098 //! @param flags On this version, this parameter is not supported
dflet 0:6ad60d78b315 1099 //! @param to pointer to an address structure indicating the destination
dflet 0:6ad60d78b315 1100 //! address: sockaddr. On this version only AF_INET is
dflet 0:6ad60d78b315 1101 //! supported.
dflet 0:6ad60d78b315 1102 //! @param tolen destination address structure size
dflet 0:6ad60d78b315 1103 //!
dflet 0:6ad60d78b315 1104 //! @return Return the number of bytes transmitted, or -1 if an
dflet 0:6ad60d78b315 1105 //! error occurred
dflet 0:6ad60d78b315 1106 //!
dflet 0:6ad60d78b315 1107 //! @brief Write data to TCP socket
dflet 0:6ad60d78b315 1108 //! This function is used to transmit a message to another
dflet 0:6ad60d78b315 1109 //! socket.
dflet 0:6ad60d78b315 1110 //!
dflet 0:6ad60d78b315 1111 //! @Note On this version, only blocking mode is supported.
dflet 0:6ad60d78b315 1112 //!
dflet 0:6ad60d78b315 1113 //! @sa send
dflet 0:6ad60d78b315 1114 //
dflet 0:6ad60d78b315 1115 //*****************************************************************************
dflet 0:6ad60d78b315 1116
dflet 0:6ad60d78b315 1117 int
dflet 0:6ad60d78b315 1118 sendto(long sd, const void *buf, long len, long flags, const sockaddr *to,
dflet 0:6ad60d78b315 1119 socklen_t tolen)
dflet 0:6ad60d78b315 1120 {
dflet 0:6ad60d78b315 1121 return(simple_link_send(sd, buf, len, flags, to, tolen, HCI_CMND_SENDTO));
dflet 0:6ad60d78b315 1122 }
dflet 0:6ad60d78b315 1123
dflet 0:6ad60d78b315 1124 //*****************************************************************************
dflet 0:6ad60d78b315 1125 //
dflet 0:6ad60d78b315 1126 //! mdnsAdvertiser
dflet 0:6ad60d78b315 1127 //!
dflet 0:6ad60d78b315 1128 //! @param[in] mdnsEnabled flag to enable/disable the mDNS feature
dflet 0:6ad60d78b315 1129 //! @param[in] deviceServiceName Service name as part of the published
dflet 0:6ad60d78b315 1130 //! canonical domain name
dflet 0:6ad60d78b315 1131 //! @param[in] deviceServiceNameLength Length of the service name
dflet 0:6ad60d78b315 1132 //!
dflet 0:6ad60d78b315 1133 //!
dflet 0:6ad60d78b315 1134 //! @return On success, zero is returned, return SOC_ERROR if socket was not
dflet 0:6ad60d78b315 1135 //! opened successfully, or if an error occurred.
dflet 0:6ad60d78b315 1136 //!
dflet 0:6ad60d78b315 1137 //! @brief Set CC3000 in mDNS advertiser mode in order to advertise itself.
dflet 0:6ad60d78b315 1138 //
dflet 0:6ad60d78b315 1139 //*****************************************************************************
dflet 0:6ad60d78b315 1140
dflet 0:6ad60d78b315 1141 int
dflet 0:6ad60d78b315 1142 mdnsAdvertiser(unsigned short mdnsEnabled, char * deviceServiceName, unsigned short deviceServiceNameLength)
dflet 0:6ad60d78b315 1143 {
dflet 0:6ad60d78b315 1144 int ret;
dflet 0:6ad60d78b315 1145 unsigned char *pTxBuffer, *pArgs;
dflet 0:6ad60d78b315 1146
dflet 0:6ad60d78b315 1147 if (deviceServiceNameLength > MDNS_DEVICE_SERVICE_MAX_LENGTH)
dflet 0:6ad60d78b315 1148 {
dflet 0:6ad60d78b315 1149 return EFAIL;
dflet 0:6ad60d78b315 1150 }
dflet 0:6ad60d78b315 1151
dflet 0:6ad60d78b315 1152 pTxBuffer = tSLInformation.pucTxCommandBuffer;
dflet 0:6ad60d78b315 1153 pArgs = (pTxBuffer + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE);
dflet 0:6ad60d78b315 1154
dflet 0:6ad60d78b315 1155 // Fill in HCI packet structure
dflet 0:6ad60d78b315 1156 pArgs = UINT32_TO_STREAM(pArgs, mdnsEnabled);
dflet 0:6ad60d78b315 1157 pArgs = UINT32_TO_STREAM(pArgs, 8);
dflet 0:6ad60d78b315 1158 pArgs = UINT32_TO_STREAM(pArgs, deviceServiceNameLength);
dflet 0:6ad60d78b315 1159 ARRAY_TO_STREAM(pArgs, deviceServiceName, deviceServiceNameLength);
dflet 0:6ad60d78b315 1160
dflet 0:6ad60d78b315 1161 // Initiate a HCI command
dflet 0:6ad60d78b315 1162 hci_command_send(HCI_CMND_MDNS_ADVERTISE, pTxBuffer, SOCKET_MDNS_ADVERTISE_PARAMS_LEN + deviceServiceNameLength);
dflet 0:6ad60d78b315 1163
dflet 0:6ad60d78b315 1164 // Since we are in blocking state - wait for event complete
dflet 0:6ad60d78b315 1165 SimpleLinkWaitEvent(HCI_EVNT_MDNS_ADVERTISE, &ret);
dflet 0:6ad60d78b315 1166
dflet 0:6ad60d78b315 1167 return ret;
dflet 0:6ad60d78b315 1168
dflet 0:6ad60d78b315 1169 }
dflet 0:6ad60d78b315 1170