erez i / cc3000_hostdriver_mbedsocket

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3000_socket.h Source File

cc3000_socket.h

00001 /*****************************************************************************
00002 *
00003 *  C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
00004 *  Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
00005 *  provided help.
00006 *
00007 *  This version of "host driver" uses CC3000 Host Driver Implementation. Thus
00008 *  read the following copyright:
00009 *
00010 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00011 *
00012 *  Redistribution and use in source and binary forms, with or without
00013 *  modification, are permitted provided that the following conditions
00014 *  are met:
00015 *
00016 *    Redistributions of source code must retain the above copyright
00017 *    notice, this list of conditions and the following disclaimer.
00018 *
00019 *    Redistributions in binary form must reproduce the above copyright
00020 *    notice, this list of conditions and the following disclaimer in the
00021 *    documentation and/or other materials provided with the
00022 *    distribution.
00023 *
00024 *    Neither the name of Texas Instruments Incorporated nor the names of
00025 *    its contributors may be used to endorse or promote products derived
00026 *    from this software without specific prior written permission.
00027 *
00028 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039 *
00040 *****************************************************************************/
00041 #ifndef CC3000_SOCKET_H
00042 #define CC3000_SOCKET_H
00043 
00044 #define SOCKET_MAX_FREE_BUFFERS    6
00045 
00046 #define SOCKET_STATUS_ACTIVE       0
00047 #define SOCKET_STATUS_INACTIVE     1
00048 
00049 #define SOCKET_STATUS_INIT_VAL  0xFFFF
00050 #define M_IS_VALID_SD(sd) ((0 <= (sd)) && ((sd) <= 7))
00051 #define M_IS_VALID_STATUS(status) (((status) == SOCKET_STATUS_ACTIVE)||((status) == SOCKET_STATUS_INACTIVE))
00052 
00053 #ifdef _API_USE_BSD_CLOSE
00054     #define close(sd) closesocket(sd)
00055 #endif
00056 
00057 //Enable this flag if and only if you must comply with BSD socket read() and
00058 //write() functions
00059 #ifdef _API_USE_BSD_READ_WRITE
00060     #define read(sd, buf, len, flags) recv(sd, buf, len, flags)
00061     #define write(sd, buf, len, flags) send(sd, buf, len, flags)
00062 #endif
00063 
00064 #define SOCKET_OPEN_PARAMS_LEN                 (12)
00065 #define SOCKET_CLOSE_PARAMS_LEN                (4)
00066 #define SOCKET_ACCEPT_PARAMS_LEN               (4)
00067 #define SOCKET_BIND_PARAMS_LEN                 (20)
00068 #define SOCKET_LISTEN_PARAMS_LEN               (8)
00069 #define SOCKET_GET_HOST_BY_NAME_PARAMS_LEN     (9)
00070 #define SOCKET_CONNECT_PARAMS_LEN              (20)
00071 #define SOCKET_SELECT_PARAMS_LEN               (44)
00072 #define SOCKET_SET_SOCK_OPT_PARAMS_LEN         (20)
00073 #define SOCKET_GET_SOCK_OPT_PARAMS_LEN         (12)
00074 #define SOCKET_RECV_FROM_PARAMS_LEN            (12)
00075 #define SOCKET_SENDTO_PARAMS_LEN               (24)
00076 #define SOCKET_MDNS_ADVERTISE_PARAMS_LEN       (12)
00077 
00078 //#define NULL 0
00079 
00080 // The legnth of arguments for the SEND command: sd + buff_offset + len + flags,
00081 // while size of each parameter is 32 bit - so the total length is 16 bytes;
00082 
00083 #define HCI_CMND_SEND_ARG_LENGTH                    (16)
00084 #define SELECT_TIMEOUT_MIN_MICRO_SECONDS            5000
00085 #define HEADERS_SIZE_DATA                           (SPI_HEADER_SIZE + 5)
00086 #define SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE  (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE)
00087 #define MDNS_DEVICE_SERVICE_MAX_LENGTH              (32)
00088 
00089 
00090 #define HOSTNAME_MAX_LENGTH (230)  // 230 bytes + header shouldn't exceed 8 bit value
00091 
00092 //--------- Address Families --------
00093 
00094 #define  AF_INET                2
00095 #define  AF_INET6               23
00096 
00097 //------------ Socket Types ------------
00098 
00099 #define  SOCK_STREAM            1
00100 #define  SOCK_DGRAM             2
00101 #define  SOCK_RAW               3           // Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers
00102 #define  SOCK_RDM               4
00103 #define  SOCK_SEQPACKET         5
00104 
00105 //----------- Socket Protocol ----------
00106 
00107 #define IPPROTO_IP              0           // dummy for IP
00108 #define IPPROTO_ICMP            1           // control message protocol
00109 #define IPPROTO_IPV4            IPPROTO_IP  // IP inside IP
00110 #define IPPROTO_TCP             6           // tcp
00111 #define IPPROTO_UDP             17          // user datagram protocol
00112 #define IPPROTO_IPV6            41          // IPv6 in IPv6
00113 #define IPPROTO_NONE            59          // No next header
00114 #define IPPROTO_RAW             255         // raw IP packet
00115 #define IPPROTO_MAX             256
00116 
00117 //----------- Socket retunr codes  -----------
00118 
00119 #define SOC_ERROR                (-1)        // error
00120 #define SOC_IN_PROGRESS          (-2)        // socket in progress
00121 
00122 //----------- Socket Options -----------
00123 #define  SOL_SOCKET             0xffff       //  socket level
00124 #define  SOCKOPT_RECV_TIMEOUT   1            //  optname to configure recv and recvfromtimeout
00125 #define  SOCKOPT_NONBLOCK       2            // accept non block mode set SOCK_ON or SOCK_OFF (default block mode )
00126 #define  SOCK_ON                0            // socket non-blocking mode    is enabled
00127 #define  SOCK_OFF               1            // socket blocking mode is enabled
00128 
00129 #define  TCP_NODELAY            0x0001
00130 #define  TCP_BSDURGENT          0x7000
00131 
00132 #define  MAX_PACKET_SIZE        1500
00133 #define  MAX_LISTEN_QUEUE       4
00134 
00135 #define  IOCTL_SOCKET_EVENTMASK
00136 
00137 #define __FD_SETSIZE            32
00138 
00139 #define  ASIC_ADDR_LEN          8
00140 
00141 #define NO_QUERY_RECIVED        -3
00142 
00143 
00144 typedef struct _in_addr_t
00145 {
00146     uint32_t s_addr;                   // load with inet_aton()
00147 } in_addr;
00148 
00149 /*typedef struct _sockaddr_t
00150 {
00151     unsigned short int  sa_family;
00152     unsigned char       sa_data[14];
00153 } sockaddr;*/
00154 
00155 typedef struct _sockaddr_in_t
00156 {
00157     int16_t  sin_family;            // e.g. AF_INET
00158     uint16_t sin_port;              // e.g. htons(3490)
00159     in_addr  sin_addr;              // see struct in_addr, below
00160     uint8_t  sin_zero[8];           // zero this if you want to
00161 } sockaddr_in;
00162 
00163 typedef uint32_t socklen_t;
00164 
00165 // The fd_set member is required to be an array of longs.
00166 typedef int32_t __fd_mask;
00167 
00168 // It's easier to assume 8-bit bytes than to get CHAR_BIT.
00169 #define __NFDBITS               (8 * sizeof (__fd_mask))
00170 #define __FDELT(d)              ((d) / __NFDBITS)
00171 #define __FDMASK(d)             ((__fd_mask) 1 << ((d) % __NFDBITS))
00172 
00173 #ifndef FD_SET
00174 //not used in the current code
00175 #define ENOBUFS                 55          // No buffer space available
00176 
00177 // Access macros for 'fd_set'.
00178 #define FD_SET(fd, fdsetp)      __FD_SET (fd, fdsetp)
00179 #define FD_CLR(fd, fdsetp)      __FD_CLR (fd, fdsetp)
00180 #define FD_ISSET(fd, fdsetp)    __FD_ISSET (fd, fdsetp)
00181 #define FD_ZERO(fdsetp)         __FD_ZERO (fdsetp)
00182 
00183 // fd_set for select and pselect.
00184 typedef struct
00185 {
00186     __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
00187 #define __FDS_BITS(set)        ((set)->fds_bits)
00188 } fd_set;
00189 
00190 #endif /* FD_SET */
00191 
00192 // We don't use `memset' because this would require a prototype and
00193 //   the array isn't too big.
00194 #define __FD_ZERO(set)                               \
00195   do {                                                \
00196     uint32_t __i;                                 \
00197     fd_set *__arr = (set);                            \
00198     for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) \
00199       __FDS_BITS (__arr)[__i] = 0;                    \
00200   } while (0)
00201 #define __FD_SET(d, set)       (__FDS_BITS (set)[__FDELT (d)] |= __FDMASK (d))
00202 #define __FD_CLR(d, set)       (__FDS_BITS (set)[__FDELT (d)] &= ~__FDMASK (d))
00203 #define __FD_ISSET(d, set)     (__FDS_BITS (set)[__FDELT (d)] & __FDMASK (d))
00204 
00205 //Use in case of Big Endian only
00206 
00207 #define htonl(A)    ((((uint32_t)(A) & 0xff000000) >> 24) | \
00208                      (((uint32_t)(A) & 0x00ff0000) >> 8) | \
00209                      (((uint32_t)(A) & 0x0000ff00) << 8) | \
00210                      (((uint32_t)(A) & 0x000000ff) << 24))
00211 
00212 #define ntohl                   htonl
00213 
00214 //Use in case of Big Endian only
00215 #define htons(A)     ((((uint32_t)(A) & 0xff00) >> 8) | \
00216                       (((uint32_t)(A) & 0x00ff) << 8))
00217 
00218 
00219 #define ntohs                   htons
00220 
00221 // mDNS port - 5353    mDNS multicast address - 224.0.0.251
00222 #define SET_mDNS_ADD(sockaddr) sockaddr.sa_data[0] = 0x14; \
00223                                sockaddr.sa_data[1] = 0xe9; \
00224                                sockaddr.sa_data[2] = 0xe0; \
00225                                sockaddr.sa_data[3] = 0x0;  \
00226                                sockaddr.sa_data[4] = 0x0;  \
00227                                sockaddr.sa_data[5] = 0xfb;
00228 
00229 #endif