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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers snmp.cpp Source File

snmp.cpp

00001 /**
00002 * Agentbed SNMP Server
00003 *  Modified for mbed, 2011 Suga.
00004 *
00005 * Agentuino SNMP Agent Library Prototyping...
00006 * Copyright 2010 Eric C. Gionet <lavco_eg@hotmail.com>
00007 */
00008 
00009 #include "mbed.h"
00010 #include "EthernetNetIf.h"
00011 #include "weather.h"
00012 #ifdef USE_SNMP
00013 #include "Agentbed.h"
00014 
00015 static AgentbedClass Agentbed;
00016 
00017 // RFC1213-MIB OIDs
00018 // .iso (.1)
00019 // .iso.org (.1.3)
00020 // .iso.org.dod (.1.3.6)
00021 // .iso.org.dod.internet (.1.3.6.1)
00022 // .iso.org.dod.internet.mgmt (.1.3.6.1.2)
00023 // .iso.org.dod.internet.mgmt.mib-2 (.1.3.6.1.2.1)
00024 // .iso.org.dod.internet.mgmt.mib-2.system (.1.3.6.1.2.1.1)
00025 // .iso.org.dod.internet.mgmt.mib-2.system.sysDescr (.1.3.6.1.2.1.1.1)
00026 const char sysDescr[]    = "1.3.6.1.2.1.1.1.0";  // read-only  (DisplayString)
00027 // .iso.org.dod.internet.mgmt.mib-2.system.sysObjectID (.1.3.6.1.2.1.1.2)
00028 const char sysObjectID[] = "1.3.6.1.2.1.1.2.0";  // read-only  (ObjectIdentifier)
00029 // .iso.org.dod.internet.mgmt.mib-2.system.sysUpTime (.1.3.6.1.2.1.1.3)
00030 const char sysUpTime[]   = "1.3.6.1.2.1.1.3.0";  // read-only  (TimeTicks)
00031 // .iso.org.dod.internet.mgmt.mib-2.system.sysContact (.1.3.6.1.2.1.1.4)
00032 const char sysContact[]  = "1.3.6.1.2.1.1.4.0";  // read-write (DisplayString)
00033 // .iso.org.dod.internet.mgmt.mib-2.system.sysName (.1.3.6.1.2.1.1.5)
00034 const char sysName[]     = "1.3.6.1.2.1.1.5.0";  // read-write (DisplayString)
00035 // .iso.org.dod.internet.mgmt.mib-2.system.sysLocation (.1.3.6.1.2.1.1.6)
00036 const char sysLocation[] = "1.3.6.1.2.1.1.6.0";  // read-write (DisplayString)
00037 // .iso.org.dod.internet.mgmt.mib-2.system.sysServices (.1.3.6.1.2.1.1.7)
00038 const char sysServices[] = "1.3.6.1.2.1.1.7.0";  // read-only  (Integer)
00039 //
00040 // Arduino defined OIDs
00041 // .iso.org.dod.internet.private (.1.3.6.1.4)
00042 // .iso.org.dod.internet.private.enterprises (.1.3.6.1.4.1)
00043 // .iso.org.dod.internet.private.enterprises.arduino (.1.3.6.1.4.1.36582)
00044 const char enterprises[] = "1.3.6.1.4.1.36582.";  // read-only  (Integer)
00045 //
00046 //
00047 // RFC1213 local values
00048 static char locDescr[]              = "mbed Weather Platform";  // read-only (static)
00049 static char locObjectID[]           = "1.3.6.1.4.1.36582";                       // read-only (static)
00050 //static uint32_t locUpTime           = 0;                                        // read-only (static)
00051 static char locContact[]            = "<root@weather>";                            // should be stored/read from EEPROM - read/write (not done for simplicity)
00052 static char locName[]               = "weather.mbed";                              // should be stored/read from EEPROM - read/write (not done for simplicity)
00053 static char locLocation[]           = "weather";                        // should be stored/read from EEPROM - read/write (not done for simplicity)
00054 static int32_t locServices          = 7;                                        // read-only (static)
00055 
00056 static char oid[SNMP_MAX_OID_LEN];
00057 static SNMP_API_STAT_CODES api_status;
00058 static SNMP_ERR_CODES status;
00059 
00060 
00061 void pduReceived()
00062 {
00063   SNMP_PDU pdu;
00064   //
00065   LED_NET_ON;
00066   api_status = Agentbed.requestPdu(&pdu);
00067   //
00068   if ( pdu.type == SNMP_PDU_GET || pdu.type == SNMP_PDU_GET_NEXT || pdu.type == SNMP_PDU_SET
00069     && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS ) {
00070     //
00071     pdu.OID.toString(oid);
00072     //
00073     pdu.error = SNMP_ERR_READ_ONLY;
00074     //
00075     if ( strcmp(oid, sysDescr ) == 0 ) {
00076       // handle sysDescr (set/get) requests
00077       if ( pdu.type == SNMP_PDU_GET ) {
00078         // response packet from get-request - locDescr
00079         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locDescr);
00080         pdu.error = status;
00081       }
00082     } else if ( strcmp(oid, sysObjectID ) == 0 ) {
00083       // handle sysName (set/get) requests
00084       if ( pdu.type == SNMP_PDU_GET ) {
00085         // response packet from get-request - locUpTime
00086         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locObjectID);
00087         pdu.error = status;
00088       }
00089     } else if ( strcmp(oid, sysUpTime ) == 0 ) {
00090       // handle sysName (set/get) requests
00091       if ( pdu.type == SNMP_PDU_GET ) {
00092         // response packet from get-request - locUpTime
00093         status = pdu.VALUE.encode(SNMP_SYNTAX_TIME_TICKS, locUpTime);
00094         pdu.error = status;
00095       }
00096     } else if ( strcmp(oid, sysName ) == 0 ) {
00097       // handle sysName (set/get) requests
00098       if ( pdu.type == SNMP_PDU_GET ) {
00099         // response packet from get-request - locName
00100         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locName);
00101         pdu.error = status;
00102       }
00103     } else if ( strcmp(oid, sysContact ) == 0 ) {
00104       // handle sysContact (set/get) requests
00105       if ( pdu.type == SNMP_PDU_GET ) {
00106         // response packet from get-request - locContact
00107         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locContact);
00108         pdu.error = status;
00109       }
00110     } else if ( strcmp(oid, sysLocation ) == 0 ) {
00111       // handle sysLocation (set/get) requests
00112       if ( pdu.type == SNMP_PDU_GET ) {
00113         // response packet from get-request - locLocation
00114         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locLocation);
00115         pdu.error = status;
00116       }
00117     } else if ( strcmp(oid, sysServices) == 0 ) {
00118       // handle sysServices (set/get) requests
00119       if ( pdu.type == SNMP_PDU_GET ) {
00120         // response packet from get-request - locServices
00121         status = pdu.VALUE.encode(SNMP_SYNTAX_INT, locServices);
00122         pdu.error = status;
00123       }
00124     } else if ( strncmp(oid, enterprises, strlen(enterprises)) == 0 ) {
00125       // handle enterprises (set/get) requests
00126       if ( pdu.type == SNMP_PDU_GET ) {
00127         // response packet from get-request - enterprises
00128         switch (oid[strlen(enterprises)]) {
00129         case '0':
00130             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.pres);
00131             break;
00132         case '1':
00133             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.temp);
00134             break;
00135         case '2':
00136             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.humi);
00137             break;
00138         case '3':
00139             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.anemo);
00140             break;
00141         case '4':
00142             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.vane);
00143             break;
00144         case '5':
00145             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.rain);
00146             break;
00147         case '6':
00148             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.light);
00149             break;
00150         case '7':
00151             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.uv);
00152             break;
00153         case '8':
00154             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.moist);
00155             break;
00156         case '9':
00157             status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.temp2);
00158             break;
00159         }
00160         pdu.error = status;
00161       }
00162     } else {
00163       // oid does not exist
00164       //
00165       // response packet - object not found
00166       pdu.error = SNMP_ERR_NO_SUCH_NAME;
00167     }
00168     //
00169     pdu.type = SNMP_PDU_RESPONSE;
00170 
00171     Agentbed.responsePdu(&pdu);
00172   }
00173   //
00174   Agentbed.freePdu(&pdu);
00175   //
00176   //Serial << "UDP Packet Received End.." << " RAM:" << freeMemory() << endl;
00177 }
00178 #endif
00179 
00180 int snmp_init (char *commname) {
00181 #ifdef USE_SNMP
00182     if (! ethernet_flg) return 0;
00183     pc.printf("SNMP community name: %s\r\n", buf);
00184 
00185     Agentbed.begin(commname, "None", SNMP_DEFAULT_PORT, eth);
00186     Agentbed.onPduReceive(pduReceived);
00187 #endif
00188     return 0;
00189 }
00190