Skip to content

evedon/mbed-os-example-socket-stats

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket statistics Mbed OS example

This example demonstrates how you can collect statistics from network sockets on an Mbed OS Enabled platform.

Please refer to the mbed-os-example-sockets example for more information about network socket setup.

You can build the project with all supported Mbed OS build tools. However, this example project specifically refers to the command-line interface tool Arm Mbed CLI.

  1. Install Mbed CLI.

  2. Clone this repository on your system, and change the current directory to where the project was cloned:

    $ git clone git@github.com:armmbed/mbed-os-example-socket-stats && cd mbed-os-example-socket-stats

    Alternatively, you can download the example project with Arm Mbed CLI using the import subcommand:

    $ mbed import mbed-os-example-socket-stats && cd mbed-os-example-socket-stats

Application functionality

This application enables socket statistics by setting nsapi.socket-stats-enabled to true in mbed_app.json:

{
    "target_overrides": {
        "*": {
            "nsapi.socket-stats-enabled": true
        }
    }
}

The main() function opens a socket on the Ethernet interface and creates a TCP connection to generate some data traffic. It also starts a thread to periodically print socket statistics, such as ID, State, Proto, Sent, Recv and Time on the serial interface.

Note

The current example is configured to use the Ethernet interface on supported devices. To use the example with a different interface, please follow the socket example README.

Building and running

  1. Connect a USB cable between the USB port on the target and the host computer.

  2. Run this command to build the example project and program the microcontroller flash memory:

    $ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm

(Note: You can use the Mbed CLI command-line option "--sterm" to open a serial terminal after flashing.)

Your PC may take a few minutes to compile your code.

The binary is located at ./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-socket-stats.bin.

Alternatively, you can manually copy the binary to the target, which gets mounted on the host computer through USB.

Depending on the target, you can build the example project with the GCC_ARM, ARM or IAR toolchain. After installing Arm Mbed CLI, run the command below to determine which toolchain supports your target:

$ mbed compile -S

Expected output

The serial terminal shows an output similar to:

--- Terminal on /dev/ttyACM0 - 9600,8,N,1 ---
Mbed OS Socket statistics example
IP address: 10.2.202.163
Netmask: 255.255.255.0
Gateway: 10.2.202.1

Iteration: 1    ID: 0x2000e208    State: Open      Proto: TCP    Sent: 0    Recv: 0    Time: 7610
Iteration: 1    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 2    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 2    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 3    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 3    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 4    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 4    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 5    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 5    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 6    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 6    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 7    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 7    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 8    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 8    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 9    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 9    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 10    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 10    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 11    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 11    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 12    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 12    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 13    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 13    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 14    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 14    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615
Iteration: 15    ID: 0x2000e208    State: Connected Proto: TCP    Sent: 58    Recv: 0    Time: 7815
Iteration: 15    ID: 0x2000e050    State: Closed    Proto: UDP    Sent: 31    Recv: 257    Time: 7615

The information below shows how to interpret the above fields:

Iteration:      Number of times socket state changes
ID:             Socket id
State:          Socket states
Protocol:       TCP, UDP
Sent bytes:     Number of bytes sent through this socket  
Received bytes: Number of bytes received through this socket
Time in us:     When state last changed

Troubleshooting

If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.

Related links

License and contributions

The software is provided under the Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more information.

This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.

About

Example for Network socket statistics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 100.0%