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