mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/
Dependencies: ChaNFSSD EthernetNetIf I2CLEDDisp Agentbed ChaNFSUSB ILinterpreter mbed BMP085 WeatherMeters ConfigFile ChaNFS I2CLCD
snmp.cpp@2:a3e5edf84f74, 2011-08-24 (annotated)
- Committer:
- okini3939
- Date:
- Wed Aug 24 13:22:32 2011 +0000
- Revision:
- 2:a3e5edf84f74
- Parent:
- 1:6c7141895545
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:bdb53686c194 | 1 | /** |
okini3939 | 0:bdb53686c194 | 2 | * Agentbed SNMP Server |
okini3939 | 0:bdb53686c194 | 3 | * Modified for mbed, 2011 Suga. |
okini3939 | 0:bdb53686c194 | 4 | * |
okini3939 | 0:bdb53686c194 | 5 | * Agentuino SNMP Agent Library Prototyping... |
okini3939 | 0:bdb53686c194 | 6 | * Copyright 2010 Eric C. Gionet <lavco_eg@hotmail.com> |
okini3939 | 0:bdb53686c194 | 7 | */ |
okini3939 | 1:6c7141895545 | 8 | |
okini3939 | 0:bdb53686c194 | 9 | #include "mbed.h" |
okini3939 | 0:bdb53686c194 | 10 | #include "EthernetNetIf.h" |
okini3939 | 0:bdb53686c194 | 11 | #include "weather.h" |
okini3939 | 0:bdb53686c194 | 12 | #ifdef USE_SNMP |
okini3939 | 0:bdb53686c194 | 13 | #include "Agentbed.h" |
okini3939 | 0:bdb53686c194 | 14 | |
okini3939 | 0:bdb53686c194 | 15 | static AgentbedClass Agentbed; |
okini3939 | 0:bdb53686c194 | 16 | |
okini3939 | 0:bdb53686c194 | 17 | // RFC1213-MIB OIDs |
okini3939 | 0:bdb53686c194 | 18 | // .iso (.1) |
okini3939 | 0:bdb53686c194 | 19 | // .iso.org (.1.3) |
okini3939 | 0:bdb53686c194 | 20 | // .iso.org.dod (.1.3.6) |
okini3939 | 0:bdb53686c194 | 21 | // .iso.org.dod.internet (.1.3.6.1) |
okini3939 | 0:bdb53686c194 | 22 | // .iso.org.dod.internet.mgmt (.1.3.6.1.2) |
okini3939 | 0:bdb53686c194 | 23 | // .iso.org.dod.internet.mgmt.mib-2 (.1.3.6.1.2.1) |
okini3939 | 0:bdb53686c194 | 24 | // .iso.org.dod.internet.mgmt.mib-2.system (.1.3.6.1.2.1.1) |
okini3939 | 0:bdb53686c194 | 25 | // .iso.org.dod.internet.mgmt.mib-2.system.sysDescr (.1.3.6.1.2.1.1.1) |
okini3939 | 0:bdb53686c194 | 26 | const char sysDescr[] = "1.3.6.1.2.1.1.1.0"; // read-only (DisplayString) |
okini3939 | 0:bdb53686c194 | 27 | // .iso.org.dod.internet.mgmt.mib-2.system.sysObjectID (.1.3.6.1.2.1.1.2) |
okini3939 | 0:bdb53686c194 | 28 | const char sysObjectID[] = "1.3.6.1.2.1.1.2.0"; // read-only (ObjectIdentifier) |
okini3939 | 0:bdb53686c194 | 29 | // .iso.org.dod.internet.mgmt.mib-2.system.sysUpTime (.1.3.6.1.2.1.1.3) |
okini3939 | 0:bdb53686c194 | 30 | const char sysUpTime[] = "1.3.6.1.2.1.1.3.0"; // read-only (TimeTicks) |
okini3939 | 0:bdb53686c194 | 31 | // .iso.org.dod.internet.mgmt.mib-2.system.sysContact (.1.3.6.1.2.1.1.4) |
okini3939 | 0:bdb53686c194 | 32 | const char sysContact[] = "1.3.6.1.2.1.1.4.0"; // read-write (DisplayString) |
okini3939 | 0:bdb53686c194 | 33 | // .iso.org.dod.internet.mgmt.mib-2.system.sysName (.1.3.6.1.2.1.1.5) |
okini3939 | 0:bdb53686c194 | 34 | const char sysName[] = "1.3.6.1.2.1.1.5.0"; // read-write (DisplayString) |
okini3939 | 0:bdb53686c194 | 35 | // .iso.org.dod.internet.mgmt.mib-2.system.sysLocation (.1.3.6.1.2.1.1.6) |
okini3939 | 0:bdb53686c194 | 36 | const char sysLocation[] = "1.3.6.1.2.1.1.6.0"; // read-write (DisplayString) |
okini3939 | 0:bdb53686c194 | 37 | // .iso.org.dod.internet.mgmt.mib-2.system.sysServices (.1.3.6.1.2.1.1.7) |
okini3939 | 0:bdb53686c194 | 38 | const char sysServices[] = "1.3.6.1.2.1.1.7.0"; // read-only (Integer) |
okini3939 | 0:bdb53686c194 | 39 | // |
okini3939 | 0:bdb53686c194 | 40 | // Arduino defined OIDs |
okini3939 | 0:bdb53686c194 | 41 | // .iso.org.dod.internet.private (.1.3.6.1.4) |
okini3939 | 0:bdb53686c194 | 42 | // .iso.org.dod.internet.private.enterprises (.1.3.6.1.4.1) |
okini3939 | 0:bdb53686c194 | 43 | // .iso.org.dod.internet.private.enterprises.arduino (.1.3.6.1.4.1.36582) |
okini3939 | 0:bdb53686c194 | 44 | const char enterprises[] = "1.3.6.1.4.1.36582."; // read-only (Integer) |
okini3939 | 0:bdb53686c194 | 45 | // |
okini3939 | 0:bdb53686c194 | 46 | // |
okini3939 | 0:bdb53686c194 | 47 | // RFC1213 local values |
okini3939 | 0:bdb53686c194 | 48 | static char locDescr[] = "mbed Weather Platform"; // read-only (static) |
okini3939 | 0:bdb53686c194 | 49 | static char locObjectID[] = "1.3.6.1.4.1.36582"; // read-only (static) |
okini3939 | 2:a3e5edf84f74 | 50 | //static uint32_t locUpTime = 0; // read-only (static) |
okini3939 | 0:bdb53686c194 | 51 | static char locContact[] = "<root@weather>"; // should be stored/read from EEPROM - read/write (not done for simplicity) |
okini3939 | 0:bdb53686c194 | 52 | static char locName[] = "weather.mbed"; // should be stored/read from EEPROM - read/write (not done for simplicity) |
okini3939 | 0:bdb53686c194 | 53 | static char locLocation[] = "weather"; // should be stored/read from EEPROM - read/write (not done for simplicity) |
okini3939 | 0:bdb53686c194 | 54 | static int32_t locServices = 7; // read-only (static) |
okini3939 | 0:bdb53686c194 | 55 | |
okini3939 | 0:bdb53686c194 | 56 | static char oid[SNMP_MAX_OID_LEN]; |
okini3939 | 0:bdb53686c194 | 57 | static SNMP_API_STAT_CODES api_status; |
okini3939 | 0:bdb53686c194 | 58 | static SNMP_ERR_CODES status; |
okini3939 | 0:bdb53686c194 | 59 | |
okini3939 | 0:bdb53686c194 | 60 | |
okini3939 | 0:bdb53686c194 | 61 | void pduReceived() |
okini3939 | 0:bdb53686c194 | 62 | { |
okini3939 | 0:bdb53686c194 | 63 | SNMP_PDU pdu; |
okini3939 | 0:bdb53686c194 | 64 | // |
okini3939 | 0:bdb53686c194 | 65 | LED_NET_ON; |
okini3939 | 0:bdb53686c194 | 66 | api_status = Agentbed.requestPdu(&pdu); |
okini3939 | 0:bdb53686c194 | 67 | // |
okini3939 | 0:bdb53686c194 | 68 | if ( pdu.type == SNMP_PDU_GET || pdu.type == SNMP_PDU_GET_NEXT || pdu.type == SNMP_PDU_SET |
okini3939 | 0:bdb53686c194 | 69 | && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS ) { |
okini3939 | 0:bdb53686c194 | 70 | // |
okini3939 | 0:bdb53686c194 | 71 | pdu.OID.toString(oid); |
okini3939 | 0:bdb53686c194 | 72 | // |
okini3939 | 0:bdb53686c194 | 73 | pdu.error = SNMP_ERR_READ_ONLY; |
okini3939 | 0:bdb53686c194 | 74 | // |
okini3939 | 0:bdb53686c194 | 75 | if ( strcmp(oid, sysDescr ) == 0 ) { |
okini3939 | 0:bdb53686c194 | 76 | // handle sysDescr (set/get) requests |
okini3939 | 0:bdb53686c194 | 77 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 78 | // response packet from get-request - locDescr |
okini3939 | 0:bdb53686c194 | 79 | status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locDescr); |
okini3939 | 0:bdb53686c194 | 80 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 81 | } |
okini3939 | 0:bdb53686c194 | 82 | } else if ( strcmp(oid, sysObjectID ) == 0 ) { |
okini3939 | 0:bdb53686c194 | 83 | // handle sysName (set/get) requests |
okini3939 | 0:bdb53686c194 | 84 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 85 | // response packet from get-request - locUpTime |
okini3939 | 0:bdb53686c194 | 86 | status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locObjectID); |
okini3939 | 0:bdb53686c194 | 87 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 88 | } |
okini3939 | 0:bdb53686c194 | 89 | } else if ( strcmp(oid, sysUpTime ) == 0 ) { |
okini3939 | 0:bdb53686c194 | 90 | // handle sysName (set/get) requests |
okini3939 | 0:bdb53686c194 | 91 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 92 | // response packet from get-request - locUpTime |
okini3939 | 0:bdb53686c194 | 93 | status = pdu.VALUE.encode(SNMP_SYNTAX_TIME_TICKS, locUpTime); |
okini3939 | 0:bdb53686c194 | 94 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 95 | } |
okini3939 | 0:bdb53686c194 | 96 | } else if ( strcmp(oid, sysName ) == 0 ) { |
okini3939 | 0:bdb53686c194 | 97 | // handle sysName (set/get) requests |
okini3939 | 0:bdb53686c194 | 98 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 99 | // response packet from get-request - locName |
okini3939 | 0:bdb53686c194 | 100 | status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locName); |
okini3939 | 0:bdb53686c194 | 101 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 102 | } |
okini3939 | 0:bdb53686c194 | 103 | } else if ( strcmp(oid, sysContact ) == 0 ) { |
okini3939 | 0:bdb53686c194 | 104 | // handle sysContact (set/get) requests |
okini3939 | 0:bdb53686c194 | 105 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 106 | // response packet from get-request - locContact |
okini3939 | 0:bdb53686c194 | 107 | status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locContact); |
okini3939 | 0:bdb53686c194 | 108 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 109 | } |
okini3939 | 0:bdb53686c194 | 110 | } else if ( strcmp(oid, sysLocation ) == 0 ) { |
okini3939 | 0:bdb53686c194 | 111 | // handle sysLocation (set/get) requests |
okini3939 | 0:bdb53686c194 | 112 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 113 | // response packet from get-request - locLocation |
okini3939 | 0:bdb53686c194 | 114 | status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locLocation); |
okini3939 | 0:bdb53686c194 | 115 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 116 | } |
okini3939 | 0:bdb53686c194 | 117 | } else if ( strcmp(oid, sysServices) == 0 ) { |
okini3939 | 0:bdb53686c194 | 118 | // handle sysServices (set/get) requests |
okini3939 | 0:bdb53686c194 | 119 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 120 | // response packet from get-request - locServices |
okini3939 | 0:bdb53686c194 | 121 | status = pdu.VALUE.encode(SNMP_SYNTAX_INT, locServices); |
okini3939 | 0:bdb53686c194 | 122 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 123 | } |
okini3939 | 0:bdb53686c194 | 124 | } else if ( strncmp(oid, enterprises, strlen(enterprises)) == 0 ) { |
okini3939 | 0:bdb53686c194 | 125 | // handle enterprises (set/get) requests |
okini3939 | 0:bdb53686c194 | 126 | if ( pdu.type == SNMP_PDU_GET ) { |
okini3939 | 0:bdb53686c194 | 127 | // response packet from get-request - enterprises |
okini3939 | 0:bdb53686c194 | 128 | switch (oid[strlen(enterprises)]) { |
okini3939 | 0:bdb53686c194 | 129 | case '0': |
okini3939 | 0:bdb53686c194 | 130 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.pres); |
okini3939 | 0:bdb53686c194 | 131 | break; |
okini3939 | 0:bdb53686c194 | 132 | case '1': |
okini3939 | 0:bdb53686c194 | 133 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.temp); |
okini3939 | 0:bdb53686c194 | 134 | break; |
okini3939 | 0:bdb53686c194 | 135 | case '2': |
okini3939 | 0:bdb53686c194 | 136 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.humi); |
okini3939 | 0:bdb53686c194 | 137 | break; |
okini3939 | 0:bdb53686c194 | 138 | case '3': |
okini3939 | 0:bdb53686c194 | 139 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.anemo); |
okini3939 | 0:bdb53686c194 | 140 | break; |
okini3939 | 0:bdb53686c194 | 141 | case '4': |
okini3939 | 0:bdb53686c194 | 142 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.vane); |
okini3939 | 0:bdb53686c194 | 143 | break; |
okini3939 | 0:bdb53686c194 | 144 | case '5': |
okini3939 | 0:bdb53686c194 | 145 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.rain); |
okini3939 | 0:bdb53686c194 | 146 | break; |
okini3939 | 0:bdb53686c194 | 147 | case '6': |
okini3939 | 0:bdb53686c194 | 148 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.light); |
okini3939 | 0:bdb53686c194 | 149 | break; |
okini3939 | 0:bdb53686c194 | 150 | case '7': |
okini3939 | 0:bdb53686c194 | 151 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.uv); |
okini3939 | 0:bdb53686c194 | 152 | break; |
okini3939 | 0:bdb53686c194 | 153 | case '8': |
okini3939 | 0:bdb53686c194 | 154 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.moist); |
okini3939 | 0:bdb53686c194 | 155 | break; |
okini3939 | 0:bdb53686c194 | 156 | case '9': |
okini3939 | 0:bdb53686c194 | 157 | status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.temp2); |
okini3939 | 0:bdb53686c194 | 158 | break; |
okini3939 | 0:bdb53686c194 | 159 | } |
okini3939 | 0:bdb53686c194 | 160 | pdu.error = status; |
okini3939 | 0:bdb53686c194 | 161 | } |
okini3939 | 0:bdb53686c194 | 162 | } else { |
okini3939 | 0:bdb53686c194 | 163 | // oid does not exist |
okini3939 | 0:bdb53686c194 | 164 | // |
okini3939 | 0:bdb53686c194 | 165 | // response packet - object not found |
okini3939 | 0:bdb53686c194 | 166 | pdu.error = SNMP_ERR_NO_SUCH_NAME; |
okini3939 | 0:bdb53686c194 | 167 | } |
okini3939 | 0:bdb53686c194 | 168 | // |
okini3939 | 0:bdb53686c194 | 169 | pdu.type = SNMP_PDU_RESPONSE; |
okini3939 | 0:bdb53686c194 | 170 | |
okini3939 | 0:bdb53686c194 | 171 | Agentbed.responsePdu(&pdu); |
okini3939 | 0:bdb53686c194 | 172 | } |
okini3939 | 0:bdb53686c194 | 173 | // |
okini3939 | 0:bdb53686c194 | 174 | Agentbed.freePdu(&pdu); |
okini3939 | 0:bdb53686c194 | 175 | // |
okini3939 | 0:bdb53686c194 | 176 | //Serial << "UDP Packet Received End.." << " RAM:" << freeMemory() << endl; |
okini3939 | 0:bdb53686c194 | 177 | } |
okini3939 | 0:bdb53686c194 | 178 | #endif |
okini3939 | 0:bdb53686c194 | 179 | |
okini3939 | 0:bdb53686c194 | 180 | int snmp_init (char *commname) { |
okini3939 | 0:bdb53686c194 | 181 | #ifdef USE_SNMP |
okini3939 | 2:a3e5edf84f74 | 182 | if (! ethernet_flg) return 0; |
okini3939 | 2:a3e5edf84f74 | 183 | pc.printf("SNMP community name: %s\r\n", buf); |
okini3939 | 2:a3e5edf84f74 | 184 | |
okini3939 | 0:bdb53686c194 | 185 | Agentbed.begin(commname, "None", SNMP_DEFAULT_PORT, eth); |
okini3939 | 0:bdb53686c194 | 186 | Agentbed.onPduReceive(pduReceived); |
okini3939 | 0:bdb53686c194 | 187 | #endif |
okini3939 | 0:bdb53686c194 | 188 | return 0; |
okini3939 | 0:bdb53686c194 | 189 | } |
okini3939 | 0:bdb53686c194 | 190 |