MQTT client test with W5200 ethernet shield

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Committer:
hillkim7
Date:
Thu Dec 25 11:18:46 2014 +0000
Revision:
11:313e091ab3f9
The IBM MQTT client demo program that is tested with Nucleo F401 and Seeedstudio Ethernet Shield. It is based on Wiznet sample program.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hillkim7 11:313e091ab3f9 1
hillkim7 11:313e091ab3f9 2 #pragma once
hillkim7 11:313e091ab3f9 3
hillkim7 11:313e091ab3f9 4 #include "mbed.h"
hillkim7 11:313e091ab3f9 5 #include "mbed_debug.h"
hillkim7 11:313e091ab3f9 6
hillkim7 11:313e091ab3f9 7 #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
hillkim7 11:313e091ab3f9 8
hillkim7 11:313e091ab3f9 9 #define DEFAULT_WAIT_RESP_TIMEOUT 500
hillkim7 11:313e091ab3f9 10
hillkim7 11:313e091ab3f9 11 #define MAX_SOCK_NUM 8
hillkim7 11:313e091ab3f9 12
hillkim7 11:313e091ab3f9 13 #define MR 0x0000
hillkim7 11:313e091ab3f9 14 #define GAR 0x0001
hillkim7 11:313e091ab3f9 15 #define SUBR 0x0005
hillkim7 11:313e091ab3f9 16 #define SHAR 0x0009
hillkim7 11:313e091ab3f9 17 #define SIPR 0x000f
hillkim7 11:313e091ab3f9 18 #define PHYCFGR 0x002e
hillkim7 11:313e091ab3f9 19
hillkim7 11:313e091ab3f9 20 // W5200 socket
hillkim7 11:313e091ab3f9 21 #define Sn_MR 0x4000
hillkim7 11:313e091ab3f9 22 #define Sn_CR 0x4001
hillkim7 11:313e091ab3f9 23 #define Sn_IR 0x4002
hillkim7 11:313e091ab3f9 24 #define Sn_SR 0x4003
hillkim7 11:313e091ab3f9 25 #define Sn_PORT 0x4004
hillkim7 11:313e091ab3f9 26 #define Sn_DIPR 0x400c
hillkim7 11:313e091ab3f9 27 #define Sn_DPORT 0x4010
hillkim7 11:313e091ab3f9 28 #define Sn_RXBUF_SIZE 0x401e
hillkim7 11:313e091ab3f9 29 #define Sn_TXBUF_SIZE 0x401f
hillkim7 11:313e091ab3f9 30 #define Sn_TX_FSR 0x4020
hillkim7 11:313e091ab3f9 31 #define Sn_TX_WR 0x4024
hillkim7 11:313e091ab3f9 32 #define Sn_RX_RSR 0x4026
hillkim7 11:313e091ab3f9 33 #define Sn_RX_RD 0x4028
hillkim7 11:313e091ab3f9 34
hillkim7 11:313e091ab3f9 35 class WIZnet_Chip {
hillkim7 11:313e091ab3f9 36 public:
hillkim7 11:313e091ab3f9 37
hillkim7 11:313e091ab3f9 38 enum Protocol { CLOSED = 0, TCP = 1, UDP = 2,};
hillkim7 11:313e091ab3f9 39 enum Command { OPEN = 0x01, LISTEN = 0x02, CONNECT = 0x04, DISCON = 0x08, CLOSE = 0x10, SEND = 0x20, \
hillkim7 11:313e091ab3f9 40 SEND_MAC = 0x21, SEND_KEEP = 0x22, RECV = 0x40, };
hillkim7 11:313e091ab3f9 41 enum Interrupt { INT_CON = 0x01, INT_DISCON = 0x02, INT_RECV = 0x04, INT_TIMEOUT = 0x08, INT_SEND_OK = 0x10,};
hillkim7 11:313e091ab3f9 42
hillkim7 11:313e091ab3f9 43 enum Status { SOCK_CLOSED = 0x00, SOCK_INIT = 0x13, SOCK_LISTEN = 0x14, SOCK_SYNSENT = 0x15, SOCK_ESTABLISHED = 0x17, \
hillkim7 11:313e091ab3f9 44 SOCK_CLOSE_WAIT = 0x1c, SOCK_UDP = 0x22, };
hillkim7 11:313e091ab3f9 45
hillkim7 11:313e091ab3f9 46 /*
hillkim7 11:313e091ab3f9 47 * Set MAC Address
hillkim7 11:313e091ab3f9 48 *
hillkim7 11:313e091ab3f9 49 * @return true if connected, false otherwise
hillkim7 11:313e091ab3f9 50 */
hillkim7 11:313e091ab3f9 51 bool setmac();
hillkim7 11:313e091ab3f9 52
hillkim7 11:313e091ab3f9 53 /*
hillkim7 11:313e091ab3f9 54 * Constructor
hillkim7 11:313e091ab3f9 55 *
hillkim7 11:313e091ab3f9 56 * @param spi spi class
hillkim7 11:313e091ab3f9 57 * @param cs cs of the W5200
hillkim7 11:313e091ab3f9 58 * @param reset reset pin of the W5200
hillkim7 11:313e091ab3f9 59 */
hillkim7 11:313e091ab3f9 60 WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
hillkim7 11:313e091ab3f9 61 WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
hillkim7 11:313e091ab3f9 62
hillkim7 11:313e091ab3f9 63 /*
hillkim7 11:313e091ab3f9 64 * Connect the W5200 module to the ssid contained in the constructor.
hillkim7 11:313e091ab3f9 65 *
hillkim7 11:313e091ab3f9 66 * @return true if connected, false otherwise
hillkim7 11:313e091ab3f9 67 */
hillkim7 11:313e091ab3f9 68 bool setip();
hillkim7 11:313e091ab3f9 69
hillkim7 11:313e091ab3f9 70 /*
hillkim7 11:313e091ab3f9 71 * Get Link Status
hillkim7 11:313e091ab3f9 72 *
hillkim7 11:313e091ab3f9 73 * @return true if Link up, false Link down
hillkim7 11:313e091ab3f9 74 */
hillkim7 11:313e091ab3f9 75 bool linkstatus();
hillkim7 11:313e091ab3f9 76
hillkim7 11:313e091ab3f9 77 /*
hillkim7 11:313e091ab3f9 78 * Disconnect the W5200 module from the access point
hillkim7 11:313e091ab3f9 79 *
hillkim7 11:313e091ab3f9 80 * @ returns true if successful
hillkim7 11:313e091ab3f9 81 */
hillkim7 11:313e091ab3f9 82 bool disconnect();
hillkim7 11:313e091ab3f9 83
hillkim7 11:313e091ab3f9 84 /*
hillkim7 11:313e091ab3f9 85 * Open a tcp connection with the specified host on the specified port
hillkim7 11:313e091ab3f9 86 *
hillkim7 11:313e091ab3f9 87 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
hillkim7 11:313e091ab3f9 88 * @param port port
hillkim7 11:313e091ab3f9 89 * @ returns true if successful
hillkim7 11:313e091ab3f9 90 */
hillkim7 11:313e091ab3f9 91 bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
hillkim7 11:313e091ab3f9 92
hillkim7 11:313e091ab3f9 93 /*
hillkim7 11:313e091ab3f9 94 * Set the protocol (UDP or TCP)
hillkim7 11:313e091ab3f9 95 *
hillkim7 11:313e091ab3f9 96 * @param p protocol
hillkim7 11:313e091ab3f9 97 * @ returns true if successful
hillkim7 11:313e091ab3f9 98 */
hillkim7 11:313e091ab3f9 99 bool setProtocol(int socket, Protocol p);
hillkim7 11:313e091ab3f9 100
hillkim7 11:313e091ab3f9 101 /*
hillkim7 11:313e091ab3f9 102 * Reset the W5200 module
hillkim7 11:313e091ab3f9 103 */
hillkim7 11:313e091ab3f9 104 void reset();
hillkim7 11:313e091ab3f9 105
hillkim7 11:313e091ab3f9 106
hillkim7 11:313e091ab3f9 107 int wait_readable(int socket, int wait_time_ms, int req_size = 0);
hillkim7 11:313e091ab3f9 108
hillkim7 11:313e091ab3f9 109 int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
hillkim7 11:313e091ab3f9 110
hillkim7 11:313e091ab3f9 111 /*
hillkim7 11:313e091ab3f9 112 * Check if a tcp link is active
hillkim7 11:313e091ab3f9 113 *
hillkim7 11:313e091ab3f9 114 * @returns true if successful
hillkim7 11:313e091ab3f9 115 */
hillkim7 11:313e091ab3f9 116 bool is_connected(int socket);
hillkim7 11:313e091ab3f9 117
hillkim7 11:313e091ab3f9 118 /*
hillkim7 11:313e091ab3f9 119 * Close a tcp connection
hillkim7 11:313e091ab3f9 120 *
hillkim7 11:313e091ab3f9 121 * @ returns true if successful
hillkim7 11:313e091ab3f9 122 */
hillkim7 11:313e091ab3f9 123 bool close(int socket);
hillkim7 11:313e091ab3f9 124
hillkim7 11:313e091ab3f9 125 /*
hillkim7 11:313e091ab3f9 126 * @param str string to be sent
hillkim7 11:313e091ab3f9 127 * @param len string length
hillkim7 11:313e091ab3f9 128 */
hillkim7 11:313e091ab3f9 129 int send(int socket, const char * str, int len);
hillkim7 11:313e091ab3f9 130
hillkim7 11:313e091ab3f9 131 int recv(int socket, char* buf, int len);
hillkim7 11:313e091ab3f9 132
hillkim7 11:313e091ab3f9 133 /*
hillkim7 11:313e091ab3f9 134 * Return true if the module is using dhcp
hillkim7 11:313e091ab3f9 135 *
hillkim7 11:313e091ab3f9 136 * @returns true if the module is using dhcp
hillkim7 11:313e091ab3f9 137 */
hillkim7 11:313e091ab3f9 138 bool isDHCP() {
hillkim7 11:313e091ab3f9 139 return dhcp;
hillkim7 11:313e091ab3f9 140 }
hillkim7 11:313e091ab3f9 141
hillkim7 11:313e091ab3f9 142 bool gethostbyname(const char* host, uint32_t* ip);
hillkim7 11:313e091ab3f9 143
hillkim7 11:313e091ab3f9 144 static WIZnet_Chip * getInstance() {
hillkim7 11:313e091ab3f9 145 return inst;
hillkim7 11:313e091ab3f9 146 };
hillkim7 11:313e091ab3f9 147
hillkim7 11:313e091ab3f9 148 int new_socket();
hillkim7 11:313e091ab3f9 149 uint16_t new_port();
hillkim7 11:313e091ab3f9 150 void scmd(int socket, Command cmd);
hillkim7 11:313e091ab3f9 151
hillkim7 11:313e091ab3f9 152 template<typename T>
hillkim7 11:313e091ab3f9 153 void sreg(int socket, uint16_t addr, T data) {
hillkim7 11:313e091ab3f9 154 reg_wr<T>(addr+0x100*socket, data);
hillkim7 11:313e091ab3f9 155 }
hillkim7 11:313e091ab3f9 156
hillkim7 11:313e091ab3f9 157 template<typename T>
hillkim7 11:313e091ab3f9 158 T sreg(int socket, uint16_t addr) {
hillkim7 11:313e091ab3f9 159 return reg_rd<T>(addr+0x100*socket);
hillkim7 11:313e091ab3f9 160 }
hillkim7 11:313e091ab3f9 161
hillkim7 11:313e091ab3f9 162 template<typename T>
hillkim7 11:313e091ab3f9 163 void reg_wr(uint16_t addr, T data) {
hillkim7 11:313e091ab3f9 164 uint8_t buf[sizeof(T)];
hillkim7 11:313e091ab3f9 165 *reinterpret_cast<T*>(buf) = data;
hillkim7 11:313e091ab3f9 166 for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
hillkim7 11:313e091ab3f9 167 uint8_t t = buf[i];
hillkim7 11:313e091ab3f9 168 buf[i] = buf[sizeof(buf)-1-i];
hillkim7 11:313e091ab3f9 169 buf[sizeof(buf)-1-i] = t;
hillkim7 11:313e091ab3f9 170 }
hillkim7 11:313e091ab3f9 171 spi_write(addr, buf, sizeof(buf));
hillkim7 11:313e091ab3f9 172 }
hillkim7 11:313e091ab3f9 173
hillkim7 11:313e091ab3f9 174 template<typename T>
hillkim7 11:313e091ab3f9 175 T reg_rd(uint16_t addr) {
hillkim7 11:313e091ab3f9 176 uint8_t buf[sizeof(T)];
hillkim7 11:313e091ab3f9 177 spi_read(addr, buf, sizeof(buf));
hillkim7 11:313e091ab3f9 178 for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
hillkim7 11:313e091ab3f9 179 uint8_t t = buf[i];
hillkim7 11:313e091ab3f9 180 buf[i] = buf[sizeof(buf)-1-i];
hillkim7 11:313e091ab3f9 181 buf[sizeof(buf)-1-i] = t;
hillkim7 11:313e091ab3f9 182 }
hillkim7 11:313e091ab3f9 183 return *reinterpret_cast<T*>(buf);
hillkim7 11:313e091ab3f9 184 }
hillkim7 11:313e091ab3f9 185
hillkim7 11:313e091ab3f9 186 void reg_rd_mac(uint16_t addr, uint8_t* data) {
hillkim7 11:313e091ab3f9 187 spi_read(addr, data, 6);
hillkim7 11:313e091ab3f9 188 }
hillkim7 11:313e091ab3f9 189
hillkim7 11:313e091ab3f9 190 void reg_wr_ip(uint16_t addr, const char* ip) {
hillkim7 11:313e091ab3f9 191 uint8_t buf[4];
hillkim7 11:313e091ab3f9 192 char* p = (char*)ip;
hillkim7 11:313e091ab3f9 193 for(int i = 0; i < 4; i++) {
hillkim7 11:313e091ab3f9 194 buf[i] = atoi(p);
hillkim7 11:313e091ab3f9 195 p = strchr(p, '.');
hillkim7 11:313e091ab3f9 196 if (p == NULL) {
hillkim7 11:313e091ab3f9 197 break;
hillkim7 11:313e091ab3f9 198 }
hillkim7 11:313e091ab3f9 199 p++;
hillkim7 11:313e091ab3f9 200 }
hillkim7 11:313e091ab3f9 201 spi_write(addr, buf, sizeof(buf));
hillkim7 11:313e091ab3f9 202 }
hillkim7 11:313e091ab3f9 203
hillkim7 11:313e091ab3f9 204 void sreg_ip(int socket, uint16_t addr, const char* ip) {
hillkim7 11:313e091ab3f9 205 reg_wr_ip(addr+0x100*socket, ip);
hillkim7 11:313e091ab3f9 206 }
hillkim7 11:313e091ab3f9 207
hillkim7 11:313e091ab3f9 208 protected:
hillkim7 11:313e091ab3f9 209 uint8_t mac[6];
hillkim7 11:313e091ab3f9 210 uint32_t ip;
hillkim7 11:313e091ab3f9 211 uint32_t netmask;
hillkim7 11:313e091ab3f9 212 uint32_t gateway;
hillkim7 11:313e091ab3f9 213 uint32_t dnsaddr;
hillkim7 11:313e091ab3f9 214 bool dhcp;
hillkim7 11:313e091ab3f9 215
hillkim7 11:313e091ab3f9 216 static WIZnet_Chip* inst;
hillkim7 11:313e091ab3f9 217
hillkim7 11:313e091ab3f9 218 void reg_wr_mac(uint16_t addr, uint8_t* data) {
hillkim7 11:313e091ab3f9 219 spi_write(addr, data, 6);
hillkim7 11:313e091ab3f9 220 }
hillkim7 11:313e091ab3f9 221
hillkim7 11:313e091ab3f9 222 void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len);
hillkim7 11:313e091ab3f9 223 void spi_read(uint16_t addr, uint8_t *buf, uint16_t len);
hillkim7 11:313e091ab3f9 224 SPI* spi;
hillkim7 11:313e091ab3f9 225 DigitalOut cs;
hillkim7 11:313e091ab3f9 226 DigitalOut reset_pin;
hillkim7 11:313e091ab3f9 227 };
hillkim7 11:313e091ab3f9 228
hillkim7 11:313e091ab3f9 229 extern uint32_t str_to_ip(const char* str);
hillkim7 11:313e091ab3f9 230 extern void printfBytes(char* str, uint8_t* buf, int len);
hillkim7 11:313e091ab3f9 231 extern void printHex(uint8_t* buf, int len);
hillkim7 11:313e091ab3f9 232 extern void debug_hex(uint8_t* buf, int len);
hillkim7 11:313e091ab3f9 233