\subsection{SICS Sample Environment  Handling}
In addition to driving motors and counting SICS has to take care of sample
environment devices as well. These can be temperature controllers, current
controllers etc. It can be assumed, that SICS has to take care of
intelligent sample environment devices only, i. e. devices which do their
own realtime processing. SICS, however, has to implement the logic to drive
an environment controller to a new value. Furthermore SICS has to do the
error handling when an environment controller fails. In order to do this the
following software components will be needed:
\begin{itemize}
\item An environment monitor. This component will register any active
environment control device. A test function will be called from the main
loop which checks for controller out of range and initiates error handling.
\item A new interface, the environment interface (see \ref{interface}).
\item Controller objects. One for each environment controller. Again, the
SICS distinction between logical objects and hardware drivers will be
maintained.
\end{itemize}

Controller objects will be able to share some code among them. Each
environemnt controller will hold a target value, a permissable tolerance,
 a selctor variable which defines the type of error handling to perform and
 an interrupt code to issue when that form of error handling is choosen.
Furthermore each environment controller needs to implement the drivable
interface (for driving to new values) and the environment interface (for
 monitoring the controller).   Controller object will also share the error
handling during the monitor phase between them. The scheme is that each controller will have a
variable which selects the error handling mechanism. Depending on this
variable an error managment routine will be invoked. Current two schemes for
handling errors are supported:
\begin{itemize}
\item Print a warning but do nothing else.
\item Interrupt: an interrupt will be issued.
\item Pause: the measurement will be paused.
\item Try to drive to a safe value. This may be used to rescue a oven
gone haywire by setting a low setpoint where the device is safe. 
\end{itemize}
This scheme allows for adding more error handling strategies when necessary
and to implement them in one place for all environment controllers. 
Consequently, there will be some some general controller code in files
evcontrol.*. In the object oriented paradigm environment controller would
inherit from this general controller. This is implemented by delegation in
SICS.

Please note, that any environment device has three modes of
operation. The first, trivial one, is idle. The second one is the driving
phase. This phase is entered when a drive or run command tries to set the
controller to a new value. Once this target has been reached a environment
controller will be registered in the environment monitor which will take
care of monitoring the controller during its operation. This is the
third stage: inMonitor. 


\subsection{The Environment Monitor}
The environment monitors (emon) task is to register all active environment
controllers. It will be called from the main loop to check if each
registered environment controller is still within tolerances. If not, emon
has to enforce error processing. Else it does nothing. 

The environment monitor uses the following datastructures:
\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap1}
$\langle$emondat {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@   typedef struct __EnvMon {@\\
\mbox{}\verb@                            pObjectDescriptor pDes;@\\
\mbox{}\verb@                            int iList;@\\
\mbox{}\verb@                            int iEnd;@\\
\mbox{}\verb@                            } EnvMon;@\\
\mbox{}\verb@/*---------------------------------------------------------------------------*/@\\
\mbox{}\verb@   typedef struct {@\\
\mbox{}\verb@                    pDummy pDum;@\\
\mbox{}\verb@                    char *pName;@\\
\mbox{}\verb@                    pEVInterface pInter;@\\
\mbox{}\verb@                  } EVEntry, *pEVEntry;     @\\
\mbox{}\verb@@\\
\mbox{}\verb@@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
As it it will be accessible as SICS object emon needs an object descriptor.
The other data member is a list of active controllers.
 
The EVEntry structure contains the data held in the list for each active
controller. It is, disguised as pDum, a pointer to the controllers
datastructure, the name of the controller, and the environment interface of
the controller. 

The following interface is implemented by the environment monitor:
\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap2}
$\langle$emonint {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@@\\
\mbox{}\verb@   pEnvMon CreateEnvMon(void);@\\
\mbox{}\verb@   void    DeleteEnvMon(void *pData);@\\
\mbox{}\verb@@\\
\mbox{}\verb@   int EVRegisterController(pEnvMon self, char *pName, void *pData,@\\
\mbox{}\verb@                            SConnection *pCon);@\\
\mbox{}\verb@   int EVUnregister(pEnvMon self, char *name);@\\
\mbox{}\verb@   @\\
\mbox{}\verb@   int EVMonitorControllers(pEnvMon self);@\\
\mbox{}\verb@@\\
\mbox{}\verb@   int EVList(pEnvMon self, SConnection *pCon);@\\
\mbox{}\verb@@\\
\mbox{}\verb@   int EVWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,@\\
\mbox{}\verb@                 int argc, char *argv[]);@\\
\mbox{}\verb@@\\
\mbox{}\verb@   pEnvMon FindEMON(SicsInterp *pSics);@\\
\mbox{}\verb@   @\\
\mbox{}\verb@   int EnvMonTask(void *pEv);@\\
\mbox{}\verb@   void EnvMonSignal(void *pEndv, int iSignal, void *pSigData);@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
{\bf CreateEnvMon} initialises a emon data structure. As usual it returns
NULL on error, a pointer to a new structure on success.

{\bf DeleteEnvMon} deletes and emon data structure and frees all memory
associated with it. 

{\bf EVRegisterController} registers a controller for monitoring. The
parameters are:
\begin{itemize}
\item {\bf self}. A pointer to the environment monitor where to register the
controller.
\item {\bf pName}. The name of the controller object.
\item {\bf pData}. A pointer to the data structure belonging to the
controller object.
\item {\bf pCon}. A pointer to the client connection to which erros shall be
reported.
\end{itemize}

{\bf EVUnregister} Removes the controller name from the monitoring list.

{\bf EVMonitorControllers} is the function which will be called from the
main loop and which is responsible for monitoring the registered
controllers.


{\bf EVList} prints a status report about the emon to the Connection object
specified. 

{\bf EVWrapper} is the wrapper function necessary to make the emon
available from SICS. The user may interact with the emon through SICS. The
user will be enabled to list the currently registered environment
controllers and their current state and to register and unnregister
controllers.

{\bf EnvMonTask} is the task function associated with the environment
monitor. This is called by the task handling object and eventually
invokes EVMonitorControllers for checking on the environment
controllers configured into SICS. 

{\bf EnvMonSignal} is the signale handler for the environment monitor task.

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap3}
\verb@"emon.h"@ {\footnotesize ? }$\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*--------------------------------------------------------------------------@\\
\mbox{}\verb@                E N V I R O N M E N T   M O N I T O R@\\
\mbox{}\verb@@\\
\mbox{}\verb@  The environment monitor is responsible for monitoring active environment@\\
\mbox{}\verb@  devices. @\\
\mbox{}\verb@@\\
\mbox{}\verb@  Mark Koennecke, Juli 1997@\\
\mbox{}\verb@@\\
\mbox{}\verb@  copyright: see implementation file.@\\
\mbox{}\verb@  doc: programmers reference.@\\
\mbox{}\verb@----------------------------------------------------------------------------*/@\\
\mbox{}\verb@#ifndef SICSEMON@\\
\mbox{}\verb@#define SICSEMON@\\
\mbox{}\verb@  typedef struct __EnvMon *pEnvMon;@\\
\mbox{}\verb@/*-------------------------------------------------------------------------*/@\\
\mbox{}\verb@@$\langle$emonint {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@#endif@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-2ex}
\end{minipage}\\[4ex]
\end{flushleft}
\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap4}
\verb@"emon.i"@ {\footnotesize ? }$\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*--------------------------------------------------------------------------@\\
\mbox{}\verb@   Internal header file defining the datastructure for the environment@\\
\mbox{}\verb@   controller.@\\
\mbox{}\verb@@\\
\mbox{}\verb@   Mark Koennecke, Juli 1997@\\
\mbox{}\verb@---------------------------------------------------------------------------*/@\\
\mbox{}\verb@@$\langle$emondat {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-2ex}
\end{minipage}\\[4ex]
\end{flushleft}
