diff --git a/apds-9007-app/Makefile b/apds-9007-app/Makefile new file mode 100644 index 0000000..dbde253 --- /dev/null +++ b/apds-9007-app/Makefile @@ -0,0 +1,61 @@ +APPLICATION = hamilton_apds9007_easyapp +RIOTBASE ?= /home/hskim/Desktop/rebase/RIOT-OS +BOARD = hamilton + +# System functions +USEMODULE += random +USEMODULE += xtimer +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 + +# Sensors +USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +USEMODULE += auto_init_saul +USEMODULE += tmp006 # ambient temperature +USEMODULE += hdc1000 # humidity and temperature +USEMODULE += fxos8700 # acceleration and magnetic field +USEMODULE += apds9007 # illumination +USEMODULE += ekmb1101111 # pir-based occupancy +USEMODULE += push_button # simple button + +# Security +USEMODULE += crypto +USEMODULE += cipher_modes +CFLAGS += -DCRYPTO_AES + +CFLAGS += -DAUTO_CSMA_EN=1 + +# Radio +USEMODULE += at86rf2xx +CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=26 + +# Network +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Duty-cycling +USEMODULE += gnrc_lasmac +CFLAGS += -DDUTYCYCLE_SLEEP_INTERVAL=00000UL # If it is ZERO, no duty-cycling for packet reception +CFLAGS += -DLEAF_NODE=1 # Default is 1 +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6 +USEMODULE += gnrc_udp +CFLAGS += -DSOCK_HAS_IPV6 +# Add also the shell, some shell commands +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 + + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +# CFLAGS += -DDEVELHELP + +QUIET ?= 1 + +FEATURES_REQUIRED += periph_timer + +include $(RIOTBASE)/Makefile.include diff --git a/apds-9007-app/main.c b/apds-9007-app/main.c new file mode 100644 index 0000000..bc346ce --- /dev/null +++ b/apds-9007-app/main.c @@ -0,0 +1,62 @@ +#include +#include +#include "xtimer.h" +#include +#include "net/gnrc/udp.h" +#include "phydat.h" +#include "saul_reg.h" +#include "periph/adc.h" +#include "periph/dmac.h" +#include "periph/timer.h" + +#define ENABLE_DEBUG (1) +#include "debug.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL ( 1000000UL) +#endif + +saul_reg_t *sensor_light_t = NULL; + +void critical_error(void) { + DEBUG("CRITICAL ERROR, REBOOT\n"); + NVIC_SystemReset(); + return; +} + +void sensor_config(void) { + sensor_light_t = saul_reg_find_type(SAUL_SENSE_LIGHT); + if (sensor_light_t == NULL) { + DEBUG("[ERROR] Failed to init LIGHT sensor\n"); + critical_error(); + } else { + DEBUG("LIGHT sensor OK\n"); + } +} + +/* ToDo: Sampling sequence arrangement or thread/interrupt based sensing may be better */ +void sample(void) { + phydat_t output; /* Sensor output data (maximum 3-dimension)*/ + int dim; /* Demension of sensor output */ + + /* Illumination 1-dim */ + dim = saul_reg_read(sensor_light_t, &output); + (void) dim; + //phydat_dump(&output, dim); +} + +int main(void) { + dmac_init(); + //adc_set_dma_channel(0); + + sensor_config(); + LED_OFF; + + while (1) { + //Sample + sample(); + xtimer_usleep(SAMPLE_INTERVAL); + } + + return 0; +} diff --git a/hamilton-combined-v9.0-app/Makefile b/hamilton-combined-v9.0-app/Makefile new file mode 100644 index 0000000..8c4c7a1 --- /dev/null +++ b/hamilton-combined-v9.0-app/Makefile @@ -0,0 +1,61 @@ +APPLICATION = hamilton_easyapp +RIOTBASE ?= /home/hskim/Desktop/rebase/RIOT-OS +BOARD = hamilton + +# System functions +USEMODULE += random +USEMODULE += xtimer +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 + +# Sensors +USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +USEMODULE += auto_init_saul +USEMODULE += tmp006 # ambient temperature +USEMODULE += hdc1000 # humidity and temperature +USEMODULE += fxos8700 # acceleration and magnetic field +USEMODULE += apds9007 # illumination +USEMODULE += ekmb1101111 # pir-based occupancy +USEMODULE += push_button # simple button + +# Security +USEMODULE += crypto +USEMODULE += cipher_modes +CFLAGS += -DCRYPTO_AES + +CFLAGS += -DAUTO_CSMA_EN=1 + +# Radio +USEMODULE += at86rf2xx +CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=26 + +# Network +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Duty-cycling +USEMODULE += gnrc_lasmac +CFLAGS += -DDUTYCYCLE_SLEEP_INTERVAL=00000UL # If it is ZERO, no duty-cycling for packet reception +CFLAGS += -DLEAF_NODE=1 # Default is 1 +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6 +USEMODULE += gnrc_udp +CFLAGS += -DSOCK_HAS_IPV6 +# Add also the shell, some shell commands +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 + + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +# CFLAGS += -DDEVELHELP + +QUIET ?= 1 + +FEATURES_REQUIRED += periph_timer + +include $(RIOTBASE)/Makefile.include diff --git a/hamilton-combined-v9.0-app/main.c b/hamilton-combined-v9.0-app/main.c new file mode 100644 index 0000000..1067ce7 --- /dev/null +++ b/hamilton-combined-v9.0-app/main.c @@ -0,0 +1,325 @@ +#include +#include +#include "xtimer.h" +#include +#include "net/gnrc/udp.h" +#include "phydat.h" +#include "saul_reg.h" +#include "periph/adc.h" +#include "periph/dmac.h" +#include "periph/i2c.h" +#include "periph/spi.h" +#include "periph/timer.h" + +#define ENABLE_DEBUG (1) +#include "debug.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL ( 10000000UL) +#endif +#define SAMPLE_JITTER ( 200000UL) + +#define TYPE_FIELD 8 + + +void send_udp(char *addr_str, uint16_t port, uint8_t *data, uint16_t datalen); + +#define AES_SKIP_START_BYTES 4 + +typedef struct __attribute__((packed,aligned(4))) { + uint16_t type; + uint16_t serial; + //From below is encrypted + //We use a zero IV, so it is important that the first AES block + //is completely unique, which is why we include uptime. + //It is expected that hamilton nodes never reboot and that + //uptime is strictly monotonic + uint64_t uptime; + uint16_t flags; //which of the fields below exist + int16_t acc_x; + int16_t acc_y; + int16_t acc_z; + int16_t mag_x; + int16_t mag_y; + int16_t mag_z; + int16_t radtemp; + int16_t temp; + int16_t hum; + int16_t light_lux; + uint16_t buttons; + uint16_t occup; + uint32_t reserved1; + uint32_t reserved2; + uint32_t reserved3; +} ham7c_t; + +saul_reg_t *sensor_radtemp_t = NULL; +saul_reg_t *sensor_temp_t = NULL; +saul_reg_t *sensor_hum_t = NULL; +saul_reg_t *sensor_mag_t = NULL; +saul_reg_t *sensor_accel_t = NULL; +saul_reg_t *sensor_light_t = NULL; +saul_reg_t *sensor_occup_t = NULL; +saul_reg_t *sensor_button_t = NULL; + +//Uptime is mandatory (for AES security) +#define FLAG_ACC (1<<0) +#define FLAG_MAG (1<<1) +#define FLAG_TMP (1<<2) +#define FLAG_HDC (1<<3) +#define FLAG_LUX (1<<4) +#define FLAG_BUTTONS (1<<5) +#define FLAG_OCCUP (1<<6) + +//All of the flags +#define PROVIDED_FLAGS (0x7F) + +void critical_error(void) { + DEBUG("CRITICAL ERROR, REBOOT\n"); + NVIC_SystemReset(); + return; +} + +void sensor_config(void) { + sensor_radtemp_t = saul_reg_find_type(SAUL_SENSE_RADTEMP); + if (sensor_radtemp_t == NULL) { + DEBUG("[ERROR] Failed to init RADTEMP sensor\n"); + critical_error(); + } else { + DEBUG("TEMP sensor OK\n"); + } + + sensor_hum_t = saul_reg_find_type(SAUL_SENSE_HUM); + if (sensor_hum_t == NULL) { + DEBUG("[ERROR] Failed to init HUM sensor\n"); + critical_error(); + } else { + DEBUG("HUM sensor OK\n"); + } + + sensor_temp_t = saul_reg_find_type(SAUL_SENSE_TEMP); + if (sensor_temp_t == NULL) { + DEBUG("[ERROR] Failed to init TEMP sensor\n"); + critical_error(); + } else { + DEBUG("TEMP sensor OK\n"); + } + + sensor_mag_t = saul_reg_find_type(SAUL_SENSE_MAG); + if (sensor_mag_t == NULL) { + DEBUG("[ERROR] Failed to init MAGNETIC sensor\n"); + critical_error(); + } else { + DEBUG("MAGNETIC sensor OK\n"); + } + + sensor_accel_t = saul_reg_find_type(SAUL_SENSE_ACCEL); + if (sensor_accel_t == NULL) { + DEBUG("[ERROR] Failed to init ACCEL sensor\n"); + critical_error(); + } else { + DEBUG("ACCEL sensor OK\n"); + } + + sensor_light_t = saul_reg_find_type(SAUL_SENSE_LIGHT); + if (sensor_light_t == NULL) { + DEBUG("[ERROR] Failed to init LIGHT sensor\n"); + critical_error(); + } else { + DEBUG("LIGHT sensor OK\n"); + } + + sensor_occup_t = saul_reg_find_type(SAUL_SENSE_OCCUP); + if (sensor_occup_t == NULL) { + DEBUG("[ERROR] Failed to init OCCUP sensor\n"); + critical_error(); + } else { + DEBUG("OCCUP sensor OK\n"); + } + + sensor_button_t = saul_reg_find_type(SAUL_SENSE_BTN); + if (sensor_button_t == NULL) { + DEBUG("[ERROR] Failed to init BUTTON sensor\n"); + critical_error(); + } else { + DEBUG("BUTTON sensor OK\n"); + } +} + +/* ToDo: Sampling sequence arrangement or thread/interrupt based sensing may be better */ +void sample(ham7c_t *m) { + phydat_t output; /* Sensor output data (maximum 3-dimension)*/ + int dim; /* Demension of sensor output */ + + /* Occupancy 1-dim */ + dim = saul_reg_read(sensor_occup_t, &output); + if (dim > 0) { + m->occup = output.val[0]; + printf("\nDev: %s\tType: %s\n", sensor_occup_t->name, + saul_class_to_str(sensor_occup_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read Occupancy\n"); + } + + /* Push button events 1-dim */ + dim = saul_reg_read(sensor_button_t, &output); + if (dim > 0) { + m->buttons = output.val[0]; + printf("\nDev: %s\tType: %s\n", sensor_button_t->name, + saul_class_to_str(sensor_button_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read button events\n"); + } + + /* Illumination 1-dim */ + dim = saul_reg_read(sensor_light_t, &output); + if (dim > 0) { + m->light_lux = output.val[0]; + printf("\nDev: %s\tType: %s\n", sensor_light_t->name, + saul_class_to_str(sensor_light_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read Illumination\n"); + } + + /* Magnetic field 3-dim */ + dim = saul_reg_read(sensor_mag_t, &output); + if (dim > 0) { + m->mag_x = output.val[0]; m->mag_y = output.val[1]; m->mag_z = output.val[2]; + printf("\nDev: %s\tType: %s\n", sensor_mag_t->name, + saul_class_to_str(sensor_mag_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read magnetic field\n"); + } + + /* Acceleration 3-dim */ + dim = saul_reg_read(sensor_accel_t, &output); + if (dim > 0) { + m->acc_x = output.val[0]; m->acc_y = output.val[1]; m->acc_z = output.val[2]; + printf("\nDev: %s\tType: %s\n", sensor_accel_t->name, + saul_class_to_str(sensor_accel_t->driver->type)); + phydat_dump(&output, dim); + } else { + printf("[ERROR] Failed to read Acceleration\n"); + } + + /* Radient temperature 1-dim */ + dim = saul_reg_read(sensor_radtemp_t, &output); /* 500ms */ + if (dim > 0) { + m->temp = output.val[0]; + m->radtemp = output.val[1]; + printf("\nDev: %s\tType: %s\n", sensor_radtemp_t->name, + saul_class_to_str(sensor_radtemp_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read Radient Temperature\n"); + } + + /* Temperature 1-dim */ + // dim = saul_reg_read(sensor_temp_t, &output); /* 15ms */ + /*if (dim > 0) { + m->temp = output.val[0]; + printf("\nDev: %s\tType: %s\n", sensor_temp_t->name, + saul_class_to_str(sensor_temp_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read Temperature\n"); + }*/ + + /* Humidity 1-dim */ + dim = saul_reg_read(sensor_hum_t, &output); /* 15ms */ + if (dim > 0) { + m->hum = output.val[0]; + printf("\nDev: %s\tType: %s\n", sensor_hum_t->name, + saul_class_to_str(sensor_hum_t->driver->type)); + phydat_dump(&output, dim); + } else { + DEBUG("[ERROR] Failed to read Humidity\n"); + } + + /* Time from start */ + m->uptime = xtimer_usec_from_ticks64(xtimer_now64()); + + /* Others */ + m->serial = *fb_device_id; + m->type = TYPE_FIELD; + m->flags = PROVIDED_FLAGS; + + puts("\n##########################"); +} + +uint32_t interval_with_jitter(void) +{ + int32_t t = SAMPLE_INTERVAL; + /*t += rand() % SAMPLE_JITTER; + t -= SAMPLE_JITTER / 2;*/ + return (uint32_t)t; +} + +ham7c_t frontbuf; + +uint8_t obuffer [sizeof(ham7c_t)]; +uint8_t iv [16]; + +#include "crypto/ciphers.h" +#include "crypto/modes/cbc.h" +cipher_t aesc; + +void crypto_init(void) { + //While this appears absurd, don't worry too much about it. + //The first block is guaranteed to be unique so we don't really + //need the IV + for (int i = 0; i < 16; i++) { + iv[i] = i; + } + //printf("us: %d\n", *fb_device_id); + //printf("key: "); + //for (int i = 0; i < 16; i++) { + // printf("%02x", fb_aes128_key[i]); + //} + //printf("\n"); + int rv = cipher_init(&aesc, CIPHER_AES_128, fb_aes128_key, 16); + if (rv != CIPHER_INIT_SUCCESS) { + DEBUG("[ERROR] Failed to init Cipher\n"); + critical_error(); + } +} +void aes_populate(void) { + cipher_encrypt_cbc(&aesc, iv, ((uint8_t*)&frontbuf) + AES_SKIP_START_BYTES, + sizeof(ham7c_t)-AES_SKIP_START_BYTES, &obuffer[AES_SKIP_START_BYTES]); + memcpy(obuffer, ((uint8_t*)&frontbuf), AES_SKIP_START_BYTES); +} + + +int main(void) { + //This value is good randomness and unique per mote + srand(*((uint32_t*)fb_aes128_key)); + crypto_init(); + sensor_config(); + dmac_init(); + adc_set_dma_channel(0); + i2c_set_dma_channel(0, 1); + spi_set_dma_channel(0, 2, 3); + //uint16_t i = 0; + //uint16_t j = 0; + LED_OFF; + + printf("size %u\n", sizeof(ham7c_t)); + //xtimer_ticks32_t last_wakeup = xtimer_now(); + while (1) { + //Sample + sample(&frontbuf); + //printf("zzzzz\n"); + //aes_populate(); + //Send + //send_udp("ff02::1",4747,obuffer,sizeof(obuffer)); + //Sleep + //xtimer_periodic_wakeup(&last_wakeup, interval_with_jitter()); + xtimer_usleep(interval_with_jitter()); + } + + return 0; +} diff --git a/hamilton-combined-v9.0-app/udp.c b/hamilton-combined-v9.0-app/udp.c new file mode 100644 index 0000000..8d38842 --- /dev/null +++ b/hamilton-combined-v9.0-app/udp.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup examples + * @{ + * + * @file + * @brief Demonstrating the sending and receiving of UDP data + * + * @author Hauke Petersen + * + * @} + */ + +#if MODULE_GNRC_UDP +#include +#include + +#include "net/gnrc.h" +#include "net/gnrc/ipv6.h" +#include "net/gnrc/udp.h" +#include "net/gnrc/pktdump.h" +#include "timex.h" +#include "xtimer.h" + + +void send_udp(char *addr_str, uint16_t port, uint8_t *data, uint16_t datalen) +{ + ipv6_addr_t addr; + + /* parse destination address */ + if (ipv6_addr_from_str(&addr, addr_str) == NULL) { + puts("Error: unable to parse destination address"); + return; + } + + gnrc_pktsnip_t *payload, *udp, *ip; + /* allocate payload */ + payload = gnrc_pktbuf_add(NULL, data, datalen, GNRC_NETTYPE_UNDEF); + if (payload == NULL) { + puts("Error: unable to copy data to packet buffer"); + return; + } + /* allocate UDP header, set source port := destination port */ + udp = gnrc_udp_hdr_build(payload, port, port); + if (udp == NULL) { + puts("Error: unable to allocate UDP header"); + gnrc_pktbuf_release(payload); + return; + } + /* allocate IPv6 header */ + ip = gnrc_ipv6_hdr_build(udp, NULL, &addr); + if (ip == NULL) { + puts("Error: unable to allocate IPv6 header"); + gnrc_pktbuf_release(udp); + return; + } + /* send packet */ + if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { + puts("Error: unable to locate UDP thread"); + gnrc_pktbuf_release(ip); + return; + } +} +#endif diff --git a/hdc-1000-app/Makefile b/hdc-1000-app/Makefile new file mode 100644 index 0000000..3e56481 --- /dev/null +++ b/hdc-1000-app/Makefile @@ -0,0 +1,61 @@ +APPLICATION = hamilton_hdc1000_easyapp +RIOTBASE ?= /home/hskim/Desktop/rebase/RIOT-OS +BOARD = hamilton + +# System functions +USEMODULE += random +USEMODULE += xtimer +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 + +# Sensors +USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +USEMODULE += auto_init_saul +USEMODULE += tmp006 # ambient temperature +USEMODULE += hdc1000 # humidity and temperature +USEMODULE += fxos8700 # acceleration and magnetic field +USEMODULE += apds9007 # illumination +USEMODULE += ekmb1101111 # pir-based occupancy +USEMODULE += push_button # simple button + +# Security +USEMODULE += crypto +USEMODULE += cipher_modes +CFLAGS += -DCRYPTO_AES + +CFLAGS += -DAUTO_CSMA_EN=1 + +# Radio +USEMODULE += at86rf2xx +CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=26 + +# Network +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Duty-cycling +USEMODULE += gnrc_lasmac +CFLAGS += -DDUTYCYCLE_SLEEP_INTERVAL=00000UL # If it is ZERO, no duty-cycling for packet reception +CFLAGS += -DLEAF_NODE=1 # Default is 1 +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6 +USEMODULE += gnrc_udp +CFLAGS += -DSOCK_HAS_IPV6 +# Add also the shell, some shell commands +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 + + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +# CFLAGS += -DDEVELHELP + +QUIET ?= 1 + +FEATURES_REQUIRED += periph_timer + +include $(RIOTBASE)/Makefile.include diff --git a/hdc-1000-app/main.c b/hdc-1000-app/main.c new file mode 100644 index 0000000..91934f7 --- /dev/null +++ b/hdc-1000-app/main.c @@ -0,0 +1,62 @@ +#include +#include +#include "xtimer.h" +#include +#include "net/gnrc/udp.h" +#include "phydat.h" +#include "saul_reg.h" +#include "periph/dmac.h" +#include "periph/i2c.h" +#include "periph/timer.h" + +#define ENABLE_DEBUG (1) +#include "debug.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL ( 1000000UL) +#endif + +saul_reg_t *sensor_hum_t = NULL; + +void critical_error(void) { + DEBUG("CRITICAL ERROR, REBOOT\n"); + NVIC_SystemReset(); + return; +} + +void sensor_config(void) { + sensor_hum_t = saul_reg_find_type(SAUL_SENSE_HUM); + if (sensor_hum_t == NULL) { + DEBUG("[ERROR] Failed to init HUM sensor\n"); + critical_error(); + } else { + DEBUG("HUM sensor OK\n"); + } +} + +/* ToDo: Sampling sequence arrangement or thread/interrupt based sensing may be better */ +void sample(void) { + phydat_t output; /* Sensor output data (maximum 3-dimension)*/ + int dim; /* Demension of sensor output */ + + /* Illumination 1-dim */ + dim = saul_reg_read(sensor_hum_t, &output); + (void) dim; + //phydat_dump(&output, dim); +} + +int main(void) { + dmac_init(); + i2c_set_dma_channel(0, 0); + + sensor_config(); + LED_OFF; + + while (1) { + //Sample + sample(); + xtimer_usleep(SAMPLE_INTERVAL); + } + + return 0; +} diff --git a/openthread/Makefile b/openthread/Makefile new file mode 100644 index 0000000..fbd7241 --- /dev/null +++ b/openthread/Makefile @@ -0,0 +1,81 @@ +APPLICATION = openthread + +# If no BOARD is found in the environment, use this default: +BOARD ?= hamilton + +# These are the boards that OpenThread stack has been tested on +BOARD_WHITELIST := hamilton samr21-xpro iotlab-m3 fox iotlab-a8-m3 + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE = /home/hskim/Desktop/RIOT-OS + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +#CFLAGS += -DDEVELHELP -Wall + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +USEPKG += openthread +OPENTHREAD_TYPE ?= ftd +ifeq ($(OPENTHREAD_TYPE),mtd) + # MTD: A Minimal Thread Device does not have router functionality + # compiled in. As a result, it is not necessary to configure the + # routerrole on an MTD. At the same time, an MTD may or may not be sleepy. + USEMODULE += openthread-mtd + USEMODULE += openthread-cli-mtd +else + # ftd: A Full Thread Device has router functionality compiled in + USEMODULE += openthread-ftd + USEMODULE += openthread-cli-ftd +endif + +#Define PANID, CHANNEL and UART_BAUDRATE used by default +OPENTHREAD_PANID ?= 0xbeef +OPENTHREAD_CHANNEL ?= 26 +OPENTHREAD_UART_BAUDRATE ?= 115200 + +CFLAGS += -DOPENTHREAD_PANID=${OPENTHREAD_PANID} +CFLAGS += -DOPENTHREAD_CHANNEL=${OPENTHREAD_CHANNEL} +CFLAGS += -DOPENTHREAD_UART_BAUDRATE=${OPENTHREAD_UART_BAUDRATE} + +ifneq (,$(filter hamilton samr21-xpro,$(BOARD))) + DRIVER := at86rf233 +endif +ifneq (,$(filter iotlab-m3 fox iotlab-a8-m3,$(BOARD))) + DRIVER := at86rf231 +endif + +ifneq (,$(filter at86rf2%,$(DRIVER))) + FEATURES_REQUIRED += periph_spi periph_gpio +endif + +USEMODULE += $(DRIVER) + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 + +# Sensors +USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +USEMODULE += auto_init_saul +USEMODULE += tmp006 # ambient temperature +USEMODULE += hdc1000 # humidity and temperature +USEMODULE += fxos8700 # acceleration and magnetic field +USEMODULE += apds9007 # illumination +USEMODULE += ekmb1101111 # pir-based occupancy +USEMODULE += push_button # simple button + +USEMODULE += xtimer +USEMODULE += random +USEMODULE += ps + +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +#required for C++ compiling +CXXEXFLAGS += -fno-rtti +USEMODULE += cpp11-compat + +include $(RIOTBASE)/Makefile.include diff --git a/openthread/README.md b/openthread/README.md new file mode 100644 index 0000000..0bbdc31 --- /dev/null +++ b/openthread/README.md @@ -0,0 +1,46 @@ +## OpenThread on RIOT + +This test demonstrates how to use the [OpenThread](https://github.com/openthread/openthread) +open source implementation of [Thread](https://threadgroup.org/) on RIOT. + The [Command Line Interface](https://github.com/openthread/openthread/blob/master/examples/apps/cli/README.md) of +OpenThread was ported. Please check the [full documentation] +(https://github.com/openthread/openthread/blob/master/src/cli/README.md) of the CLI for usage information. + +You can either build a FTD or MTD firmware: +- MTD: A Minimal Thread Device does not have router functionality compiled in. An MTD may or may not be sleepy. +- FTD: A Full Thread Device has router functionality compiled in. + +## Quick usage + +With RIOT port, a node is auto-setup and ready to communicate with this configuration: +OPENTHREAD_PANID: 0xbeef +OPENTHREAD_CHANNEL: 26 + +You can pass the panid/channel independently when building the firmware: +`make BOARD= OPENTHREAD_PANID=0xaaaa OPENTHREAD_TYPE=ftd flash term` +`make BOARD= OPENTHREAD_CHANNEL=20 OPENTHREAD_TYPE=ftd flash term` + +To test OpenThread in RIOT, you can do the following: + +1. Flash nodes with MTD or FTD functionality: +`make BOARD= clean all flash OPENTHREAD_TYPE=mtd` +`make BOARD= clean all flash OPENTHREAD_TYPE=ftd` +2. Check the state of the node with `state`. In the beginning, it should be `detached`, but after some seconds it should + become `leader` +3. Start another node and check that it becomes `router`. There is only one leader in a Thread network. +4. Get the mesh IP address of a node with `ipaddr`. +``` +ipaddr + fdde:ad00:beef::ff:fe00:8000 + fe80::ff:fe00:8000 + fdde:ad00:beef:0:946a:c722:a5d9:8481 + fe80::3984:f4eb:d182:5dae +``` +5. Ping from another node with `ping fdde:ad00:beef:0:946a:c722:a5d9:848`. +6. You can try IEEE802.15.4 scan with `scan` command +7. You can also check other commands with `help` + + +## OpenThread port on RIOT status + +OpenThread port on RIOT is stable. In case of any bug, please report via GitHub issue. diff --git a/openthread/core b/openthread/core new file mode 100644 index 0000000..44a4b8a Binary files /dev/null and b/openthread/core differ diff --git a/openthread/main.c b/openthread/main.c new file mode 100644 index 0000000..878161e --- /dev/null +++ b/openthread/main.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2017 Baptiste CLENET + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @file + * @brief OpenThread test application + * + * @author Baptiste Clenet + */ + +#include +#include +#include "ot.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL (1000000UL) +#endif + +int main(void) +{ + puts("This a test for OpenThread"); + /* Example of how to call OpenThread stack functions */ + puts("Get PANID "); + //uint16_t panid = 0; + //uint8_t res = ot_call_command("panid", NULL, (void*)&panid); + //printf("Current panid: 0x%x (res:%x)\n", panid, res); + while (1) { + //Sample + //sample(&frontbuf); + //aes_populate(); + //Send + //send_udp("ff02::1",4747,obuffer,sizeof(obuffer)); + //Sleep + //xtimer_periodic_wakeup(&last_wakeup, interval_with_jitter()); + xtimer_usleep(SAMPLE_INTERVAL); + } + return 0; +} diff --git a/openthread_ncp/Makefile b/openthread_ncp/Makefile new file mode 100644 index 0000000..7210e8f --- /dev/null +++ b/openthread_ncp/Makefile @@ -0,0 +1,67 @@ +APPLICATION = openthread_ncp + +# If no BOARD is found in the environment, use this default: +BOARD ?= samr21-xpro + +# These are the boards that OpenThread stack has been tested on +BOARD_WHITELIST := hamilton samr21-xpro iotlab-m3 fox iotlab-a8-m3 + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE = /home/hskim/Desktop/wpantund/RIOT-OS + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +#CFLAGS += -DDEVELHELP -Wall + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +# NCP: A Network Co-Processor is used with wpantund software. wpantund is a user-space network +# interface driver/daemon that provides a native IPv6 network interface to a low-power wireless Network Co-Processor +# FTD (see openthread_test_ftd): NCP needs Full Thread Device to act as a router in the network +USEPKG += openthread +USEMODULE += openthread-ftd +USEMODULE += openthread-ncp-ftd + +#Define UART_BAUDRATE used by default +OPENTHREAD_UART_BAUDRATE ?= 115200 +CFLAGS += -DOPENTHREAD_UART_BAUDRATE=${OPENTHREAD_UART_BAUDRATE} +OPENTHREAD_PANID ?= 0xbeef +OPENTHREAD_CHANNEL ?= 26 + +ifneq (,$(filter hamilton samr21-xpro,$(BOARD))) + DRIVER := at86rf233 +endif +ifneq (,$(filter iotlab-m3 fox iotlab-a8-m3,$(BOARD))) + DRIVER := at86rf231 +endif + +ifneq (,$(filter at86rf2%,$(DRIVER))) + FEATURES_REQUIRED += periph_spi periph_gpio +endif + +USEMODULE += $(DRIVER) + +#CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 +# CPU clock speed +#CFLAGS += -DBORDER_ROUTER=1 + +# Sensors +#USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +#USEMODULE += auto_init_saul +#USEMODULE += fxos8700 # acceleration and magnetic field + +USEMODULE += xtimer +USEMODULE += random +USEMODULE += ps + +#USEMODULE += rtt_stdio +#CFLAGS += -DRTT_STDIO_DISABLE_STDIN +#CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +#required for C++ compiling +CXXEXFLAGS += -fno-rtti +USEMODULE += cpp11-compat + +include $(RIOTBASE)/Makefile.include diff --git a/openthread_ncp/README.md b/openthread_ncp/README.md new file mode 100644 index 0000000..940c35d --- /dev/null +++ b/openthread_ncp/README.md @@ -0,0 +1,43 @@ +## OpenThread on RIOT + +This test demonstrates how to use the [OpenThread](https://github.com/openthread/openthread) +open source implementation of [Thread](https://threadgroup.org/) on RIOT. + +This test shows how to use a Network Co-Processor (NCP). A Network Co-Processor is used with wpantund software. +Wpantund is a user-space network interface driver/daemon that provides a native IPv6 network interface to a low-power +wireless Network Co-Processor. NCP and wpantund communicates by UART (UART(0) is used here) + +## Quick usage + +To test OpenThread NCP in RIOT, you can do the following: + +1. Flash one node with openthread_ncp example with `make BOARD= clean all flash -C tests/openthread_ncp` +2. Install wpantund: On Ubuntu 16.04, run +``` +sudo apt-get -y install build-essential subversion libncurses5-dev libssl-dev zlib1g-dev gawk gcc-multilib flex git-core gettext gcc binutils bzip2 python perl make unzip libz-dev tftp git shtool autogen automake libtool autotools-dev libdbus-1-dev +git clone https://github.com/openthread/wpantund.git +cd wpantund +./bootstrap.sh +./configure +make +sudo make install +``` + +3. Start wpantund on your host. Be sure to pass flags appropriate to connect wpantund to your NCP connection type. +You can also name the network interface at this time or let wpantund automatically assign a name (wpan0): + +``` +sudo /usr/local/sbin/wpantund -s /dev/ttyACM0 -I wpan0 -o SyslogMask all +``` + +4. Start wpanctl to access the command line management interface for controlling Thread features on your device. +This is similar to the Thread cli interface conceptually but with slightly different command syntax: + +`sudo /usr/local/bin/wpanctl -I wpan0` + +5. With wpanctl CLI, you can use "status" cmd to check status of NCP, "scan" cmd scan for WPAN network around or "form" to create new WPAN network +6. Confirm your network interface has started and is operational by running standard IP commands such `ifconfig` and `ping6`. + +## OpenThread port on RIOT status + +OpenThread port on RIOT is stable. In case of any bug, please report via GitHub issue. diff --git a/openthread_ncp/main.c b/openthread_ncp/main.c new file mode 100644 index 0000000..284bc12 --- /dev/null +++ b/openthread_ncp/main.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017 Baptiste CLENET + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @file + * @brief OpenThread test application + * + * @author Baptiste Clenet + */ + +#include + +#include "ot.h" +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL (1000000UL) +#endif +int main(void) +{ + /* Run wpantund to interact with NCP */ + /*while(1) { + xtimer_usleep(SAMPLE_INTERVAL); + LED0_TOGGLE; + }*/ + return 0; +} diff --git a/openthread_samr21/Makefile b/openthread_samr21/Makefile new file mode 100644 index 0000000..559b78d --- /dev/null +++ b/openthread_samr21/Makefile @@ -0,0 +1,64 @@ +APPLICATION = openthread + +# If no BOARD is found in the environment, use this default: +BOARD ?= samr21-xpro + +# These are the boards that OpenThread stack has been tested on +BOARD_WHITELIST := samr21-xpro iotlab-m3 fox iotlab-a8-m3 + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE = /home/hskim/Desktop/wpantund/RIOT-OS + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +#CFLAGS += -DDEVELHELP -Wall + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +USEPKG += openthread +OPENTHREAD_TYPE ?= ftd +ifeq ($(OPENTHREAD_TYPE),mtd) + # MTD: A Minimal Thread Device does not have router functionality + # compiled in. As a result, it is not necessary to configure the + # routerrole on an MTD. At the same time, an MTD may or may not be sleepy. + USEMODULE += openthread-mtd + USEMODULE += openthread-cli-mtd +else + # ftd: A Full Thread Device has router functionality compiled in + USEMODULE += openthread-ftd + USEMODULE += openthread-cli-ftd +endif + +#Define PANID, CHANNEL and UART_BAUDRATE used by default +OPENTHREAD_PANID ?= 0xbeef +OPENTHREAD_CHANNEL ?= 26 +OPENTHREAD_UART_BAUDRATE ?= 115200 + +CFLAGS += -DOPENTHREAD_PANID=${OPENTHREAD_PANID} +CFLAGS += -DOPENTHREAD_CHANNEL=${OPENTHREAD_CHANNEL} +CFLAGS += -DOPENTHREAD_UART_BAUDRATE=${OPENTHREAD_UART_BAUDRATE} + +ifneq (,$(filter samr21-xpro,$(BOARD))) + DRIVER := at86rf233 +endif +ifneq (,$(filter iotlab-m3 fox iotlab-a8-m3,$(BOARD))) + DRIVER := at86rf231 +endif + +ifneq (,$(filter at86rf2%,$(DRIVER))) + FEATURES_REQUIRED += periph_spi periph_gpio +endif + +USEMODULE += $(DRIVER) + +USEMODULE += xtimer +USEMODULE += random +USEMODULE += ps + +#required for C++ compiling +CXXEXFLAGS += -fno-rtti +USEMODULE += cpp11-compat + +include $(RIOTBASE)/Makefile.include diff --git a/openthread_samr21/README.md b/openthread_samr21/README.md new file mode 100644 index 0000000..0bbdc31 --- /dev/null +++ b/openthread_samr21/README.md @@ -0,0 +1,46 @@ +## OpenThread on RIOT + +This test demonstrates how to use the [OpenThread](https://github.com/openthread/openthread) +open source implementation of [Thread](https://threadgroup.org/) on RIOT. + The [Command Line Interface](https://github.com/openthread/openthread/blob/master/examples/apps/cli/README.md) of +OpenThread was ported. Please check the [full documentation] +(https://github.com/openthread/openthread/blob/master/src/cli/README.md) of the CLI for usage information. + +You can either build a FTD or MTD firmware: +- MTD: A Minimal Thread Device does not have router functionality compiled in. An MTD may or may not be sleepy. +- FTD: A Full Thread Device has router functionality compiled in. + +## Quick usage + +With RIOT port, a node is auto-setup and ready to communicate with this configuration: +OPENTHREAD_PANID: 0xbeef +OPENTHREAD_CHANNEL: 26 + +You can pass the panid/channel independently when building the firmware: +`make BOARD= OPENTHREAD_PANID=0xaaaa OPENTHREAD_TYPE=ftd flash term` +`make BOARD= OPENTHREAD_CHANNEL=20 OPENTHREAD_TYPE=ftd flash term` + +To test OpenThread in RIOT, you can do the following: + +1. Flash nodes with MTD or FTD functionality: +`make BOARD= clean all flash OPENTHREAD_TYPE=mtd` +`make BOARD= clean all flash OPENTHREAD_TYPE=ftd` +2. Check the state of the node with `state`. In the beginning, it should be `detached`, but after some seconds it should + become `leader` +3. Start another node and check that it becomes `router`. There is only one leader in a Thread network. +4. Get the mesh IP address of a node with `ipaddr`. +``` +ipaddr + fdde:ad00:beef::ff:fe00:8000 + fe80::ff:fe00:8000 + fdde:ad00:beef:0:946a:c722:a5d9:8481 + fe80::3984:f4eb:d182:5dae +``` +5. Ping from another node with `ping fdde:ad00:beef:0:946a:c722:a5d9:848`. +6. You can try IEEE802.15.4 scan with `scan` command +7. You can also check other commands with `help` + + +## OpenThread port on RIOT status + +OpenThread port on RIOT is stable. In case of any bug, please report via GitHub issue. diff --git a/openthread_samr21/main.c b/openthread_samr21/main.c new file mode 100644 index 0000000..facd03f --- /dev/null +++ b/openthread_samr21/main.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017 Baptiste CLENET + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @file + * @brief OpenThread test application + * + * @author Baptiste Clenet + */ + +#include + +#include "ot.h" + +int main(void) +{ + puts("This a test for OpenThread"); + /* Example of how to call OpenThread stack functions */ + puts("Get PANID "); + uint16_t panid = 0; + uint8_t res = ot_call_command("panid", NULL, (void*)&panid); + printf("Current panid: 0x%x (res:%x)\n", panid, res); + + return 0; +} diff --git a/samr21-basic/Makefile b/samr21-basic/Makefile new file mode 100644 index 0000000..9fcc082 --- /dev/null +++ b/samr21-basic/Makefile @@ -0,0 +1,48 @@ +APPLICATION = samr21-basic +RIOTBASE = /home/hskim/Desktop/RIOT-OS +BOARD = samr21-xpro + +# System functions +USEMODULE += random +USEMODULE += xtimer +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 +CFLAGS += -DCLOCK_USE_ADAPTIVE=0 + +# Computation offload +CFLAGS += -DSENSOR_COMPUTOFFLOAD=0 + +# Radio +USEMODULE += at86rf2xx +CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=26 + +# Network +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Duty-cycling +#USEMODULE += gnrc_lasmac +CFLAGS += -DDUTYCYCLE_SLEEP_INTERVAL=0000UL # If it is ZERO, no duty-cycling for packet reception +CFLAGS += -DLEAF_NODE=1 # Default is 1 +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6 +USEMODULE += gnrc_udp +CFLAGS += -DSOCK_HAS_IPV6 +# Add also the shell, some shell commands +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 + + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +# CFLAGS += -DDEVELHELP + +QUIET ?= 1 + +FEATURES_REQUIRED += periph_timer + +include $(RIOTBASE)/Makefile.include diff --git a/samr21-basic/main.c b/samr21-basic/main.c new file mode 100644 index 0000000..af39e5d --- /dev/null +++ b/samr21-basic/main.c @@ -0,0 +1,23 @@ +#include +#include +#include "xtimer.h" +#include +#include "periph/timer.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL ( 1000000UL) +#endif + +int main(void) { + xtimer_ticks32_t last_wakeup = xtimer_now(); + while (1) { + LED0_TOGGLE; + //Sleep + xtimer_periodic_wakeup(&last_wakeup, SAMPLE_INTERVAL); + } + + return 0; +} diff --git a/udp-recv-app/Makefile b/udp-recv-app/Makefile new file mode 100644 index 0000000..324af3e --- /dev/null +++ b/udp-recv-app/Makefile @@ -0,0 +1,61 @@ +APPLICATION = udp_recv_app +RIOTBASE ?= /home/hskim/Desktop/rebase/RIOT-OS +BOARD = hamilton + +# System functions +USEMODULE += random +USEMODULE += xtimer +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 + +# Sensors +USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +USEMODULE += auto_init_saul +USEMODULE += tmp006 # ambient temperature +USEMODULE += hdc1000 # humidity and temperature +USEMODULE += fxos8700 # acceleration and magnetic field +USEMODULE += apds9007 # illumination +USEMODULE += ekmb1101111 # pir-based occupancy +USEMODULE += push_button # simple button + +# Security +USEMODULE += crypto +USEMODULE += cipher_modes +CFLAGS += -DCRYPTO_AES + +CFLAGS += -DAUTO_CSMA_EN=1 + +# Radio +USEMODULE += at86rf2xx +CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=26 + +# Network +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Duty-cycling +USEMODULE += gnrc_lasmac +CFLAGS += -DDUTYCYCLE_SLEEP_INTERVAL=00000UL # If it is ZERO, no duty-cycling for packet reception +CFLAGS += -DLEAF_NODE=0 # Default is 1 +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6 +USEMODULE += gnrc_udp +CFLAGS += -DSOCK_HAS_IPV6 +# Add also the shell, some shell commands +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 + + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +# CFLAGS += -DDEVELHELP + +QUIET ?= 1 + +FEATURES_REQUIRED += periph_timer + +include $(RIOTBASE)/Makefile.include diff --git a/udp-recv-app/main.c b/udp-recv-app/main.c new file mode 100644 index 0000000..359c0c1 --- /dev/null +++ b/udp-recv-app/main.c @@ -0,0 +1,54 @@ +#include +#include +#include "xtimer.h" +#include +#include "net/gnrc/pktdump.h" +#include "net/gnrc/udp.h" +#include "phydat.h" +#include "saul_reg.h" +#include "periph/adc.h" +#include "periph/dmac.h" +#include "periph/i2c.h" +#include "periph/spi.h" +#include "periph/timer.h" + +#define ENABLE_DEBUG (1) +#include "debug.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL (100000000UL) +#endif + +void send_udp(char *addr_str, uint16_t port, uint8_t *data, uint16_t datalen); + +int main(void) { + //This value is good randomness and unique per mote + srand(*((uint32_t*)fb_aes128_key)); + dmac_init(); + spi_set_dma_channel(0, 0, 1); + + LED_OFF; + + msg_t msg_queue[8]; + msg_init_queue(msg_queue, 8); + + gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, sched_active_pid); + gnrc_netreg_register(GNRC_NETTYPE_UDP, &server); + + msg_t msg; + char stringbuf[127]; + for (;;) { + msg_receive(&msg); + + gnrc_pktsnip_t* pkt = msg.content.ptr; + uint8_t* data = pkt->data + 8; // Don't use UDP header + size_t data_size = pkt->size - 8; + size_t to_copy = (data_size < sizeof(stringbuf)) ? data_size : (sizeof(stringbuf) - 1); + memcpy(stringbuf, data, to_copy); + stringbuf[to_copy] = '\0'; + printf("Got a packet: %s\n", stringbuf); + gnrc_pktbuf_release(pkt); + } + + return 0; +} diff --git a/udp-recv-app/udp.c b/udp-recv-app/udp.c new file mode 100644 index 0000000..8d38842 --- /dev/null +++ b/udp-recv-app/udp.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup examples + * @{ + * + * @file + * @brief Demonstrating the sending and receiving of UDP data + * + * @author Hauke Petersen + * + * @} + */ + +#if MODULE_GNRC_UDP +#include +#include + +#include "net/gnrc.h" +#include "net/gnrc/ipv6.h" +#include "net/gnrc/udp.h" +#include "net/gnrc/pktdump.h" +#include "timex.h" +#include "xtimer.h" + + +void send_udp(char *addr_str, uint16_t port, uint8_t *data, uint16_t datalen) +{ + ipv6_addr_t addr; + + /* parse destination address */ + if (ipv6_addr_from_str(&addr, addr_str) == NULL) { + puts("Error: unable to parse destination address"); + return; + } + + gnrc_pktsnip_t *payload, *udp, *ip; + /* allocate payload */ + payload = gnrc_pktbuf_add(NULL, data, datalen, GNRC_NETTYPE_UNDEF); + if (payload == NULL) { + puts("Error: unable to copy data to packet buffer"); + return; + } + /* allocate UDP header, set source port := destination port */ + udp = gnrc_udp_hdr_build(payload, port, port); + if (udp == NULL) { + puts("Error: unable to allocate UDP header"); + gnrc_pktbuf_release(payload); + return; + } + /* allocate IPv6 header */ + ip = gnrc_ipv6_hdr_build(udp, NULL, &addr); + if (ip == NULL) { + puts("Error: unable to allocate IPv6 header"); + gnrc_pktbuf_release(udp); + return; + } + /* send packet */ + if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { + puts("Error: unable to locate UDP thread"); + gnrc_pktbuf_release(ip); + return; + } +} +#endif diff --git a/udp-send-app/Makefile b/udp-send-app/Makefile new file mode 100644 index 0000000..c47c747 --- /dev/null +++ b/udp-send-app/Makefile @@ -0,0 +1,61 @@ +APPLICATION = udp_send_app +RIOTBASE ?= /home/hskim/Desktop/rebase/RIOT-OS +BOARD = hamilton + +# System functions +USEMODULE += random +USEMODULE += xtimer +USEMODULE += rtt_stdio +CFLAGS += -DRTT_STDIO_DISABLE_STDIN +CFLAGS += -DTHREAD_STACKSIZE_DEFAULT=2048 + +# CPU clock speed +CFLAGS += -DCLOCK_USE_OSCULP32_DFLL=1 + +# Sensors +USEMODULE += saul_reg # SAUL: sensor/actuator abstraction layer +USEMODULE += auto_init_saul +USEMODULE += tmp006 # ambient temperature +USEMODULE += hdc1000 # humidity and temperature +USEMODULE += fxos8700 # acceleration and magnetic field +USEMODULE += apds9007 # illumination +USEMODULE += ekmb1101111 # pir-based occupancy +USEMODULE += push_button # simple button + +# Security +USEMODULE += crypto +USEMODULE += cipher_modes +CFLAGS += -DCRYPTO_AES + +CFLAGS += -DAUTO_CSMA_EN=1 + +# Radio +USEMODULE += at86rf2xx +CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=26 + +# Network +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Duty-cycling +USEMODULE += gnrc_lasmac +CFLAGS += -DDUTYCYCLE_SLEEP_INTERVAL=00000UL # If it is ZERO, no duty-cycling for packet reception +CFLAGS += -DLEAF_NODE=1 # Default is 1 +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6 +USEMODULE += gnrc_udp +CFLAGS += -DSOCK_HAS_IPV6 +# Add also the shell, some shell commands +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 + + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +# CFLAGS += -DDEVELHELP + +QUIET ?= 1 + +FEATURES_REQUIRED += periph_timer + +include $(RIOTBASE)/Makefile.include diff --git a/udp-send-app/main.c b/udp-send-app/main.c new file mode 100644 index 0000000..ab2b1e9 --- /dev/null +++ b/udp-send-app/main.c @@ -0,0 +1,40 @@ +#include +#include +#include "xtimer.h" +#include +#include "net/gnrc/udp.h" +#include "phydat.h" +#include "saul_reg.h" +#include "periph/adc.h" +#include "periph/dmac.h" +#include "periph/i2c.h" +#include "periph/spi.h" +#include "periph/timer.h" + +#define ENABLE_DEBUG (1) +#include "debug.h" + +#ifndef SAMPLE_INTERVAL +#define SAMPLE_INTERVAL (1000000UL) +#endif + +void send_udp(char *addr_str, uint16_t port, uint8_t *data, uint16_t datalen); + +int main(void) { + //This value is good randomness and unique per mote + srand(*((uint32_t*)fb_aes128_key)); + dmac_init(); + spi_set_dma_channel(0, 0, 1); + + LED_OFF; + + while (1) { + char* message = "Hello, world!"; + uint16_t message_length = (uint16_t) strlen(message); + send_udp("ff02::1", 8943, (uint8_t*) message, message_length); + //Sleep + xtimer_usleep(SAMPLE_INTERVAL); + } + + return 0; +} diff --git a/udp-send-app/udp.c b/udp-send-app/udp.c new file mode 100644 index 0000000..8d38842 --- /dev/null +++ b/udp-send-app/udp.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup examples + * @{ + * + * @file + * @brief Demonstrating the sending and receiving of UDP data + * + * @author Hauke Petersen + * + * @} + */ + +#if MODULE_GNRC_UDP +#include +#include + +#include "net/gnrc.h" +#include "net/gnrc/ipv6.h" +#include "net/gnrc/udp.h" +#include "net/gnrc/pktdump.h" +#include "timex.h" +#include "xtimer.h" + + +void send_udp(char *addr_str, uint16_t port, uint8_t *data, uint16_t datalen) +{ + ipv6_addr_t addr; + + /* parse destination address */ + if (ipv6_addr_from_str(&addr, addr_str) == NULL) { + puts("Error: unable to parse destination address"); + return; + } + + gnrc_pktsnip_t *payload, *udp, *ip; + /* allocate payload */ + payload = gnrc_pktbuf_add(NULL, data, datalen, GNRC_NETTYPE_UNDEF); + if (payload == NULL) { + puts("Error: unable to copy data to packet buffer"); + return; + } + /* allocate UDP header, set source port := destination port */ + udp = gnrc_udp_hdr_build(payload, port, port); + if (udp == NULL) { + puts("Error: unable to allocate UDP header"); + gnrc_pktbuf_release(payload); + return; + } + /* allocate IPv6 header */ + ip = gnrc_ipv6_hdr_build(udp, NULL, &addr); + if (ip == NULL) { + puts("Error: unable to allocate IPv6 header"); + gnrc_pktbuf_release(udp); + return; + } + /* send packet */ + if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { + puts("Error: unable to locate UDP thread"); + gnrc_pktbuf_release(ip); + return; + } +} +#endif