Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move 2018-sensys apps from libtock-c #5

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions courses/2018-11-SenSys/exercises/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SenSys 2018 Tutorial
====================

In November 2018 we hosted [a tutorial at SenSys](https://www.tockos.org/events/sensys2018),
a preeminent conference for networking, sensors, and systems.

The goal of this tutorial was to explore how multi-tenancy can cause problems
(one buggy app draining system [energy] resources) and also provide solutions
(via management that allow runtime monitoring and correction).
The tutorial begins with a general overview of Tock and its programming
environment, then considers application development and management, and finally
digs into the 802.15.4 and UDP/IP capabilities in Tock.

**If you are interested in doing this tutorial yourself, please
`git checkout courses/2018-sensys` in both this repository and the Tock kernel.**
16 changes: 16 additions & 0 deletions courses/2018-11-SenSys/exercises/important-client/app1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Change name of generated app
PACKAGE_NAME = app1

APP_HEAP_SIZE := 2048

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
16 changes: 16 additions & 0 deletions courses/2018-11-SenSys/exercises/important-client/app1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
UDP Button Press App
====================

Your client needs to keep track of sporadic events and record when those
events occur by pressing the 'user' button on the Imix.

This app is for platforms with an 802.15.4 radio. It broadcasts button presses
over the network, so they can be recorded at a central server.
Currently, this app sends UDP packets using 6lowpan to a single neighbor with
an IP address known ahead of time.

## Running

To run this app, simply place the IP address of the destination node in the `dst_addr` struct.
Notably, until Tock has neighbor discovery implemented, you also have to configure
the destination MAC address in the kernel (in `boards/imix/src/main.rs`).
92 changes: 92 additions & 0 deletions courses/2018-11-SenSys/exercises/important-client/app1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdbool.h>
#include <stdio.h>

#include <button.h>

#include <ieee802154.h>
#include <udp.h>

#define DEBUG 0

static unsigned char BUF_BIND_CFG[2 * sizeof(sock_addr_t)];
static int button_press;

void print_ipv6(ipv6_addr_t *);

void print_ipv6(ipv6_addr_t *ipv6_addr) {
for (int j = 0; j < 14; j += 2) {
printf("%02x%02x:", ipv6_addr->addr[j], ipv6_addr->addr[j + 1]);
}
printf("%02x%02x", ipv6_addr->addr[14], ipv6_addr->addr[15]);
}

int main(void) {

char packet[64];
button_press = 0;

ieee802154_set_pan(0xABCD);
ieee802154_config_commit();
ieee802154_up();

ipv6_addr_t ifaces[10];
udp_list_ifaces(ifaces, 10);

sock_handle_t handle;
sock_addr_t addr = {
ifaces[2],
15123
};

printf("Opening socket on ");
print_ipv6(&ifaces[2]);
printf(" : %d\n", addr.port);
int bind_return = udp_bind(&handle, &addr, BUF_BIND_CFG);
if (bind_return < 0) {
printf("Bind failed. Error code: %d\n", bind_return);
return -1;
}

// Set the below address to IP address of receiver
ipv6_addr_t dest_addr = {
{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0, 0xdf, 0xf0}
};
sock_addr_t destination = {
dest_addr,
16123
};

int count = 0;
while (1) {
// wait for gpio pin to be pressed
while (button_press == 0) {
button_read(0, &button_press);
}
count++;

int len = snprintf(packet, sizeof(packet), "{\"buttons\": %d}", count);
if (DEBUG) {
printf("Button press detected\n");

printf("Sending packet (length %d) --> ", len);
print_ipv6(&(destination.addr));
printf(" : %d\n", destination.port);
}
ssize_t result = udp_send_to(packet, len, &destination);

switch (result) {
case RETURNCODE_SUCCESS:
if (DEBUG) {
printf("Packet sent.\n");
}
break;
default:
printf("Error sending packet %d\n", result);
}

// Debounce
while (button_press != 0) {
button_read(0, &button_press);
}
}
}
13 changes: 13 additions & 0 deletions courses/2018-11-SenSys/exercises/important-client/app2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

APP_HEAP_SIZE := 2048

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
31 changes: 31 additions & 0 deletions courses/2018-11-SenSys/exercises/important-client/app2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
UDP Sensor App
==============

Your customer needs to periodically measure the environment around their board.

This app is for platforms with hardware RNG support, environmental sensors, and
an 802.15.4 radio. The app will broadcast periodic sensor readings over the
network. Currently, it sends UDP packets using 6lowpan to a single neighbor
with an IP address known ahead of time. The contents of the payload (which are
serialized to JSON) include:

1. A random number, generated by the application accessing the underlying hardware RNG.

2. Three sensor readings (temperature, relative humidity, and light).

## Running

To run this app, simply place the IP address of the destination node in the `dst_addr` struct.
Notably, until Tock has neighbor discovery implemented, you also have to configure
the destination MAC address in the kernel (in `boards/imix/src/main.rs`).

### UDP Layer Reception Test

The best way to test this app is by using the UDP reception app on another Imix.
Program the kernel on two imixs. On one imix load this app to send packets. On
the other imix load the `sensys_udp_rx` app found in
`userland/examples/sensys_udp_rx`. You will want to set the
`PRINT_STRING` macro to `1` for maximum visibility.

If everything is working, you will see packets printed on the console.
These lines contain the payload of the received UDP packet.
111 changes: 111 additions & 0 deletions courses/2018-11-SenSys/exercises/important-client/app2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include <ambient_light.h>
#include <button.h>
#include <humidity.h>
#include <rng.h>
#include <temperature.h>
#include <timer.h>

#include <ieee802154.h>
#include <udp.h>

#define DEBUG 0

static unsigned char BUF_BIND_CFG[2 * sizeof(sock_addr_t)];

void print_ipv6(ipv6_addr_t *);
int serialize_to_json(char* packet, int len, uint32_t rand, int temp, int humi, int lux);

int main(void) {

printf("[UDP] Starting UDP App.\n");

static unsigned int humi = 0;
static int temp = 0;
static int lux = 0;
static char packet[70];

ieee802154_set_pan(0xABCD);
ieee802154_config_commit();
ieee802154_up();

ipv6_addr_t ifaces[10];
udp_list_ifaces(ifaces, 10);

sock_handle_t handle;
sock_addr_t addr = {
ifaces[2],
11111
};

printf("Opening socket on ");
print_ipv6(&ifaces[2]);
printf(" : %d\n", addr.port);
int bind_return = udp_bind(&handle, &addr, BUF_BIND_CFG);

if (bind_return < 0) {
printf("Bind failed. Error code: %d\n", bind_return);
return -1;
}

// Set the below address to be the IP address of your receiver
ipv6_addr_t dest_addr = {
{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0, 0xdf, 0xf0}
};
sock_addr_t destination = {
dest_addr,
16123
};

while (1) {
temperature_read_sync(&temp);
humidity_read_sync(&humi);
ambient_light_read_intensity_sync(&lux);

// get randomness
uint32_t rand = 0;
int num_received = 0;
int err = rng_sync((uint8_t*)&rand, 4, 4, &num_received);
if (err < 0) {
printf("Error obtaining random number: %d\n", err);
} else if (num_received < 4) {
printf("Only obtained %d bytes of randomness\n", num_received);
}

int len = serialize_to_json(packet, sizeof(packet), rand, temp, humi, lux);
if (DEBUG) {
printf("Sending packet (length %d) --> ", len);
print_ipv6(&(destination.addr));
printf(" : %d\n", destination.port);
}
ssize_t result = udp_send_to(packet, len, &destination);

switch (result) {
case RETURNCODE_SUCCESS:
if (DEBUG) {
printf("Packet sent.\n\n");
}
break;
default:
printf("Error sending packet %d\n\n", result);
}

delay_ms(5000);
}
}

void print_ipv6(ipv6_addr_t *ipv6_addr) {
for (int j = 0; j < 14; j += 2) {
printf("%02x%02x:", ipv6_addr->addr[j], ipv6_addr->addr[j + 1]);
}
printf("%02x%02x", ipv6_addr->addr[14], ipv6_addr->addr[15]);
}

int serialize_to_json(char* buf, int buflen, uint32_t rand, int temp, int humi, int lux) {
return snprintf(buf, buflen, "{\"rand\": %lu, \"temp\": %d, \"humi\": %d, \"lux\": %d}",
rand, temp, humi, lux);
}

11 changes: 11 additions & 0 deletions courses/2018-11-SenSys/exercises/sensys_udp_rx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
4 changes: 4 additions & 0 deletions courses/2018-11-SenSys/exercises/sensys_udp_rx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Test to receive UDP packets. Listens on the MAC address specified by the serial
number of the sam4l of this device, and the link-local IPv6 address generated
from the EUI-64 corresponding to this MAC address.

Loading