DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

The MAXREFDES155# is an internet-of-things (IoT) embedded-security reference design, built to authenticate and control a sensing node using elliptic-curve-based public-key cryptography with control and notification from a web server.

The hardware includes an ARM® mbed™ shield and attached sensor endpoint. The shield contains a DS2476 DeepCover® ECDSA/SHA-2 coprocessor, Wifi communication, LCD push-button controls, and status LEDs. The sensor endpoint is attached to the shield using a 300mm cable and contains a DS28C36 DeepCover ECDSA/SHA-2 authenticator, IR-thermal sensor, and aiming laser for the IR sensor. The MAXREFDES155# is equipped with a standard Arduino® form-factor shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The combination of these two devices represent an IoT device. Communication to the web server is accomplished with the shield Wifi circuitry. Communication from the shield to the attached sensor module is accomplished over I2C . The sensor module represents an IoT endpoint that generates small data with a requirement for message authenticity/integrity and secure on/off operational control.

The design is hierarchical with each mbed platform and shield communicating data from the sensor node to a web server that maintains a centralized log and dispatches notifications as necessary. The simplicity of this design enables rapid integration into any star-topology IoT network to provide security with the low overhead and cost provided by the ECDSA-P256 asymmetric-key and SHA-256 symmetric-key algorithms.

More information about the MAXREFDES155# is available on the Maxim Integrated website.

Committer:
IanBenzMaxim
Date:
Thu Oct 03 11:40:13 2019 -0500
Revision:
16:a004191a79ab
Parent:
simplelink/source/socket.c@0:33d4e66780c0
Updated MaximInterface to version 2.0. Updated mbed-os to version 5.5.7. Cleaned up code styling.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:33d4e66780c0 1 /*
IanBenzMaxim 0:33d4e66780c0 2 * socket.c - CC31xx/CC32xx Host Driver Implementation
IanBenzMaxim 0:33d4e66780c0 3 *
IanBenzMaxim 0:33d4e66780c0 4 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
IanBenzMaxim 0:33d4e66780c0 5 *
IanBenzMaxim 0:33d4e66780c0 6 *
IanBenzMaxim 0:33d4e66780c0 7 * Redistribution and use in source and binary forms, with or without
IanBenzMaxim 0:33d4e66780c0 8 * modification, are permitted provided that the following conditions
IanBenzMaxim 0:33d4e66780c0 9 * are met:
IanBenzMaxim 0:33d4e66780c0 10 *
IanBenzMaxim 0:33d4e66780c0 11 * Redistributions of source code must retain the above copyright
IanBenzMaxim 0:33d4e66780c0 12 * notice, this list of conditions and the following disclaimer.
IanBenzMaxim 0:33d4e66780c0 13 *
IanBenzMaxim 0:33d4e66780c0 14 * Redistributions in binary form must reproduce the above copyright
IanBenzMaxim 0:33d4e66780c0 15 * notice, this list of conditions and the following disclaimer in the
IanBenzMaxim 0:33d4e66780c0 16 * documentation and/or other materials provided with the
IanBenzMaxim 0:33d4e66780c0 17 * distribution.
IanBenzMaxim 0:33d4e66780c0 18 *
IanBenzMaxim 0:33d4e66780c0 19 * Neither the name of Texas Instruments Incorporated nor the names of
IanBenzMaxim 0:33d4e66780c0 20 * its contributors may be used to endorse or promote products derived
IanBenzMaxim 0:33d4e66780c0 21 * from this software without specific prior written permission.
IanBenzMaxim 0:33d4e66780c0 22 *
IanBenzMaxim 0:33d4e66780c0 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
IanBenzMaxim 0:33d4e66780c0 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
IanBenzMaxim 0:33d4e66780c0 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
IanBenzMaxim 0:33d4e66780c0 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
IanBenzMaxim 0:33d4e66780c0 27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
IanBenzMaxim 0:33d4e66780c0 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
IanBenzMaxim 0:33d4e66780c0 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
IanBenzMaxim 0:33d4e66780c0 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
IanBenzMaxim 0:33d4e66780c0 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
IanBenzMaxim 0:33d4e66780c0 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
IanBenzMaxim 0:33d4e66780c0 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IanBenzMaxim 0:33d4e66780c0 34 *
IanBenzMaxim 0:33d4e66780c0 35 */
IanBenzMaxim 0:33d4e66780c0 36
IanBenzMaxim 0:33d4e66780c0 37
IanBenzMaxim 0:33d4e66780c0 38
IanBenzMaxim 0:33d4e66780c0 39
IanBenzMaxim 0:33d4e66780c0 40 /*****************************************************************************/
IanBenzMaxim 0:33d4e66780c0 41 /* Include files */
IanBenzMaxim 0:33d4e66780c0 42 /*****************************************************************************/
IanBenzMaxim 0:33d4e66780c0 43 #include "simplelink.h"
IanBenzMaxim 0:33d4e66780c0 44 #include "protocol.h"
IanBenzMaxim 0:33d4e66780c0 45 #include "driver.h"
IanBenzMaxim 0:33d4e66780c0 46
IanBenzMaxim 0:33d4e66780c0 47
IanBenzMaxim 0:33d4e66780c0 48 static void _sl_BuildAddress(const SlSockAddr_t *addr, _SocketAddrCommand_u *pCmd);
IanBenzMaxim 0:33d4e66780c0 49 _SlReturnVal_t _sl_HandleAsync_Connect(void *pVoidBuf);
IanBenzMaxim 0:33d4e66780c0 50
IanBenzMaxim 0:33d4e66780c0 51 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 52 void _sl_ParseAddress(_SocketAddrResponse_u *pRsp, SlSockAddr_t *addr, SlSocklen_t *addrlen);
IanBenzMaxim 0:33d4e66780c0 53 _SlReturnVal_t _sl_HandleAsync_Accept(void *pVoidBuf);
IanBenzMaxim 0:33d4e66780c0 54 _SlReturnVal_t _sl_HandleAsync_Select(void *pVoidBuf);
IanBenzMaxim 0:33d4e66780c0 55 #endif
IanBenzMaxim 0:33d4e66780c0 56 static _u16 _sl_TruncatePayloadByProtocol(const _i16 pSd, const _u16 length);
IanBenzMaxim 0:33d4e66780c0 57
IanBenzMaxim 0:33d4e66780c0 58 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 59 /* Functions */
IanBenzMaxim 0:33d4e66780c0 60 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 61
IanBenzMaxim 0:33d4e66780c0 62
IanBenzMaxim 0:33d4e66780c0 63 /* Note: parsing of family and port in the generic way for all IPV4, IPV6 and EUI48 */
IanBenzMaxim 0:33d4e66780c0 64 /* is possible as _i32 as these parameters are in the same offset and size for these */
IanBenzMaxim 0:33d4e66780c0 65 /* three families. */
IanBenzMaxim 0:33d4e66780c0 66 #define SL_SOCKET_PAYLOAD_BASE (1350)
IanBenzMaxim 0:33d4e66780c0 67
IanBenzMaxim 0:33d4e66780c0 68 static const _u8 _SlPayloadByProtocolLUT[16] =
IanBenzMaxim 0:33d4e66780c0 69 {
IanBenzMaxim 0:33d4e66780c0 70 (1472 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV4 */
IanBenzMaxim 0:33d4e66780c0 71 (1460 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV4 */
IanBenzMaxim 0:33d4e66780c0 72 (1452 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV6 */
IanBenzMaxim 0:33d4e66780c0 73 (1440 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV6 */
IanBenzMaxim 0:33d4e66780c0 74 (1386 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV4_SECURE */
IanBenzMaxim 0:33d4e66780c0 75 (1386 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV4_SECURE */
IanBenzMaxim 0:33d4e66780c0 76 (1396 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_UDP_IPV6_SECURE */
IanBenzMaxim 0:33d4e66780c0 77 (1396 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_TCP_IPV6_SECURE */
IanBenzMaxim 0:33d4e66780c0 78 (1476 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_TRANCEIVER */
IanBenzMaxim 0:33d4e66780c0 79 (1514 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_PACKET */
IanBenzMaxim 0:33d4e66780c0 80 (1480 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_IP4 */
IanBenzMaxim 0:33d4e66780c0 81 (1480 - SL_SOCKET_PAYLOAD_BASE), /* SL_SOCKET_PAYLOAD_TYPE_RAW_IP6 */
IanBenzMaxim 0:33d4e66780c0 82 (1440 - SL_SOCKET_PAYLOAD_BASE), /* Default */
IanBenzMaxim 0:33d4e66780c0 83 (1440 - SL_SOCKET_PAYLOAD_BASE), /* Default */
IanBenzMaxim 0:33d4e66780c0 84 (1440 - SL_SOCKET_PAYLOAD_BASE), /* Default */
IanBenzMaxim 0:33d4e66780c0 85 (1440 - SL_SOCKET_PAYLOAD_BASE) /* Default */
IanBenzMaxim 0:33d4e66780c0 86 };
IanBenzMaxim 0:33d4e66780c0 87
IanBenzMaxim 0:33d4e66780c0 88
IanBenzMaxim 0:33d4e66780c0 89
IanBenzMaxim 0:33d4e66780c0 90 /* ******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 91 /* _sl_BuildAddress */
IanBenzMaxim 0:33d4e66780c0 92 /* ******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 93 static void _sl_BuildAddress(const SlSockAddr_t *addr, _SocketAddrCommand_u *pCmd)
IanBenzMaxim 0:33d4e66780c0 94 {
IanBenzMaxim 0:33d4e66780c0 95
IanBenzMaxim 0:33d4e66780c0 96 /* Note: parsing of family and port in the generic way for all IPV4, IPV6 and EUI48
IanBenzMaxim 0:33d4e66780c0 97 is possible as long as these parameters are in the same offset and size for these
IanBenzMaxim 0:33d4e66780c0 98 three families. */
IanBenzMaxim 0:33d4e66780c0 99 pCmd->IpV4.FamilyAndFlags = (_u8)((addr->sa_family << 4) & 0xF0);
IanBenzMaxim 0:33d4e66780c0 100 pCmd->IpV4.port = ((SlSockAddrIn_t *)addr)->sin_port;
IanBenzMaxim 0:33d4e66780c0 101
IanBenzMaxim 0:33d4e66780c0 102 if(SL_AF_INET == addr->sa_family)
IanBenzMaxim 0:33d4e66780c0 103 {
IanBenzMaxim 0:33d4e66780c0 104 pCmd->IpV4.address = ((SlSockAddrIn_t *)addr)->sin_addr.s_addr;
IanBenzMaxim 0:33d4e66780c0 105 }
IanBenzMaxim 0:33d4e66780c0 106 else if (SL_AF_INET6_EUI_48 == addr->sa_family )
IanBenzMaxim 0:33d4e66780c0 107 {
IanBenzMaxim 0:33d4e66780c0 108 sl_Memcpy( pCmd->IpV6EUI48.address,((SlSockAddrIn6_t *)addr)->sin6_addr._S6_un._S6_u8, 6);
IanBenzMaxim 0:33d4e66780c0 109 }
IanBenzMaxim 0:33d4e66780c0 110 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 111 else
IanBenzMaxim 0:33d4e66780c0 112 {
IanBenzMaxim 0:33d4e66780c0 113 sl_Memcpy(pCmd->IpV6.address, ((sockaddr_in6 *)addr)->sin6_addr._S6_un._S6_u32, 16 );
IanBenzMaxim 0:33d4e66780c0 114 }
IanBenzMaxim 0:33d4e66780c0 115 #endif
IanBenzMaxim 0:33d4e66780c0 116 }
IanBenzMaxim 0:33d4e66780c0 117
IanBenzMaxim 0:33d4e66780c0 118
IanBenzMaxim 0:33d4e66780c0 119 /*****************************************************************************
IanBenzMaxim 0:33d4e66780c0 120 _sl_TruncatePayloadByProtocol
IanBenzMaxim 0:33d4e66780c0 121 *****************************************************************************/
IanBenzMaxim 0:33d4e66780c0 122 static _u16 _sl_TruncatePayloadByProtocol(const _i16 sd, const _u16 length)
IanBenzMaxim 0:33d4e66780c0 123 {
IanBenzMaxim 0:33d4e66780c0 124 _u16 maxLength;
IanBenzMaxim 0:33d4e66780c0 125
IanBenzMaxim 0:33d4e66780c0 126
IanBenzMaxim 0:33d4e66780c0 127 maxLength = (_u16)(SL_SOCKET_PAYLOAD_BASE + _SlPayloadByProtocolLUT[((sd & SL_SOCKET_PAYLOAD_TYPE_MASK) >> 4)]);
IanBenzMaxim 0:33d4e66780c0 128
IanBenzMaxim 0:33d4e66780c0 129
IanBenzMaxim 0:33d4e66780c0 130
IanBenzMaxim 0:33d4e66780c0 131 if( length > maxLength )
IanBenzMaxim 0:33d4e66780c0 132 {
IanBenzMaxim 0:33d4e66780c0 133 return maxLength;
IanBenzMaxim 0:33d4e66780c0 134 }
IanBenzMaxim 0:33d4e66780c0 135 else
IanBenzMaxim 0:33d4e66780c0 136 {
IanBenzMaxim 0:33d4e66780c0 137 return length;
IanBenzMaxim 0:33d4e66780c0 138 }
IanBenzMaxim 0:33d4e66780c0 139 }
IanBenzMaxim 0:33d4e66780c0 140
IanBenzMaxim 0:33d4e66780c0 141 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 142 /* _sl_ParseAddress */
IanBenzMaxim 0:33d4e66780c0 143 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 144
IanBenzMaxim 0:33d4e66780c0 145 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 146 void _sl_ParseAddress(_SocketAddrResponse_u *pRsp, SlSockAddr_t *addr, SlSocklen_t *addrlen)
IanBenzMaxim 0:33d4e66780c0 147 {
IanBenzMaxim 0:33d4e66780c0 148 /* Note: parsing of family and port in the generic way for all IPV4, IPV6 and EUI48 */
IanBenzMaxim 0:33d4e66780c0 149 /* is possible as long as these parameters are in the same offset and size for these */
IanBenzMaxim 0:33d4e66780c0 150 /* three families. */
IanBenzMaxim 0:33d4e66780c0 151 addr->sa_family = pRsp->IpV4.family;
IanBenzMaxim 0:33d4e66780c0 152 ((SlSockAddrIn_t *)addr)->sin_port = pRsp->IpV4.port;
IanBenzMaxim 0:33d4e66780c0 153
IanBenzMaxim 0:33d4e66780c0 154 *addrlen = (SlSocklen_t)((SL_AF_INET == addr->sa_family) ? sizeof(SlSockAddrIn_t) : sizeof(SlSockAddrIn6_t));
IanBenzMaxim 0:33d4e66780c0 155
IanBenzMaxim 0:33d4e66780c0 156 if(SL_AF_INET == addr->sa_family)
IanBenzMaxim 0:33d4e66780c0 157 {
IanBenzMaxim 0:33d4e66780c0 158 ((SlSockAddrIn_t *)addr)->sin_addr.s_addr = pRsp->IpV4.address;
IanBenzMaxim 0:33d4e66780c0 159 }
IanBenzMaxim 0:33d4e66780c0 160 else if (SL_AF_INET6_EUI_48 == addr->sa_family )
IanBenzMaxim 0:33d4e66780c0 161 {
IanBenzMaxim 0:33d4e66780c0 162 sl_Memcpy(((SlSockAddrIn6_t *)addr)->sin6_addr._S6_un._S6_u8, pRsp->IpV6EUI48.address, 6);
IanBenzMaxim 0:33d4e66780c0 163 }
IanBenzMaxim 0:33d4e66780c0 164 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 165 else
IanBenzMaxim 0:33d4e66780c0 166 {
IanBenzMaxim 0:33d4e66780c0 167 sl_Memcpy(((sockaddr_in6 *)addr)->sin6_addr._S6_un._S6_u32, pRsp->IpV6.address, 16);
IanBenzMaxim 0:33d4e66780c0 168 }
IanBenzMaxim 0:33d4e66780c0 169 #endif
IanBenzMaxim 0:33d4e66780c0 170 }
IanBenzMaxim 0:33d4e66780c0 171
IanBenzMaxim 0:33d4e66780c0 172 #endif
IanBenzMaxim 0:33d4e66780c0 173
IanBenzMaxim 0:33d4e66780c0 174 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 175 /* sl_Socket */
IanBenzMaxim 0:33d4e66780c0 176 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 177 typedef union
IanBenzMaxim 0:33d4e66780c0 178 {
IanBenzMaxim 0:33d4e66780c0 179 _u32 Dummy;
IanBenzMaxim 0:33d4e66780c0 180 _SocketCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 181 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 182 }_SlSockSocketMsg_u;
IanBenzMaxim 0:33d4e66780c0 183
IanBenzMaxim 0:33d4e66780c0 184
IanBenzMaxim 0:33d4e66780c0 185
IanBenzMaxim 0:33d4e66780c0 186 #if _SL_INCLUDE_FUNC(sl_Socket)
IanBenzMaxim 0:33d4e66780c0 187
IanBenzMaxim 0:33d4e66780c0 188 static const _SlCmdCtrl_t _SlSockSocketCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 189 {
IanBenzMaxim 0:33d4e66780c0 190 SL_OPCODE_SOCKET_SOCKET,
IanBenzMaxim 0:33d4e66780c0 191 (_SlArgSize_t)sizeof(_SocketCommand_t),
IanBenzMaxim 0:33d4e66780c0 192 (_SlArgSize_t)sizeof(_SocketResponse_t)
IanBenzMaxim 0:33d4e66780c0 193 };
IanBenzMaxim 0:33d4e66780c0 194
IanBenzMaxim 0:33d4e66780c0 195 _i16 sl_Socket(_i16 Domain, _i16 Type, _i16 Protocol)
IanBenzMaxim 0:33d4e66780c0 196 {
IanBenzMaxim 0:33d4e66780c0 197 _SlSockSocketMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 198
IanBenzMaxim 0:33d4e66780c0 199 Msg.Cmd.Domain = (_u8)Domain;
IanBenzMaxim 0:33d4e66780c0 200 Msg.Cmd.Type = (_u8)Type;
IanBenzMaxim 0:33d4e66780c0 201 Msg.Cmd.Protocol = (_u8)Protocol;
IanBenzMaxim 0:33d4e66780c0 202
IanBenzMaxim 0:33d4e66780c0 203 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 204 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 205 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 206
IanBenzMaxim 0:33d4e66780c0 207 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSockSocketCmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 208
IanBenzMaxim 0:33d4e66780c0 209 if( Msg.Rsp.statusOrLen < 0 )
IanBenzMaxim 0:33d4e66780c0 210 {
IanBenzMaxim 0:33d4e66780c0 211 return( Msg.Rsp.statusOrLen );
IanBenzMaxim 0:33d4e66780c0 212 }
IanBenzMaxim 0:33d4e66780c0 213 else
IanBenzMaxim 0:33d4e66780c0 214 {
IanBenzMaxim 0:33d4e66780c0 215 return (_i16)((_u8)Msg.Rsp.sd);
IanBenzMaxim 0:33d4e66780c0 216 }
IanBenzMaxim 0:33d4e66780c0 217 }
IanBenzMaxim 0:33d4e66780c0 218 #endif
IanBenzMaxim 0:33d4e66780c0 219
IanBenzMaxim 0:33d4e66780c0 220 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 221 /* sl_Close */
IanBenzMaxim 0:33d4e66780c0 222 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 223 typedef union
IanBenzMaxim 0:33d4e66780c0 224 {
IanBenzMaxim 0:33d4e66780c0 225 _CloseCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 226 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 227 }_SlSockCloseMsg_u;
IanBenzMaxim 0:33d4e66780c0 228
IanBenzMaxim 0:33d4e66780c0 229
IanBenzMaxim 0:33d4e66780c0 230 #if _SL_INCLUDE_FUNC(sl_Close)
IanBenzMaxim 0:33d4e66780c0 231
IanBenzMaxim 0:33d4e66780c0 232 static const _SlCmdCtrl_t _SlSockCloseCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 233 {
IanBenzMaxim 0:33d4e66780c0 234 SL_OPCODE_SOCKET_CLOSE,
IanBenzMaxim 0:33d4e66780c0 235 (_SlArgSize_t)sizeof(_CloseCommand_t),
IanBenzMaxim 0:33d4e66780c0 236 (_SlArgSize_t)sizeof(_SocketResponse_t)
IanBenzMaxim 0:33d4e66780c0 237 };
IanBenzMaxim 0:33d4e66780c0 238
IanBenzMaxim 0:33d4e66780c0 239 _i16 sl_Close(_i16 sd)
IanBenzMaxim 0:33d4e66780c0 240 {
IanBenzMaxim 0:33d4e66780c0 241 _SlSockCloseMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 242
IanBenzMaxim 0:33d4e66780c0 243 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 244 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 245 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 246
IanBenzMaxim 0:33d4e66780c0 247 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 248
IanBenzMaxim 0:33d4e66780c0 249 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSockCloseCmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 250
IanBenzMaxim 0:33d4e66780c0 251 return Msg.Rsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 252 }
IanBenzMaxim 0:33d4e66780c0 253 #endif
IanBenzMaxim 0:33d4e66780c0 254
IanBenzMaxim 0:33d4e66780c0 255 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 256 /* sl_Bind */
IanBenzMaxim 0:33d4e66780c0 257 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 258 typedef union
IanBenzMaxim 0:33d4e66780c0 259 {
IanBenzMaxim 0:33d4e66780c0 260 _SocketAddrCommand_u Cmd;
IanBenzMaxim 0:33d4e66780c0 261 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 262 }_SlSockBindMsg_u;
IanBenzMaxim 0:33d4e66780c0 263
IanBenzMaxim 0:33d4e66780c0 264 #if _SL_INCLUDE_FUNC(sl_Bind)
IanBenzMaxim 0:33d4e66780c0 265 _i16 sl_Bind(_i16 sd, const SlSockAddr_t *addr, _i16 addrlen)
IanBenzMaxim 0:33d4e66780c0 266 {
IanBenzMaxim 0:33d4e66780c0 267 _SlSockBindMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 268 _SlCmdCtrl_t CmdCtrl = {0, 0, (_SlArgSize_t)sizeof(_SocketResponse_t)};
IanBenzMaxim 0:33d4e66780c0 269
IanBenzMaxim 0:33d4e66780c0 270 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 271 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 272 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 273
IanBenzMaxim 0:33d4e66780c0 274 switch(addr->sa_family)
IanBenzMaxim 0:33d4e66780c0 275 {
IanBenzMaxim 0:33d4e66780c0 276 case SL_AF_INET :
IanBenzMaxim 0:33d4e66780c0 277 CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND;
IanBenzMaxim 0:33d4e66780c0 278 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv4Command_t);
IanBenzMaxim 0:33d4e66780c0 279 break;
IanBenzMaxim 0:33d4e66780c0 280 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 281 case SL_AF_INET6_EUI_48:
IanBenzMaxim 0:33d4e66780c0 282 CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND_V6;
IanBenzMaxim 0:33d4e66780c0 283 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv6EUI48Command_t);
IanBenzMaxim 0:33d4e66780c0 284 break;
IanBenzMaxim 0:33d4e66780c0 285
IanBenzMaxim 0:33d4e66780c0 286 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 287 case AF_INET6:
IanBenzMaxim 0:33d4e66780c0 288 CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND_V6;
IanBenzMaxim 0:33d4e66780c0 289 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv6Command_t);
IanBenzMaxim 0:33d4e66780c0 290 break;
IanBenzMaxim 0:33d4e66780c0 291 #endif
IanBenzMaxim 0:33d4e66780c0 292 #endif
IanBenzMaxim 0:33d4e66780c0 293
IanBenzMaxim 0:33d4e66780c0 294
IanBenzMaxim 0:33d4e66780c0 295 case SL_AF_RF :
IanBenzMaxim 0:33d4e66780c0 296 default:
IanBenzMaxim 0:33d4e66780c0 297 return SL_RET_CODE_INVALID_INPUT;
IanBenzMaxim 0:33d4e66780c0 298 }
IanBenzMaxim 0:33d4e66780c0 299
IanBenzMaxim 0:33d4e66780c0 300 Msg.Cmd.IpV4.lenOrPadding = 0;
IanBenzMaxim 0:33d4e66780c0 301 Msg.Cmd.IpV4.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 302
IanBenzMaxim 0:33d4e66780c0 303 _sl_BuildAddress(addr, &Msg.Cmd);
IanBenzMaxim 0:33d4e66780c0 304
IanBenzMaxim 0:33d4e66780c0 305 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&CmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 306
IanBenzMaxim 0:33d4e66780c0 307 return Msg.Rsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 308 }
IanBenzMaxim 0:33d4e66780c0 309 #endif
IanBenzMaxim 0:33d4e66780c0 310
IanBenzMaxim 0:33d4e66780c0 311 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 312 /* sl_Sendto */
IanBenzMaxim 0:33d4e66780c0 313 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 314 typedef union
IanBenzMaxim 0:33d4e66780c0 315 {
IanBenzMaxim 0:33d4e66780c0 316 _SocketAddrCommand_u Cmd;
IanBenzMaxim 0:33d4e66780c0 317 /* no response for 'sendto' commands*/
IanBenzMaxim 0:33d4e66780c0 318 }_SlSendtoMsg_u;
IanBenzMaxim 0:33d4e66780c0 319
IanBenzMaxim 0:33d4e66780c0 320 #if _SL_INCLUDE_FUNC(sl_SendTo)
IanBenzMaxim 0:33d4e66780c0 321 _i16 sl_SendTo(_i16 sd, const void *pBuf, _i16 Len, _i16 flags, const SlSockAddr_t *to, SlSocklen_t tolen)
IanBenzMaxim 0:33d4e66780c0 322 {
IanBenzMaxim 0:33d4e66780c0 323 _SlSendtoMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 324 _SlCmdCtrl_t CmdCtrl = {0, 0, 0};
IanBenzMaxim 0:33d4e66780c0 325 _SlCmdExt_t CmdExt;
IanBenzMaxim 0:33d4e66780c0 326 _u16 ChunkLen;
IanBenzMaxim 0:33d4e66780c0 327 _i16 RetVal;
IanBenzMaxim 0:33d4e66780c0 328
IanBenzMaxim 0:33d4e66780c0 329 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 330 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 331 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 332
IanBenzMaxim 0:33d4e66780c0 333 _SlDrvResetCmdExt(&CmdExt);
IanBenzMaxim 0:33d4e66780c0 334 CmdExt.TxPayloadLen = (_u16)Len;
IanBenzMaxim 0:33d4e66780c0 335 CmdExt.pTxPayload = (_u8 *)pBuf;
IanBenzMaxim 0:33d4e66780c0 336
IanBenzMaxim 0:33d4e66780c0 337 switch(to->sa_family)
IanBenzMaxim 0:33d4e66780c0 338 {
IanBenzMaxim 0:33d4e66780c0 339 case SL_AF_INET:
IanBenzMaxim 0:33d4e66780c0 340 CmdCtrl.Opcode = SL_OPCODE_SOCKET_SENDTO;
IanBenzMaxim 0:33d4e66780c0 341 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv4Command_t);
IanBenzMaxim 0:33d4e66780c0 342 break;
IanBenzMaxim 0:33d4e66780c0 343 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 344 case SL_AF_INET6_EUI_48:
IanBenzMaxim 0:33d4e66780c0 345 CmdCtrl.Opcode = SL_OPCODE_SOCKET_BIND_V6;
IanBenzMaxim 0:33d4e66780c0 346 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv6EUI48Command_t);
IanBenzMaxim 0:33d4e66780c0 347 break;
IanBenzMaxim 0:33d4e66780c0 348 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 349 case AF_INET6:
IanBenzMaxim 0:33d4e66780c0 350 CmdCtrl.Opcode = SL_OPCODE_SOCKET_SENDTO_V6;
IanBenzMaxim 0:33d4e66780c0 351 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv6Command_t);
IanBenzMaxim 0:33d4e66780c0 352 break;
IanBenzMaxim 0:33d4e66780c0 353 #endif
IanBenzMaxim 0:33d4e66780c0 354 #endif
IanBenzMaxim 0:33d4e66780c0 355 case SL_AF_RF:
IanBenzMaxim 0:33d4e66780c0 356 default:
IanBenzMaxim 0:33d4e66780c0 357 return SL_RET_CODE_INVALID_INPUT;
IanBenzMaxim 0:33d4e66780c0 358 }
IanBenzMaxim 0:33d4e66780c0 359
IanBenzMaxim 0:33d4e66780c0 360 ChunkLen = _sl_TruncatePayloadByProtocol(sd,(_u16)Len);
IanBenzMaxim 0:33d4e66780c0 361 Msg.Cmd.IpV4.lenOrPadding = (_i16)ChunkLen;
IanBenzMaxim 0:33d4e66780c0 362 CmdExt.TxPayloadLen = ChunkLen;
IanBenzMaxim 0:33d4e66780c0 363
IanBenzMaxim 0:33d4e66780c0 364 Msg.Cmd.IpV4.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 365
IanBenzMaxim 0:33d4e66780c0 366 _sl_BuildAddress(to, &Msg.Cmd);
IanBenzMaxim 0:33d4e66780c0 367
IanBenzMaxim 0:33d4e66780c0 368 Msg.Cmd.IpV4.FamilyAndFlags |= flags & 0x0F;
IanBenzMaxim 0:33d4e66780c0 369
IanBenzMaxim 0:33d4e66780c0 370 do
IanBenzMaxim 0:33d4e66780c0 371 {
IanBenzMaxim 0:33d4e66780c0 372 RetVal = _SlDrvDataWriteOp((_SlSd_t)sd, &CmdCtrl, &Msg, &CmdExt);
IanBenzMaxim 0:33d4e66780c0 373
IanBenzMaxim 0:33d4e66780c0 374 if(SL_OS_RET_CODE_OK == RetVal)
IanBenzMaxim 0:33d4e66780c0 375 {
IanBenzMaxim 0:33d4e66780c0 376 CmdExt.pTxPayload += ChunkLen;
IanBenzMaxim 0:33d4e66780c0 377 ChunkLen = (_u16)((_u8 *)pBuf + Len - CmdExt.pTxPayload);
IanBenzMaxim 0:33d4e66780c0 378 ChunkLen = _sl_TruncatePayloadByProtocol(sd,ChunkLen);
IanBenzMaxim 0:33d4e66780c0 379 CmdExt.TxPayloadLen = ChunkLen;
IanBenzMaxim 0:33d4e66780c0 380 Msg.Cmd.IpV4.lenOrPadding = (_i16)ChunkLen;
IanBenzMaxim 0:33d4e66780c0 381 }
IanBenzMaxim 0:33d4e66780c0 382 else
IanBenzMaxim 0:33d4e66780c0 383 {
IanBenzMaxim 0:33d4e66780c0 384 return RetVal;
IanBenzMaxim 0:33d4e66780c0 385 }
IanBenzMaxim 0:33d4e66780c0 386 }while(ChunkLen > 0);
IanBenzMaxim 0:33d4e66780c0 387
IanBenzMaxim 0:33d4e66780c0 388 return (_i16)Len;
IanBenzMaxim 0:33d4e66780c0 389 }
IanBenzMaxim 0:33d4e66780c0 390 #endif
IanBenzMaxim 0:33d4e66780c0 391
IanBenzMaxim 0:33d4e66780c0 392 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 393 /* sl_Recvfrom */
IanBenzMaxim 0:33d4e66780c0 394 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 395 typedef union
IanBenzMaxim 0:33d4e66780c0 396 {
IanBenzMaxim 0:33d4e66780c0 397 _sendRecvCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 398 _SocketAddrResponse_u Rsp;
IanBenzMaxim 0:33d4e66780c0 399 }_SlRecvfromMsg_u;
IanBenzMaxim 0:33d4e66780c0 400
IanBenzMaxim 0:33d4e66780c0 401 static const _SlCmdCtrl_t _SlRecvfomCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 402 {
IanBenzMaxim 0:33d4e66780c0 403 SL_OPCODE_SOCKET_RECVFROM,
IanBenzMaxim 0:33d4e66780c0 404 (_SlArgSize_t)sizeof(_sendRecvCommand_t),
IanBenzMaxim 0:33d4e66780c0 405 (_SlArgSize_t)sizeof(_SocketAddrResponse_u)
IanBenzMaxim 0:33d4e66780c0 406 };
IanBenzMaxim 0:33d4e66780c0 407
IanBenzMaxim 0:33d4e66780c0 408
IanBenzMaxim 0:33d4e66780c0 409
IanBenzMaxim 0:33d4e66780c0 410 #if _SL_INCLUDE_FUNC(sl_RecvFrom)
IanBenzMaxim 0:33d4e66780c0 411 _i16 sl_RecvFrom(_i16 sd, void *buf, _i16 Len, _i16 flags, SlSockAddr_t *from, SlSocklen_t *fromlen)
IanBenzMaxim 0:33d4e66780c0 412 {
IanBenzMaxim 0:33d4e66780c0 413 _SlRecvfromMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 414 _SlCmdExt_t CmdExt;
IanBenzMaxim 0:33d4e66780c0 415 _i16 RetVal;
IanBenzMaxim 0:33d4e66780c0 416
IanBenzMaxim 0:33d4e66780c0 417 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 418 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 419 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 420
IanBenzMaxim 0:33d4e66780c0 421 _SlDrvResetCmdExt(&CmdExt);
IanBenzMaxim 0:33d4e66780c0 422 CmdExt.RxPayloadLen = Len;
IanBenzMaxim 0:33d4e66780c0 423 CmdExt.pRxPayload = (_u8 *)buf;
IanBenzMaxim 0:33d4e66780c0 424
IanBenzMaxim 0:33d4e66780c0 425 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 426 Msg.Cmd.StatusOrLen = (_u16)Len;
IanBenzMaxim 0:33d4e66780c0 427
IanBenzMaxim 0:33d4e66780c0 428 /* no size truncation in recv path */
IanBenzMaxim 0:33d4e66780c0 429 CmdExt.RxPayloadLen = (_i16)Msg.Cmd.StatusOrLen;
IanBenzMaxim 0:33d4e66780c0 430
IanBenzMaxim 0:33d4e66780c0 431
IanBenzMaxim 0:33d4e66780c0 432 Msg.Cmd.FamilyAndFlags = (_u8)(flags & 0x0F);
IanBenzMaxim 0:33d4e66780c0 433
IanBenzMaxim 0:33d4e66780c0 434
IanBenzMaxim 0:33d4e66780c0 435 if(sizeof(SlSockAddrIn_t) == *fromlen)
IanBenzMaxim 0:33d4e66780c0 436 {
IanBenzMaxim 0:33d4e66780c0 437 Msg.Cmd.FamilyAndFlags |= (SL_AF_INET << 4);
IanBenzMaxim 0:33d4e66780c0 438 }
IanBenzMaxim 0:33d4e66780c0 439 else if (sizeof(SlSockAddrIn6_t) == *fromlen)
IanBenzMaxim 0:33d4e66780c0 440 {
IanBenzMaxim 0:33d4e66780c0 441 Msg.Cmd.FamilyAndFlags |= (SL_AF_INET6 << 4);
IanBenzMaxim 0:33d4e66780c0 442 }
IanBenzMaxim 0:33d4e66780c0 443 else
IanBenzMaxim 0:33d4e66780c0 444 {
IanBenzMaxim 0:33d4e66780c0 445 return SL_RET_CODE_INVALID_INPUT;
IanBenzMaxim 0:33d4e66780c0 446 }
IanBenzMaxim 0:33d4e66780c0 447
IanBenzMaxim 0:33d4e66780c0 448 RetVal = _SlDrvDataReadOp((_SlSd_t)sd, (_SlCmdCtrl_t *)&_SlRecvfomCmdCtrl, &Msg, &CmdExt);
IanBenzMaxim 0:33d4e66780c0 449 if( RetVal != SL_OS_RET_CODE_OK )
IanBenzMaxim 0:33d4e66780c0 450 {
IanBenzMaxim 0:33d4e66780c0 451 return RetVal;
IanBenzMaxim 0:33d4e66780c0 452 }
IanBenzMaxim 0:33d4e66780c0 453
IanBenzMaxim 0:33d4e66780c0 454 RetVal = Msg.Rsp.IpV4.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 455
IanBenzMaxim 0:33d4e66780c0 456 if(RetVal >= 0)
IanBenzMaxim 0:33d4e66780c0 457 {
IanBenzMaxim 0:33d4e66780c0 458 VERIFY_PROTOCOL(sd == (_i16)Msg.Rsp.IpV4.sd);
IanBenzMaxim 0:33d4e66780c0 459 #if 0
IanBenzMaxim 0:33d4e66780c0 460 _sl_ParseAddress(&Msg.Rsp, from, fromlen);
IanBenzMaxim 0:33d4e66780c0 461 #else
IanBenzMaxim 0:33d4e66780c0 462 from->sa_family = Msg.Rsp.IpV4.family;
IanBenzMaxim 0:33d4e66780c0 463 if(SL_AF_INET == from->sa_family)
IanBenzMaxim 0:33d4e66780c0 464 {
IanBenzMaxim 0:33d4e66780c0 465 ((SlSockAddrIn_t *)from)->sin_port = Msg.Rsp.IpV4.port;
IanBenzMaxim 0:33d4e66780c0 466 ((SlSockAddrIn_t *)from)->sin_addr.s_addr = Msg.Rsp.IpV4.address;
IanBenzMaxim 0:33d4e66780c0 467 *fromlen = (SlSocklen_t)sizeof(SlSockAddrIn_t);
IanBenzMaxim 0:33d4e66780c0 468 }
IanBenzMaxim 0:33d4e66780c0 469 else if (SL_AF_INET6_EUI_48 == from->sa_family )
IanBenzMaxim 0:33d4e66780c0 470 {
IanBenzMaxim 0:33d4e66780c0 471 ((SlSockAddrIn6_t *)from)->sin6_port = Msg.Rsp.IpV6EUI48.port;
IanBenzMaxim 0:33d4e66780c0 472 sl_Memcpy(((SlSockAddrIn6_t *)from)->sin6_addr._S6_un._S6_u8, Msg.Rsp.IpV6EUI48.address, 6);
IanBenzMaxim 0:33d4e66780c0 473 }
IanBenzMaxim 0:33d4e66780c0 474 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 475 else if(AF_INET6 == from->sa_family)
IanBenzMaxim 0:33d4e66780c0 476 {
IanBenzMaxim 0:33d4e66780c0 477 VERIFY_PROTOCOL(*fromlen >= sizeof(sockaddr_in6));
IanBenzMaxim 0:33d4e66780c0 478
IanBenzMaxim 0:33d4e66780c0 479 ((sockaddr_in6 *)from)->sin6_port = Msg.Rsp.IpV6.port;
IanBenzMaxim 0:33d4e66780c0 480 sl_Memcpy(((sockaddr_in6 *)from)->sin6_addr._S6_un._S6_u32, Msg.Rsp.IpV6.address, 16);
IanBenzMaxim 0:33d4e66780c0 481 *fromlen = sizeof(sockaddr_in6);
IanBenzMaxim 0:33d4e66780c0 482 }
IanBenzMaxim 0:33d4e66780c0 483 #endif
IanBenzMaxim 0:33d4e66780c0 484 #endif
IanBenzMaxim 0:33d4e66780c0 485 }
IanBenzMaxim 0:33d4e66780c0 486
IanBenzMaxim 0:33d4e66780c0 487 return (_i16)RetVal;
IanBenzMaxim 0:33d4e66780c0 488 }
IanBenzMaxim 0:33d4e66780c0 489 #endif
IanBenzMaxim 0:33d4e66780c0 490
IanBenzMaxim 0:33d4e66780c0 491 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 492 /* sl_Connect */
IanBenzMaxim 0:33d4e66780c0 493 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 494 typedef union
IanBenzMaxim 0:33d4e66780c0 495 {
IanBenzMaxim 0:33d4e66780c0 496 _SocketAddrCommand_u Cmd;
IanBenzMaxim 0:33d4e66780c0 497 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 498 }_SlSockConnectMsg_u;
IanBenzMaxim 0:33d4e66780c0 499
IanBenzMaxim 0:33d4e66780c0 500 #if _SL_INCLUDE_FUNC(sl_Connect)
IanBenzMaxim 0:33d4e66780c0 501 _i16 sl_Connect(_i16 sd, const SlSockAddr_t *addr, _i16 addrlen)
IanBenzMaxim 0:33d4e66780c0 502 {
IanBenzMaxim 0:33d4e66780c0 503 _SlSockConnectMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 504 _SlReturnVal_t RetVal;
IanBenzMaxim 0:33d4e66780c0 505 _SlCmdCtrl_t CmdCtrl = {0, (_SlArgSize_t)0, (_SlArgSize_t)sizeof(_SocketResponse_t)};
IanBenzMaxim 0:33d4e66780c0 506 _SocketResponse_t AsyncRsp;
IanBenzMaxim 0:33d4e66780c0 507 _u8 ObjIdx = MAX_CONCURRENT_ACTIONS;
IanBenzMaxim 0:33d4e66780c0 508
IanBenzMaxim 0:33d4e66780c0 509 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 510 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 511 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 512
IanBenzMaxim 0:33d4e66780c0 513
IanBenzMaxim 0:33d4e66780c0 514 switch(addr->sa_family)
IanBenzMaxim 0:33d4e66780c0 515 {
IanBenzMaxim 0:33d4e66780c0 516 case SL_AF_INET :
IanBenzMaxim 0:33d4e66780c0 517 CmdCtrl.Opcode = SL_OPCODE_SOCKET_CONNECT;
IanBenzMaxim 0:33d4e66780c0 518 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv4Command_t);
IanBenzMaxim 0:33d4e66780c0 519 /* Do nothing - cmd already initialized to this type */
IanBenzMaxim 0:33d4e66780c0 520 break;
IanBenzMaxim 0:33d4e66780c0 521 case SL_AF_INET6_EUI_48:
IanBenzMaxim 0:33d4e66780c0 522 CmdCtrl.Opcode = SL_OPCODE_SOCKET_CONNECT_V6;
IanBenzMaxim 0:33d4e66780c0 523 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv6EUI48Command_t);
IanBenzMaxim 0:33d4e66780c0 524 break;
IanBenzMaxim 0:33d4e66780c0 525 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 526 case AF_INET6:
IanBenzMaxim 0:33d4e66780c0 527 CmdCtrl.Opcode = SL_OPCODE_SOCKET_CONNECT_V6;
IanBenzMaxim 0:33d4e66780c0 528 CmdCtrl.TxDescLen = (_SlArgSize_t)sizeof(_SocketAddrIPv6Command_t);
IanBenzMaxim 0:33d4e66780c0 529 break;
IanBenzMaxim 0:33d4e66780c0 530 #endif
IanBenzMaxim 0:33d4e66780c0 531 case SL_AF_RF:
IanBenzMaxim 0:33d4e66780c0 532 default:
IanBenzMaxim 0:33d4e66780c0 533 return SL_RET_CODE_INVALID_INPUT;
IanBenzMaxim 0:33d4e66780c0 534 }
IanBenzMaxim 0:33d4e66780c0 535
IanBenzMaxim 0:33d4e66780c0 536 Msg.Cmd.IpV4.lenOrPadding = 0;
IanBenzMaxim 0:33d4e66780c0 537 Msg.Cmd.IpV4.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 538
IanBenzMaxim 0:33d4e66780c0 539 _sl_BuildAddress(addr, &Msg.Cmd);
IanBenzMaxim 0:33d4e66780c0 540
IanBenzMaxim 0:33d4e66780c0 541
IanBenzMaxim 0:33d4e66780c0 542 ObjIdx = _SlDrvProtectAsyncRespSetting((_u8*)&AsyncRsp, CONNECT_ID, (_u8)(sd & BSD_SOCKET_ID_MASK));
IanBenzMaxim 0:33d4e66780c0 543
IanBenzMaxim 0:33d4e66780c0 544 if (MAX_CONCURRENT_ACTIONS == ObjIdx)
IanBenzMaxim 0:33d4e66780c0 545 {
IanBenzMaxim 0:33d4e66780c0 546 return SL_POOL_IS_EMPTY;
IanBenzMaxim 0:33d4e66780c0 547 }
IanBenzMaxim 0:33d4e66780c0 548
IanBenzMaxim 0:33d4e66780c0 549 /* send the command */
IanBenzMaxim 0:33d4e66780c0 550 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&CmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 551 VERIFY_PROTOCOL(Msg.Rsp.sd == (_u8)sd)
IanBenzMaxim 0:33d4e66780c0 552
IanBenzMaxim 0:33d4e66780c0 553 RetVal = Msg.Rsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 554
IanBenzMaxim 0:33d4e66780c0 555 if(SL_RET_CODE_OK == RetVal)
IanBenzMaxim 0:33d4e66780c0 556 {
IanBenzMaxim 0:33d4e66780c0 557 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 558 /*In case socket is non-blocking one, the async event should be received immediately */
IanBenzMaxim 0:33d4e66780c0 559 if( g_pCB->SocketNonBlocking >> (sd & BSD_SOCKET_ID_MASK))
IanBenzMaxim 0:33d4e66780c0 560 {
IanBenzMaxim 0:33d4e66780c0 561 SL_DRV_SYNC_OBJ_WAIT_TIMEOUT(&g_pCB->ObjPool[ObjIdx].SyncObj,
IanBenzMaxim 0:33d4e66780c0 562 SL_DRIVER_TIMEOUT_SHORT,
IanBenzMaxim 0:33d4e66780c0 563 SL_DRIVER_API_SOCKET_CONNECT
IanBenzMaxim 0:33d4e66780c0 564 );
IanBenzMaxim 0:33d4e66780c0 565 }
IanBenzMaxim 0:33d4e66780c0 566 else
IanBenzMaxim 0:33d4e66780c0 567 #endif
IanBenzMaxim 0:33d4e66780c0 568 {
IanBenzMaxim 0:33d4e66780c0 569 /* wait for async and get Data Read parameters */
IanBenzMaxim 0:33d4e66780c0 570 SL_DRV_SYNC_OBJ_WAIT_FOREVER(&g_pCB->ObjPool[ObjIdx].SyncObj);
IanBenzMaxim 0:33d4e66780c0 571 }
IanBenzMaxim 0:33d4e66780c0 572
IanBenzMaxim 0:33d4e66780c0 573 VERIFY_PROTOCOL(AsyncRsp.sd == (_u8)sd);
IanBenzMaxim 0:33d4e66780c0 574
IanBenzMaxim 0:33d4e66780c0 575 RetVal = AsyncRsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 576 }
IanBenzMaxim 0:33d4e66780c0 577
IanBenzMaxim 0:33d4e66780c0 578
IanBenzMaxim 0:33d4e66780c0 579
IanBenzMaxim 0:33d4e66780c0 580 _SlDrvReleasePoolObj(ObjIdx);
IanBenzMaxim 0:33d4e66780c0 581 return RetVal;
IanBenzMaxim 0:33d4e66780c0 582 }
IanBenzMaxim 0:33d4e66780c0 583
IanBenzMaxim 0:33d4e66780c0 584 #endif
IanBenzMaxim 0:33d4e66780c0 585
IanBenzMaxim 0:33d4e66780c0 586
IanBenzMaxim 0:33d4e66780c0 587 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 588 /* _sl_HandleAsync_Connect */
IanBenzMaxim 0:33d4e66780c0 589 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 590 _SlReturnVal_t _sl_HandleAsync_Connect(void *pVoidBuf)
IanBenzMaxim 0:33d4e66780c0 591 {
IanBenzMaxim 0:33d4e66780c0 592 _SocketResponse_t *pMsgArgs = (_SocketResponse_t *)_SL_RESP_ARGS_START(pVoidBuf);
IanBenzMaxim 0:33d4e66780c0 593
IanBenzMaxim 0:33d4e66780c0 594 SL_DRV_PROTECTION_OBJ_LOCK_FOREVER();
IanBenzMaxim 0:33d4e66780c0 595
IanBenzMaxim 0:33d4e66780c0 596 VERIFY_PROTOCOL((pMsgArgs->sd & BSD_SOCKET_ID_MASK) <= SL_MAX_SOCKETS);
IanBenzMaxim 0:33d4e66780c0 597 VERIFY_SOCKET_CB(NULL != g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs);
IanBenzMaxim 0:33d4e66780c0 598
IanBenzMaxim 0:33d4e66780c0 599
IanBenzMaxim 0:33d4e66780c0 600 ((_SocketResponse_t *)(g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs))->sd = pMsgArgs->sd;
IanBenzMaxim 0:33d4e66780c0 601 ((_SocketResponse_t *)(g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs))->statusOrLen = pMsgArgs->statusOrLen;
IanBenzMaxim 0:33d4e66780c0 602
IanBenzMaxim 0:33d4e66780c0 603
IanBenzMaxim 0:33d4e66780c0 604 SL_DRV_SYNC_OBJ_SIGNAL(&g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].SyncObj);
IanBenzMaxim 0:33d4e66780c0 605 SL_DRV_PROTECTION_OBJ_UNLOCK();
IanBenzMaxim 0:33d4e66780c0 606
IanBenzMaxim 0:33d4e66780c0 607 return SL_RET_CODE_OK;
IanBenzMaxim 0:33d4e66780c0 608 }
IanBenzMaxim 0:33d4e66780c0 609
IanBenzMaxim 0:33d4e66780c0 610 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 611 /* sl_Send */
IanBenzMaxim 0:33d4e66780c0 612 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 613 typedef union
IanBenzMaxim 0:33d4e66780c0 614 {
IanBenzMaxim 0:33d4e66780c0 615 _sendRecvCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 616 /* no response for 'sendto' commands*/
IanBenzMaxim 0:33d4e66780c0 617 }_SlSendMsg_u;
IanBenzMaxim 0:33d4e66780c0 618
IanBenzMaxim 0:33d4e66780c0 619 static const _SlCmdCtrl_t _SlSendCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 620 {
IanBenzMaxim 0:33d4e66780c0 621 SL_OPCODE_SOCKET_SEND,
IanBenzMaxim 0:33d4e66780c0 622 (_SlArgSize_t)sizeof(_sendRecvCommand_t),
IanBenzMaxim 0:33d4e66780c0 623 (_SlArgSize_t)0
IanBenzMaxim 0:33d4e66780c0 624 };
IanBenzMaxim 0:33d4e66780c0 625
IanBenzMaxim 0:33d4e66780c0 626 #if _SL_INCLUDE_FUNC(sl_Send)
IanBenzMaxim 0:33d4e66780c0 627 _i16 sl_Send(_i16 sd, const void *pBuf, _i16 Len, _i16 flags)
IanBenzMaxim 0:33d4e66780c0 628 {
IanBenzMaxim 0:33d4e66780c0 629 _SlSendMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 630 _SlCmdExt_t CmdExt;
IanBenzMaxim 0:33d4e66780c0 631 _u16 ChunkLen;
IanBenzMaxim 0:33d4e66780c0 632 _i16 RetVal;
IanBenzMaxim 0:33d4e66780c0 633 _u32 tempVal;
IanBenzMaxim 0:33d4e66780c0 634 _u8 runSingleChunk = FALSE;
IanBenzMaxim 0:33d4e66780c0 635
IanBenzMaxim 0:33d4e66780c0 636 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 637 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 638 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 639
IanBenzMaxim 0:33d4e66780c0 640 _SlDrvResetCmdExt(&CmdExt);
IanBenzMaxim 0:33d4e66780c0 641 CmdExt.TxPayloadLen = (_u16)Len;
IanBenzMaxim 0:33d4e66780c0 642 CmdExt.pTxPayload = (_u8 *)pBuf;
IanBenzMaxim 0:33d4e66780c0 643
IanBenzMaxim 0:33d4e66780c0 644 /* Only for RAW transceiver type socket, relay the flags parameter in the 2 bytes (4 byte aligned) before the actual payload */
IanBenzMaxim 0:33d4e66780c0 645 if ((sd & SL_SOCKET_PAYLOAD_TYPE_MASK) == SL_SOCKET_PAYLOAD_TYPE_RAW_TRANCEIVER)
IanBenzMaxim 0:33d4e66780c0 646 {
IanBenzMaxim 0:33d4e66780c0 647 tempVal = (_u32)flags;
IanBenzMaxim 0:33d4e66780c0 648 CmdExt.pRxPayload = (_u8 *)&tempVal;
IanBenzMaxim 0:33d4e66780c0 649 CmdExt.RxPayloadLen = -4; /* mark as Rx data to send */
IanBenzMaxim 0:33d4e66780c0 650 runSingleChunk = TRUE;
IanBenzMaxim 0:33d4e66780c0 651 }
IanBenzMaxim 0:33d4e66780c0 652 else
IanBenzMaxim 0:33d4e66780c0 653 {
IanBenzMaxim 0:33d4e66780c0 654 CmdExt.pRxPayload = NULL;
IanBenzMaxim 0:33d4e66780c0 655 }
IanBenzMaxim 0:33d4e66780c0 656
IanBenzMaxim 0:33d4e66780c0 657 ChunkLen = _sl_TruncatePayloadByProtocol(sd,(_u16)Len);
IanBenzMaxim 0:33d4e66780c0 658 CmdExt.TxPayloadLen = ChunkLen;
IanBenzMaxim 0:33d4e66780c0 659 Msg.Cmd.StatusOrLen = ChunkLen;
IanBenzMaxim 0:33d4e66780c0 660 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 661 Msg.Cmd.FamilyAndFlags |= flags & 0x0F;
IanBenzMaxim 0:33d4e66780c0 662
IanBenzMaxim 0:33d4e66780c0 663 do
IanBenzMaxim 0:33d4e66780c0 664 {
IanBenzMaxim 0:33d4e66780c0 665 RetVal = _SlDrvDataWriteOp((_u8)sd, (_SlCmdCtrl_t *)&_SlSendCmdCtrl, &Msg, &CmdExt);
IanBenzMaxim 0:33d4e66780c0 666 if(SL_OS_RET_CODE_OK == RetVal)
IanBenzMaxim 0:33d4e66780c0 667 {
IanBenzMaxim 0:33d4e66780c0 668 CmdExt.pTxPayload += ChunkLen;
IanBenzMaxim 0:33d4e66780c0 669 ChunkLen = (_u16)((_u8 *)pBuf + Len - CmdExt.pTxPayload);
IanBenzMaxim 0:33d4e66780c0 670 ChunkLen = _sl_TruncatePayloadByProtocol(sd, ChunkLen);
IanBenzMaxim 0:33d4e66780c0 671 CmdExt.TxPayloadLen = ChunkLen;
IanBenzMaxim 0:33d4e66780c0 672 Msg.Cmd.StatusOrLen = ChunkLen;
IanBenzMaxim 0:33d4e66780c0 673 }
IanBenzMaxim 0:33d4e66780c0 674 else
IanBenzMaxim 0:33d4e66780c0 675 {
IanBenzMaxim 0:33d4e66780c0 676 return RetVal;
IanBenzMaxim 0:33d4e66780c0 677 }
IanBenzMaxim 0:33d4e66780c0 678 }while((ChunkLen > 0) && (runSingleChunk==FALSE));
IanBenzMaxim 0:33d4e66780c0 679
IanBenzMaxim 0:33d4e66780c0 680 return (_i16)Len;
IanBenzMaxim 0:33d4e66780c0 681 }
IanBenzMaxim 0:33d4e66780c0 682 #endif
IanBenzMaxim 0:33d4e66780c0 683
IanBenzMaxim 0:33d4e66780c0 684 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 685 /* sl_Listen */
IanBenzMaxim 0:33d4e66780c0 686 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 687 typedef union
IanBenzMaxim 0:33d4e66780c0 688 {
IanBenzMaxim 0:33d4e66780c0 689 _ListenCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 690 _BasicResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 691 }_SlListenMsg_u;
IanBenzMaxim 0:33d4e66780c0 692
IanBenzMaxim 0:33d4e66780c0 693
IanBenzMaxim 0:33d4e66780c0 694
IanBenzMaxim 0:33d4e66780c0 695 #if _SL_INCLUDE_FUNC(sl_Listen)
IanBenzMaxim 0:33d4e66780c0 696
IanBenzMaxim 0:33d4e66780c0 697 static const _SlCmdCtrl_t _SlListenCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 698 {
IanBenzMaxim 0:33d4e66780c0 699 SL_OPCODE_SOCKET_LISTEN,
IanBenzMaxim 0:33d4e66780c0 700 (_SlArgSize_t)sizeof(_ListenCommand_t),
IanBenzMaxim 0:33d4e66780c0 701 (_SlArgSize_t)sizeof(_BasicResponse_t),
IanBenzMaxim 0:33d4e66780c0 702 };
IanBenzMaxim 0:33d4e66780c0 703
IanBenzMaxim 0:33d4e66780c0 704 _i16 sl_Listen(_i16 sd, _i16 backlog)
IanBenzMaxim 0:33d4e66780c0 705 {
IanBenzMaxim 0:33d4e66780c0 706 _SlListenMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 707
IanBenzMaxim 0:33d4e66780c0 708 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 709 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 710 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 711
IanBenzMaxim 0:33d4e66780c0 712 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 713 Msg.Cmd.backlog = (_u8)backlog;
IanBenzMaxim 0:33d4e66780c0 714
IanBenzMaxim 0:33d4e66780c0 715 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlListenCmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 716
IanBenzMaxim 0:33d4e66780c0 717 return (_i16)Msg.Rsp.status;
IanBenzMaxim 0:33d4e66780c0 718 }
IanBenzMaxim 0:33d4e66780c0 719 #endif
IanBenzMaxim 0:33d4e66780c0 720
IanBenzMaxim 0:33d4e66780c0 721 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 722 /* sl_Accept */
IanBenzMaxim 0:33d4e66780c0 723 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 724 typedef union
IanBenzMaxim 0:33d4e66780c0 725 {
IanBenzMaxim 0:33d4e66780c0 726 _AcceptCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 727 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 728 }_SlSockAcceptMsg_u;
IanBenzMaxim 0:33d4e66780c0 729
IanBenzMaxim 0:33d4e66780c0 730
IanBenzMaxim 0:33d4e66780c0 731
IanBenzMaxim 0:33d4e66780c0 732 #if _SL_INCLUDE_FUNC(sl_Accept)
IanBenzMaxim 0:33d4e66780c0 733
IanBenzMaxim 0:33d4e66780c0 734 static const _SlCmdCtrl_t _SlAcceptCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 735 {
IanBenzMaxim 0:33d4e66780c0 736 SL_OPCODE_SOCKET_ACCEPT,
IanBenzMaxim 0:33d4e66780c0 737 (_SlArgSize_t)sizeof(_AcceptCommand_t),
IanBenzMaxim 0:33d4e66780c0 738 (_SlArgSize_t)sizeof(_BasicResponse_t),
IanBenzMaxim 0:33d4e66780c0 739 };
IanBenzMaxim 0:33d4e66780c0 740
IanBenzMaxim 0:33d4e66780c0 741 _i16 sl_Accept(_i16 sd, SlSockAddr_t *addr, SlSocklen_t *addrlen)
IanBenzMaxim 0:33d4e66780c0 742 {
IanBenzMaxim 0:33d4e66780c0 743 _SlSockAcceptMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 744 _SlReturnVal_t RetVal;
IanBenzMaxim 0:33d4e66780c0 745 _SocketAddrResponse_u AsyncRsp;
IanBenzMaxim 0:33d4e66780c0 746
IanBenzMaxim 0:33d4e66780c0 747 _u8 ObjIdx = MAX_CONCURRENT_ACTIONS;
IanBenzMaxim 0:33d4e66780c0 748
IanBenzMaxim 0:33d4e66780c0 749 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 750 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 751 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 752
IanBenzMaxim 0:33d4e66780c0 753
IanBenzMaxim 0:33d4e66780c0 754
IanBenzMaxim 0:33d4e66780c0 755 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 756 Msg.Cmd.family = (_u8)((sizeof(SlSockAddrIn_t) == *addrlen) ? SL_AF_INET : SL_AF_INET6);
IanBenzMaxim 0:33d4e66780c0 757
IanBenzMaxim 0:33d4e66780c0 758
IanBenzMaxim 0:33d4e66780c0 759 ObjIdx = _SlDrvProtectAsyncRespSetting((_u8*)&AsyncRsp, ACCEPT_ID, (_u8)sd & BSD_SOCKET_ID_MASK );
IanBenzMaxim 0:33d4e66780c0 760
IanBenzMaxim 0:33d4e66780c0 761 if (MAX_CONCURRENT_ACTIONS == ObjIdx)
IanBenzMaxim 0:33d4e66780c0 762 {
IanBenzMaxim 0:33d4e66780c0 763 return SL_POOL_IS_EMPTY;
IanBenzMaxim 0:33d4e66780c0 764 }
IanBenzMaxim 0:33d4e66780c0 765
IanBenzMaxim 0:33d4e66780c0 766 /* send the command */
IanBenzMaxim 0:33d4e66780c0 767 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlAcceptCmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 768 VERIFY_PROTOCOL(Msg.Rsp.sd == (_u8)sd);
IanBenzMaxim 0:33d4e66780c0 769
IanBenzMaxim 0:33d4e66780c0 770 RetVal = Msg.Rsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 771
IanBenzMaxim 0:33d4e66780c0 772 if(SL_OS_RET_CODE_OK == RetVal)
IanBenzMaxim 0:33d4e66780c0 773 {
IanBenzMaxim 0:33d4e66780c0 774 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 775 /* in case socket is non-blocking one, the async event should be received immediately */
IanBenzMaxim 0:33d4e66780c0 776 if( g_pCB->SocketNonBlocking & (1<<(sd & BSD_SOCKET_ID_MASK) ))
IanBenzMaxim 0:33d4e66780c0 777 {
IanBenzMaxim 0:33d4e66780c0 778 SL_DRV_SYNC_OBJ_WAIT_TIMEOUT(&g_pCB->ObjPool[ObjIdx].SyncObj,
IanBenzMaxim 0:33d4e66780c0 779 SL_DRIVER_TIMEOUT_SHORT,
IanBenzMaxim 0:33d4e66780c0 780 SL_DRIVER_API_SOCKET_ACCEPT
IanBenzMaxim 0:33d4e66780c0 781 );
IanBenzMaxim 0:33d4e66780c0 782 }
IanBenzMaxim 0:33d4e66780c0 783 else
IanBenzMaxim 0:33d4e66780c0 784 #endif
IanBenzMaxim 0:33d4e66780c0 785 {
IanBenzMaxim 0:33d4e66780c0 786 /* wait for async and get Data Read parameters */
IanBenzMaxim 0:33d4e66780c0 787 SL_DRV_SYNC_OBJ_WAIT_FOREVER(&g_pCB->ObjPool[ObjIdx].SyncObj);
IanBenzMaxim 0:33d4e66780c0 788 }
IanBenzMaxim 0:33d4e66780c0 789
IanBenzMaxim 0:33d4e66780c0 790 VERIFY_PROTOCOL(AsyncRsp.IpV4.sd == (_u8)sd);
IanBenzMaxim 0:33d4e66780c0 791
IanBenzMaxim 0:33d4e66780c0 792 RetVal = AsyncRsp.IpV4.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 793 if( (NULL != addr) && (NULL != addrlen) )
IanBenzMaxim 0:33d4e66780c0 794 {
IanBenzMaxim 0:33d4e66780c0 795 #if 0 /* Kept for backup */
IanBenzMaxim 0:33d4e66780c0 796 _sl_ParseAddress(&AsyncRsp, addr, addrlen);
IanBenzMaxim 0:33d4e66780c0 797 #else
IanBenzMaxim 0:33d4e66780c0 798 addr->sa_family = AsyncRsp.IpV4.family;
IanBenzMaxim 0:33d4e66780c0 799
IanBenzMaxim 0:33d4e66780c0 800 if(SL_AF_INET == addr->sa_family)
IanBenzMaxim 0:33d4e66780c0 801 {
IanBenzMaxim 0:33d4e66780c0 802 if( *addrlen == (SlSocklen_t)sizeof( SlSockAddrIn_t ) )
IanBenzMaxim 0:33d4e66780c0 803 {
IanBenzMaxim 0:33d4e66780c0 804 ((SlSockAddrIn_t *)addr)->sin_port = AsyncRsp.IpV4.port;
IanBenzMaxim 0:33d4e66780c0 805 ((SlSockAddrIn_t *)addr)->sin_addr.s_addr = AsyncRsp.IpV4.address;
IanBenzMaxim 0:33d4e66780c0 806 }
IanBenzMaxim 0:33d4e66780c0 807 else
IanBenzMaxim 0:33d4e66780c0 808 {
IanBenzMaxim 0:33d4e66780c0 809 *addrlen = 0;
IanBenzMaxim 0:33d4e66780c0 810 }
IanBenzMaxim 0:33d4e66780c0 811 }
IanBenzMaxim 0:33d4e66780c0 812 else if (SL_AF_INET6_EUI_48 == addr->sa_family )
IanBenzMaxim 0:33d4e66780c0 813 {
IanBenzMaxim 0:33d4e66780c0 814 if( *addrlen == (SlSocklen_t)sizeof( SlSockAddrIn6_t ) )
IanBenzMaxim 0:33d4e66780c0 815 {
IanBenzMaxim 0:33d4e66780c0 816 ((SlSockAddrIn6_t *)addr)->sin6_port = AsyncRsp.IpV6EUI48.port ;
IanBenzMaxim 0:33d4e66780c0 817 /* will be called from here and from _sl_BuildAddress*/
IanBenzMaxim 0:33d4e66780c0 818 sl_Memcpy(((SlSockAddrIn6_t *)addr)->sin6_addr._S6_un._S6_u8, AsyncRsp.IpV6EUI48.address, 6);
IanBenzMaxim 0:33d4e66780c0 819 }
IanBenzMaxim 0:33d4e66780c0 820 else
IanBenzMaxim 0:33d4e66780c0 821 {
IanBenzMaxim 0:33d4e66780c0 822 *addrlen = 0;
IanBenzMaxim 0:33d4e66780c0 823 }
IanBenzMaxim 0:33d4e66780c0 824 }
IanBenzMaxim 0:33d4e66780c0 825 #ifdef SL_SUPPORT_IPV6
IanBenzMaxim 0:33d4e66780c0 826 else
IanBenzMaxim 0:33d4e66780c0 827 {
IanBenzMaxim 0:33d4e66780c0 828 if( *addrlen == sizeof( sockaddr_in6 ) )
IanBenzMaxim 0:33d4e66780c0 829 {
IanBenzMaxim 0:33d4e66780c0 830 ((sockaddr_in6 *)addr)->sin6_port = AsyncRsp.IpV6.port ;
IanBenzMaxim 0:33d4e66780c0 831 sl_Memcpy(((sockaddr_in6 *)addr)->sin6_addr._S6_un._S6_u32, AsyncRsp.IpV6.address, 16);
IanBenzMaxim 0:33d4e66780c0 832 }
IanBenzMaxim 0:33d4e66780c0 833 else
IanBenzMaxim 0:33d4e66780c0 834 {
IanBenzMaxim 0:33d4e66780c0 835 *addrlen = 0;
IanBenzMaxim 0:33d4e66780c0 836 }
IanBenzMaxim 0:33d4e66780c0 837 }
IanBenzMaxim 0:33d4e66780c0 838 #endif
IanBenzMaxim 0:33d4e66780c0 839 #endif
IanBenzMaxim 0:33d4e66780c0 840 }
IanBenzMaxim 0:33d4e66780c0 841 }
IanBenzMaxim 0:33d4e66780c0 842
IanBenzMaxim 0:33d4e66780c0 843 _SlDrvReleasePoolObj(ObjIdx);
IanBenzMaxim 0:33d4e66780c0 844 return (_i16)RetVal;
IanBenzMaxim 0:33d4e66780c0 845 }
IanBenzMaxim 0:33d4e66780c0 846 #endif
IanBenzMaxim 0:33d4e66780c0 847
IanBenzMaxim 0:33d4e66780c0 848
IanBenzMaxim 0:33d4e66780c0 849 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 850 /* sl_Htonl */
IanBenzMaxim 0:33d4e66780c0 851 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 852 _u32 sl_Htonl( _u32 val )
IanBenzMaxim 0:33d4e66780c0 853 {
IanBenzMaxim 0:33d4e66780c0 854 _u32 i = 1;
IanBenzMaxim 0:33d4e66780c0 855 _i8 *p = (_i8 *)&i;
IanBenzMaxim 0:33d4e66780c0 856 if (p[0] == 1) /* little endian */
IanBenzMaxim 0:33d4e66780c0 857 {
IanBenzMaxim 0:33d4e66780c0 858 p[0] = ((_i8* )&val)[3];
IanBenzMaxim 0:33d4e66780c0 859 p[1] = ((_i8* )&val)[2];
IanBenzMaxim 0:33d4e66780c0 860 p[2] = ((_i8* )&val)[1];
IanBenzMaxim 0:33d4e66780c0 861 p[3] = ((_i8* )&val)[0];
IanBenzMaxim 0:33d4e66780c0 862 return i;
IanBenzMaxim 0:33d4e66780c0 863 }
IanBenzMaxim 0:33d4e66780c0 864 else /* big endian */
IanBenzMaxim 0:33d4e66780c0 865 {
IanBenzMaxim 0:33d4e66780c0 866 return val;
IanBenzMaxim 0:33d4e66780c0 867 }
IanBenzMaxim 0:33d4e66780c0 868 }
IanBenzMaxim 0:33d4e66780c0 869
IanBenzMaxim 0:33d4e66780c0 870 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 871 /* sl_Htonl */
IanBenzMaxim 0:33d4e66780c0 872 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 873 _u16 sl_Htons( _u16 val )
IanBenzMaxim 0:33d4e66780c0 874 {
IanBenzMaxim 0:33d4e66780c0 875 _i16 i = 1;
IanBenzMaxim 0:33d4e66780c0 876 _i8 *p = (_i8 *)&i;
IanBenzMaxim 0:33d4e66780c0 877 if (p[0] == 1) /* little endian */
IanBenzMaxim 0:33d4e66780c0 878 {
IanBenzMaxim 0:33d4e66780c0 879 p[0] = ((_i8* )&val)[1];
IanBenzMaxim 0:33d4e66780c0 880 p[1] = ((_i8* )&val)[0];
IanBenzMaxim 0:33d4e66780c0 881 return (_u16)i;
IanBenzMaxim 0:33d4e66780c0 882 }
IanBenzMaxim 0:33d4e66780c0 883 else /* big endian */
IanBenzMaxim 0:33d4e66780c0 884 {
IanBenzMaxim 0:33d4e66780c0 885 return val;
IanBenzMaxim 0:33d4e66780c0 886 }
IanBenzMaxim 0:33d4e66780c0 887 }
IanBenzMaxim 0:33d4e66780c0 888
IanBenzMaxim 0:33d4e66780c0 889 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 890 /* _sl_HandleAsync_Accept */
IanBenzMaxim 0:33d4e66780c0 891 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 892 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 893 _SlReturnVal_t _sl_HandleAsync_Accept(void *pVoidBuf)
IanBenzMaxim 0:33d4e66780c0 894 {
IanBenzMaxim 0:33d4e66780c0 895 _SocketAddrResponse_u *pMsgArgs = (_SocketAddrResponse_u *)_SL_RESP_ARGS_START(pVoidBuf);
IanBenzMaxim 0:33d4e66780c0 896
IanBenzMaxim 0:33d4e66780c0 897 SL_DRV_PROTECTION_OBJ_LOCK_FOREVER();
IanBenzMaxim 0:33d4e66780c0 898
IanBenzMaxim 0:33d4e66780c0 899 VERIFY_PROTOCOL(( pMsgArgs->IpV4.sd & BSD_SOCKET_ID_MASK) <= SL_MAX_SOCKETS);
IanBenzMaxim 0:33d4e66780c0 900 VERIFY_SOCKET_CB(NULL != g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs);
IanBenzMaxim 0:33d4e66780c0 901
IanBenzMaxim 0:33d4e66780c0 902 sl_Memcpy(g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs, pMsgArgs,sizeof(_SocketAddrResponse_u));
IanBenzMaxim 0:33d4e66780c0 903 SL_DRV_SYNC_OBJ_SIGNAL(&g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].SyncObj);
IanBenzMaxim 0:33d4e66780c0 904
IanBenzMaxim 0:33d4e66780c0 905 SL_DRV_PROTECTION_OBJ_UNLOCK();
IanBenzMaxim 0:33d4e66780c0 906 return SL_RET_CODE_OK;
IanBenzMaxim 0:33d4e66780c0 907 }
IanBenzMaxim 0:33d4e66780c0 908
IanBenzMaxim 0:33d4e66780c0 909 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 910 /* _sl_HandleAsync_Select */
IanBenzMaxim 0:33d4e66780c0 911 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 912 _SlReturnVal_t _sl_HandleAsync_Select(void *pVoidBuf)
IanBenzMaxim 0:33d4e66780c0 913 {
IanBenzMaxim 0:33d4e66780c0 914 _SelectAsyncResponse_t *pMsgArgs = (_SelectAsyncResponse_t *)_SL_RESP_ARGS_START(pVoidBuf);
IanBenzMaxim 0:33d4e66780c0 915
IanBenzMaxim 0:33d4e66780c0 916 SL_DRV_PROTECTION_OBJ_LOCK_FOREVER();
IanBenzMaxim 0:33d4e66780c0 917
IanBenzMaxim 0:33d4e66780c0 918 VERIFY_SOCKET_CB(NULL != g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs);
IanBenzMaxim 0:33d4e66780c0 919
IanBenzMaxim 0:33d4e66780c0 920 sl_Memcpy(g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs, pMsgArgs, sizeof(_SelectAsyncResponse_t));
IanBenzMaxim 0:33d4e66780c0 921
IanBenzMaxim 0:33d4e66780c0 922 SL_DRV_SYNC_OBJ_SIGNAL(&g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].SyncObj);
IanBenzMaxim 0:33d4e66780c0 923
IanBenzMaxim 0:33d4e66780c0 924 SL_DRV_PROTECTION_OBJ_UNLOCK();
IanBenzMaxim 0:33d4e66780c0 925
IanBenzMaxim 0:33d4e66780c0 926 return SL_RET_CODE_OK;
IanBenzMaxim 0:33d4e66780c0 927 }
IanBenzMaxim 0:33d4e66780c0 928
IanBenzMaxim 0:33d4e66780c0 929 #endif
IanBenzMaxim 0:33d4e66780c0 930
IanBenzMaxim 0:33d4e66780c0 931 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 932 /* sl_Recv */
IanBenzMaxim 0:33d4e66780c0 933 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 934 typedef union
IanBenzMaxim 0:33d4e66780c0 935 {
IanBenzMaxim 0:33d4e66780c0 936 _sendRecvCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 937 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 938 }_SlRecvMsg_u;
IanBenzMaxim 0:33d4e66780c0 939
IanBenzMaxim 0:33d4e66780c0 940
IanBenzMaxim 0:33d4e66780c0 941 #if _SL_INCLUDE_FUNC(sl_Recv)
IanBenzMaxim 0:33d4e66780c0 942
IanBenzMaxim 0:33d4e66780c0 943 static const _SlCmdCtrl_t _SlRecvCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 944 {
IanBenzMaxim 0:33d4e66780c0 945 SL_OPCODE_SOCKET_RECV,
IanBenzMaxim 0:33d4e66780c0 946 (_SlArgSize_t)sizeof(_sendRecvCommand_t),
IanBenzMaxim 0:33d4e66780c0 947 (_SlArgSize_t)sizeof(_SocketResponse_t)
IanBenzMaxim 0:33d4e66780c0 948 };
IanBenzMaxim 0:33d4e66780c0 949
IanBenzMaxim 0:33d4e66780c0 950
IanBenzMaxim 0:33d4e66780c0 951 _i16 sl_Recv(_i16 sd, void *pBuf, _i16 Len, _i16 flags)
IanBenzMaxim 0:33d4e66780c0 952 {
IanBenzMaxim 0:33d4e66780c0 953 _SlRecvMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 954 _SlCmdExt_t CmdExt;
IanBenzMaxim 0:33d4e66780c0 955 _SlReturnVal_t status;
IanBenzMaxim 0:33d4e66780c0 956
IanBenzMaxim 0:33d4e66780c0 957 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 958 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 959 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 960
IanBenzMaxim 0:33d4e66780c0 961 _SlDrvResetCmdExt(&CmdExt);
IanBenzMaxim 0:33d4e66780c0 962 CmdExt.RxPayloadLen = Len;
IanBenzMaxim 0:33d4e66780c0 963 CmdExt.pRxPayload = (_u8 *)pBuf;
IanBenzMaxim 0:33d4e66780c0 964
IanBenzMaxim 0:33d4e66780c0 965 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 966 Msg.Cmd.StatusOrLen = (_u16)Len;
IanBenzMaxim 0:33d4e66780c0 967
IanBenzMaxim 0:33d4e66780c0 968 /* no size truncation in recv path */
IanBenzMaxim 0:33d4e66780c0 969 CmdExt.RxPayloadLen = (_i16)Msg.Cmd.StatusOrLen;
IanBenzMaxim 0:33d4e66780c0 970
IanBenzMaxim 0:33d4e66780c0 971 Msg.Cmd.FamilyAndFlags = (_u8)(flags & 0x0F);
IanBenzMaxim 0:33d4e66780c0 972
IanBenzMaxim 0:33d4e66780c0 973 status = _SlDrvDataReadOp((_SlSd_t)sd, (_SlCmdCtrl_t *)&_SlRecvCmdCtrl, &Msg, &CmdExt);
IanBenzMaxim 0:33d4e66780c0 974 if( status != SL_OS_RET_CODE_OK )
IanBenzMaxim 0:33d4e66780c0 975 {
IanBenzMaxim 0:33d4e66780c0 976 return status;
IanBenzMaxim 0:33d4e66780c0 977 }
IanBenzMaxim 0:33d4e66780c0 978
IanBenzMaxim 0:33d4e66780c0 979 /* if the Device side sends less than expected it is not the Driver's role */
IanBenzMaxim 0:33d4e66780c0 980 /* the returned value could be smaller than the requested size */
IanBenzMaxim 0:33d4e66780c0 981 return (_i16)Msg.Rsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 982 }
IanBenzMaxim 0:33d4e66780c0 983 #endif
IanBenzMaxim 0:33d4e66780c0 984
IanBenzMaxim 0:33d4e66780c0 985 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 986 /* sl_SetSockOpt */
IanBenzMaxim 0:33d4e66780c0 987 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 988 typedef union
IanBenzMaxim 0:33d4e66780c0 989 {
IanBenzMaxim 0:33d4e66780c0 990 _setSockOptCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 991 _SocketResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 992 }_SlSetSockOptMsg_u;
IanBenzMaxim 0:33d4e66780c0 993
IanBenzMaxim 0:33d4e66780c0 994 static const _SlCmdCtrl_t _SlSetSockOptCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 995 {
IanBenzMaxim 0:33d4e66780c0 996 SL_OPCODE_SOCKET_SETSOCKOPT,
IanBenzMaxim 0:33d4e66780c0 997 (_SlArgSize_t)sizeof(_setSockOptCommand_t),
IanBenzMaxim 0:33d4e66780c0 998 (_SlArgSize_t)sizeof(_SocketResponse_t)
IanBenzMaxim 0:33d4e66780c0 999 };
IanBenzMaxim 0:33d4e66780c0 1000
IanBenzMaxim 0:33d4e66780c0 1001 #if _SL_INCLUDE_FUNC(sl_SetSockOpt)
IanBenzMaxim 0:33d4e66780c0 1002 _i16 sl_SetSockOpt(_i16 sd, _i16 level, _i16 optname, const void *optval, SlSocklen_t optlen)
IanBenzMaxim 0:33d4e66780c0 1003 {
IanBenzMaxim 0:33d4e66780c0 1004 _SlSetSockOptMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 1005 _SlCmdExt_t CmdExt;
IanBenzMaxim 0:33d4e66780c0 1006
IanBenzMaxim 0:33d4e66780c0 1007 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 1008 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 1009 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 1010
IanBenzMaxim 0:33d4e66780c0 1011 _SlDrvResetCmdExt(&CmdExt);
IanBenzMaxim 0:33d4e66780c0 1012 CmdExt.TxPayloadLen = optlen;
IanBenzMaxim 0:33d4e66780c0 1013 CmdExt.pTxPayload = (_u8 *)optval;
IanBenzMaxim 0:33d4e66780c0 1014
IanBenzMaxim 0:33d4e66780c0 1015 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 1016 Msg.Cmd.level = (_u8)level;
IanBenzMaxim 0:33d4e66780c0 1017 Msg.Cmd.optionLen = (_u8)optlen;
IanBenzMaxim 0:33d4e66780c0 1018 Msg.Cmd.optionName = (_u8)optname;
IanBenzMaxim 0:33d4e66780c0 1019
IanBenzMaxim 0:33d4e66780c0 1020 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSetSockOptCmdCtrl, &Msg, &CmdExt));
IanBenzMaxim 0:33d4e66780c0 1021
IanBenzMaxim 0:33d4e66780c0 1022 return (_i16)Msg.Rsp.statusOrLen;
IanBenzMaxim 0:33d4e66780c0 1023 }
IanBenzMaxim 0:33d4e66780c0 1024 #endif
IanBenzMaxim 0:33d4e66780c0 1025
IanBenzMaxim 0:33d4e66780c0 1026 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1027 /* sl_GetSockOpt */
IanBenzMaxim 0:33d4e66780c0 1028 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1029 typedef union
IanBenzMaxim 0:33d4e66780c0 1030 {
IanBenzMaxim 0:33d4e66780c0 1031 _getSockOptCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 1032 _getSockOptResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 1033 }_SlGetSockOptMsg_u;
IanBenzMaxim 0:33d4e66780c0 1034
IanBenzMaxim 0:33d4e66780c0 1035
IanBenzMaxim 0:33d4e66780c0 1036 #if _SL_INCLUDE_FUNC(sl_GetSockOpt)
IanBenzMaxim 0:33d4e66780c0 1037
IanBenzMaxim 0:33d4e66780c0 1038 static const _SlCmdCtrl_t _SlGetSockOptCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 1039 {
IanBenzMaxim 0:33d4e66780c0 1040 SL_OPCODE_SOCKET_GETSOCKOPT,
IanBenzMaxim 0:33d4e66780c0 1041 (_SlArgSize_t)sizeof(_getSockOptCommand_t),
IanBenzMaxim 0:33d4e66780c0 1042 (_SlArgSize_t)sizeof(_getSockOptResponse_t)
IanBenzMaxim 0:33d4e66780c0 1043 };
IanBenzMaxim 0:33d4e66780c0 1044
IanBenzMaxim 0:33d4e66780c0 1045 _i16 sl_GetSockOpt(_i16 sd, _i16 level, _i16 optname, void *optval, SlSocklen_t *optlen)
IanBenzMaxim 0:33d4e66780c0 1046 {
IanBenzMaxim 0:33d4e66780c0 1047 _SlGetSockOptMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 1048 _SlCmdExt_t CmdExt;
IanBenzMaxim 0:33d4e66780c0 1049
IanBenzMaxim 0:33d4e66780c0 1050 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 1051 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 1052 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 1053 if (*optlen == 0)
IanBenzMaxim 0:33d4e66780c0 1054 {
IanBenzMaxim 0:33d4e66780c0 1055 return SL_EZEROLEN;
IanBenzMaxim 0:33d4e66780c0 1056 }
IanBenzMaxim 0:33d4e66780c0 1057
IanBenzMaxim 0:33d4e66780c0 1058 _SlDrvResetCmdExt(&CmdExt);
IanBenzMaxim 0:33d4e66780c0 1059 CmdExt.RxPayloadLen = (_i16)(*optlen);
IanBenzMaxim 0:33d4e66780c0 1060 CmdExt.pRxPayload = optval;
IanBenzMaxim 0:33d4e66780c0 1061
IanBenzMaxim 0:33d4e66780c0 1062 Msg.Cmd.sd = (_u8)sd;
IanBenzMaxim 0:33d4e66780c0 1063 Msg.Cmd.level = (_u8)level;
IanBenzMaxim 0:33d4e66780c0 1064 Msg.Cmd.optionLen = (_u8)(*optlen);
IanBenzMaxim 0:33d4e66780c0 1065 Msg.Cmd.optionName = (_u8)optname;
IanBenzMaxim 0:33d4e66780c0 1066
IanBenzMaxim 0:33d4e66780c0 1067 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlGetSockOptCmdCtrl, &Msg, &CmdExt));
IanBenzMaxim 0:33d4e66780c0 1068
IanBenzMaxim 0:33d4e66780c0 1069 if (CmdExt.RxPayloadLen < CmdExt.ActualRxPayloadLen)
IanBenzMaxim 0:33d4e66780c0 1070 {
IanBenzMaxim 0:33d4e66780c0 1071 *optlen = Msg.Rsp.optionLen;
IanBenzMaxim 0:33d4e66780c0 1072 return SL_ESMALLBUF;
IanBenzMaxim 0:33d4e66780c0 1073 }
IanBenzMaxim 0:33d4e66780c0 1074 else
IanBenzMaxim 0:33d4e66780c0 1075 {
IanBenzMaxim 0:33d4e66780c0 1076 *optlen = (_u8)CmdExt.ActualRxPayloadLen;
IanBenzMaxim 0:33d4e66780c0 1077 }
IanBenzMaxim 0:33d4e66780c0 1078 return (_i16)Msg.Rsp.status;
IanBenzMaxim 0:33d4e66780c0 1079 }
IanBenzMaxim 0:33d4e66780c0 1080 #endif
IanBenzMaxim 0:33d4e66780c0 1081
IanBenzMaxim 0:33d4e66780c0 1082 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1083 /* sl_Select */
IanBenzMaxim 0:33d4e66780c0 1084 /* ******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1085 typedef union
IanBenzMaxim 0:33d4e66780c0 1086 {
IanBenzMaxim 0:33d4e66780c0 1087 _SelectCommand_t Cmd;
IanBenzMaxim 0:33d4e66780c0 1088 _BasicResponse_t Rsp;
IanBenzMaxim 0:33d4e66780c0 1089 }_SlSelectMsg_u;
IanBenzMaxim 0:33d4e66780c0 1090
IanBenzMaxim 0:33d4e66780c0 1091
IanBenzMaxim 0:33d4e66780c0 1092
IanBenzMaxim 0:33d4e66780c0 1093 #ifndef SL_TINY_EXT
IanBenzMaxim 0:33d4e66780c0 1094 #if _SL_INCLUDE_FUNC(sl_Select)
IanBenzMaxim 0:33d4e66780c0 1095
IanBenzMaxim 0:33d4e66780c0 1096 static const _SlCmdCtrl_t _SlSelectCmdCtrl =
IanBenzMaxim 0:33d4e66780c0 1097 {
IanBenzMaxim 0:33d4e66780c0 1098 SL_OPCODE_SOCKET_SELECT,
IanBenzMaxim 0:33d4e66780c0 1099 (_SlArgSize_t)sizeof(_SelectCommand_t),
IanBenzMaxim 0:33d4e66780c0 1100 (_SlArgSize_t)sizeof(_BasicResponse_t)
IanBenzMaxim 0:33d4e66780c0 1101 };
IanBenzMaxim 0:33d4e66780c0 1102
IanBenzMaxim 0:33d4e66780c0 1103
IanBenzMaxim 0:33d4e66780c0 1104 _i16 sl_Select(_i16 nfds, SlFdSet_t *readsds, SlFdSet_t *writesds, SlFdSet_t *exceptsds, struct SlTimeval_t *timeout)
IanBenzMaxim 0:33d4e66780c0 1105 {
IanBenzMaxim 0:33d4e66780c0 1106 _SlSelectMsg_u Msg;
IanBenzMaxim 0:33d4e66780c0 1107 _SelectAsyncResponse_t AsyncRsp;
IanBenzMaxim 0:33d4e66780c0 1108 _u8 ObjIdx = MAX_CONCURRENT_ACTIONS;
IanBenzMaxim 0:33d4e66780c0 1109
IanBenzMaxim 0:33d4e66780c0 1110 /* verify no erorr handling in progress. if in progress than
IanBenzMaxim 0:33d4e66780c0 1111 ignore the API execution and return immediately with an error */
IanBenzMaxim 0:33d4e66780c0 1112 VERIFY_NO_ERROR_HANDLING_IN_PROGRESS();
IanBenzMaxim 0:33d4e66780c0 1113
IanBenzMaxim 0:33d4e66780c0 1114 Msg.Cmd.nfds = (_u8)nfds;
IanBenzMaxim 0:33d4e66780c0 1115 Msg.Cmd.readFdsCount = 0;
IanBenzMaxim 0:33d4e66780c0 1116 Msg.Cmd.writeFdsCount = 0;
IanBenzMaxim 0:33d4e66780c0 1117
IanBenzMaxim 0:33d4e66780c0 1118 Msg.Cmd.readFds = 0;
IanBenzMaxim 0:33d4e66780c0 1119 Msg.Cmd.writeFds = 0;
IanBenzMaxim 0:33d4e66780c0 1120
IanBenzMaxim 0:33d4e66780c0 1121
IanBenzMaxim 0:33d4e66780c0 1122 if( readsds )
IanBenzMaxim 0:33d4e66780c0 1123 {
IanBenzMaxim 0:33d4e66780c0 1124 Msg.Cmd.readFds = (_u16)readsds->fd_array[0];
IanBenzMaxim 0:33d4e66780c0 1125 }
IanBenzMaxim 0:33d4e66780c0 1126 if( writesds )
IanBenzMaxim 0:33d4e66780c0 1127 {
IanBenzMaxim 0:33d4e66780c0 1128 Msg.Cmd.writeFds = (_u16)writesds->fd_array[0];
IanBenzMaxim 0:33d4e66780c0 1129 }
IanBenzMaxim 0:33d4e66780c0 1130 if( NULL == timeout )
IanBenzMaxim 0:33d4e66780c0 1131 {
IanBenzMaxim 0:33d4e66780c0 1132 Msg.Cmd.tv_sec = 0xffff;
IanBenzMaxim 0:33d4e66780c0 1133 Msg.Cmd.tv_usec = 0xffff;
IanBenzMaxim 0:33d4e66780c0 1134 }
IanBenzMaxim 0:33d4e66780c0 1135 else
IanBenzMaxim 0:33d4e66780c0 1136 {
IanBenzMaxim 0:33d4e66780c0 1137 if( 0xffff <= timeout->tv_sec )
IanBenzMaxim 0:33d4e66780c0 1138 {
IanBenzMaxim 0:33d4e66780c0 1139 Msg.Cmd.tv_sec = 0xffff;
IanBenzMaxim 0:33d4e66780c0 1140 }
IanBenzMaxim 0:33d4e66780c0 1141 else
IanBenzMaxim 0:33d4e66780c0 1142 {
IanBenzMaxim 0:33d4e66780c0 1143 Msg.Cmd.tv_sec = (_u16)timeout->tv_sec;
IanBenzMaxim 0:33d4e66780c0 1144 }
IanBenzMaxim 0:33d4e66780c0 1145 timeout->tv_usec = timeout->tv_usec >> 10; /* convert to milliseconds */
IanBenzMaxim 0:33d4e66780c0 1146 if( 0xffff <= timeout->tv_usec )
IanBenzMaxim 0:33d4e66780c0 1147 {
IanBenzMaxim 0:33d4e66780c0 1148 Msg.Cmd.tv_usec = 0xffff;
IanBenzMaxim 0:33d4e66780c0 1149 }
IanBenzMaxim 0:33d4e66780c0 1150 else
IanBenzMaxim 0:33d4e66780c0 1151 {
IanBenzMaxim 0:33d4e66780c0 1152 Msg.Cmd.tv_usec = (_u16)timeout->tv_usec;
IanBenzMaxim 0:33d4e66780c0 1153 }
IanBenzMaxim 0:33d4e66780c0 1154 }
IanBenzMaxim 0:33d4e66780c0 1155
IanBenzMaxim 0:33d4e66780c0 1156 /* Use Obj to issue the command, if not available try later */
IanBenzMaxim 0:33d4e66780c0 1157 ObjIdx = _SlDrvProtectAsyncRespSetting((_u8*)&AsyncRsp, SELECT_ID, SL_MAX_SOCKETS);
IanBenzMaxim 0:33d4e66780c0 1158
IanBenzMaxim 0:33d4e66780c0 1159 if (MAX_CONCURRENT_ACTIONS == ObjIdx)
IanBenzMaxim 0:33d4e66780c0 1160 {
IanBenzMaxim 0:33d4e66780c0 1161 return SL_POOL_IS_EMPTY;
IanBenzMaxim 0:33d4e66780c0 1162 }
IanBenzMaxim 0:33d4e66780c0 1163
IanBenzMaxim 0:33d4e66780c0 1164
IanBenzMaxim 0:33d4e66780c0 1165 /* send the command */
IanBenzMaxim 0:33d4e66780c0 1166 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlSelectCmdCtrl, &Msg, NULL));
IanBenzMaxim 0:33d4e66780c0 1167
IanBenzMaxim 0:33d4e66780c0 1168 if(SL_OS_RET_CODE_OK == (_i16)Msg.Rsp.status)
IanBenzMaxim 0:33d4e66780c0 1169 {
IanBenzMaxim 0:33d4e66780c0 1170 SL_DRV_SYNC_OBJ_WAIT_FOREVER(&g_pCB->ObjPool[ObjIdx].SyncObj);
IanBenzMaxim 0:33d4e66780c0 1171
IanBenzMaxim 0:33d4e66780c0 1172 Msg.Rsp.status = (_i16)AsyncRsp.status;
IanBenzMaxim 0:33d4e66780c0 1173
IanBenzMaxim 0:33d4e66780c0 1174 if( ((_i16)Msg.Rsp.status) >= 0 )
IanBenzMaxim 0:33d4e66780c0 1175 {
IanBenzMaxim 0:33d4e66780c0 1176 if( readsds )
IanBenzMaxim 0:33d4e66780c0 1177 {
IanBenzMaxim 0:33d4e66780c0 1178 readsds->fd_array[0] = AsyncRsp.readFds;
IanBenzMaxim 0:33d4e66780c0 1179 }
IanBenzMaxim 0:33d4e66780c0 1180 if( writesds )
IanBenzMaxim 0:33d4e66780c0 1181 {
IanBenzMaxim 0:33d4e66780c0 1182 writesds->fd_array[0] = AsyncRsp.writeFds;
IanBenzMaxim 0:33d4e66780c0 1183 }
IanBenzMaxim 0:33d4e66780c0 1184 }
IanBenzMaxim 0:33d4e66780c0 1185 }
IanBenzMaxim 0:33d4e66780c0 1186
IanBenzMaxim 0:33d4e66780c0 1187 _SlDrvReleasePoolObj(ObjIdx);
IanBenzMaxim 0:33d4e66780c0 1188 return (_i16)Msg.Rsp.status;
IanBenzMaxim 0:33d4e66780c0 1189 }
IanBenzMaxim 0:33d4e66780c0 1190
IanBenzMaxim 0:33d4e66780c0 1191 /* Select helper functions */
IanBenzMaxim 0:33d4e66780c0 1192 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1193 /* SL_FD_SET */
IanBenzMaxim 0:33d4e66780c0 1194 /* ******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1195 void SL_FD_SET(_i16 fd, SlFdSet_t *fdset)
IanBenzMaxim 0:33d4e66780c0 1196 {
IanBenzMaxim 0:33d4e66780c0 1197 fdset->fd_array[0] |= (1<< (fd & BSD_SOCKET_ID_MASK));
IanBenzMaxim 0:33d4e66780c0 1198 }
IanBenzMaxim 0:33d4e66780c0 1199 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1200 /* SL_FD_CLR */
IanBenzMaxim 0:33d4e66780c0 1201 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1202 void SL_FD_CLR(_i16 fd, SlFdSet_t *fdset)
IanBenzMaxim 0:33d4e66780c0 1203 {
IanBenzMaxim 0:33d4e66780c0 1204 fdset->fd_array[0] &= ~(1<< (fd & BSD_SOCKET_ID_MASK));
IanBenzMaxim 0:33d4e66780c0 1205 }
IanBenzMaxim 0:33d4e66780c0 1206 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1207 /* SL_FD_ISSET */
IanBenzMaxim 0:33d4e66780c0 1208 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1209 _i16 SL_FD_ISSET(_i16 fd, SlFdSet_t *fdset)
IanBenzMaxim 0:33d4e66780c0 1210 {
IanBenzMaxim 0:33d4e66780c0 1211 if( fdset->fd_array[0] & (1<< (fd & BSD_SOCKET_ID_MASK)) )
IanBenzMaxim 0:33d4e66780c0 1212 {
IanBenzMaxim 0:33d4e66780c0 1213 return 1;
IanBenzMaxim 0:33d4e66780c0 1214 }
IanBenzMaxim 0:33d4e66780c0 1215 return 0;
IanBenzMaxim 0:33d4e66780c0 1216 }
IanBenzMaxim 0:33d4e66780c0 1217 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1218 /* SL_FD_ZERO */
IanBenzMaxim 0:33d4e66780c0 1219 /*******************************************************************************/
IanBenzMaxim 0:33d4e66780c0 1220 void SL_FD_ZERO(SlFdSet_t *fdset)
IanBenzMaxim 0:33d4e66780c0 1221 {
IanBenzMaxim 0:33d4e66780c0 1222 fdset->fd_array[0] = 0;
IanBenzMaxim 0:33d4e66780c0 1223 }
IanBenzMaxim 0:33d4e66780c0 1224
IanBenzMaxim 0:33d4e66780c0 1225 #endif
IanBenzMaxim 0:33d4e66780c0 1226 #endif
IanBenzMaxim 0:33d4e66780c0 1227
IanBenzMaxim 0:33d4e66780c0 1228
IanBenzMaxim 0:33d4e66780c0 1229