version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
Kojto
Date:
Thu Sep 19 07:55:14 2013 +0000
Revision:
0:615c697c33b0
Child:
4:15b58c119a0a
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:615c697c33b0 1 /*****************************************************************************
Kojto 0:615c697c33b0 2 *
Kojto 0:615c697c33b0 3 * C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
Kojto 0:615c697c33b0 4 * Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
Kojto 0:615c697c33b0 5 * provided help.
Kojto 0:615c697c33b0 6 *
Kojto 0:615c697c33b0 7 * This version of "host driver" uses CC3000 Host Driver Implementation. Thus
Kojto 0:615c697c33b0 8 * read the following copyright:
Kojto 0:615c697c33b0 9 *
Kojto 0:615c697c33b0 10 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Kojto 0:615c697c33b0 11 *
Kojto 0:615c697c33b0 12 * Redistribution and use in source and binary forms, with or without
Kojto 0:615c697c33b0 13 * modification, are permitted provided that the following conditions
Kojto 0:615c697c33b0 14 * are met:
Kojto 0:615c697c33b0 15 *
Kojto 0:615c697c33b0 16 * Redistributions of source code must retain the above copyright
Kojto 0:615c697c33b0 17 * notice, this list of conditions and the following disclaimer.
Kojto 0:615c697c33b0 18 *
Kojto 0:615c697c33b0 19 * Redistributions in binary form must reproduce the above copyright
Kojto 0:615c697c33b0 20 * notice, this list of conditions and the following disclaimer in the
Kojto 0:615c697c33b0 21 * documentation and/or other materials provided with the
Kojto 0:615c697c33b0 22 * distribution.
Kojto 0:615c697c33b0 23 *
Kojto 0:615c697c33b0 24 * Neither the name of Texas Instruments Incorporated nor the names of
Kojto 0:615c697c33b0 25 * its contributors may be used to endorse or promote products derived
Kojto 0:615c697c33b0 26 * from this software without specific prior written permission.
Kojto 0:615c697c33b0 27 *
Kojto 0:615c697c33b0 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Kojto 0:615c697c33b0 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Kojto 0:615c697c33b0 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Kojto 0:615c697c33b0 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Kojto 0:615c697c33b0 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Kojto 0:615c697c33b0 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Kojto 0:615c697c33b0 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Kojto 0:615c697c33b0 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Kojto 0:615c697c33b0 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kojto 0:615c697c33b0 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Kojto 0:615c697c33b0 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 0:615c697c33b0 39 *
Kojto 0:615c697c33b0 40 *****************************************************************************/
Kojto 0:615c697c33b0 41 #ifndef CC3000_SOCKET_H
Kojto 0:615c697c33b0 42 #define CC3000_SOCKET_H
Kojto 0:615c697c33b0 43
Kojto 0:615c697c33b0 44 #define SOCKET_STATUS_ACTIVE 0
Kojto 0:615c697c33b0 45 #define SOCKET_STATUS_INACTIVE 1
Kojto 0:615c697c33b0 46
Kojto 0:615c697c33b0 47 #define SOCKET_STATUS_INIT_VAL 0xFFFF
Kojto 0:615c697c33b0 48 #define M_IS_VALID_SD(sd) ((0 <= (sd)) && ((sd) <= 7))
Kojto 0:615c697c33b0 49 #define M_IS_VALID_STATUS(status) (((status) == SOCKET_STATUS_ACTIVE)||((status) == SOCKET_STATUS_INACTIVE))
Kojto 0:615c697c33b0 50
Kojto 0:615c697c33b0 51 #ifdef _API_USE_BSD_CLOSE
Kojto 0:615c697c33b0 52 #define close(sd) closesocket(sd)
Kojto 0:615c697c33b0 53 #endif
Kojto 0:615c697c33b0 54
Kojto 0:615c697c33b0 55 //Enable this flag if and only if you must comply with BSD socket read() and
Kojto 0:615c697c33b0 56 //write() functions
Kojto 0:615c697c33b0 57 #ifdef _API_USE_BSD_READ_WRITE
Kojto 0:615c697c33b0 58 #define read(sd, buf, len, flags) recv(sd, buf, len, flags)
Kojto 0:615c697c33b0 59 #define write(sd, buf, len, flags) send(sd, buf, len, flags)
Kojto 0:615c697c33b0 60 #endif
Kojto 0:615c697c33b0 61
Kojto 0:615c697c33b0 62 #define SOCKET_OPEN_PARAMS_LEN (12)
Kojto 0:615c697c33b0 63 #define SOCKET_CLOSE_PARAMS_LEN (4)
Kojto 0:615c697c33b0 64 #define SOCKET_ACCEPT_PARAMS_LEN (4)
Kojto 0:615c697c33b0 65 #define SOCKET_BIND_PARAMS_LEN (20)
Kojto 0:615c697c33b0 66 #define SOCKET_LISTEN_PARAMS_LEN (8)
Kojto 0:615c697c33b0 67 #define SOCKET_GET_HOST_BY_NAME_PARAMS_LEN (9)
Kojto 0:615c697c33b0 68 #define SOCKET_CONNECT_PARAMS_LEN (20)
Kojto 0:615c697c33b0 69 #define SOCKET_SELECT_PARAMS_LEN (44)
Kojto 0:615c697c33b0 70 #define SOCKET_SET_SOCK_OPT_PARAMS_LEN (20)
Kojto 0:615c697c33b0 71 #define SOCKET_GET_SOCK_OPT_PARAMS_LEN (12)
Kojto 0:615c697c33b0 72 #define SOCKET_RECV_FROM_PARAMS_LEN (12)
Kojto 0:615c697c33b0 73 #define SOCKET_SENDTO_PARAMS_LEN (24)
Kojto 0:615c697c33b0 74 #define SOCKET_MDNS_ADVERTISE_PARAMS_LEN (12)
Kojto 0:615c697c33b0 75
Kojto 0:615c697c33b0 76 //#define NULL 0
Kojto 0:615c697c33b0 77
Kojto 0:615c697c33b0 78 // The legnth of arguments for the SEND command: sd + buff_offset + len + flags,
Kojto 0:615c697c33b0 79 // while size of each parameter is 32 bit - so the total length is 16 bytes;
Kojto 0:615c697c33b0 80
Kojto 0:615c697c33b0 81 #define HCI_CMND_SEND_ARG_LENGTH (16)
Kojto 0:615c697c33b0 82 #define SELECT_TIMEOUT_MIN_MICRO_SECONDS 5000
Kojto 0:615c697c33b0 83 #define HEADERS_SIZE_DATA (SPI_HEADER_SIZE + 5)
Kojto 0:615c697c33b0 84 #define SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE)
Kojto 0:615c697c33b0 85 #define MDNS_DEVICE_SERVICE_MAX_LENGTH (32)
Kojto 0:615c697c33b0 86
Kojto 0:615c697c33b0 87
Kojto 0:615c697c33b0 88 #define HOSTNAME_MAX_LENGTH (230) // 230 bytes + header shouldn't exceed 8 bit value
Kojto 0:615c697c33b0 89
Kojto 0:615c697c33b0 90 //--------- Address Families --------
Kojto 0:615c697c33b0 91
Kojto 0:615c697c33b0 92 #define AF_INET 2
Kojto 0:615c697c33b0 93 #define AF_INET6 23
Kojto 0:615c697c33b0 94
Kojto 0:615c697c33b0 95 //------------ Socket Types ------------
Kojto 0:615c697c33b0 96
Kojto 0:615c697c33b0 97 #define SOCK_STREAM 1
Kojto 0:615c697c33b0 98 #define SOCK_DGRAM 2
Kojto 0:615c697c33b0 99 #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
Kojto 0:615c697c33b0 100 #define SOCK_RDM 4
Kojto 0:615c697c33b0 101 #define SOCK_SEQPACKET 5
Kojto 0:615c697c33b0 102
Kojto 0:615c697c33b0 103 //----------- Socket Protocol ----------
Kojto 0:615c697c33b0 104
Kojto 0:615c697c33b0 105 #define IPPROTO_IP 0 // dummy for IP
Kojto 0:615c697c33b0 106 #define IPPROTO_ICMP 1 // control message protocol
Kojto 0:615c697c33b0 107 #define IPPROTO_IPV4 IPPROTO_IP // IP inside IP
Kojto 0:615c697c33b0 108 #define IPPROTO_TCP 6 // tcp
Kojto 0:615c697c33b0 109 #define IPPROTO_UDP 17 // user datagram protocol
Kojto 0:615c697c33b0 110 #define IPPROTO_IPV6 41 // IPv6 in IPv6
Kojto 0:615c697c33b0 111 #define IPPROTO_NONE 59 // No next header
Kojto 0:615c697c33b0 112 #define IPPROTO_RAW 255 // raw IP packet
Kojto 0:615c697c33b0 113 #define IPPROTO_MAX 256
Kojto 0:615c697c33b0 114
Kojto 0:615c697c33b0 115 //----------- Socket retunr codes -----------
Kojto 0:615c697c33b0 116
Kojto 0:615c697c33b0 117 #define SOC_ERROR (-1) // error
Kojto 0:615c697c33b0 118 #define SOC_IN_PROGRESS (-2) // socket in progress
Kojto 0:615c697c33b0 119
Kojto 0:615c697c33b0 120 //----------- Socket Options -----------
Kojto 0:615c697c33b0 121 #define SOL_SOCKET 0xffff // socket level
Kojto 0:615c697c33b0 122 #define SOCKOPT_RECV_TIMEOUT 1 // optname to configure recv and recvfromtimeout
Kojto 0:615c697c33b0 123 #define SOCKOPT_NONBLOCK 2 // accept non block mode set SOCK_ON or SOCK_OFF (default block mode )
Kojto 0:615c697c33b0 124 #define SOCK_ON 0 // socket non-blocking mode is enabled
Kojto 0:615c697c33b0 125 #define SOCK_OFF 1 // socket blocking mode is enabled
Kojto 0:615c697c33b0 126
Kojto 0:615c697c33b0 127 #define TCP_NODELAY 0x0001
Kojto 0:615c697c33b0 128 #define TCP_BSDURGENT 0x7000
Kojto 0:615c697c33b0 129
Kojto 0:615c697c33b0 130 #define MAX_PACKET_SIZE 1500
Kojto 0:615c697c33b0 131 #define MAX_LISTEN_QUEUE 4
Kojto 0:615c697c33b0 132
Kojto 0:615c697c33b0 133 #define IOCTL_SOCKET_EVENTMASK
Kojto 0:615c697c33b0 134
Kojto 0:615c697c33b0 135 #define ENOBUFS 55 // No buffer space available
Kojto 0:615c697c33b0 136
Kojto 0:615c697c33b0 137 #define __FD_SETSIZE 32
Kojto 0:615c697c33b0 138
Kojto 0:615c697c33b0 139 #define ASIC_ADDR_LEN 8
Kojto 0:615c697c33b0 140
Kojto 0:615c697c33b0 141 #define NO_QUERY_RECIVED -3
Kojto 0:615c697c33b0 142
Kojto 0:615c697c33b0 143
Kojto 0:615c697c33b0 144 typedef struct _in_addr_t
Kojto 0:615c697c33b0 145 {
Kojto 0:615c697c33b0 146 uint32_t s_addr; // load with inet_aton()
Kojto 0:615c697c33b0 147 } in_addr;
Kojto 0:615c697c33b0 148
Kojto 0:615c697c33b0 149 /*typedef struct _sockaddr_t
Kojto 0:615c697c33b0 150 {
Kojto 0:615c697c33b0 151 unsigned short int sa_family;
Kojto 0:615c697c33b0 152 unsigned char sa_data[14];
Kojto 0:615c697c33b0 153 } sockaddr;*/
Kojto 0:615c697c33b0 154
Kojto 0:615c697c33b0 155 typedef struct _sockaddr_in_t
Kojto 0:615c697c33b0 156 {
Kojto 0:615c697c33b0 157 int16_t sin_family; // e.g. AF_INET
Kojto 0:615c697c33b0 158 uint16_t sin_port; // e.g. htons(3490)
Kojto 0:615c697c33b0 159 in_addr sin_addr; // see struct in_addr, below
Kojto 0:615c697c33b0 160 uint8_t sin_zero[8]; // zero this if you want to
Kojto 0:615c697c33b0 161 } sockaddr_in;
Kojto 0:615c697c33b0 162
Kojto 0:615c697c33b0 163 typedef uint32_t socklen_t;
Kojto 0:615c697c33b0 164
Kojto 0:615c697c33b0 165 // The fd_set member is required to be an array of longs.
Kojto 0:615c697c33b0 166 typedef int32_t __fd_mask;
Kojto 0:615c697c33b0 167
Kojto 0:615c697c33b0 168 // It's easier to assume 8-bit bytes than to get CHAR_BIT.
Kojto 0:615c697c33b0 169 #define __NFDBITS (8 * sizeof (__fd_mask))
Kojto 0:615c697c33b0 170 #define __FDELT(d) ((d) / __NFDBITS)
Kojto 0:615c697c33b0 171 #define __FDMASK(d) ((__fd_mask) 1 << ((d) % __NFDBITS))
Kojto 0:615c697c33b0 172
Kojto 0:615c697c33b0 173 // fd_set for select and pselect.
Kojto 0:615c697c33b0 174 typedef struct
Kojto 0:615c697c33b0 175 {
Kojto 0:615c697c33b0 176 __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
Kojto 0:615c697c33b0 177 #define __FDS_BITS(set) ((set)->fds_bits)
Kojto 0:615c697c33b0 178 } fd_set;
Kojto 0:615c697c33b0 179
Kojto 0:615c697c33b0 180 // We don't use `memset' because this would require a prototype and
Kojto 0:615c697c33b0 181 // the array isn't too big.
Kojto 0:615c697c33b0 182 #define __FD_ZERO(set) \
Kojto 0:615c697c33b0 183 do { \
Kojto 0:615c697c33b0 184 uint32_t __i; \
Kojto 0:615c697c33b0 185 fd_set *__arr = (set); \
Kojto 0:615c697c33b0 186 for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) \
Kojto 0:615c697c33b0 187 __FDS_BITS (__arr)[__i] = 0; \
Kojto 0:615c697c33b0 188 } while (0)
Kojto 0:615c697c33b0 189 #define __FD_SET(d, set) (__FDS_BITS (set)[__FDELT (d)] |= __FDMASK (d))
Kojto 0:615c697c33b0 190 #define __FD_CLR(d, set) (__FDS_BITS (set)[__FDELT (d)] &= ~__FDMASK (d))
Kojto 0:615c697c33b0 191 #define __FD_ISSET(d, set) (__FDS_BITS (set)[__FDELT (d)] & __FDMASK (d))
Kojto 0:615c697c33b0 192
Kojto 0:615c697c33b0 193 // Access macros for 'fd_set'.
Kojto 0:615c697c33b0 194 #define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp)
Kojto 0:615c697c33b0 195 #define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp)
Kojto 0:615c697c33b0 196 #define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp)
Kojto 0:615c697c33b0 197 #define FD_ZERO(fdsetp) __FD_ZERO (fdsetp)
Kojto 0:615c697c33b0 198
Kojto 0:615c697c33b0 199 //Use in case of Big Endian only
Kojto 0:615c697c33b0 200
Kojto 0:615c697c33b0 201 #define htonl(A) ((((uint32_t)(A) & 0xff000000) >> 24) | \
Kojto 0:615c697c33b0 202 (((uint32_t)(A) & 0x00ff0000) >> 8) | \
Kojto 0:615c697c33b0 203 (((uint32_t)(A) & 0x0000ff00) << 8) | \
Kojto 0:615c697c33b0 204 (((uint32_t)(A) & 0x000000ff) << 24))
Kojto 0:615c697c33b0 205
Kojto 0:615c697c33b0 206 #define ntohl htonl
Kojto 0:615c697c33b0 207
Kojto 0:615c697c33b0 208 //Use in case of Big Endian only
Kojto 0:615c697c33b0 209 #define htons(A) ((((uint32_t)(A) & 0xff00) >> 8) | \
Kojto 0:615c697c33b0 210 (((uint32_t)(A) & 0x00ff) << 8))
Kojto 0:615c697c33b0 211
Kojto 0:615c697c33b0 212
Kojto 0:615c697c33b0 213 #define ntohs htons
Kojto 0:615c697c33b0 214
Kojto 0:615c697c33b0 215 // mDNS port - 5353 mDNS multicast address - 224.0.0.251
Kojto 0:615c697c33b0 216 #define SET_mDNS_ADD(sockaddr) sockaddr.sa_data[0] = 0x14; \
Kojto 0:615c697c33b0 217 sockaddr.sa_data[1] = 0xe9; \
Kojto 0:615c697c33b0 218 sockaddr.sa_data[2] = 0xe0; \
Kojto 0:615c697c33b0 219 sockaddr.sa_data[3] = 0x0; \
Kojto 0:615c697c33b0 220 sockaddr.sa_data[4] = 0x0; \
Kojto 0:615c697c33b0 221 sockaddr.sa_data[5] = 0xfb;
Kojto 0:615c697c33b0 222
Kojto 0:615c697c33b0 223 #endif