Skip to content

Lab Part 1

Cenk Gündoğan edited this page Dec 20, 2021 · 7 revisions

Development and Testing with RIOT native

In the following guide you will find information on how to use RIOTs native port in a standard PC environment without using dedicated embedded IoT Hardware.

Requirements

The following software is required to start development:

  • a text editor or full fledge IDE suitable for C/C++ Development
  • a C/C++ compiler and tools, e.g. the GCC toolchain (version >= 4.9.x)
  • standard development tools, specifically make
  • to verify, that your system has all required tools installed, try to run the Quickstart Guide as shown in the README

Note: the VS lab workstations do not have a recent compiler suitable for RIOT, please use Docker as described here or use the IoT hardware kit provided for the VS Lab4 exercise, see part two for further instructions.

Implementation

The source code provides the basic scaffold and functionality to solve the lab exercise. For the implementation you only need to fill in and modify the event loop in the main function, see main.c, i.e., the parts marked with comment @todo implement. Do not modify or remove other code parts, as this might break the application.

Before starting to implement have a look at the API description, which can also be found in elect.h. The latter also contains a number of macros which define timing and config parameters that must be used, namely:

  • ELECT_NODES_NUM: needed for static memory allocations, adapt as needed.
  • ELECT_MSG_INTERVAL: interval for broadcasts during election and CoAP GET request for sensor data from other nodes by the leader.
  • ELECT_LEADER_THRESHOLD: time (in milliseconds) to decide wether a node is the leader (aka coordinator) or not.
  • ELECT_LEADER_TIMEOUT: time (in milliseconds) after a client node assumes the current leader to be unavailable and initiates a new election round. Note: the leader is unavailable if no messages (broadcast or CoAP) were received from the respective node for the given timeout.

The primary loop in main is driven by events that are triggered via IPC messages. Hence, the algorithm is implemented by filling in the required message handlers. There are 7 message that need to be processed, they are:

  1. ELECT_BROADCAST_EVENT, is trigger when ever broadcast is received. The associated event message contains a pointer to IP address string, which can be used as follows:
ipv6_addr_t addr;
char *addr_str = m.content.ptr;
ipv6_addr_from_str(&addr, addr_str);
  1. ELECT_INTERVAL_EVENT, is trigger by the interval_event.
  2. ELECT_LEADER_ALIVE_EVENT, is triggered when a CoAP GET request was received from the leader.
  3. ELECT_LEADER_THRESHOLD_EVENT, is trigger by the leader_threshold_event.
  4. ELECT_LEADER_TIMEOUT_EVENT, is trigger by the leader_timeout_event.
  5. ELECT_NODES_EVENT, is trigger when a CoAP PUT from a client node is received. The associated event message contains a pointer to IP address string of the corresponding node, which can be used similar to above (see ELECT_BROADCAST_EVENT).
  6. ELECT_SENSOR_EVENT, is trigger when the answer to a CoAP GET is received. This also means the local node was selected as a leader. The associated event message contains a pointer to the sensor value as string, which can be used as follows:
int16_t value = (int16_t)strtol((char *)m.content.ptr, NULL, 10);

Building and Testing

To build the application and run it as process on a computer, run the following:

  • build: make clean all
  • and run: make term

To run the RIOT application multiple times, setup a TAP bridge with a number of TAP devices. RIOT provides a helper script named tapsetup. Note: this only runs on Linux and most other Unix based OS - Windows and macOS aren't supported.

  • Create a bridge with 5 TAP devices (tap0 - tap4):
./RIOT/dist/tools/tapsetup/tapsetup -c 5
  • Create single application instance:
PORT=tap0 make -C src term
  • Open a new shell and repeat the previous command with PORT=tap1 ... PORT=tap4
Clone this wiki locally