SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
Child:
2:25e12a7fe0aa
v1.1 release includes ioAlarm traps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lorcansmith 0:2a53a4c3238c 1
lorcansmith 0:2a53a4c3238c 2 /*
lorcansmith 0:2a53a4c3238c 3 Generated: Enet_SPI_LPC1768.bin
lorcansmith 0:2a53a4c3238c 4
lorcansmith 0:2a53a4c3238c 5 mbed test software for Ethernet HTTPServer with simultaneous SPI for 4 data byte updates
lorcansmith 0:2a53a4c3238c 6
lorcansmith 0:2a53a4c3238c 7 Send serial message via USB every 1.0 second tick
lorcansmith 0:2a53a4c3238c 8 Version Date Author Change
lorcansmith 0:2a53a4c3238c 9 0.2: 06/10/11 LS Add block checksum character to SPI data
lorcansmith 0:2a53a4c3238c 10 0.3: 07/10/11 LS Add SPI read from cmd_buf
lorcansmith 0:2a53a4c3238c 11 0.4: 13/10/11 LS Integrated with HTTP server example code
lorcansmith 0:2a53a4c3238c 12 0.5: 18/10/11 LS Initial file handling
lorcansmith 0:2a53a4c3238c 13 0.6: 26/10/11 LS File IO handled by SPI_server class
lorcansmith 0:2a53a4c3238c 14 0.64: 04/11/11 LS RPC bodge added as part of HTTP file handling:
lorcansmith 0:2a53a4c3238c 15 0.66: 04/11/11 LS Show register values as hex bytes as well as word value
lorcansmith 0:2a53a4c3238c 16 0.78: 17/11/11 LS Add SNMP files for system info reporting, but HTTP svr.bind disabled
lorcansmith 0:2a53a4c3238c 17 0.79 30/11/11 LS Add f3kTable as ID 7 in F3000 private MIB
lorcansmith 0:2a53a4c3238c 18 0.79e 12/12/11 LS Reads network addresses from file & uses Field enterprise address
lorcansmith 0:2a53a4c3238c 19 0.80 18/12/11 LS Save last number read in read_addrs_from_file().
lorcansmith 0:2a53a4c3238c 20 Add write_addrs_to_file() to testaddrs.txt
lorcansmith 0:2a53a4c3238c 21 0.81 22/12/11 LS Add new_cmd_idx to indicate which parameter has been set (0 for none).
lorcansmith 0:2a53a4c3238c 22 0.82 22/12/11 LS Add blinker to show comms activity
lorcansmith 0:2a53a4c3238c 23 0.83 23/12/11 LS Clear cmd_buf[0] after SPI been read 4 times
lorcansmith 0:2a53a4c3238c 24 0.84 27/12/11 LS snmp_fw_version & f3k_fw_version saved as 4 char strings
lorcansmith 0:2a53a4c3238c 25 0.85 29/12/11 LS Update f3k_sys_set_value() & f3k_sys_set_test() to enable net address change
lorcansmith 0:2a53a4c3238c 26 0.90 29/12/11 LS Add 3-step routine for passing commands over SPI bus from cmd_buf
lorcansmith 0:2a53a4c3238c 27 0.92 06/01/12 LS Change 3000 index for 3000 firmware version
lorcansmith 0:2a53a4c3238c 28 0.93 09/01/12 LS Add copy_net_addresses_from() for address updates from 3000 CPU
lorcansmith 0:2a53a4c3238c 29 0.94 10/01/12 LS Add led_link = LPC_GPIO1->FIOPIN & (1<<25) in netservice.cpp
lorcansmith 0:2a53a4c3238c 30 0.95 11/01/12 LS Fix f3k_speed reporting error in private_mib.c
lorcansmith 0:2a53a4c3238c 31 0.96 12/01/12 LS Move led_link = LPC_GPIO1->FIOPIN & (1<<25) to eth_poll in eth_drv.cpp
lorcansmith 0:2a53a4c3238c 32 0.97 30/01/12 LS Add get_SPI_count() & SPI count to MIB as 3000 CPU activity indicator
lorcansmith 0:2a53a4c3238c 33 0.98 26/04/12 LS Increase snmp_publiccommunity[7] to [21] and allow this to be set from file
lorcansmith 0:2a53a4c3238c 34 0.99 08/06/12 LS #define SNMP_TRAP_DESTINATIONS 1. Declare th_ad[4] for host trap address
lorcansmith 0:2a53a4c3238c 35 Add init_traps() to ensure head-head link status trap is sent
lorcansmith 0:2a53a4c3238c 36 21/06/12 Add check for changes to IO bytes and chk_SPI_active() every 60 seconds
lorcansmith 0:2a53a4c3238c 37 1.00 22/06/12 LS Save SNMP changes to net addresses & mask4_IO_registers in net_adrs.txt
lorcansmith 0:2a53a4c3238c 38 1.01 25/07/12 LS Allow snmp_publiccommunity to be modified via SNMP
lorcansmith 0:2a53a4c3238c 39 1.02 26/07/12 LS Add changed variable in snmp_varbind io_var to ioChange trap
lorcansmith 0:2a53a4c3238c 40 1.03 01/08/12 LS Only monitor ls byte of I/O registers for changes to mask out Monostable O/Ps
lorcansmith 0:2a53a4c3238c 41 1.04 03/08/12 LS Add chk_alarms() for monitoring up to NUM_MASK I/O alarms
lorcansmith 0:2a53a4c3238c 42 1.05 03/08/12 LS Add struct snmp_varbind io_alarm to pass alarm_msg to f3kIOalarm_trap()
lorcansmith 0:2a53a4c3238c 43 1.06 07/08/12 LS Reduce MSG_LNGTH to 16 from 32. Free up more RAM for alarm strings
lorcansmith 0:2a53a4c3238c 44 1.07 08/08/12 LS Add SNMP R/W of alarm strings
lorcansmith 0:2a53a4c3238c 45 1.08 09/08/12 LS Create array of NUM_MASK alarm strings
lorcansmith 0:2a53a4c3238c 46 1.09 09/08/12 LS Correct problems reading alarm strings via SNMP OK with 32 char strings
lorcansmith 0:2a53a4c3238c 47 1.10 09/08/12 LS Tested OK with MSG_LNGTH 16 char strings
lorcansmith 0:2a53a4c3238c 48
lorcansmith 0:2a53a4c3238c 49 http://192.168.99.99/rpc/offset,604 writes 604 to reg_offset
lorcansmith 0:2a53a4c3238c 50 http://192.168.99.99/rpc/led2/write,1 writes 1 to led2
lorcansmith 0:2a53a4c3238c 51 http://192.168.99.99/files/IO1000PX.htm uploads file IO1000PX.htm
lorcansmith 0:2a53a4c3238c 52 */
lorcansmith 0:2a53a4c3238c 53
lorcansmith 0:2a53a4c3238c 54 #include "mbed.h"
lorcansmith 0:2a53a4c3238c 55 #include "EthernetNetIf.h"
lorcansmith 0:2a53a4c3238c 56 #include "HTTPServer.h"
lorcansmith 0:2a53a4c3238c 57 #include "snmp_structs.h"
lorcansmith 0:2a53a4c3238c 58 #include "snmp_msg.h"
lorcansmith 0:2a53a4c3238c 59 #include "snmp_asn1.h"
lorcansmith 0:2a53a4c3238c 60 #include "IO_file.h"
lorcansmith 0:2a53a4c3238c 61
lorcansmith 0:2a53a4c3238c 62 #define LPC1768 0x60 // SPI address of LPC1768 module
lorcansmith 0:2a53a4c3238c 63 #define READ_IO 0x01 // bit in address field for data reads
lorcansmith 0:2a53a4c3238c 64 #define SPI_BYTES 4 // # of data bytes in SPI message
lorcansmith 0:2a53a4c3238c 65 #define INV_SPI_CNT 0xfffff // invalid (over-range SPI count
lorcansmith 0:2a53a4c3238c 66 #define NUM_READ 8 // # of SPI replies for valid reads
lorcansmith 0:2a53a4c3238c 67 #define NUM_INDX 4 // # of SPI replies for valid cmd_buf index
lorcansmith 0:2a53a4c3238c 68 #define MAX_CMD 4 // size of CMD buffer
lorcansmith 0:2a53a4c3238c 69 #define NO_CMD 0xff // data on cmd_buf[0] when NO_CMD to be sent
lorcansmith 0:2a53a4c3238c 70 #define NO_UPDATE 0 // NO change to Ethernet addresses
lorcansmith 0:2a53a4c3238c 71 #define UPDATED 0xff // Ethernet addresses updated in file
lorcansmith 0:2a53a4c3238c 72 #define NET_ADRS_FILE "/webfs/net_adrs.txt"
lorcansmith 0:2a53a4c3238c 73 #define ALARM_FILE "/webfs/alarms.txt"
lorcansmith 0:2a53a4c3238c 74
lorcansmith 0:2a53a4c3238c 75
lorcansmith 0:2a53a4c3238c 76 const char * snmp_fw_version = "1.10";
lorcansmith 0:2a53a4c3238c 77
lorcansmith 0:2a53a4c3238c 78 /** v0.7: OID for enterprise specific ioChange TRAPs */
lorcansmith 0:2a53a4c3238c 79 const struct snmp_obj_id io_change_id = {9,{1,3,6,1,4,1,SNMP_ENTERPRISE_ID,3,1}};
lorcansmith 0:2a53a4c3238c 80 /** v1.08: OID for enterprise specific ioAlarm TRAPs */
lorcansmith 0:2a53a4c3238c 81 const struct snmp_obj_id io_alarm_id = {9,{1,3,6,1,4,1,SNMP_ENTERPRISE_ID,4,1}};
lorcansmith 0:2a53a4c3238c 82 //const struct snmp_obj_id al_mask_id = {9,{1,3,6,1,4,1,SNMP_ENTERPRISE_ID,5,1}};
lorcansmith 0:2a53a4c3238c 83 /* v1.05: structures for enterprise specific TRAPs */
lorcansmith 0:2a53a4c3238c 84 extern struct snmp_varbind io_var;
lorcansmith 0:2a53a4c3238c 85 extern struct snmp_varbind al_var;
lorcansmith 0:2a53a4c3238c 86 extern struct snmp_varbind io_alarm;
lorcansmith 0:2a53a4c3238c 87 /* v1.02: OID for enterprise specific ioChange & ioAlarm TRAPs */
lorcansmith 0:2a53a4c3238c 88 unsigned char io_chng_idx; // index of changed I/O register
lorcansmith 0:2a53a4c3238c 89 u32_t io_chng_val; // new I/O value if changed
lorcansmith 0:2a53a4c3238c 90 s32_t io_oid[10]; // OID for io values used by ioChange & ioAlarm
lorcansmith 0:2a53a4c3238c 91 s32_t als_oid[10]; // OID for alarm string
lorcansmith 0:2a53a4c3238c 92
lorcansmith 0:2a53a4c3238c 93 /* default ethernet address setup */
lorcansmith 0:2a53a4c3238c 94 unsigned char ip_ad[4] = {192,168,99,99};
lorcansmith 0:2a53a4c3238c 95 unsigned char msk_ad[4] = {255,255,255,0};
lorcansmith 0:2a53a4c3238c 96 unsigned char gwy_ad[4] = {192,168,99,253};
lorcansmith 0:2a53a4c3238c 97 unsigned char dns_ad[4] = {192,168,99,253};
lorcansmith 0:2a53a4c3238c 98 unsigned char th_ad[4] = {146,38,105,49}; // v0.99: host address for SNMP traps
lorcansmith 0:2a53a4c3238c 99 unsigned char net_addrs_set = false; // flags net address update from SNMP
lorcansmith 0:2a53a4c3238c 100 unsigned char net_addrs_chng = NO_UPDATE; // flags net address changed in 3000
lorcansmith 0:2a53a4c3238c 101 unsigned char byte_changed = false; // true if any IO regs have changed
lorcansmith 0:2a53a4c3238c 102 unsigned char all_IO_updated = false; // true if all 32 IO regs have been read
lorcansmith 0:2a53a4c3238c 103 unsigned char alarm_str_set = false; // flags alarm string update from SNMP
lorcansmith 0:2a53a4c3238c 104 char hex_mask[12]; // IO mask as hex string
lorcansmith 0:2a53a4c3238c 105 char reg_change[NUM_MASK]; // register changed associated with io_masks
lorcansmith 0:2a53a4c3238c 106 u32_t new_io_val[NUM_MASK]; // chnaged IO value associated with io_masks
lorcansmith 0:2a53a4c3238c 107 unsigned long io_mask[NUM_MASK]; // masks for alarms on IO registers
lorcansmith 0:2a53a4c3238c 108 unsigned long mask4_IO_registers = 0x3fffffff; // mask for IO regs ignoring IO31 & 32
lorcansmith 0:2a53a4c3238c 109 unsigned long spi_count = 0; // last SPI_COUNT saved in SPI interrupt
lorcansmith 0:2a53a4c3238c 110 unsigned char spi_active = true; // avoid false trap message at start
lorcansmith 0:2a53a4c3238c 111
lorcansmith 0:2a53a4c3238c 112 char alarm_msg[NUM_MASK][MSG_LNGTH]; // alarm messages associated with io_masks
lorcansmith 0:2a53a4c3238c 113 static u8_t syslocation[MSG_LNGTH]; /* v0.9b */
lorcansmith 0:2a53a4c3238c 114 u8_t sysloc_len;
lorcansmith 0:2a53a4c3238c 115
lorcansmith 0:2a53a4c3238c 116 SPISlave spi_slave(p11, p12, p13, p14); // mosi, miso, sclk, ssel
lorcansmith 0:2a53a4c3238c 117
lorcansmith 0:2a53a4c3238c 118 Ticker comms_tick;
lorcansmith 0:2a53a4c3238c 119 Ticker sys_tick; // system tick for SNMP sysuptime report
lorcansmith 0:2a53a4c3238c 120 Ticker spi_tick; // tick for monitoring SPI activity
lorcansmith 0:2a53a4c3238c 121 Timeout cold_start_delay;
lorcansmith 0:2a53a4c3238c 122
lorcansmith 0:2a53a4c3238c 123 DigitalOut led1(LED1, "led1"); // mimics head-head link status o/p in 3000 I/O
lorcansmith 0:2a53a4c3238c 124 DigitalOut led2(LED2, "led2");
lorcansmith 0:2a53a4c3238c 125 DigitalOut led3(LED3, "led3");
lorcansmith 0:2a53a4c3238c 126 DigitalOut led4(LED4, "led4");
lorcansmith 0:2a53a4c3238c 127 DigitalOut led_link(p29); // Green link status: ON=good, Blink=Activity
lorcansmith 0:2a53a4c3238c 128 DigitalOut led_speed(p30); // Yellow link speed: ON for 100Mb/s
lorcansmith 0:2a53a4c3238c 129
lorcansmith 0:2a53a4c3238c 130 unsigned char blink_led = false;
lorcansmith 0:2a53a4c3238c 131 unsigned char trap_init = false; // true after cold_start trap sent
lorcansmith 0:2a53a4c3238c 132
lorcansmith 0:2a53a4c3238c 133 LocalFileSystem fs( "webfs" );
lorcansmith 0:2a53a4c3238c 134
lorcansmith 0:2a53a4c3238c 135 IpAddr mip, mnm, mgw, mdns; // instantiate from core\ipaddr.cpp
lorcansmith 0:2a53a4c3238c 136
lorcansmith 0:2a53a4c3238c 137 //EthernetNetIf eth; // use if DNS sets up address
lorcansmith 0:2a53a4c3238c 138
lorcansmith 0:2a53a4c3238c 139 HTTPServer svr;
lorcansmith 0:2a53a4c3238c 140 SPI_Server fsvr( "fsvr" );
lorcansmith 0:2a53a4c3238c 141
lorcansmith 0:2a53a4c3238c 142 Serial pc( USBTX, USBRX ); // for diagnostic serial comms to PC via USB
lorcansmith 0:2a53a4c3238c 143
lorcansmith 0:2a53a4c3238c 144 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 145 /* SPI interface data and routines */
lorcansmith 0:2a53a4c3238c 146 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 147 /* commands sent to 3000 cpu from cmd_buf[0] and
lorcansmith 0:2a53a4c3238c 148 waiting commands stored in higher indices until sent */
lorcansmith 0:2a53a4c3238c 149 unsigned char cmd_buf[ MAX_CMD ]; // store for commands from Ethernet
lorcansmith 0:2a53a4c3238c 150 unsigned char new_cmd_idx;
lorcansmith 0:2a53a4c3238c 151
lorcansmith 0:2a53a4c3238c 152
lorcansmith 0:2a53a4c3238c 153 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 154 /* SPI_Server:
lorcansmith 0:2a53a4c3238c 155 * Description: Instantiates Class to hold & access all data read from RAM by SPI
lorcansmith 0:2a53a4c3238c 156 * Called by FSHandler::doGet() in services\http\server\impl
lorcansmith 0:2a53a4c3238c 157 * Parameters: name of object instantiated
lorcansmith 0:2a53a4c3238c 158 * Returns: NONE
lorcansmith 0:2a53a4c3238c 159 */
lorcansmith 0:2a53a4c3238c 160 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 161 SPI_Server::SPI_Server(const char * name)
lorcansmith 0:2a53a4c3238c 162 {
lorcansmith 0:2a53a4c3238c 163 int idx;
lorcansmith 0:2a53a4c3238c 164 for ( idx = 0; idx < MAX_RAM; idx++ )
lorcansmith 0:2a53a4c3238c 165 { // clear ram_img to minimise traps at start-up
lorcansmith 0:2a53a4c3238c 166 ram_img[ idx ] = 0;
lorcansmith 0:2a53a4c3238c 167 }
lorcansmith 0:2a53a4c3238c 168 reg_offset = 0;
lorcansmith 0:2a53a4c3238c 169 }
lorcansmith 0:2a53a4c3238c 170
lorcansmith 0:2a53a4c3238c 171 SPI_Server::~SPI_Server()
lorcansmith 0:2a53a4c3238c 172 {
lorcansmith 0:2a53a4c3238c 173 if(fp) fclose(fp);
lorcansmith 0:2a53a4c3238c 174 }
lorcansmith 0:2a53a4c3238c 175
lorcansmith 0:2a53a4c3238c 176 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 177 /* update_IO_file:
lorcansmith 0:2a53a4c3238c 178 * Description: Reads data from ram_img[] into IO1000PX.htm for web access
lorcansmith 0:2a53a4c3238c 179 * Called by FSHandler::doGet() in services\http\server\impl
lorcansmith 0:2a53a4c3238c 180 * Globals used: NONE
lorcansmith 0:2a53a4c3238c 181 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 182 * Returns: 0 if OK, 1 if failed to open files
lorcansmith 0:2a53a4c3238c 183 */
lorcansmith 0:2a53a4c3238c 184 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 185 int SPI_Server::update_IO_file()
lorcansmith 0:2a53a4c3238c 186 {
lorcansmith 0:2a53a4c3238c 187 int c, i; // characters read/written in files
lorcansmith 0:2a53a4c3238c 188 unsigned short chnl, dval; // data from 1000PX
lorcansmith 0:2a53a4c3238c 189
lorcansmith 0:2a53a4c3238c 190 DBG("\r\nIn SPI_Server::update_IO_file()\r\n");
lorcansmith 0:2a53a4c3238c 191 FILE *fr = fopen("/webfs/hdr.htm", "r");
lorcansmith 0:2a53a4c3238c 192 if(!fr) {
lorcansmith 0:2a53a4c3238c 193 fprintf(stderr, "\r\nFile /webfs/hdr.htm could not be opened!\r\n");
lorcansmith 0:2a53a4c3238c 194 exit(1);
lorcansmith 0:2a53a4c3238c 195 }
lorcansmith 0:2a53a4c3238c 196 else
lorcansmith 0:2a53a4c3238c 197 {
lorcansmith 0:2a53a4c3238c 198 fp = fopen( "/webfs/IO1000PX.htm", "w" );
lorcansmith 0:2a53a4c3238c 199 if(!fp) {
lorcansmith 0:2a53a4c3238c 200 fprintf(stderr, "\r\nFile /webfs/IO1000PX.htm could not be opened to write!\r\n" );
lorcansmith 0:2a53a4c3238c 201 exit(1);
lorcansmith 0:2a53a4c3238c 202 }
lorcansmith 0:2a53a4c3238c 203 else
lorcansmith 0:2a53a4c3238c 204 {
lorcansmith 0:2a53a4c3238c 205 while ( (c = fgetc(fr)) != EOF )
lorcansmith 0:2a53a4c3238c 206 {
lorcansmith 0:2a53a4c3238c 207 fputc( c, fp );
lorcansmith 0:2a53a4c3238c 208 }
lorcansmith 0:2a53a4c3238c 209 fclose(fr);
lorcansmith 0:2a53a4c3238c 210 DBG("\r\nIn SPI_Server::update_IO_file() - header copied\r\n");
lorcansmith 0:2a53a4c3238c 211 for ( i = 0; i < 16; i++ )
lorcansmith 0:2a53a4c3238c 212 {
lorcansmith 0:2a53a4c3238c 213 chnl = reg_offset + i;
lorcansmith 0:2a53a4c3238c 214 dval = 256 * ram_img[ 2*chnl ] + ram_img[ 2*chnl + 1 ];
lorcansmith 0:2a53a4c3238c 215 fprintf(fp, " <TR VALIGN=TOP>\r\n");
lorcansmith 0:2a53a4c3238c 216 fprintf(fp, " <TD WIDTH=124>\r\n");
lorcansmith 0:2a53a4c3238c 217 fprintf(fp, " <P>%02d</P>\r\n", chnl );
lorcansmith 0:2a53a4c3238c 218 fprintf(fp, " </TD>\r\n");
lorcansmith 0:2a53a4c3238c 219 fprintf(fp, " <TD WIDTH=124>\r\n");
lorcansmith 0:2a53a4c3238c 220 fprintf(fp, " <P>%d</P>\r\n", dval );
lorcansmith 0:2a53a4c3238c 221 fprintf(fp, " </TD>\r\n");
lorcansmith 0:2a53a4c3238c 222 fprintf(fp, " <TD WIDTH=124>\r\n");
lorcansmith 0:2a53a4c3238c 223 fprintf(fp, " <P>%02x</P>\r\n", (dval/256) );
lorcansmith 0:2a53a4c3238c 224 fprintf(fp, " </TD>\r\n");
lorcansmith 0:2a53a4c3238c 225 fprintf(fp, " <TD WIDTH=124>\r\n");
lorcansmith 0:2a53a4c3238c 226 fprintf(fp, " <P>%02x</P>\r\n", (dval & 0xff) );
lorcansmith 0:2a53a4c3238c 227 fprintf(fp, " </TD>\r\n");
lorcansmith 0:2a53a4c3238c 228 fprintf(fp, " </TR>\r\n");
lorcansmith 0:2a53a4c3238c 229 }
lorcansmith 0:2a53a4c3238c 230 fprintf(fp, "</TABLE>\r\n<P><BR><BR>\r\n</P>\r\n</BODY>\r\n</HTML>\r\n");
lorcansmith 0:2a53a4c3238c 231 }
lorcansmith 0:2a53a4c3238c 232 }
lorcansmith 0:2a53a4c3238c 233 fclose(fp);
lorcansmith 0:2a53a4c3238c 234 // fclose(fr);
lorcansmith 0:2a53a4c3238c 235 return 0;
lorcansmith 0:2a53a4c3238c 236 }
lorcansmith 0:2a53a4c3238c 237
lorcansmith 0:2a53a4c3238c 238 int SPI_Server::read( void )
lorcansmith 0:2a53a4c3238c 239 {
lorcansmith 0:2a53a4c3238c 240 DBG("\r\nIn SPI_Server::read()\r\n");
lorcansmith 0:2a53a4c3238c 241 return reg_offset;
lorcansmith 0:2a53a4c3238c 242 }
lorcansmith 0:2a53a4c3238c 243
lorcansmith 0:2a53a4c3238c 244 void SPI_Server::write( int new_offset )
lorcansmith 0:2a53a4c3238c 245 {
lorcansmith 0:2a53a4c3238c 246 DBG("\r\nIn SPI_Server::write()\r\n");
lorcansmith 0:2a53a4c3238c 247 if ( (new_offset >= 0) && (new_offset < MAX_RAM) ) reg_offset = new_offset;
lorcansmith 0:2a53a4c3238c 248 }
lorcansmith 0:2a53a4c3238c 249
lorcansmith 0:2a53a4c3238c 250 unsigned char SPI_Server::get_byte( unsigned short idx )
lorcansmith 0:2a53a4c3238c 251 {
lorcansmith 0:2a53a4c3238c 252 return ram_img[ idx ];
lorcansmith 0:2a53a4c3238c 253 }
lorcansmith 0:2a53a4c3238c 254
lorcansmith 0:2a53a4c3238c 255 void SPI_Server::put_byte( unsigned short idx, unsigned char new_byte )
lorcansmith 0:2a53a4c3238c 256 {
lorcansmith 0:2a53a4c3238c 257 if ( idx < MAX_RAM )
lorcansmith 0:2a53a4c3238c 258 ram_img[ idx ] = new_byte;
lorcansmith 0:2a53a4c3238c 259 }
lorcansmith 0:2a53a4c3238c 260
lorcansmith 0:2a53a4c3238c 261 unsigned short SPI_Server::get_error( void )
lorcansmith 0:2a53a4c3238c 262 {
lorcansmith 0:2a53a4c3238c 263 // return error register value from Field 3000 CPU
lorcansmith 0:2a53a4c3238c 264 return (256 * ram_img[ 1786 ]) + ram_img[ 1787 ];
lorcansmith 0:2a53a4c3238c 265 }
lorcansmith 0:2a53a4c3238c 266
lorcansmith 0:2a53a4c3238c 267 unsigned short SPI_Server::get_SPI_count( void )
lorcansmith 0:2a53a4c3238c 268 {
lorcansmith 0:2a53a4c3238c 269 // return SPI write count from Field 3000 CPU - indicates F3000 running OK
lorcansmith 0:2a53a4c3238c 270 return (256 * ram_img[ SPI_COUNT ]) + ram_img[ SPI_COUNT + 1 ];
lorcansmith 0:2a53a4c3238c 271 }
lorcansmith 0:2a53a4c3238c 272
lorcansmith 0:2a53a4c3238c 273 unsigned char * SPI_Server::net_address_ptr( void )
lorcansmith 0:2a53a4c3238c 274 {
lorcansmith 0:2a53a4c3238c 275 return &ram_img[ NET_IP_ADDRESS ];
lorcansmith 0:2a53a4c3238c 276 }
lorcansmith 0:2a53a4c3238c 277
lorcansmith 0:2a53a4c3238c 278 unsigned char * SPI_Server::trap_host_ptr( void )
lorcansmith 0:2a53a4c3238c 279 {
lorcansmith 0:2a53a4c3238c 280 return &ram_img[ TRAP_HOST_ADDRESS ];
lorcansmith 0:2a53a4c3238c 281 }
lorcansmith 0:2a53a4c3238c 282
lorcansmith 0:2a53a4c3238c 283 #if 0
lorcansmith 0:2a53a4c3238c 284 //#ifdef MBED_RPC trap_host_ptr
lorcansmith 0:2a53a4c3238c 285 const struct rpc_method *SPI_Server::get_rpc_methods() {
lorcansmith 0:2a53a4c3238c 286 static const rpc_method methods[] = {
lorcansmith 0:2a53a4c3238c 287 { "read", rpc_method_caller<int, SPI_Server, &SPI_Server::read> },
lorcansmith 0:2a53a4c3238c 288 { "write", rpc_method_caller<SPI_Server, int, &SPI_Server::write> },
lorcansmith 0:2a53a4c3238c 289 RPC_METHOD_SUPER(Base) //Stream)
lorcansmith 0:2a53a4c3238c 290 };
lorcansmith 0:2a53a4c3238c 291 return methods;
lorcansmith 0:2a53a4c3238c 292 }
lorcansmith 0:2a53a4c3238c 293
lorcansmith 0:2a53a4c3238c 294 struct rpc_class *SPI_Server::get_rpc_class() {
lorcansmith 0:2a53a4c3238c 295 static const rpc_function funcs[] = {
lorcansmith 0:2a53a4c3238c 296 { "new", rpc_function_caller<const char*, int, &Base::construct<SPI_Server, int> > },
lorcansmith 0:2a53a4c3238c 297 RPC_METHOD_END
lorcansmith 0:2a53a4c3238c 298 };
lorcansmith 0:2a53a4c3238c 299 static rpc_class c = { "SPI_Server", funcs, NULL };
lorcansmith 0:2a53a4c3238c 300 return &c;
lorcansmith 0:2a53a4c3238c 301 }
lorcansmith 0:2a53a4c3238c 302 #endif
lorcansmith 0:2a53a4c3238c 303
lorcansmith 0:2a53a4c3238c 304 void check4write_offset( char * req )
lorcansmith 0:2a53a4c3238c 305 {
lorcansmith 0:2a53a4c3238c 306 unsigned char l, c, i;
lorcansmith 0:2a53a4c3238c 307 char v[ 20 ] = {0};
lorcansmith 0:2a53a4c3238c 308 char s[ 20 ] = {0};
lorcansmith 0:2a53a4c3238c 309 char * p;
lorcansmith 0:2a53a4c3238c 310
lorcansmith 0:2a53a4c3238c 311 strcpy( v, "/offset," );
lorcansmith 0:2a53a4c3238c 312 p = strstr( req, v );
lorcansmith 0:2a53a4c3238c 313 strcpy( s, p );
lorcansmith 0:2a53a4c3238c 314 if ( p != NULL )
lorcansmith 0:2a53a4c3238c 315 {
lorcansmith 0:2a53a4c3238c 316 i = strlen(v);
lorcansmith 0:2a53a4c3238c 317 l = strlen(s);
lorcansmith 0:2a53a4c3238c 318 if ( (l > i) && (l < (i + 6)) )
lorcansmith 0:2a53a4c3238c 319 {
lorcansmith 0:2a53a4c3238c 320 l -= i;
lorcansmith 0:2a53a4c3238c 321 for ( c = 0; c < l; c++ )
lorcansmith 0:2a53a4c3238c 322 {
lorcansmith 0:2a53a4c3238c 323 v[ c ] = s[ i + c ];
lorcansmith 0:2a53a4c3238c 324 }
lorcansmith 0:2a53a4c3238c 325 v[ c ] = 0;
lorcansmith 0:2a53a4c3238c 326 fsvr.write( atoi(v) );
lorcansmith 0:2a53a4c3238c 327 }
lorcansmith 0:2a53a4c3238c 328 }
lorcansmith 0:2a53a4c3238c 329 }
lorcansmith 0:2a53a4c3238c 330
lorcansmith 0:2a53a4c3238c 331 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 332 /* chk_alarms: v1.04
lorcansmith 0:2a53a4c3238c 333 * Description: called when io_value has changed and checks each io_mask
lorcansmith 0:2a53a4c3238c 334 * to see if this mask_idx (IO register - 1) is masked in
lorcansmith 0:2a53a4c3238c 335 * Globals used: reg_change[], io_mask[], new_io_val[]
lorcansmith 0:2a53a4c3238c 336 * Parameters: IN: int mask_idx - 0-based index into mask = (io_reg - 1)
lorcansmith 0:2a53a4c3238c 337 * IN: unsigned short io_value - new I/O value for register
lorcansmith 0:2a53a4c3238c 338 * Returns: NONE
lorcansmith 0:2a53a4c3238c 339 */
lorcansmith 0:2a53a4c3238c 340 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 341 void chk_alarms( int mask_idx, unsigned short io_value )
lorcansmith 0:2a53a4c3238c 342 {
lorcansmith 0:2a53a4c3238c 343 unsigned char mask;
lorcansmith 0:2a53a4c3238c 344
lorcansmith 0:2a53a4c3238c 345 for ( mask = 0; mask < NUM_MASK; mask++ )
lorcansmith 0:2a53a4c3238c 346 {
lorcansmith 0:2a53a4c3238c 347 if ( (reg_change[ mask ] == 0) && ( io_mask[ mask ] & (1 << mask_idx) ) )
lorcansmith 0:2a53a4c3238c 348 { // masked-in register has changed
lorcansmith 0:2a53a4c3238c 349 new_io_val[ mask ] = (u32_t)io_value;
lorcansmith 0:2a53a4c3238c 350 reg_change[ mask ] = mask_idx + 1;
lorcansmith 0:2a53a4c3238c 351 }
lorcansmith 0:2a53a4c3238c 352 }
lorcansmith 0:2a53a4c3238c 353 }
lorcansmith 0:2a53a4c3238c 354
lorcansmith 0:2a53a4c3238c 355 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 356 /* get_SPI_data:
lorcansmith 0:2a53a4c3238c 357 * Description: Implements transaction as SPI slave.
lorcansmith 0:2a53a4c3238c 358 * Reads 4 data bytes from SPI bus into destbuf for SPI write, or
lorcansmith 0:2a53a4c3238c 359 * Puts 1 byte from srcbuf onto SPI for SPI read
lorcansmith 0:2a53a4c3238c 360 * Called by main when spi_slave.receive interrupts
lorcansmith 0:2a53a4c3238c 361 * LPC1768 SPI slave handles a maximum of 8 bytes per transfer
lorcansmith 0:2a53a4c3238c 362 * Globals used: NONE
lorcansmith 0:2a53a4c3238c 363 * Parameters: IN/OUT: unsigned char * srcbuf : buffer to read from
lorcansmith 0:2a53a4c3238c 364 * Returns: int buf_idx : index to SPI data in destbuf/srcbuf
lorcansmith 0:2a53a4c3238c 365 */
lorcansmith 0:2a53a4c3238c 366 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 367 int get_SPI_data( unsigned char * srcbuf )
lorcansmith 0:2a53a4c3238c 368 {
lorcansmith 0:2a53a4c3238c 369 static unsigned char rd_cnt; // v0.83: count of times valid data put on bus
lorcansmith 0:2a53a4c3238c 370 int buf_idx = 0xffff; // index to save SPI data into destbuf
lorcansmith 0:2a53a4c3238c 371 int idx; // index to compare with net address block
lorcansmith 0:2a53a4c3238c 372 unsigned char spi_buf[ 8 ]; // temporary buffer for SPI data received
lorcansmith 0:2a53a4c3238c 373 unsigned char byte_cnt; // count of SPI data bytes received
lorcansmith 0:2a53a4c3238c 374 unsigned char spiv; // SPI value - initial slave address
lorcansmith 0:2a53a4c3238c 375 unsigned char bcc = 0; // received block check character
lorcansmith 0:2a53a4c3238c 376 unsigned char bcc_ok = false; // default to reject messages without bcc
lorcansmith 0:2a53a4c3238c 377 unsigned short io_val; // first I/O value changed from registers 1-32
lorcansmith 0:2a53a4c3238c 378
lorcansmith 0:2a53a4c3238c 379 spiv = spi_slave.read(); // Read byte from master
lorcansmith 0:2a53a4c3238c 380 if ( (spiv & 0xfe) == (LPC1768 & 0xfe) )
lorcansmith 0:2a53a4c3238c 381 {
lorcansmith 0:2a53a4c3238c 382 if ( spiv & READ_IO )
lorcansmith 0:2a53a4c3238c 383 {
lorcansmith 0:2a53a4c3238c 384 led4 = 1;
lorcansmith 0:2a53a4c3238c 385 led3 = 0;
lorcansmith 0:2a53a4c3238c 386 if ( *srcbuf < NO_CMD )
lorcansmith 0:2a53a4c3238c 387 { // new data to be read by 3000 cpu
lorcansmith 0:2a53a4c3238c 388 if ( ++rd_cnt >= NUM_READ )
lorcansmith 0:2a53a4c3238c 389 { // v0.83: clear data after 4 reads
lorcansmith 0:2a53a4c3238c 390 spi_slave.reply( NO_CMD ); // put invalid data in SPI buffer
lorcansmith 0:2a53a4c3238c 391 *srcbuf = NO_CMD;
lorcansmith 0:2a53a4c3238c 392 rd_cnt = 0;
lorcansmith 0:2a53a4c3238c 393 }
lorcansmith 0:2a53a4c3238c 394 else if ( rd_cnt >= NUM_INDX )
lorcansmith 0:2a53a4c3238c 395 { // next put data from srcbuf in SPI buffer
lorcansmith 0:2a53a4c3238c 396 spi_slave.reply( *srcbuf );
lorcansmith 0:2a53a4c3238c 397 }
lorcansmith 0:2a53a4c3238c 398 else // put new_cmd_idx on SPI bus first
lorcansmith 0:2a53a4c3238c 399 {
lorcansmith 0:2a53a4c3238c 400 spi_slave.reply( NO_CMD - new_cmd_idx ); // put new_cmd_idx in SPI buffer
lorcansmith 0:2a53a4c3238c 401 }
lorcansmith 0:2a53a4c3238c 402 } // end if ( *srcbuf < 0xff )
lorcansmith 0:2a53a4c3238c 403 else
lorcansmith 0:2a53a4c3238c 404 {
lorcansmith 0:2a53a4c3238c 405 rd_cnt = 0; // re-initialise read count
lorcansmith 0:2a53a4c3238c 406 spi_slave.reply( NO_CMD ); // put invalid data in SPI buffer
lorcansmith 0:2a53a4c3238c 407 }
lorcansmith 0:2a53a4c3238c 408 }
lorcansmith 0:2a53a4c3238c 409 else // NOT READ_IO - data write
lorcansmith 0:2a53a4c3238c 410 {
lorcansmith 0:2a53a4c3238c 411 buf_idx = spi_slave.read(); // Read ms index from master
lorcansmith 0:2a53a4c3238c 412 bcc ^= buf_idx; // Start checksum calculation
lorcansmith 0:2a53a4c3238c 413 buf_idx *= 256; // Make ms index from master
lorcansmith 0:2a53a4c3238c 414 buf_idx += spi_slave.read(); // Read ls index from master
lorcansmith 0:2a53a4c3238c 415 bcc ^= (buf_idx & 0xff);
lorcansmith 0:2a53a4c3238c 416 byte_cnt = 0;
lorcansmith 0:2a53a4c3238c 417 while( spi_slave.receive() ) {
lorcansmith 0:2a53a4c3238c 418 if( byte_cnt < SPI_BYTES ) { // Read 4 data bytes
lorcansmith 0:2a53a4c3238c 419 spi_buf[ byte_cnt ] = spi_slave.read();
lorcansmith 0:2a53a4c3238c 420 bcc ^= spi_buf[ byte_cnt++ ];
lorcansmith 0:2a53a4c3238c 421 }
lorcansmith 0:2a53a4c3238c 422 else if ( byte_cnt == SPI_BYTES ) {
lorcansmith 0:2a53a4c3238c 423 spiv = spi_slave.read();
lorcansmith 0:2a53a4c3238c 424 bcc_ok = ( bcc == spiv );
lorcansmith 0:2a53a4c3238c 425 }
lorcansmith 0:2a53a4c3238c 426 } // end while ( spi_slave.receive() )
lorcansmith 0:2a53a4c3238c 427 if ( bcc_ok && (buf_idx < MAX_RAM) )
lorcansmith 0:2a53a4c3238c 428 {
lorcansmith 0:2a53a4c3238c 429 for( byte_cnt = 0; byte_cnt < SPI_BYTES; byte_cnt++ )
lorcansmith 0:2a53a4c3238c 430 {
lorcansmith 0:2a53a4c3238c 431 idx = buf_idx + byte_cnt;
lorcansmith 0:2a53a4c3238c 432 if ( (idx <= MAX_IO_REG_BYTE) && (idx & 1) ) // check only ls byte of IO registers
lorcansmith 0:2a53a4c3238c 433 { // check ls byte of IO register changed from last saved
lorcansmith 0:2a53a4c3238c 434 if ( spi_buf[ byte_cnt ] != fsvr.get_byte( idx ) )
lorcansmith 0:2a53a4c3238c 435 { // ls byte of register changed
lorcansmith 0:2a53a4c3238c 436 io_val = spi_buf[ byte_cnt ] + 256 * spi_buf[ byte_cnt - 1 ];
lorcansmith 0:2a53a4c3238c 437 if ( !byte_changed && ( mask4_IO_registers & (1 << (idx/2)) ) )
lorcansmith 0:2a53a4c3238c 438 { // masked-in register has changed
lorcansmith 0:2a53a4c3238c 439 io_chng_val = io_val;
lorcansmith 0:2a53a4c3238c 440 io_chng_idx = idx/2 + 1; // save index of first I/O register to change
lorcansmith 0:2a53a4c3238c 441 byte_changed = true;
lorcansmith 0:2a53a4c3238c 442 }
lorcansmith 0:2a53a4c3238c 443 chk_alarms( idx/2, io_val );
lorcansmith 0:2a53a4c3238c 444 }
lorcansmith 0:2a53a4c3238c 445 if ( idx == MAX_IO_REG_BYTE )
lorcansmith 0:2a53a4c3238c 446 { // after each I/O scan
lorcansmith 0:2a53a4c3238c 447 all_IO_updated = true;
lorcansmith 0:2a53a4c3238c 448 spi_count = fsvr.get_SPI_count(); // save last SPI_count value
lorcansmith 0:2a53a4c3238c 449 }
lorcansmith 0:2a53a4c3238c 450 }
lorcansmith 0:2a53a4c3238c 451 else if ( idx == NET_ADDRESS_CHANGE )
lorcansmith 0:2a53a4c3238c 452 { // v0.93: check for net address update from 3000 CPU
lorcansmith 0:2a53a4c3238c 453 if ( spi_buf[ byte_cnt ] == NO_UPDATE )
lorcansmith 0:2a53a4c3238c 454 {
lorcansmith 0:2a53a4c3238c 455 net_addrs_chng = NO_UPDATE;
lorcansmith 0:2a53a4c3238c 456 }
lorcansmith 0:2a53a4c3238c 457 else if ( net_addrs_chng != UPDATED )
lorcansmith 0:2a53a4c3238c 458 {
lorcansmith 0:2a53a4c3238c 459 net_addrs_chng = spi_buf[ byte_cnt ];
lorcansmith 0:2a53a4c3238c 460 }
lorcansmith 0:2a53a4c3238c 461 }
lorcansmith 0:2a53a4c3238c 462 fsvr.put_byte( idx, spi_buf[ byte_cnt ] ); // save byte read from SPI
lorcansmith 0:2a53a4c3238c 463 }
lorcansmith 0:2a53a4c3238c 464 led3 = 0;
lorcansmith 0:2a53a4c3238c 465 }
lorcansmith 0:2a53a4c3238c 466 else // checksum failed
lorcansmith 0:2a53a4c3238c 467 {
lorcansmith 0:2a53a4c3238c 468 led3 = 1;
lorcansmith 0:2a53a4c3238c 469 }
lorcansmith 0:2a53a4c3238c 470 led4 = 0;
lorcansmith 0:2a53a4c3238c 471 } // end else not READ_IO
lorcansmith 0:2a53a4c3238c 472 } // end if ( (spiv & 0xfe) == (LPC1768 & 0xfe) )
lorcansmith 0:2a53a4c3238c 473 return buf_idx;
lorcansmith 0:2a53a4c3238c 474 }
lorcansmith 0:2a53a4c3238c 475
lorcansmith 0:2a53a4c3238c 476 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 477 /* chk_SPI_active:
lorcansmith 0:2a53a4c3238c 478 * Description: checks spi_count has been updated in SPI interrupt
lorcansmith 0:2a53a4c3238c 479 * to be called by timer, independently of SPI interrupt
lorcansmith 0:2a53a4c3238c 480 * Globals used: spi_active, spi_count
lorcansmith 0:2a53a4c3238c 481 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 482 * Returns: NONE
lorcansmith 0:2a53a4c3238c 483 */
lorcansmith 0:2a53a4c3238c 484 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 485 void chk_SPI_active( void )
lorcansmith 0:2a53a4c3238c 486 {
lorcansmith 0:2a53a4c3238c 487 if ( spi_count == INV_SPI_CNT )
lorcansmith 0:2a53a4c3238c 488 {
lorcansmith 0:2a53a4c3238c 489 spi_active = false; // flag SPI failure
lorcansmith 0:2a53a4c3238c 490 }
lorcansmith 0:2a53a4c3238c 491 else
lorcansmith 0:2a53a4c3238c 492 {
lorcansmith 0:2a53a4c3238c 493 spi_count = INV_SPI_CNT; // set to be overwritten by next SPI update
lorcansmith 0:2a53a4c3238c 494 spi_active = true; // SPI updated OK
lorcansmith 0:2a53a4c3238c 495 }
lorcansmith 0:2a53a4c3238c 496 }
lorcansmith 0:2a53a4c3238c 497
lorcansmith 0:2a53a4c3238c 498 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 499 /* get_hex_mask:
lorcansmith 0:2a53a4c3238c 500 * Description: converts long bit_mask to hexadecimal string
lorcansmith 0:2a53a4c3238c 501 * Globals used: hex_mask
lorcansmith 0:2a53a4c3238c 502 * Parameters: IN: unsigned long bit_mask - numeric value of bit mask
lorcansmith 0:2a53a4c3238c 503 * Returns: pointer to hex_mask
lorcansmith 0:2a53a4c3238c 504 */
lorcansmith 0:2a53a4c3238c 505 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 506 char * get_hex_mask( unsigned long bit_mask )
lorcansmith 0:2a53a4c3238c 507 {
lorcansmith 0:2a53a4c3238c 508 unsigned char i;
lorcansmith 0:2a53a4c3238c 509 for ( i = 0; i < 12; i++ )
lorcansmith 0:2a53a4c3238c 510 {
lorcansmith 0:2a53a4c3238c 511 hex_mask[ i ] = 0;
lorcansmith 0:2a53a4c3238c 512 }
lorcansmith 0:2a53a4c3238c 513 sprintf(hex_mask, "%08x", bit_mask);
lorcansmith 0:2a53a4c3238c 514 return hex_mask;
lorcansmith 0:2a53a4c3238c 515 }
lorcansmith 0:2a53a4c3238c 516
lorcansmith 0:2a53a4c3238c 517 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 518 /* convert2decimal:
lorcansmith 0:2a53a4c3238c 519 * Description: converts hexadecimal string to long mask4_IO_registers
lorcansmith 0:2a53a4c3238c 520 * Globals used: NONE
lorcansmith 0:2a53a4c3238c 521 * Parameters: u8_t * uc_ptr: pointer to hex-formatted mask string
lorcansmith 0:2a53a4c3238c 522 * u16_t len: length of string
lorcansmith 0:2a53a4c3238c 523 * Returns: unsigned long temp_sum
lorcansmith 0:2a53a4c3238c 524 */
lorcansmith 0:2a53a4c3238c 525 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 526 unsigned long convert2decimal( u8_t * uc_ptr, u16_t len )
lorcansmith 0:2a53a4c3238c 527 {
lorcansmith 0:2a53a4c3238c 528 char ch, i;
lorcansmith 0:2a53a4c3238c 529 unsigned long temp_sum = 0;
lorcansmith 0:2a53a4c3238c 530
lorcansmith 0:2a53a4c3238c 531 for ( i = 0; i < len; i++ )
lorcansmith 0:2a53a4c3238c 532 {
lorcansmith 0:2a53a4c3238c 533 ch = (char)*(uc_ptr + i);
lorcansmith 0:2a53a4c3238c 534 if ( islower( ch ) )
lorcansmith 0:2a53a4c3238c 535 {
lorcansmith 0:2a53a4c3238c 536 ch = ch - 'a' + 10;
lorcansmith 0:2a53a4c3238c 537 }
lorcansmith 0:2a53a4c3238c 538 else if ( isupper( ch ) )
lorcansmith 0:2a53a4c3238c 539 {
lorcansmith 0:2a53a4c3238c 540 ch = ch - 'A' + 10;
lorcansmith 0:2a53a4c3238c 541 }
lorcansmith 0:2a53a4c3238c 542 else if ( isdigit( ch ) )
lorcansmith 0:2a53a4c3238c 543 {
lorcansmith 0:2a53a4c3238c 544 ch = ch - '0';
lorcansmith 0:2a53a4c3238c 545 }
lorcansmith 0:2a53a4c3238c 546 else
lorcansmith 0:2a53a4c3238c 547 {
lorcansmith 0:2a53a4c3238c 548 temp_sum = 0xfffffff;
lorcansmith 0:2a53a4c3238c 549 ch = 15;
lorcansmith 0:2a53a4c3238c 550 i = len; // end on error;
lorcansmith 0:2a53a4c3238c 551 }
lorcansmith 0:2a53a4c3238c 552 temp_sum = 16 * temp_sum + ch;
lorcansmith 0:2a53a4c3238c 553 }
lorcansmith 0:2a53a4c3238c 554 return temp_sum;
lorcansmith 0:2a53a4c3238c 555 }
lorcansmith 0:2a53a4c3238c 556
lorcansmith 0:2a53a4c3238c 557 void send_ram( void )
lorcansmith 0:2a53a4c3238c 558 {
lorcansmith 0:2a53a4c3238c 559 static int idx;
lorcansmith 0:2a53a4c3238c 560 char i;
lorcansmith 0:2a53a4c3238c 561
lorcansmith 0:2a53a4c3238c 562 if ( (idx & 0x7ff) == 0 )
lorcansmith 0:2a53a4c3238c 563 {
lorcansmith 0:2a53a4c3238c 564 pc.printf("CMD data %x", idx );
lorcansmith 0:2a53a4c3238c 565 for ( i = 0; i < MAX_CMD; i++ )
lorcansmith 0:2a53a4c3238c 566 {
lorcansmith 0:2a53a4c3238c 567 pc.printf(", %x", cmd_buf[ i ] );
lorcansmith 0:2a53a4c3238c 568 }
lorcansmith 0:2a53a4c3238c 569 pc.printf( "\r\n" );
lorcansmith 0:2a53a4c3238c 570 pc.printf("RAM data %x", idx & 0x7ff );
lorcansmith 0:2a53a4c3238c 571 for ( i = 0; i < 8; i++ )
lorcansmith 0:2a53a4c3238c 572 {
lorcansmith 0:2a53a4c3238c 573 pc.printf(", %x", fsvr.get_byte(idx++ & 0x7ff) );
lorcansmith 0:2a53a4c3238c 574 }
lorcansmith 0:2a53a4c3238c 575 pc.printf( "\r\n" );
lorcansmith 0:2a53a4c3238c 576 }
lorcansmith 0:2a53a4c3238c 577 else
lorcansmith 0:2a53a4c3238c 578 {
lorcansmith 0:2a53a4c3238c 579 pc.printf("RAM data %x", idx & 0x7ff );
lorcansmith 0:2a53a4c3238c 580 for ( i = 0; i < 8; i++ )
lorcansmith 0:2a53a4c3238c 581 {
lorcansmith 0:2a53a4c3238c 582 pc.printf(", %x", fsvr.get_byte(idx++ & 0x7ff) );
lorcansmith 0:2a53a4c3238c 583 }
lorcansmith 0:2a53a4c3238c 584 pc.printf( "\r\n" );
lorcansmith 0:2a53a4c3238c 585 }
lorcansmith 0:2a53a4c3238c 586 }
lorcansmith 0:2a53a4c3238c 587
lorcansmith 0:2a53a4c3238c 588 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 589 /* write_addrs_to_file:
lorcansmith 0:2a53a4c3238c 590 * Description: Writes net addresses to /webfs/net_adrs.txt
lorcansmith 0:2a53a4c3238c 591 * Globals used: NET_ADRS_FILE
lorcansmith 0:2a53a4c3238c 592 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 593 * Returns: 0 if OK, 1 if failed to open files
lorcansmith 0:2a53a4c3238c 594 */
lorcansmith 0:2a53a4c3238c 595 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 596 int write_addrs_to_file()
lorcansmith 0:2a53a4c3238c 597 {
lorcansmith 0:2a53a4c3238c 598 FILE *fp = fopen( NET_ADRS_FILE, "w" );
lorcansmith 0:2a53a4c3238c 599 if(!fp) {
lorcansmith 0:2a53a4c3238c 600 fprintf(stderr, "\r\nFile /webfs/net_adrs.txt could not be opened to write!\r\n");
lorcansmith 0:2a53a4c3238c 601 exit(1);
lorcansmith 0:2a53a4c3238c 602 }
lorcansmith 0:2a53a4c3238c 603 else // file opened OK, so save addresses
lorcansmith 0:2a53a4c3238c 604 {
lorcansmith 0:2a53a4c3238c 605 fprintf(fp, "IP address %u.%u.%u.%u \r\n", ip_ad[0],ip_ad[1],ip_ad[2],ip_ad[3]);
lorcansmith 0:2a53a4c3238c 606 fprintf(fp, "Address mask %u.%u.%u.%u \r\n", msk_ad[0],msk_ad[1],msk_ad[2],msk_ad[3]);
lorcansmith 0:2a53a4c3238c 607 fprintf(fp, "Gwy address %u.%u.%u.%u \r\n", gwy_ad[0],gwy_ad[1],gwy_ad[2],gwy_ad[3]);
lorcansmith 0:2a53a4c3238c 608 fprintf(fp, "DNS address %u.%u.%u.%u \r\n", dns_ad[0],dns_ad[1],dns_ad[2],dns_ad[3]);
lorcansmith 0:2a53a4c3238c 609 fprintf(fp, "%s\r\n", snmp_publiccommunity ); // v0.98: save community string
lorcansmith 0:2a53a4c3238c 610 fprintf(fp, "Trap host address %u.%u.%u.%u \r\n", th_ad[0],th_ad[1],th_ad[2],th_ad[3]);
lorcansmith 0:2a53a4c3238c 611 fprintf(fp, "%u \r\n", mask4_IO_registers );
lorcansmith 0:2a53a4c3238c 612 fclose(fp);
lorcansmith 0:2a53a4c3238c 613 } // file opened OK
lorcansmith 0:2a53a4c3238c 614 return 0;
lorcansmith 0:2a53a4c3238c 615 }
lorcansmith 0:2a53a4c3238c 616
lorcansmith 0:2a53a4c3238c 617 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 618 /* copy_trap_host_address: v0.99
lorcansmith 0:2a53a4c3238c 619 * Description: Copies src array to th_ad
lorcansmith 0:2a53a4c3238c 620 * Globals used: th_ad
lorcansmith 0:2a53a4c3238c 621 * net_addrs_chng set to UPDATED
lorcansmith 0:2a53a4c3238c 622 * Parameters: IN: unsigned char * src - source array of 4 bytes
lorcansmith 0:2a53a4c3238c 623 * Returns: NONE
lorcansmith 0:2a53a4c3238c 624 */
lorcansmith 0:2a53a4c3238c 625 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 626 void copy_trap_host_address( unsigned char * src )
lorcansmith 0:2a53a4c3238c 627 {
lorcansmith 0:2a53a4c3238c 628 unsigned char i;
lorcansmith 0:2a53a4c3238c 629 for ( i = 0; i < 4; i++ )
lorcansmith 0:2a53a4c3238c 630 {
lorcansmith 0:2a53a4c3238c 631 if ( src[ i ] != 0 )
lorcansmith 0:2a53a4c3238c 632 {
lorcansmith 0:2a53a4c3238c 633 {
lorcansmith 0:2a53a4c3238c 634 th_ad[ i ] = src[ i ];
lorcansmith 0:2a53a4c3238c 635 }
lorcansmith 0:2a53a4c3238c 636 }
lorcansmith 0:2a53a4c3238c 637 }
lorcansmith 0:2a53a4c3238c 638 net_addrs_chng = UPDATED;
lorcansmith 0:2a53a4c3238c 639 }
lorcansmith 0:2a53a4c3238c 640
lorcansmith 0:2a53a4c3238c 641 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 642 /* copy_net_addresses_from: v0.93
lorcansmith 0:2a53a4c3238c 643 * Description: Copies src array to ip_ad, msk_ad, gwy_ad, dns_ad
lorcansmith 0:2a53a4c3238c 644 * Globals used: ip_ad, msk_ad, gwy_ad, dns_ad
lorcansmith 0:2a53a4c3238c 645 * Parameters: IN: unsigned char * src - source array of 16 bytes
lorcansmith 0:2a53a4c3238c 646 * Returns: NONE
lorcansmith 0:2a53a4c3238c 647 */
lorcansmith 0:2a53a4c3238c 648 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 649 void copy_net_addresses_from( unsigned char * src )
lorcansmith 0:2a53a4c3238c 650 {
lorcansmith 0:2a53a4c3238c 651 unsigned char i;
lorcansmith 0:2a53a4c3238c 652 for ( i = 0; i < 4; i++ )
lorcansmith 0:2a53a4c3238c 653 {
lorcansmith 0:2a53a4c3238c 654 if ( src[ i ] != 0 )
lorcansmith 0:2a53a4c3238c 655 {
lorcansmith 0:2a53a4c3238c 656 ip_ad[ i ] = src[ i ];
lorcansmith 0:2a53a4c3238c 657 }
lorcansmith 0:2a53a4c3238c 658 }
lorcansmith 0:2a53a4c3238c 659 for ( i = 4; i < 8; i++ )
lorcansmith 0:2a53a4c3238c 660 {
lorcansmith 0:2a53a4c3238c 661 if ( src[ i ] != 0 )
lorcansmith 0:2a53a4c3238c 662 {
lorcansmith 0:2a53a4c3238c 663 msk_ad[ i-4 ] = src[ i ];
lorcansmith 0:2a53a4c3238c 664 }
lorcansmith 0:2a53a4c3238c 665 }
lorcansmith 0:2a53a4c3238c 666 for ( i = 8; i < 12; i++ )
lorcansmith 0:2a53a4c3238c 667 {
lorcansmith 0:2a53a4c3238c 668 if ( src[ i ] != 0 )
lorcansmith 0:2a53a4c3238c 669 {
lorcansmith 0:2a53a4c3238c 670 gwy_ad[ i-8 ] = src[ i ];
lorcansmith 0:2a53a4c3238c 671 }
lorcansmith 0:2a53a4c3238c 672 }
lorcansmith 0:2a53a4c3238c 673 for ( i = 12; i < 16; i++ )
lorcansmith 0:2a53a4c3238c 674 {
lorcansmith 0:2a53a4c3238c 675 if ( src[ i ] != 0 )
lorcansmith 0:2a53a4c3238c 676 {
lorcansmith 0:2a53a4c3238c 677 dns_ad[ i-12 ] = src[ i ];
lorcansmith 0:2a53a4c3238c 678 }
lorcansmith 0:2a53a4c3238c 679 }
lorcansmith 0:2a53a4c3238c 680 }
lorcansmith 0:2a53a4c3238c 681
lorcansmith 0:2a53a4c3238c 682 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 683 /* parse4numbers:
lorcansmith 0:2a53a4c3238c 684 * Description: Reads file fr and separates out max_num numbers delimited by
lorcansmith 0:2a53a4c3238c 685 * any non-digit characters
lorcansmith 0:2a53a4c3238c 686 * Globals used:
lorcansmith 0:2a53a4c3238c 687 * Parameters: IN: FILE *fr - file to read
lorcansmith 0:2a53a4c3238c 688 * OUT: unsigned char * numb - array of numbers read
lorcansmith 0:2a53a4c3238c 689 * IN: unsigned char max_num - number of numbers to read
lorcansmith 0:2a53a4c3238c 690 * Returns: NONE
lorcansmith 0:2a53a4c3238c 691 */
lorcansmith 0:2a53a4c3238c 692 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 693 void parse4numbers( FILE *fr, unsigned char * numb, unsigned char max_num )
lorcansmith 0:2a53a4c3238c 694 {
lorcansmith 0:2a53a4c3238c 695 int c, i, new_num;
lorcansmith 0:2a53a4c3238c 696 char digit_found = false;
lorcansmith 0:2a53a4c3238c 697 char number_completed = false;
lorcansmith 0:2a53a4c3238c 698
lorcansmith 0:2a53a4c3238c 699 i = new_num = 0;
lorcansmith 0:2a53a4c3238c 700 while ( ((c = fgetc(fr)) != EOF) && (i < max_num) )
lorcansmith 0:2a53a4c3238c 701 {
lorcansmith 0:2a53a4c3238c 702 numb[ i ] = 0;
lorcansmith 0:2a53a4c3238c 703 if ( c < '0' || c > '9' )
lorcansmith 0:2a53a4c3238c 704 { // not a numeric character
lorcansmith 0:2a53a4c3238c 705 if ( digit_found )
lorcansmith 0:2a53a4c3238c 706 {
lorcansmith 0:2a53a4c3238c 707 number_completed = true;
lorcansmith 0:2a53a4c3238c 708 if ( i >= (max_num - 1) )
lorcansmith 0:2a53a4c3238c 709 { // max_num distinct numbers read
lorcansmith 0:2a53a4c3238c 710 break;
lorcansmith 0:2a53a4c3238c 711 }
lorcansmith 0:2a53a4c3238c 712 }
lorcansmith 0:2a53a4c3238c 713 }
lorcansmith 0:2a53a4c3238c 714 else // numeric character
lorcansmith 0:2a53a4c3238c 715 {
lorcansmith 0:2a53a4c3238c 716 digit_found = true;
lorcansmith 0:2a53a4c3238c 717 if ( number_completed )
lorcansmith 0:2a53a4c3238c 718 { // save number into array
lorcansmith 0:2a53a4c3238c 719 numb[ i++ ] = new_num & 0xff;
lorcansmith 0:2a53a4c3238c 720 new_num = 0;
lorcansmith 0:2a53a4c3238c 721 }
lorcansmith 0:2a53a4c3238c 722 new_num = 10*new_num + c - '0';
lorcansmith 0:2a53a4c3238c 723 number_completed = false;
lorcansmith 0:2a53a4c3238c 724 } // else numeric character
lorcansmith 0:2a53a4c3238c 725 } // end while
lorcansmith 0:2a53a4c3238c 726 if ( (number_completed || (c == EOF)) && (i < max_num) )
lorcansmith 0:2a53a4c3238c 727 { // 0.80: save last number read
lorcansmith 0:2a53a4c3238c 728 numb[ i++ ] = new_num & 0xff;
lorcansmith 0:2a53a4c3238c 729 }
lorcansmith 0:2a53a4c3238c 730 }
lorcansmith 0:2a53a4c3238c 731
lorcansmith 0:2a53a4c3238c 732 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 733 /* read_addrs_from_file:
lorcansmith 0:2a53a4c3238c 734 * Description: Reads data from /webfs/net_adrs.txt
lorcansmith 0:2a53a4c3238c 735 * Globals used: NET_ADRS_FILE
lorcansmith 0:2a53a4c3238c 736 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 737 * Returns: 0 if OK, 1 if failed to open files
lorcansmith 0:2a53a4c3238c 738 */
lorcansmith 0:2a53a4c3238c 739 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 740 int read_addrs_from_file()
lorcansmith 0:2a53a4c3238c 741 {
lorcansmith 0:2a53a4c3238c 742 int c, i;
lorcansmith 0:2a53a4c3238c 743 unsigned char numbers[17];
lorcansmith 0:2a53a4c3238c 744 char community[ MAX_COMMUNITY ];
lorcansmith 0:2a53a4c3238c 745
lorcansmith 0:2a53a4c3238c 746 for ( i = 0; i < MAX_COMMUNITY; i++ )
lorcansmith 0:2a53a4c3238c 747 {
lorcansmith 0:2a53a4c3238c 748 community[ i ] = 0;
lorcansmith 0:2a53a4c3238c 749 }
lorcansmith 0:2a53a4c3238c 750 FILE *fr = fopen( NET_ADRS_FILE, "r" );
lorcansmith 0:2a53a4c3238c 751 if(!fr) {
lorcansmith 0:2a53a4c3238c 752 fprintf(stderr, "\r\nFile /webfs/net_adrs.txt could not be opened!\r\n");
lorcansmith 0:2a53a4c3238c 753 exit(1);
lorcansmith 0:2a53a4c3238c 754 }
lorcansmith 0:2a53a4c3238c 755 else // file opened OK
lorcansmith 0:2a53a4c3238c 756 {
lorcansmith 0:2a53a4c3238c 757 parse4numbers( fr, numbers, 16 );
lorcansmith 0:2a53a4c3238c 758 copy_net_addresses_from( numbers ); // v0.93: new subroutine
lorcansmith 0:2a53a4c3238c 759 /* v0.98: now read the community string from the file after reading end of line */
lorcansmith 0:2a53a4c3238c 760 while ( ((c = fgetc(fr)) != EOF) && ((c == ' ') || (c == '\r') || (c == '\n')) );
lorcansmith 0:2a53a4c3238c 761 i = 0;
lorcansmith 0:2a53a4c3238c 762 do
lorcansmith 0:2a53a4c3238c 763 {
lorcansmith 0:2a53a4c3238c 764 c = (char)c;
lorcansmith 0:2a53a4c3238c 765 if ( (c != ' ') && (c != '\r') && (c != '\n') && (i < MAX_COMMUNITY-1) )
lorcansmith 0:2a53a4c3238c 766 {
lorcansmith 0:2a53a4c3238c 767 community[ i++ ] = c;
lorcansmith 0:2a53a4c3238c 768 }
lorcansmith 0:2a53a4c3238c 769 else // string terminated
lorcansmith 0:2a53a4c3238c 770 {
lorcansmith 0:2a53a4c3238c 771 break;
lorcansmith 0:2a53a4c3238c 772 }
lorcansmith 0:2a53a4c3238c 773 } while ( ((c = fgetc(fr)) != EOF) && (c != 0) );
lorcansmith 0:2a53a4c3238c 774 parse4numbers( fr, numbers, 4 ); // now read in Trap host address
lorcansmith 0:2a53a4c3238c 775 copy_trap_host_address( numbers );
lorcansmith 0:2a53a4c3238c 776 fscanf( fr, "%lu", &mask4_IO_registers );
lorcansmith 0:2a53a4c3238c 777 fclose(fr);
lorcansmith 0:2a53a4c3238c 778 } // file opened OK
lorcansmith 0:2a53a4c3238c 779 if ( strlen(community) >= MIN_COMMUNITY )
lorcansmith 0:2a53a4c3238c 780 { // string length 4 to 20 characters
lorcansmith 0:2a53a4c3238c 781 strcpy( snmp_publiccommunity, community );
lorcansmith 0:2a53a4c3238c 782 pc.printf("community: %s %d \r\n", snmp_publiccommunity, strlen(snmp_publiccommunity) );
lorcansmith 0:2a53a4c3238c 783 }
lorcansmith 0:2a53a4c3238c 784 else
lorcansmith 0:2a53a4c3238c 785 {
lorcansmith 0:2a53a4c3238c 786 pc.printf("community too short: %s %d \r\n", community, strlen(community) );
lorcansmith 0:2a53a4c3238c 787 }
lorcansmith 0:2a53a4c3238c 788 return 0;
lorcansmith 0:2a53a4c3238c 789 }
lorcansmith 0:2a53a4c3238c 790
lorcansmith 0:2a53a4c3238c 791 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 792 /* read_alarm_from_file:
lorcansmith 0:2a53a4c3238c 793 * Description: Reads one alarm_msg from /webfs/alarms.txt if alarm_idx < NUM_MASK
lorcansmith 0:2a53a4c3238c 794 * or can read all io_masks as well if alarm_idx >= NUM_MASK
lorcansmith 0:2a53a4c3238c 795 * Globals used: ALARM_FILE, alarm_msg, io_mask
lorcansmith 0:2a53a4c3238c 796 * Parameters: IN: unsigned char alarm_idx - index to alarm message
lorcansmith 0:2a53a4c3238c 797 * Returns: 0 if OK, 1 if failed to open files
lorcansmith 0:2a53a4c3238c 798 */
lorcansmith 0:2a53a4c3238c 799 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 800 int read_alarm_from_file(unsigned char alarm_idx)
lorcansmith 0:2a53a4c3238c 801 {
lorcansmith 0:2a53a4c3238c 802 unsigned char i;
lorcansmith 0:2a53a4c3238c 803
lorcansmith 0:2a53a4c3238c 804 FILE *fr = fopen( ALARM_FILE, "r" );
lorcansmith 0:2a53a4c3238c 805 if(!fr) {
lorcansmith 0:2a53a4c3238c 806 fprintf(stderr, "\r\nFile /webfs/alarm.txt could not be opened!\r\n");
lorcansmith 0:2a53a4c3238c 807 exit(1);
lorcansmith 0:2a53a4c3238c 808 }
lorcansmith 0:2a53a4c3238c 809 else // file opened OK
lorcansmith 0:2a53a4c3238c 810 {
lorcansmith 0:2a53a4c3238c 811 if ( alarm_idx < NUM_MASK )
lorcansmith 0:2a53a4c3238c 812 {
lorcansmith 0:2a53a4c3238c 813 for ( i = 0; i <= alarm_idx; i++ )
lorcansmith 0:2a53a4c3238c 814 { // read specified alarm_msg
lorcansmith 0:2a53a4c3238c 815 fscanf( fr, "%s", &alarm_msg[ i ] );
lorcansmith 0:2a53a4c3238c 816 }
lorcansmith 0:2a53a4c3238c 817 }
lorcansmith 0:2a53a4c3238c 818 else // read io_masks
lorcansmith 0:2a53a4c3238c 819 {
lorcansmith 0:2a53a4c3238c 820 for ( i = 0; i < NUM_MASK; i++ )
lorcansmith 0:2a53a4c3238c 821 { // read all alarm_msg
lorcansmith 0:2a53a4c3238c 822 fscanf( fr, "%s", &alarm_msg[ i ] );
lorcansmith 0:2a53a4c3238c 823 }
lorcansmith 0:2a53a4c3238c 824 for ( i = 0; i < NUM_MASK; i++ )
lorcansmith 0:2a53a4c3238c 825 { // read all io_masks
lorcansmith 0:2a53a4c3238c 826 fscanf( fr, "%lx", &io_mask[ i ] );
lorcansmith 0:2a53a4c3238c 827 }
lorcansmith 0:2a53a4c3238c 828 }
lorcansmith 0:2a53a4c3238c 829 fclose(fr);
lorcansmith 0:2a53a4c3238c 830 } // file opened OK
lorcansmith 0:2a53a4c3238c 831 return 0;
lorcansmith 0:2a53a4c3238c 832 }
lorcansmith 0:2a53a4c3238c 833
lorcansmith 0:2a53a4c3238c 834 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 835 /* blinker:
lorcansmith 0:2a53a4c3238c 836 * Description: Blinks led_link for 10 calls
lorcansmith 0:2a53a4c3238c 837 * Globals used: led_link, blink_led,
lorcansmith 0:2a53a4c3238c 838 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 839 * Returns: NONE
lorcansmith 0:2a53a4c3238c 840 */
lorcansmith 0:2a53a4c3238c 841 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 842 void blinker( void )
lorcansmith 0:2a53a4c3238c 843 {
lorcansmith 0:2a53a4c3238c 844 static unsigned char blink_cnt;
lorcansmith 0:2a53a4c3238c 845
lorcansmith 0:2a53a4c3238c 846 if ( blink_led && blink_cnt++ < 10 )
lorcansmith 0:2a53a4c3238c 847 {
lorcansmith 0:2a53a4c3238c 848 led_link = !led_link;
lorcansmith 0:2a53a4c3238c 849 }
lorcansmith 0:2a53a4c3238c 850 else
lorcansmith 0:2a53a4c3238c 851 {
lorcansmith 0:2a53a4c3238c 852 blink_cnt = 0;
lorcansmith 0:2a53a4c3238c 853 blink_led = false;
lorcansmith 0:2a53a4c3238c 854 }
lorcansmith 0:2a53a4c3238c 855 }
lorcansmith 0:2a53a4c3238c 856
lorcansmith 0:2a53a4c3238c 857 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 858 /* write_alarms_to_file: v1.04
lorcansmith 0:2a53a4c3238c 859 * Description: Writes alarm messages and masks to /webfs/alarms.txt
lorcansmith 0:2a53a4c3238c 860 * Globals used: ALARM_FILE, alarm_msg[], io_mask[]
lorcansmith 0:2a53a4c3238c 861 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 862 * Returns: 0 if OK, 1 if failed to open files
lorcansmith 0:2a53a4c3238c 863 */
lorcansmith 0:2a53a4c3238c 864 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 865 int write_alarms_to_file( void )
lorcansmith 0:2a53a4c3238c 866 {
lorcansmith 0:2a53a4c3238c 867 u8_t i = 1; // return value if failed to write file
lorcansmith 0:2a53a4c3238c 868
lorcansmith 0:2a53a4c3238c 869 FILE *fp = fopen( ALARM_FILE, "w" );
lorcansmith 0:2a53a4c3238c 870 if(!fp) {
lorcansmith 0:2a53a4c3238c 871 fprintf(stderr, "\r\nFile /webfs/alarms.txt could not be opened to write!\r\n");
lorcansmith 0:2a53a4c3238c 872 }
lorcansmith 0:2a53a4c3238c 873 else // file opened OK, so save addresses
lorcansmith 0:2a53a4c3238c 874 {
lorcansmith 0:2a53a4c3238c 875 for ( i = 0; i < NUM_MASK; i++ )
lorcansmith 0:2a53a4c3238c 876 {
lorcansmith 0:2a53a4c3238c 877 fprintf(fp, "%s\r\n", alarm_msg[ i ] );
lorcansmith 0:2a53a4c3238c 878 }
lorcansmith 0:2a53a4c3238c 879 for ( i = 0; i < NUM_MASK; i++ )
lorcansmith 0:2a53a4c3238c 880 {
lorcansmith 0:2a53a4c3238c 881 fprintf(fp, "%08x\r\n", io_mask[ i ] );
lorcansmith 0:2a53a4c3238c 882 }
lorcansmith 0:2a53a4c3238c 883 fclose(fp);
lorcansmith 0:2a53a4c3238c 884 i = 0;
lorcansmith 0:2a53a4c3238c 885 } // file opened OK
lorcansmith 0:2a53a4c3238c 886 return i;
lorcansmith 0:2a53a4c3238c 887 }
lorcansmith 0:2a53a4c3238c 888
lorcansmith 0:2a53a4c3238c 889 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 890 /* init_traps:
lorcansmith 0:2a53a4c3238c 891 * Description: initialises trap flags and sends cold start trap when done
lorcansmith 0:2a53a4c3238c 892 * Globals used: led1: mimics state of head-link status output in 3000 I/O
lorcansmith 0:2a53a4c3238c 893 * trap_init set true after cold start
lorcansmith 0:2a53a4c3238c 894 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 895 * Returns: NONE
lorcansmith 0:2a53a4c3238c 896 */
lorcansmith 0:2a53a4c3238c 897 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 898 void init_traps( void )
lorcansmith 0:2a53a4c3238c 899 {
lorcansmith 0:2a53a4c3238c 900 u8_t i = 0;
lorcansmith 0:2a53a4c3238c 901
lorcansmith 0:2a53a4c3238c 902 snmp_coldstart_trap();
lorcansmith 0:2a53a4c3238c 903 // force sending trap for head-head link up/down status
lorcansmith 0:2a53a4c3238c 904 led1 = fsvr.get_byte( H_H_LINK_LSBYTE ) ? 0 : 1;
lorcansmith 0:2a53a4c3238c 905 /* initialise variable binding for f3kIOchange_trap */
lorcansmith 0:2a53a4c3238c 906 io_var.next = io_var.prev = NULL;
lorcansmith 0:2a53a4c3238c 907 io_var.ident_len = io_change_id.len;
lorcansmith 0:2a53a4c3238c 908 io_var.ident = io_oid;
lorcansmith 0:2a53a4c3238c 909 do
lorcansmith 0:2a53a4c3238c 910 {
lorcansmith 0:2a53a4c3238c 911 io_oid[ i ] = io_change_id.id[ i ];
lorcansmith 0:2a53a4c3238c 912 } while ( i++ < io_change_id.len );
lorcansmith 0:2a53a4c3238c 913 io_var.value_type = (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE);
lorcansmith 0:2a53a4c3238c 914 io_var.value_len = sizeof(u32_t);
lorcansmith 0:2a53a4c3238c 915 io_var.value = &io_chng_val;
lorcansmith 0:2a53a4c3238c 916 /* initialise variable binding for f3kIOalarm_trap al_var data */
lorcansmith 0:2a53a4c3238c 917 al_var.prev = NULL; // first in list
lorcansmith 0:2a53a4c3238c 918 al_var.next = &io_alarm; // NULL if only varbind in list
lorcansmith 0:2a53a4c3238c 919 al_var.ident_len = io_change_id.len;
lorcansmith 0:2a53a4c3238c 920 al_var.ident = io_oid;
lorcansmith 0:2a53a4c3238c 921 al_var.value_type = (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE);
lorcansmith 0:2a53a4c3238c 922 al_var.value_len = sizeof(u32_t);
lorcansmith 0:2a53a4c3238c 923 al_var.value = &new_io_val[ 0 ];
lorcansmith 0:2a53a4c3238c 924 /* initialise variable binding for f3kIOalarm_trap string */
lorcansmith 0:2a53a4c3238c 925 io_alarm.prev = &al_var; // after al_var binding
lorcansmith 0:2a53a4c3238c 926 io_alarm.next = NULL; // always last in list ... al_string_id
lorcansmith 0:2a53a4c3238c 927 io_alarm.ident_len = io_alarm_id.len;
lorcansmith 0:2a53a4c3238c 928 io_alarm.ident = als_oid;
lorcansmith 0:2a53a4c3238c 929 for ( i = 0; i < io_alarm_id.len; i++ )
lorcansmith 0:2a53a4c3238c 930 {
lorcansmith 0:2a53a4c3238c 931 als_oid[ i ] = io_alarm_id.id[ i ];
lorcansmith 0:2a53a4c3238c 932 }
lorcansmith 0:2a53a4c3238c 933 io_alarm.value_type = (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR);
lorcansmith 0:2a53a4c3238c 934 io_alarm.value_len = MSG_LNGTH;
lorcansmith 0:2a53a4c3238c 935 io_alarm.value = &alarm_msg[ 0 ];
lorcansmith 0:2a53a4c3238c 936 trap_init = true;
lorcansmith 0:2a53a4c3238c 937 }
lorcansmith 0:2a53a4c3238c 938
lorcansmith 0:2a53a4c3238c 939 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 940 /* check_all_traps:
lorcansmith 0:2a53a4c3238c 941 * Description: test states for all trap events and sends appropriate trap
lorcansmith 0:2a53a4c3238c 942 * Globals used: led1: mimics state of head-link status output in 3000 I/O
lorcansmith 0:2a53a4c3238c 943 * led2: set to ON if SPI not active
lorcansmith 0:2a53a4c3238c 944 * byte_changed, all_IO_updated, spi_active
lorcansmith 0:2a53a4c3238c 945 * Parameters: NONE
lorcansmith 0:2a53a4c3238c 946 * Returns: NONE
lorcansmith 0:2a53a4c3238c 947 */
lorcansmith 0:2a53a4c3238c 948 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 949 void check_all_traps( void )
lorcansmith 0:2a53a4c3238c 950 {
lorcansmith 0:2a53a4c3238c 951 unsigned char alm;
lorcansmith 0:2a53a4c3238c 952
lorcansmith 0:2a53a4c3238c 953 if ( !led1 && (fsvr.get_byte( H_H_LINK_LSBYTE ) || fsvr.get_byte( H_H_LINK_MSBYTE )) )
lorcansmith 0:2a53a4c3238c 954 { // change of link state
lorcansmith 0:2a53a4c3238c 955 led1 = 1; // flag: link is up when led1 on
lorcansmith 0:2a53a4c3238c 956 snmp_linkup_trap();
lorcansmith 0:2a53a4c3238c 957 }
lorcansmith 0:2a53a4c3238c 958 else if ( (fsvr.get_byte( H_H_LINK_LSBYTE ) == 0) && (fsvr.get_byte( H_H_LINK_MSBYTE ) == 0) && led1 )
lorcansmith 0:2a53a4c3238c 959 { // change of link state
lorcansmith 0:2a53a4c3238c 960 led1 = 0;
lorcansmith 0:2a53a4c3238c 961 snmp_linkdown_trap();
lorcansmith 0:2a53a4c3238c 962 }
lorcansmith 0:2a53a4c3238c 963 if ( all_IO_updated && byte_changed )
lorcansmith 0:2a53a4c3238c 964 { // any masked-in I/O register changed
lorcansmith 0:2a53a4c3238c 965 io_oid[ IO_INDEX ] = io_chng_idx;
lorcansmith 0:2a53a4c3238c 966 io_var.value = &io_chng_val;
lorcansmith 0:2a53a4c3238c 967 f3kIOchange_trap();
lorcansmith 0:2a53a4c3238c 968 all_IO_updated = byte_changed = false;
lorcansmith 0:2a53a4c3238c 969 }
lorcansmith 0:2a53a4c3238c 970 if ( spi_active && led2 )
lorcansmith 0:2a53a4c3238c 971 {
lorcansmith 0:2a53a4c3238c 972 led2 = 0; // flag: SPI is up when led2 off
lorcansmith 0:2a53a4c3238c 973 snmp_SPIup_trap();
lorcansmith 0:2a53a4c3238c 974 }
lorcansmith 0:2a53a4c3238c 975 else if ( !spi_active && !led2 )
lorcansmith 0:2a53a4c3238c 976 {
lorcansmith 0:2a53a4c3238c 977 led2 = 1; // flag: SPI is down when led2 on
lorcansmith 0:2a53a4c3238c 978 snmp_SPIdown_trap();
lorcansmith 0:2a53a4c3238c 979 }
lorcansmith 0:2a53a4c3238c 980 /* send trap for any changed I/O alarm */
lorcansmith 0:2a53a4c3238c 981 for ( alm = 0; alm < NUM_MASK; alm++ )
lorcansmith 0:2a53a4c3238c 982 {
lorcansmith 0:2a53a4c3238c 983 if ( reg_change[ alm ] )
lorcansmith 0:2a53a4c3238c 984 { // any alarmed I/O register changed
lorcansmith 0:2a53a4c3238c 985 io_oid[ IO_INDEX ] = reg_change[ alm ];
lorcansmith 0:2a53a4c3238c 986 al_var.value = &new_io_val[ alm ];
lorcansmith 0:2a53a4c3238c 987 als_oid[ IO_INDEX ] = alm + 1;
lorcansmith 0:2a53a4c3238c 988 io_alarm.value = &alarm_msg[ alm ];
lorcansmith 0:2a53a4c3238c 989 io_alarm.value_len = strlen( (const char * )alarm_msg[ alm ] );
lorcansmith 0:2a53a4c3238c 990 f3kIOalarm_trap();
lorcansmith 0:2a53a4c3238c 991 reg_change[ alm ] = 0;
lorcansmith 0:2a53a4c3238c 992 }
lorcansmith 0:2a53a4c3238c 993 }
lorcansmith 0:2a53a4c3238c 994 }
lorcansmith 0:2a53a4c3238c 995
lorcansmith 0:2a53a4c3238c 996
lorcansmith 0:2a53a4c3238c 997 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 998 /****************************************************************************/
lorcansmith 0:2a53a4c3238c 999 int main() {
lorcansmith 0:2a53a4c3238c 1000 int idx;
lorcansmith 0:2a53a4c3238c 1001
lorcansmith 0:2a53a4c3238c 1002 new_cmd_idx = 0xaa;
lorcansmith 0:2a53a4c3238c 1003 for ( idx = 0; idx < MAX_CMD; idx++ )
lorcansmith 0:2a53a4c3238c 1004 { // put meaningful non-valid data in cmd_buf
lorcansmith 0:2a53a4c3238c 1005 cmd_buf[ idx ] = new_cmd_idx;
lorcansmith 0:2a53a4c3238c 1006 }
lorcansmith 0:2a53a4c3238c 1007 /* v1.04: clear all alarm trap masks */
lorcansmith 0:2a53a4c3238c 1008 for ( idx = 0; idx < NUM_MASK; idx++ )
lorcansmith 0:2a53a4c3238c 1009 {
lorcansmith 0:2a53a4c3238c 1010 reg_change[ idx ] = 0; // clear all I/O register changed flags
lorcansmith 0:2a53a4c3238c 1011 io_mask[ idx ] = 0; // clear masks for alarms on IO registers
lorcansmith 0:2a53a4c3238c 1012 }
lorcansmith 0:2a53a4c3238c 1013 pc.printf("=================== Version %s ====================\r\n", snmp_fw_version);
lorcansmith 0:2a53a4c3238c 1014 // pc.printf("Press any key to start...\r\n");
lorcansmith 0:2a53a4c3238c 1015 led3 = 1;
lorcansmith 0:2a53a4c3238c 1016
lorcansmith 0:2a53a4c3238c 1017 FSHandler::mount("/webfs", "/files"); // Mount /webfs path on /files web path
lorcansmith 0:2a53a4c3238c 1018 FSHandler::mount("/webfs", "/"); // Mount /webfs path on web root path
lorcansmith 0:2a53a4c3238c 1019 read_addrs_from_file();
lorcansmith 0:2a53a4c3238c 1020 read_alarm_from_file( NUM_MASK ); // read all I/O masks
lorcansmith 0:2a53a4c3238c 1021
lorcansmith 0:2a53a4c3238c 1022 /* Force fixed Ethernet address */
lorcansmith 0:2a53a4c3238c 1023 EthernetNetIf eth(
lorcansmith 0:2a53a4c3238c 1024 IpAddr( ip_ad[0], ip_ad[1], ip_ad[2], ip_ad[3] ), //IP Address
lorcansmith 0:2a53a4c3238c 1025 IpAddr( msk_ad[0], msk_ad[1], msk_ad[2], msk_ad[3] ), //Network Mask
lorcansmith 0:2a53a4c3238c 1026 IpAddr( gwy_ad[0], gwy_ad[1], gwy_ad[2], gwy_ad[3] ), //Gateway
lorcansmith 0:2a53a4c3238c 1027 IpAddr( dns_ad[0], dns_ad[1], dns_ad[2], dns_ad[3] ) //DNS Server
lorcansmith 0:2a53a4c3238c 1028 );
lorcansmith 0:2a53a4c3238c 1029
lorcansmith 0:2a53a4c3238c 1030 Base::add_rpc_class<DigitalOut>();
lorcansmith 0:2a53a4c3238c 1031 #if 0
lorcansmith 0:2a53a4c3238c 1032 Base::add_rpc_class<SPI_Server>();
lorcansmith 0:2a53a4c3238c 1033 #endif
lorcansmith 0:2a53a4c3238c 1034
lorcansmith 0:2a53a4c3238c 1035 // pc.getc(); // wait for keyboard
lorcansmith 0:2a53a4c3238c 1036 led3 = 0;
lorcansmith 0:2a53a4c3238c 1037
lorcansmith 0:2a53a4c3238c 1038 strcpy( (char *)syslocation, (const char *)syslocation_default );
lorcansmith 0:2a53a4c3238c 1039 sysloc_len = strlen( (const char *)syslocation );
lorcansmith 0:2a53a4c3238c 1040 snmp_set_syslocation( syslocation, &sysloc_len );
lorcansmith 0:2a53a4c3238c 1041
lorcansmith 0:2a53a4c3238c 1042 pc.printf("\r\nSetting up...\r\n");
lorcansmith 0:2a53a4c3238c 1043 EthernetErr ethErr = eth.setup();
lorcansmith 0:2a53a4c3238c 1044 if( ethErr )
lorcansmith 0:2a53a4c3238c 1045 {
lorcansmith 0:2a53a4c3238c 1046 pc.printf("Error %d in Ethernet setup.\r\n", ethErr);
lorcansmith 0:2a53a4c3238c 1047 // return -1;
lorcansmith 0:2a53a4c3238c 1048 }
lorcansmith 0:2a53a4c3238c 1049 else // Ethernet Setup OK
lorcansmith 0:2a53a4c3238c 1050 {
lorcansmith 0:2a53a4c3238c 1051 mip = eth.getIp();
lorcansmith 0:2a53a4c3238c 1052 led3 = 1;
lorcansmith 0:2a53a4c3238c 1053 led_speed = LPC_GPIO1->FIOPIN & (1<<26); // GPIO1.26 = "LED_SPEED".
lorcansmith 0:2a53a4c3238c 1054 led_link = LPC_GPIO1->FIOPIN & (1<<25); // GPIO1.25 = "LED_LINK".
lorcansmith 0:2a53a4c3238c 1055 pc.printf("\r\nEthernet Setup OK\r\n");
lorcansmith 0:2a53a4c3238c 1056
lorcansmith 0:2a53a4c3238c 1057 svr.addHandler<RPCHandler>("/rpc");
lorcansmith 0:2a53a4c3238c 1058 svr.addHandler<FSHandler>("/files");
lorcansmith 0:2a53a4c3238c 1059 svr.addHandler<FSHandler>("/"); //Default handler
lorcansmith 0:2a53a4c3238c 1060 //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
lorcansmith 0:2a53a4c3238c 1061
lorcansmith 0:2a53a4c3238c 1062 // svr.bind(80); disable for SNMP development
lorcansmith 0:2a53a4c3238c 1063
lorcansmith 0:2a53a4c3238c 1064 // snmp_init(); done in lwip/core/init.c
lorcansmith 0:2a53a4c3238c 1065
lorcansmith 0:2a53a4c3238c 1066 pc.printf("Listening...\n\r");
lorcansmith 0:2a53a4c3238c 1067
lorcansmith 0:2a53a4c3238c 1068 snmp_trap_dst_ip_set( ONLY_TRAP, (ip_addr_t *)&th_ad[0] );
lorcansmith 0:2a53a4c3238c 1069 snmp_trap_dst_enable( ONLY_TRAP, ENABLED );
lorcansmith 0:2a53a4c3238c 1070 snmp_coldstart_trap(); // to force ARP call
lorcansmith 0:2a53a4c3238c 1071
lorcansmith 0:2a53a4c3238c 1072 led3 = led4 = 0;
lorcansmith 0:2a53a4c3238c 1073 spi_slave.format(8,3); // Setup: byte data, high steady state clock, 2nd edge capture
lorcansmith 0:2a53a4c3238c 1074
lorcansmith 0:2a53a4c3238c 1075 sys_tick.attach( &snmp_inc_sysuptime, 0.01 );
lorcansmith 0:2a53a4c3238c 1076 spi_tick.attach( &chk_SPI_active, 120.0 );
lorcansmith 0:2a53a4c3238c 1077 comms_tick.attach( &blinker, 0.07 ); // tick to blink activity LED
lorcansmith 0:2a53a4c3238c 1078 cold_start_delay.attach( &init_traps, 30.0 ); // send cold_start trap after 30 seconds
lorcansmith 0:2a53a4c3238c 1079
lorcansmith 0:2a53a4c3238c 1080
lorcansmith 0:2a53a4c3238c 1081 // Listen indefinitely to Ethernet and SPI
lorcansmith 0:2a53a4c3238c 1082 while ( true )
lorcansmith 0:2a53a4c3238c 1083 {
lorcansmith 0:2a53a4c3238c 1084 Net::poll(); // check for Ethernet requests
lorcansmith 0:2a53a4c3238c 1085
lorcansmith 0:2a53a4c3238c 1086 if ( trap_init )
lorcansmith 0:2a53a4c3238c 1087 {
lorcansmith 0:2a53a4c3238c 1088 check_all_traps();
lorcansmith 0:2a53a4c3238c 1089 }
lorcansmith 0:2a53a4c3238c 1090
lorcansmith 0:2a53a4c3238c 1091 if( spi_slave.receive() ) {
lorcansmith 0:2a53a4c3238c 1092 // Data available - needs chip select line to activate
lorcansmith 0:2a53a4c3238c 1093 get_SPI_data( cmd_buf );
lorcansmith 0:2a53a4c3238c 1094 } // end if( spi_slave.receive() )
lorcansmith 0:2a53a4c3238c 1095 if ( (net_addrs_chng == 2) || (net_addrs_chng == 1) )
lorcansmith 0:2a53a4c3238c 1096 { // v0.93: address changed in 3000 CPU
lorcansmith 0:2a53a4c3238c 1097 copy_net_addresses_from( fsvr.net_address_ptr() );
lorcansmith 0:2a53a4c3238c 1098 copy_trap_host_address( fsvr.trap_host_ptr() );
lorcansmith 0:2a53a4c3238c 1099 write_addrs_to_file();
lorcansmith 0:2a53a4c3238c 1100 }
lorcansmith 0:2a53a4c3238c 1101 if ( net_addrs_set )
lorcansmith 0:2a53a4c3238c 1102 { // v0.99: address changed from SNMP i/f
lorcansmith 0:2a53a4c3238c 1103 write_addrs_to_file();
lorcansmith 0:2a53a4c3238c 1104 net_addrs_set = false;
lorcansmith 0:2a53a4c3238c 1105 }
lorcansmith 0:2a53a4c3238c 1106 if ( alarm_str_set )
lorcansmith 0:2a53a4c3238c 1107 {
lorcansmith 0:2a53a4c3238c 1108 write_alarms_to_file();
lorcansmith 0:2a53a4c3238c 1109 alarm_str_set = false;
lorcansmith 0:2a53a4c3238c 1110 }
lorcansmith 0:2a53a4c3238c 1111 } // end while ( true )
lorcansmith 0:2a53a4c3238c 1112
lorcansmith 0:2a53a4c3238c 1113 } // else Ethernet Setup OK
lorcansmith 0:2a53a4c3238c 1114 return 0;
lorcansmith 0:2a53a4c3238c 1115 }