Lorcan Smith
/
Enet_SPI
SNMP agent attached to SPI slave
Diff: main.cpp
- Revision:
- 2:25e12a7fe0aa
- Parent:
- 0:2a53a4c3238c
--- a/main.cpp Mon Sep 03 10:46:04 2012 +0000 +++ b/main.cpp Thu Sep 06 12:52:48 2012 +0000 @@ -4,47 +4,29 @@ mbed test software for Ethernet HTTPServer with simultaneous SPI for 4 data byte updates -Send serial message via USB every 1.0 second tick Version Date Author Change 0.2: 06/10/11 LS Add block checksum character to SPI data 0.3: 07/10/11 LS Add SPI read from cmd_buf 0.4: 13/10/11 LS Integrated with HTTP server example code 0.5: 18/10/11 LS Initial file handling 0.6: 26/10/11 LS File IO handled by SPI_server class -0.64: 04/11/11 LS RPC bodge added as part of HTTP file handling: -0.66: 04/11/11 LS Show register values as hex bytes as well as word value 0.78: 17/11/11 LS Add SNMP files for system info reporting, but HTTP svr.bind disabled -0.79 30/11/11 LS Add f3kTable as ID 7 in F3000 private MIB -0.79e 12/12/11 LS Reads network addresses from file & uses Field enterprise address +0.79 12/12/11 LS Reads network addresses from file & uses enterprise address 0.80 18/12/11 LS Save last number read in read_addrs_from_file(). - Add write_addrs_to_file() to testaddrs.txt 0.81 22/12/11 LS Add new_cmd_idx to indicate which parameter has been set (0 for none). 0.82 22/12/11 LS Add blinker to show comms activity 0.83 23/12/11 LS Clear cmd_buf[0] after SPI been read 4 times 0.84 27/12/11 LS snmp_fw_version & f3k_fw_version saved as 4 char strings 0.85 29/12/11 LS Update f3k_sys_set_value() & f3k_sys_set_test() to enable net address change 0.90 29/12/11 LS Add 3-step routine for passing commands over SPI bus from cmd_buf -0.92 06/01/12 LS Change 3000 index for 3000 firmware version -0.93 09/01/12 LS Add copy_net_addresses_from() for address updates from 3000 CPU 0.94 10/01/12 LS Add led_link = LPC_GPIO1->FIOPIN & (1<<25) in netservice.cpp 0.95 11/01/12 LS Fix f3k_speed reporting error in private_mib.c 0.96 12/01/12 LS Move led_link = LPC_GPIO1->FIOPIN & (1<<25) to eth_poll in eth_drv.cpp -0.97 30/01/12 LS Add get_SPI_count() & SPI count to MIB as 3000 CPU activity indicator 0.98 26/04/12 LS Increase snmp_publiccommunity[7] to [21] and allow this to be set from file 0.99 08/06/12 LS #define SNMP_TRAP_DESTINATIONS 1. Declare th_ad[4] for host trap address Add init_traps() to ensure head-head link status trap is sent 21/06/12 Add check for changes to IO bytes and chk_SPI_active() every 60 seconds 1.00 22/06/12 LS Save SNMP changes to net addresses & mask4_IO_registers in net_adrs.txt -1.01 25/07/12 LS Allow snmp_publiccommunity to be modified via SNMP -1.02 26/07/12 LS Add changed variable in snmp_varbind io_var to ioChange trap -1.03 01/08/12 LS Only monitor ls byte of I/O registers for changes to mask out Monostable O/Ps -1.04 03/08/12 LS Add chk_alarms() for monitoring up to NUM_MASK I/O alarms -1.05 03/08/12 LS Add struct snmp_varbind io_alarm to pass alarm_msg to f3kIOalarm_trap() -1.06 07/08/12 LS Reduce MSG_LNGTH to 16 from 32. Free up more RAM for alarm strings -1.07 08/08/12 LS Add SNMP R/W of alarm strings -1.08 09/08/12 LS Create array of NUM_MASK alarm strings -1.09 09/08/12 LS Correct problems reading alarm strings via SNMP OK with 32 char strings -1.10 09/08/12 LS Tested OK with MSG_LNGTH 16 char strings http://192.168.99.99/rpc/offset,604 writes 604 to reg_offset http://192.168.99.99/rpc/led2/write,1 writes 1 to led2 @@ -54,9 +36,8 @@ #include "mbed.h" #include "EthernetNetIf.h" #include "HTTPServer.h" -#include "snmp_structs.h" -#include "snmp_msg.h" -#include "snmp_asn1.h" +#include "lwip/snmp_structs.h" +#include "lwip/snmp_msg.h" #include "IO_file.h" #define LPC1768 0x60 // SPI address of LPC1768 module @@ -70,25 +51,9 @@ #define NO_UPDATE 0 // NO change to Ethernet addresses #define UPDATED 0xff // Ethernet addresses updated in file #define NET_ADRS_FILE "/webfs/net_adrs.txt" -#define ALARM_FILE "/webfs/alarms.txt" -const char * snmp_fw_version = "1.10"; - -/** v0.7: OID for enterprise specific ioChange TRAPs */ -const struct snmp_obj_id io_change_id = {9,{1,3,6,1,4,1,SNMP_ENTERPRISE_ID,3,1}}; -/** v1.08: OID for enterprise specific ioAlarm TRAPs */ -const struct snmp_obj_id io_alarm_id = {9,{1,3,6,1,4,1,SNMP_ENTERPRISE_ID,4,1}}; -//const struct snmp_obj_id al_mask_id = {9,{1,3,6,1,4,1,SNMP_ENTERPRISE_ID,5,1}}; -/* v1.05: structures for enterprise specific TRAPs */ -extern struct snmp_varbind io_var; -extern struct snmp_varbind al_var; -extern struct snmp_varbind io_alarm; -/* v1.02: OID for enterprise specific ioChange & ioAlarm TRAPs */ -unsigned char io_chng_idx; // index of changed I/O register -u32_t io_chng_val; // new I/O value if changed -s32_t io_oid[10]; // OID for io values used by ioChange & ioAlarm -s32_t als_oid[10]; // OID for alarm string +const char * snmp_fw_version = "1.a"; /* default ethernet address setup */ unsigned char ip_ad[4] = {192,168,99,99}; @@ -97,20 +62,15 @@ unsigned char dns_ad[4] = {192,168,99,253}; unsigned char th_ad[4] = {146,38,105,49}; // v0.99: host address for SNMP traps unsigned char net_addrs_set = false; // flags net address update from SNMP -unsigned char net_addrs_chng = NO_UPDATE; // flags net address changed in 3000 +unsigned char net_addrs_chng = NO_UPDATE; // flags net address changed in main CPU unsigned char byte_changed = false; // true if any IO regs have changed unsigned char all_IO_updated = false; // true if all 32 IO regs have been read -unsigned char alarm_str_set = false; // flags alarm string update from SNMP char hex_mask[12]; // IO mask as hex string -char reg_change[NUM_MASK]; // register changed associated with io_masks -u32_t new_io_val[NUM_MASK]; // chnaged IO value associated with io_masks -unsigned long io_mask[NUM_MASK]; // masks for alarms on IO registers unsigned long mask4_IO_registers = 0x3fffffff; // mask for IO regs ignoring IO31 & 32 unsigned long spi_count = 0; // last SPI_COUNT saved in SPI interrupt unsigned char spi_active = true; // avoid false trap message at start -char alarm_msg[NUM_MASK][MSG_LNGTH]; // alarm messages associated with io_masks -static u8_t syslocation[MSG_LNGTH]; /* v0.9b */ +static u8_t syslocation[32]; /* v0.9b */ u8_t sysloc_len; SPISlave spi_slave(p11, p12, p13, p14); // mosi, miso, sclk, ssel @@ -120,7 +80,7 @@ Ticker spi_tick; // tick for monitoring SPI activity Timeout cold_start_delay; -DigitalOut led1(LED1, "led1"); // mimics head-head link status o/p in 3000 I/O +DigitalOut led1(LED1, "led1"); // mimics head-head link status o/p in main CPU I/O DigitalOut led2(LED2, "led2"); DigitalOut led3(LED3, "led3"); DigitalOut led4(LED4, "led4"); @@ -144,7 +104,7 @@ /****************************************************************************/ /* SPI interface data and routines */ /****************************************************************************/ -/* commands sent to 3000 cpu from cmd_buf[0] and +/* commands sent to main CPU cpu from cmd_buf[0] and waiting commands stored in higher indices until sent */ unsigned char cmd_buf[ MAX_CMD ]; // store for commands from Ethernet unsigned char new_cmd_idx; @@ -162,8 +122,8 @@ { int idx; for ( idx = 0; idx < MAX_RAM; idx++ ) - { // clear ram_img to minimise traps at start-up - ram_img[ idx ] = 0; + { + ram_img[ idx ] = (MAX_RAM - idx) & 0xff; } reg_offset = 0; } @@ -260,13 +220,13 @@ unsigned short SPI_Server::get_error( void ) { - // return error register value from Field 3000 CPU + // return error register value from main CPU return (256 * ram_img[ 1786 ]) + ram_img[ 1787 ]; } unsigned short SPI_Server::get_SPI_count( void ) { - // return SPI write count from Field 3000 CPU - indicates F3000 running OK + // return SPI write count from main CPU - indicates main CPU running OK return (256 * ram_img[ SPI_COUNT ]) + ram_img[ SPI_COUNT + 1 ]; } @@ -329,30 +289,6 @@ } /****************************************************************************/ -/* chk_alarms: v1.04 -* Description: called when io_value has changed and checks each io_mask -* to see if this mask_idx (IO register - 1) is masked in -* Globals used: reg_change[], io_mask[], new_io_val[] -* Parameters: IN: int mask_idx - 0-based index into mask = (io_reg - 1) -* IN: unsigned short io_value - new I/O value for register -* Returns: NONE -*/ -/****************************************************************************/ -void chk_alarms( int mask_idx, unsigned short io_value ) -{ -unsigned char mask; - - for ( mask = 0; mask < NUM_MASK; mask++ ) - { - if ( (reg_change[ mask ] == 0) && ( io_mask[ mask ] & (1 << mask_idx) ) ) - { // masked-in register has changed - new_io_val[ mask ] = (u32_t)io_value; - reg_change[ mask ] = mask_idx + 1; - } - } -} - -/****************************************************************************/ /* get_SPI_data: * Description: Implements transaction as SPI slave. * Reads 4 data bytes from SPI bus into destbuf for SPI write, or @@ -374,7 +310,6 @@ unsigned char spiv; // SPI value - initial slave address unsigned char bcc = 0; // received block check character unsigned char bcc_ok = false; // default to reject messages without bcc -unsigned short io_val; // first I/O value changed from registers 1-32 spiv = spi_slave.read(); // Read byte from master if ( (spiv & 0xfe) == (LPC1768 & 0xfe) ) @@ -384,7 +319,7 @@ led4 = 1; led3 = 0; if ( *srcbuf < NO_CMD ) - { // new data to be read by 3000 cpu + { // new data to be read by main CPU cpu if ( ++rd_cnt >= NUM_READ ) { // v0.83: clear data after 4 reads spi_slave.reply( NO_CMD ); // put invalid data in SPI buffer @@ -429,18 +364,14 @@ for( byte_cnt = 0; byte_cnt < SPI_BYTES; byte_cnt++ ) { idx = buf_idx + byte_cnt; - if ( (idx <= MAX_IO_REG_BYTE) && (idx & 1) ) // check only ls byte of IO registers - { // check ls byte of IO register changed from last saved - if ( spi_buf[ byte_cnt ] != fsvr.get_byte( idx ) ) - { // ls byte of register changed - io_val = spi_buf[ byte_cnt ] + 256 * spi_buf[ byte_cnt - 1 ]; - if ( !byte_changed && ( mask4_IO_registers & (1 << (idx/2)) ) ) + if ( idx <= MAX_IO_REG_BYTE ) + { // check for IO changed from last saved + if ( (spi_buf[ byte_cnt ] != fsvr.get_byte( idx )) && !byte_changed ) + { + if ( mask4_IO_registers & (1 << (idx/2)) ) { // masked-in register has changed - io_chng_val = io_val; - io_chng_idx = idx/2 + 1; // save index of first I/O register to change byte_changed = true; } - chk_alarms( idx/2, io_val ); } if ( idx == MAX_IO_REG_BYTE ) { // after each I/O scan @@ -449,7 +380,7 @@ } } else if ( idx == NET_ADDRESS_CHANGE ) - { // v0.93: check for net address update from 3000 CPU + { // v0.93: check for net address update from main CPU CPU if ( spi_buf[ byte_cnt ] == NO_UPDATE ) { net_addrs_chng = NO_UPDATE; @@ -497,33 +428,33 @@ /****************************************************************************/ /* get_hex_mask: -* Description: converts long bit_mask to hexadecimal string -* Globals used: hex_mask -* Parameters: IN: unsigned long bit_mask - numeric value of bit mask +* Description: converts long mask4_IO_registers to hexadecimal string +* Globals used: mask4_IO_registers, hex_mask +* Parameters: NONE * Returns: pointer to hex_mask */ /****************************************************************************/ -char * get_hex_mask( unsigned long bit_mask ) +char * get_hex_mask( void ) { unsigned char i; for ( i = 0; i < 12; i++ ) { hex_mask[ i ] = 0; } - sprintf(hex_mask, "%08x", bit_mask); + sprintf(hex_mask, "%08x", mask4_IO_registers); return hex_mask; } /****************************************************************************/ /* convert2decimal: * Description: converts hexadecimal string to long mask4_IO_registers -* Globals used: NONE +* Globals used: mask4_IO_registers * Parameters: u8_t * uc_ptr: pointer to hex-formatted mask string * u16_t len: length of string -* Returns: unsigned long temp_sum +* Returns: NONE */ /****************************************************************************/ -unsigned long convert2decimal( u8_t * uc_ptr, u16_t len ) +void convert2decimal( u8_t * uc_ptr, u16_t len ) { char ch, i; unsigned long temp_sum = 0; @@ -551,7 +482,7 @@ } temp_sum = 16 * temp_sum + ch; } - return temp_sum; + mask4_IO_registers = temp_sum; } void send_ram( void ) @@ -776,7 +707,7 @@ fscanf( fr, "%lu", &mask4_IO_registers ); fclose(fr); } // file opened OK - if ( strlen(community) >= MIN_COMMUNITY ) + if ( strlen(community) > 3 ) { // string length 4 to 20 characters strcpy( snmp_publiccommunity, community ); pc.printf("community: %s %d \r\n", snmp_publiccommunity, strlen(snmp_publiccommunity) ); @@ -789,49 +720,6 @@ } /****************************************************************************/ -/* read_alarm_from_file: -* Description: Reads one alarm_msg from /webfs/alarms.txt if alarm_idx < NUM_MASK -* or can read all io_masks as well if alarm_idx >= NUM_MASK -* Globals used: ALARM_FILE, alarm_msg, io_mask -* Parameters: IN: unsigned char alarm_idx - index to alarm message -* Returns: 0 if OK, 1 if failed to open files -*/ -/****************************************************************************/ -int read_alarm_from_file(unsigned char alarm_idx) -{ -unsigned char i; - - FILE *fr = fopen( ALARM_FILE, "r" ); - if(!fr) { - fprintf(stderr, "\r\nFile /webfs/alarm.txt could not be opened!\r\n"); - exit(1); - } - else // file opened OK - { - if ( alarm_idx < NUM_MASK ) - { - for ( i = 0; i <= alarm_idx; i++ ) - { // read specified alarm_msg - fscanf( fr, "%s", &alarm_msg[ i ] ); - } - } - else // read io_masks - { - for ( i = 0; i < NUM_MASK; i++ ) - { // read all alarm_msg - fscanf( fr, "%s", &alarm_msg[ i ] ); - } - for ( i = 0; i < NUM_MASK; i++ ) - { // read all io_masks - fscanf( fr, "%lx", &io_mask[ i ] ); - } - } - fclose(fr); - } // file opened OK - return 0; -} - -/****************************************************************************/ /* blinker: * Description: Blinks led_link for 10 calls * Globals used: led_link, blink_led, @@ -855,92 +743,23 @@ } /****************************************************************************/ -/* write_alarms_to_file: v1.04 -* Description: Writes alarm messages and masks to /webfs/alarms.txt -* Globals used: ALARM_FILE, alarm_msg[], io_mask[] -* Parameters: NONE -* Returns: 0 if OK, 1 if failed to open files -*/ -/****************************************************************************/ -int write_alarms_to_file( void ) -{ -u8_t i = 1; // return value if failed to write file - - FILE *fp = fopen( ALARM_FILE, "w" ); - if(!fp) { - fprintf(stderr, "\r\nFile /webfs/alarms.txt could not be opened to write!\r\n"); - } - else // file opened OK, so save addresses - { - for ( i = 0; i < NUM_MASK; i++ ) - { - fprintf(fp, "%s\r\n", alarm_msg[ i ] ); - } - for ( i = 0; i < NUM_MASK; i++ ) - { - fprintf(fp, "%08x\r\n", io_mask[ i ] ); - } - fclose(fp); - i = 0; - } // file opened OK - return i; -} - -/****************************************************************************/ /* init_traps: * Description: initialises trap flags and sends cold start trap when done -* Globals used: led1: mimics state of head-link status output in 3000 I/O -* trap_init set true after cold start +* Globals used: trap_init set true after cold start * Parameters: NONE * Returns: NONE */ /****************************************************************************/ void init_traps( void ) { -u8_t i = 0; - snmp_coldstart_trap(); - // force sending trap for head-head link up/down status - led1 = fsvr.get_byte( H_H_LINK_LSBYTE ) ? 0 : 1; - /* initialise variable binding for f3kIOchange_trap */ - io_var.next = io_var.prev = NULL; - io_var.ident_len = io_change_id.len; - io_var.ident = io_oid; - do - { - io_oid[ i ] = io_change_id.id[ i ]; - } while ( i++ < io_change_id.len ); - io_var.value_type = (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE); - io_var.value_len = sizeof(u32_t); - io_var.value = &io_chng_val; - /* initialise variable binding for f3kIOalarm_trap al_var data */ - al_var.prev = NULL; // first in list - al_var.next = &io_alarm; // NULL if only varbind in list - al_var.ident_len = io_change_id.len; - al_var.ident = io_oid; - al_var.value_type = (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE); - al_var.value_len = sizeof(u32_t); - al_var.value = &new_io_val[ 0 ]; - /* initialise variable binding for f3kIOalarm_trap string */ - io_alarm.prev = &al_var; // after al_var binding - io_alarm.next = NULL; // always last in list ... al_string_id - io_alarm.ident_len = io_alarm_id.len; - io_alarm.ident = als_oid; - for ( i = 0; i < io_alarm_id.len; i++ ) - { - als_oid[ i ] = io_alarm_id.id[ i ]; - } - io_alarm.value_type = (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR); - io_alarm.value_len = MSG_LNGTH; - io_alarm.value = &alarm_msg[ 0 ]; trap_init = true; } /****************************************************************************/ /* check_all_traps: * Description: test states for all trap events and sends appropriate trap -* Globals used: led1: mimics state of head-link status output in 3000 I/O -* led2: set to ON if SPI not active +* Globals used: led2: set to ON if SPI not active * byte_changed, all_IO_updated, spi_active * Parameters: NONE * Returns: NONE @@ -948,8 +767,6 @@ /****************************************************************************/ void check_all_traps( void ) { -unsigned char alm; - if ( !led1 && (fsvr.get_byte( H_H_LINK_LSBYTE ) || fsvr.get_byte( H_H_LINK_MSBYTE )) ) { // change of link state led1 = 1; // flag: link is up when led1 on @@ -960,10 +777,8 @@ led1 = 0; snmp_linkdown_trap(); } - if ( all_IO_updated && byte_changed ) + if ( byte_changed && all_IO_updated ) { // any masked-in I/O register changed - io_oid[ IO_INDEX ] = io_chng_idx; - io_var.value = &io_chng_val; f3kIOchange_trap(); all_IO_updated = byte_changed = false; } @@ -976,21 +791,7 @@ { led2 = 1; // flag: SPI is down when led2 on snmp_SPIdown_trap(); - } - /* send trap for any changed I/O alarm */ - for ( alm = 0; alm < NUM_MASK; alm++ ) - { - if ( reg_change[ alm ] ) - { // any alarmed I/O register changed - io_oid[ IO_INDEX ] = reg_change[ alm ]; - al_var.value = &new_io_val[ alm ]; - als_oid[ IO_INDEX ] = alm + 1; - io_alarm.value = &alarm_msg[ alm ]; - io_alarm.value_len = strlen( (const char * )alarm_msg[ alm ] ); - f3kIOalarm_trap(); - reg_change[ alm ] = 0; - } - } + } } @@ -1004,20 +805,13 @@ { // put meaningful non-valid data in cmd_buf cmd_buf[ idx ] = new_cmd_idx; } - /* v1.04: clear all alarm trap masks */ - for ( idx = 0; idx < NUM_MASK; idx++ ) - { - reg_change[ idx ] = 0; // clear all I/O register changed flags - io_mask[ idx ] = 0; // clear masks for alarms on IO registers - } pc.printf("=================== Version %s ====================\r\n", snmp_fw_version); // pc.printf("Press any key to start...\r\n"); led3 = 1; - FSHandler::mount("/webfs", "/files"); // Mount /webfs path on /files web path - FSHandler::mount("/webfs", "/"); // Mount /webfs path on web root path + FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path + FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path read_addrs_from_file(); - read_alarm_from_file( NUM_MASK ); // read all I/O masks /* Force fixed Ethernet address */ EthernetNetIf eth( @@ -1073,7 +867,7 @@ spi_slave.format(8,3); // Setup: byte data, high steady state clock, 2nd edge capture sys_tick.attach( &snmp_inc_sysuptime, 0.01 ); - spi_tick.attach( &chk_SPI_active, 120.0 ); + spi_tick.attach( &chk_SPI_active, 60.0 ); comms_tick.attach( &blinker, 0.07 ); // tick to blink activity LED cold_start_delay.attach( &init_traps, 30.0 ); // send cold_start trap after 30 seconds @@ -1092,22 +886,11 @@ // Data available - needs chip select line to activate get_SPI_data( cmd_buf ); } // end if( spi_slave.receive() ) - if ( (net_addrs_chng == 2) || (net_addrs_chng == 1) ) - { // v0.93: address changed in 3000 CPU - copy_net_addresses_from( fsvr.net_address_ptr() ); - copy_trap_host_address( fsvr.trap_host_ptr() ); - write_addrs_to_file(); - } if ( net_addrs_set ) { // v0.99: address changed from SNMP i/f write_addrs_to_file(); net_addrs_set = false; } - if ( alarm_str_set ) - { - write_alarms_to_file(); - alarm_str_set = false; - } } // end while ( true ) } // else Ethernet Setup OK