![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
main.cpp@0:715023d993d6, 2011-10-17 (annotated)
- Committer:
- gbeardall
- Date:
- Mon Oct 17 10:39:17 2011 +0000
- Revision:
- 0:715023d993d6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gbeardall | 0:715023d993d6 | 1 | /* |
gbeardall | 0:715023d993d6 | 2 | * eth1 |
gbeardall | 0:715023d993d6 | 3 | * |
gbeardall | 0:715023d993d6 | 4 | * http://mbed.org/cookbook/Ethernet |
gbeardall | 0:715023d993d6 | 5 | * |
gbeardall | 0:715023d993d6 | 6 | */ |
gbeardall | 0:715023d993d6 | 7 | |
gbeardall | 0:715023d993d6 | 8 | #include "mbed.h" |
gbeardall | 0:715023d993d6 | 9 | |
gbeardall | 0:715023d993d6 | 10 | #include "EthernetNetIf.h" |
gbeardall | 0:715023d993d6 | 11 | #include "TCPSocket.h" |
gbeardall | 0:715023d993d6 | 12 | #include "ipaddr.h" |
gbeardall | 0:715023d993d6 | 13 | #include "host.h" |
gbeardall | 0:715023d993d6 | 14 | |
gbeardall | 0:715023d993d6 | 15 | |
gbeardall | 0:715023d993d6 | 16 | EthernetNetIf eth; |
gbeardall | 0:715023d993d6 | 17 | |
gbeardall | 0:715023d993d6 | 18 | |
gbeardall | 0:715023d993d6 | 19 | DigitalOut myled(LED1); |
gbeardall | 0:715023d993d6 | 20 | |
gbeardall | 0:715023d993d6 | 21 | char* sockErr(int err, int flag=0) { |
gbeardall | 0:715023d993d6 | 22 | static char buff[32]; |
gbeardall | 0:715023d993d6 | 23 | |
gbeardall | 0:715023d993d6 | 24 | if(flag == 0 && err == TCPSOCKET_OK) ///<Success |
gbeardall | 0:715023d993d6 | 25 | return "TCPSOCKET_OK"; |
gbeardall | 0:715023d993d6 | 26 | |
gbeardall | 0:715023d993d6 | 27 | switch(err) { |
gbeardall | 0:715023d993d6 | 28 | case TCPSOCKET_SETUP: ///<TCPSocket not properly configured |
gbeardall | 0:715023d993d6 | 29 | return "TCPSOCKET_SETUP"; |
gbeardall | 0:715023d993d6 | 30 | case TCPSOCKET_TIMEOUT: ///<Connection timed out |
gbeardall | 0:715023d993d6 | 31 | return "TCPSOCKET_TIMEOUT"; |
gbeardall | 0:715023d993d6 | 32 | case TCPSOCKET_IF: ///<Interface has problems, does not exist or is not initialized |
gbeardall | 0:715023d993d6 | 33 | return "TCPSOCKET_IF"; |
gbeardall | 0:715023d993d6 | 34 | case TCPSOCKET_MEM: ///<Not enough mem |
gbeardall | 0:715023d993d6 | 35 | return "TCPSOCKET_MEM"; |
gbeardall | 0:715023d993d6 | 36 | case TCPSOCKET_INUSE: ///<Interface / Port is in use |
gbeardall | 0:715023d993d6 | 37 | return "TCPSOCKET_INUSE"; |
gbeardall | 0:715023d993d6 | 38 | case TCPSOCKET_EMPTY: ///<Connections queue is empty |
gbeardall | 0:715023d993d6 | 39 | return "TCPSOCKET_EMPTY"; |
gbeardall | 0:715023d993d6 | 40 | case TCPSOCKET_RST: |
gbeardall | 0:715023d993d6 | 41 | return "TCPSOCKET_RST"; |
gbeardall | 0:715023d993d6 | 42 | } |
gbeardall | 0:715023d993d6 | 43 | |
gbeardall | 0:715023d993d6 | 44 | sprintf(buff, "%d", err); |
gbeardall | 0:715023d993d6 | 45 | |
gbeardall | 0:715023d993d6 | 46 | return buff; |
gbeardall | 0:715023d993d6 | 47 | } // sockErr |
gbeardall | 0:715023d993d6 | 48 | |
gbeardall | 0:715023d993d6 | 49 | int main() { |
gbeardall | 0:715023d993d6 | 50 | |
gbeardall | 0:715023d993d6 | 51 | printf("eth1\n\r"); |
gbeardall | 0:715023d993d6 | 52 | |
gbeardall | 0:715023d993d6 | 53 | EthernetErr etherr = eth.setup(); |
gbeardall | 0:715023d993d6 | 54 | printf("etherr=%d\n\r", etherr); |
gbeardall | 0:715023d993d6 | 55 | |
gbeardall | 0:715023d993d6 | 56 | |
gbeardall | 0:715023d993d6 | 57 | TCPSocket sock; |
gbeardall | 0:715023d993d6 | 58 | TCPSocketErr sockerr; |
gbeardall | 0:715023d993d6 | 59 | |
gbeardall | 0:715023d993d6 | 60 | #if 0 |
gbeardall | 0:715023d993d6 | 61 | // Send/recv |
gbeardall | 0:715023d993d6 | 62 | |
gbeardall | 0:715023d993d6 | 63 | IpAddr ip(209,85,147,106); // google.com |
gbeardall | 0:715023d993d6 | 64 | |
gbeardall | 0:715023d993d6 | 65 | Host host(ip,80,"google.com"); |
gbeardall | 0:715023d993d6 | 66 | |
gbeardall | 0:715023d993d6 | 67 | sockerr = sock.connect(host); |
gbeardall | 0:715023d993d6 | 68 | printf("connect=%s\n\r", sockErr(sockerr)); |
gbeardall | 0:715023d993d6 | 69 | |
gbeardall | 0:715023d993d6 | 70 | int len = sock.send("GET /\n\r", 7); |
gbeardall | 0:715023d993d6 | 71 | printf("send=%s\n\r", sockErr(len,1)); |
gbeardall | 0:715023d993d6 | 72 | |
gbeardall | 0:715023d993d6 | 73 | char buff[1024]; |
gbeardall | 0:715023d993d6 | 74 | len = sock.recv(buff, sizeof(buff)); |
gbeardall | 0:715023d993d6 | 75 | printf("recv=%s\n\r", sockErr(len,1)); |
gbeardall | 0:715023d993d6 | 76 | #endif |
gbeardall | 0:715023d993d6 | 77 | |
gbeardall | 0:715023d993d6 | 78 | #if 1 |
gbeardall | 0:715023d993d6 | 79 | // Listener |
gbeardall | 0:715023d993d6 | 80 | |
gbeardall | 0:715023d993d6 | 81 | Host me(eth.getIp(), 80, "mbed"); |
gbeardall | 0:715023d993d6 | 82 | sockerr = sock.bind(me); |
gbeardall | 0:715023d993d6 | 83 | printf("bind=%s\n\r", sockErr(sockerr)); |
gbeardall | 0:715023d993d6 | 84 | |
gbeardall | 0:715023d993d6 | 85 | sockerr = sock.listen(); |
gbeardall | 0:715023d993d6 | 86 | printf("listen=%s\n\r", sockErr(sockerr)); |
gbeardall | 0:715023d993d6 | 87 | |
gbeardall | 0:715023d993d6 | 88 | Host client; |
gbeardall | 0:715023d993d6 | 89 | TCPSocket* pNewTcpSocket; |
gbeardall | 0:715023d993d6 | 90 | sockerr = sock.accept(&client, &pNewTcpSocket); |
gbeardall | 0:715023d993d6 | 91 | printf("accept=%s\n\r", sockErr(sockerr)); |
gbeardall | 0:715023d993d6 | 92 | #endif |
gbeardall | 0:715023d993d6 | 93 | |
gbeardall | 0:715023d993d6 | 94 | } // main |
gbeardall | 0:715023d993d6 | 95 | |
gbeardall | 0:715023d993d6 | 96 | // == end == |