TI's CC3100. A test demo with very little testing done!

Dependencies:   mbed

Fork of cc3100_Test_Demo2 by 高 杨

Committer:
dflet
Date:
Tue Feb 10 12:09:29 2015 +0000
Revision:
0:e89ba455dbcf
For test only! Not much has been tested, but the 2 demo apps run ok. Alot more work needs to be done!

Who changed what in which revision?

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