Merge branch 'master' of git.martlubbers.net:msc-thesis1617
[msc-thesis1617.git] / arch.devices.tex
index cca4449..16e2a50 100644 (file)
@@ -1,44 +1,55 @@
-A device is suitable for the system if it can run the engine.
+A device is suitable for the system as a client if it can run the engine.
 The engine is compiled from one codebase and devices implement (part of) the
 device specific interface. The shared codebase only uses standard \gls{C} and
 no special libraries or tricks are used. Therefore, the code is compilable for
-almost any device or system. Note that it is not needed to implement a full
-interface. The full interface --- excluding the device specific settings --- is
-listed in Appendix~\ref{app:device-interface}.  The interface works in a
-similar fashion as the \gls{EDSL}. Devices do not have to implement all
-functionality, this is analogous to the fact that views do not have to
-implement all type classes in the \gls{EDSL}.  When the device connects with
-the server for the first time, the specifications of what is implemented is
-communicated.
+almost any device or system. The full interface --- excluding the device
+specific settings --- is listed in Appendix~\ref{app:device-interface}. The
+interface works in a similar fashion as the \gls{EDSL}. Devices do not have to
+implement all functionality, this is analogous to the fact that views do not
+have to implement all type classes in the \gls{EDSL}. When the device connects
+with the server for the first time, the specifications of what is implemented
+is communicated. Devices must be available throughout sessions and cannot
+always be kept in scope and therefore they are stored in an \gls{SDS}.
 
 At the time of writing the following device families are supported and can run
-the device software.
+the device software. Porting the client software to a new device does not
+require a lot of work. For example, porting to the \gls{mbed} device family
+only took about an hour.
 \begin{itemize}
        \item \texttt{POSIX} compatible systems connected via the \gls{TCP}.
 
-               This includes systems running \emph{Linux} and \emph{MacOS}.
-       \item The \texttt{STM32} microcontrollers family supported by
-               \texttt{ChibiOS} connected via serial communication.
+               This port only uses functionality from the standard \gls{C} library and
+               therefore runs on \emph{Linux} and \emph{MacOS}.
+       \item Microcontrollers supported by the
+               \gls{mbed}\footnote{\url{https://mbed.com}} environment.
 
                This is tested in particular on the \texttt{STM32f7x} series \gls{ARM}
                development board.
-       \item Microcontrollers which are programmable in the \gls{Arduino} \gls{IDE}
-               connected via serial communication or via \gls{TCP} over WiFi or
-               Ethernet.
+       \item Microcontrollers family supported by
+               \texttt{ChibiOS}\footnote{\url{https://chibios.org}} connected via
+               serial communication.
+
+               This is also tested in particular on the \texttt{STM32f7x} series
+               \gls{ARM} development board.
+       \item Microcontrollers which are programmable in the \gls{Arduino}
+               \gls{IDE} connected via serial communication or via \gls{TCP} over WiFi
+               or Ethernet.
 
                This does not only include \gls{Arduino} compatible boards but also
                other boards capable of running \gls{Arduino} code. A port of the
-               client has been made for the \texttt{ESP8266} powered \emph{NodeMCU}
+               client has been made for the \textsc{ESP8266} powered \textsc{NodeMCU}
                that is connected via \gls{TCP} over WiFi. A port also has been made
-               for the regular \gls{Arduino} \emph{UNO} board which only boasts a
-               meager \emph{2K} \emph{RAM}. The stack size and storage available for
-               devices boasting this little \emph{RAM} has to be smaller than default
+               for the regular \gls{Arduino} Uno board which only boasts a
+               meager \emph{2K} RAM. The stack size and storage available % chktex 13
+               for devices boasting this little RAM has to be smaller than default
                but are still suitable to hold a hand full of \glspl{Task}.
 \end{itemize}
 
 \subsection{Client}
 \subsubsection{Engine}
-The client is in a constant loop listening for input and waiting to execute
+The client software is responsible for maintaining the communication and
+executing the \glspl{Task} when scheduled. In practise, this means that the
+client is in a constant loop, checking communication and executing
 \glspl{Task}. The pseudocode for this is shown in Algorithm~\ref{alg:client}.
 The \CI{input\_available} function waits for input, but has a timeout set which
 can be interrupted. The timeout of the function determines the amount of loops
@@ -77,17 +88,17 @@ device.
 \end{algorithm}
 
 \subsubsection{Storage}
-\glspl{Task} and \glspl{SDS} are stored on the client in memory. Some devices
-have very little memory and therefore memory space is very expensive and needs
-to be used optimally. Almost all microcontrollers support heaps nowadays,
-however, the functions for allocating and freeing the memory on the heap are
-not very space optimal and often leave holes in the heap if allocations are not
-freed in reverse order. To overcome this problem the client will allocate a big
-memory segment in the global data block. This block of memory resides under the
-stack and its size can be set in the interface implementation. This block of
-memory will be managed in a similar way as the entire memory space of the
-device is managed. \Glspl{Task} will grow from the bottom up and \glspl{SDS}
-will grow from the top down.
+\glspl{Task} and \glspl{SDS} are stored on the client not in program memory but
+in memory. Some devices have very little memory and therefore memory space is
+very expensive and needs to be used optimally. Almost all microcontrollers
+support heaps nowadays, however, the functions for allocating and freeing the
+memory on the heap are not very space optimal and often leave holes in the heap
+if allocations are not freed in a last in first out fashion. To overcome this
+problem, the client will allocate a big memory segment in the global data
+block. This block of memory resides under the stack and its size can be set in
+the interface implementation. This block of memory will be managed in a similar
+way as the entire memory space of the device is managed. \Glspl{Task} will grow
+from the bottom up and \glspl{SDS} will grow from the top down.
 
 When a \gls{Task} is received, the program will traverse the memory space from
 the bottom up, jumping over all \glspl{Task}. A \gls{Task} is stored as the
@@ -97,18 +108,19 @@ to determine the size of the \gls{Task}. When the program arrived at the last
 copied to there. This method is analogously applied for \glspl{SDS}, however,
 the \glspl{SDS} grow from the bottom down.
 
-When a \gls{Task} or \gls{SDS} is removed, all remaining objects are compressed
-again. This means that if the first received \gls{Task} is removed, all
+When a \gls{Task} or \gls{SDS} is removed, all the remaining objects in the
+memory space are reordered in such a way that there are no holes left. In
+practice this means that if the first received \gls{Task} is removed, all
 \glspl{Task} received later will have to move back. Obviously, this is quite
 time intensive but it can not be permitted to leave holes in the memory since
-the memory space is so limited. This techniques allows for even the smallest
-tested microcontrollers with only $2K$ \emph{RAM} to hold several \glspl{Task}
-and \glspl{SDS}. If this technique would not be used the memory space will
-decrease over time and the client can then not run for very long since holes
-are evidently created at some point.
+the memory space is so limited. With this technique, even the smallest
+tested microcontrollers with only $2K$ RAM can hold several \glspl{Task} and
+\glspl{SDS}. Without this technique, the memory space will decrease over time
+and the client can then not run for very long since holes are evidently created
+at some point.
 
-The structure instances and helper functions for traversing them in memory for
-\glspl{Task} and \glspl{SDS} are shown in Listing~\ref{lst:structs}.
+The structure instances and helper functions for traversing for \glspl{Task}
+and \glspl{SDS} are shown in Listing~\ref{lst:structs}.
 
 \begin{lstlisting}[language=C,label={lst:structs},%
        caption={The data type storing the \glspl{Task}},float]
@@ -139,11 +151,11 @@ and always starts with setting the program counter and stack
 pointer to zero and the bottom respectively. When finished, the
 interpreter executes one step at the time while the program counter is smaller
 than the program length. This code is listed in Listing~\ref{lst:interpr}. One
-execution step is basically a big switch statement going over all possible
-bytecode instructions. Of some instructions, the implementations are shown in
-the listing. The \CI{BCPush} instruction is a little more complicated in the
-real code because some decoding will take place as not all \CI{BCValue}s are of
-the same length and are encoded.
+execution step is basically a switch statement going over all possible bytecode
+instructions. The implementation of some instructions is shown in the
+listing. The \CI{BCPush} instruction is a little more complicated in the real
+code because some decoding will take place as not all \CI{BCValue}s are of the
+same length and are encoded.
 
 \begin{lstlisting}[language=C,label={lst:interpr},%
        caption={Rough code outline for interpretation}]
@@ -169,37 +181,16 @@ void run_task(struct task *t){
                        pc+=2;
                        break;
                // ...
-               case BCADD: trace("add");
+               case BCADD:
                        stack[sp-2] = stack[sp-2] + stack[sp-1];
                        sp -= 1;
                        break;
                // ...
-               case BCJMPT: trace("jmpt to %d", program[pc]);
+               case BCJMPT:
                        pc = stack[--sp] ? program[pc]-1 : pc+1;
                        break;
-}
-\end{lstlisting}
-
-\subsection{Specification}
-The server stores a description for every device available in a record type.
-From the macro settings in the interface file, a profile is created that
-describes the specification of the device. When the connection between the
-server and a client is established, the server will send a request for
-specification. The client serializes its specification and send it to the
-server so that the server knows what the client is capable of.  The exact
-specification is shown in Listing~\ref{lst:devicespec} and stores the
-peripheral availability, the memory available for storing \glspl{Task} and
-\glspl{SDS} and the size of the stack.
-
-\begin{lstlisting}[label={lst:devicespec},
-       caption={Device specification for \gls{mTask}-\glspl{Task}}]
-:: MTaskDeviceSpec =
-       { haveLed     :: Bool
-       , haveLCD     :: Bool
-       , have...
-       , bytesMemory :: Int
-       , stackSize   :: Int
-       , aPins       :: Int
-       , dPins       :: Int
+               // ...
+               }
        }
+}
 \end{lstlisting}