version_2.0
Dependents: cc3000_ping_demo_try_2
Fork of cc3000_hostdriver_mbedsocket by
cc3000_socket.cpp@42:bd2c631a031a, 2013-10-12 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Sat Oct 12 21:53:28 2013 +0000
- Revision:
- 42:bd2c631a031a
- Parent:
- 20:30b6ed7bf8fd
- Child:
- 45:50ab13d8f2dc
Added David's IRQ checking before re-enabling the IRQ.; Modified the is_connected function, connect + dhcp are needed ; Moved inet_ntoa_r to the socket class, not sure this is the best place, but other conversion functions live here.
Who changed what in which revision?
User | Revision | Line number | New 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 | 20:30b6ed7bf8fd | 49 | : _simple_link(simplelink), _hci(hci), _event(event) |
Kojto | 20:30b6ed7bf8fd | 50 | { |
Kojto | 20:30b6ed7bf8fd | 51 | |
Kojto | 20:30b6ed7bf8fd | 52 | } |
Kojto | 20:30b6ed7bf8fd | 53 | |
Kojto | 20:30b6ed7bf8fd | 54 | cc3000_socket::~cc3000_socket() |
Kojto | 20:30b6ed7bf8fd | 55 | { |
Kojto | 20:30b6ed7bf8fd | 56 | |
Kojto | 20:30b6ed7bf8fd | 57 | } |
Kojto | 20:30b6ed7bf8fd | 58 | |
Kojto | 20:30b6ed7bf8fd | 59 | int32_t cc3000_socket::HostFlowControlConsumeBuff(int32_t sd) { |
Kojto | 20:30b6ed7bf8fd | 60 | #ifndef SEND_NON_BLOCKING |
Kojto | 20:30b6ed7bf8fd | 61 | /* wait in busy loop */ |
Kojto | 20:30b6ed7bf8fd | 62 | do |
Kojto | 20:30b6ed7bf8fd | 63 | { |
Kojto | 20:30b6ed7bf8fd | 64 | // When the last transmission failed, return the last failure reason. |
Kojto | 20:30b6ed7bf8fd | 65 | // Note that the buffer will not be allocated in this case |
Kojto | 20:30b6ed7bf8fd | 66 | if (_simple_link.get_transmit_error() != 0) |
Kojto | 20:30b6ed7bf8fd | 67 | { |
Kojto | 20:30b6ed7bf8fd | 68 | errno = _simple_link.get_transmit_error(); |
Kojto | 20:30b6ed7bf8fd | 69 | _simple_link.set_transmit_error(0); |
Kojto | 20:30b6ed7bf8fd | 70 | return errno; |
Kojto | 20:30b6ed7bf8fd | 71 | } |
Kojto | 20:30b6ed7bf8fd | 72 | |
Kojto | 20:30b6ed7bf8fd | 73 | if(SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd)) |
Kojto | 20:30b6ed7bf8fd | 74 | return -1; |
Kojto | 20:30b6ed7bf8fd | 75 | } while(0 == _simple_link.get_number_free_buffers()); |
Kojto | 20:30b6ed7bf8fd | 76 | |
Kojto | 20:30b6ed7bf8fd | 77 | uint16_t free_buffer = _simple_link.get_number_free_buffers(); |
Kojto | 20:30b6ed7bf8fd | 78 | free_buffer--; |
Kojto | 20:30b6ed7bf8fd | 79 | _simple_link.set_number_free_buffers(free_buffer); |
Kojto | 20:30b6ed7bf8fd | 80 | |
Kojto | 20:30b6ed7bf8fd | 81 | return 0; |
Kojto | 20:30b6ed7bf8fd | 82 | #else |
Kojto | 20:30b6ed7bf8fd | 83 | |
Kojto | 20:30b6ed7bf8fd | 84 | // When the last transmission failed, return the last failure reason. |
Kojto | 20:30b6ed7bf8fd | 85 | // Note that the buffer will not be allocated in this case |
Kojto | 20:30b6ed7bf8fd | 86 | if (_simple_link.get_transmit_error() != 0) |
Kojto | 20:30b6ed7bf8fd | 87 | { |
Kojto | 20:30b6ed7bf8fd | 88 | errno = _simple_link.get_transmit_error(); |
Kojto | 20:30b6ed7bf8fd | 89 | _simple_link.set_transmit_error(0); |
Kojto | 20:30b6ed7bf8fd | 90 | return errno; |
Kojto | 20:30b6ed7bf8fd | 91 | } |
Kojto | 20:30b6ed7bf8fd | 92 | if(SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd)) |
Kojto | 20:30b6ed7bf8fd | 93 | return -1; |
Kojto | 20:30b6ed7bf8fd | 94 | |
Kojto | 20:30b6ed7bf8fd | 95 | // If there are no available buffers, return -2. It is recommended to use |
Kojto | 20:30b6ed7bf8fd | 96 | // select or receive to see if there is any buffer occupied with received data |
Kojto | 20:30b6ed7bf8fd | 97 | // If so, call receive() to release the buffer. |
Kojto | 20:30b6ed7bf8fd | 98 | if(0 == _simple_link.get_number_free_buffers()) |
Kojto | 20:30b6ed7bf8fd | 99 | { |
Kojto | 20:30b6ed7bf8fd | 100 | return -2; |
Kojto | 20:30b6ed7bf8fd | 101 | } |
Kojto | 20:30b6ed7bf8fd | 102 | else |
Kojto | 20:30b6ed7bf8fd | 103 | { |
Kojto | 20:30b6ed7bf8fd | 104 | uint16_t free_buffer = _simple_link.get_number_free_buffers(); |
Kojto | 20:30b6ed7bf8fd | 105 | free_buffer--; |
Kojto | 20:30b6ed7bf8fd | 106 | _simple_link.set_number_free_buffers(free_buffer); |
Kojto | 20:30b6ed7bf8fd | 107 | return 0; |
Kojto | 20:30b6ed7bf8fd | 108 | } |
Kojto | 20:30b6ed7bf8fd | 109 | #endif |
Kojto | 20:30b6ed7bf8fd | 110 | } |
Kojto | 20:30b6ed7bf8fd | 111 | |
Kojto | 20:30b6ed7bf8fd | 112 | int32_t cc3000_socket::socket(int32_t domain, int32_t type, int32_t protocol) { |
Kojto | 20:30b6ed7bf8fd | 113 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 114 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 115 | |
Kojto | 20:30b6ed7bf8fd | 116 | ret = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 117 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 118 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 119 | |
Kojto | 20:30b6ed7bf8fd | 120 | // Fill in HCI packet structure |
Kojto | 20:30b6ed7bf8fd | 121 | args = UINT32_TO_STREAM(args, domain); |
Kojto | 20:30b6ed7bf8fd | 122 | args = UINT32_TO_STREAM(args, type); |
Kojto | 20:30b6ed7bf8fd | 123 | args = UINT32_TO_STREAM(args, protocol); |
Kojto | 20:30b6ed7bf8fd | 124 | |
Kojto | 20:30b6ed7bf8fd | 125 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 126 | _hci.command_send(HCI_CMND_SOCKET, ptr, SOCKET_OPEN_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 127 | |
Kojto | 20:30b6ed7bf8fd | 128 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 129 | _event.simplelink_wait_event(HCI_CMND_SOCKET, &ret); |
Kojto | 20:30b6ed7bf8fd | 130 | |
Kojto | 20:30b6ed7bf8fd | 131 | // Process the event |
Kojto | 20:30b6ed7bf8fd | 132 | errno = ret; |
Kojto | 20:30b6ed7bf8fd | 133 | |
Kojto | 20:30b6ed7bf8fd | 134 | _event.set_socket_active_status(ret, SOCKET_STATUS_ACTIVE); |
Kojto | 20:30b6ed7bf8fd | 135 | |
Kojto | 20:30b6ed7bf8fd | 136 | return(ret); |
Kojto | 20:30b6ed7bf8fd | 137 | } |
Kojto | 20:30b6ed7bf8fd | 138 | |
Kojto | 20:30b6ed7bf8fd | 139 | int32_t cc3000_socket::closesocket(int32_t sd) { |
Kojto | 20:30b6ed7bf8fd | 140 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 141 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 142 | |
Kojto | 20:30b6ed7bf8fd | 143 | while(_simple_link.get_number_free_buffers() != SOCKET_MAX_FREE_BUFFERS); |
Kojto | 20:30b6ed7bf8fd | 144 | ret = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 145 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 146 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 147 | |
Kojto | 20:30b6ed7bf8fd | 148 | // Fill in HCI packet structure |
Kojto | 20:30b6ed7bf8fd | 149 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 150 | |
Kojto | 20:30b6ed7bf8fd | 151 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 152 | _hci.command_send(HCI_CMND_CLOSE_SOCKET, ptr, SOCKET_CLOSE_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 153 | |
Kojto | 20:30b6ed7bf8fd | 154 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 155 | _event.simplelink_wait_event(HCI_CMND_CLOSE_SOCKET, &ret); |
Kojto | 20:30b6ed7bf8fd | 156 | errno = ret; |
Kojto | 20:30b6ed7bf8fd | 157 | |
Kojto | 20:30b6ed7bf8fd | 158 | // since 'close' call may result in either OK (and then it closed) or error, mark this socket as invalid |
Kojto | 20:30b6ed7bf8fd | 159 | _event.set_socket_active_status(sd, SOCKET_STATUS_INACTIVE); |
Kojto | 20:30b6ed7bf8fd | 160 | |
Kojto | 20:30b6ed7bf8fd | 161 | return(ret); |
Kojto | 20:30b6ed7bf8fd | 162 | } |
Kojto | 20:30b6ed7bf8fd | 163 | |
Kojto | 20:30b6ed7bf8fd | 164 | int32_t cc3000_socket::accept(int32_t sd, sockaddr *addr, socklen_t *addrlen) { |
Kojto | 20:30b6ed7bf8fd | 165 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 166 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 167 | tBsdReturnParams tAcceptReturnArguments; |
Kojto | 20:30b6ed7bf8fd | 168 | |
Kojto | 20:30b6ed7bf8fd | 169 | ret = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 170 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 171 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 172 | |
Kojto | 20:30b6ed7bf8fd | 173 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 174 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 175 | |
Kojto | 20:30b6ed7bf8fd | 176 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 177 | _hci.command_send(HCI_CMND_ACCEPT, ptr, SOCKET_ACCEPT_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 178 | |
Kojto | 20:30b6ed7bf8fd | 179 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 180 | _event.simplelink_wait_event(HCI_CMND_ACCEPT, &tAcceptReturnArguments); |
Kojto | 20:30b6ed7bf8fd | 181 | |
Kojto | 20:30b6ed7bf8fd | 182 | |
Kojto | 20:30b6ed7bf8fd | 183 | // need specify return parameters!!! |
Kojto | 20:30b6ed7bf8fd | 184 | memcpy(addr, &tAcceptReturnArguments.tSocketAddress, ASIC_ADDR_LEN); |
Kojto | 20:30b6ed7bf8fd | 185 | *addrlen = ASIC_ADDR_LEN; |
Kojto | 20:30b6ed7bf8fd | 186 | errno = tAcceptReturnArguments.iStatus; |
Kojto | 20:30b6ed7bf8fd | 187 | ret = errno; |
Kojto | 20:30b6ed7bf8fd | 188 | |
Kojto | 20:30b6ed7bf8fd | 189 | // if succeeded, iStatus = new socket descriptor. otherwise - error number |
Kojto | 20:30b6ed7bf8fd | 190 | if(M_IS_VALID_SD(ret)) |
Kojto | 20:30b6ed7bf8fd | 191 | { |
Kojto | 20:30b6ed7bf8fd | 192 | _event.set_socket_active_status(ret, SOCKET_STATUS_ACTIVE); |
Kojto | 20:30b6ed7bf8fd | 193 | } |
Kojto | 20:30b6ed7bf8fd | 194 | else |
Kojto | 20:30b6ed7bf8fd | 195 | { |
Kojto | 20:30b6ed7bf8fd | 196 | _event.set_socket_active_status(sd, SOCKET_STATUS_INACTIVE); |
Kojto | 20:30b6ed7bf8fd | 197 | } |
Kojto | 20:30b6ed7bf8fd | 198 | |
Kojto | 20:30b6ed7bf8fd | 199 | return(ret); |
Kojto | 20:30b6ed7bf8fd | 200 | } |
Kojto | 20:30b6ed7bf8fd | 201 | |
Kojto | 20:30b6ed7bf8fd | 202 | int32_t cc3000_socket::bind(int32_t sd, const sockaddr *addr, int32_t addrlen) { |
Kojto | 20:30b6ed7bf8fd | 203 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 204 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 205 | |
Kojto | 20:30b6ed7bf8fd | 206 | ret = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 207 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 208 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 209 | |
Kojto | 20:30b6ed7bf8fd | 210 | addrlen = ASIC_ADDR_LEN; |
Kojto | 20:30b6ed7bf8fd | 211 | |
Kojto | 20:30b6ed7bf8fd | 212 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 213 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 214 | args = UINT32_TO_STREAM(args, 0x00000008); |
Kojto | 20:30b6ed7bf8fd | 215 | args = UINT32_TO_STREAM(args, addrlen); |
Kojto | 20:30b6ed7bf8fd | 216 | ARRAY_TO_STREAM(args, ((uint8_t *)addr), addrlen); |
Kojto | 20:30b6ed7bf8fd | 217 | |
Kojto | 20:30b6ed7bf8fd | 218 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 219 | _hci.command_send(HCI_CMND_BIND, ptr, SOCKET_BIND_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 220 | |
Kojto | 20:30b6ed7bf8fd | 221 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 222 | _event.simplelink_wait_event(HCI_CMND_BIND, &ret); |
Kojto | 20:30b6ed7bf8fd | 223 | |
Kojto | 20:30b6ed7bf8fd | 224 | errno = ret; |
Kojto | 20:30b6ed7bf8fd | 225 | |
Kojto | 20:30b6ed7bf8fd | 226 | return(ret); |
Kojto | 20:30b6ed7bf8fd | 227 | } |
Kojto | 20:30b6ed7bf8fd | 228 | |
Kojto | 20:30b6ed7bf8fd | 229 | int32_t cc3000_socket::listen(int32_t sd, int32_t backlog) { |
Kojto | 20:30b6ed7bf8fd | 230 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 231 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 232 | |
Kojto | 20:30b6ed7bf8fd | 233 | ret = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 234 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 235 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 236 | |
Kojto | 20:30b6ed7bf8fd | 237 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 238 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 239 | args = UINT32_TO_STREAM(args, backlog); |
Kojto | 20:30b6ed7bf8fd | 240 | |
Kojto | 20:30b6ed7bf8fd | 241 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 242 | _hci.command_send(HCI_CMND_LISTEN, ptr, SOCKET_LISTEN_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 243 | |
Kojto | 20:30b6ed7bf8fd | 244 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 245 | _event.simplelink_wait_event(HCI_CMND_LISTEN, &ret); |
Kojto | 20:30b6ed7bf8fd | 246 | errno = ret; |
Kojto | 20:30b6ed7bf8fd | 247 | |
Kojto | 20:30b6ed7bf8fd | 248 | return(ret); |
Kojto | 20:30b6ed7bf8fd | 249 | } |
Kojto | 20:30b6ed7bf8fd | 250 | |
Kojto | 20:30b6ed7bf8fd | 251 | int32_t cc3000_socket::connect(int32_t sd, const sockaddr *addr, int32_t addrlen) { |
Kojto | 20:30b6ed7bf8fd | 252 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 253 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 254 | |
Kojto | 20:30b6ed7bf8fd | 255 | ret = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 256 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 257 | args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 258 | addrlen = 8; |
Kojto | 20:30b6ed7bf8fd | 259 | |
Kojto | 20:30b6ed7bf8fd | 260 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 261 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 262 | args = UINT32_TO_STREAM(args, 0x00000008); |
Kojto | 20:30b6ed7bf8fd | 263 | args = UINT32_TO_STREAM(args, addrlen); |
Kojto | 20:30b6ed7bf8fd | 264 | ARRAY_TO_STREAM(args, ((uint8_t *)addr), addrlen); |
Kojto | 20:30b6ed7bf8fd | 265 | |
Kojto | 20:30b6ed7bf8fd | 266 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 267 | _hci.command_send(HCI_CMND_CONNECT, ptr, SOCKET_CONNECT_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 268 | |
Kojto | 20:30b6ed7bf8fd | 269 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 270 | _event.simplelink_wait_event(HCI_CMND_CONNECT, &ret); |
Kojto | 20:30b6ed7bf8fd | 271 | |
Kojto | 20:30b6ed7bf8fd | 272 | errno = ret; |
Kojto | 20:30b6ed7bf8fd | 273 | |
Kojto | 20:30b6ed7bf8fd | 274 | return((int32_t)ret); |
Kojto | 20:30b6ed7bf8fd | 275 | } |
Kojto | 20:30b6ed7bf8fd | 276 | |
Kojto | 20:30b6ed7bf8fd | 277 | int32_t cc3000_socket::select(int32_t nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, struct timeval *timeout) { |
Kojto | 20:30b6ed7bf8fd | 278 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 279 | tBsdSelectRecvParams tParams; |
Kojto | 20:30b6ed7bf8fd | 280 | uint32_t is_blocking; |
Kojto | 20:30b6ed7bf8fd | 281 | |
Kojto | 20:30b6ed7bf8fd | 282 | if( timeout == NULL) |
Kojto | 20:30b6ed7bf8fd | 283 | { |
Kojto | 20:30b6ed7bf8fd | 284 | is_blocking = 1; /* blocking , infinity timeout */ |
Kojto | 20:30b6ed7bf8fd | 285 | } |
Kojto | 20:30b6ed7bf8fd | 286 | else |
Kojto | 20:30b6ed7bf8fd | 287 | { |
Kojto | 20:30b6ed7bf8fd | 288 | is_blocking = 0; /* no blocking, timeout */ |
Kojto | 20:30b6ed7bf8fd | 289 | } |
Kojto | 20:30b6ed7bf8fd | 290 | |
Kojto | 20:30b6ed7bf8fd | 291 | // Fill in HCI packet structure |
Kojto | 20:30b6ed7bf8fd | 292 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 293 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 294 | |
Kojto | 20:30b6ed7bf8fd | 295 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 296 | args = UINT32_TO_STREAM(args, nfds); |
Kojto | 20:30b6ed7bf8fd | 297 | args = UINT32_TO_STREAM(args, 0x00000014); |
Kojto | 20:30b6ed7bf8fd | 298 | args = UINT32_TO_STREAM(args, 0x00000014); |
Kojto | 20:30b6ed7bf8fd | 299 | args = UINT32_TO_STREAM(args, 0x00000014); |
Kojto | 20:30b6ed7bf8fd | 300 | args = UINT32_TO_STREAM(args, 0x00000014); |
Kojto | 20:30b6ed7bf8fd | 301 | args = UINT32_TO_STREAM(args, is_blocking); |
Kojto | 20:30b6ed7bf8fd | 302 | args = UINT32_TO_STREAM(args, ((readsds) ? *(uint32_t*)readsds : 0)); |
Kojto | 20:30b6ed7bf8fd | 303 | args = UINT32_TO_STREAM(args, ((writesds) ? *(uint32_t*)writesds : 0)); |
Kojto | 20:30b6ed7bf8fd | 304 | args = UINT32_TO_STREAM(args, ((exceptsds) ? *(uint32_t*)exceptsds : 0)); |
Kojto | 20:30b6ed7bf8fd | 305 | |
Kojto | 20:30b6ed7bf8fd | 306 | if (timeout) |
Kojto | 20:30b6ed7bf8fd | 307 | { |
Kojto | 20:30b6ed7bf8fd | 308 | if ( 0 == timeout->tv_sec && timeout->tv_usec < SELECT_TIMEOUT_MIN_MICRO_SECONDS) |
Kojto | 20:30b6ed7bf8fd | 309 | { |
Kojto | 20:30b6ed7bf8fd | 310 | timeout->tv_usec = SELECT_TIMEOUT_MIN_MICRO_SECONDS; |
Kojto | 20:30b6ed7bf8fd | 311 | } |
Kojto | 20:30b6ed7bf8fd | 312 | args = UINT32_TO_STREAM(args, timeout->tv_sec); |
Kojto | 20:30b6ed7bf8fd | 313 | args = UINT32_TO_STREAM(args, timeout->tv_usec); |
Kojto | 20:30b6ed7bf8fd | 314 | } |
Kojto | 20:30b6ed7bf8fd | 315 | |
Kojto | 20:30b6ed7bf8fd | 316 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 317 | _hci.command_send(HCI_CMND_BSD_SELECT, ptr, SOCKET_SELECT_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 318 | |
Kojto | 20:30b6ed7bf8fd | 319 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 320 | _event.simplelink_wait_event(HCI_EVNT_SELECT, &tParams); |
Kojto | 20:30b6ed7bf8fd | 321 | |
Kojto | 20:30b6ed7bf8fd | 322 | // Update actually read FD |
Kojto | 20:30b6ed7bf8fd | 323 | if (tParams.iStatus >= 0) |
Kojto | 20:30b6ed7bf8fd | 324 | { |
Kojto | 20:30b6ed7bf8fd | 325 | if (readsds) |
Kojto | 20:30b6ed7bf8fd | 326 | { |
Kojto | 20:30b6ed7bf8fd | 327 | memcpy(readsds, &tParams.uiRdfd, sizeof(tParams.uiRdfd)); |
Kojto | 20:30b6ed7bf8fd | 328 | } |
Kojto | 20:30b6ed7bf8fd | 329 | |
Kojto | 20:30b6ed7bf8fd | 330 | if (writesds) |
Kojto | 20:30b6ed7bf8fd | 331 | { |
Kojto | 20:30b6ed7bf8fd | 332 | memcpy(writesds, &tParams.uiWrfd, sizeof(tParams.uiWrfd)); |
Kojto | 20:30b6ed7bf8fd | 333 | } |
Kojto | 20:30b6ed7bf8fd | 334 | |
Kojto | 20:30b6ed7bf8fd | 335 | if (exceptsds) |
Kojto | 20:30b6ed7bf8fd | 336 | { |
Kojto | 20:30b6ed7bf8fd | 337 | memcpy(exceptsds, &tParams.uiExfd, sizeof(tParams.uiExfd)); |
Kojto | 20:30b6ed7bf8fd | 338 | } |
Kojto | 20:30b6ed7bf8fd | 339 | |
Kojto | 20:30b6ed7bf8fd | 340 | return(tParams.iStatus); |
Kojto | 20:30b6ed7bf8fd | 341 | |
Kojto | 20:30b6ed7bf8fd | 342 | } |
Kojto | 20:30b6ed7bf8fd | 343 | else |
Kojto | 20:30b6ed7bf8fd | 344 | { |
Kojto | 20:30b6ed7bf8fd | 345 | errno = tParams.iStatus; |
Kojto | 20:30b6ed7bf8fd | 346 | return(-1); |
Kojto | 20:30b6ed7bf8fd | 347 | } |
Kojto | 20:30b6ed7bf8fd | 348 | } |
Kojto | 20:30b6ed7bf8fd | 349 | |
Kojto | 20:30b6ed7bf8fd | 350 | int32_t cc3000_socket::getsockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen) { |
Kojto | 20:30b6ed7bf8fd | 351 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 352 | tBsdGetSockOptReturnParams tRetParams; |
Kojto | 20:30b6ed7bf8fd | 353 | |
Kojto | 20:30b6ed7bf8fd | 354 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 355 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 356 | |
Kojto | 20:30b6ed7bf8fd | 357 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 358 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 359 | args = UINT32_TO_STREAM(args, level); |
Kojto | 20:30b6ed7bf8fd | 360 | args = UINT32_TO_STREAM(args, optname); |
Kojto | 20:30b6ed7bf8fd | 361 | |
Kojto | 20:30b6ed7bf8fd | 362 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 363 | _hci.command_send(HCI_CMND_GETSOCKOPT, ptr, SOCKET_GET_SOCK_OPT_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 364 | |
Kojto | 20:30b6ed7bf8fd | 365 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 366 | _event.simplelink_wait_event(HCI_CMND_GETSOCKOPT, &tRetParams); |
Kojto | 20:30b6ed7bf8fd | 367 | |
Kojto | 20:30b6ed7bf8fd | 368 | if (((int8_t)tRetParams.iStatus) >= 0) |
Kojto | 20:30b6ed7bf8fd | 369 | { |
Kojto | 20:30b6ed7bf8fd | 370 | *optlen = 4; |
Kojto | 20:30b6ed7bf8fd | 371 | memcpy(optval, tRetParams.ucOptValue, 4); |
Kojto | 20:30b6ed7bf8fd | 372 | return (0); |
Kojto | 20:30b6ed7bf8fd | 373 | } |
Kojto | 20:30b6ed7bf8fd | 374 | else |
Kojto | 20:30b6ed7bf8fd | 375 | { |
Kojto | 20:30b6ed7bf8fd | 376 | errno = tRetParams.iStatus; |
Kojto | 20:30b6ed7bf8fd | 377 | return errno; |
Kojto | 20:30b6ed7bf8fd | 378 | } |
Kojto | 20:30b6ed7bf8fd | 379 | } |
Kojto | 20:30b6ed7bf8fd | 380 | |
Kojto | 20:30b6ed7bf8fd | 381 | 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 | 382 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 383 | tBsdReadReturnParams tSocketReadEvent; |
Kojto | 20:30b6ed7bf8fd | 384 | |
Kojto | 20:30b6ed7bf8fd | 385 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 386 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 387 | |
Kojto | 20:30b6ed7bf8fd | 388 | // Fill in HCI packet structure |
Kojto | 20:30b6ed7bf8fd | 389 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 390 | args = UINT32_TO_STREAM(args, len); |
Kojto | 20:30b6ed7bf8fd | 391 | args = UINT32_TO_STREAM(args, flags); |
Kojto | 20:30b6ed7bf8fd | 392 | |
Kojto | 20:30b6ed7bf8fd | 393 | // Generate the read command, and wait for the |
Kojto | 20:30b6ed7bf8fd | 394 | _hci.command_send(opcode, ptr, SOCKET_RECV_FROM_PARAMS_LEN); |
Kojto | 20:30b6ed7bf8fd | 395 | |
Kojto | 20:30b6ed7bf8fd | 396 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 397 | _event.simplelink_wait_event(opcode, &tSocketReadEvent); |
Kojto | 20:30b6ed7bf8fd | 398 | |
Kojto | 20:30b6ed7bf8fd | 399 | // In case the number of bytes is more then zero - read data |
Kojto | 20:30b6ed7bf8fd | 400 | if (tSocketReadEvent.iNumberOfBytes > 0) |
Kojto | 20:30b6ed7bf8fd | 401 | { |
Kojto | 20:30b6ed7bf8fd | 402 | // Wait for the data in a synchronous way. Here we assume that the bug is |
Kojto | 20:30b6ed7bf8fd | 403 | // big enough to store also parameters of receive from too.... |
Kojto | 20:30b6ed7bf8fd | 404 | _event.simplelink_wait_data((uint8_t *)buf, (uint8_t *)from, (uint8_t *)fromlen); |
Kojto | 20:30b6ed7bf8fd | 405 | } |
Kojto | 20:30b6ed7bf8fd | 406 | |
Kojto | 20:30b6ed7bf8fd | 407 | errno = tSocketReadEvent.iNumberOfBytes; |
Kojto | 20:30b6ed7bf8fd | 408 | |
Kojto | 20:30b6ed7bf8fd | 409 | return(tSocketReadEvent.iNumberOfBytes); |
Kojto | 20:30b6ed7bf8fd | 410 | } |
Kojto | 20:30b6ed7bf8fd | 411 | |
Kojto | 20:30b6ed7bf8fd | 412 | int32_t cc3000_socket::recv(int32_t sd, void *buf, int32_t len, int32_t flags) { |
Kojto | 20:30b6ed7bf8fd | 413 | return(simple_link_recv(sd, buf, len, flags, NULL, NULL, HCI_CMND_RECV)); |
Kojto | 20:30b6ed7bf8fd | 414 | } |
Kojto | 20:30b6ed7bf8fd | 415 | |
Kojto | 20:30b6ed7bf8fd | 416 | int32_t cc3000_socket::recvfrom(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen) { |
Kojto | 20:30b6ed7bf8fd | 417 | return(simple_link_recv(sd, buf, len, flags, from, fromlen, HCI_CMND_RECVFROM)); |
Kojto | 20:30b6ed7bf8fd | 418 | } |
Kojto | 20:30b6ed7bf8fd | 419 | |
Kojto | 20:30b6ed7bf8fd | 420 | 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 | 421 | uint8_t uArgSize = 0x00, addrlen = 0x00; |
Kojto | 20:30b6ed7bf8fd | 422 | uint8_t *ptr, *pDataPtr = NULL, *args; |
Kojto | 20:30b6ed7bf8fd | 423 | uint32_t addr_offset = 0x00; |
Kojto | 20:30b6ed7bf8fd | 424 | int32_t res; |
Kojto | 20:30b6ed7bf8fd | 425 | tBsdReadReturnParams tSocketSendEvent; |
Kojto | 20:30b6ed7bf8fd | 426 | |
Kojto | 20:30b6ed7bf8fd | 427 | // Check the bsd_arguments |
Kojto | 20:30b6ed7bf8fd | 428 | if (0 != (res = HostFlowControlConsumeBuff(sd))) |
Kojto | 20:30b6ed7bf8fd | 429 | { |
Kojto | 20:30b6ed7bf8fd | 430 | return res; |
Kojto | 20:30b6ed7bf8fd | 431 | } |
Kojto | 20:30b6ed7bf8fd | 432 | |
Kojto | 20:30b6ed7bf8fd | 433 | //Update the number of sent packets |
Kojto | 20:30b6ed7bf8fd | 434 | uint16_t sent_packets = _simple_link.get_sent_packets(); |
Kojto | 20:30b6ed7bf8fd | 435 | sent_packets++; |
Kojto | 20:30b6ed7bf8fd | 436 | _simple_link.set_sent_packets(sent_packets); |
Kojto | 20:30b6ed7bf8fd | 437 | |
Kojto | 20:30b6ed7bf8fd | 438 | // Allocate a buffer and construct a packet and send it over spi |
Kojto | 20:30b6ed7bf8fd | 439 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 440 | args = (ptr + HEADERS_SIZE_DATA); |
Kojto | 20:30b6ed7bf8fd | 441 | |
Kojto | 20:30b6ed7bf8fd | 442 | // Update the offset of data and parameters according to the command |
Kojto | 20:30b6ed7bf8fd | 443 | switch(opcode) |
Kojto | 20:30b6ed7bf8fd | 444 | { |
Kojto | 20:30b6ed7bf8fd | 445 | case HCI_CMND_SENDTO: |
Kojto | 20:30b6ed7bf8fd | 446 | { |
Kojto | 20:30b6ed7bf8fd | 447 | addr_offset = len + sizeof(len) + sizeof(len); |
Kojto | 20:30b6ed7bf8fd | 448 | addrlen = 8; |
Kojto | 20:30b6ed7bf8fd | 449 | uArgSize = SOCKET_SENDTO_PARAMS_LEN; |
Kojto | 20:30b6ed7bf8fd | 450 | pDataPtr = ptr + HEADERS_SIZE_DATA + SOCKET_SENDTO_PARAMS_LEN; |
Kojto | 20:30b6ed7bf8fd | 451 | break; |
Kojto | 20:30b6ed7bf8fd | 452 | } |
Kojto | 20:30b6ed7bf8fd | 453 | |
Kojto | 20:30b6ed7bf8fd | 454 | case HCI_CMND_SEND: |
Kojto | 20:30b6ed7bf8fd | 455 | { |
Kojto | 20:30b6ed7bf8fd | 456 | tolen = 0; |
Kojto | 20:30b6ed7bf8fd | 457 | to = NULL; |
Kojto | 20:30b6ed7bf8fd | 458 | uArgSize = HCI_CMND_SEND_ARG_LENGTH; |
Kojto | 20:30b6ed7bf8fd | 459 | pDataPtr = ptr + HEADERS_SIZE_DATA + HCI_CMND_SEND_ARG_LENGTH; |
Kojto | 20:30b6ed7bf8fd | 460 | break; |
Kojto | 20:30b6ed7bf8fd | 461 | } |
Kojto | 20:30b6ed7bf8fd | 462 | |
Kojto | 20:30b6ed7bf8fd | 463 | default: |
Kojto | 20:30b6ed7bf8fd | 464 | { |
Kojto | 20:30b6ed7bf8fd | 465 | break; |
Kojto | 20:30b6ed7bf8fd | 466 | } |
Kojto | 20:30b6ed7bf8fd | 467 | } |
Kojto | 20:30b6ed7bf8fd | 468 | |
Kojto | 20:30b6ed7bf8fd | 469 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 470 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 471 | args = UINT32_TO_STREAM(args, uArgSize - sizeof(sd)); |
Kojto | 20:30b6ed7bf8fd | 472 | args = UINT32_TO_STREAM(args, len); |
Kojto | 20:30b6ed7bf8fd | 473 | args = UINT32_TO_STREAM(args, flags); |
Kojto | 20:30b6ed7bf8fd | 474 | |
Kojto | 20:30b6ed7bf8fd | 475 | if (opcode == HCI_CMND_SENDTO) |
Kojto | 20:30b6ed7bf8fd | 476 | { |
Kojto | 20:30b6ed7bf8fd | 477 | args = UINT32_TO_STREAM(args, addr_offset); |
Kojto | 20:30b6ed7bf8fd | 478 | args = UINT32_TO_STREAM(args, addrlen); |
Kojto | 20:30b6ed7bf8fd | 479 | } |
Kojto | 20:30b6ed7bf8fd | 480 | |
Kojto | 20:30b6ed7bf8fd | 481 | // Copy the data received from user into the TX Buffer |
Kojto | 20:30b6ed7bf8fd | 482 | ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)buf), len); |
Kojto | 20:30b6ed7bf8fd | 483 | |
Kojto | 20:30b6ed7bf8fd | 484 | // In case we are using SendTo, copy the to parameters |
Kojto | 20:30b6ed7bf8fd | 485 | if (opcode == HCI_CMND_SENDTO) |
Kojto | 20:30b6ed7bf8fd | 486 | { |
Kojto | 20:30b6ed7bf8fd | 487 | ARRAY_TO_STREAM(pDataPtr, ((uint8_t *)to), tolen); |
Kojto | 20:30b6ed7bf8fd | 488 | } |
Kojto | 20:30b6ed7bf8fd | 489 | |
Kojto | 20:30b6ed7bf8fd | 490 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 491 | _hci.data_send(opcode, ptr, uArgSize, len,(uint8_t*)to, tolen); |
Kojto | 20:30b6ed7bf8fd | 492 | if (opcode == HCI_CMND_SENDTO) |
Kojto | 20:30b6ed7bf8fd | 493 | _event.simplelink_wait_event(HCI_EVNT_SENDTO, &tSocketSendEvent); |
Kojto | 20:30b6ed7bf8fd | 494 | else |
Kojto | 20:30b6ed7bf8fd | 495 | _event.simplelink_wait_event(HCI_EVNT_SEND, &tSocketSendEvent); |
Kojto | 20:30b6ed7bf8fd | 496 | |
Kojto | 20:30b6ed7bf8fd | 497 | return (len); |
Kojto | 20:30b6ed7bf8fd | 498 | } |
Kojto | 20:30b6ed7bf8fd | 499 | |
Kojto | 20:30b6ed7bf8fd | 500 | int32_t cc3000_socket::send(int32_t sd, const void *buf, int32_t len, int32_t flags) { |
Kojto | 20:30b6ed7bf8fd | 501 | return(simple_link_send(sd, buf, len, flags, NULL, 0, HCI_CMND_SEND)); |
Kojto | 20:30b6ed7bf8fd | 502 | } |
Kojto | 20:30b6ed7bf8fd | 503 | |
Kojto | 20:30b6ed7bf8fd | 504 | 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 | 505 | return(simple_link_send(sd, buf, len, flags, to, tolen, HCI_CMND_SENDTO)); |
Kojto | 20:30b6ed7bf8fd | 506 | } |
Kojto | 20:30b6ed7bf8fd | 507 | |
Kojto | 20:30b6ed7bf8fd | 508 | int32_t cc3000_socket::mdns_advertiser(uint16_t mdns_enabled, uint8_t *device_service_name, uint16_t device_service_name_length) { |
Kojto | 20:30b6ed7bf8fd | 509 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 510 | uint8_t *pTxBuffer, *pArgs; |
Kojto | 20:30b6ed7bf8fd | 511 | |
Kojto | 20:30b6ed7bf8fd | 512 | if (device_service_name_length > MDNS_DEVICE_SERVICE_MAX_LENGTH) |
Kojto | 20:30b6ed7bf8fd | 513 | { |
Kojto | 20:30b6ed7bf8fd | 514 | return EFAIL; |
Kojto | 20:30b6ed7bf8fd | 515 | } |
Kojto | 20:30b6ed7bf8fd | 516 | |
Kojto | 20:30b6ed7bf8fd | 517 | pTxBuffer = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 518 | pArgs = (pTxBuffer + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 519 | |
Kojto | 20:30b6ed7bf8fd | 520 | // Fill in HCI packet structure |
Kojto | 20:30b6ed7bf8fd | 521 | pArgs = UINT32_TO_STREAM(pArgs, mdns_enabled); |
Kojto | 20:30b6ed7bf8fd | 522 | pArgs = UINT32_TO_STREAM(pArgs, 8); |
Kojto | 20:30b6ed7bf8fd | 523 | pArgs = UINT32_TO_STREAM(pArgs, device_service_name_length); |
Kojto | 20:30b6ed7bf8fd | 524 | ARRAY_TO_STREAM(pArgs, device_service_name, device_service_name_length); |
Kojto | 20:30b6ed7bf8fd | 525 | |
Kojto | 20:30b6ed7bf8fd | 526 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 527 | _hci.command_send(HCI_CMND_MDNS_ADVERTISE, pTxBuffer, SOCKET_MDNS_ADVERTISE_PARAMS_LEN + device_service_name_length); |
Kojto | 20:30b6ed7bf8fd | 528 | |
Kojto | 20:30b6ed7bf8fd | 529 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 530 | _event.simplelink_wait_event(HCI_EVNT_MDNS_ADVERTISE, &ret); |
Kojto | 20:30b6ed7bf8fd | 531 | |
Kojto | 20:30b6ed7bf8fd | 532 | return ret; |
Kojto | 20:30b6ed7bf8fd | 533 | } |
Kojto | 20:30b6ed7bf8fd | 534 | |
Kojto | 20:30b6ed7bf8fd | 535 | |
Kojto | 20:30b6ed7bf8fd | 536 | #ifndef CC3000_TINY_DRIVER |
Kojto | 20:30b6ed7bf8fd | 537 | int32_t cc3000_socket::gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) { |
Kojto | 20:30b6ed7bf8fd | 538 | tBsdGethostbynameParams ret; |
Kojto | 20:30b6ed7bf8fd | 539 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 540 | |
Kojto | 20:30b6ed7bf8fd | 541 | errno = EFAIL; |
Kojto | 20:30b6ed7bf8fd | 542 | |
Kojto | 20:30b6ed7bf8fd | 543 | if (name_length > HOSTNAME_MAX_LENGTH) |
Kojto | 20:30b6ed7bf8fd | 544 | { |
Kojto | 20:30b6ed7bf8fd | 545 | return errno; |
Kojto | 20:30b6ed7bf8fd | 546 | } |
Kojto | 20:30b6ed7bf8fd | 547 | |
Kojto | 20:30b6ed7bf8fd | 548 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 549 | args = (ptr + SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 550 | |
Kojto | 20:30b6ed7bf8fd | 551 | // Fill in HCI packet structure |
Kojto | 20:30b6ed7bf8fd | 552 | args = UINT32_TO_STREAM(args, 8); |
Kojto | 20:30b6ed7bf8fd | 553 | args = UINT32_TO_STREAM(args, name_length); |
Kojto | 20:30b6ed7bf8fd | 554 | ARRAY_TO_STREAM(args, hostname, name_length); |
Kojto | 20:30b6ed7bf8fd | 555 | |
Kojto | 20:30b6ed7bf8fd | 556 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 557 | _hci.command_send(HCI_CMND_GETHOSTNAME, ptr, SOCKET_GET_HOST_BY_NAME_PARAMS_LEN + name_length - 1); |
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_EVNT_BSD_GETHOSTBYNAME, &ret); |
Kojto | 20:30b6ed7bf8fd | 561 | |
Kojto | 20:30b6ed7bf8fd | 562 | errno = ret.retVal; |
Kojto | 20:30b6ed7bf8fd | 563 | |
Kojto | 20:30b6ed7bf8fd | 564 | (*((int32_t*)out_ip_addr)) = ret.outputAddress; |
Kojto | 20:30b6ed7bf8fd | 565 | |
Kojto | 20:30b6ed7bf8fd | 566 | return (errno); |
Kojto | 20:30b6ed7bf8fd | 567 | } |
Kojto | 20:30b6ed7bf8fd | 568 | |
Kojto | 20:30b6ed7bf8fd | 569 | int32_t cc3000_socket::setsockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen) { |
Kojto | 20:30b6ed7bf8fd | 570 | int32_t ret; |
Kojto | 20:30b6ed7bf8fd | 571 | uint8_t *ptr, *args; |
Kojto | 20:30b6ed7bf8fd | 572 | |
Kojto | 20:30b6ed7bf8fd | 573 | ptr = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 574 | args = (ptr + HEADERS_SIZE_CMD); |
Kojto | 20:30b6ed7bf8fd | 575 | |
Kojto | 20:30b6ed7bf8fd | 576 | // Fill in temporary command buffer |
Kojto | 20:30b6ed7bf8fd | 577 | args = UINT32_TO_STREAM(args, sd); |
Kojto | 20:30b6ed7bf8fd | 578 | args = UINT32_TO_STREAM(args, level); |
Kojto | 20:30b6ed7bf8fd | 579 | args = UINT32_TO_STREAM(args, optname); |
Kojto | 20:30b6ed7bf8fd | 580 | args = UINT32_TO_STREAM(args, 0x00000008); |
Kojto | 20:30b6ed7bf8fd | 581 | args = UINT32_TO_STREAM(args, optlen); |
Kojto | 20:30b6ed7bf8fd | 582 | ARRAY_TO_STREAM(args, ((uint8_t *)optval), optlen); |
Kojto | 20:30b6ed7bf8fd | 583 | |
Kojto | 20:30b6ed7bf8fd | 584 | // Initiate a HCI command |
Kojto | 20:30b6ed7bf8fd | 585 | _hci.command_send(HCI_CMND_SETSOCKOPT, ptr, SOCKET_SET_SOCK_OPT_PARAMS_LEN + optlen); |
Kojto | 20:30b6ed7bf8fd | 586 | |
Kojto | 20:30b6ed7bf8fd | 587 | // Since we are in blocking state - wait for event complete |
Kojto | 20:30b6ed7bf8fd | 588 | _event.simplelink_wait_event(HCI_CMND_SETSOCKOPT, &ret); |
Kojto | 20:30b6ed7bf8fd | 589 | |
Kojto | 20:30b6ed7bf8fd | 590 | if (ret >= 0) |
Kojto | 20:30b6ed7bf8fd | 591 | { |
Kojto | 20:30b6ed7bf8fd | 592 | return (0); |
Kojto | 20:30b6ed7bf8fd | 593 | } |
Kojto | 20:30b6ed7bf8fd | 594 | else |
Kojto | 20:30b6ed7bf8fd | 595 | { |
Kojto | 20:30b6ed7bf8fd | 596 | errno = ret; |
Kojto | 20:30b6ed7bf8fd | 597 | return ret; |
Kojto | 20:30b6ed7bf8fd | 598 | } |
Kojto | 20:30b6ed7bf8fd | 599 | } |
Kojto | 20:30b6ed7bf8fd | 600 | |
Kojto | 20:30b6ed7bf8fd | 601 | #endif |
Kojto | 20:30b6ed7bf8fd | 602 | |
SolderSplashLabs | 42:bd2c631a031a | 603 | char * cc3000_socket::inet_ntoa_r(uint32_t s_addr, char *buf, int buflen) |
SolderSplashLabs | 42:bd2c631a031a | 604 | { |
SolderSplashLabs | 42:bd2c631a031a | 605 | char inv[3]; |
SolderSplashLabs | 42:bd2c631a031a | 606 | char *rp; |
SolderSplashLabs | 42:bd2c631a031a | 607 | uint8_t *ap; |
SolderSplashLabs | 42:bd2c631a031a | 608 | uint8_t rem; |
SolderSplashLabs | 42:bd2c631a031a | 609 | uint8_t n; |
SolderSplashLabs | 42:bd2c631a031a | 610 | uint8_t i; |
SolderSplashLabs | 42:bd2c631a031a | 611 | int len = 0; |
SolderSplashLabs | 42:bd2c631a031a | 612 | |
SolderSplashLabs | 42:bd2c631a031a | 613 | rp = buf; |
SolderSplashLabs | 42:bd2c631a031a | 614 | ap = (uint8_t *)&s_addr; |
SolderSplashLabs | 42:bd2c631a031a | 615 | for(n = 0; n < 4; n++) { |
SolderSplashLabs | 42:bd2c631a031a | 616 | i = 0; |
SolderSplashLabs | 42:bd2c631a031a | 617 | do { |
SolderSplashLabs | 42:bd2c631a031a | 618 | rem = *ap % (uint8_t)10; |
SolderSplashLabs | 42:bd2c631a031a | 619 | *ap /= (uint8_t)10; |
SolderSplashLabs | 42:bd2c631a031a | 620 | inv[i++] = '0' + rem; |
SolderSplashLabs | 42:bd2c631a031a | 621 | } while(*ap); |
SolderSplashLabs | 42:bd2c631a031a | 622 | while(i--) { |
SolderSplashLabs | 42:bd2c631a031a | 623 | if (len++ >= buflen) { |
SolderSplashLabs | 42:bd2c631a031a | 624 | return NULL; |
SolderSplashLabs | 42:bd2c631a031a | 625 | } |
SolderSplashLabs | 42:bd2c631a031a | 626 | *rp++ = inv[i]; |
SolderSplashLabs | 42:bd2c631a031a | 627 | } |
SolderSplashLabs | 42:bd2c631a031a | 628 | if (len++ >= buflen) { |
SolderSplashLabs | 42:bd2c631a031a | 629 | return NULL; |
SolderSplashLabs | 42:bd2c631a031a | 630 | } |
SolderSplashLabs | 42:bd2c631a031a | 631 | *rp++ = '.'; |
SolderSplashLabs | 42:bd2c631a031a | 632 | ap++; |
SolderSplashLabs | 42:bd2c631a031a | 633 | } |
SolderSplashLabs | 42:bd2c631a031a | 634 | *--rp = 0; |
SolderSplashLabs | 42:bd2c631a031a | 635 | return buf; |
SolderSplashLabs | 42:bd2c631a031a | 636 | } |
SolderSplashLabs | 42:bd2c631a031a | 637 | |
Kojto | 20:30b6ed7bf8fd | 638 | } /* end of cc3000 namespace */ |