|
| 1 | +.. _latmon: |
| 2 | + |
| 3 | +Latmon Network Service |
| 4 | +###################### |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 2 |
| 9 | + |
| 10 | +Overview |
| 11 | +******** |
| 12 | + |
| 13 | +Provides the functionality required for network-based latency monitoring, including socket |
| 14 | +management, client-server communication, and data exchange with the Latmus service running |
| 15 | +on the System Under Test (SUT). |
| 16 | + |
| 17 | +The Latmon network service is responsible for establishing and managing network |
| 18 | +communication between the Latmon application (running on a Zephyr-based board) and |
| 19 | +the Latmus service (running on the SUT). |
| 20 | + |
| 21 | +It uses TCP sockets for reliable communication and UDP sockets for broadcasting |
| 22 | +the IP address of the Latmon device. |
| 23 | + |
| 24 | +API Reference |
| 25 | +************* |
| 26 | + |
| 27 | +.. doxygengroup:: latmon |
| 28 | + |
| 29 | +Features |
| 30 | +******** |
| 31 | + |
| 32 | +- **Socket Management**: Creates and manages TCP and UDP sockets for communication. |
| 33 | +- **Client-Server Communication**: Handles incoming connections from the Latmus service. |
| 34 | +- **Data Exchange**: Sends latency metrics and histogram data to the Latmus service. |
| 35 | +- **IP Address Broadcasting**: Broadcasts the IP address of the Latmon device to facilitate |
| 36 | + discovery by the Latmus service. |
| 37 | +- **Thread-Safe Design**: Uses Zephyr's kernel primitives (e.g., message queues and semaphores) for |
| 38 | + synchronization. |
| 39 | + |
| 40 | +Key Components |
| 41 | +************** |
| 42 | + |
| 43 | +Socket Management |
| 44 | +================= |
| 45 | + |
| 46 | +The service provides functions to create, bind, and listen on sockets. |
| 47 | +It also supports sending and receiving data over TCP and UDP. |
| 48 | + |
| 49 | +Message Queue |
| 50 | +============= |
| 51 | + |
| 52 | +A message queue (``latmon_msgq``) is used to pass messages between threads, |
| 53 | +ensuring thread-safe communication. |
| 54 | + |
| 55 | +Synchronization |
| 56 | +=============== |
| 57 | + |
| 58 | +Semaphores (``latmon_done`` and ``monitor_done``) are used to synchronize the monitoring |
| 59 | +process and ensure proper cleanup. |
| 60 | + |
| 61 | +Thread Management |
| 62 | +================= |
| 63 | + |
| 64 | +The service uses Zephyr threads to handle incoming connections and manage the |
| 65 | +monitoring process. |
| 66 | + |
| 67 | +Workflow |
| 68 | +******** |
| 69 | + |
| 70 | +Socket Creation |
| 71 | +=============== |
| 72 | + |
| 73 | +The ``net_latmon_get_socket()`` function is called to create and configure a TCP socket to |
| 74 | +communicate with the Latmus service. A connection address can be specified as a paramenter to |
| 75 | +bind the socket to a specific interface and port. |
| 76 | + |
| 77 | +Connection Handling |
| 78 | +=================== |
| 79 | + |
| 80 | +The ``net_latmon_connect()`` function waits for a connection from the Latmus service. |
| 81 | +If no connection is received within the timeout period, the service broadcasts its IP address |
| 82 | +using UDP and returns ``-EAGAIN``. |
| 83 | +If the broadcast request cannot be sent, the function returns ``-1``, and the client should quit. |
| 84 | + |
| 85 | +Monitoring Start |
| 86 | +================ |
| 87 | + |
| 88 | +Once a connection is established, the ``net_latmon_start()`` function is called to |
| 89 | +start the monitoring process. This function uses a callback to calculate latency deltas |
| 90 | +and sends the data to the Latmus service. |
| 91 | + |
| 92 | +Monitoring Status |
| 93 | +================= |
| 94 | + |
| 95 | +The ``net_latmon_running()`` function can be used to check if the monitoring process is active. |
| 96 | + |
| 97 | +Thread Management |
| 98 | +================= |
| 99 | + |
| 100 | +The service uses Zephyr threads to handle incoming connections and manage the monitoring |
| 101 | +process. |
| 102 | + |
| 103 | +Enabling the Latmon Service |
| 104 | +*************************** |
| 105 | + |
| 106 | +The following configuration option must be enabled in the :file:`prj.conf` file. |
| 107 | + |
| 108 | +- :kconfig:option:`CONFIG_NET_LATMON` |
| 109 | + |
| 110 | +The following options may be configured to customize the Latmon service: |
| 111 | + |
| 112 | +- :kconfig:option:`CONFIG_NET_LATMON_PORT` - Port number for the Latmon service. |
| 113 | +- :kconfig:option:`CONFIG_NET_LATMON_XFER_THREAD_STACK_SIZE` |
| 114 | +- :kconfig:option:`CONFIG_NET_LATMON_XFER_THREAD_PRIORITY` |
| 115 | +- :kconfig:option:`CONFIG_NET_LATMON_THREAD_STACK_SIZE` |
| 116 | +- :kconfig:option:`CONFIG_NET_LATMON_THREAD_PRIORITY` |
| 117 | +- :kconfig:option:`CONFIG_NET_LATMON_MONITOR_THREAD_STACK_SIZE` |
| 118 | +- :kconfig:option:`CONFIG_NET_LATMON_MONITOR_THREAD_PRIORITY` |
| 119 | + |
| 120 | +Example Usage |
| 121 | +************* |
| 122 | + |
| 123 | +.. code-block:: c |
| 124 | +
|
| 125 | + #include <zephyr/net/latmon.h> |
| 126 | + #include <zephyr/net/socket.h> |
| 127 | +
|
| 128 | + void main(void) |
| 129 | + { |
| 130 | + struct in_addr ip; |
| 131 | + int server_socket, client_socket; |
| 132 | +
|
| 133 | + /* Create and configure the server socket */ |
| 134 | + server_socket = net_latmon_get_socket(); |
| 135 | +
|
| 136 | + while (1) { |
| 137 | + /* Wait for a connection from the Latmus service */ |
| 138 | + client_socket = net_latmon_connect(server_socket, &ip); |
| 139 | + if (client_socket < 0) { |
| 140 | + if (client_socket == -EAGAIN) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + goto out; |
| 144 | + } |
| 145 | +
|
| 146 | + /* Start the latency monitoring process */ |
| 147 | + net_latmon_start(client_socket, measure_latency_cycles); |
| 148 | + } |
| 149 | + out: |
| 150 | + close(socket); |
| 151 | + } |
0 commit comments