Skip to content

Commit f560cf6

Browse files
committed
Simplify application configuration
Moved initialization of mac addresses and lldp port name to stack.
1 parent ce42bb0 commit f560cf6

20 files changed

+179
-221
lines changed

include/pnet_api.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,24 +1170,12 @@ typedef struct pnet_ethaddr
11701170
#define PNET_LLDP_PORT_ID_MAX_SIZE \
11711171
(PNET_STATION_NAME_MAX_SIZE + PNET_PORT_NAME_MAX_SIZE)
11721172

1173-
/**
1174-
* Network interface
1175-
*/
1176-
typedef struct pnet_netif
1177-
{
1178-
char if_name[PNET_INTERFACE_NAME_MAX_SIZE]; /**< Terminated string */
1179-
pnet_ethaddr_t eth_addr; /**< Interface MAC address */
1180-
} pnet_netif_t;
1181-
11821173
/**
11831174
* Physical Port Configuration
11841175
*/
11851176
typedef struct pnet_port_cfg
11861177
{
1187-
pnet_netif_t phy_port;
1188-
char port_name[PNET_PORT_NAME_MAX_SIZE]; /**< Terminated string */
1189-
uint16_t rtclass_2_status;
1190-
uint16_t rtclass_3_status;
1178+
const char * netif_name;
11911179
} pnet_port_cfg_t;
11921180

11931181
/**
@@ -1213,10 +1201,10 @@ typedef struct pnet_ip_cfg
12131201
*/
12141202
typedef struct pnet_if_cfg
12151203
{
1216-
pnet_netif_t main_port; /**< Main (DAP) network interface. */
1204+
const char * main_netif_name; /**< Main (DAP) network interface. */
12171205
pnet_ip_cfg_t ip_cfg; /**< IP Settings for main network interface */
12181206

1219-
pnet_port_cfg_t ports[PNET_MAX_PORT]; /**< Physical ports (DAP ports) */
1207+
pnet_port_cfg_t physical_ports[PNET_MAX_PORT];
12201208
} pnet_if_cfg_t;
12211209

12221210
/**

sample_app/sampleapp_common.c

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void ip_to_string (pnal_ipaddr_t ip, char * outputstring)
6060
(uint8_t) (ip & 0xFF));
6161
}
6262

63-
void app_mac_to_string (pnal_ethaddr_t mac, char * outputstring)
63+
void app_mac_to_string (pnet_ethaddr_t mac, char * outputstring)
6464
{
6565
snprintf (
6666
outputstring,
@@ -1317,7 +1317,6 @@ void app_plug_dap (pnet_t * net, void * arg)
13171317

13181318
int app_pnet_cfg_init_default (pnet_cfg_t * stack_config)
13191319
{
1320-
uint16_t i;
13211320
memset (stack_config, 0, sizeof (pnet_cfg_t));
13221321
stack_config->tick_us = APP_TICK_INTERVAL_US;
13231322

@@ -1391,18 +1390,6 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config)
13911390
/* Timing */
13921391
stack_config->min_device_interval = 32; /* Corresponds to 1 ms */
13931392

1394-
/* LLDP settings */
1395-
for (i = 0; i < PNET_MAX_PORT; i++)
1396-
{
1397-
snprintf (
1398-
stack_config->if_cfg.ports[i].port_name,
1399-
sizeof (stack_config->if_cfg.ports[i].port_name),
1400-
"port-%03d",
1401-
i + 1);
1402-
stack_config->if_cfg.ports[i].rtclass_2_status = 0;
1403-
stack_config->if_cfg.ports[i].rtclass_3_status = 0;
1404-
}
1405-
14061393
/* Network configuration */
14071394
stack_config->send_hello = true;
14081395
stack_config->if_cfg.ip_cfg.dhcp_enable = false;
@@ -1415,50 +1402,6 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config)
14151402
return 0;
14161403
}
14171404

1418-
/**
1419-
* Initialize a network interface configuration from a network
1420-
* interface name.
1421-
* This includes reading the Ethernet MAC address.
1422-
* If the network interface can not be found or the operation
1423-
* fails to read the mac address from the network interface,
1424-
* error is returned.
1425-
* @param netif_name In: Network interface name
1426-
* @param p_netif Out: Network interface configuration
1427-
* @param verbosity In: Verbosity
1428-
* @return 0 on success
1429-
* -1 on error
1430-
*/
1431-
int app_port_cfg_init (
1432-
const char * netif_name,
1433-
pnet_netif_t * p_netif,
1434-
int verbosity)
1435-
{
1436-
pnal_ethaddr_t mac;
1437-
char mac_string[PNAL_ETH_ADDRSTR_SIZE]; /* Terminated string */
1438-
1439-
int ret = pnal_get_macaddress (netif_name, &mac);
1440-
if (ret != 0)
1441-
{
1442-
printf (
1443-
"Error: The given network interface does not exist: \"%s\"\n",
1444-
netif_name);
1445-
return -1;
1446-
}
1447-
else
1448-
{
1449-
if (verbosity > 0)
1450-
{
1451-
app_mac_to_string (mac, mac_string);
1452-
printf ("%-10s %s\n", netif_name, mac_string);
1453-
}
1454-
}
1455-
1456-
strncpy (p_netif->if_name, netif_name, PNET_INTERFACE_NAME_MAX_SIZE);
1457-
memcpy (p_netif->eth_addr.addr, mac.addr, sizeof (pnal_ethaddr_t));
1458-
1459-
return 0;
1460-
}
1461-
14621405
int app_get_netif_namelist (
14631406
const char * arg_str,
14641407
app_netif_namelist_t * p_if_list,
@@ -1528,6 +1471,7 @@ int app_get_netif_namelist (
15281471

15291472
int app_pnet_cfg_init_netifs (
15301473
const char * netif_list_str,
1474+
app_netif_namelist_t * if_list,
15311475
pnet_cfg_t * p_cfg,
15321476
int verbosity)
15331477
{
@@ -1536,48 +1480,35 @@ int app_pnet_cfg_init_netifs (
15361480
pnal_ipaddr_t ip;
15371481
pnal_ipaddr_t netmask;
15381482
pnal_ipaddr_t gateway;
1539-
app_netif_namelist_t if_list;
15401483

1541-
ret = app_get_netif_namelist (netif_list_str, &if_list, PNET_MAX_PORT);
1484+
ret = app_get_netif_namelist (netif_list_str, if_list, PNET_MAX_PORT);
15421485
if (ret != 0)
15431486
{
15441487
return ret;
15451488
}
1489+
p_cfg->if_cfg.main_netif_name = if_list->netif[0].name;
15461490

15471491
if (verbosity > 0)
15481492
{
1549-
printf ("Management port: ");
1550-
}
1551-
if (
1552-
app_port_cfg_init (
1553-
if_list.netif[0].name,
1554-
&p_cfg->if_cfg.main_port,
1555-
verbosity) != 0)
1556-
{
1557-
return -1;
1493+
printf ("Management port: %s\n", p_cfg->if_cfg.main_netif_name);
15581494
}
15591495

15601496
for (i = 1; i <= PNET_MAX_PORT; i++)
15611497
{
1498+
p_cfg->if_cfg.physical_ports[i - 1].netif_name = if_list->netif[i].name;
15621499
if (verbosity > 0)
15631500
{
1564-
printf ("Physical port [%u]: ", i);
1565-
}
1566-
1567-
if (
1568-
app_port_cfg_init (
1569-
if_list.netif[i].name,
1570-
&p_cfg->if_cfg.ports[i - 1].phy_port,
1571-
verbosity) != 0)
1572-
{
1573-
return -1;
1501+
printf (
1502+
"Physical port [%u]: %s\n",
1503+
i,
1504+
p_cfg->if_cfg.physical_ports[i - 1].netif_name);
15741505
}
15751506
}
15761507

15771508
/* Read IP, netmask, gateway from operating system */
1578-
ip = pnal_get_ip_address (if_list.netif[0].name);
1579-
netmask = pnal_get_netmask (if_list.netif[0].name);
1580-
gateway = pnal_get_gateway (if_list.netif[0].name);
1509+
ip = pnal_get_ip_address (p_cfg->if_cfg.main_netif_name);
1510+
netmask = pnal_get_netmask (p_cfg->if_cfg.main_netif_name);
1511+
gateway = pnal_get_gateway (p_cfg->if_cfg.main_netif_name);
15811512

15821513
if (verbosity > 0)
15831514
{

sample_app/sampleapp_common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ typedef struct app_data_obj
242242
bool alarm_allowed;
243243
pnet_alarm_argument_t alarm_arg;
244244
struct cmd_args arguments;
245+
app_netif_namelist_t if_list;
245246
uint32_t app_param_1;
246247
uint32_t app_param_2;
247248
uint8_t inputdata[APP_DATASIZE_INPUT];
@@ -276,7 +277,7 @@ typedef enum app_demo_state
276277
* @param outputstring Out: Resulting string buffer. Should have size
277278
* PNAL_ETH_ADDRSTR_SIZE.
278279
*/
279-
void app_mac_to_string (pnal_ethaddr_t mac, char * outputstring);
280+
void app_mac_to_string (pnet_ethaddr_t mac, char * outputstring);
280281

281282
/**
282283
* Print out current IP address, netmask and default gateway.
@@ -306,13 +307,15 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config);
306307
*
307308
* @param netif_list_str In: Comma separated list of network interfaces.
308309
* Terminated string.
310+
* @param if_list Out: Network interface string storage.
309311
* @param p_cfg InOut: p-net configuration
310312
* @param verbosity In: Verbosity
311313
* @return 0 on success
312314
* -1 on error
313315
*/
314316
int app_pnet_cfg_init_netifs (
315317
const char * netif_list_str,
318+
app_netif_namelist_t * if_list,
316319
pnet_cfg_t * p_cfg,
317320
int verbosity);
318321

src/common/pf_dcp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static void pf_dcp_responder (pnet_t * net, void * arg, uint32_t current_time)
187187
{
188188
if (net->dcp_delayed_response_waiting == true)
189189
{
190-
if (pf_eth_send (net, net->eth_handle, p_buf) > 0)
190+
if (pf_eth_send (net, net->pf_interface.main_port.handle, p_buf) > 0)
191191
{
192192
LOG_DEBUG (
193193
PNET_LOG,
@@ -1091,7 +1091,7 @@ static int pf_dcp_get_set (
10911091
p_dst_dcphdr->data_length = htons (dst_pos - dst_start);
10921092
p_rsp->len = dst_pos;
10931093

1094-
if (pf_eth_send (net, net->eth_handle, p_rsp) > 0)
1094+
if (pf_eth_send (net, net->pf_interface.main_port.handle, p_rsp) > 0)
10951095
{
10961096
LOG_DEBUG (
10971097
PF_DCP_LOG,
@@ -1366,7 +1366,7 @@ int pf_dcp_hello_req (pnet_t * net)
13661366
p_dcphdr->data_length = htons (dst_pos - dst_start_pos);
13671367
p_buf->len = dst_pos;
13681368

1369-
(void)pf_eth_send (net, net->eth_handle, p_buf);
1369+
(void)pf_eth_send (net, net->pf_interface.main_port.handle, p_buf);
13701370
}
13711371
pnal_buf_free (p_buf);
13721372
}

src/common/pf_eth.c

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,65 @@
2727
#ifdef UNIT_TEST
2828
#define pnal_eth_init mock_pnal_eth_init
2929
#define pnal_eth_send mock_pnal_eth_send
30+
#define pnal_get_macaddress mock_pnal_get_macaddress
3031
#endif
3132

3233
#include <string.h>
3334
#include "pf_includes.h"
3435

36+
/**
37+
* Init network interface.
38+
* Set name, read mac address, register receive callback.
39+
*
40+
* @param net InOut: The p-net stack instance
41+
* @param netif_name In: Network interface name
42+
* @param eth_receive_type In: Ethernet frame types that shall be received
43+
* by the network interface / port.
44+
* @param netif Out: Initialized network interface.
45+
* @return
46+
*/
47+
static int pf_eth_init_netif (
48+
pnet_t * net,
49+
const char * netif_name,
50+
pnal_ethertype_t eth_receive_type,
51+
pf_netif_t * netif)
52+
{
53+
pnal_ethaddr_t pnal_mac_addr;
54+
55+
snprintf (netif->name, sizeof (netif->name), "%s", netif_name);
56+
57+
netif->handle =
58+
pnal_eth_init (netif->name, eth_receive_type, pf_eth_recv, (void *)net);
59+
60+
if (netif->handle == NULL)
61+
{
62+
LOG_ERROR (PNET_LOG, "Failed to init \"%s\"\n", netif_name);
63+
return -1;
64+
}
65+
66+
if (pnal_get_macaddress (netif_name, &pnal_mac_addr) != 0)
67+
{
68+
LOG_ERROR (PNET_LOG, "Failed read mac address on \"%s\"\n", netif_name);
69+
return -1;
70+
}
71+
72+
memcpy (
73+
netif->mac_address.addr,
74+
pnal_mac_addr.addr,
75+
sizeof (netif->mac_address.addr));
76+
77+
return 0;
78+
}
79+
3580
int pf_eth_init (pnet_t * net, const pnet_cfg_t * p_cfg)
3681
{
3782
int port;
3883
pf_port_iterator_t port_iterator;
3984
pf_port_t * p_port_data;
40-
const pnet_port_cfg_t * p_port_cfg;
4185
pnal_ethertype_t main_port_receive_type;
86+
#if PNET_MAX_PORT > 1
87+
const pnet_port_cfg_t * p_port_cfg;
88+
#endif
4289

4390
memset (net->eth_id_map, 0, sizeof (net->eth_id_map));
4491

@@ -49,17 +96,13 @@ int pf_eth_init (pnet_t * net, const pnet_cfg_t * p_cfg)
4996
#endif
5097

5198
/* Init management port */
52-
net->eth_handle = pnal_eth_init (
53-
p_cfg->if_cfg.main_port.if_name,
54-
main_port_receive_type,
55-
pf_eth_recv,
56-
(void *)net);
57-
if (net->eth_handle == NULL)
99+
if (
100+
pf_eth_init_netif (
101+
net,
102+
p_cfg->if_cfg.main_netif_name,
103+
main_port_receive_type,
104+
&net->pf_interface.main_port) != 0)
58105
{
59-
LOG_ERROR (
60-
PNET_LOG,
61-
"Failed to init \"%s\"\n",
62-
p_cfg->if_cfg.main_port.if_name);
63106
return -1;
64107
}
65108

@@ -69,27 +112,24 @@ int pf_eth_init (pnet_t * net, const pnet_cfg_t * p_cfg)
69112
while (port != 0)
70113
{
71114
p_port_data = pf_port_get_state (net, port);
72-
p_port_cfg = pf_port_get_config (net, port);
73115

74116
#if PNET_MAX_PORT > 1
75-
p_port_data->eth_handle = pnal_eth_init (
76-
p_port_cfg->phy_port.if_name,
77-
PNAL_ETHTYPE_LLDP,
78-
pf_eth_recv,
79-
(void *)net);
80-
#else
81-
/* In single port configuration managed port is also physical port 1 */
82-
p_port_data->eth_handle = net->eth_handle;
83-
#endif
117+
p_port_cfg = pf_port_get_config (net, port);
84118

85-
if (p_port_data->eth_handle == NULL)
119+
if (
120+
pf_eth_init_netif (
121+
net,
122+
p_port_cfg->netif_name,
123+
PNAL_ETHTYPE_LLDP,
124+
&p_port_data->netif) != 0)
86125
{
87-
LOG_ERROR (
88-
PNET_LOG,
89-
"Failed to init \"%s\"\n",
90-
p_port_cfg->phy_port.if_name);
91126
return -1;
92127
}
128+
#else
129+
/* In single port configuration the managed port is also physical port 1
130+
*/
131+
p_port_data->netif = net->pf_interface.main_port;
132+
#endif
93133

94134
port = pf_port_get_next (&port_iterator);
95135
}

0 commit comments

Comments
 (0)