RTno is communicating library and framework which allows you to make your embedded device capable of communicating with RT-middleware world. RT-middleware is a platform software to realize Robotic system. In RTM, robots are developed by constructing robotics technologies\' elements (components) named RT-component. Therefore, the RTno helps you to create your own RT-component with your mbed and arduino. To know how to use your RTno device, visit here: http://ysuga.net/robot_e/rtm_e/rtc_e/1065?lang=en To know about RT-middleware and RT-component, visit http://www.openrtm.org
Dependencies: EthernetInterface mbed-rtos
EtherTcp.cpp@0:5f7bc45bc2e8, 2013-06-24 (annotated)
- Committer:
- ysuga
- Date:
- Mon Jun 24 06:42:11 2013 +0000
- Revision:
- 0:5f7bc45bc2e8
- Child:
- 1:f74116b37bc9
[mbed] converted /RTnoV3/RTnoV3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysuga | 0:5f7bc45bc2e8 | 1 | #include "mbed.h" |
ysuga | 0:5f7bc45bc2e8 | 2 | #include "EtherTcp.h" |
ysuga | 0:5f7bc45bc2e8 | 3 | #include "EthernetNetIf.h" |
ysuga | 0:5f7bc45bc2e8 | 4 | #include "TCPSocket.h" |
ysuga | 0:5f7bc45bc2e8 | 5 | |
ysuga | 0:5f7bc45bc2e8 | 6 | #include "ip_addr.h" |
ysuga | 0:5f7bc45bc2e8 | 7 | /** |
ysuga | 0:5f7bc45bc2e8 | 8 | #include <../SPI/SPI.h> |
ysuga | 0:5f7bc45bc2e8 | 9 | #include <../Ethernet/Ethernet.h> |
ysuga | 0:5f7bc45bc2e8 | 10 | |
ysuga | 0:5f7bc45bc2e8 | 11 | static EthernetServer *m_pServer; |
ysuga | 0:5f7bc45bc2e8 | 12 | static EthernetClient *m_pClient; |
ysuga | 0:5f7bc45bc2e8 | 13 | */ |
ysuga | 0:5f7bc45bc2e8 | 14 | static EthernetNetIf* m_pInterface; |
ysuga | 0:5f7bc45bc2e8 | 15 | static TCPSocket* m_pServerSocket; |
ysuga | 0:5f7bc45bc2e8 | 16 | static TCPSocket* m_pClientSocket; |
ysuga | 0:5f7bc45bc2e8 | 17 | |
ysuga | 0:5f7bc45bc2e8 | 18 | |
ysuga | 0:5f7bc45bc2e8 | 19 | #define ETCP_RX_BUFFER_SIZE 128 |
ysuga | 0:5f7bc45bc2e8 | 20 | unsigned char etcp_rx_buffer[ETCP_RX_BUFFER_SIZE]; |
ysuga | 0:5f7bc45bc2e8 | 21 | int etcp_rx_buffer_pointer_head = 0; |
ysuga | 0:5f7bc45bc2e8 | 22 | int etcp_rx_buffer_pointer_tail = 0; |
ysuga | 0:5f7bc45bc2e8 | 23 | Host m_Client; |
ysuga | 0:5f7bc45bc2e8 | 24 | |
ysuga | 0:5f7bc45bc2e8 | 25 | Serial *pSerial; |
ysuga | 0:5f7bc45bc2e8 | 26 | |
ysuga | 0:5f7bc45bc2e8 | 27 | /** |
ysuga | 0:5f7bc45bc2e8 | 28 | * Push data to ring buffer. |
ysuga | 0:5f7bc45bc2e8 | 29 | */ |
ysuga | 0:5f7bc45bc2e8 | 30 | int etcp_rx_buffer_push(unsigned char c) { |
ysuga | 0:5f7bc45bc2e8 | 31 | etcp_rx_buffer[etcp_rx_buffer_pointer_tail] = c; |
ysuga | 0:5f7bc45bc2e8 | 32 | etcp_rx_buffer_pointer_tail++; |
ysuga | 0:5f7bc45bc2e8 | 33 | if(etcp_rx_buffer_pointer_tail >= ETCP_RX_BUFFER_SIZE) { |
ysuga | 0:5f7bc45bc2e8 | 34 | etcp_rx_buffer_pointer_tail = 0; |
ysuga | 0:5f7bc45bc2e8 | 35 | } |
ysuga | 0:5f7bc45bc2e8 | 36 | return 0; |
ysuga | 0:5f7bc45bc2e8 | 37 | } |
ysuga | 0:5f7bc45bc2e8 | 38 | |
ysuga | 0:5f7bc45bc2e8 | 39 | /** |
ysuga | 0:5f7bc45bc2e8 | 40 | * Pop data fron ring buffer |
ysuga | 0:5f7bc45bc2e8 | 41 | */ |
ysuga | 0:5f7bc45bc2e8 | 42 | int etcp_rx_buffer_pop(unsigned char *c) { |
ysuga | 0:5f7bc45bc2e8 | 43 | *c = etcp_rx_buffer[etcp_rx_buffer_pointer_head]; |
ysuga | 0:5f7bc45bc2e8 | 44 | etcp_rx_buffer_pointer_head++; |
ysuga | 0:5f7bc45bc2e8 | 45 | if(etcp_rx_buffer_pointer_head >= ETCP_RX_BUFFER_SIZE) { |
ysuga | 0:5f7bc45bc2e8 | 46 | etcp_rx_buffer_pointer_head = 0; |
ysuga | 0:5f7bc45bc2e8 | 47 | } |
ysuga | 0:5f7bc45bc2e8 | 48 | return 0; |
ysuga | 0:5f7bc45bc2e8 | 49 | } |
ysuga | 0:5f7bc45bc2e8 | 50 | |
ysuga | 0:5f7bc45bc2e8 | 51 | int etcp_rx_buffer_get_size() { |
ysuga | 0:5f7bc45bc2e8 | 52 | int size = etcp_rx_buffer_pointer_tail - etcp_rx_buffer_pointer_head; |
ysuga | 0:5f7bc45bc2e8 | 53 | if(size < 0) { |
ysuga | 0:5f7bc45bc2e8 | 54 | size += ETCP_RX_BUFFER_SIZE; |
ysuga | 0:5f7bc45bc2e8 | 55 | } |
ysuga | 0:5f7bc45bc2e8 | 56 | return size; |
ysuga | 0:5f7bc45bc2e8 | 57 | } |
ysuga | 0:5f7bc45bc2e8 | 58 | |
ysuga | 0:5f7bc45bc2e8 | 59 | static void EtherTcp_onClientEvent(TCPSocketEvent e) { |
ysuga | 0:5f7bc45bc2e8 | 60 | switch (e) { |
ysuga | 0:5f7bc45bc2e8 | 61 | // If the socket is readable, do stuff |
ysuga | 0:5f7bc45bc2e8 | 62 | case TCPSOCKET_READABLE: |
ysuga | 0:5f7bc45bc2e8 | 63 | while(1) { |
ysuga | 0:5f7bc45bc2e8 | 64 | char buf; |
ysuga | 0:5f7bc45bc2e8 | 65 | int ret = m_pClientSocket->recv(&buf, 1); |
ysuga | 0:5f7bc45bc2e8 | 66 | if (ret == 0) break; |
ysuga | 0:5f7bc45bc2e8 | 67 | etcp_rx_buffer_push(buf); |
ysuga | 0:5f7bc45bc2e8 | 68 | } |
ysuga | 0:5f7bc45bc2e8 | 69 | |
ysuga | 0:5f7bc45bc2e8 | 70 | break; |
ysuga | 0:5f7bc45bc2e8 | 71 | case TCPSOCKET_CONTIMEOUT: |
ysuga | 0:5f7bc45bc2e8 | 72 | case TCPSOCKET_CONRST: |
ysuga | 0:5f7bc45bc2e8 | 73 | case TCPSOCKET_CONABRT: |
ysuga | 0:5f7bc45bc2e8 | 74 | case TCPSOCKET_ERROR: |
ysuga | 0:5f7bc45bc2e8 | 75 | case TCPSOCKET_DISCONNECTED: |
ysuga | 0:5f7bc45bc2e8 | 76 | delete m_pClientSocket; |
ysuga | 0:5f7bc45bc2e8 | 77 | m_pClientSocket = NULL; |
ysuga | 0:5f7bc45bc2e8 | 78 | break; |
ysuga | 0:5f7bc45bc2e8 | 79 | } |
ysuga | 0:5f7bc45bc2e8 | 80 | } |
ysuga | 0:5f7bc45bc2e8 | 81 | |
ysuga | 0:5f7bc45bc2e8 | 82 | static void EtherTcp_onServerEvent(TCPSocketEvent e) { |
ysuga | 0:5f7bc45bc2e8 | 83 | if(e == TCPSOCKET_ACCEPT ) { |
ysuga | 0:5f7bc45bc2e8 | 84 | if ( m_pServerSocket->accept(&m_Client, &m_pClientSocket) ) { |
ysuga | 0:5f7bc45bc2e8 | 85 | return; //Error in accept, discard connection |
ysuga | 0:5f7bc45bc2e8 | 86 | } |
ysuga | 0:5f7bc45bc2e8 | 87 | |
ysuga | 0:5f7bc45bc2e8 | 88 | m_pClientSocket->setOnEvent(EtherTcp_onClientEvent); |
ysuga | 0:5f7bc45bc2e8 | 89 | } |
ysuga | 0:5f7bc45bc2e8 | 90 | } |
ysuga | 0:5f7bc45bc2e8 | 91 | |
ysuga | 0:5f7bc45bc2e8 | 92 | void EtherTcp_init(uint8_t* mac, uint8_t* ip, |
ysuga | 0:5f7bc45bc2e8 | 93 | uint8_t* gateway, uint8_t* subnet, |
ysuga | 0:5f7bc45bc2e8 | 94 | uint16_t port) |
ysuga | 0:5f7bc45bc2e8 | 95 | |
ysuga | 0:5f7bc45bc2e8 | 96 | { |
ysuga | 0:5f7bc45bc2e8 | 97 | pSerial = new Serial(USBTX, USBRX); |
ysuga | 0:5f7bc45bc2e8 | 98 | m_pInterface = new EthernetNetIf( |
ysuga | 0:5f7bc45bc2e8 | 99 | IpAddr(ip[0], ip[1], ip[2], ip[3]), |
ysuga | 0:5f7bc45bc2e8 | 100 | IpAddr(subnet[0], subnet[1], subnet[2], subnet[3]), |
ysuga | 0:5f7bc45bc2e8 | 101 | IpAddr(gateway[0], gateway[1], gateway[2], gateway[3]), |
ysuga | 0:5f7bc45bc2e8 | 102 | IpAddr(gateway[0], gateway[1], gateway[2], gateway[3])); |
ysuga | 0:5f7bc45bc2e8 | 103 | printf("Hello %d %d %d %d\r\n", ip[0], ip[1], ip[2], ip[3]); |
ysuga | 0:5f7bc45bc2e8 | 104 | EthernetErr ethErr = m_pInterface->setup(); |
ysuga | 0:5f7bc45bc2e8 | 105 | if (ethErr) { |
ysuga | 0:5f7bc45bc2e8 | 106 | return; |
ysuga | 0:5f7bc45bc2e8 | 107 | } |
ysuga | 0:5f7bc45bc2e8 | 108 | |
ysuga | 0:5f7bc45bc2e8 | 109 | m_pServerSocket = new TCPSocket(); |
ysuga | 0:5f7bc45bc2e8 | 110 | m_pServerSocket->setOnEvent(EtherTcp_onServerEvent); |
ysuga | 0:5f7bc45bc2e8 | 111 | m_pServerSocket->bind(Host(IP_ADDR_ANY, port)); |
ysuga | 0:5f7bc45bc2e8 | 112 | m_pServerSocket->listen(); |
ysuga | 0:5f7bc45bc2e8 | 113 | SerialDevice_available = EtherTcp_available; |
ysuga | 0:5f7bc45bc2e8 | 114 | SerialDevice_getc = EtherTcp_getc; |
ysuga | 0:5f7bc45bc2e8 | 115 | SerialDevice_putc = EtherTcp_putc; |
ysuga | 0:5f7bc45bc2e8 | 116 | } |
ysuga | 0:5f7bc45bc2e8 | 117 | |
ysuga | 0:5f7bc45bc2e8 | 118 | uint8_t EtherTcp_available() |
ysuga | 0:5f7bc45bc2e8 | 119 | { |
ysuga | 0:5f7bc45bc2e8 | 120 | Net::poll(); |
ysuga | 0:5f7bc45bc2e8 | 121 | return etcp_rx_buffer_get_size(); |
ysuga | 0:5f7bc45bc2e8 | 122 | } |
ysuga | 0:5f7bc45bc2e8 | 123 | |
ysuga | 0:5f7bc45bc2e8 | 124 | |
ysuga | 0:5f7bc45bc2e8 | 125 | void EtherTcp_putc(const char c) { |
ysuga | 0:5f7bc45bc2e8 | 126 | if(m_pClientSocket != NULL) { |
ysuga | 0:5f7bc45bc2e8 | 127 | m_pClientSocket->send(&c, 1); |
ysuga | 0:5f7bc45bc2e8 | 128 | } |
ysuga | 0:5f7bc45bc2e8 | 129 | } |
ysuga | 0:5f7bc45bc2e8 | 130 | |
ysuga | 0:5f7bc45bc2e8 | 131 | char EtherTcp_getc() |
ysuga | 0:5f7bc45bc2e8 | 132 | { |
ysuga | 0:5f7bc45bc2e8 | 133 | char c; |
ysuga | 0:5f7bc45bc2e8 | 134 | etcp_rx_buffer_pop((unsigned char*)&c); |
ysuga | 0:5f7bc45bc2e8 | 135 | return c; |
ysuga | 0:5f7bc45bc2e8 | 136 | } |