MQTT client test with W5200 ethernet shield
Dependents: IBMIoTClientEthernetExample_W5200
Fork of W5500Interface by
Revision 10:713b6d2aaefb, committed 2014-10-16
- Comitter:
- hjjeon
- Date:
- Thu Oct 16 06:36:16 2014 +0000
- Parent:
- 9:dfffa4d6f022
- Commit message:
- Bug fixed.
Changed in this revision
--- a/Socket/Socket.cpp Wed Oct 15 06:25:21 2014 +0000 +++ b/Socket/Socket.cpp Thu Oct 16 06:36:16 2014 +0000 @@ -17,7 +17,7 @@ */ #include "Socket.h" -static uint16_t sock_any_port = SOCK_ANY_PORT_NUM; + Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) { eth = WIZnet_Chip::getInstance(); @@ -46,45 +46,3 @@ { close(); //Don't want to leak } - - -int8_t Socket::Socket_macraw(uint8_t sn, uint16_t port, uint8_t flag) -{ - //CHECK_SOCKNUM(); - - if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG; - - eth->close(sn); - eth->setSn_MR(sn, (Sn_MR_MACRAW | (flag & 0xF0))); - if(!port) - { - port = sock_any_port++; - if(sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM; - } - eth->setSn_PORT(sn,port); - eth->setSn_CR(sn,Sn_CR_OPEN); - while(eth->getSn_CR(sn)); - //sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn); - //sock_is_sending &= ~(1<<sn); - //sock_remained_size[sn] = 0; - //sock_pack_info[sn] = 0; - while(eth->getSn_SR(sn) == SOCK_CLOSED); - return (int8_t)sn; -} -/* -int8_t Socket::close(uint8_t sn) -{ - //CHECK_SOCKNUM(); - - eth->setSn_CR(sn,Sn_CR_CLOSE); - // wait to process the command... - while( eth->getSn_CR(sn) ); - // clear all interrupt of the socket. - eth->setSn_IR(sn, 0xFF); - //sock_is_sending &= ~(1<<sn); - //sock_remained_size[sn] = 0; - //sock_pack_info[sn] = 0; - while(eth->getSn_SR(sn) != SOCK_CLOSED); - return SOCK_OK; -} -*/ \ No newline at end of file
--- a/Socket/Socket.h Wed Oct 15 06:25:21 2014 +0000 +++ b/Socket/Socket.h Thu Oct 16 06:36:16 2014 +0000 @@ -25,32 +25,11 @@ #define htonl(x) __REV(x) #define ntohl(x) __REV(x) -#define SOCK_OK 1 ///< Result is OK about socket process. -#define SOCK_BUSY 0 ///< Socket is busy on processing the operation. Valid only Non-block IO Mode. -#define SOCK_FATAL -1000 ///< Result is fatal error about socket process. - -#define SOCK_ERROR 0 -#define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number -#define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option -#define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized -#define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed. -#define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation. -#define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag -#define SOCKERR_SOCKSTATUS (SOCK_ERROR - 7) ///< Invalid socket status for socket operation. -#define SOCKERR_ARG (SOCK_ERROR - 10) ///< Invalid argrument. -#define SOCKERR_PORTZERO (SOCK_ERROR - 11) ///< Port number is zero -#define SOCKERR_IPINVALID (SOCK_ERROR - 12) ///< Invalid IP address -#define SOCKERR_TIMEOUT (SOCK_ERROR - 13) ///< Timeout occurred -#define SOCKERR_DATALEN (SOCK_ERROR - 14) ///< Data length is zero or greater than buffer max size. -#define SOCKERR_BUFFER (SOCK_ERROR - 15) ///< Socket buffer is not enough for data communication. - -#define SOCK_ANY_PORT_NUM 0xC000; - /** Socket file descriptor and select wrapper */ class Socket { -public: +public: /** Socket */ Socket(); @@ -65,17 +44,15 @@ /** Close the socket file descriptor */ int close(); - + + ~Socket(); - - + protected: int _sock_fd; bool _blocking; int _timeout; WIZnet_Chip* eth; - - };
--- a/WIZnet/W5500.cpp Wed Oct 15 06:25:21 2014 +0000 +++ b/WIZnet/W5500.cpp Thu Oct 16 06:36:16 2014 +0000 @@ -35,6 +35,7 @@ cs = 1; reset_pin = 1; inst = this; + sock_any_port = SOCK_ANY_PORT_NUM; } WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset): @@ -44,6 +45,7 @@ cs = 1; reset_pin = 1; inst = this; + sock_any_port = SOCK_ANY_PORT_NUM; } bool WIZnet_Chip::setmac()
--- a/WIZnet/W5500.h Wed Oct 15 06:25:21 2014 +0000 +++ b/WIZnet/W5500.h Thu Oct 16 06:36:16 2014 +0000 @@ -3,63 +3,34 @@ #include "mbed.h" #include "mbed_debug.h" + #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; #define DEFAULT_WAIT_RESP_TIMEOUT 500 -enum Command { - OPEN = 0x01, - LISTEN = 0x02, - CONNECT = 0x04, - DISCON = 0x08, - CLOSE = 0x10, - SEND = 0x20, - SEND_MAC = 0x21, - SEND_KEEP = 0x22, - RECV = 0x40, - -}; + -enum Interrupt { - INT_CON = 0x01, - INT_DISCON = 0x02, - INT_RECV = 0x04, - INT_TIMEOUT = 0x08, - INT_SEND_OK = 0x10, -}; +#define SOCK_OK 1 ///< Result is OK about socket process. +#define SOCK_BUSY 0 ///< Socket is busy on processing the operation. Valid only Non-block IO Mode. +#define SOCK_FATAL -1000 ///< Result is fatal error about socket process. -enum Status { - SOCK_CLOSED = 0x00, - SOCK_INIT = 0x13, - SOCK_LISTEN = 0x14, - SOCK_SYNSENT = 0x15, - SOCK_ESTABLISHED = 0x17, - SOCK_CLOSE_WAIT = 0x1c, - SOCK_UDP = 0x22, -}; - -typedef enum -{ - - IK_WOL = (1 << 4), ///< Wake On Lan by receiving the magic packet. Valid in W500. - IK_PPPOE_TERMINATED = (1 << 5), ///< PPPoE Disconnected +#define SOCK_ERROR 0 +#define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number +#define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option +#define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized +#define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed. +#define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation. +#define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag +#define SOCKERR_SOCKSTATUS (SOCK_ERROR - 7) ///< Invalid socket status for socket operation. +#define SOCKERR_ARG (SOCK_ERROR - 10) ///< Invalid argrument. +#define SOCKERR_PORTZERO (SOCK_ERROR - 11) ///< Port number is zero +#define SOCKERR_IPINVALID (SOCK_ERROR - 12) ///< Invalid IP address +#define SOCKERR_TIMEOUT (SOCK_ERROR - 13) ///< Timeout occurred +#define SOCKERR_DATALEN (SOCK_ERROR - 14) ///< Data length is zero or greater than buffer max size. +#define SOCKERR_BUFFER (SOCK_ERROR - 15) ///< Socket buffer is not enough for data communication. - IK_DEST_UNREACH = (1 << 6), ///< Destination IP & Port Unreable, No use in W5200 - - IK_IP_CONFLICT = (1 << 7), ///< IP conflict occurred +#define SOCK_ANY_PORT_NUM 0xC000; - IK_SOCK_0 = (1 << 8), ///< Socket 0 interrupt - IK_SOCK_1 = (1 << 9), ///< Socket 1 interrupt - IK_SOCK_2 = (1 << 10), ///< Socket 2 interrupt - IK_SOCK_3 = (1 << 11), ///< Socket 3 interrupt - IK_SOCK_4 = (1 << 12), ///< Socket 4 interrupt, No use in 5100 - IK_SOCK_5 = (1 << 13), ///< Socket 5 interrupt, No use in 5100 - IK_SOCK_6 = (1 << 14), ///< Socket 6 interrupt, No use in 5100 - IK_SOCK_7 = (1 << 15), ///< Socket 7 interrupt, No use in 5100 - - IK_SOCK_ALL = (0xFF << 8) ///< All Socket interrpt - -}intr_kind; #define MAX_SOCK_NUM 8 @@ -85,7 +56,6 @@ #define PHYCFGR 0x002e #define VERSIONR 0x0039 - // W5500 socket register #define Sn_MR 0x0000 #define Sn_CR 0x0001 @@ -204,6 +174,9 @@ enum Status { SOCK_CLOSED = 0x00, SOCK_INIT = 0x13, SOCK_LISTEN = 0x14, SOCK_SYNSENT = 0x15, SOCK_ESTABLISHED = 0x17, \ SOCK_CLOSE_WAIT = 0x1c, SOCK_UDP = 0x22, }; + + uint16_t sock_any_port; + /* * Constructor * @@ -275,11 +248,10 @@ */ bool is_connected(int socket); - int8_t Socket_macraw(uint8_t sn, uint16_t port, uint8_t flag) { //CHECK_SOCKNUM(); - + if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG; close(sn); @@ -291,7 +263,7 @@ } setSn_PORT(sn,port); setSn_CR(sn,Sn_CR_OPEN); - while(eth->getSn_CR(sn)); + while(getSn_CR(sn)); //sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn); //sock_is_sending &= ~(1<<sn); //sock_remained_size[sn] = 0; @@ -395,6 +367,11 @@ } spi_write(addr, cb, buf, sizeof(buf)); } + + void sreg_ip(int socket, uint16_t addr, const char* ip) { + reg_wr_ip(addr, (0x0C + (socket << 5)), ip); + } + void reg_rd_ip_byte(uint16_t addr, uint8_t* data) { spi_read(addr, 0x00, data, 4); } @@ -402,12 +379,7 @@ void reg_wr_ip_byte(uint16_t addr, uint8_t* data) { spi_write(addr, 0x04, data, 4); } - - void sreg_ip(int socket, uint16_t addr, const char* ip) { - reg_wr_ip(addr, (0x0C + (socket << 5)), ip); - } - - + ///////////////////////////////// // Common Register I/O function // ///////////////////////////////// @@ -1270,6 +1242,7 @@ return (getSn_TXBUF_SIZE(sn) << 10); } + protected: uint8_t mac[6]; uint32_t ip; @@ -1277,6 +1250,8 @@ uint32_t gateway; uint32_t dnsaddr; bool dhcp; + + static WIZnet_Chip* inst;