Revised to prevent DHCPClient::discover from hanging.
Fork of WIZ820ioInterface by
WIZ820io/WIZ820io.h@9:c1722862c13b, 2014-08-15 (annotated)
- Committer:
- rraab
- Date:
- Fri Aug 15 19:53:25 2014 +0000
- Revision:
- 9:c1722862c13b
- Parent:
- 7:c0cd6680bcb7
Fixed DHCPClient::discover calling rand, which hangs in an infinite loop.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:fb4494783863 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 1:fb4494783863 | 2 | * |
samux | 1:fb4494783863 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:fb4494783863 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 1:fb4494783863 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 1:fb4494783863 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 1:fb4494783863 | 7 | * furnished to do so, subject to the following conditions: |
samux | 1:fb4494783863 | 8 | * |
samux | 1:fb4494783863 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:fb4494783863 | 10 | * substantial portions of the Software. |
samux | 1:fb4494783863 | 11 | * |
samux | 1:fb4494783863 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:fb4494783863 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:fb4494783863 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:fb4494783863 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:fb4494783863 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:fb4494783863 | 17 | * |
samux | 1:fb4494783863 | 18 | */ |
samux | 1:fb4494783863 | 19 | |
va009039 | 5:fb15c35d1e28 | 20 | #pragma once |
samux | 1:fb4494783863 | 21 | |
samux | 1:fb4494783863 | 22 | #include "mbed.h" |
va009039 | 5:fb15c35d1e28 | 23 | #include "mbed_debug.h" |
va009039 | 5:fb15c35d1e28 | 24 | |
va009039 | 5:fb15c35d1e28 | 25 | #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; |
samux | 1:fb4494783863 | 26 | |
samux | 1:fb4494783863 | 27 | #define DEFAULT_WAIT_RESP_TIMEOUT 500 |
samux | 1:fb4494783863 | 28 | |
va009039 | 5:fb15c35d1e28 | 29 | enum Protocol { |
va009039 | 5:fb15c35d1e28 | 30 | CLOSED = 0, |
va009039 | 5:fb15c35d1e28 | 31 | TCP = 1, |
va009039 | 5:fb15c35d1e28 | 32 | UDP = 2, |
va009039 | 5:fb15c35d1e28 | 33 | }; |
va009039 | 5:fb15c35d1e28 | 34 | |
va009039 | 5:fb15c35d1e28 | 35 | enum Command { |
va009039 | 5:fb15c35d1e28 | 36 | OPEN = 0x01, |
va009039 | 5:fb15c35d1e28 | 37 | LISTEN = 0x02, |
va009039 | 5:fb15c35d1e28 | 38 | CONNECT = 0x04, |
va009039 | 5:fb15c35d1e28 | 39 | DISCON = 0x08, |
va009039 | 5:fb15c35d1e28 | 40 | CLOSE = 0x10, |
va009039 | 5:fb15c35d1e28 | 41 | SEND = 0x20, |
va009039 | 5:fb15c35d1e28 | 42 | SEND_MAC = 0x21, |
va009039 | 5:fb15c35d1e28 | 43 | SEND_KEEP = 0x22, |
va009039 | 5:fb15c35d1e28 | 44 | RECV = 0x40, |
va009039 | 5:fb15c35d1e28 | 45 | |
samux | 1:fb4494783863 | 46 | }; |
samux | 1:fb4494783863 | 47 | |
ban4jp | 7:c0cd6680bcb7 | 48 | enum Interrupt { |
ban4jp | 7:c0cd6680bcb7 | 49 | INT_CON = 0x01, |
ban4jp | 7:c0cd6680bcb7 | 50 | INT_DISCON = 0x02, |
ban4jp | 7:c0cd6680bcb7 | 51 | INT_RECV = 0x04, |
ban4jp | 7:c0cd6680bcb7 | 52 | INT_TIMEOUT = 0x08, |
ban4jp | 7:c0cd6680bcb7 | 53 | INT_SEND_OK = 0x10, |
ban4jp | 7:c0cd6680bcb7 | 54 | }; |
ban4jp | 7:c0cd6680bcb7 | 55 | |
va009039 | 5:fb15c35d1e28 | 56 | enum Status { |
va009039 | 5:fb15c35d1e28 | 57 | SOCK_CLOSED = 0x00, |
va009039 | 5:fb15c35d1e28 | 58 | SOCK_INIT = 0x13, |
va009039 | 5:fb15c35d1e28 | 59 | SOCK_LISTEN = 0x14, |
va009039 | 5:fb15c35d1e28 | 60 | SOCK_SYNSENT = 0x15, |
va009039 | 5:fb15c35d1e28 | 61 | SOCK_ESTABLISHED = 0x17, |
va009039 | 5:fb15c35d1e28 | 62 | SOCK_CLOSE_WAIT = 0x1c, |
va009039 | 5:fb15c35d1e28 | 63 | SOCK_UDP = 0x22, |
samux | 1:fb4494783863 | 64 | }; |
samux | 1:fb4494783863 | 65 | |
va009039 | 5:fb15c35d1e28 | 66 | #define MAX_SOCK_NUM 8 |
samux | 1:fb4494783863 | 67 | |
va009039 | 5:fb15c35d1e28 | 68 | #define MR 0x0000 |
va009039 | 5:fb15c35d1e28 | 69 | #define GAR 0x0001 |
va009039 | 5:fb15c35d1e28 | 70 | #define SUBR 0x0005 |
va009039 | 5:fb15c35d1e28 | 71 | #define SHAR 0x0009 |
va009039 | 5:fb15c35d1e28 | 72 | #define SIPR 0x000f |
va009039 | 5:fb15c35d1e28 | 73 | #define PHYSTATUS 0x0035 |
va009039 | 5:fb15c35d1e28 | 74 | // socket |
ban4jp | 7:c0cd6680bcb7 | 75 | #define Sn_MR 0x4000 |
ban4jp | 7:c0cd6680bcb7 | 76 | #define Sn_CR 0x4001 |
ban4jp | 7:c0cd6680bcb7 | 77 | #define Sn_IR 0x4002 |
ban4jp | 7:c0cd6680bcb7 | 78 | #define Sn_SR 0x4003 |
ban4jp | 7:c0cd6680bcb7 | 79 | #define Sn_PORT 0x4004 |
ban4jp | 7:c0cd6680bcb7 | 80 | #define Sn_DIPR 0x400c |
ban4jp | 7:c0cd6680bcb7 | 81 | #define Sn_DPORT 0x4010 |
ban4jp | 7:c0cd6680bcb7 | 82 | #define Sn_RXBUF_SIZE 0x401e |
ban4jp | 7:c0cd6680bcb7 | 83 | #define Sn_TXBUF_SIZE 0x401f |
ban4jp | 7:c0cd6680bcb7 | 84 | #define Sn_TX_FSR 0x4020 |
ban4jp | 7:c0cd6680bcb7 | 85 | #define Sn_TX_WR 0x4024 |
ban4jp | 7:c0cd6680bcb7 | 86 | #define Sn_RX_RSR 0x4026 |
ban4jp | 7:c0cd6680bcb7 | 87 | #define Sn_RX_RD 0x4028 |
va009039 | 5:fb15c35d1e28 | 88 | |
va009039 | 5:fb15c35d1e28 | 89 | class WIZ820io { |
samux | 1:fb4494783863 | 90 | public: |
samux | 1:fb4494783863 | 91 | /* |
samux | 1:fb4494783863 | 92 | * Constructor |
samux | 1:fb4494783863 | 93 | * |
samux | 1:fb4494783863 | 94 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 1:fb4494783863 | 95 | * @param rx mbed pin to use for rx line of Serial interface |
va009039 | 5:fb15c35d1e28 | 96 | * @param reset reset pin of the WIZ820io module |
samux | 1:fb4494783863 | 97 | */ |
va009039 | 5:fb15c35d1e28 | 98 | WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); |
va009039 | 5:fb15c35d1e28 | 99 | WIZ820io(SPI* spi, PinName cs, PinName reset); |
samux | 1:fb4494783863 | 100 | |
samux | 1:fb4494783863 | 101 | /* |
va009039 | 5:fb15c35d1e28 | 102 | * Connect the WIZ820io module to the ssid contained in the constructor. |
samux | 1:fb4494783863 | 103 | * |
samux | 1:fb4494783863 | 104 | * @return true if connected, false otherwise |
samux | 1:fb4494783863 | 105 | */ |
samux | 1:fb4494783863 | 106 | bool join(); |
samux | 1:fb4494783863 | 107 | |
samux | 1:fb4494783863 | 108 | /* |
va009039 | 5:fb15c35d1e28 | 109 | * Disconnect the WIZ820io module from the access point |
samux | 1:fb4494783863 | 110 | * |
samux | 1:fb4494783863 | 111 | * @ returns true if successful |
samux | 1:fb4494783863 | 112 | */ |
samux | 1:fb4494783863 | 113 | bool disconnect(); |
samux | 1:fb4494783863 | 114 | |
samux | 1:fb4494783863 | 115 | /* |
samux | 1:fb4494783863 | 116 | * Open a tcp connection with the specified host on the specified port |
samux | 1:fb4494783863 | 117 | * |
samux | 1:fb4494783863 | 118 | * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) |
samux | 1:fb4494783863 | 119 | * @param port port |
samux | 1:fb4494783863 | 120 | * @ returns true if successful |
samux | 1:fb4494783863 | 121 | */ |
va009039 | 5:fb15c35d1e28 | 122 | bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); |
samux | 1:fb4494783863 | 123 | |
samux | 1:fb4494783863 | 124 | /* |
samux | 1:fb4494783863 | 125 | * Set the protocol (UDP or TCP) |
samux | 1:fb4494783863 | 126 | * |
samux | 1:fb4494783863 | 127 | * @param p protocol |
samux | 1:fb4494783863 | 128 | * @ returns true if successful |
samux | 1:fb4494783863 | 129 | */ |
va009039 | 5:fb15c35d1e28 | 130 | bool setProtocol(int socket, Protocol p); |
samux | 1:fb4494783863 | 131 | |
samux | 1:fb4494783863 | 132 | /* |
va009039 | 5:fb15c35d1e28 | 133 | * Reset the WIZ820io module |
samux | 1:fb4494783863 | 134 | */ |
samux | 1:fb4494783863 | 135 | void reset(); |
samux | 3:9aa05e19c62e | 136 | |
va009039 | 5:fb15c35d1e28 | 137 | |
va009039 | 5:fb15c35d1e28 | 138 | int wait_readable(int socket, int wait_time_ms, int req_size = 0); |
samux | 1:fb4494783863 | 139 | |
va009039 | 5:fb15c35d1e28 | 140 | int wait_writeable(int socket, int wait_time_ms, int req_size = 0); |
samux | 1:fb4494783863 | 141 | |
samux | 1:fb4494783863 | 142 | /* |
samux | 1:fb4494783863 | 143 | * Check if a tcp link is active |
samux | 1:fb4494783863 | 144 | * |
samux | 1:fb4494783863 | 145 | * @returns true if successful |
samux | 1:fb4494783863 | 146 | */ |
va009039 | 5:fb15c35d1e28 | 147 | bool is_connected(int socket); |
samux | 1:fb4494783863 | 148 | |
samux | 1:fb4494783863 | 149 | /* |
samux | 1:fb4494783863 | 150 | * Close a tcp connection |
samux | 1:fb4494783863 | 151 | * |
samux | 1:fb4494783863 | 152 | * @ returns true if successful |
samux | 1:fb4494783863 | 153 | */ |
va009039 | 5:fb15c35d1e28 | 154 | bool close(int socket); |
samux | 1:fb4494783863 | 155 | |
samux | 1:fb4494783863 | 156 | /* |
samux | 1:fb4494783863 | 157 | * @param str string to be sent |
samux | 1:fb4494783863 | 158 | * @param len string length |
samux | 1:fb4494783863 | 159 | */ |
va009039 | 5:fb15c35d1e28 | 160 | int send(int socket, const char * str, int len); |
samux | 1:fb4494783863 | 161 | |
va009039 | 5:fb15c35d1e28 | 162 | int recv(int socket, char* buf, int len); |
va009039 | 5:fb15c35d1e28 | 163 | |
samux | 4:0bcec6272784 | 164 | /* |
samux | 4:0bcec6272784 | 165 | * Return true if the module is using dhcp |
samux | 4:0bcec6272784 | 166 | * |
samux | 4:0bcec6272784 | 167 | * @returns true if the module is using dhcp |
samux | 4:0bcec6272784 | 168 | */ |
samux | 4:0bcec6272784 | 169 | bool isDHCP() { |
va009039 | 5:fb15c35d1e28 | 170 | return dhcp; |
samux | 4:0bcec6272784 | 171 | } |
samux | 1:fb4494783863 | 172 | |
va009039 | 5:fb15c35d1e28 | 173 | bool gethostbyname(const char* host, uint32_t* ip); |
samux | 1:fb4494783863 | 174 | |
va009039 | 5:fb15c35d1e28 | 175 | static WIZ820io * getInstance() { |
samux | 1:fb4494783863 | 176 | return inst; |
samux | 1:fb4494783863 | 177 | }; |
samux | 1:fb4494783863 | 178 | |
va009039 | 5:fb15c35d1e28 | 179 | int new_socket(); |
va009039 | 5:fb15c35d1e28 | 180 | uint16_t new_port(); |
va009039 | 5:fb15c35d1e28 | 181 | void scmd(int socket, Command cmd); |
va009039 | 5:fb15c35d1e28 | 182 | |
va009039 | 5:fb15c35d1e28 | 183 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 184 | void sreg(int socket, uint16_t addr, T data) { |
va009039 | 5:fb15c35d1e28 | 185 | reg_wr<T>(addr+0x100*socket, data); |
va009039 | 5:fb15c35d1e28 | 186 | } |
va009039 | 5:fb15c35d1e28 | 187 | |
va009039 | 5:fb15c35d1e28 | 188 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 189 | T sreg(int socket, uint16_t addr) { |
va009039 | 5:fb15c35d1e28 | 190 | return reg_rd<T>(addr+0x100*socket); |
va009039 | 5:fb15c35d1e28 | 191 | } |
samux | 1:fb4494783863 | 192 | |
va009039 | 5:fb15c35d1e28 | 193 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 194 | void reg_wr(uint16_t addr, T data) { |
va009039 | 5:fb15c35d1e28 | 195 | uint8_t buf[sizeof(T)]; |
va009039 | 5:fb15c35d1e28 | 196 | *reinterpret_cast<T*>(buf) = data; |
va009039 | 5:fb15c35d1e28 | 197 | for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian |
va009039 | 5:fb15c35d1e28 | 198 | uint8_t t = buf[i]; |
va009039 | 5:fb15c35d1e28 | 199 | buf[i] = buf[sizeof(buf)-1-i]; |
va009039 | 5:fb15c35d1e28 | 200 | buf[sizeof(buf)-1-i] = t; |
va009039 | 5:fb15c35d1e28 | 201 | } |
va009039 | 5:fb15c35d1e28 | 202 | spi_write(addr, buf, sizeof(buf)); |
va009039 | 5:fb15c35d1e28 | 203 | } |
va009039 | 5:fb15c35d1e28 | 204 | |
va009039 | 5:fb15c35d1e28 | 205 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 206 | T reg_rd(uint16_t addr) { |
va009039 | 5:fb15c35d1e28 | 207 | uint8_t buf[sizeof(T)]; |
va009039 | 5:fb15c35d1e28 | 208 | spi_read(addr, buf, sizeof(buf)); |
va009039 | 5:fb15c35d1e28 | 209 | for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian |
va009039 | 5:fb15c35d1e28 | 210 | uint8_t t = buf[i]; |
va009039 | 5:fb15c35d1e28 | 211 | buf[i] = buf[sizeof(buf)-1-i]; |
va009039 | 5:fb15c35d1e28 | 212 | buf[sizeof(buf)-1-i] = t; |
va009039 | 5:fb15c35d1e28 | 213 | } |
va009039 | 5:fb15c35d1e28 | 214 | return *reinterpret_cast<T*>(buf); |
va009039 | 5:fb15c35d1e28 | 215 | } |
samux | 1:fb4494783863 | 216 | |
va009039 | 5:fb15c35d1e28 | 217 | void reg_rd_mac(uint16_t addr, uint8_t* data) { |
va009039 | 5:fb15c35d1e28 | 218 | spi_read(addr, data, 6); |
va009039 | 5:fb15c35d1e28 | 219 | } |
samux | 1:fb4494783863 | 220 | |
va009039 | 5:fb15c35d1e28 | 221 | void reg_wr_ip(uint16_t addr, const char* ip) { |
va009039 | 5:fb15c35d1e28 | 222 | uint8_t buf[4]; |
va009039 | 5:fb15c35d1e28 | 223 | char* p = (char*)ip; |
va009039 | 5:fb15c35d1e28 | 224 | for(int i = 0; i < 4; i++) { |
va009039 | 5:fb15c35d1e28 | 225 | buf[i] = atoi(p); |
va009039 | 5:fb15c35d1e28 | 226 | p = strchr(p, '.'); |
va009039 | 5:fb15c35d1e28 | 227 | if (p == NULL) { |
va009039 | 5:fb15c35d1e28 | 228 | break; |
va009039 | 5:fb15c35d1e28 | 229 | } |
va009039 | 5:fb15c35d1e28 | 230 | p++; |
va009039 | 5:fb15c35d1e28 | 231 | } |
va009039 | 5:fb15c35d1e28 | 232 | spi_write(addr, buf, sizeof(buf)); |
va009039 | 5:fb15c35d1e28 | 233 | } |
samux | 1:fb4494783863 | 234 | |
va009039 | 5:fb15c35d1e28 | 235 | void sreg_ip(int socket, uint16_t addr, const char* ip) { |
va009039 | 5:fb15c35d1e28 | 236 | reg_wr_ip(addr+0x100*socket, ip); |
va009039 | 5:fb15c35d1e28 | 237 | } |
va009039 | 5:fb15c35d1e28 | 238 | |
va009039 | 5:fb15c35d1e28 | 239 | protected: |
va009039 | 5:fb15c35d1e28 | 240 | uint32_t ip; |
va009039 | 5:fb15c35d1e28 | 241 | uint32_t netmask; |
va009039 | 5:fb15c35d1e28 | 242 | uint32_t gateway; |
va009039 | 5:fb15c35d1e28 | 243 | uint32_t dnsaddr; |
va009039 | 5:fb15c35d1e28 | 244 | bool dhcp; |
samux | 1:fb4494783863 | 245 | |
va009039 | 5:fb15c35d1e28 | 246 | static WIZ820io* inst; |
va009039 | 5:fb15c35d1e28 | 247 | |
va009039 | 5:fb15c35d1e28 | 248 | void reg_wr_mac(uint16_t addr, uint8_t* data) { |
va009039 | 5:fb15c35d1e28 | 249 | spi_write(addr, data, 6); |
va009039 | 5:fb15c35d1e28 | 250 | } |
va009039 | 5:fb15c35d1e28 | 251 | |
va009039 | 5:fb15c35d1e28 | 252 | void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); |
va009039 | 5:fb15c35d1e28 | 253 | void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); |
va009039 | 5:fb15c35d1e28 | 254 | SPI* spi; |
va009039 | 5:fb15c35d1e28 | 255 | DigitalOut cs; |
va009039 | 5:fb15c35d1e28 | 256 | DigitalOut reset_pin; |
samux | 1:fb4494783863 | 257 | }; |
samux | 1:fb4494783863 | 258 | |
va009039 | 5:fb15c35d1e28 | 259 | // WIZ820io.cpp |
va009039 | 5:fb15c35d1e28 | 260 | extern uint32_t str_to_ip(const char* str); |
va009039 | 5:fb15c35d1e28 | 261 | extern void printfBytes(char* str, uint8_t* buf, int len); |
va009039 | 5:fb15c35d1e28 | 262 | extern void printHex(uint8_t* buf, int len); |
va009039 | 5:fb15c35d1e28 | 263 | extern void debug_hex(uint8_t* buf, int len); |