using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; // threading using System.Runtime.InteropServices; // for GetLastError() using AIOWDMNet; // the namespace exposes the AIOWDM Class interface namespace WDMSample { public partial class Form1 : Form { UInt32 Status = 0; // This is the basic data to discover and interface with the card: static int NumCards = 0; static int CardNum = 0; // This sample assumes there is only one card present public UInt32 Offset = 0; // This the offset that will be used based on the base address of the card public bool RunFlag = false; // We will be using classes instead of structures in C# to store the card data: // Card Specific Data Class: public class TCOSData { public TCOSData(byte i) { A = B = C = i; } public byte A, B, C; } public class TCardData { public TCardData(int sizeCOS) { mi = sizeCOS; IsValid = false; IsSelected = false; DeviceID = 0; Base = 0; PPIs = 0; IsWDG = false; IRQCount = 0; LastCOSData = new TCOSData[4]; //$$$ 5 max Log = "No info for this card."; WDGPets = 0; } public int mi; public bool IsValid; public bool IsSelected; public uint DeviceID; public uint Base; public uint PPIs; public bool IsWDG; public uint IRQCount; public TCOSData[] LastCOSData; public String Log; public uint WDGPets; } // Declare class object: public TCardData CardData; // = new TCardData[MAX_CARDS]; // array of 10 max structs // Interval Timer fou WDG updates: // (Note: A Windows Forms timer designed for single threaded environments not a System Timer 55 ms min res) static System.Windows.Forms.Timer myWDGTimer = new System.Windows.Forms.Timer(); public Form1() { InitializeComponent(); // Create the card data array elements: CardData = new TCardData(4); for (int j = 0; j != 4; j++) { CardData.LastCOSData[j] = new TCOSData(0); } // Add the event and the event handler for the method that will // process the WDG timer event to the timer: myWDGTimer.Tick += new EventHandler(WDGTimerEventProcessor); // Set the timer interval in miliseconds and start (55ms min resolution): // start later on demand for WDG: //myWDGTimer.Interval = 1000; } private void Form1_Load(object sender, EventArgs e) { // Test for driver install using an absolute address: if (AIOWDM.InPortB(0x61) == 0xAA55) { MessageBox.Show("ACCESNT.SYS not detected. Please copy ACCESNT.SYS into [NT]/system32/drivers and re-run this sample.", "Warning"); } // This will get all the installed card info: FindCardsWDM(); } private void FindCardsWDM() { bool found = false; // Vars for QueryCardInfo() : UInt32 DeviceID, Base; UInt32 NameSize = 256; UInt16[] Name = new UInt16[256]; String strname; // Init all CardData: CardData.IsValid = false; CardData.IsSelected = false; CardData.DeviceID = 0; CardData.Base = 0; CardData.PPIs = 0; CardData.IRQCount = 0; CardData.WDGPets = 0; CardData.IsWDG = false; for (int j = 0; j < 4; j++) { CardData.LastCOSData[j].A = 0; CardData.LastCOSData[j].B = 0; CardData.LastCOSData[j].C = 0; } RunFlag = true; // Get the total number of cards installed: NumCards = AIOWDM.GetNumCards(); if (NumCards == 0) // no cards present { strname = String.Format("No Card Found"); CardLabel.Text = strname; textBoxMessage.Text = "No cards were found!"; textBoxMessage.Text += "This may mean the card is not installed. "; textBoxMessage.Text += "Check Device Manager for a card and its status"; textBoxMessage.Text += "You may consider rebooting your system."; RunFlag = false; } else { // Validate and store card data: CardData.IsWDG = false; // Get each cards data: Status = AIOWDM.QueryCardInfo(CardNum, out DeviceID, out Base, out NameSize, out strname); // Populate list box with cards found and set flags: switch (DeviceID) { case 0x22C0://PCI-WDG-CSM case 0x25C0: case 0x2FC0: case 0x2FC1: //PCI-WDG-CSM, P104-WDG-E, P104-WDG-CSM, and P104-WDG-CSMA CardLabel.Text = strname; CardData.IsWDG = true; AIOWDM.WDGInit(CardNum); CardData.Base = Base; CardData.IsValid = true; CardData.DeviceID = DeviceID; found = true; break; }//switch // Save and Display card specific text information: if (CardData.PPIs != 0) { CardData.Log = ("There is a COS card present. \r\n"); } else { if (CardData.IsWDG) { CardData.Log = ("This sample provides " + "special functions for watchdog cards like this one.\r\n\r\n" + "Click 'Handle Watchdog IRQ' to handle the next IRQ for the card selected above with the selected action.\r\n\r\n" + "You can also click 'Test Watchdog Timing' to set the watchdog timer, pet it a few times, then let it time out, " + "'Read Temp' to read the temperature(if installed)," + "'Read Status' to read the card''s status byte," + "or 'Reboot' to quickly reboot your computer.\r\n\r\n"); } else { CardData.Log = ("There is a non WDG card present. \r\n"); } } }//end else card present // If card(s) are present but no valid cards were found: if ((NumCards != 0) && (found == false)) { strname = String.Format("No Valid Card Found"); CardLabel.Text = strname; textBoxMessage.Text = "No valid card was found!"; textBoxMessage.Text += "This may mean the card is not installed. "; textBoxMessage.Text += "Check Device Manager for a card and its status"; textBoxMessage.Text += "You may consider rebooting your system."; RunFlag = false; } // Once done set initial default values: if (RunFlag) { // This is the result setting the initial Card Num and offset to be used: CardNum = 0; // default to 0 first card found (before next call) Offset = 0; //default to 0 offset from base for any relative Port read or writes calls } textBoxMessage.Text = CardData.Log; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); } private void btnExit_Click(object sender, EventArgs e) { // Close the form and Application: this.Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Any additional cleanup: } private void btnReboot_Click(object sender, EventArgs e) { // Reboot: DialogResult dr = MessageBox.Show("Are you certain you want to reboot? ", " Emergency Reboot !!! ", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if(dr == DialogResult.Yes) { if (AIOWDM.EmergencyReboot() != 0) textBoxMessage.Text += "Rebooting...\r\n"; else textBoxMessage.Text += "Reboot failed!\r\n"; } } private void btnReadStatus_Click(object sender, EventArgs e) { // Read status: uint temp; temp = AIOWDM.WDGReadStatus(CardNum); labelStatus.Text = temp.ToString("X"); } private void btnReadTemp_Click(object sender, EventArgs e) { // Note: if this hardware feature is not preset This will still read a fixed value: // Read the temperature: double temp; temp = AIOWDM.WDGReadTemp(CardNum); labelTemperature.Text = temp.ToString("F"); } private void btnCancelWDGTiming_Click(object sender, EventArgs e) { AIOWDM.WDGStop(CardNum); myWDGTimer.Stop(); myWDGTimer.Enabled = false; textBoxMessage.Text += "Watchdog timing cancelled.\r\n"; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); } private void btnHandleWDGIRQ_Click(object sender, EventArgs e) { uint WDGAction; if(radioSoft.Checked) WDGAction = AIOWDM.WDG_ACTION_SOFT_RESTART; else if(radioMostlySoft.Checked) WDGAction = AIOWDM.WDG_ACTION_MOSTLY_SOFT_RESTART; else WDGAction = AIOWDM.WDG_ACTION_IGNORE; if(checkBoxDisableWDGTimer.Checked) WDGAction = WDGAction + AIOWDM.WDG_ACTION_DISABLE; AIOWDM.WDGHandleIRQ(CardNum, WDGAction); } private void btnTestWDGTiming_Click(object sender, EventArgs e) { AIOWDM.WDGSetTimeout(CardNum, 2 * 1000, AIOWDM.PCI_WDG_CSM_RATE); //2 sec timeout AIOWDM.WDGSetResetDuration(CardNum, 1, AIOWDM.PCI_WDG_CSM_RATE); //infinite reset //WDGSetResetDuration(CardNum, 250, PCI_WDG_CSM_RATE); //250 ms reset (option) AIOWDM.WDGStart(CardNum); textBoxMessage.Text += "\r\nWatchdog timing started...\r\n"; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); CardData.WDGPets = 0; // reset pet myWDGTimer.Tick -= WDGTimerWaitProcessor; //remove wait handler myWDGTimer.Tick += new EventHandler(WDGTimerEventProcessor); // add event and pet handler myWDGTimer.Interval = 1000; // 1 sec timer myWDGTimer.Enabled = true; myWDGTimer.Start(); } private void WDGTimerEventProcessor(Object myObject, EventArgs myEventArgs) { // This is the 1st method that runs when the WDDG timer event is raised // We will call this a few times to test WDG Pet function // Then drop into a wait function until a WDG timeout is reported if ((AIOWDM.RelInPortB(CardNum, Offset + 4) & 0x01) == 0) { textBoxMessage.Text += "Watchdog timed out early!\r\n"; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); myWDGTimer.Stop(); myWDGTimer.Enabled = false; return; } CardData.WDGPets++; if (CardData.WDGPets >= 4) { textBoxMessage.Text += "Waiting for timeout...\r\n"; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); myWDGTimer.Tick -= WDGTimerEventProcessor; //remove basic event and pet handler myWDGTimer.Tick += new EventHandler(WDGTimerWaitProcessor); // add new wait handler myWDGTimer.Interval = 100; // increase speed now } else { AIOWDM.WDGPet(CardNum); textBoxMessage.Text += "Petting watchdog, pet " + CardData.WDGPets.ToString() + " of 3..." + Environment.NewLine; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); } } private void WDGTimerWaitProcessor(Object myObject, EventArgs myEventArgs) { // This just polls the state of WDG for a timeout: if( (AIOWDM.RelInPortB(CardNum, Offset + 4) & 0x01) == 0) { textBoxMessage.Text += "Watchdog timed out.\r\n"; int num = textBoxMessage.TextLength; textBoxMessage.Select(num, textBoxMessage.TextLength - 1); textBoxMessage.ScrollToCaret(); myWDGTimer.Stop(); myWDGTimer.Enabled = false; } } }// end class }