lldp_debug.c
Go to the documentation of this file.
1 /**
2  * @file lldp_debug.c
3  * @brief Data logging functions for debugging purpose (LLDP)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL LLDP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "lldp/lldp.h"
37 #include "lldp/lldp_ext_dot1.h"
38 #include "lldp/lldp_ext_dot3.h"
39 #include "lldp/lldp_ext_med.h"
40 #include "lldp/lldp_ext_pno.h"
41 #include "lldp/lldp_debug.h"
42 #include "debug.h"
43 
44 //Check TCP/IP stack configuration
45 #if (LLDP_SUPPORT == ENABLED)
46 
47 //TLV type values
49 {
50  {LLDP_TLV_TYPE_END_OF_LLDPDU, "End Of LLDPDU"},
51  {LLDP_TLV_TYPE_CHASSIS_ID, "Chassis ID"},
52  {LLDP_TLV_TYPE_PORT_ID, "Port ID"},
53  {LLDP_TLV_TYPE_TIME_TO_LIVE, "Time To Live"},
54  {LLDP_TLV_TYPE_PORT_DESC, "Port Description"},
55  {LLDP_TLV_TYPE_SYS_NAME, "System Name"},
56  {LLDP_TLV_TYPE_SYS_DESC, "System Description"},
57  {LLDP_TLV_TYPE_SYS_CAP, "System Capabilities"},
58  {LLDP_TLV_TYPE_MGMT_ADDR, "Management Address"},
59  {LLDP_TLV_TYPE_ORG_DEFINED, "Organizationally Specific"}
60 };
61 
62 //Organizationally unique identifiers
64 {
65  {LLDP_DOT1_OUI, "IEEE 802.1"},
66  {LLDP_DOT3_OUI, "IEEE 802.3"},
67  {LLDP_MED_OUI, "LLDP-MED"},
68  {LLDP_PNO_OUI, "PROFINET"}
69 };
70 
71 //IEEE 802.1 subtype values
73 {
74  {LLDP_DOT1_SUBTYPE_RESERVED, "Reserved"},
75  {LLDP_DOT1_SUBTYPE_PORT_VLAN_ID, "Port VLAN ID"},
76  {LLDP_DOT1_SUBTYPE_PORT_PROTO_VLAN_ID, "Port And Protocol VLAN ID"},
77  {LLDP_DOT1_SUBTYPE_VLAN_NAME, "VLAN Name"},
78  {LLDP_DOT1_SUBTYPE_PROTOCOL_ID, "Protocol Identity"}
79 };
80 
81 //IEEE 802.3 subtype values
83 {
84  {LLDP_DOT3_SUBTYPE_RESERVED, "Reserved"},
85  {LLDP_DOT3_SUBTYPE_MAC_PHY_CONFIG_STATUS, "MAC/PHY Configuration/Status"},
86  {LLDP_DOT3_SUBTYPE_POWER_VIA_MDI, "Power Via MDI"},
87  {LLDP_DOT3_SUBTYPE_LINK_AGGREGATION, "Link Aggregation"},
88  {LLDP_DOT3_SUBTYPE_MAX_FRAME_SIZE, "Maximum Frame Size"}
89 };
90 
91 //LLDP-MED subtype values
93 {
94  {LLDP_MED_SUBTYPE_RESERVED, "Reserved"},
95  {LLDP_MED_SUBTYPE_LLDP_MED_CAP, "LLDP-MED Capabilities"},
96  {LLDP_MED_SUBTYPE_NETWORK_POLICY, "Network Policy"},
97  {LLDP_MED_SUBTYPE_LOCATION_ID, "Location Identification"},
98  {LLDP_MED_SUBTYPE_EXT_POWER_VIA_MDI, "Extended Power-via-MDI"},
99  {LLDP_MED_SUBTYPE_HARDWARE_REVISION, "Inventory - Hardware Revision"},
100  {LLDP_MED_SUBTYPE_FIRMWARE_REVISION, "Inventory - Firmware Revision"},
101  {LLDP_MED_SUBTYPE_SOFTWARE_REVISION, "Inventory - Software Revision"},
102  {LLDP_MED_SUBTYPE_SERIAL_NUMBER, "Inventory - Serial Number"},
103  {LLDP_MED_SUBTYPE_MANUFACTURER_NAME, "Inventory - Manufacturer Name"},
104  {LLDP_MED_SUBTYPE_MODEL_NAME, "Inventory - Model Name"},
105  {LLDP_MED_SUBTYPE_ASSET_ID, "Inventory - Asset ID"}
106 };
107 
108 //PROFINET subtype values
110 {
111  {LLDP_PNO_SUBTYPE_RESERVED, "Reserved"},
112  {LLDP_PNO_SUBTYPE_MEASURED_DELAY_VALUES, "Measured Delay Values"},
113  {LLDP_PNO_SUBTYPE_PORT_STATUS, "Port Status"},
114  {LLDP_PNO_SUBTYPE_ALIAS, "Alias"},
115  {LLDP_PNO_SUBTYPE_MRP_PORT_STATUS, "MRP Port Status"},
116  {LLDP_PNO_SUBTYPE_INTERFACE_MAC_ADDR, "Interface MAC address"},
117  {LLDP_PNO_SUBTYPE_PTCP_STATUS, "PTCP Status"},
118 };
119 
120 
121 /**
122  * @brief Dump LLDP data unit
123  * @param[in] lldpdu Pointer to the LLDPDU
124  **/
125 
127 {
128 #if (TRACE_LEVEL >= TRACE_LEVEL_VERBOSE)
129  error_t error;
130  LldpTlv tlv;
131 
132  //Extract the first TLV
133  error = lldpGetFirstTlv(lldpdu, &tlv);
134 
135  //Parse the LLDP data unit
136  while(!error)
137  {
138  //Dump TLV structure
139  lldpDumpTlv(&tlv);
140 
141  //Extract the next TLV
142  error = lldpGetNextTlv(lldpdu, &tlv);
143  }
144 #endif
145 }
146 
147 
148 /**
149  * @brief Dump TLV structure
150  * @param[in] tlv Pointer to the TLV
151  **/
152 
153 void lldpDumpTlv(const LldpTlv *tlv)
154 {
155 #if (TRACE_LEVEL >= TRACE_LEVEL_VERBOSE)
156  uint32_t oui;
157  const char_t *name;
158  const LldpOrgDefTlv *orgDefTlv;
159 
160  //Convert the TLV type to string representation
163 
164  //The TLV type field occupies the seven most significant bits of the
165  //first octet of the TLV format
166  TRACE_VERBOSE(" TLV Type = %" PRIu8 " (%s)\r\n", tlv->type, name);
167 
168  //Check TLV type
169  if(tlv->type == LLDP_TLV_TYPE_ORG_DEFINED)
170  {
171  //Check TLV length
172  if(tlv->length >= sizeof(LldpOrgDefTlv))
173  {
174  //Point to the organizationally specific TLV
175  orgDefTlv = (LldpOrgDefTlv *) tlv->value;
176 
177  //Get the organizationally unique identifier
178  oui = LOAD24BE(orgDefTlv->oui);
179 
180  //Convert the OUI to string representation
182 
183  //Dump organizationally unique identifier
184  TRACE_VERBOSE(" OUI = %02" PRIX8 "-%02" PRIX8 "-%02" PRIX8 " (%s)\r\n",
185  orgDefTlv->oui[0], orgDefTlv->oui[1],
186  orgDefTlv->oui[2], name);
187 
188  //Convert the subtype to string representation
189  if(oui == LLDP_DOT1_OUI)
190  {
191  name = lldpGetParamName(orgDefTlv->subtype,
193  }
194  else if(oui == LLDP_DOT3_OUI)
195  {
196  name = lldpGetParamName(orgDefTlv->subtype,
198  }
199  else if(oui == LLDP_MED_OUI)
200  {
201  name = lldpGetParamName(orgDefTlv->subtype,
203  }
204  else if(oui == LLDP_PNO_OUI)
205  {
206  name = lldpGetParamName(orgDefTlv->subtype,
208  }
209  else
210  {
211  name = "Unknown";
212  }
213 
214  //Convert the subtype to string representation
215  TRACE_VERBOSE(" Subtype = %" PRIu8 " (%s)\r\n",
216  orgDefTlv->subtype, name);
217 
218  //Dump TLV value
219  TRACE_VERBOSE_ARRAY(" ", orgDefTlv->value,
220  tlv->length - sizeof(LldpOrgDefTlv));
221  }
222  else
223  {
224  //Dump TLV value
225  TRACE_VERBOSE_ARRAY(" ", tlv->value, tlv->length);
226  }
227  }
228  else
229  {
230  //Dump TLV value
231  TRACE_VERBOSE_ARRAY(" ", tlv->value, tlv->length);
232  }
233 #endif
234 }
235 
236 
237 /**
238  * @brief Convert a parameter to string representation
239  * @param[in] value Parameter value
240  * @param[in] paramList List of acceptable parameters
241  * @param[in] paramListLen Number of entries in the list
242  * @return NULL-terminated string describing the parameter
243  **/
244 
246  size_t paramListLen)
247 {
248  uint_t i;
249 
250  //Default name for unknown values
251  static const char_t defaultName[] = "Unknown";
252 
253  //Loop through the list of acceptable parameters
254  for(i = 0; i < paramListLen; i++)
255  {
256  if(paramList[i].value == value)
257  return paramList[i].name;
258  }
259 
260  //Unknown value
261  return defaultName;
262 }
263 
264 #endif
error_t lldpGetFirstTlv(LldpDataUnit *lldpdu, LldpTlv *tlv)
Extract the first TLV from an LLDPDU.
Definition: lldp_tlv.c:247
uint8_t type
Definition: lldp_tlv.h:202
@ LLDP_MED_SUBTYPE_EXT_POWER_VIA_MDI
Extended Power-via-MDI.
Definition: lldp_ext_med.h:59
@ LLDP_MED_SUBTYPE_FIRMWARE_REVISION
Inventory - Firmware Revision.
Definition: lldp_ext_med.h:61
@ LLDP_TLV_TYPE_SYS_CAP
System Capabilities.
Definition: lldp_tlv.h:100
const LldpParamName lldpTlvTypeList[]
Definition: lldp_debug.c:48
IEEE 802.1 LLDP extension.
@ LLDP_DOT3_SUBTYPE_POWER_VIA_MDI
Power Via MDI.
Definition: lldp_ext_dot3.h:52
@ LLDP_DOT1_SUBTYPE_PORT_PROTO_VLAN_ID
Port And Protocol VLAN ID.
Definition: lldp_ext_dot1.h:62
const LldpParamName lldpPnoSubtypeList[]
Definition: lldp_debug.c:109
@ LLDP_MED_SUBTYPE_MANUFACTURER_NAME
Inventory - Manufacturer Name.
Definition: lldp_ext_med.h:64
const LldpParamName lldpDot1SubtypeList[]
Definition: lldp_debug.c:72
@ LLDP_MED_SUBTYPE_SOFTWARE_REVISION
Inventory - Software Revision.
Definition: lldp_ext_med.h:62
void lldpDumpDataUnit(LldpDataUnit *lldpdu)
Dump LLDP data unit.
Definition: lldp_debug.c:126
LldpOrgDefTlv
Definition: lldp_tlv.h:313
@ LLDP_TLV_TYPE_MGMT_ADDR
Management Address.
Definition: lldp_tlv.h:101
error_t lldpGetNextTlv(LldpDataUnit *lldpdu, LldpTlv *tlv)
Extract the next TLV from an LLDPDU.
Definition: lldp_tlv.c:264
@ LLDP_TLV_TYPE_ORG_DEFINED
Organizationally Specific TLVs.
Definition: lldp_tlv.h:102
@ LLDP_MED_SUBTYPE_ASSET_ID
Inventory - Asset ID.
Definition: lldp_ext_med.h:66
char_t name[]
@ LLDP_MED_SUBTYPE_MODEL_NAME
Inventory - Model Name.
Definition: lldp_ext_med.h:65
@ LLDP_PNO_OUI
PROFIBUS.
Definition: lldp_tlv.h:191
@ LLDP_PNO_SUBTYPE_ALIAS
Alias.
Definition: lldp_ext_pno.h:53
@ LLDP_DOT1_SUBTYPE_RESERVED
Reserved.
Definition: lldp_ext_dot1.h:60
@ LLDP_TLV_TYPE_END_OF_LLDPDU
End Of LLDPDU.
Definition: lldp_tlv.h:93
Data logging functions for debugging purpose (LLDP)
@ LLDP_DOT3_SUBTYPE_RESERVED
Reserved.
Definition: lldp_ext_dot3.h:50
@ LLDP_PNO_SUBTYPE_MEASURED_DELAY_VALUES
Measured Delay Values.
Definition: lldp_ext_pno.h:51
PROFINET extension.
error_t
Error codes.
Definition: error.h:43
@ LLDP_MED_SUBTYPE_SERIAL_NUMBER
Inventory - Serial Number.
Definition: lldp_ext_med.h:63
@ LLDP_DOT1_SUBTYPE_PORT_VLAN_ID
Port VLAN ID.
Definition: lldp_ext_dot1.h:61
const LldpParamName lldpDot3SubtypeList[]
Definition: lldp_debug.c:82
@ LLDP_MED_SUBTYPE_LLDP_MED_CAP
LLDP-MED Capabilities.
Definition: lldp_ext_med.h:56
@ LLDP_MED_SUBTYPE_NETWORK_POLICY
Network Policy.
Definition: lldp_ext_med.h:57
@ LLDP_MED_SUBTYPE_HARDWARE_REVISION
Inventory - Hardware Revision.
Definition: lldp_ext_med.h:60
@ LLDP_MED_OUI
LLDP-MED.
Definition: lldp_tlv.h:190
@ LLDP_DOT3_SUBTYPE_MAX_FRAME_SIZE
Maximum Frame Size.
Definition: lldp_ext_dot3.h:54
@ LLDP_DOT1_OUI
IEEE 802.1.
Definition: lldp_tlv.h:188
const char_t * lldpGetParamName(uint_t value, const LldpParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: lldp_debug.c:245
@ LLDP_TLV_TYPE_SYS_NAME
System Name.
Definition: lldp_tlv.h:98
@ LLDP_TLV_TYPE_CHASSIS_ID
Chassis ID.
Definition: lldp_tlv.h:94
@ LLDP_PNO_SUBTYPE_MRP_PORT_STATUS
MRP Port Status.
Definition: lldp_ext_pno.h:54
char char_t
Definition: compiler_port.h:48
@ LLDP_PNO_SUBTYPE_INTERFACE_MAC_ADDR
Interface MAC address.
Definition: lldp_ext_pno.h:55
@ LLDP_PNO_SUBTYPE_PTCP_STATUS
PTCP Status.
Definition: lldp_ext_pno.h:56
@ LLDP_MED_SUBTYPE_RESERVED
Reserved.
Definition: lldp_ext_med.h:55
@ LLDP_TLV_TYPE_SYS_DESC
System Description.
Definition: lldp_tlv.h:99
LLDP-MED extension (LLDP for Media Endpoint Devices)
@ LLDP_DOT3_OUI
IEEE 802.3.
Definition: lldp_tlv.h:189
size_t length
Definition: lldp_tlv.h:203
@ LLDP_PNO_SUBTYPE_PORT_STATUS
Port Status.
Definition: lldp_ext_pno.h:52
#define LOAD24BE(p)
Definition: cpu_endian.h:197
uint8_t value[]
Definition: tcp.h:369
void lldpDumpTlv(const LldpTlv *tlv)
Dump TLV structure.
Definition: lldp_debug.c:153
@ LLDP_DOT1_SUBTYPE_VLAN_NAME
VLAN Name.
Definition: lldp_ext_dot1.h:63
const LldpParamName lldpOuiList[]
Definition: lldp_debug.c:63
@ LLDP_TLV_TYPE_PORT_ID
Port ID.
Definition: lldp_tlv.h:95
@ LLDP_MED_SUBTYPE_LOCATION_ID
Location Identification.
Definition: lldp_ext_med.h:58
@ LLDP_DOT1_SUBTYPE_PROTOCOL_ID
Protocol Identity.
Definition: lldp_ext_dot1.h:64
LLDP (Link Layer Discovery Protocol)
@ LLDP_DOT3_SUBTYPE_MAC_PHY_CONFIG_STATUS
MAC/PHY Configuration/Status.
Definition: lldp_ext_dot3.h:51
@ LLDP_DOT3_SUBTYPE_LINK_AGGREGATION
Link Aggregation.
Definition: lldp_ext_dot3.h:53
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
const LldpParamName lldpMedSubtypeList[]
Definition: lldp_debug.c:92
@ LLDP_TLV_TYPE_TIME_TO_LIVE
Time To Live.
Definition: lldp_tlv.h:96
IEEE 802.3 LLDP extension.
Parameter value/name binding.
Definition: lldp_debug.h:49
TLV structure.
Definition: lldp_tlv.h:200
@ LLDP_PNO_SUBTYPE_RESERVED
Reserved.
Definition: lldp_ext_pno.h:50
@ LLDP_TLV_TYPE_PORT_DESC
Port Description.
Definition: lldp_tlv.h:97
const char_t * name
Definition: lldp_debug.h:51
Debugging facilities.
#define TRACE_VERBOSE(...)
Definition: debug.h:124
#define arraysize(a)
Definition: os_port.h:71
#define LldpDataUnit
Definition: lldp.h:36
uint8_t * value
Definition: lldp_tlv.h:204
#define TRACE_VERBOSE_ARRAY(p, a, n)
Definition: debug.h:125