W5200(WIZ820io) network interface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MyNetUdpSocket.cpp Source File

MyNetUdpSocket.cpp

00001 // MyNetUdpSocket.cpp 2012/4/16
00002 #include "mbed.h"
00003 #include "MyNetUdpSocket.h"
00004 #include "w5100.h"
00005 
00006 //#define DEBUG
00007 
00008 #ifdef DEBUG
00009 #include "Utils.h"
00010 #define PRINT_FUNC() printf("%p %d:%s\n", this,__LINE__,__PRETTY_FUNCTION__)
00011 #else //DEBUG
00012 #define PRINT_FUNC()
00013 #endif //DEBUG
00014 
00015 extern int w5200_new_socket(); // MyNetTcpSocket.cpp
00016 
00017 MyNetUdpSocket::MyNetUdpSocket(int socket) : NetUdpSocket(),_socket(socket)  {
00018     PRINT_FUNC();
00019     if (_socket == (-1)) {
00020         _socket = w5200_new_socket();
00021     }
00022     if (_socket != (-1)) {
00023         W5100.writeSnMR(_socket, SnMR::UDP); // set UDP mode
00024     }
00025 }
00026 
00027 MyNetUdpSocket::~MyNetUdpSocket() {
00028     PRINT_FUNC();
00029     close();
00030     if (_socket != (-1)) {
00031         W5100.writeSnMR(_socket, SnMR::CLOSE);
00032     }
00033 }
00034 
00035 NetUdpSocketErr MyNetUdpSocket::bind(const Host& me) {
00036     PRINT_FUNC();
00037     if (_socket == (-1)) {
00038         return NETUDPSOCKET_MEM;
00039     }
00040     int port = me.getPort();
00041     W5100.writeSnPORT(_socket, port);
00042     W5100.execCmdSn( _socket, Sock_OPEN); // set OPEN command
00043     return NETUDPSOCKET_OK;
00044 }
00045 
00046 int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::sendto(const char* buf, int len, Host* pHost) {
00047     PRINT_FUNC();
00048     if (_socket == (-1)) {
00049         return NETUDPSOCKET_MEM;
00050     }
00051     uint8_t ip[4];
00052     ip[0] = pHost->getIp()[0];
00053     ip[1] = pHost->getIp()[1];
00054     ip[2] = pHost->getIp()[2];
00055     ip[3] = pHost->getIp()[3];
00056     int port = pHost->getPort();
00057     W5100.writeSnDIPR(_socket, ip);
00058     W5100.writeSnDPORT(_socket, port);
00059     W5100.send_data_processing(_socket, (uint8_t*)buf, len);
00060     W5100.execCmdSn(_socket, Sock_SEND);
00061 #ifdef DEBUG
00062     printHex((u8*)buf, len);
00063     W5100.getIPAddress(ip);
00064     printf("SIPR: %d.%d.%d.%d Sn_PORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnPORT(_socket));
00065     W5100.readSnDIPR(_socket, ip);
00066     printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
00067 #endif //DEBUG
00068     return len;
00069 }
00070 
00071 int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::recvfrom(char* buf, int len, Host* pHost) {
00072     PRINT_FUNC();
00073     if (_socket == (-1)) {
00074         return NETUDPSOCKET_MEM;
00075     }
00076     int size = W5100.getRXReceivedSize(_socket);
00077     if (size < 8) {
00078         return -1;
00079     }
00080     uint8_t info[8];
00081     W5100.recv_data_processing(_socket, info, 8);
00082     W5100.execCmdSn(_socket, Sock_RECV);
00083     pHost->setIp(IpAddr(info[0],info[1],info[2],info[3]));
00084     pHost->setPort(info[4]<<8|info[5]);
00085     size -= 8;
00086     if (size > len) {
00087         size = len;
00088     }    
00089     W5100.recv_data_processing(_socket, (uint8_t*)buf, size);
00090     W5100.execCmdSn(_socket, Sock_RECV);
00091 #ifdef DEBUG
00092     printfBytes("UDP PACKET-INFO", (u8*)info, 8);
00093     printHex((u8*)buf, size);
00094 #endif //DEBUG
00095     return size;
00096 }
00097 
00098 NetUdpSocketErr MyNetUdpSocket::close() {
00099     PRINT_FUNC();
00100     if(m_closed) {
00101         return NETUDPSOCKET_OK;
00102     }
00103     if (_socket == (-1)) {
00104         return NETUDPSOCKET_MEM;
00105     }
00106     m_closed = true;
00107     cleanUp();
00108     W5100.writeSnMR(_socket, SnMR::CLOSE);
00109     return NETUDPSOCKET_OK;
00110 }
00111 
00112 NetUdpSocketErr MyNetUdpSocket::poll() {
00113     PRINT_FUNC();
00114     NetUdpSocket::flushEvents();
00115 #ifdef DEBUG
00116     printf("%p socket:%d\n", this,_socket); 
00117     if (_socket != (-1)) {
00118         printf("SnMR:%02x SnIR:%02x SnSR:%02x\n",  
00119             W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
00120         uint8_t ip[4];
00121         W5100.readSnDIPR(_socket, ip);
00122         printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
00123         uint8_t mac[6];
00124         W5100.readSnDHAR(_socket, mac);
00125         printf("Sn_DHAR: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00126         printf("Sn_RX_RSR:%5d, Sn_RX_RD:%5d, Sn_RX_WR:%5d\n",
00127                 W5100.readSnRX_RSR(_socket), W5100.readSnRX_RD(_socket), W5100.readSnRX_WR(_socket));
00128         printf("Sn_TX_FSR:%5d, Sn_TX_RD:%5d, Sn_TX_WR:%5d\n",
00129                 W5100.readSnTX_FSR(_socket), W5100.readSnTX_RD(_socket), W5100.readSnTX_WR(_socket));
00130     }
00131     wait_ms(200);    
00132 #endif //DEBUG
00133     if (_socket != (-1)) {
00134         if (W5100.getRXReceivedSize(_socket) > 0) {
00135             queueEvent(NETUDPSOCKET_READABLE);
00136         }
00137     }
00138     return NETUDPSOCKET_OK;
00139 }
00140 
00141 void MyNetUdpSocket::cleanUp() //Flush input buffer
00142 {
00143     PRINT_FUNC();
00144     if (_socket == (-1)) {
00145         return;
00146     }
00147     while(W5100.getRXReceivedSize(_socket) > 0) {
00148         uint8_t temp[1];
00149         W5100.recv_data_processing(_socket, temp, 1);
00150         W5100.execCmdSn(_socket, Sock_RECV);
00151     }    
00152 }