\chapter{Solution}
The solution consists of two separate applications: a website used for transmitting data, and a smartphone application used for receiving data. Transmission between the two application happens by a simple protocol on top of signals that use magnetic induction. I call the system MagSend.

\section{Protocol}
\label{section:protocol}
The following section describes how the MagSend protocol works. First I explain how the data gets encoded on the sender site, then the transmission, and finally how the data is received.

\subsection{Sending}
This section explains how a user-provided message gets encoded and finally transmitted using \gls{mi}.

Data over MagSend gets sent in a packet. For the structure of the packet I tried to follow other packet formats that already exist. Most of the structure is similiar to the structure of a LoRa packet. \cite{LoRaPacketFormat} I choose LoRa as example as I am familiar to it was a part of my studies and as LoRa is also a protocol designed with a low data-rate in mind. The packet contains three parts.

\subsubsection*{Header}
The first part of the packet is the header. The header is \SI{4}{\bit} long and contains the length of the payload in bytes. The length is mainly used to detect the end of the payload.

\subsubsection*{Payload}
Next is the actual payload. As the length of the payload is specified in the header, the payload is limited to a size of $2^4$ bytes. While the contents of the payload can be any bit stream, the sender and receiver should assume that the bit stream is interpreted as ASCII text.

\subsubsection*{Checksum}
For verifying the integrity of the payload, a checksum of the payload is calculated and placed at the end of the packet. The algorithm used is the 8-bit CRC-8-AUTOSAR algorithm (see \autoref{section:crc}). 

\subsubsection*{Encoding}
The contents of the packet is then encoded using a manchester code (see \autoref{section:manchester}) for transmission. 

\subsubsection*{Transmission}
The transmission works over magnetic induction. By putting a load on a CPU the resulting magnetic field can be changed. High signals should be interpreted as a 1 and low signals should be interpreted as a 0.

To indicate the start of a message in the signal, a preamble is used. The preamble used in this protocol is 3 high signals, followed by 3 low signals, followed by 3 high signals. The duration of one signal in the preamble is 1 second. That means the entire preamble takes 9 seconds.

After the preamble, the contents of the manchester encoded packet is sent. The entire transmission is repeated until the user stops the process.

\begin{figure}[H]
    \begin{bytefield}[bitwidth=.05\linewidth, bitheight=11mm]{4}
        \bitbox[]{4}[]{Preamble} & \bitbox[]{2}[]{Payload Length} & \bitbox[]{10}[]{Payload} & \bitbox[]{4}[]{Checksum} \\
        \bitbox{4}[bgcolor=gray!30]{9 Symbols} & \bitbox{2}[bgcolor=red!30]{\SI{4}{\bit}} & \bitbox{10}[bgcolor=blue!30]{\SI[number-math-rm = \mathnormal, parse-numbers = false]{n}{\byte}} & \bitbox{4}[bgcolor=green!30]{\SI{1}{\byte}}
    \end{bytefield}
    \caption{Packet Structure, with Preamble at the Start}
\end{figure}

The following pseudocode in \autoref{listing:sending_pseudo} explains step by
step how a message should get encoded and transmitted using MagSend.

\lstinputlisting[
    style=bfh-c,
    language=C,
    caption={Pseudocode for Sending},
    label={listing:sending_pseudo}
]{thesis/listings/sending_pseudo.py}

\subsubsection*{Example}
As an example the message "Test" is shown in the MagSend packet structure in \autoref{figure:testPacket}.

\begin{figure}[H]
    \begin{bytefield}[bitwidth=.05\linewidth, bitheight=11mm]{4}
        \bitbox[]{2}[]{Payload Length} & \bitbox[]{10}[]{Payload} & \bitbox[]{4}[]{Checksum} \\
        \bitbox{2}[bgcolor=red!30]{0x4} & \bitbox{10}[bgcolor=blue!30]{0x54657374} & \bitbox{4}[bgcolor=green!30]{0x2D}
    \end{bytefield}
    \caption{Packet Structure of the message "Test"}
    \label{figure:testPacket}
\end{figure}

When that previous packet is transmitted, the CPU utilization should look similiar to the one
in \autoref{figure:testCpu} with the included preamble.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{cpu_test.png}
    \caption{CPU Utilization while sending a Packet}
    \label{figure:testCpu}
\end{figure}

\subsection{Receiving}
The receiver utilizes a magnetometer to measure the magnetic field and
interprets the received sensor values as a signal.

The following pseudocode in \autoref{listing:receiving_pseudo} explains step by
step how the signal should get interpreted and decoded using MagSend.

\lstinputlisting[
    style=bfh-c,
    language=C,
    caption={Pseudocode for Receiving},
    label={listing:receiving_pseudo}
]{thesis/listings/receiving_pseudo.py}

\subsubsection*{Example}
In \autoref{figure:testPacketMagneticInduction} the same packet from the sending
example can be seen. This time it is the \gls{mi} signals that the smartphone is
receiving from the magnetometer.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{magnetic_induction_test_packet.png}
    \caption{Magnetic Induction Signal while receiving a Packet}
    \label{figure:testPacketMagneticInduction}
\end{figure}

\newpage
\section{Website}
The website provides the user with an interface to transmit text over the
MagSend protocol.

When the user opens the website they are greeted by a simple interface. It
contains an input field for entering text and two buttons. The first button
will send the entered text, and the second one will start the calibration
process.

\begin{figure}[H]
    \centering
    \fbox{\includegraphics[width=.65\textwidth]{test_cases/t1.png}}
    \caption{Website - Home}
\end{figure}

When the calibration process is started, a message is displayed, so it's clear
that we are in the calibration process. What happens during calibration is, that the website
puts a full load on the CPU for the duration of the calibration. The
actual calibration process happens in the smartphone application.

\begin{figure}[H]
    \centering
    \fbox{\includegraphics[width=.65\textwidth]{test_cases/t2.png}}
    \caption{Website - Calibration}
\end{figure}

\newpage
When the user presses the sending button, the text will be sent by the website
using the protocol described in \autoref{section:protocol}, by putting stress
on the CPU to generate a signal. A message is displayed that indicates that a sending process is ongoing. 

\begin{figure}[H]
    \centering
    \fbox{\includegraphics[width=.65\textwidth]{test_cases/t9.png}}
    \caption{Website - Sending}
\end{figure}

\newpage
\section{App}
The app allows the user to receive messages over the MagSend protocol.

On the homescreen you have two buttons. One button is for receiving messages and one is to start the calibration process. By clicking one of the buttons the user will be switched to the corresponding screen.

\begin{figure}[H]
    \centering
    \fbox{\includegraphics[width=0.4\textwidth]{app_home.jpg}}
    \caption{App - Home}
\end{figure}

\newpage
The calibration mode is used to find the most optimal location where the user should put the smartphone on the sending device. The website will continously put a load on the CPU. The app will then show the current strength of the magnetic field. The location where the strength is the highest is the one where you will get the best transmission results.

\begin{figure}[H]
    \centering
    \fbox{\includegraphics[width=0.4\textwidth]{test_cases/t5.jpg}}
    \caption{App - Calibration}
\end{figure}

\newpage
During the reception process, the currently received text is displayed on the screen while a loading indicator indicates that the process is still ongoing. When the process is finished, the complete text is displayed. At any time, the user has the possibility to restart the reception process for a new message.

\begin{figure}[H]
    \begin{minipage}{.5\textwidth}
        \centering
        \fbox{\includegraphics[width=0.8\textwidth]{app_receive.jpg}}
        \caption{App - Receiving in Progress}
    \end{minipage}  
    \begin{minipage}{.5\textwidth}
        \centering
        \fbox{\includegraphics[width=0.8\textwidth]{test_cases/t11.jpg}}
        \caption{App - Receving Completed}
    \end{minipage}  
\end{figure}
