version_2.0
Dependents: cc3000_ping_demo_try_2
Fork of cc3000_hostdriver_mbedsocket by
cc3000_socket.h@4:15b58c119a0a, 2013-09-21 (annotated)
- Committer:
- Kojto
- Date:
- Sat Sep 21 15:01:05 2013 +0000
- Revision:
- 4:15b58c119a0a
- Parent:
- 0:615c697c33b0
- Child:
- 20:30b6ed7bf8fd
mbed socket interface changes; - socket init (creates upd/tcp socket); - debug prints; host driver changes; - blocking call when closing socket (Frank's recommedation); - delete profiles (user info retrieved, then erased)
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:15b58c119a0a | 44 | #define SOCKET_MAX_FREE_BUFFERS 6 |
Kojto | 4:15b58c119a0a | 45 | |
Kojto | 0:615c697c33b0 | 46 | #define SOCKET_STATUS_ACTIVE 0 |
Kojto | 0:615c697c33b0 | 47 | #define SOCKET_STATUS_INACTIVE 1 |
Kojto | 0:615c697c33b0 | 48 | |
Kojto | 0:615c697c33b0 | 49 | #define SOCKET_STATUS_INIT_VAL 0xFFFF |
Kojto | 0:615c697c33b0 | 50 | #define M_IS_VALID_SD(sd) ((0 <= (sd)) && ((sd) <= 7)) |
Kojto | 0:615c697c33b0 | 51 | #define M_IS_VALID_STATUS(status) (((status) == SOCKET_STATUS_ACTIVE)||((status) == SOCKET_STATUS_INACTIVE)) |
Kojto | 0:615c697c33b0 | 52 | |
Kojto | 0:615c697c33b0 | 53 | #ifdef _API_USE_BSD_CLOSE |
Kojto | 0:615c697c33b0 | 54 | #define close(sd) closesocket(sd) |
Kojto | 0:615c697c33b0 | 55 | #endif |
Kojto | 0:615c697c33b0 | 56 | |
Kojto | 0:615c697c33b0 | 57 | //Enable this flag if and only if you must comply with BSD socket read() and |
Kojto | 0:615c697c33b0 | 58 | //write() functions |
Kojto | 0:615c697c33b0 | 59 | #ifdef _API_USE_BSD_READ_WRITE |
Kojto | 0:615c697c33b0 | 60 | #define read(sd, buf, len, flags) recv(sd, buf, len, flags) |
Kojto | 0:615c697c33b0 | 61 | #define write(sd, buf, len, flags) send(sd, buf, len, flags) |
Kojto | 0:615c697c33b0 | 62 | #endif |
Kojto | 0:615c697c33b0 | 63 | |
Kojto | 0:615c697c33b0 | 64 | #define SOCKET_OPEN_PARAMS_LEN (12) |
Kojto | 0:615c697c33b0 | 65 | #define SOCKET_CLOSE_PARAMS_LEN (4) |
Kojto | 0:615c697c33b0 | 66 | #define SOCKET_ACCEPT_PARAMS_LEN (4) |
Kojto | 0:615c697c33b0 | 67 | #define SOCKET_BIND_PARAMS_LEN (20) |
Kojto | 0:615c697c33b0 | 68 | #define SOCKET_LISTEN_PARAMS_LEN (8) |
Kojto | 0:615c697c33b0 | 69 | #define SOCKET_GET_HOST_BY_NAME_PARAMS_LEN (9) |
Kojto | 0:615c697c33b0 | 70 | #define SOCKET_CONNECT_PARAMS_LEN (20) |
Kojto | 0:615c697c33b0 | 71 | #define SOCKET_SELECT_PARAMS_LEN (44) |
Kojto | 0:615c697c33b0 | 72 | #define SOCKET_SET_SOCK_OPT_PARAMS_LEN (20) |
Kojto | 0:615c697c33b0 | 73 | #define SOCKET_GET_SOCK_OPT_PARAMS_LEN (12) |
Kojto | 0:615c697c33b0 | 74 | #define SOCKET_RECV_FROM_PARAMS_LEN (12) |
Kojto | 0:615c697c33b0 | 75 | #define SOCKET_SENDTO_PARAMS_LEN (24) |
Kojto | 0:615c697c33b0 | 76 | #define SOCKET_MDNS_ADVERTISE_PARAMS_LEN (12) |
Kojto | 0:615c697c33b0 | 77 | |
Kojto | 0:615c697c33b0 | 78 | //#define NULL 0 |
Kojto | 0:615c697c33b0 | 79 | |
Kojto | 0:615c697c33b0 | 80 | // The legnth of arguments for the SEND command: sd + buff_offset + len + flags, |
Kojto | 0:615c697c33b0 | 81 | // while size of each parameter is 32 bit - so the total length is 16 bytes; |
Kojto | 0:615c697c33b0 | 82 | |
Kojto | 0:615c697c33b0 | 83 | #define HCI_CMND_SEND_ARG_LENGTH (16) |
Kojto | 0:615c697c33b0 | 84 | #define SELECT_TIMEOUT_MIN_MICRO_SECONDS 5000 |
Kojto | 0:615c697c33b0 | 85 | #define HEADERS_SIZE_DATA (SPI_HEADER_SIZE + 5) |
Kojto | 0:615c697c33b0 | 86 | #define SIMPLE_LINK_HCI_CMND_TRANSPORT_HEADER_SIZE (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE) |
Kojto | 0:615c697c33b0 | 87 | #define MDNS_DEVICE_SERVICE_MAX_LENGTH (32) |
Kojto | 0:615c697c33b0 | 88 | |
Kojto | 0:615c697c33b0 | 89 | |
Kojto | 0:615c697c33b0 | 90 | #define HOSTNAME_MAX_LENGTH (230) // 230 bytes + header shouldn't exceed 8 bit value |
Kojto | 0:615c697c33b0 | 91 | |
Kojto | 0:615c697c33b0 | 92 | //--------- Address Families -------- |
Kojto | 0:615c697c33b0 | 93 | |
Kojto | 0:615c697c33b0 | 94 | #define AF_INET 2 |
Kojto | 0:615c697c33b0 | 95 | #define AF_INET6 23 |
Kojto | 0:615c697c33b0 | 96 | |
Kojto | 0:615c697c33b0 | 97 | //------------ Socket Types ------------ |
Kojto | 0:615c697c33b0 | 98 | |
Kojto | 0:615c697c33b0 | 99 | #define SOCK_STREAM 1 |
Kojto | 0:615c697c33b0 | 100 | #define SOCK_DGRAM 2 |
Kojto | 0:615c697c33b0 | 101 | #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 | 102 | #define SOCK_RDM 4 |
Kojto | 0:615c697c33b0 | 103 | #define SOCK_SEQPACKET 5 |
Kojto | 0:615c697c33b0 | 104 | |
Kojto | 0:615c697c33b0 | 105 | //----------- Socket Protocol ---------- |
Kojto | 0:615c697c33b0 | 106 | |
Kojto | 0:615c697c33b0 | 107 | #define IPPROTO_IP 0 // dummy for IP |
Kojto | 0:615c697c33b0 | 108 | #define IPPROTO_ICMP 1 // control message protocol |
Kojto | 0:615c697c33b0 | 109 | #define IPPROTO_IPV4 IPPROTO_IP // IP inside IP |
Kojto | 0:615c697c33b0 | 110 | #define IPPROTO_TCP 6 // tcp |
Kojto | 0:615c697c33b0 | 111 | #define IPPROTO_UDP 17 // user datagram protocol |
Kojto | 0:615c697c33b0 | 112 | #define IPPROTO_IPV6 41 // IPv6 in IPv6 |
Kojto | 0:615c697c33b0 | 113 | #define IPPROTO_NONE 59 // No next header |
Kojto | 0:615c697c33b0 | 114 | #define IPPROTO_RAW 255 // raw IP packet |
Kojto | 0:615c697c33b0 | 115 | #define IPPROTO_MAX 256 |
Kojto | 0:615c697c33b0 | 116 | |
Kojto | 0:615c697c33b0 | 117 | //----------- Socket retunr codes ----------- |
Kojto | 0:615c697c33b0 | 118 | |
Kojto | 0:615c697c33b0 | 119 | #define SOC_ERROR (-1) // error |
Kojto | 0:615c697c33b0 | 120 | #define SOC_IN_PROGRESS (-2) // socket in progress |
Kojto | 0:615c697c33b0 | 121 | |
Kojto | 0:615c697c33b0 | 122 | //----------- Socket Options ----------- |
Kojto | 0:615c697c33b0 | 123 | #define SOL_SOCKET 0xffff // socket level |
Kojto | 0:615c697c33b0 | 124 | #define SOCKOPT_RECV_TIMEOUT 1 // optname to configure recv and recvfromtimeout |
Kojto | 0:615c697c33b0 | 125 | #define SOCKOPT_NONBLOCK 2 // accept non block mode set SOCK_ON or SOCK_OFF (default block mode ) |
Kojto | 0:615c697c33b0 | 126 | #define SOCK_ON 0 // socket non-blocking mode is enabled |
Kojto | 0:615c697c33b0 | 127 | #define SOCK_OFF 1 // socket blocking mode is enabled |
Kojto | 0:615c697c33b0 | 128 | |
Kojto | 0:615c697c33b0 | 129 | #define TCP_NODELAY 0x0001 |
Kojto | 0:615c697c33b0 | 130 | #define TCP_BSDURGENT 0x7000 |
Kojto | 0:615c697c33b0 | 131 | |
Kojto | 0:615c697c33b0 | 132 | #define MAX_PACKET_SIZE 1500 |
Kojto | 0:615c697c33b0 | 133 | #define MAX_LISTEN_QUEUE 4 |
Kojto | 0:615c697c33b0 | 134 | |
Kojto | 0:615c697c33b0 | 135 | #define IOCTL_SOCKET_EVENTMASK |
Kojto | 0:615c697c33b0 | 136 | |
Kojto | 0:615c697c33b0 | 137 | #define ENOBUFS 55 // No buffer space available |
Kojto | 0:615c697c33b0 | 138 | |
Kojto | 0:615c697c33b0 | 139 | #define __FD_SETSIZE 32 |
Kojto | 0:615c697c33b0 | 140 | |
Kojto | 0:615c697c33b0 | 141 | #define ASIC_ADDR_LEN 8 |
Kojto | 0:615c697c33b0 | 142 | |
Kojto | 0:615c697c33b0 | 143 | #define NO_QUERY_RECIVED -3 |
Kojto | 0:615c697c33b0 | 144 | |
Kojto | 0:615c697c33b0 | 145 | |
Kojto | 0:615c697c33b0 | 146 | typedef struct _in_addr_t |
Kojto | 0:615c697c33b0 | 147 | { |
Kojto | 0:615c697c33b0 | 148 | uint32_t s_addr; // load with inet_aton() |
Kojto | 0:615c697c33b0 | 149 | } in_addr; |
Kojto | 0:615c697c33b0 | 150 | |
Kojto | 0:615c697c33b0 | 151 | /*typedef struct _sockaddr_t |
Kojto | 0:615c697c33b0 | 152 | { |
Kojto | 0:615c697c33b0 | 153 | unsigned short int sa_family; |
Kojto | 0:615c697c33b0 | 154 | unsigned char sa_data[14]; |
Kojto | 0:615c697c33b0 | 155 | } sockaddr;*/ |
Kojto | 0:615c697c33b0 | 156 | |
Kojto | 0:615c697c33b0 | 157 | typedef struct _sockaddr_in_t |
Kojto | 0:615c697c33b0 | 158 | { |
Kojto | 0:615c697c33b0 | 159 | int16_t sin_family; // e.g. AF_INET |
Kojto | 0:615c697c33b0 | 160 | uint16_t sin_port; // e.g. htons(3490) |
Kojto | 0:615c697c33b0 | 161 | in_addr sin_addr; // see struct in_addr, below |
Kojto | 0:615c697c33b0 | 162 | uint8_t sin_zero[8]; // zero this if you want to |
Kojto | 0:615c697c33b0 | 163 | } sockaddr_in; |
Kojto | 0:615c697c33b0 | 164 | |
Kojto | 0:615c697c33b0 | 165 | typedef uint32_t socklen_t; |
Kojto | 0:615c697c33b0 | 166 | |
Kojto | 0:615c697c33b0 | 167 | // The fd_set member is required to be an array of longs. |
Kojto | 0:615c697c33b0 | 168 | typedef int32_t __fd_mask; |
Kojto | 0:615c697c33b0 | 169 | |
Kojto | 0:615c697c33b0 | 170 | // It's easier to assume 8-bit bytes than to get CHAR_BIT. |
Kojto | 0:615c697c33b0 | 171 | #define __NFDBITS (8 * sizeof (__fd_mask)) |
Kojto | 0:615c697c33b0 | 172 | #define __FDELT(d) ((d) / __NFDBITS) |
Kojto | 0:615c697c33b0 | 173 | #define __FDMASK(d) ((__fd_mask) 1 << ((d) % __NFDBITS)) |
Kojto | 0:615c697c33b0 | 174 | |
Kojto | 0:615c697c33b0 | 175 | // fd_set for select and pselect. |
Kojto | 0:615c697c33b0 | 176 | typedef struct |
Kojto | 0:615c697c33b0 | 177 | { |
Kojto | 0:615c697c33b0 | 178 | __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS]; |
Kojto | 0:615c697c33b0 | 179 | #define __FDS_BITS(set) ((set)->fds_bits) |
Kojto | 0:615c697c33b0 | 180 | } fd_set; |
Kojto | 0:615c697c33b0 | 181 | |
Kojto | 0:615c697c33b0 | 182 | // We don't use `memset' because this would require a prototype and |
Kojto | 0:615c697c33b0 | 183 | // the array isn't too big. |
Kojto | 0:615c697c33b0 | 184 | #define __FD_ZERO(set) \ |
Kojto | 0:615c697c33b0 | 185 | do { \ |
Kojto | 0:615c697c33b0 | 186 | uint32_t __i; \ |
Kojto | 0:615c697c33b0 | 187 | fd_set *__arr = (set); \ |
Kojto | 0:615c697c33b0 | 188 | for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) \ |
Kojto | 0:615c697c33b0 | 189 | __FDS_BITS (__arr)[__i] = 0; \ |
Kojto | 0:615c697c33b0 | 190 | } while (0) |
Kojto | 0:615c697c33b0 | 191 | #define __FD_SET(d, set) (__FDS_BITS (set)[__FDELT (d)] |= __FDMASK (d)) |
Kojto | 0:615c697c33b0 | 192 | #define __FD_CLR(d, set) (__FDS_BITS (set)[__FDELT (d)] &= ~__FDMASK (d)) |
Kojto | 0:615c697c33b0 | 193 | #define __FD_ISSET(d, set) (__FDS_BITS (set)[__FDELT (d)] & __FDMASK (d)) |
Kojto | 0:615c697c33b0 | 194 | |
Kojto | 0:615c697c33b0 | 195 | // Access macros for 'fd_set'. |
Kojto | 0:615c697c33b0 | 196 | #define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp) |
Kojto | 0:615c697c33b0 | 197 | #define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp) |
Kojto | 0:615c697c33b0 | 198 | #define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp) |
Kojto | 0:615c697c33b0 | 199 | #define FD_ZERO(fdsetp) __FD_ZERO (fdsetp) |
Kojto | 0:615c697c33b0 | 200 | |
Kojto | 0:615c697c33b0 | 201 | //Use in case of Big Endian only |
Kojto | 0:615c697c33b0 | 202 | |
Kojto | 0:615c697c33b0 | 203 | #define htonl(A) ((((uint32_t)(A) & 0xff000000) >> 24) | \ |
Kojto | 0:615c697c33b0 | 204 | (((uint32_t)(A) & 0x00ff0000) >> 8) | \ |
Kojto | 0:615c697c33b0 | 205 | (((uint32_t)(A) & 0x0000ff00) << 8) | \ |
Kojto | 0:615c697c33b0 | 206 | (((uint32_t)(A) & 0x000000ff) << 24)) |
Kojto | 0:615c697c33b0 | 207 | |
Kojto | 0:615c697c33b0 | 208 | #define ntohl htonl |
Kojto | 0:615c697c33b0 | 209 | |
Kojto | 0:615c697c33b0 | 210 | //Use in case of Big Endian only |
Kojto | 0:615c697c33b0 | 211 | #define htons(A) ((((uint32_t)(A) & 0xff00) >> 8) | \ |
Kojto | 0:615c697c33b0 | 212 | (((uint32_t)(A) & 0x00ff) << 8)) |
Kojto | 0:615c697c33b0 | 213 | |
Kojto | 0:615c697c33b0 | 214 | |
Kojto | 0:615c697c33b0 | 215 | #define ntohs htons |
Kojto | 0:615c697c33b0 | 216 | |
Kojto | 0:615c697c33b0 | 217 | // mDNS port - 5353 mDNS multicast address - 224.0.0.251 |
Kojto | 0:615c697c33b0 | 218 | #define SET_mDNS_ADD(sockaddr) sockaddr.sa_data[0] = 0x14; \ |
Kojto | 0:615c697c33b0 | 219 | sockaddr.sa_data[1] = 0xe9; \ |
Kojto | 0:615c697c33b0 | 220 | sockaddr.sa_data[2] = 0xe0; \ |
Kojto | 0:615c697c33b0 | 221 | sockaddr.sa_data[3] = 0x0; \ |
Kojto | 0:615c697c33b0 | 222 | sockaddr.sa_data[4] = 0x0; \ |
Kojto | 0:615c697c33b0 | 223 | sockaddr.sa_data[5] = 0xfb; |
Kojto | 0:615c697c33b0 | 224 | |
Kojto | 0:615c697c33b0 | 225 | #endif |