W5200(WIZ820io) network interface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MyNetTcpSocket.cpp Source File

MyNetTcpSocket.cpp

00001 // MyNetTcpSocket.cpp 2012/4/19
00002 #include "mbed.h"
00003 #include "w5100.h"
00004 #include "MyNetTcpSocket.h"
00005 //#define __DEBUG
00006 #include "dbg/dbg.h"
00007 
00008 #ifdef __DEBUG
00009 #define DBG2(...) do{ DebugStream::debug("%p %s ", this,__PRETTY_FUNCTION__); DebugStream::debug(__VA_ARGS__); } while(0);
00010 #else
00011 #define DBG2(...) while(0);
00012 #endif //__DEBUG
00013 
00014 //#define DEBUG
00015 
00016 #ifdef DEBUG
00017 #include "Utils.h"
00018 #define PRINT_FUNC() printf("%p %d:%s\n", this,__LINE__,__PRETTY_FUNCTION__)
00019 #else //DEBUG
00020 #define PRINT_FUNC()
00021 #endif //DEBUG
00022 
00023 int w5200_new_socket() 
00024 {
00025     for(int i = 0; i < MAX_SOCK_NUM; i++) {
00026         if (W5100.readSnMR(i) == SnMR::CLOSE) { // 0x00
00027             if (W5100.readSnSR(i) != SnSR::LISTEN) { // 0x14
00028                 return i;
00029             }
00030         }
00031     }
00032     return -1; // not found
00033 }
00034 
00035 MyNetTcpSocket::MyNetTcpSocket(int socket) : NetTcpSocket(),_socket(socket),wait_accept(false)  {
00036     PRINT_FUNC();
00037     DBG2("socket: %d\n", socket);
00038     if (_socket == (-1)) {
00039         _socket = w5200_new_socket();
00040     }
00041 #ifdef DEBUG
00042     printf("%p socket: %d\n", this, _socket);
00043 #endif //DEBUG
00044     if (_socket != (-1)) {
00045         W5100.writeSnMR(_socket, SnMR::TCP); // set TCP mode
00046     }
00047 }
00048 
00049 MyNetTcpSocket::~MyNetTcpSocket() {
00050     PRINT_FUNC();
00051     DBG2("socket=%d\n", _socket);
00052     close();
00053     if (_socket != (-1)) {
00054         W5100.writeSnMR(_socket, SnMR::CLOSE);
00055     }
00056 }
00057 
00058 NetTcpSocketErr MyNetTcpSocket::bind(const Host& me) {
00059     PRINT_FUNC();
00060     if (_socket == (-1)) {
00061         return NETTCPSOCKET_MEM;
00062     }
00063     int port = me.getPort();
00064     W5100.writeSnPORT(_socket, port);
00065     return NETTCPSOCKET_OK;
00066 }
00067 
00068 NetTcpSocketErr MyNetTcpSocket::listen() {
00069     PRINT_FUNC();
00070     if (_socket == (-1)) {
00071         return NETTCPSOCKET_MEM;
00072     }
00073     W5100.execCmdSn(_socket, Sock_OPEN); // set OPEN command
00074     W5100.execCmdSn(_socket, Sock_LISTEN); // listen
00075 #ifdef DEBUG
00076     uint8_t ip[4];
00077     printf("socket:%d SnMR:%02x SnIR:%02x SnSR:%02x\n", _socket, 
00078         W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
00079     W5100.getIPAddress(ip);
00080     printf("SIPR: %d.%d.%d.%d Sn_PORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnPORT(_socket));
00081 #endif //DEBUG
00082     wait_accept = true;
00083     return NETTCPSOCKET_OK;
00084 }
00085 
00086 NetTcpSocketErr MyNetTcpSocket::connect(const Host& host) {
00087     PRINT_FUNC();
00088     if (_socket == (-1)) {
00089         return NETTCPSOCKET_MEM;
00090     }
00091     uint8_t ip[4];
00092     ip[0] = host.getIp()[0];
00093     ip[1] = host.getIp()[1];
00094     ip[2] = host.getIp()[2];
00095     ip[3] = host.getIp()[3];
00096     int port = host.getPort();
00097     W5100.writeSnDIPR(_socket, ip);
00098     W5100.writeSnDPORT(_socket, port);
00099     if (W5100.readSnPORT(_socket) == 0) {
00100         W5100.writeSnPORT(_socket, 1024 + _socket);
00101     }
00102     W5100.execCmdSn(_socket, Sock_OPEN); // set OPEN command
00103     W5100.execCmdSn(_socket, Sock_CONNECT);
00104 #ifdef DEBUG
00105     printf("socket:%d SnMR:%02x SnIR:%02x SnSR:%02x\n", _socket, 
00106         W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
00107     W5100.getIPAddress(ip);
00108     printf("SIPR: %d.%d.%d.%d Sn_PORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnPORT(_socket));
00109     W5100.readSnDIPR(_socket, ip);
00110     printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
00111 #endif //DEBUG
00112     return NETTCPSOCKET_OK;
00113 }
00114 
00115 NetTcpSocketErr MyNetTcpSocket::accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) {
00116     PRINT_FUNC();
00117     if (_socket == (-1)) {
00118         return NETTCPSOCKET_MEM;
00119     }
00120     uint8_t ip[4];
00121     W5100.readSnDIPR(_socket, ip);
00122     pClient->setIp(IpAddr(ip[0],ip[1],ip[2],ip[3]));
00123     int port = W5100.readSnDPORT(_socket);
00124     pClient->setPort(port);
00125     Host me;
00126     me.setPort(W5100.readSnPORT(_socket));
00127     MyNetTcpSocket* pNewNetTcpSocket = new MyNetTcpSocket(_socket);
00128     if (pNewNetTcpSocket == NULL) {
00129         return NETTCPSOCKET_EMPTY;
00130     }
00131     pNewNetTcpSocket->m_refs++;
00132     *ppNewNetTcpSocket = pNewNetTcpSocket;
00133     _socket = w5200_new_socket();
00134     if (_socket != (-1)) {
00135         W5100.writeSnMR(_socket, SnMR::TCP); // set TCP mode
00136         bind(me);
00137         listen();
00138      }
00139     return NETTCPSOCKET_OK;
00140 }
00141 
00142 int /*if < 0 : NetTcpSocketErr*/ MyNetTcpSocket::send(const char* buf, int len) {
00143     PRINT_FUNC();
00144 #ifdef DEBUG
00145     printf("buf:%p, len=%d\n", buf, len);
00146     printHex((u8*)buf, len);
00147 #endif //DEBUG
00148     if (_socket == (-1)) {
00149         return NETTCPSOCKET_MEM;
00150     }
00151     if (len > 0) {
00152         W5100.send_data_processing(_socket, (uint8_t*)buf, len);
00153         W5100.execCmdSn(_socket, Sock_SEND);
00154     }
00155     return len;
00156 }
00157 
00158 int /*if < 0 : NetTcpSocketErr*/ MyNetTcpSocket::recv(char* buf, int len){
00159     PRINT_FUNC();
00160     if (_socket == (-1)) {
00161         return NETTCPSOCKET_MEM;
00162     }
00163     int size = W5100.getRXReceivedSize(_socket);
00164     if (size > len) {
00165         size = len;
00166     }
00167     if (size > 0) {
00168         W5100.recv_data_processing(_socket, (uint8_t*)buf, size);
00169         W5100.execCmdSn(_socket, Sock_RECV);
00170     }
00171 #ifdef DEBUG
00172     printHex((uint8_t*)buf, size);
00173 #endif //DEBUG
00174     return size;
00175 }
00176 
00177 NetTcpSocketErr MyNetTcpSocket::close() {
00178     PRINT_FUNC();
00179     DBG2("m_closed=%d m_refs=%d\n", m_closed, m_refs);
00180     if(m_closed) {
00181         return NETTCPSOCKET_OK;
00182     }
00183     m_closed = true;
00184     cleanUp();
00185     if (_socket != (-1)) {
00186         W5100.execCmdSn(_socket, Sock_DISCON);
00187         W5100.execCmdSn(_socket, Sock_CLOSE);
00188     }
00189     return NETTCPSOCKET_OK;
00190 }
00191 
00192 NetTcpSocketErr MyNetTcpSocket::poll(){
00193     PRINT_FUNC();
00194     NetTcpSocket::flushEvents();
00195 #ifdef DEBUG
00196     printf("%p socket:%d\n", this,_socket);
00197     if (_socket != (-1)) {
00198         printf("SnMR:%02x SnIR:%02x SnSR:%02x\n", 
00199             W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
00200         uint8_t ip[4];
00201         W5100.readSnDIPR(_socket, ip);
00202         printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
00203         printf("Sn_RX_RSR:%5d, Sn_RX_RD:%5d, Sn_RX_WR:%5d\n",
00204                 W5100.readSnRX_RSR(_socket), W5100.readSnRX_RD(_socket), W5100.readSnRX_WR(_socket));
00205         printf("Sn_TX_FSR:%5d, Sn_TX_RD:%5d, Sn_TX_WR:%5d\n",
00206                 W5100.readSnTX_FSR(_socket), W5100.readSnTX_RD(_socket), W5100.readSnTX_WR(_socket));
00207     }
00208     wait_ms(200);
00209 #endif //DEBUG
00210     if (_socket == (-1)) {
00211         return NETTCPSOCKET_OK;
00212     }
00213     uint8_t Sn_SR = W5100.readSnSR(_socket);
00214     if (wait_accept) {
00215         if (Sn_SR == 0x17) {
00216             queueEvent(NETTCPSOCKET_ACCEPT);
00217             wait_accept = false;
00218         }
00219     }
00220     if (Sn_SR == 0x1c) {
00221         queueEvent(NETTCPSOCKET_CONRST);
00222     }
00223     if (W5100.getRXReceivedSize(_socket) > 0) {
00224         queueEvent(NETTCPSOCKET_READABLE);
00225     }
00226     if (Sn_SR == 0x17) {
00227         queueEvent(NETTCPSOCKET_CONNECTED);
00228         if (W5100.getTXFreeSize(_socket) > 0) {
00229             queueEvent(NETTCPSOCKET_WRITEABLE);
00230         }
00231     }
00232     if (Sn_SR == 0x00) {
00233         queueEvent(NETTCPSOCKET_DISCONNECTED);
00234     }
00235     return NETTCPSOCKET_OK;
00236 }
00237 
00238 void MyNetTcpSocket::cleanUp() //Flush input buffer
00239 {
00240     PRINT_FUNC();
00241     if (_socket == (-1)) {
00242         return;
00243     }
00244     while(W5100.getRXReceivedSize(_socket) > 0) {
00245         uint8_t temp[1];
00246         W5100.recv_data_processing(_socket, temp, 1);
00247         W5100.execCmdSn(_socket, Sock_RECV);
00248     }    
00249 }