TI's CC3100. A test demo with very little testing done!
Fork of cc3100_Test_Demo2 by
simplelink/cc3100_socket.cpp@9:b8da46b97970, 2017-07-30 (annotated)
- Committer:
- gaoyang
- Date:
- Sun Jul 30 06:32:08 2017 +0000
- Revision:
- 9:b8da46b97970
- Parent:
- 6:778b081f6a13
DAS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 6:778b081f6a13 | 1 | /* |
dflet | 6:778b081f6a13 | 2 | * socket.c - CC31xx/CC32xx Host Driver Implementation |
dflet | 6:778b081f6a13 | 3 | * |
dflet | 6:778b081f6a13 | 4 | * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ |
dflet | 6:778b081f6a13 | 5 | * |
dflet | 6:778b081f6a13 | 6 | * |
dflet | 6:778b081f6a13 | 7 | * Redistribution and use in source and binary forms, with or without |
dflet | 6:778b081f6a13 | 8 | * modification, are permitted provided that the following conditions |
dflet | 6:778b081f6a13 | 9 | * are met: |
dflet | 6:778b081f6a13 | 10 | * |
dflet | 6:778b081f6a13 | 11 | * Redistributions of source code must retain the above copyright |
dflet | 6:778b081f6a13 | 12 | * notice, this list of conditions and the following disclaimer. |
dflet | 6:778b081f6a13 | 13 | * |
dflet | 6:778b081f6a13 | 14 | * Redistributions in binary form must reproduce the above copyright |
dflet | 6:778b081f6a13 | 15 | * notice, this list of conditions and the following disclaimer in the |
dflet | 6:778b081f6a13 | 16 | * documentation and/or other materials provided with the |
dflet | 6:778b081f6a13 | 17 | * distribution. |
dflet | 6:778b081f6a13 | 18 | * |
dflet | 6:778b081f6a13 | 19 | * Neither the name of Texas Instruments Incorporated nor the names of |
dflet | 6:778b081f6a13 | 20 | * its contributors may be used to endorse or promote products derived |
dflet | 6:778b081f6a13 | 21 | * from this software without specific prior written permission. |
dflet | 6:778b081f6a13 | 22 | * |
dflet | 6:778b081f6a13 | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
dflet | 6:778b081f6a13 | 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
dflet | 6:778b081f6a13 | 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
dflet | 6:778b081f6a13 | 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
dflet | 6:778b081f6a13 | 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
dflet | 6:778b081f6a13 | 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
dflet | 6:778b081f6a13 | 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
dflet | 6:778b081f6a13 | 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
dflet | 6:778b081f6a13 | 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
dflet | 6:778b081f6a13 | 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
dflet | 6:778b081f6a13 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dflet | 6:778b081f6a13 | 34 | * |
dflet | 6:778b081f6a13 | 35 | */ |
dflet | 6:778b081f6a13 | 36 | |
dflet | 6:778b081f6a13 | 37 | |
dflet | 6:778b081f6a13 | 38 | |
dflet | 6:778b081f6a13 | 39 | |
dflet | 6:778b081f6a13 | 40 | /*****************************************************************************/ |
dflet | 6:778b081f6a13 | 41 | /* Include files */ |
dflet | 6:778b081f6a13 | 42 | /*****************************************************************************/ |
dflet | 6:778b081f6a13 | 43 | #include "cc3100_simplelink.h" |
dflet | 6:778b081f6a13 | 44 | #include "cc3100_protocol.h" |
dflet | 6:778b081f6a13 | 45 | #include "cc3100_driver.h" |
dflet | 6:778b081f6a13 | 46 | |
dflet | 6:778b081f6a13 | 47 | #include "cc3100_socket.h" |
dflet | 6:778b081f6a13 | 48 | |
dflet | 6:778b081f6a13 | 49 | namespace mbed_cc3100 { |
dflet | 6:778b081f6a13 | 50 | |
dflet | 6:778b081f6a13 | 51 | /* Note: parsing of family and port in the generic way for all IPV4, IPV6 and EUI48 */ |
dflet | 6:778b081f6a13 | 52 | /* is possible as _i32 as these parameters are in the same offset and size for these */ |
dflet | 6:778b081f6a13 | 53 | /* three families. */ |
dflet | 6:778b081f6a13 | 54 | #define SL_SOCKET_PAYLOAD_BASE (1350) |
dflet | 6:778b081f6a13 | 55 | |
dflet | 6:778b081f6a13 | 56 | const uint8_t _SlPayloadByProtocolLUT[16] = |
dflet | 6:778b081f6a13 | 57 | { |
dflet | 6:778b081f6a13 | 58 | (1472 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV4 */ |
dflet | 6:778b081f6a13 | 59 | (1460 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV4 */ |
dflet | 6:778b081f6a13 | 60 | (1452 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV6 */ |
dflet | 6:778b081f6a13 | 61 | (1440 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV6 */ |
dflet | 6:778b081f6a13 | 62 | (1386 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV4_SECURE */ |
dflet | 6:778b081f6a13 | 63 | (1386 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV4_SECURE */ |
dflet | 6:778b081f6a13 | 64 | (1396 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV6_SECURE */ |
dflet | 6:778b081f6a13 | 65 | (1396 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV6_SECURE */ |
dflet | 6:778b081f6a13 | 66 | (1476 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_TRANCEIVER */ |
dflet | 6:778b081f6a13 | 67 | (1514 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_PACKET */ |
dflet | 6:778b081f6a13 | 68 | (1480 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_IP4 */ |
dflet | 6:778b081f6a13 | 69 | (1480 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_IP6 */ |
dflet | 6:778b081f6a13 | 70 | (1440 - SL_SOCKET_PAYLOAD_BASE), /* Default */ |
dflet | 6:778b081f6a13 | 71 | (1440 - SL_SOCKET_PAYLOAD_BASE), /* Default */ |
dflet | 6:778b081f6a13 | 72 | (1440 - SL_SOCKET_PAYLOAD_BASE), /* Default */ |
dflet | 6:778b081f6a13 | 73 | (1440 - SL_SOCKET_PAYLOAD_BASE) /* Default */ |
dflet | 6:778b081f6a13 | 74 | }; |
dflet | 6:778b081f6a13 | 75 | |
dflet | 6:778b081f6a13 | 76 | |
dflet | 6:778b081f6a13 | 77 | cc3100_socket::cc3100_socket(cc3100_driver &driver, cc3100_nonos &nonos) |
dflet | 6:778b081f6a13 | 78 | : _driver(driver), _nonos(nonos) |
dflet | 6:778b081f6a13 | 79 | { |
dflet | 6:778b081f6a13 | 80 | |
dflet | 6:778b081f6a13 | 81 | } |
dflet | 6:778b081f6a13 | 82 | |
dflet | 6:778b081f6a13 | 83 | cc3100_socket::~cc3100_socket() |
dflet | 6:778b081f6a13 | 84 | { |
dflet | 6:778b081f6a13 | 85 | |
dflet | 6:778b081f6a13 | 86 | } |
dflet | 6:778b081f6a13 | 87 | |
dflet | 6:778b081f6a13 | 88 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 89 | /* Functions */ |
dflet | 6:778b081f6a13 | 90 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 91 | |
dflet | 6:778b081f6a13 | 92 | /* ******************************************************************************/ |
dflet | 6:778b081f6a13 | 93 | /* _sl_BuildAddress */ |
dflet | 6:778b081f6a13 | 94 | /* ******************************************************************************/ |
dflet | 6:778b081f6a13 | 95 | void cc3100_socket::_sl_BuildAddress(const SlSockAddr_t *addr, _SocketAddrCommand_u *pCmd) |
dflet | 6:778b081f6a13 | 96 | { |
dflet | 6:778b081f6a13 | 97 | /* Note: parsing of family and port in the generic way for all IPV4, IPV6 and EUI48 |
dflet | 6:778b081f6a13 | 98 | is possible as long as these parameters are in the same offset and size for these |
dflet | 6:778b081f6a13 | 99 | three families. */ |
dflet | 6:778b081f6a13 | 100 | pCmd->IpV4.FamilyAndFlags = (addr->sa_family << 4) & 0xF0; |
dflet | 6:778b081f6a13 | 101 | pCmd->IpV4.port = ((SlSockAddrIn_t *)addr)->sin_port; |
dflet | 6:778b081f6a13 | 102 | |
dflet | 6:778b081f6a13 | 103 | if(SL_AF_INET == addr->sa_family) { |
dflet | 6:778b081f6a13 | 104 | pCmd->IpV4.address = ((SlSockAddrIn_t *)addr)->sin_addr.s_addr; |
dflet | 6:778b081f6a13 | 105 | } else if (SL_AF_INET6_EUI_48 == addr->sa_family ) { |
dflet | 6:778b081f6a13 | 106 | memcpy( pCmd->IpV6EUI48.address,((SlSockAddrIn6_t *)addr)->sin6_addr._S6_un._S6_u8, 6); |
dflet | 6:778b081f6a13 | 107 | } |
dflet | 6:778b081f6a13 | 108 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 109 | else { |
dflet | 6:778b081f6a13 | 110 | memcpy(pCmd->IpV6.address, ((sockaddr_in6 *)addr)->sin6_addr._S6_un._S6_u32, 16 ); |
dflet | 6:778b081f6a13 | 111 | } |
dflet | 6:778b081f6a13 | 112 | #endif |
dflet | 6:778b081f6a13 | 113 | } |
dflet | 6:778b081f6a13 | 114 | |
dflet | 6:778b081f6a13 | 115 | /* ******************************************************************************/ |
dflet | 6:778b081f6a13 | 116 | /* _sl_TruncatePayloadByProtocol */ |
dflet | 6:778b081f6a13 | 117 | /* ******************************************************************************/ |
dflet | 6:778b081f6a13 | 118 | uint16_t cc3100_socket::_sl_TruncatePayloadByProtocol(const int16_t sd,const uint16_t length) |
dflet | 6:778b081f6a13 | 119 | { |
dflet | 6:778b081f6a13 | 120 | |
dflet | 6:778b081f6a13 | 121 | uint32_t maxLength; |
dflet | 6:778b081f6a13 | 122 | |
dflet | 6:778b081f6a13 | 123 | maxLength = SL_SOCKET_PAYLOAD_BASE + _SlPayloadByProtocolLUT[((sd & SL_SOCKET_PAYLOAD_TYPE_MASK) >> 4)]; |
dflet | 6:778b081f6a13 | 124 | |
dflet | 6:778b081f6a13 | 125 | if( length > maxLength ) |
dflet | 6:778b081f6a13 | 126 | { |
dflet | 6:778b081f6a13 | 127 | return maxLength; |
dflet | 6:778b081f6a13 | 128 | } |
dflet | 6:778b081f6a13 | 129 | else |
dflet | 6:778b081f6a13 | 130 | { |
dflet | 6:778b081f6a13 | 131 | return length; |
dflet | 6:778b081f6a13 | 132 | } |
dflet | 6:778b081f6a13 | 133 | |
dflet | 6:778b081f6a13 | 134 | } |
dflet | 6:778b081f6a13 | 135 | |
dflet | 6:778b081f6a13 | 136 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 137 | /* _sl_ParseAddress */ |
dflet | 6:778b081f6a13 | 138 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 139 | #ifndef SL_TINY_EXT |
dflet | 6:778b081f6a13 | 140 | void cc3100_socket::_sl_ParseAddress(_SocketAddrResponse_u *pRsp, SlSockAddr_t *addr, SlSocklen_t *addrlen) |
dflet | 6:778b081f6a13 | 141 | { |
dflet | 6:778b081f6a13 | 142 | /* Note: parsing of family and port in the generic way for all IPV4, IPV6 and EUI48 */ |
dflet | 6:778b081f6a13 | 143 | /* is possible as long as these parameters are in the same offset and size for these */ |
dflet | 6:778b081f6a13 | 144 | /* three families. */ |
dflet | 6:778b081f6a13 | 145 | addr->sa_family = pRsp->IpV4.family; |
dflet | 6:778b081f6a13 | 146 | ((SlSockAddrIn_t *)addr)->sin_port = pRsp->IpV4.port; |
dflet | 6:778b081f6a13 | 147 | |
dflet | 6:778b081f6a13 | 148 | *addrlen = (SL_AF_INET == addr->sa_family) ? sizeof(SlSockAddrIn_t) : sizeof(SlSockAddrIn6_t); |
dflet | 6:778b081f6a13 | 149 | |
dflet | 6:778b081f6a13 | 150 | if(SL_AF_INET == addr->sa_family) { |
dflet | 6:778b081f6a13 | 151 | ((SlSockAddrIn_t *)addr)->sin_addr.s_addr = pRsp->IpV4.address; |
dflet | 6:778b081f6a13 | 152 | } else if (SL_AF_INET6_EUI_48 == addr->sa_family ) { |
dflet | 6:778b081f6a13 | 153 | memcpy(((SlSockAddrIn6_t *)addr)->sin6_addr._S6_un._S6_u8, pRsp->IpV6EUI48.address, 6); |
dflet | 6:778b081f6a13 | 154 | } |
dflet | 6:778b081f6a13 | 155 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 156 | else { |
dflet | 6:778b081f6a13 | 157 | memcpy(((sockaddr_in6 *)addr)->sin6_addr._S6_un._S6_u32, pRsp->IpV6.address, 16); |
dflet | 6:778b081f6a13 | 158 | } |
dflet | 6:778b081f6a13 | 159 | #endif |
dflet | 6:778b081f6a13 | 160 | } |
dflet | 6:778b081f6a13 | 161 | #endif |
dflet | 6:778b081f6a13 | 162 | |
dflet | 6:778b081f6a13 | 163 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 164 | /* sl_Socket */ |
dflet | 6:778b081f6a13 | 165 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 166 | typedef union { |
dflet | 6:778b081f6a13 | 167 | uint32_t Dummy; |
dflet | 6:778b081f6a13 | 168 | _SocketCommand_t Cmd; |
dflet | 6:778b081f6a13 | 169 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 170 | } _SlSockSocketMsg_u; |
dflet | 6:778b081f6a13 | 171 | |
dflet | 6:778b081f6a13 | 172 | #if _SL_INCLUDE_FUNC(sl_Socket) |
dflet | 6:778b081f6a13 | 173 | const _SlCmdCtrl_t _SlSockSocketCmdCtrl = { |
dflet | 6:778b081f6a13 | 174 | SL_OPCODE_SOCKET_SOCKET, |
dflet | 6:778b081f6a13 | 175 | sizeof(_SocketCommand_t), |
dflet | 6:778b081f6a13 | 176 | sizeof(_SocketResponse_t) |
dflet | 6:778b081f6a13 | 177 | }; |
dflet | 6:778b081f6a13 | 178 | |
dflet | 6:778b081f6a13 | 179 | int16_t cc3100_socket::sl_Socket(int16_t Domain, int16_t Type, int16_t Protocol) |
dflet | 6:778b081f6a13 | 180 | { |
dflet | 6:778b081f6a13 | 181 | _SlSockSocketMsg_u Msg; |
dflet | 6:778b081f6a13 | 182 | |
dflet | 6:778b081f6a13 | 183 | Msg.Cmd.Domain = (uint8_t)Domain; |
dflet | 6:778b081f6a13 | 184 | Msg.Cmd.Type = (uint8_t)Type; |
dflet | 6:778b081f6a13 | 185 | Msg.Cmd.Protocol = (uint8_t)Protocol; |
dflet | 6:778b081f6a13 | 186 | |
dflet | 6:778b081f6a13 | 187 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSockSocketCmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 188 | |
dflet | 6:778b081f6a13 | 189 | if( Msg.Rsp.statusOrLen < 0 ) { |
dflet | 6:778b081f6a13 | 190 | return( Msg.Rsp.statusOrLen ); |
dflet | 6:778b081f6a13 | 191 | } else { |
dflet | 6:778b081f6a13 | 192 | return (int16_t)((uint8_t)Msg.Rsp.sd); |
dflet | 6:778b081f6a13 | 193 | } |
dflet | 6:778b081f6a13 | 194 | } |
dflet | 6:778b081f6a13 | 195 | #endif |
dflet | 6:778b081f6a13 | 196 | |
dflet | 6:778b081f6a13 | 197 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 198 | /* sl_Close */ |
dflet | 6:778b081f6a13 | 199 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 200 | typedef union { |
dflet | 6:778b081f6a13 | 201 | _CloseCommand_t Cmd; |
dflet | 6:778b081f6a13 | 202 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 203 | } _SlSockCloseMsg_u; |
dflet | 6:778b081f6a13 | 204 | |
dflet | 6:778b081f6a13 | 205 | #if _SL_INCLUDE_FUNC(sl_Close) |
dflet | 6:778b081f6a13 | 206 | const _SlCmdCtrl_t _SlSockCloseCmdCtrl = { |
dflet | 6:778b081f6a13 | 207 | SL_OPCODE_SOCKET_CLOSE, |
dflet | 6:778b081f6a13 | 208 | sizeof(_CloseCommand_t), |
dflet | 6:778b081f6a13 | 209 | sizeof(_SocketResponse_t) |
dflet | 6:778b081f6a13 | 210 | }; |
dflet | 6:778b081f6a13 | 211 | |
dflet | 6:778b081f6a13 | 212 | int16_t cc3100_socket::sl_Close(int16_t sd) |
dflet | 6:778b081f6a13 | 213 | { |
dflet | 6:778b081f6a13 | 214 | _SlSockCloseMsg_u Msg; |
dflet | 6:778b081f6a13 | 215 | |
dflet | 6:778b081f6a13 | 216 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 217 | |
dflet | 6:778b081f6a13 | 218 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSockCloseCmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 219 | |
dflet | 6:778b081f6a13 | 220 | return Msg.Rsp.statusOrLen; |
dflet | 6:778b081f6a13 | 221 | } |
dflet | 6:778b081f6a13 | 222 | #endif |
dflet | 6:778b081f6a13 | 223 | |
dflet | 6:778b081f6a13 | 224 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 225 | /* sl_Bind */ |
dflet | 6:778b081f6a13 | 226 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 227 | typedef union { |
dflet | 6:778b081f6a13 | 228 | _SocketAddrCommand_u Cmd; |
dflet | 6:778b081f6a13 | 229 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 230 | } _SlSockBindMsg_u; |
dflet | 6:778b081f6a13 | 231 | |
dflet | 6:778b081f6a13 | 232 | #if _SL_INCLUDE_FUNC(sl_Bind) |
dflet | 6:778b081f6a13 | 233 | int16_t cc3100_socket::sl_Bind(int16_t sd, const SlSockAddr_t *addr, int16_t addrlen) |
dflet | 6:778b081f6a13 | 234 | { |
dflet | 6:778b081f6a13 | 235 | _SlSockBindMsg_u Msg; |
dflet | 6:778b081f6a13 | 236 | _SlCmdCtrl_t CmdCtrl = {0, 0, sizeof(_SocketResponse_t)}; |
dflet | 6:778b081f6a13 | 237 | |
dflet | 6:778b081f6a13 | 238 | switch(addr->sa_family) { |
dflet | 6:778b081f6a13 | 239 | case SL_AF_INET : |
dflet | 6:778b081f6a13 | 240 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND; |
dflet | 6:778b081f6a13 | 241 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv4Command_t); |
dflet | 6:778b081f6a13 | 242 | break; |
dflet | 6:778b081f6a13 | 243 | #ifndef SL_TINY_EXT |
dflet | 6:778b081f6a13 | 244 | case SL_AF_INET6_EUI_48: |
dflet | 6:778b081f6a13 | 245 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND_V6; |
dflet | 6:778b081f6a13 | 246 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv6EUI48Command_t); |
dflet | 6:778b081f6a13 | 247 | break; |
dflet | 6:778b081f6a13 | 248 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 249 | case AF_INET6: |
dflet | 6:778b081f6a13 | 250 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND_V6; |
dflet | 6:778b081f6a13 | 251 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv6Command_t); |
dflet | 6:778b081f6a13 | 252 | break; |
dflet | 6:778b081f6a13 | 253 | #endif |
dflet | 6:778b081f6a13 | 254 | #endif |
dflet | 6:778b081f6a13 | 255 | case SL_AF_RF : |
dflet | 6:778b081f6a13 | 256 | default: |
dflet | 6:778b081f6a13 | 257 | return SL_RET_CODE_INVALID_INPUT; |
dflet | 6:778b081f6a13 | 258 | } |
dflet | 6:778b081f6a13 | 259 | |
dflet | 6:778b081f6a13 | 260 | Msg.Cmd.IpV4.lenOrPadding = 0; |
dflet | 6:778b081f6a13 | 261 | Msg.Cmd.IpV4.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 262 | |
dflet | 6:778b081f6a13 | 263 | _sl_BuildAddress(addr, &Msg.Cmd); |
dflet | 6:778b081f6a13 | 264 | |
dflet | 6:778b081f6a13 | 265 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&CmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 266 | |
dflet | 6:778b081f6a13 | 267 | return Msg.Rsp.statusOrLen; |
dflet | 6:778b081f6a13 | 268 | } |
dflet | 6:778b081f6a13 | 269 | #endif |
dflet | 6:778b081f6a13 | 270 | |
dflet | 6:778b081f6a13 | 271 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 272 | /* sl_Sendto */ |
dflet | 6:778b081f6a13 | 273 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 274 | typedef union { |
dflet | 6:778b081f6a13 | 275 | _SocketAddrCommand_u Cmd; |
dflet | 6:778b081f6a13 | 276 | /* no response for 'sendto' commands*/ |
dflet | 6:778b081f6a13 | 277 | } _SlSendtoMsg_u; |
dflet | 6:778b081f6a13 | 278 | |
dflet | 6:778b081f6a13 | 279 | #if _SL_INCLUDE_FUNC(sl_SendTo) |
dflet | 6:778b081f6a13 | 280 | int16_t cc3100_socket::sl_SendTo(int16_t sd, const void *pBuf, int16_t Len, int16_t flags, const SlSockAddr_t *to, SlSocklen_t tolen) |
dflet | 6:778b081f6a13 | 281 | { |
dflet | 6:778b081f6a13 | 282 | _SlSendtoMsg_u Msg; |
dflet | 6:778b081f6a13 | 283 | _SlCmdCtrl_t CmdCtrl = {0, 0, 0}; |
dflet | 6:778b081f6a13 | 284 | _SlCmdExt_t CmdExt; |
dflet | 6:778b081f6a13 | 285 | uint16_t ChunkLen; |
dflet | 6:778b081f6a13 | 286 | int16_t RetVal; |
dflet | 6:778b081f6a13 | 287 | |
dflet | 6:778b081f6a13 | 288 | _driver._SlDrvResetCmdExt(&CmdExt); |
dflet | 6:778b081f6a13 | 289 | CmdExt.TxPayloadLen = (uint16_t)Len; |
dflet | 6:778b081f6a13 | 290 | CmdExt.pTxPayload = (uint8_t *)pBuf; |
dflet | 6:778b081f6a13 | 291 | |
dflet | 6:778b081f6a13 | 292 | switch(to->sa_family) { |
dflet | 6:778b081f6a13 | 293 | case SL_AF_INET: |
dflet | 6:778b081f6a13 | 294 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_SENDTO; |
dflet | 6:778b081f6a13 | 295 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv4Command_t); |
dflet | 6:778b081f6a13 | 296 | break; |
dflet | 6:778b081f6a13 | 297 | #ifndef SL_TINY_EXT |
dflet | 6:778b081f6a13 | 298 | case SL_AF_INET6_EUI_48: |
dflet | 6:778b081f6a13 | 299 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND_V6; |
dflet | 6:778b081f6a13 | 300 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv6EUI48Command_t); |
dflet | 6:778b081f6a13 | 301 | break; |
dflet | 6:778b081f6a13 | 302 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 303 | case AF_INET6: |
dflet | 6:778b081f6a13 | 304 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_SENDTO_V6; |
dflet | 6:778b081f6a13 | 305 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv6Command_t); |
dflet | 6:778b081f6a13 | 306 | break; |
dflet | 6:778b081f6a13 | 307 | #endif |
dflet | 6:778b081f6a13 | 308 | #endif |
dflet | 6:778b081f6a13 | 309 | case SL_AF_RF: |
dflet | 6:778b081f6a13 | 310 | default: |
dflet | 6:778b081f6a13 | 311 | return SL_RET_CODE_INVALID_INPUT; |
dflet | 6:778b081f6a13 | 312 | } |
dflet | 6:778b081f6a13 | 313 | |
dflet | 6:778b081f6a13 | 314 | ChunkLen = _sl_TruncatePayloadByProtocol(sd,Len); |
dflet | 6:778b081f6a13 | 315 | Msg.Cmd.IpV4.lenOrPadding = ChunkLen; |
dflet | 6:778b081f6a13 | 316 | CmdExt.TxPayloadLen = ChunkLen; |
dflet | 6:778b081f6a13 | 317 | |
dflet | 6:778b081f6a13 | 318 | Msg.Cmd.IpV4.sd = (unsigned char)sd; |
dflet | 6:778b081f6a13 | 319 | |
dflet | 6:778b081f6a13 | 320 | _sl_BuildAddress(to, &Msg.Cmd); |
dflet | 6:778b081f6a13 | 321 | |
dflet | 6:778b081f6a13 | 322 | Msg.Cmd.IpV4.FamilyAndFlags |= flags & 0x0F; |
dflet | 6:778b081f6a13 | 323 | |
dflet | 6:778b081f6a13 | 324 | do { |
dflet | 6:778b081f6a13 | 325 | RetVal = _driver._SlDrvDataWriteOp((_SlSd_t)sd, &CmdCtrl, &Msg, &CmdExt); |
dflet | 6:778b081f6a13 | 326 | |
dflet | 6:778b081f6a13 | 327 | if(SL_OS_RET_CODE_OK == RetVal) { |
dflet | 6:778b081f6a13 | 328 | CmdExt.pTxPayload += ChunkLen; |
dflet | 6:778b081f6a13 | 329 | ChunkLen = (uint16_t)((unsigned char *)pBuf + Len - CmdExt.pTxPayload); |
dflet | 6:778b081f6a13 | 330 | ChunkLen = _sl_TruncatePayloadByProtocol(sd,ChunkLen); |
dflet | 6:778b081f6a13 | 331 | CmdExt.TxPayloadLen = ChunkLen; |
dflet | 6:778b081f6a13 | 332 | Msg.Cmd.IpV4.lenOrPadding = ChunkLen; |
dflet | 6:778b081f6a13 | 333 | } else { |
dflet | 6:778b081f6a13 | 334 | return RetVal; |
dflet | 6:778b081f6a13 | 335 | } |
dflet | 6:778b081f6a13 | 336 | } while(ChunkLen > 0); |
dflet | 6:778b081f6a13 | 337 | |
dflet | 6:778b081f6a13 | 338 | return (int16_t)Len; |
dflet | 6:778b081f6a13 | 339 | } |
dflet | 6:778b081f6a13 | 340 | #endif |
dflet | 6:778b081f6a13 | 341 | |
dflet | 6:778b081f6a13 | 342 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 343 | /* sl_Recvfrom */ |
dflet | 6:778b081f6a13 | 344 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 345 | typedef union { |
dflet | 6:778b081f6a13 | 346 | _sendRecvCommand_t Cmd; |
dflet | 6:778b081f6a13 | 347 | _SocketAddrResponse_u Rsp; |
dflet | 6:778b081f6a13 | 348 | } _SlRecvfromMsg_u; |
dflet | 6:778b081f6a13 | 349 | |
dflet | 6:778b081f6a13 | 350 | const _SlCmdCtrl_t _SlRecvfomCmdCtrl = { |
dflet | 6:778b081f6a13 | 351 | SL_OPCODE_SOCKET_RECVFROM, |
dflet | 6:778b081f6a13 | 352 | sizeof(_sendRecvCommand_t), |
dflet | 6:778b081f6a13 | 353 | sizeof(_SocketAddrResponse_u) |
dflet | 6:778b081f6a13 | 354 | }; |
dflet | 6:778b081f6a13 | 355 | |
dflet | 6:778b081f6a13 | 356 | #if _SL_INCLUDE_FUNC(sl_RecvFrom) |
dflet | 6:778b081f6a13 | 357 | int16_t cc3100_socket::sl_RecvFrom(int16_t sd, void *buf, int16_t Len, int16_t flags, SlSockAddr_t *from, SlSocklen_t *fromlen) |
dflet | 6:778b081f6a13 | 358 | { |
dflet | 6:778b081f6a13 | 359 | _SlRecvfromMsg_u Msg; |
dflet | 6:778b081f6a13 | 360 | _SlCmdExt_t CmdExt; |
dflet | 6:778b081f6a13 | 361 | int16_t RetVal; |
dflet | 6:778b081f6a13 | 362 | |
dflet | 6:778b081f6a13 | 363 | _driver._SlDrvResetCmdExt(&CmdExt); |
dflet | 6:778b081f6a13 | 364 | CmdExt.RxPayloadLen = Len; |
dflet | 6:778b081f6a13 | 365 | CmdExt.pRxPayload = (uint8_t *)buf; |
dflet | 6:778b081f6a13 | 366 | |
dflet | 6:778b081f6a13 | 367 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 368 | Msg.Cmd.StatusOrLen = Len; |
dflet | 6:778b081f6a13 | 369 | /* no size truncation in recv path */ |
dflet | 6:778b081f6a13 | 370 | CmdExt.RxPayloadLen = Msg.Cmd.StatusOrLen; |
dflet | 6:778b081f6a13 | 371 | |
dflet | 6:778b081f6a13 | 372 | Msg.Cmd.FamilyAndFlags = flags & 0x0F; |
dflet | 6:778b081f6a13 | 373 | |
dflet | 6:778b081f6a13 | 374 | if(sizeof(SlSockAddrIn_t) == *fromlen) { |
dflet | 6:778b081f6a13 | 375 | Msg.Cmd.FamilyAndFlags |= (SL_AF_INET << 4); |
dflet | 6:778b081f6a13 | 376 | } |
dflet | 6:778b081f6a13 | 377 | else if (sizeof(SlSockAddrIn6_t) == *fromlen) |
dflet | 6:778b081f6a13 | 378 | { |
dflet | 6:778b081f6a13 | 379 | Msg.Cmd.FamilyAndFlags |= (SL_AF_INET6 << 4); |
dflet | 6:778b081f6a13 | 380 | } |
dflet | 6:778b081f6a13 | 381 | else |
dflet | 6:778b081f6a13 | 382 | { |
dflet | 6:778b081f6a13 | 383 | return SL_RET_CODE_INVALID_INPUT; |
dflet | 6:778b081f6a13 | 384 | } |
dflet | 6:778b081f6a13 | 385 | |
dflet | 6:778b081f6a13 | 386 | RetVal = _driver._SlDrvDataReadOp((_SlSd_t)sd, (_SlCmdCtrl_t *)&_SlRecvfomCmdCtrl, &Msg, &CmdExt); |
dflet | 6:778b081f6a13 | 387 | if( RetVal != SL_OS_RET_CODE_OK ) { |
dflet | 6:778b081f6a13 | 388 | return RetVal; |
dflet | 6:778b081f6a13 | 389 | } |
dflet | 6:778b081f6a13 | 390 | |
dflet | 6:778b081f6a13 | 391 | RetVal = Msg.Rsp.IpV4.statusOrLen; |
dflet | 6:778b081f6a13 | 392 | |
dflet | 6:778b081f6a13 | 393 | if(RetVal >= 0) { |
dflet | 6:778b081f6a13 | 394 | VERIFY_PROTOCOL(sd == Msg.Rsp.IpV4.sd); |
dflet | 6:778b081f6a13 | 395 | #if 0 |
dflet | 6:778b081f6a13 | 396 | _sl_ParseAddress(&Msg.Rsp, from, fromlen); |
dflet | 6:778b081f6a13 | 397 | #else |
dflet | 6:778b081f6a13 | 398 | from->sa_family = Msg.Rsp.IpV4.family; |
dflet | 6:778b081f6a13 | 399 | if(SL_AF_INET == from->sa_family) { |
dflet | 6:778b081f6a13 | 400 | ((SlSockAddrIn_t *)from)->sin_port = Msg.Rsp.IpV4.port; |
dflet | 6:778b081f6a13 | 401 | ((SlSockAddrIn_t *)from)->sin_addr.s_addr = Msg.Rsp.IpV4.address; |
dflet | 6:778b081f6a13 | 402 | *fromlen = sizeof(SlSockAddrIn_t); |
dflet | 6:778b081f6a13 | 403 | } else if (SL_AF_INET6_EUI_48 == from->sa_family ) { |
dflet | 6:778b081f6a13 | 404 | ((SlSockAddrIn6_t *)from)->sin6_port = Msg.Rsp.IpV6EUI48.port; |
dflet | 6:778b081f6a13 | 405 | memcpy(((SlSockAddrIn6_t *)from)->sin6_addr._S6_un._S6_u8, Msg.Rsp.IpV6EUI48.address, 6); |
dflet | 6:778b081f6a13 | 406 | } |
dflet | 6:778b081f6a13 | 407 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 408 | else if(AF_INET6 == from->sa_family) { |
dflet | 6:778b081f6a13 | 409 | VERIFY_PROTOCOL(*fromlen >= sizeof(sockaddr_in6)); |
dflet | 6:778b081f6a13 | 410 | |
dflet | 6:778b081f6a13 | 411 | ((sockaddr_in6 *)from)->sin6_port = Msg.Rsp.IpV6.port; |
dflet | 6:778b081f6a13 | 412 | memcpy(((sockaddr_in6 *)from)->sin6_addr._S6_un._S6_u32, Msg.Rsp.IpV6.address, 16); |
dflet | 6:778b081f6a13 | 413 | *fromlen = sizeof(sockaddr_in6); |
dflet | 6:778b081f6a13 | 414 | } |
dflet | 6:778b081f6a13 | 415 | #endif |
dflet | 6:778b081f6a13 | 416 | #endif |
dflet | 6:778b081f6a13 | 417 | } |
dflet | 6:778b081f6a13 | 418 | |
dflet | 6:778b081f6a13 | 419 | return (int16_t)RetVal; |
dflet | 6:778b081f6a13 | 420 | } |
dflet | 6:778b081f6a13 | 421 | #endif |
dflet | 6:778b081f6a13 | 422 | |
dflet | 6:778b081f6a13 | 423 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 424 | /* sl_Connect */ |
dflet | 6:778b081f6a13 | 425 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 426 | typedef union { |
dflet | 6:778b081f6a13 | 427 | _SocketAddrCommand_u Cmd; |
dflet | 6:778b081f6a13 | 428 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 429 | } _SlSockConnectMsg_u; |
dflet | 6:778b081f6a13 | 430 | |
dflet | 6:778b081f6a13 | 431 | #if _SL_INCLUDE_FUNC(sl_Connect) |
dflet | 6:778b081f6a13 | 432 | int16_t cc3100_socket::sl_Connect(int16_t sd, const SlSockAddr_t *addr, int16_t addrlen) |
dflet | 6:778b081f6a13 | 433 | { |
dflet | 6:778b081f6a13 | 434 | _SlSockConnectMsg_u Msg; |
dflet | 6:778b081f6a13 | 435 | _SlReturnVal_t RetVal; |
dflet | 6:778b081f6a13 | 436 | _SlCmdCtrl_t CmdCtrl = {0, 0, sizeof(_SocketResponse_t)}; |
dflet | 6:778b081f6a13 | 437 | _SocketResponse_t AsyncRsp; |
dflet | 6:778b081f6a13 | 438 | uint8_t ObjIdx = MAX_CONCURRENT_ACTIONS; |
dflet | 6:778b081f6a13 | 439 | |
dflet | 6:778b081f6a13 | 440 | |
dflet | 6:778b081f6a13 | 441 | switch(addr->sa_family) { |
dflet | 6:778b081f6a13 | 442 | case SL_AF_INET : |
dflet | 6:778b081f6a13 | 443 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_CONNECT; |
dflet | 6:778b081f6a13 | 444 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv4Command_t); |
dflet | 6:778b081f6a13 | 445 | /* Do nothing - cmd already initialized to this type */ |
dflet | 6:778b081f6a13 | 446 | break; |
dflet | 6:778b081f6a13 | 447 | case SL_AF_INET6_EUI_48: |
dflet | 6:778b081f6a13 | 448 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_CONNECT_V6; |
dflet | 6:778b081f6a13 | 449 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv6EUI48Command_t); |
dflet | 6:778b081f6a13 | 450 | break; |
dflet | 6:778b081f6a13 | 451 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 452 | case AF_INET6: |
dflet | 6:778b081f6a13 | 453 | CmdCtrl.Opcode = SL_OPCODE_SOCKET_CONNECT_V6; |
dflet | 6:778b081f6a13 | 454 | CmdCtrl.TxDescLen = sizeof(_SocketAddrIPv6Command_t); |
dflet | 6:778b081f6a13 | 455 | break; |
dflet | 6:778b081f6a13 | 456 | #endif |
dflet | 6:778b081f6a13 | 457 | case SL_AF_RF : |
dflet | 6:778b081f6a13 | 458 | default: |
dflet | 6:778b081f6a13 | 459 | return SL_RET_CODE_INVALID_INPUT; |
dflet | 6:778b081f6a13 | 460 | } |
dflet | 6:778b081f6a13 | 461 | |
dflet | 6:778b081f6a13 | 462 | Msg.Cmd.IpV4.lenOrPadding = 0; |
dflet | 6:778b081f6a13 | 463 | Msg.Cmd.IpV4.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 464 | |
dflet | 6:778b081f6a13 | 465 | _sl_BuildAddress(addr, &Msg.Cmd); |
dflet | 6:778b081f6a13 | 466 | |
dflet | 6:778b081f6a13 | 467 | |
dflet | 6:778b081f6a13 | 468 | ObjIdx = _driver._SlDrvProtectAsyncRespSetting((uint8_t*)&AsyncRsp, CONNECT_ID, sd & BSD_SOCKET_ID_MASK); |
dflet | 6:778b081f6a13 | 469 | |
dflet | 6:778b081f6a13 | 470 | if (MAX_CONCURRENT_ACTIONS == ObjIdx) |
dflet | 6:778b081f6a13 | 471 | { |
dflet | 6:778b081f6a13 | 472 | return SL_POOL_IS_EMPTY; |
dflet | 6:778b081f6a13 | 473 | } |
dflet | 6:778b081f6a13 | 474 | |
dflet | 6:778b081f6a13 | 475 | /* send the command */ |
dflet | 6:778b081f6a13 | 476 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&CmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 477 | VERIFY_PROTOCOL(Msg.Rsp.sd == sd) |
dflet | 6:778b081f6a13 | 478 | |
dflet | 6:778b081f6a13 | 479 | RetVal = Msg.Rsp.statusOrLen; |
dflet | 6:778b081f6a13 | 480 | |
dflet | 6:778b081f6a13 | 481 | if(SL_RET_CODE_OK == RetVal) { |
dflet | 6:778b081f6a13 | 482 | /* wait for async and get Data Read parameters */ |
dflet | 6:778b081f6a13 | 483 | _driver._SlDrvSyncObjWaitForever(&g_pCB->ObjPool[ObjIdx].SyncObj); |
dflet | 6:778b081f6a13 | 484 | |
dflet | 6:778b081f6a13 | 485 | VERIFY_PROTOCOL(AsyncRsp.sd == sd); |
dflet | 6:778b081f6a13 | 486 | |
dflet | 6:778b081f6a13 | 487 | RetVal = AsyncRsp.statusOrLen; |
dflet | 6:778b081f6a13 | 488 | } |
dflet | 6:778b081f6a13 | 489 | _driver._SlDrvReleasePoolObj(ObjIdx); |
dflet | 6:778b081f6a13 | 490 | return RetVal; |
dflet | 6:778b081f6a13 | 491 | } |
dflet | 6:778b081f6a13 | 492 | #endif |
dflet | 6:778b081f6a13 | 493 | |
dflet | 6:778b081f6a13 | 494 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 495 | /* sl_Send */ |
dflet | 6:778b081f6a13 | 496 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 497 | typedef union { |
dflet | 6:778b081f6a13 | 498 | _sendRecvCommand_t Cmd; |
dflet | 6:778b081f6a13 | 499 | /* no response for 'sendto' commands*/ |
dflet | 6:778b081f6a13 | 500 | } _SlSendMsg_u; |
dflet | 6:778b081f6a13 | 501 | |
dflet | 6:778b081f6a13 | 502 | const _SlCmdCtrl_t _SlSendCmdCtrl = { |
dflet | 6:778b081f6a13 | 503 | SL_OPCODE_SOCKET_SEND, |
dflet | 6:778b081f6a13 | 504 | sizeof(_sendRecvCommand_t), |
dflet | 6:778b081f6a13 | 505 | 0 |
dflet | 6:778b081f6a13 | 506 | }; |
dflet | 6:778b081f6a13 | 507 | |
dflet | 6:778b081f6a13 | 508 | #if _SL_INCLUDE_FUNC(sl_Send) |
dflet | 6:778b081f6a13 | 509 | int16_t cc3100_socket::sl_Send(int16_t sd, const void *pBuf, int16_t Len, int16_t flags) |
dflet | 6:778b081f6a13 | 510 | { |
dflet | 6:778b081f6a13 | 511 | _SlSendMsg_u Msg; |
dflet | 6:778b081f6a13 | 512 | _SlCmdExt_t CmdExt; |
dflet | 6:778b081f6a13 | 513 | uint16_t ChunkLen; |
dflet | 6:778b081f6a13 | 514 | int16_t RetVal; |
dflet | 6:778b081f6a13 | 515 | uint32_t tempVal; |
dflet | 6:778b081f6a13 | 516 | uint8_t runSingleChunk = FALSE; |
dflet | 6:778b081f6a13 | 517 | |
dflet | 6:778b081f6a13 | 518 | _driver._SlDrvResetCmdExt(&CmdExt); |
dflet | 6:778b081f6a13 | 519 | CmdExt.TxPayloadLen = Len; |
dflet | 6:778b081f6a13 | 520 | CmdExt.pTxPayload = (uint8_t *)pBuf; |
dflet | 6:778b081f6a13 | 521 | |
dflet | 6:778b081f6a13 | 522 | /* Only for RAW transceiver type socket, relay the flags parameter in the 2 bytes (4 byte aligned) before the actual payload */ |
dflet | 6:778b081f6a13 | 523 | if ((sd & SL_SOCKET_PAYLOAD_TYPE_MASK) == SL_SOCKET_PAYLOAD_TYPE_RAW_TRANCEIVER) { |
dflet | 6:778b081f6a13 | 524 | tempVal = flags; |
dflet | 6:778b081f6a13 | 525 | CmdExt.pRxPayload = (uint8_t *)&tempVal; |
dflet | 6:778b081f6a13 | 526 | CmdExt.RxPayloadLen = -4; /* mark as Rx data to send */ |
dflet | 6:778b081f6a13 | 527 | runSingleChunk = TRUE; |
dflet | 6:778b081f6a13 | 528 | } else { |
dflet | 6:778b081f6a13 | 529 | CmdExt.pRxPayload = NULL; |
dflet | 6:778b081f6a13 | 530 | } |
dflet | 6:778b081f6a13 | 531 | |
dflet | 6:778b081f6a13 | 532 | ChunkLen = _sl_TruncatePayloadByProtocol(sd,Len); |
dflet | 6:778b081f6a13 | 533 | CmdExt.TxPayloadLen = ChunkLen; |
dflet | 6:778b081f6a13 | 534 | |
dflet | 6:778b081f6a13 | 535 | Msg.Cmd.StatusOrLen = ChunkLen; |
dflet | 6:778b081f6a13 | 536 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 537 | Msg.Cmd.FamilyAndFlags |= flags & 0x0F; |
dflet | 6:778b081f6a13 | 538 | |
dflet | 6:778b081f6a13 | 539 | do { |
dflet | 6:778b081f6a13 | 540 | RetVal = _driver._SlDrvDataWriteOp((uint8_t)sd, (_SlCmdCtrl_t *)&_SlSendCmdCtrl, &Msg, &CmdExt); |
dflet | 6:778b081f6a13 | 541 | if(SL_OS_RET_CODE_OK == RetVal) { |
dflet | 6:778b081f6a13 | 542 | CmdExt.pTxPayload += ChunkLen; |
dflet | 6:778b081f6a13 | 543 | ChunkLen = (uint8_t *)pBuf + Len - CmdExt.pTxPayload; |
dflet | 6:778b081f6a13 | 544 | ChunkLen = _sl_TruncatePayloadByProtocol(sd,ChunkLen); |
dflet | 6:778b081f6a13 | 545 | CmdExt.TxPayloadLen = ChunkLen; |
dflet | 6:778b081f6a13 | 546 | Msg.Cmd.StatusOrLen = ChunkLen; |
dflet | 6:778b081f6a13 | 547 | } else { |
dflet | 6:778b081f6a13 | 548 | return RetVal; |
dflet | 6:778b081f6a13 | 549 | } |
dflet | 6:778b081f6a13 | 550 | } while((ChunkLen > 0) && (runSingleChunk==FALSE)); |
dflet | 6:778b081f6a13 | 551 | |
dflet | 6:778b081f6a13 | 552 | return (int16_t)Len; |
dflet | 6:778b081f6a13 | 553 | } |
dflet | 6:778b081f6a13 | 554 | #endif |
dflet | 6:778b081f6a13 | 555 | |
dflet | 6:778b081f6a13 | 556 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 557 | /* sl_Listen */ |
dflet | 6:778b081f6a13 | 558 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 559 | typedef union { |
dflet | 6:778b081f6a13 | 560 | _ListenCommand_t Cmd; |
dflet | 6:778b081f6a13 | 561 | _BasicResponse_t Rsp; |
dflet | 6:778b081f6a13 | 562 | } _SlListenMsg_u; |
dflet | 6:778b081f6a13 | 563 | |
dflet | 6:778b081f6a13 | 564 | #if _SL_INCLUDE_FUNC(sl_Listen) |
dflet | 6:778b081f6a13 | 565 | const _SlCmdCtrl_t _SlListenCmdCtrl = { |
dflet | 6:778b081f6a13 | 566 | SL_OPCODE_SOCKET_LISTEN, |
dflet | 6:778b081f6a13 | 567 | sizeof(_ListenCommand_t), |
dflet | 6:778b081f6a13 | 568 | sizeof(_BasicResponse_t), |
dflet | 6:778b081f6a13 | 569 | }; |
dflet | 6:778b081f6a13 | 570 | |
dflet | 6:778b081f6a13 | 571 | int16_t cc3100_socket::sl_Listen(int16_t sd, int16_t backlog) |
dflet | 6:778b081f6a13 | 572 | { |
dflet | 6:778b081f6a13 | 573 | _SlListenMsg_u Msg; |
dflet | 6:778b081f6a13 | 574 | |
dflet | 6:778b081f6a13 | 575 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 576 | Msg.Cmd.backlog = (uint8_t)backlog; |
dflet | 6:778b081f6a13 | 577 | |
dflet | 6:778b081f6a13 | 578 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlListenCmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 579 | |
dflet | 6:778b081f6a13 | 580 | return (int16_t)Msg.Rsp.status; |
dflet | 6:778b081f6a13 | 581 | } |
dflet | 6:778b081f6a13 | 582 | #endif |
dflet | 6:778b081f6a13 | 583 | |
dflet | 6:778b081f6a13 | 584 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 585 | /* sl_Accept */ |
dflet | 6:778b081f6a13 | 586 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 587 | typedef union { |
dflet | 6:778b081f6a13 | 588 | _AcceptCommand_t Cmd; |
dflet | 6:778b081f6a13 | 589 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 590 | } _SlSockAcceptMsg_u; |
dflet | 6:778b081f6a13 | 591 | |
dflet | 6:778b081f6a13 | 592 | #if _SL_INCLUDE_FUNC(sl_Accept) |
dflet | 6:778b081f6a13 | 593 | const _SlCmdCtrl_t _SlAcceptCmdCtrl = { |
dflet | 6:778b081f6a13 | 594 | SL_OPCODE_SOCKET_ACCEPT, |
dflet | 6:778b081f6a13 | 595 | sizeof(_AcceptCommand_t), |
dflet | 6:778b081f6a13 | 596 | sizeof(_BasicResponse_t), |
dflet | 6:778b081f6a13 | 597 | }; |
dflet | 6:778b081f6a13 | 598 | |
dflet | 6:778b081f6a13 | 599 | int16_t cc3100_socket::sl_Accept(int16_t sd, SlSockAddr_t *addr, SlSocklen_t *addrlen) |
dflet | 6:778b081f6a13 | 600 | { |
dflet | 6:778b081f6a13 | 601 | _SlSockAcceptMsg_u Msg; |
dflet | 6:778b081f6a13 | 602 | _SlReturnVal_t RetVal; |
dflet | 6:778b081f6a13 | 603 | _SocketAddrResponse_u AsyncRsp; |
dflet | 6:778b081f6a13 | 604 | |
dflet | 6:778b081f6a13 | 605 | uint8_t ObjIdx = MAX_CONCURRENT_ACTIONS; |
dflet | 6:778b081f6a13 | 606 | |
dflet | 6:778b081f6a13 | 607 | |
dflet | 6:778b081f6a13 | 608 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 609 | Msg.Cmd.family = (sizeof(SlSockAddrIn_t) == *addrlen) ? SL_AF_INET : SL_AF_INET6; |
dflet | 6:778b081f6a13 | 610 | |
dflet | 6:778b081f6a13 | 611 | ObjIdx = _driver._SlDrvProtectAsyncRespSetting((uint8_t*)&AsyncRsp, ACCEPT_ID, sd & BSD_SOCKET_ID_MASK ); |
dflet | 6:778b081f6a13 | 612 | |
dflet | 6:778b081f6a13 | 613 | if (MAX_CONCURRENT_ACTIONS == ObjIdx) |
dflet | 6:778b081f6a13 | 614 | { |
dflet | 6:778b081f6a13 | 615 | return SL_POOL_IS_EMPTY; |
dflet | 6:778b081f6a13 | 616 | } |
dflet | 6:778b081f6a13 | 617 | |
dflet | 6:778b081f6a13 | 618 | /* send the command */ |
dflet | 6:778b081f6a13 | 619 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlAcceptCmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 620 | VERIFY_PROTOCOL(Msg.Rsp.sd == sd); |
dflet | 6:778b081f6a13 | 621 | |
dflet | 6:778b081f6a13 | 622 | RetVal = Msg.Rsp.statusOrLen; |
dflet | 6:778b081f6a13 | 623 | |
dflet | 6:778b081f6a13 | 624 | if(SL_OS_RET_CODE_OK == RetVal) { |
dflet | 6:778b081f6a13 | 625 | /* wait for async and get Data Read parameters */ |
dflet | 6:778b081f6a13 | 626 | _driver._SlDrvSyncObjWaitForever(&g_pCB->ObjPool[ObjIdx].SyncObj); |
dflet | 6:778b081f6a13 | 627 | |
dflet | 6:778b081f6a13 | 628 | VERIFY_PROTOCOL(AsyncRsp.IpV4.sd == sd); |
dflet | 6:778b081f6a13 | 629 | |
dflet | 6:778b081f6a13 | 630 | RetVal = AsyncRsp.IpV4.statusOrLen; |
dflet | 6:778b081f6a13 | 631 | if( (NULL != addr) && (NULL != addrlen) ) { |
dflet | 6:778b081f6a13 | 632 | #if 0 /* Kept for backup */ |
dflet | 6:778b081f6a13 | 633 | _sl_ParseAddress(&AsyncRsp, addr, addrlen); |
dflet | 6:778b081f6a13 | 634 | #else |
dflet | 6:778b081f6a13 | 635 | addr->sa_family = AsyncRsp.IpV4.family; |
dflet | 6:778b081f6a13 | 636 | |
dflet | 6:778b081f6a13 | 637 | if(SL_AF_INET == addr->sa_family) { |
dflet | 6:778b081f6a13 | 638 | if( *addrlen == sizeof( SlSockAddrIn_t ) ) { |
dflet | 6:778b081f6a13 | 639 | ((SlSockAddrIn_t *)addr)->sin_port = AsyncRsp.IpV4.port; |
dflet | 6:778b081f6a13 | 640 | ((SlSockAddrIn_t *)addr)->sin_addr.s_addr = AsyncRsp.IpV4.address; |
dflet | 6:778b081f6a13 | 641 | } else { |
dflet | 6:778b081f6a13 | 642 | *addrlen = 0; |
dflet | 6:778b081f6a13 | 643 | } |
dflet | 6:778b081f6a13 | 644 | } else if (SL_AF_INET6_EUI_48 == addr->sa_family ) { |
dflet | 6:778b081f6a13 | 645 | if( *addrlen == sizeof( SlSockAddrIn6_t ) ) { |
dflet | 6:778b081f6a13 | 646 | ((SlSockAddrIn6_t *)addr)->sin6_port = AsyncRsp.IpV6EUI48.port ; |
dflet | 6:778b081f6a13 | 647 | /* will be called from here and from _sl_BuildAddress*/ |
dflet | 6:778b081f6a13 | 648 | memcpy(((SlSockAddrIn6_t *)addr)->sin6_addr._S6_un._S6_u8, AsyncRsp.IpV6EUI48.address, 6); |
dflet | 6:778b081f6a13 | 649 | } else { |
dflet | 6:778b081f6a13 | 650 | *addrlen = 0; |
dflet | 6:778b081f6a13 | 651 | } |
dflet | 6:778b081f6a13 | 652 | } |
dflet | 6:778b081f6a13 | 653 | #ifdef SL_SUPPORT_IPV6 |
dflet | 6:778b081f6a13 | 654 | else { |
dflet | 6:778b081f6a13 | 655 | if( *addrlen == sizeof( sockaddr_in6 ) ) { |
dflet | 6:778b081f6a13 | 656 | ((sockaddr_in6 *)addr)->sin6_port = AsyncRsp.IpV6.port ; |
dflet | 6:778b081f6a13 | 657 | memcpy(((sockaddr_in6 *)addr)->sin6_addr._S6_un._S6_u32, AsyncRsp.IpV6.address, 16); |
dflet | 6:778b081f6a13 | 658 | } else { |
dflet | 6:778b081f6a13 | 659 | *addrlen = 0; |
dflet | 6:778b081f6a13 | 660 | } |
dflet | 6:778b081f6a13 | 661 | } |
dflet | 6:778b081f6a13 | 662 | #endif |
dflet | 6:778b081f6a13 | 663 | #endif |
dflet | 6:778b081f6a13 | 664 | } |
dflet | 6:778b081f6a13 | 665 | } |
dflet | 6:778b081f6a13 | 666 | |
dflet | 6:778b081f6a13 | 667 | _driver._SlDrvReleasePoolObj(ObjIdx); |
dflet | 6:778b081f6a13 | 668 | return (int16_t)RetVal; |
dflet | 6:778b081f6a13 | 669 | } |
dflet | 6:778b081f6a13 | 670 | #endif |
dflet | 6:778b081f6a13 | 671 | |
dflet | 6:778b081f6a13 | 672 | |
dflet | 6:778b081f6a13 | 673 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 674 | /* sl_Htonl */ |
dflet | 6:778b081f6a13 | 675 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 676 | uint32_t cc3100_socket::sl_Htonl( uint32_t val ) |
dflet | 6:778b081f6a13 | 677 | { |
dflet | 6:778b081f6a13 | 678 | uint32_t i = 1; |
dflet | 6:778b081f6a13 | 679 | int8_t *p = (int8_t *)&i; |
dflet | 6:778b081f6a13 | 680 | if (p[0] == 1) { /* little endian */ |
dflet | 6:778b081f6a13 | 681 | p[0] = ((int8_t* )&val)[3]; |
dflet | 6:778b081f6a13 | 682 | p[1] = ((int8_t* )&val)[2]; |
dflet | 6:778b081f6a13 | 683 | p[2] = ((int8_t* )&val)[1]; |
dflet | 6:778b081f6a13 | 684 | p[3] = ((int8_t* )&val)[0]; |
dflet | 6:778b081f6a13 | 685 | return i; |
dflet | 6:778b081f6a13 | 686 | } else { /* big endian */ |
dflet | 6:778b081f6a13 | 687 | return val; |
dflet | 6:778b081f6a13 | 688 | } |
dflet | 6:778b081f6a13 | 689 | } |
dflet | 6:778b081f6a13 | 690 | |
dflet | 6:778b081f6a13 | 691 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 692 | /* sl_Htonl */ |
dflet | 6:778b081f6a13 | 693 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 694 | uint16_t cc3100_socket::sl_Htons( uint16_t val ) |
dflet | 6:778b081f6a13 | 695 | { |
dflet | 6:778b081f6a13 | 696 | int16_t i = 1; |
dflet | 6:778b081f6a13 | 697 | int8_t *p = (int8_t *)&i; |
dflet | 6:778b081f6a13 | 698 | if (p[0] == 1) { /* little endian */ |
dflet | 6:778b081f6a13 | 699 | p[0] = ((int8_t* )&val)[1]; |
dflet | 6:778b081f6a13 | 700 | p[1] = ((int8_t* )&val)[0]; |
dflet | 6:778b081f6a13 | 701 | return i; |
dflet | 6:778b081f6a13 | 702 | } else { /* big endian */ |
dflet | 6:778b081f6a13 | 703 | return val; |
dflet | 6:778b081f6a13 | 704 | } |
dflet | 6:778b081f6a13 | 705 | } |
dflet | 6:778b081f6a13 | 706 | |
dflet | 6:778b081f6a13 | 707 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 708 | /* sl_Recv */ |
dflet | 6:778b081f6a13 | 709 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 710 | typedef union { |
dflet | 6:778b081f6a13 | 711 | _sendRecvCommand_t Cmd; |
dflet | 6:778b081f6a13 | 712 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 713 | } _SlRecvMsg_u; |
dflet | 6:778b081f6a13 | 714 | |
dflet | 6:778b081f6a13 | 715 | #if _SL_INCLUDE_FUNC(sl_Recv) |
dflet | 6:778b081f6a13 | 716 | const _SlCmdCtrl_t _SlRecvCmdCtrl = { |
dflet | 6:778b081f6a13 | 717 | SL_OPCODE_SOCKET_RECV, |
dflet | 6:778b081f6a13 | 718 | sizeof(_sendRecvCommand_t), |
dflet | 6:778b081f6a13 | 719 | sizeof(_SocketResponse_t) |
dflet | 6:778b081f6a13 | 720 | }; |
dflet | 6:778b081f6a13 | 721 | |
dflet | 6:778b081f6a13 | 722 | int16_t cc3100_socket::sl_Recv(int16_t sd, void *pBuf, int16_t Len, int16_t flags) |
dflet | 6:778b081f6a13 | 723 | { |
dflet | 6:778b081f6a13 | 724 | _SlRecvMsg_u Msg; |
dflet | 6:778b081f6a13 | 725 | _SlCmdExt_t CmdExt; |
dflet | 6:778b081f6a13 | 726 | _SlReturnVal_t status; |
dflet | 6:778b081f6a13 | 727 | |
dflet | 6:778b081f6a13 | 728 | _driver._SlDrvResetCmdExt(&CmdExt); |
dflet | 6:778b081f6a13 | 729 | CmdExt.RxPayloadLen = Len; |
dflet | 6:778b081f6a13 | 730 | CmdExt.pRxPayload = (uint8_t *)pBuf; |
dflet | 6:778b081f6a13 | 731 | |
dflet | 6:778b081f6a13 | 732 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 733 | Msg.Cmd.StatusOrLen = Len; |
dflet | 6:778b081f6a13 | 734 | |
dflet | 6:778b081f6a13 | 735 | /* no size truncation in recv path */ |
dflet | 6:778b081f6a13 | 736 | CmdExt.RxPayloadLen = Msg.Cmd.StatusOrLen; |
dflet | 6:778b081f6a13 | 737 | |
dflet | 6:778b081f6a13 | 738 | Msg.Cmd.FamilyAndFlags = flags & 0x0F; |
dflet | 6:778b081f6a13 | 739 | |
dflet | 6:778b081f6a13 | 740 | status = _driver._SlDrvDataReadOp((_SlSd_t)sd, (_SlCmdCtrl_t *)&_SlRecvCmdCtrl, &Msg, &CmdExt); |
dflet | 6:778b081f6a13 | 741 | if( status != SL_OS_RET_CODE_OK ) { |
dflet | 6:778b081f6a13 | 742 | return status; |
dflet | 6:778b081f6a13 | 743 | } |
dflet | 6:778b081f6a13 | 744 | |
dflet | 6:778b081f6a13 | 745 | /* if the Device side sends less than expected it is not the Driver's role */ |
dflet | 6:778b081f6a13 | 746 | /* the returned value could be smaller than the requested size */ |
dflet | 6:778b081f6a13 | 747 | return (int16_t)Msg.Rsp.statusOrLen; |
dflet | 6:778b081f6a13 | 748 | } |
dflet | 6:778b081f6a13 | 749 | #endif |
dflet | 6:778b081f6a13 | 750 | |
dflet | 6:778b081f6a13 | 751 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 752 | /* sl_SetSockOpt */ |
dflet | 6:778b081f6a13 | 753 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 754 | typedef union { |
dflet | 6:778b081f6a13 | 755 | _setSockOptCommand_t Cmd; |
dflet | 6:778b081f6a13 | 756 | _SocketResponse_t Rsp; |
dflet | 6:778b081f6a13 | 757 | } _SlSetSockOptMsg_u; |
dflet | 6:778b081f6a13 | 758 | |
dflet | 6:778b081f6a13 | 759 | const _SlCmdCtrl_t _SlSetSockOptCmdCtrl = { |
dflet | 6:778b081f6a13 | 760 | SL_OPCODE_SOCKET_SETSOCKOPT, |
dflet | 6:778b081f6a13 | 761 | sizeof(_setSockOptCommand_t), |
dflet | 6:778b081f6a13 | 762 | sizeof(_SocketResponse_t) |
dflet | 6:778b081f6a13 | 763 | }; |
dflet | 6:778b081f6a13 | 764 | |
dflet | 6:778b081f6a13 | 765 | #if _SL_INCLUDE_FUNC(sl_SetSockOpt) |
dflet | 6:778b081f6a13 | 766 | int16_t cc3100_socket::sl_SetSockOpt(int16_t sd, int16_t level, int16_t optname, const void *optval, SlSocklen_t optlen) |
dflet | 6:778b081f6a13 | 767 | { |
dflet | 6:778b081f6a13 | 768 | _SlSetSockOptMsg_u Msg; |
dflet | 6:778b081f6a13 | 769 | _SlCmdExt_t CmdExt; |
dflet | 6:778b081f6a13 | 770 | |
dflet | 6:778b081f6a13 | 771 | _driver._SlDrvResetCmdExt(&CmdExt); |
dflet | 6:778b081f6a13 | 772 | CmdExt.TxPayloadLen = optlen; |
dflet | 6:778b081f6a13 | 773 | CmdExt.pTxPayload = (uint8_t *)optval; |
dflet | 6:778b081f6a13 | 774 | |
dflet | 6:778b081f6a13 | 775 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 776 | Msg.Cmd.level = (uint8_t)level; |
dflet | 6:778b081f6a13 | 777 | Msg.Cmd.optionLen = (uint8_t)optlen; |
dflet | 6:778b081f6a13 | 778 | Msg.Cmd.optionName = (uint8_t)optname; |
dflet | 6:778b081f6a13 | 779 | |
dflet | 6:778b081f6a13 | 780 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSetSockOptCmdCtrl, &Msg, &CmdExt)); |
dflet | 6:778b081f6a13 | 781 | |
dflet | 6:778b081f6a13 | 782 | return (int16_t)Msg.Rsp.statusOrLen; |
dflet | 6:778b081f6a13 | 783 | } |
dflet | 6:778b081f6a13 | 784 | #endif |
dflet | 6:778b081f6a13 | 785 | |
dflet | 6:778b081f6a13 | 786 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 787 | /* sl_GetSockOpt */ |
dflet | 6:778b081f6a13 | 788 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 789 | typedef union { |
dflet | 6:778b081f6a13 | 790 | _getSockOptCommand_t Cmd; |
dflet | 6:778b081f6a13 | 791 | _getSockOptResponse_t Rsp; |
dflet | 6:778b081f6a13 | 792 | } _SlGetSockOptMsg_u; |
dflet | 6:778b081f6a13 | 793 | |
dflet | 6:778b081f6a13 | 794 | #if _SL_INCLUDE_FUNC(sl_GetSockOpt) |
dflet | 6:778b081f6a13 | 795 | const _SlCmdCtrl_t _SlGetSockOptCmdCtrl = { |
dflet | 6:778b081f6a13 | 796 | SL_OPCODE_SOCKET_GETSOCKOPT, |
dflet | 6:778b081f6a13 | 797 | sizeof(_getSockOptCommand_t), |
dflet | 6:778b081f6a13 | 798 | sizeof(_getSockOptResponse_t) |
dflet | 6:778b081f6a13 | 799 | }; |
dflet | 6:778b081f6a13 | 800 | |
dflet | 6:778b081f6a13 | 801 | int16_t cc3100_socket::sl_GetSockOpt(int16_t sd, int16_t level, int16_t optname, void *optval, SlSocklen_t *optlen) |
dflet | 6:778b081f6a13 | 802 | { |
dflet | 6:778b081f6a13 | 803 | _SlGetSockOptMsg_u Msg; |
dflet | 6:778b081f6a13 | 804 | _SlCmdExt_t CmdExt; |
dflet | 6:778b081f6a13 | 805 | |
dflet | 6:778b081f6a13 | 806 | if (*optlen == 0) { |
dflet | 6:778b081f6a13 | 807 | return SL_EZEROLEN; |
dflet | 6:778b081f6a13 | 808 | } |
dflet | 6:778b081f6a13 | 809 | |
dflet | 6:778b081f6a13 | 810 | _driver._SlDrvResetCmdExt(&CmdExt); |
dflet | 6:778b081f6a13 | 811 | CmdExt.RxPayloadLen = *optlen; |
dflet | 6:778b081f6a13 | 812 | CmdExt.pRxPayload = (uint8_t*)optval; |
dflet | 6:778b081f6a13 | 813 | |
dflet | 6:778b081f6a13 | 814 | Msg.Cmd.sd = (uint8_t)sd; |
dflet | 6:778b081f6a13 | 815 | Msg.Cmd.level = (uint8_t)level; |
dflet | 6:778b081f6a13 | 816 | Msg.Cmd.optionLen = (uint8_t)(*optlen); |
dflet | 6:778b081f6a13 | 817 | Msg.Cmd.optionName = (uint8_t)optname; |
dflet | 6:778b081f6a13 | 818 | |
dflet | 6:778b081f6a13 | 819 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlGetSockOptCmdCtrl, &Msg, &CmdExt)); |
dflet | 6:778b081f6a13 | 820 | |
dflet | 6:778b081f6a13 | 821 | if (CmdExt.RxPayloadLen < CmdExt.ActualRxPayloadLen) { |
dflet | 6:778b081f6a13 | 822 | *optlen = Msg.Rsp.optionLen; |
dflet | 6:778b081f6a13 | 823 | return SL_ESMALLBUF; |
dflet | 6:778b081f6a13 | 824 | } else { |
dflet | 6:778b081f6a13 | 825 | *optlen = (uint8_t)CmdExt.ActualRxPayloadLen; |
dflet | 6:778b081f6a13 | 826 | } |
dflet | 6:778b081f6a13 | 827 | return (int16_t)Msg.Rsp.status; |
dflet | 6:778b081f6a13 | 828 | } |
dflet | 6:778b081f6a13 | 829 | #endif |
dflet | 6:778b081f6a13 | 830 | |
dflet | 6:778b081f6a13 | 831 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 832 | /* sl_Select */ |
dflet | 6:778b081f6a13 | 833 | /* ******************************************************************************/ |
dflet | 6:778b081f6a13 | 834 | typedef union { |
dflet | 6:778b081f6a13 | 835 | _SelectCommand_t Cmd; |
dflet | 6:778b081f6a13 | 836 | _BasicResponse_t Rsp; |
dflet | 6:778b081f6a13 | 837 | } _SlSelectMsg_u; |
dflet | 6:778b081f6a13 | 838 | |
dflet | 6:778b081f6a13 | 839 | #ifndef SL_TINY_EXT |
dflet | 6:778b081f6a13 | 840 | #if _SL_INCLUDE_FUNC(sl_Select) |
dflet | 6:778b081f6a13 | 841 | const _SlCmdCtrl_t _SlSelectCmdCtrl = { |
dflet | 6:778b081f6a13 | 842 | SL_OPCODE_SOCKET_SELECT, |
dflet | 6:778b081f6a13 | 843 | sizeof(_SelectCommand_t), |
dflet | 6:778b081f6a13 | 844 | sizeof(_BasicResponse_t) |
dflet | 6:778b081f6a13 | 845 | }; |
dflet | 6:778b081f6a13 | 846 | |
dflet | 6:778b081f6a13 | 847 | int16_t cc3100_socket::sl_Select(int16_t nfds, SlFdSet_t *readsds, SlFdSet_t *writesds, SlFdSet_t *exceptsds, SlTimeval_t *timeout) |
dflet | 6:778b081f6a13 | 848 | { |
dflet | 6:778b081f6a13 | 849 | _SlSelectMsg_u Msg; |
dflet | 6:778b081f6a13 | 850 | _SelectAsyncResponse_t AsyncRsp; |
dflet | 6:778b081f6a13 | 851 | uint8_t ObjIdx = MAX_CONCURRENT_ACTIONS; |
dflet | 6:778b081f6a13 | 852 | |
dflet | 6:778b081f6a13 | 853 | Msg.Cmd.nfds = (uint8_t)nfds; |
dflet | 6:778b081f6a13 | 854 | Msg.Cmd.readFdsCount = 0; |
dflet | 6:778b081f6a13 | 855 | Msg.Cmd.writeFdsCount = 0; |
dflet | 6:778b081f6a13 | 856 | |
dflet | 6:778b081f6a13 | 857 | Msg.Cmd.readFds = 0; |
dflet | 6:778b081f6a13 | 858 | Msg.Cmd.writeFds = 0; |
dflet | 6:778b081f6a13 | 859 | |
dflet | 6:778b081f6a13 | 860 | if( readsds ) { |
dflet | 6:778b081f6a13 | 861 | Msg.Cmd.readFds = (uint16_t)readsds->fd_array[0]; |
dflet | 6:778b081f6a13 | 862 | } |
dflet | 6:778b081f6a13 | 863 | if( writesds ) { |
dflet | 6:778b081f6a13 | 864 | Msg.Cmd.writeFds = (uint16_t)writesds->fd_array[0]; |
dflet | 6:778b081f6a13 | 865 | } |
dflet | 6:778b081f6a13 | 866 | if( NULL == timeout ) { |
dflet | 6:778b081f6a13 | 867 | Msg.Cmd.tv_sec = 0xffff; |
dflet | 6:778b081f6a13 | 868 | Msg.Cmd.tv_usec = 0xffff; |
dflet | 6:778b081f6a13 | 869 | } else { |
dflet | 6:778b081f6a13 | 870 | if( 0xffff <= timeout->tv_sec ) { |
dflet | 6:778b081f6a13 | 871 | Msg.Cmd.tv_sec = 0xffff; |
dflet | 6:778b081f6a13 | 872 | } else { |
dflet | 6:778b081f6a13 | 873 | Msg.Cmd.tv_sec = (uint16_t)timeout->tv_sec; |
dflet | 6:778b081f6a13 | 874 | } |
dflet | 6:778b081f6a13 | 875 | timeout->tv_usec = timeout->tv_usec >> 10; /* convert to milliseconds */ |
dflet | 6:778b081f6a13 | 876 | if( 0xffff <= timeout->tv_usec ) { |
dflet | 6:778b081f6a13 | 877 | Msg.Cmd.tv_usec = 0xffff; |
dflet | 6:778b081f6a13 | 878 | } else { |
dflet | 6:778b081f6a13 | 879 | Msg.Cmd.tv_usec = (uint16_t)timeout->tv_usec; |
dflet | 6:778b081f6a13 | 880 | } |
dflet | 6:778b081f6a13 | 881 | } |
dflet | 6:778b081f6a13 | 882 | |
dflet | 6:778b081f6a13 | 883 | /* Use Obj to issue the command, if not available try later */ |
dflet | 6:778b081f6a13 | 884 | ObjIdx = _driver._SlDrvProtectAsyncRespSetting((uint8_t*)&AsyncRsp, SELECT_ID, SL_MAX_SOCKETS); |
dflet | 6:778b081f6a13 | 885 | |
dflet | 6:778b081f6a13 | 886 | if (MAX_CONCURRENT_ACTIONS == ObjIdx) |
dflet | 6:778b081f6a13 | 887 | { |
dflet | 6:778b081f6a13 | 888 | return SL_POOL_IS_EMPTY; |
dflet | 6:778b081f6a13 | 889 | } |
dflet | 6:778b081f6a13 | 890 | |
dflet | 6:778b081f6a13 | 891 | /* send the command */ |
dflet | 6:778b081f6a13 | 892 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSelectCmdCtrl, &Msg, NULL)); |
dflet | 6:778b081f6a13 | 893 | |
dflet | 6:778b081f6a13 | 894 | if(SL_OS_RET_CODE_OK == (int16_t)Msg.Rsp.status) { |
dflet | 6:778b081f6a13 | 895 | _driver._SlDrvSyncObjWaitForever(&g_pCB->ObjPool[ObjIdx].SyncObj); |
dflet | 6:778b081f6a13 | 896 | Msg.Rsp.status = AsyncRsp.status; |
dflet | 6:778b081f6a13 | 897 | |
dflet | 6:778b081f6a13 | 898 | if( ((int16_t)Msg.Rsp.status) >= 0 ) { |
dflet | 6:778b081f6a13 | 899 | if( readsds ) { |
dflet | 6:778b081f6a13 | 900 | readsds->fd_array[0] = AsyncRsp.readFds; |
dflet | 6:778b081f6a13 | 901 | } |
dflet | 6:778b081f6a13 | 902 | if( writesds ) { |
dflet | 6:778b081f6a13 | 903 | writesds->fd_array[0] = AsyncRsp.writeFds; |
dflet | 6:778b081f6a13 | 904 | } |
dflet | 6:778b081f6a13 | 905 | } |
dflet | 6:778b081f6a13 | 906 | } |
dflet | 6:778b081f6a13 | 907 | |
dflet | 6:778b081f6a13 | 908 | _driver._SlDrvReleasePoolObj(ObjIdx); |
dflet | 6:778b081f6a13 | 909 | return (int16_t)Msg.Rsp.status; |
dflet | 6:778b081f6a13 | 910 | } |
dflet | 6:778b081f6a13 | 911 | |
dflet | 6:778b081f6a13 | 912 | /* Select helper functions */ |
dflet | 6:778b081f6a13 | 913 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 914 | /* SL_FD_SET */ |
dflet | 6:778b081f6a13 | 915 | /* ******************************************************************************/ |
dflet | 6:778b081f6a13 | 916 | void cc3100_socket::SL_FD_SET(int16_t fd, SlFdSet_t *fdset) |
dflet | 6:778b081f6a13 | 917 | { |
dflet | 6:778b081f6a13 | 918 | fdset->fd_array[0] |= (1<< (fd & BSD_SOCKET_ID_MASK)); |
dflet | 6:778b081f6a13 | 919 | } |
dflet | 6:778b081f6a13 | 920 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 921 | /* SL_FD_CLR */ |
dflet | 6:778b081f6a13 | 922 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 923 | void cc3100_socket::SL_FD_CLR(int16_t fd, SlFdSet_t *fdset) |
dflet | 6:778b081f6a13 | 924 | { |
dflet | 6:778b081f6a13 | 925 | fdset->fd_array[0] &= ~(1<< (fd & BSD_SOCKET_ID_MASK)); |
dflet | 6:778b081f6a13 | 926 | } |
dflet | 6:778b081f6a13 | 927 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 928 | /* SL_FD_ISSET */ |
dflet | 6:778b081f6a13 | 929 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 930 | int16_t cc3100_socket::SL_FD_ISSET(int16_t fd, SlFdSet_t *fdset) |
dflet | 6:778b081f6a13 | 931 | { |
dflet | 6:778b081f6a13 | 932 | if( fdset->fd_array[0] & (1<< (fd & BSD_SOCKET_ID_MASK)) ) { |
dflet | 6:778b081f6a13 | 933 | return 1; |
dflet | 6:778b081f6a13 | 934 | } |
dflet | 6:778b081f6a13 | 935 | return 0; |
dflet | 6:778b081f6a13 | 936 | } |
dflet | 6:778b081f6a13 | 937 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 938 | /* SL_FD_ZERO */ |
dflet | 6:778b081f6a13 | 939 | /*******************************************************************************/ |
dflet | 6:778b081f6a13 | 940 | void cc3100_socket::SL_FD_ZERO(SlFdSet_t *fdset) |
dflet | 6:778b081f6a13 | 941 | { |
dflet | 6:778b081f6a13 | 942 | fdset->fd_array[0] = 0; |
dflet | 6:778b081f6a13 | 943 | } |
dflet | 6:778b081f6a13 | 944 | |
dflet | 6:778b081f6a13 | 945 | #endif |
dflet | 6:778b081f6a13 | 946 | #endif |
dflet | 6:778b081f6a13 | 947 | |
dflet | 6:778b081f6a13 | 948 | }//namespace mbed_cc3100 |
dflet | 6:778b081f6a13 | 949 | |
dflet | 6:778b081f6a13 | 950 | |
dflet | 6:778b081f6a13 | 951 |