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