Network Handbook in ARM mbed

Handbook in ARM mbed includes settup tutorials for mbed platform such as Ethernet as well as mbed library.

Networking Handbook

For networking based on Ethernet network, Ethenret Interface library is provided and is composed TCP/IP Protocol layer, Ethernet, EthernetInterface and Socket. In other words, the EthernetInterface library includes the networking stack necessary for connect betwwen mbed platform and Internet.

Each layer in EthernetInterface provides APIs to connect to the internet.

  • mbed – Ethernet Class Reference
    Type Func. Descriptions
    Ethernet () Initialise the ethernet interface.
    virtual ~Ethernet () Powers the hardware down.
    int write (const char *data, int size) Writes into an outgoing ethernet packet.
    int send () Send an outgoing ethernet packet.
    int receive () Recevies an arrived ethernet packet.
    int read (const char *data, int size) Read from an recevied ethernet packet.
    void address (char *mac) RGives the ethernet address of the mbed.
    int link() Returns if an ethernet link is pressent or not.
    void set_link(Mode mode) Sets the speed and duplex parameters of an ethernet link.
  • mbed – EthernetInterface Class Reference

    Type Func. Descriptions
    static int init () Initialize the interface with DHCP.
    static int init (const char *ip, const char *mask, const char *gateway) Initialize the interface with a static IP address.
    static int connect (unsigned int timeout_ms=15000) Connect Bring the interface up, start DHCP if needed.
    static int disconnect () Disconnect Bring the interface down.
    static char* getMACAddress () Get the MAC address of your Ethernet interface.
    static char* getIPAddress () Get the IP address of your Ethernet interface.
    static char* getGateway () Get the Gateway address of your Ethernet interface.
    static char* getNetworkMask () Get the Network mask of your Ethernet interface.
  • Socket – TCPSocketServer Class Reference

    Type Func. Descriptions
    TCPSocketServer () Instantiate a TCP Server.
    int bind (int port) Bind a socket to a specific port.
    int listen (int backlog=1) Start listening for incoming connections.
    int accept ( TCPSocketConnection &connection) Accept a new connection.
    void set_blocking (bool blocking, unsigned int timeout=1500) Set blocking or non-blocking mode of the socket and a timeout on blocking socket operations.
    int set_option (int level, int optname, const void *optval, socklen_t optlen) Set socket options.
    int get_option (int level, int optname, void *optval, socklen_t *optlen) Get socket options.
    int close (bool shutdown=true) Get socket options.
  • Socket – TCPSocketConnection Class Reference

    Type Func. Descriptions
    TCPSocketConnection () TCP socket connection.
    int connect (const char *host, const int port) Connects this TCP socket to the server.
    bool is_connected (void) Check if the socket is connected.
    int send (char *data, int length) Send data to the remote host.
    int send_all (char *data, int length) Send all the data to the remote host.
    int receive (char *data, int length) Receive data from the remote host.
    int receive_all (char *data, int length) Receive all the data from the remote host.
    void set_blocking (bool blocking, unsigned int timeout=1500) Set blocking or non-blocking mode of the socket and a timeout on blocking socket operations.
    int set_option (int level, int optname, const void *optval, socklen_t optlen) Set socket options.
    int get_option (int level, int optname, void *optval, socklen_t *optlen) Get socket options.
    int close (bool shutdown=true) Close the socket.
    void reset_address (void) Reset the address of this endpoint.
    int set_address (const char *host, const int port) Set the address of this endpoint.
    char* get_address (void) Get the IP address of this endpoint.
    int get_port (void) Get the port of this endpoint.

TCP Server Example

The TCP Server example shows how to get started with the Socket API & Ethernet.

  • EthernetInterface includes lwip as software TCP/IP stck
  • EthernetInterface is expected to build the “mbed-rtos”
    TCPserver

  • lwip is a small TCP/IP protocol

  • Protocols: IP, ICMP, UDP, TCP, IGMP, ARP, PPPoS, PPPoE
  • DHCP client, DNS client, AutoIP/APIPA (Zeroconf), SNMP agent (private MIB support)
  • APIs: specialized APIs for enhanced performance, optional Berkeley-alike socket API
    lwIP
include "mbed.h"
#include "EthernetInterface.h"

EthernetInterface eth;

int main()
{
printf("Trying rn");

// as your env. change to real IP address and so on.
int ret = eth.init("192.168.77.34", "255.255.255.0", "192.168.77.1");

if (!ret) {
printf("Initialized, MAC: %snr", eth.getMACAddress());
printf("Connected, IP: %s, MASK: %s, GW: %snr",
eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
} else {
printf("Error eth.init() - ret = %dnr", ret);
return -1;
}

eth.connect();
printf("IP Address is %sn", eth.getIPAddress());

TCPSocketServer server;
server.bind(5000);
server.listen();

while (true) {
printf("nWait for new connection...n");
TCPSocketConnection client;
server.accept(client);

client.set_blocking(false, 1500); // Timeout after (1.5)s

printf("Connection from: %sn", client.get_address());

char buffer[2048];
while (true) {
int n = client.receive(buffer, sizeof(buffer));
////int n = client.receive(buffer, 0);
//printf("rcv data len is : %drn", n);
if (n < 0) break; // !_is_connected
/*
if (n <= 0) break; // !_is_connected or !wait_readable
client.send_all(buffer, n);
if (n <= 0) break;
*/
}
client.close();
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s