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
Dependents: RTnoV3_LED RTnoV3_Template RTnoV3_ADC RTnoV3_Timer ... more
EtherTcp.cpp@0:9fac71a0bff3, 2012-02-09 (annotated)
- Committer:
- ysuga
- Date:
- Thu Feb 09 02:33:10 2012 +0000
- Revision:
- 0:9fac71a0bff3
RTno Version 3: RTno is a software library and tool to connect embedded devices like arduino and mbed to RT-middleware world.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysuga | 0:9fac71a0bff3 | 1 | #include "mbed.h" |
ysuga | 0:9fac71a0bff3 | 2 | #include "EtherTcp.h" |
ysuga | 0:9fac71a0bff3 | 3 | #include "EthernetNetIf.h" |
ysuga | 0:9fac71a0bff3 | 4 | #include "TCPSocket.h" |
ysuga | 0:9fac71a0bff3 | 5 | |
ysuga | 0:9fac71a0bff3 | 6 | #include "ip_addr.h" |
ysuga | 0:9fac71a0bff3 | 7 | /** |
ysuga | 0:9fac71a0bff3 | 8 | #include <../SPI/SPI.h> |
ysuga | 0:9fac71a0bff3 | 9 | #include <../Ethernet/Ethernet.h> |
ysuga | 0:9fac71a0bff3 | 10 | |
ysuga | 0:9fac71a0bff3 | 11 | static EthernetServer *m_pServer; |
ysuga | 0:9fac71a0bff3 | 12 | static EthernetClient *m_pClient; |
ysuga | 0:9fac71a0bff3 | 13 | */ |
ysuga | 0:9fac71a0bff3 | 14 | static EthernetNetIf* m_pInterface; |
ysuga | 0:9fac71a0bff3 | 15 | static TCPSocket* m_pServerSocket; |
ysuga | 0:9fac71a0bff3 | 16 | static TCPSocket* m_pClientSocket; |
ysuga | 0:9fac71a0bff3 | 17 | |
ysuga | 0:9fac71a0bff3 | 18 | |
ysuga | 0:9fac71a0bff3 | 19 | #define ETCP_RX_BUFFER_SIZE 128 |
ysuga | 0:9fac71a0bff3 | 20 | unsigned char etcp_rx_buffer[ETCP_RX_BUFFER_SIZE]; |
ysuga | 0:9fac71a0bff3 | 21 | int etcp_rx_buffer_pointer_head = 0; |
ysuga | 0:9fac71a0bff3 | 22 | int etcp_rx_buffer_pointer_tail = 0; |
ysuga | 0:9fac71a0bff3 | 23 | Host m_Client; |
ysuga | 0:9fac71a0bff3 | 24 | |
ysuga | 0:9fac71a0bff3 | 25 | Serial *pSerial; |
ysuga | 0:9fac71a0bff3 | 26 | |
ysuga | 0:9fac71a0bff3 | 27 | /** |
ysuga | 0:9fac71a0bff3 | 28 | * Push data to ring buffer. |
ysuga | 0:9fac71a0bff3 | 29 | */ |
ysuga | 0:9fac71a0bff3 | 30 | int etcp_rx_buffer_push(unsigned char c) { |
ysuga | 0:9fac71a0bff3 | 31 | etcp_rx_buffer[etcp_rx_buffer_pointer_tail] = c; |
ysuga | 0:9fac71a0bff3 | 32 | etcp_rx_buffer_pointer_tail++; |
ysuga | 0:9fac71a0bff3 | 33 | if(etcp_rx_buffer_pointer_tail >= ETCP_RX_BUFFER_SIZE) { |
ysuga | 0:9fac71a0bff3 | 34 | etcp_rx_buffer_pointer_tail = 0; |
ysuga | 0:9fac71a0bff3 | 35 | } |
ysuga | 0:9fac71a0bff3 | 36 | return 0; |
ysuga | 0:9fac71a0bff3 | 37 | } |
ysuga | 0:9fac71a0bff3 | 38 | |
ysuga | 0:9fac71a0bff3 | 39 | /** |
ysuga | 0:9fac71a0bff3 | 40 | * Pop data fron ring buffer |
ysuga | 0:9fac71a0bff3 | 41 | */ |
ysuga | 0:9fac71a0bff3 | 42 | int etcp_rx_buffer_pop(unsigned char *c) { |
ysuga | 0:9fac71a0bff3 | 43 | *c = etcp_rx_buffer[etcp_rx_buffer_pointer_head]; |
ysuga | 0:9fac71a0bff3 | 44 | etcp_rx_buffer_pointer_head++; |
ysuga | 0:9fac71a0bff3 | 45 | if(etcp_rx_buffer_pointer_head >= ETCP_RX_BUFFER_SIZE) { |
ysuga | 0:9fac71a0bff3 | 46 | etcp_rx_buffer_pointer_head = 0; |
ysuga | 0:9fac71a0bff3 | 47 | } |
ysuga | 0:9fac71a0bff3 | 48 | return 0; |
ysuga | 0:9fac71a0bff3 | 49 | } |
ysuga | 0:9fac71a0bff3 | 50 | |
ysuga | 0:9fac71a0bff3 | 51 | int etcp_rx_buffer_get_size() { |
ysuga | 0:9fac71a0bff3 | 52 | int size = etcp_rx_buffer_pointer_tail - etcp_rx_buffer_pointer_head; |
ysuga | 0:9fac71a0bff3 | 53 | if(size < 0) { |
ysuga | 0:9fac71a0bff3 | 54 | size += ETCP_RX_BUFFER_SIZE; |
ysuga | 0:9fac71a0bff3 | 55 | } |
ysuga | 0:9fac71a0bff3 | 56 | return size; |
ysuga | 0:9fac71a0bff3 | 57 | } |
ysuga | 0:9fac71a0bff3 | 58 | |
ysuga | 0:9fac71a0bff3 | 59 | static void EtherTcp_onClientEvent(TCPSocketEvent e) { |
ysuga | 0:9fac71a0bff3 | 60 | switch (e) { |
ysuga | 0:9fac71a0bff3 | 61 | // If the socket is readable, do stuff |
ysuga | 0:9fac71a0bff3 | 62 | case TCPSOCKET_READABLE: |
ysuga | 0:9fac71a0bff3 | 63 | while(1) { |
ysuga | 0:9fac71a0bff3 | 64 | char buf; |
ysuga | 0:9fac71a0bff3 | 65 | int ret = m_pClientSocket->recv(&buf, 1); |
ysuga | 0:9fac71a0bff3 | 66 | if (ret == 0) break; |
ysuga | 0:9fac71a0bff3 | 67 | etcp_rx_buffer_push(buf); |
ysuga | 0:9fac71a0bff3 | 68 | } |
ysuga | 0:9fac71a0bff3 | 69 | |
ysuga | 0:9fac71a0bff3 | 70 | break; |
ysuga | 0:9fac71a0bff3 | 71 | case TCPSOCKET_CONTIMEOUT: |
ysuga | 0:9fac71a0bff3 | 72 | case TCPSOCKET_CONRST: |
ysuga | 0:9fac71a0bff3 | 73 | case TCPSOCKET_CONABRT: |
ysuga | 0:9fac71a0bff3 | 74 | case TCPSOCKET_ERROR: |
ysuga | 0:9fac71a0bff3 | 75 | case TCPSOCKET_DISCONNECTED: |
ysuga | 0:9fac71a0bff3 | 76 | delete m_pClientSocket; |
ysuga | 0:9fac71a0bff3 | 77 | m_pClientSocket = NULL; |
ysuga | 0:9fac71a0bff3 | 78 | break; |
ysuga | 0:9fac71a0bff3 | 79 | } |
ysuga | 0:9fac71a0bff3 | 80 | } |
ysuga | 0:9fac71a0bff3 | 81 | |
ysuga | 0:9fac71a0bff3 | 82 | static void EtherTcp_onServerEvent(TCPSocketEvent e) { |
ysuga | 0:9fac71a0bff3 | 83 | if(e == TCPSOCKET_ACCEPT ) { |
ysuga | 0:9fac71a0bff3 | 84 | if ( m_pServerSocket->accept(&m_Client, &m_pClientSocket) ) { |
ysuga | 0:9fac71a0bff3 | 85 | return; //Error in accept, discard connection |
ysuga | 0:9fac71a0bff3 | 86 | } |
ysuga | 0:9fac71a0bff3 | 87 | |
ysuga | 0:9fac71a0bff3 | 88 | m_pClientSocket->setOnEvent(EtherTcp_onClientEvent); |
ysuga | 0:9fac71a0bff3 | 89 | } |
ysuga | 0:9fac71a0bff3 | 90 | } |
ysuga | 0:9fac71a0bff3 | 91 | |
ysuga | 0:9fac71a0bff3 | 92 | void EtherTcp_init(uint8_t* mac, uint8_t* ip, |
ysuga | 0:9fac71a0bff3 | 93 | uint8_t* gateway, uint8_t* subnet, |
ysuga | 0:9fac71a0bff3 | 94 | uint16_t port) |
ysuga | 0:9fac71a0bff3 | 95 | |
ysuga | 0:9fac71a0bff3 | 96 | { |
ysuga | 0:9fac71a0bff3 | 97 | pSerial = new Serial(USBTX, USBRX); |
ysuga | 0:9fac71a0bff3 | 98 | m_pInterface = new EthernetNetIf( |
ysuga | 0:9fac71a0bff3 | 99 | IpAddr(ip[0], ip[1], ip[2], ip[3]), |
ysuga | 0:9fac71a0bff3 | 100 | IpAddr(subnet[0], subnet[1], subnet[2], subnet[3]), |
ysuga | 0:9fac71a0bff3 | 101 | IpAddr(gateway[0], gateway[1], gateway[2], gateway[3]), |
ysuga | 0:9fac71a0bff3 | 102 | IpAddr(gateway[0], gateway[1], gateway[2], gateway[3])); |
ysuga | 0:9fac71a0bff3 | 103 | printf("Hello %d %d %d %d\r\n", ip[0], ip[1], ip[2], ip[3]); |
ysuga | 0:9fac71a0bff3 | 104 | EthernetErr ethErr = m_pInterface->setup(); |
ysuga | 0:9fac71a0bff3 | 105 | if (ethErr) { |
ysuga | 0:9fac71a0bff3 | 106 | return; |
ysuga | 0:9fac71a0bff3 | 107 | } |
ysuga | 0:9fac71a0bff3 | 108 | |
ysuga | 0:9fac71a0bff3 | 109 | m_pServerSocket = new TCPSocket(); |
ysuga | 0:9fac71a0bff3 | 110 | m_pServerSocket->setOnEvent(EtherTcp_onServerEvent); |
ysuga | 0:9fac71a0bff3 | 111 | m_pServerSocket->bind(Host(IP_ADDR_ANY, port)); |
ysuga | 0:9fac71a0bff3 | 112 | m_pServerSocket->listen(); |
ysuga | 0:9fac71a0bff3 | 113 | SerialDevice_available = EtherTcp_available; |
ysuga | 0:9fac71a0bff3 | 114 | SerialDevice_getc = EtherTcp_getc; |
ysuga | 0:9fac71a0bff3 | 115 | SerialDevice_putc = EtherTcp_putc; |
ysuga | 0:9fac71a0bff3 | 116 | } |
ysuga | 0:9fac71a0bff3 | 117 | |
ysuga | 0:9fac71a0bff3 | 118 | uint8_t EtherTcp_available() |
ysuga | 0:9fac71a0bff3 | 119 | { |
ysuga | 0:9fac71a0bff3 | 120 | Net::poll(); |
ysuga | 0:9fac71a0bff3 | 121 | return etcp_rx_buffer_get_size(); |
ysuga | 0:9fac71a0bff3 | 122 | } |
ysuga | 0:9fac71a0bff3 | 123 | |
ysuga | 0:9fac71a0bff3 | 124 | |
ysuga | 0:9fac71a0bff3 | 125 | void EtherTcp_putc(const char c) { |
ysuga | 0:9fac71a0bff3 | 126 | if(m_pClientSocket != NULL) { |
ysuga | 0:9fac71a0bff3 | 127 | m_pClientSocket->send(&c, 1); |
ysuga | 0:9fac71a0bff3 | 128 | } |
ysuga | 0:9fac71a0bff3 | 129 | } |
ysuga | 0:9fac71a0bff3 | 130 | |
ysuga | 0:9fac71a0bff3 | 131 | char EtherTcp_getc() |
ysuga | 0:9fac71a0bff3 | 132 | { |
ysuga | 0:9fac71a0bff3 | 133 | char c; |
ysuga | 0:9fac71a0bff3 | 134 | etcp_rx_buffer_pop((unsigned char*)&c); |
ysuga | 0:9fac71a0bff3 | 135 | return c; |
ysuga | 0:9fac71a0bff3 | 136 | } |