Commits

Anonymous committed 226adb5

Kim - System time watchdog

  • Participants

Comments (0)

Files changed (11)

File AlarmUnit.cpp

+//---------------------------------------------------------------------------
+#include <vcl.h>
+#pragma hdrstop
+
+#include "AlarmUnit.h"
+
+//---------------------------------------------------------------------------
+TAlarmThread::TAlarmThread() : TThread(false)
+{
+  this->stop = false;
+  this->frequency = 125;
+}
+//---------------------------------------------------------------------------
+void TAlarmThread::Stop()
+{
+  this->stop = true;
+}
+//---------------------------------------------------------------------------
+__fastcall TAlarmThread::~TAlarmThread()
+{
+}
+//---------------------------------------------------------------------------
+void __fastcall TAlarmThread::Execute()
+{
+  do {
+    unsigned int i;
+    for ( i = 100U; i < 2500U; i+=25 ) {
+      this->frequency = i;
+      Beep(
+        this->frequency,
+        30
+      );
+      if ( stop ) break;
+    }
+    for ( i = this->frequency; i > 100U; i -= 25 ) {
+      this->frequency = i;
+      Beep(
+        this->frequency,
+        40
+      );
+      if ( stop ) break;
+    }
+  } while ( !stop );
+}
+//---------------------------------------------------------------------------
+#pragma package(smart_init)
+#ifndef AlarmUnitH
+#define AlarmUnitH
+//---------------------------------------------------------------------------
+class TAlarmThread : public TThread
+{
+  protected:
+    bool stop;
+    unsigned int frequency;
+  public:
+    TAlarmThread();
+    void Stop();
+    __fastcall virtual ~TAlarmThread();
+    __fastcall virtual void Execute();
+};
+//---------------------------------------------------------------------------
+#endif

File ClockUnit.cpp

+#include <windows.h>
+#include <vcl.h>
+#include <time.h>
+#pragma hdrstop
+
+#include "ClockUnit.h"
+
+//---------------------------------------------------------------------------
+TWatchdogClock::TWatchdogClock()
+{
+  unsigned int i;
+  time_t currentTime;
+
+  // Init the array with valid times to avoid a false alarm
+  time(&currentTime);
+  for (
+    i = 0U;
+    i < sizeof(this->timeArray)/sizeof(this->timeArray[0]);
+    i++
+  ) {
+    this->timeArray[i] = currentTime;
+  }
+}
+//---------------------------------------------------------------------------
+TWatchdogClock::~TWatchdogClock()
+{
+}
+//---------------------------------------------------------------------------
+void TWatchdogClock::Shift()
+{
+  unsigned int i;
+  for (
+    i = 0U;
+    i < sizeof(this->timeArray)/sizeof(this->timeArray[0])-1;
+    i++
+  ) {
+    this->timeArray[i] = this->timeArray[i+1];
+  }
+}
+//---------------------------------------------------------------------------
+void TWatchdogClock::Add(const time_t *PTime)
+{
+  this->Shift();
+  this->timeArray[
+    sizeof(this->timeArray)/sizeof(this->timeArray[0])-1
+  ] = *PTime;
+}
+//---------------------------------------------------------------------------
+bool TWatchdogClock::Check()
+{
+  unsigned int i;
+  time_t lastValue = this->timeArray[0];
+
+  for (
+    i = 1U; // Skip element zero
+    i < sizeof(this->timeArray)/sizeof(this->timeArray[0]);
+    i++
+  ) {
+    unsigned int diff = max(lastValue, this->timeArray[i]) -
+      min(lastValue, this->timeArray[i]);
+
+    if ( diff > 7200 )
+      return false; // Differing by more than two hours
+    lastValue = this->timeArray[i];
+  }
+  return true;
+}
+//---------------------------------------------------------------------------
+#pragma package(smart_init)
+#ifndef ClockUnitH
+#define ClockUnitH
+//---------------------------------------------------------------------------
+class TWatchdogClock {
+  protected:
+    time_t timeArray[10];
+    void Shift();
+  public:
+    TWatchdogClock();
+    ~TWatchdogClock();
+    void Add(const time_t *PTime);
+    bool Check();
+};
+//---------------------------------------------------------------------------
+#endif

File StatusUnit.cpp

+/*
+Copyright (C) 2001-2005 Daybo Logic.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+3. Neither the name of the project nor the names of its contributors
+   may be used to endorse or promote products derived from this software
+   without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+*/
+//---------------------------------------------------------------------------
+#include <vcl.h>
+#include <time.h>
+#pragma hdrstop
+
+#include "AlarmUnit.h"
+#include "ClockUnit.h"
+#include "StatusUnit.h"
+//---------------------------------------------------------------------------
+#pragma package(smart_init)
+#pragma resource "*.dfm"
+TStatusForm *StatusForm;
+//---------------------------------------------------------------------------
+__fastcall TStatusForm::TStatusForm(TComponent* Owner)
+        : TForm(Owner),
+          alarm(NULL)
+{
+}
+//---------------------------------------------------------------------------
+__fastcall TStatusForm::~TStatusForm()
+{
+  if ( this->alarm ) {
+    this->alarm->Stop();
+    this->alarm->Terminate();
+    delete this->alarm;
+    this->alarm = NULL;
+  }
+}
+//---------------------------------------------------------------------------
+void TStatusForm::SetStatus(bool OK)
+{
+  if ( OK ) {
+    if ( StatusLabel->Tag != 1 ) {
+      StatusLabel->Color = clGreen;
+      StatusLabel->Tag = 1;
+      StatusLabelOverlay->Caption = "No faults detected";
+      PasswordEdit->Color = clBtnFace;
+      PasswordEdit->Enabled = false;
+    }
+  }
+  else {
+    if ( StatusLabel->Tag != 0 ) {
+      StatusLabel->Color = clRed;
+      StatusLabel->Tag = 0;
+      StatusLabelOverlay->Caption = "Fault detected";
+      PasswordEdit->Color = clWindow;
+      PasswordEdit->Enabled = true;
+    }
+  }
+
+  // Ensure label and overlay colours match
+  if ( StatusLabelOverlay->Color != StatusLabel->Color )
+    StatusLabelOverlay->Color = StatusLabel->Color;
+}
+//---------------------------------------------------------------------------
+void __fastcall TStatusForm::CheckClockTimerTimer(TObject *Sender)
+{
+  time_t currentTime;
+  bool check;
+
+  time(&currentTime);
+  this->watchdogClock.Add(&currentTime);
+  check = this->watchdogClock.Check();
+  this->SetStatus(check);
+  if ( !check ) {
+    if ( !this->alarm )
+      this->alarm = new TAlarmThread();
+  }
+}
+//---------------------------------------------------------------------------

File StatusUnit.dfm

Binary file added.

File StatusUnit.h

+//---------------------------------------------------------------------------
+#ifndef StatusUnitH
+#define StatusUnitH
+//---------------------------------------------------------------------------
+#include <Classes.hpp>
+#include <Controls.hpp>
+#include <StdCtrls.hpp>
+#include <Forms.hpp>
+#include <ExtCtrls.hpp>
+//---------------------------------------------------------------------------
+class TStatusForm : public TForm
+{
+__published:	// IDE-managed Components
+        TLabel *StatusLabel;
+        TLabel *StatusLabelOverlay;
+        TTimer *CheckClockTimer;
+        TLabel *Label1;
+        TEdit *PasswordEdit;
+        void __fastcall CheckClockTimerTimer(TObject *Sender);
+private:	// User declarations
+        TAlarmThread *alarm;
+        TWatchdogClock watchdogClock;
+        void SetStatus(bool OK);
+public:		// User declarations
+        __fastcall TStatusForm(TComponent* Owner);
+        __fastcall virtual ~TStatusForm(void);
+};
+//---------------------------------------------------------------------------
+extern PACKAGE TStatusForm *StatusForm;
+//---------------------------------------------------------------------------
+#endif

File alarm.ico

Added
New image
+# ---------------------------------------------------------------------------
+!if !$d(BCB)
+BCB = $(MAKEDIR)\..
+!endif
+
+# ---------------------------------------------------------------------------
+# IDE SECTION
+# ---------------------------------------------------------------------------
+# The following section of the project makefile is managed by the BCB IDE.
+# It is recommended to use the IDE to change any of the values in this
+# section.
+# ---------------------------------------------------------------------------
+
+VERSION = BCB.04.04
+# ---------------------------------------------------------------------------
+PROJECT = kim.exe
+OBJFILES = kim.obj StatusUnit.obj ClockUnit.obj AlarmUnit.obj
+RESFILES = kim.res
+RESDEPEN = $(RESFILES) StatusUnit.dfm
+LIBFILES =
+LIBRARIES = Vcl40.lib
+SPARELIBS = Vcl40.lib
+PACKAGES = Vcl40.bpi Vclx40.bpi vcljpg40.bpi bcbsmp40.bpi Qrpt40.bpi Vcldb40.bpi \
+  ibsmp40.bpi vcldbx40.bpi TeeUI40.bpi teedb40.bpi tee40.bpi nmfast40.bpi \
+  dclocx40.bpi
+DEFFILE =
+# ---------------------------------------------------------------------------
+PATHCPP = .;
+PATHPAS = .;
+PATHASM = .;
+PATHRC = .;
+DEBUGLIBPATH = $(BCB)\lib\debug
+RELEASELIBPATH = $(BCB)\lib\release
+USERDEFINES =
+SYSDEFINES = NO_STRICT
+# ---------------------------------------------------------------------------
+CFLAG1 = -I"D:\Program Files\Borland\CBuilder4\Projects\";$(BCB)\include;$(BCB)\include\vcl \
+  -O2 -Hc -H=$(BCB)\lib\vcl40.csm -w -Ve -a8 -4 -k- -vi -c -b- -w-par -w-inl -Vx \
+  -tW -tWM -D$(SYSDEFINES);$(USERDEFINES)
+PFLAGS = -U"D:\Program Files\Borland\CBuilder4\Projects\";$(BCB)\lib\obj;$(BCB)\lib;$(RELEASELIBPATH) \
+  -I"D:\Program Files\Borland\CBuilder4\Projects\";$(BCB)\include;$(BCB)\include\vcl \
+  -$Y- -$U -$L- -$D- -v -JPHNE -M
+RFLAGS = -i"D:\Program Files\Borland\CBuilder4\Projects\";$(BCB)\include;$(BCB)\include\vcl
+AFLAGS = /i"D:\Program Files\Borland\CBuilder4\Projects" /i$(BCB)\include \
+  /i$(BCB)\include\vcl /mx /w2 /zn
+LFLAGS = -L"D:\Program Files\Borland\CBuilder4\Projects\";$(BCB)\lib\obj;$(BCB)\lib;$(RELEASELIBPATH) \
+  -aa -Tpe -x -Gn
+# ---------------------------------------------------------------------------
+ALLOBJ = c0w32.obj sysinit.obj $(OBJFILES)
+ALLRES = $(RESFILES)
+ALLLIB = $(LIBFILES) $(LIBRARIES) import32.lib cp32mt.lib
+# ---------------------------------------------------------------------------
+!ifdef IDEOPTIONS
+
+[Version Info]
+IncludeVerInfo=0
+AutoIncBuild=0
+MajorVer=1
+MinorVer=0
+Release=0
+Build=0
+Debug=0
+PreRelease=0
+Special=0
+Private=0
+DLL=0
+Locale=2057
+CodePage=1252
+
+[Version Info Keys]
+CompanyName=
+FileDescription=
+FileVersion=1.0.0.0
+InternalName=
+LegalCopyright=
+LegalTrademarks=
+OriginalFilename=
+ProductName=
+ProductVersion=1.0.0.0
+Comments=
+
+[HistoryLists\hlIncludePath]
+Count=1
+Item0=D:\Program Files\Borland\CBuilder4\Projects;$(BCB)\include;$(BCB)\include\vcl
+
+[HistoryLists\hlLibraryPath]
+Count=1
+Item0=D:\Program Files\Borland\CBuilder4\Projects;$(BCB)\lib\obj;$(BCB)\lib
+
+[HistoryLists\hlDebugSourcePath]
+Count=1
+Item0=$(BCB)\source\vcl
+
+[Debugging]
+DebugSourceDirs=$(BCB)\source\vcl
+
+[Parameters]
+RunParams=
+HostApplication=
+RemoteHost=
+RemotePath=
+RemoteDebug=0
+
+[Compiler]
+InMemoryExe=0
+ShowInfoMsgs=0
+
+!endif
+
+# ---------------------------------------------------------------------------
+# MAKE SECTION
+# ---------------------------------------------------------------------------
+# This section of the project file is not used by the BCB IDE.  It is for
+# the benefit of building from the command-line using the MAKE utility.
+# ---------------------------------------------------------------------------
+
+.autodepend
+# ---------------------------------------------------------------------------
+!if !$d(BCC32)
+BCC32 = bcc32
+!endif
+
+!if !$d(CPP32)
+CPP32 = cpp32
+!endif
+
+!if !$d(DCC32)
+DCC32 = dcc32
+!endif
+
+!if !$d(TASM32)
+TASM32 = tasm32
+!endif
+
+!if !$d(LINKER)
+LINKER = ilink32
+!endif
+
+!if !$d(BRCC32)
+BRCC32 = brcc32
+!endif
+
+# ---------------------------------------------------------------------------
+!if $d(PATHCPP)
+.PATH.CPP = $(PATHCPP)
+.PATH.C   = $(PATHCPP)
+!endif
+
+!if $d(PATHPAS)
+.PATH.PAS = $(PATHPAS)
+!endif
+
+!if $d(PATHASM)
+.PATH.ASM = $(PATHASM)
+!endif
+
+!if $d(PATHRC)
+.PATH.RC  = $(PATHRC)
+!endif
+# ---------------------------------------------------------------------------
+$(PROJECT): $(OBJFILES) $(RESDEPEN) $(DEFFILE)
+    $(BCB)\BIN\$(LINKER) @&&!
+    $(LFLAGS) +
+    $(ALLOBJ), +
+    $(PROJECT),, +
+    $(ALLLIB), +
+    $(DEFFILE), +
+    $(ALLRES)
+!
+# ---------------------------------------------------------------------------
+.pas.hpp:
+    $(BCB)\BIN\$(DCC32) $(PFLAGS) {$< }
+
+.pas.obj:
+    $(BCB)\BIN\$(DCC32) $(PFLAGS) {$< }
+
+.cpp.obj:
+    $(BCB)\BIN\$(BCC32) $(CFLAG1) -n$(@D) {$< }
+
+.c.obj:
+    $(BCB)\BIN\$(BCC32) $(CFLAG1) -n$(@D) {$< }
+
+.c.i:
+    $(BCB)\BIN\$(CPP32) $(CFLAG1) -n. {$< }
+
+.cpp.i:
+    $(BCB)\BIN\$(CPP32) $(CFLAG1) -n. {$< }
+
+.asm.obj:
+    $(BCB)\BIN\$(TASM32) $(AFLAGS) $<, $@
+
+.rc.res:
+    $(BCB)\BIN\$(BRCC32) $(RFLAGS) -fo$@ $<
+# ---------------------------------------------------------------------------
+//---------------------------------------------------------------------------
+#include <vcl.h>
+#pragma hdrstop
+USERES("kim.res");
+USEFORM("StatusUnit.cpp", StatusForm);
+USEUNIT("ClockUnit.cpp");
+USEUNIT("AlarmUnit.cpp");
+//---------------------------------------------------------------------------
+WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
+{
+        try
+        {
+                 Application->Initialize();
+                 Application->Title = "Kim - System time watchdog";
+                 Application->CreateForm(__classid(TStatusForm), &StatusForm);
+                 Application->Run();
+        }
+        catch (Exception &exception)
+        {
+                 Application->ShowException(&exception);
+        }
+        return 0;
+}
+//---------------------------------------------------------------------------

File kim.res

Binary file added.