Line data Source code
1 : #pragma once 2 : 3 : #include <AP_HAL/AP_HAL.h> 4 : 5 : #if CONFIG_HAL_BOARD == HAL_BOARD_SITL 6 : 7 : #include <stdlib.h> 8 : #include <stdio.h> 9 : #include <arpa/inet.h> 10 : 11 : /* 12 : address of the local interface SITL should send and receive its 13 : multicast traffic on, in network byte order, or zero to leave the 14 : choice to the routing table. 15 : 16 : Multicasting on whichever interface the routing table picks is 17 : normally what you want; it is how an AP_Periph, or a second machine on 18 : the network, gets at the simulation. It does mean the traffic follows 19 : the default route, so it stops working when that route is not up, or 20 : not multicast-capable. Set SITL_MULTICAST_IF_ADDR (e.g. to 127.0.0.1) 21 : to pin the traffic to one interface where a run has to be insulated 22 : from the state of the machine's network - the autotest suite does 23 : this, so that a interface coming or going does not decide whether a 24 : test passes. 25 : */ 26 10 : static inline uint32_t sitl_multicast_interface_address(void) 27 : { 28 10 : const char *addr = getenv("SITL_MULTICAST_IF_ADDR"); 29 10 : if (addr == nullptr || *addr == 0) { 30 : // unset, or set empty to get the default back in an environment 31 : // which sets it (the autotest suite does) 32 0 : return 0; 33 : } 34 10 : const uint32_t ret = inet_addr(addr); 35 10 : if (ret == INADDR_NONE) { 36 0 : ::fprintf(stderr, "Bad SITL_MULTICAST_IF_ADDR (%s); using any interface\n", addr); 37 0 : return 0; 38 : } 39 10 : return ret; 40 : } 41 : 42 : /* 43 : return a port taken from the named environment variable, or the 44 : supplied default if it is unset, empty or invalid 45 : */ 46 10 : static inline uint16_t sitl_port_from_env(const char *name, uint16_t default_port) 47 : { 48 10 : const char *port_str = getenv(name); 49 10 : if (port_str == nullptr || *port_str == 0) { 50 10 : return default_port; 51 : } 52 0 : const long port = strtol(port_str, nullptr, 10); 53 0 : if (port <= 0 || port > 65535) { 54 0 : ::fprintf(stderr, "Bad %s (%s); using default\n", name, port_str); 55 0 : return default_port; 56 : } 57 0 : return (uint16_t)port; 58 : } 59 : 60 : /* 61 : return the port for the simulation-state multicast, taking any 62 : override from the environment. The compiled-in default is a fixed 63 : constant with no per-instance offset - unlike every other SITL port - 64 : so simulations run concurrently on one machine share a state bus and 65 : each peripheral answers a vehicle which is not its own. A test 66 : framework running vehicles in parallel exports a per-instance 67 : SITL_MCAST_STATE_PORT to the vehicle and its peripherals (environment 68 : inheritance reaches both) to give each simulation its own bus. 69 : */ 70 5 : static inline uint16_t sitl_multicast_state_port(uint16_t default_port) 71 : { 72 5 : return sitl_port_from_env("SITL_MCAST_STATE_PORT", default_port); 73 : } 74 : 75 : /* 76 : return the port for the simulated CAN bus multicast, taking any 77 : override from the environment. The multicast group already varies 78 : with the CAN bus number, keeping one simulation's buses apart from 79 : each other, but the port is a fixed constant, so concurrent 80 : simulations on one machine share their CAN buses: every peripheral 81 : hears (and answers) every vehicle. A per-instance 82 : SITL_CAN_MCAST_PORT, exported to the vehicle and its peripherals, 83 : gives each simulation a private set of buses. 84 : */ 85 5 : static inline uint16_t sitl_can_multicast_port(uint16_t default_port) 86 : { 87 5 : return sitl_port_from_env("SITL_CAN_MCAST_PORT", default_port); 88 : } 89 : 90 : #endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL