Hardwired TCP/IP Chapter 15: W55MH32 SNMP Example
Hardwired TCP/IP Chapter 15: W55MH32 SNMP Example

Hardwired TCP/IP Chapter 15: W55MH32 SNMP Example
In this article, we will provide a detailed explanation on how to implement the SNMP function on the W55MH32 chip. Through practical examples, we will also explain to you how to manage the W55MH32 using the MIB Browser.
Other network protocols used in this example, such as DHCP, please refer to the relevant sections. Regarding the initialization process of the W55MH32, please refer to the Network Install section. We will not elaborate on it here.
1 Introduction to the SNMP Protocol
SNMP (Simple Network Management Protocol, Simple Network Management Protocol) is a protocol used for managing and monitoring network devices. It is an application layer protocol and is widely applied in the management and monitoring of network devices (such as routers, switches, servers, printers, etc.). SNMP provides mechanisms that allow network administrators to monitor network performance, detect network issues, and manage the devices.
2 Characteristics of the SNMP Protocol
Simplicity: The design is lightweight, facilitating implementation and deployment.
Interoperability: It provides standardized device management methods, enabling intercommunication among devices from different manufacturers via SNMP.
Cross-platform support: SNMP is an open standard protocol widely used in various network devices and operating systems.
Real-time monitoring: It supports rapid data collection and alarm mechanisms.
Scalability: Through MIB, it supports specific functions of different devices.
Resource efficiency: The protocol design is lightweight, suitable for low-bandwidth and high-latency network environments.
3 Application scenario
Next, let's take a look at what operations and applications can be accomplished using the SNMP protocol on the W55MH32?
Fault alarm and log management: When the device detects an abnormality or failure, it can send alarm information to the network management system through SNMP TRAP.
Industrial automation and environmental monitoring: In industrial automation and environmental monitoring, the W55MH32 can report sensor data through SNMP.
Data center and server room management: It is used to monitor the status of servers, switches, and other network devices in data centers and server rooms.
4 Use MIB Browser to manage the W55MH32 process
- Download and install MIB Browser (link::https://www.ireasoning.com/download/mibfree/setup.exe)
- Create a branch
Open the "mibs" folder in the installation directory, locate the "RFC1213-MIB" file, right-click on it and use Notepad to open it. The path can be found in the upper path bar.
Add a new branch after SNMP and name it "User".
3. Add leaf nodes (function)
Continue to add function code behind the branches
Leaf node format is as follows:
setLED OBJECT-TYPE Add a new leaf node with the name setLED
SYNTAX INTEGER { enabled(1),disabled(0) } The data type of the setting
ACCESS read-write Set read-write permissions
DESCRIPTION Comment
::= { User 1 } The branch where the leaf is located, along with the leaf number
After adding, save and exit
After adding, the MIB Browser software is as follows:
3. Code Adaptation Function
4. Testing
5 The components of the SNMP architecture
The SNMP architecture consists of the following three main components:
Manager: Runs SNMP management software, used to send requests and receive device information. It operates network devices through commands, performing configuration or monitoring tasks.
Agent: Software installed on the managed devices, responsible for storing the status information of the devices in the MIB and responding to queries from the management station. It can actively send traps to report events.
Management Information Base (MIB): Defines the manageable parameters of the devices and their data structures. MIB is usually organized in a tree-like structure, with each node representing a management object and having a unique Object Identifier (OID).
Protocol (SNMP protocol itself): Responsible for communication between the management station and the agent, supporting basic operations such as getting, setting, and notification.
6 Detailed Explanation of OID
The Object Identifier (OID) of the Simple Network Management Protocol (SNMP) is an identifier used to uniquely identify the objects in the Management Information Base (MIB) of network devices. OID is a hierarchical naming scheme that enables administrators to query and set various parameters on network devices.
Here is the detailed explanation of OID:
OID structure
An OID is composed of a series of integers, which are separated by dots (.) to form a hierarchical structure. For example:
1.3.6.1.2.1.1.1.0
In this structure, each number represents the position of an organization or node in the hierarchical structure.
Explanation of OID hierarchy
1: represents ISO (International Organization for Standardization)
3: represents org (organization)
6: specifies IETF (Internet Engineering Task Force) as the organization
1: represents the MIB (Management Information Base) managed by IETF
The subsequent numbers further define specific MIB modules, object types, instances, etc.
Common OID prefixes
1.3.6.1.2.1: This is the most commonly used OID prefix, usually abbreviated as.iso.org.dod.internet.mgmt.mib-2, which refers to MIB-2 defined by IETF.
Specific OID examples
1.3.6.1.2.1.1.1.0: This is the OID for sysDescr, used to obtain the system description.
1.3.6.1.2.1.1.2.0: This is the OID for sysObjectID, used to obtain the system object identifier.
1.3.6.1.2.1.1.3.0: This is the OID for sysUpTime, used to obtain the system uptime.
How to use OID
Query: Use SNMP GET request to query the value of a specific OID.
Set: Use SNMP SET request to modify the value of a specific OID (requires device support).
Traverse: Use SNMP WALK request to traverse all sub-nodes under an OID. Notes for Attention
The specific values and structures of OIDs may vary depending on the type of network device. Before using OIDs, it is advisable to consult the MIB documentation of the corresponding device to understand the specific OIDs it supports and their functions. OIDs are an indispensable part of SNMP management. Through OIDs, network administrators can perform operations such as monitoring network status, configuring network devices, and receiving alarm notifications. Understanding and mastering OIDs is very important for network management and troubleshooting.
7 SNMP Message Format
SNMP messages are encoded based on the ASN.1 (Abstract Syntax Notation One) standard and are typically transmitted using BER (Basic Encoding Rules). The following is the basic format and key fields of SNMP messages. In this example, SNMPv1 is used. The following explains SNMPv1. For the message formats of other versions, due to space limitations, we will not explain them here. Interested readers can consult relevant materials for self-study.
SNMP Message Structure
Field Name | Type | Description |
Message | Sequence | The container for the entire SNMP message, including the following fields. |
Version | Integer | SNMP protocol version number. 0 indicates SNMPv1. |
Community | Octet String | Community string, used for authentication, such as public or private. |
PDU Type | Tag, Context - Specific | Indicates the operation type, such as: GET (0xA0), GET - NEXT (0xA1), SET (0xA3), TRAP (0xA4). |
Request ID | Integer | The unique identifier of the request, used to match requests and responses. |
Error Status | Integer | Error status. The value range is: 0 (noError), 1 (tooBig), 2 (noSuchName), etc. |
Error Index | Integer | Error index, indicating the position of the variable in the variable - binding list where an error occurred (counting starts from 1). |
Variable Bindings | Sequence of VarBinds | Variable - binding list. Each binding contains an OID and a corresponding value. |
- OID | Object Identifier (OID) | The identifier of the managed object. For example, 1.3.6.1.2.1.1.1.0 represents the system description (sysDescr). |
- Value | Variable Type (Null, Integer, String, etc.) | The value corresponding to the OID. It may be a null value (for GET requests) or specific data (for responses or SET requests). |
Detailed Explanation of Message Fields
Message (Message Structure)
This is the outermost structure of an SNMP message, which is a sequence (Sequence) consisting of the following three parts:
Version (Version Number)
This field defines the version number of the SNMP protocol, represented by an integer:
- 0 represents SNMPv1.
- 1 represents SNMPv2c.
- 3 represents SNMPv3.
This field is the basis for parsing the message and different versions of the message have different formats.
Community (Community String)
The community string is a simple authentication mechanism for SNMPv1 and SNMPv2c, used to restrict access to managed objects. It is a string (Octet String), and common values include:
public: indicating read-only access permission.
private: indicating read-write access permission.
PDU (Protocol Data Unit) Type
PDU defines the operation type and is distinguished by a tag value (Context-Specific):
- 0xA0: GET Request, used to obtain the value of the managed object.
- 0xA1: GET-NEXT Request, used to obtain the value of the next object.
- 0xA3: SET Request, used to set the value of the managed object.
- 0xA4: TRAP, used for the agent to report events to the management station.
Request ID (Request ID)
The Request ID is an integer used to uniquely identify a request. It is generated by the initiator and carried in the response with the same ID. Through this field, the receiver can match the response with the corresponding request. If the Request ID in the response is inconsistent, it indicates that the response is unrelated to the request.
Error Status (Error Status)
This field is represented by an integer to indicate the execution status of the request:
0: noError, indicating that the request execution was successful and there was no error.
1: tooBig, the response data of the request exceeds the maximum allowable size of the receiver.
2: noSuchName, the OID specified in the request does not exist.
3: badValue, the value in the request is illegal or not supported.
4: readOnly, the request attempts to modify a read-only object.
5: genErr, a general error occurred, the specific reason is not clear.
Error Index (Error Index)
If the Error Status value is not 0, this field indicates the position of the variable binding that is incorrect in the variable binding list (counted from 1). If there is no error, the value of this field is 0.
Variable Bindings (Variable Binding List)
The variable binding list is the core part of the SNMP message, containing one or more variable bindings (VarBind). Each variable binding consists of the following two parts:
OID (Object Identifier)
OID is the unique identifier of the managed object, represented by a dot-decimal notation. For example:
1.3.6.1.2.1.1.1.0: represents sysDescr, system description.
1.3.6.1.2.1.1.5.0: represents sysName, system name.
Value (Data)
The value of OID may vary depending on the request type as follows:
In a GET request, the value is usually null (Empty).
In the GET-RESPONSE message, the value is specific data, such as integer (Integer32), string (Octet String), etc.
In a SET request, the value is the new value to be set.
SNMP message parsing
The MIB Browser sends an LED setting message to W55MH32:
|Message Analysis|
Simple Network Management Protocol (SNMP message)
version: version-1 (0) (The SNMP protocol version used, with a value of 0 indicating SNMPv1)
community: public (The community string is "public", usually indicating read-only access permission)
data: set-request (3) (The PDU type is SET-REQUEST, indicating the setting of the value of a management object) set-request
request-id: 1479407980 (Unique identifier for the request, used to match the request and response)
error-status: noError (0) (Error status is noError (0), indicating no error occurred)
error-index: 0 (Error index is 0, indicating there is no error object in the variable binding list)
variable-bindings: 1 item (Variable binding list contains 1 variable)
1.3.6.1.2.1.12.1.0: 1 (OID is 1.3.6.1.2.1.12.1.0, the set value is 1)
Object Name: 1.3.6.1.2.1.12.1.0 (iso.3.6.1.2.1.12.1.0) (OID corresponding to the complete name)
Value (Integer32): 1 (The set value is of integer type, and the value is 1)
[Response In: 237] (Indicates that the response message sequence number of this request is 237)
|Original Message| 30 2a 02 01 00 04 06 70 75 62 6c 69 63 a3 1d 02 04 58 2d f9 6c 02 01 00 02 01 00 30 0f 30 0d 06 08 2b 06 01 02 01 0c 01 00 02 01 01
W55MH32 Response Message:
|Message Analysis| Simple Network Management Protocol
version: version-1 (0) (Version number: SNMPv1 (value is 0))
community: public (Community string: "public", used for authentication)
data: get-response (2) (Data type: GET-RESPONSE (value is 2), indicating this is a response message)
get-response (GET-RESPONSE data section)
request-id: 1479407980 (Request ID: 1479407980, used for matching requests and responses)
error-status: noError (0) (Error status: noError (value is 0), indicating no error occurred)
error-index: 0 (Error index: 0, indicating no variable index where an error occurred)
variable-bindings: 1 item (Variable bindings: containing 1 variable)
1.3.6.1.2.1.12.1.0: 1 (Variable binding content)
Object Name: 1.3.6.1.2.1.12.1.0 (iso.3.6.1.2.1.12.1.0) (Object Identifier (OID))
Value (Integer32): 1 (OID corresponding value, type is Integer32, value is 1)
[Response To: 236] (Response to request ID 236)
[Time: 0.023844000 seconds] Response time: 0.023844 seconds)
|Original Message| 30 2a 02 01 00 04 06 70 75 62 6c 69 63 a2 1d 02 04 58 2d f9 6c 02 01 00 02 01 00 30 0f 30 0d 06 08 2b 06 01 02 01 0c 01 00 02 01 01
8 The implementation process
Next, let's take a look at how to adapt the functions added on the MIB Browser to the code.
Note: The test instance requires that the PC and W55MH32 be on the same network segment.
Step 1: Initialize the LED and register it in SNMP.
1. user_led_init();
2. user_led_control_init(get_user_led_status, set_user_led_status);
Step 2: Register SNMP timer
1. /**
2. * @brief 1ms timer IRQ Handler
3. * @param none
4. * @return none
5. */
6. void TIM3_IRQHandler(void)
7. {
8. static uint32_t tim3_1ms_count = 0;
9. static uint8_t tim3_10ms_count = 0;
10. if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
11. {
12. tim3_1ms_count++;
13. tim3_10ms_count++;
14. if (tim3_1ms_count >= 1000)
15. {
16. DHCP_time_handler();
17.
18. tim3_1ms_count = 0;
19. }
20. if(tim3_10ms_count>=10)
21. {
22. SNMP_time_handler();
23. tim3_10ms_count=0;
24. }
25. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
26. }
27. }
We need to call the SNMP_time_handler() function once every 10 milliseconds to facilitate SNMP's timeout handling.
Step 3: Add the function
In the snmpData structure variable of the snmp_custom.c file, add the function:
1. dataEntryType snmpData[] =
2. {
3. // System MIB
4. // SysDescr Entry
5. { 8, {0x2b, 6, 1, 2, 1, 1, 1, 0}, SNMPDTYPE_OCTET_STRING, 30, {"WIZnet Embedded SNMP Agent"}, NULL, NULL},
6.
7. // SysObjectID Entry
8. { 8, {0x2b, 6, 1, 2, 1, 1, 2, 0}, SNMPDTYPE_OBJ_ID, 8, {"\x2b\x06\x01\x02\x01\x01\x02\x00"}, NULL, NULL},
9.
10. // SysUptime Entry
11. { 8, {0x2b, 6, 1, 2, 1, 1, 3, 0}, SNMPDTYPE_TIME_TICKS, 0, {""}, currentUptime, NULL},
12.
13. // sysContact Entry
14. { 8, {0x2b, 6, 1, 2, 1, 1, 4, 0}, SNMPDTYPE_OCTET_STRING, 30, {"http://www.wizwiki.net/forum"}, NULL, NULL},
15.
16. // sysName Entry
17. { 8, {0x2b, 6, 1, 2, 1, 1, 5, 0}, SNMPDTYPE_OCTET_STRING, 30, {"http://www.wiznet.co.kr"}, NULL, NULL},
18.
19. // Location Entry
20. { 8, {0x2b, 6, 1, 2, 1, 1, 6, 0}, SNMPDTYPE_OCTET_STRING, 30, {"4F Humax Village"}, NULL, NULL},
21.
22. // SysServices
23. { 8, {0x2b, 6, 1, 2, 1, 1, 7, 0}, SNMPDTYPE_INTEGER, 4, {""}, NULL, NULL},
24.
25. { 8, {0x2b, 6, 1, 2, 1, 12, 2, 0}, SNMPDTYPE_OCTET_STRING, 30, {""}, get_LEDStatus_UserLED, NULL},
26.
27. { 8, {0x2b, 6, 1, 2, 1, 12, 1, 0}, SNMPDTYPE_INTEGER, 4, {""}, NULL, set_LEDStatus_UserLED},
28.
29. // OID Test #1 (long-length OID example, 19865)
30. {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x9b, 0x19, 0x01, 0x00}, SNMPDTYPE_OCTET_STRING, 30, {"long-length OID Test #1"}, NULL, NULL},
31.
32. // OID Test #2 (long-length OID example, 22210)
33. {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0xad, 0x42, 0x01, 0x00}, SNMPDTYPE_OCTET_STRING, 35, {"long-length OID Test #2"}, NULL, NULL},
34.
35. // OID Test #2: SysObjectID Entry
36. {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0xad, 0x42, 0x02, 0x00}, SNMPDTYPE_OBJ_ID, 0x0a, {"\x2b\x06\x01\x04\x01\x81\xad\x42\x02\x00"}, NULL, NULL},
37. };
The structure of the variable snmpData and the definition of the structure dataEntryType are as follows:
1. typedef struct {
2. uint8_t oidlen;
3. uint8_t oid[MAX_OID];
4. uint8_t dataType;
5. uint8_t dataLen;
6. union {
7. uint8_t octetstring[MAX_STRING];
8. uint32_t intval;
9. } u;
10. void (*getfunction)(void *, uint8_t *);
11. void (*setfunction)(int32_t);
12. } dataEntryType;
Step 4: Initialize the SNMP protocol
1. void snmpd_init(uint8_t * managerIP, uint8_t * agentIP, uint8_t sn_agent, uint8_t sn_trap)
2. {
3. #ifdef _SNMP_DEBUG_
4. printf("\r\n - SNMP : Start SNMP Agent Daemon\r\n");
5. #endif
6. SOCK_SNMP_AGENT = sn_agent;
7. SOCK_SNMP_TRAP = sn_trap;
8.
9. if((SOCK_SNMP_AGENT > _WIZCHIP_SOCK_NUM_) || (SOCK_SNMP_TRAP > _WIZCHIP_SOCK_NUM_)) return;
10.
11. startTime = getSNMPTimeTick(); // Start time (unit: 10ms)
12. initTable(); // Settings for OID entry values
13.
14. initial_Trap(managerIP, agentIP);
15.
16. /*
17. // Example Codes for SNMP Trap
18. {
19. dataEntryType enterprise_oid = {0x0a, {0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x9b, 0x19, 0x01, 0x00},
20. SNMPDTYPE_OBJ_ID, 0x0a, {"\x2b\x06\x01\x04\x01\x81\x9b\x19\x10\x00"}, NULL, NULL};
21.
22. dataEntryType trap_oid1 = {8, {0x2b, 6, 1, 4, 1, 0, 11, 0}, SNMPDTYPE_OCTET_STRING, 30, {""}, NULL, NULL};
23. dataEntryType trap_oid2 = {8, {0x2b, 6, 1, 4, 1, 0, 12, 0}, SNMPDTYPE_INTEGER, 4, {""}, NULL, NULL};
24.
25. strcpy((char *)trap_oid1.u.octetstring, "Alert!!!"); // String added
26. trap_oid2.u.intval = 123456; // Integer value added
27.
28. // Generic Trap: warmStart
29. snmp_sendTrap((void *)"192.168.0.214", (void *)"192.168.0.112", (void *)"public", enterprise_oid, SNMPTRAP_WARMSTART, 0, 0);
30.
31. // Enterprise-Specific Trap
32. snmp_sendTrap((void *)"192.168.0.214", (void *)"192.168.0.112", (void *)"public", enterprise_oid, 6, 0, 2, &trap_oid1, &trap_oid2);
33. }
34. */
35. }
This step mainly involves registering the used socket number, managing IP addresses, requesting IP addresses, and other parameters, and recording the start time. If you want to use Trap for active reporting, you can refer to the example code in the comments.
Step 5: Run the snmpd_run() function in the main loop
The code for the snmpd_run() function is as follows:
1. int32_t snmpd_run(void)
2. {
3. int32_t ret;
4. int32_t len = 0;
5.
6. uint8_t svr_addr[6];
7. uint16_t svr_port;
9. if(SOCK_SNMP_AGENT > _WIZCHIP_SOCK_NUM_) return -99;
10.
11. switch(getSn_SR(SOCK_SNMP_AGENT))
12. {
13. case SOCK_UDP :
14. if ( (len = getSn_RX_RSR(SOCK_SNMP_AGENT)) > 0)
15. {
16. request_msg.len= recvfrom(SOCK_SNMP_AGENT, request_msg.buffer, len, svr_addr, &svr_port);
17. }
18. else
19. {
20. request_msg.len = 0;
21. }
22.
23. if (request_msg.len > 0)
24. {
25. #ifdef _SNMP_DEBUG_
26. dumpCode((void *)"\r\n[Request]\r\n", (void *)"\r\n", request_msg.buffer, request_msg.len);
27. #endif
28. // Initialize
29. request_msg.index = 0;
30. response_msg.index = 0;
31. errorStatus = errorIndex = 0;
32. memset(response_msg.buffer, 0x00, MAX_SNMPMSG_LEN);
33.
34. // Received message parsing and send response process
35. if (parseSNMPMessage() != -1)
36. {
37. sendto(SOCK_SNMP_AGENT, response_msg.buffer, response_msg.index, svr_addr, svr_port);
38. }
39.
40. #ifdef _SNMP_DEBUG_
41. dumpCode((void *)"\r\n[Response]\r\n", (void *)"\r\n", response_msg.buffer, response_msg.index);
42. #endif
43. }
44. break;
45.
46. case SOCK_CLOSED :
47. if((ret = socket(SOCK_SNMP_AGENT, Sn_MR_UDP, PORT_SNMP_AGENT, 0x00)) != SOCK_SNMP_AGENT)
48. return ret;
49. #ifdef _SNMP_DEBUG_
50. printf(" - [%d] UDP Socket for SNMP Agent, port [%d]\r\n", SOCK_SNMP_AGENT, PORT_SNMP_AGENT);
51. #endif
52. break;
53.
54. default :
55. break;
56. }
57.
59. return 1;
60. }
The `snmpd_run()` function will execute a UDP state machine. When it receives an SNMP management message, it will perform parsing and response operations.
9 Run results
After the burning routine is executed, the first thing you will notice is that the PHY link detection and DHCP acquisition of network information have been printed. Then, the SNMP program is run.
Open the MIB Browser, enter the address of W55MH32, and then click on each node under the "system" branch in sequence. The obtained result is the same as the code definition:
Then find the "setLED" instruction under the "User" branch. Right-click on the instruction, select "set", choose "integer" as the type, enter 1 in the Value field, and click "OK".
The "getLED" command, the right-click command, simply click "get" to read the status of the LED.
10 Summary
This article explains how to implement SNMP functionality on the W55MH32 chip. Through practical examples, it demonstrates the specific process of managing the W55MH32 using MIB Browser, covering key steps such as creating branches, adding leaf nodes, and adapting the functions in the code. The article details the concept, characteristics, application scenarios, architecture composition, detailed explanation of OIDs, and message format of the SNMP protocol, helping readers understand its significant role in network device management and monitoring.
The next article will focus on the PING command, explaining its principle of testing network connectivity and its application in network fault troubleshooting, as well as how to use the PING command on the W55MH32 for network diagnosis. Stay tuned!
WIZnet is a non-fabrication semiconductor company founded in 1998. Its products include the Internet processor iMCU™, which adopts TOE (TCP/IP Offloading Engine) technology and is based on a unique patented fully hardwired TCP/IP. iMCU™ is designed for embedded Internet devices in various applications.
WIZnet has over 70 distributors worldwide, with offices in Hong Kong, South Korea, and the United States, providing technical support and product marketing.
The region managed by the Hong Kong office includes: Australia, India, Turkey, and Asia (excluding South Korea and Japan).