Skip to content

Commit d4f3da4

Browse files
authored
Merge pull request #517 from tyler-potyondy/mobisys-tutorial
MobiSys Tutorial 2025: OpenThread Module
2 parents b9d1585 + e94181d commit d4f3da4

File tree

38 files changed

+933
-13
lines changed

38 files changed

+933
-13
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Thread Tutorials
2+
3+
There are two Thread tutorials:
4+
- Encrypted Sensor Data
5+
- Temperature Sensor
6+
7+
Both tutorials introduce using Tock and OpenThread in addition
8+
to presenting various components of Tock's design. They are
9+
both intended as standalone seperate tutorials (although there
10+
is some overlap in presenting the OpenThread concepts). The guide
11+
for these tutorials can be found at:
12+
13+
- https://book.tockos.org/course/thread-tutorials/overview.html
14+
15+
### Encrypted Sensor Data Tutorial
16+
17+
This tutorial teaches participants how to create an OpenThread
18+
app (using Tock's OpenThread port) that is able to join a Thread
19+
network and send/receive UDP packets. This tutorial focuses on the
20+
ways in which Tock's can be used as a secure root of trust and how
21+
this can be used to create secure, networked IoT devices.
22+
23+
### Temperature Sensor
24+
25+
This tutorial teaches participants how to create an OpenThread
26+
app (using Tock's OpenThread port) that is able to join a Thread
27+
network and send/receive UDP packets. This tutorial guides participants
28+
in creating a networked temperature sensor that includes user button
29+
input, reading from a sensor, and displaying information on a screen.
30+
This introduces concepts in Tock such as syscalls, callbacks, and
31+
interprocess communication. This tutorial also presents how Tock can
32+
uniquely be used to create robust applications by leveraging Tock's
33+
by-default process isolation.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../../..
5+
6+
# Specify this app depends on the MTD OpenThread library.
7+
include $(TOCK_USERLAND_BASE_DIR)/libopenthread/libopenthread-mtd.mk
8+
9+
# set stack size to 8000 to support openthread app
10+
STACK_SIZE:=8000
11+
12+
# App name
13+
PACKAGE_NAME:=openthread_sensor
14+
15+
C_SRCS := main.c
16+
17+
# Include userland master makefile. Contains rules and flags for actually
18+
# building the application.
19+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include <assert.h>
2+
3+
#include <libopenthread/platform/openthread-system.h>
4+
#include <libopenthread/platform/plat.h>
5+
#include <openthread/dataset_ftd.h>
6+
#include <openthread/instance.h>
7+
#include <openthread/ip6.h>
8+
#include <openthread/message.h>
9+
#include <openthread/platform/alarm-milli.h>
10+
#include <openthread/tasklet.h>
11+
#include <openthread/thread.h>
12+
#include <openthread/udp.h>
13+
14+
#include <libtock/interface/led.h>
15+
16+
#include <libtock/tock.h>
17+
18+
#include <stdio.h>
19+
#include <string.h>
20+
21+
// helper utility demonstrating network config setup
22+
static void __attribute__((unused)) setNetworkConfiguration(otInstance* aInstance);
23+
24+
// callback for Thread state change events
25+
static void stateChangeCallback(uint32_t flags, void* context);
26+
27+
// helper utility to print ip address
28+
static void __attribute__((unused)) print_ip_addr(otInstance* instance);
29+
30+
int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[]) {
31+
// Initialize OpenThread instance.
32+
otSysInit(argc, argv);
33+
otInstance* instance;
34+
instance = otInstanceInitSingle();
35+
assert(instance);
36+
37+
// Set child timeout to 60 seconds.
38+
otThreadSetChildTimeout(instance, 60);
39+
40+
// Set callback to be notified when thread state changes.
41+
otSetStateChangedCallback(instance, stateChangeCallback, instance);
42+
43+
///////////////////////////////////////////////////
44+
// THREAD NETWORK SETUP HERE
45+
46+
// TODO: Configure network.
47+
48+
// TODO: Enable network interface.
49+
50+
// TODO: Start Thread network.
51+
52+
//
53+
////////////////////////////////////////////////////
54+
55+
// OpenThread main loop
56+
for ( ;;) {
57+
// Execute any pending OpenThread related work.
58+
otTaskletsProcess(instance);
59+
60+
// Execute any platform related work (e.g. check
61+
// radio buffer for new packets).
62+
otSysProcessDrivers(instance);
63+
64+
if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
65+
yield();
66+
}
67+
68+
}
69+
70+
return 0;
71+
}
72+
73+
// Helper method that configures the OpenThread network dataset
74+
// for the desired tutorial configuration.
75+
// We set the following dataset parameters:
76+
// -- Channel: 26
77+
// -- PanId: 0xabcd
78+
// -- Networkkey: 00112233445566778899aabbccddeeff
79+
void setNetworkConfiguration(otInstance* aInstance) {
80+
otOperationalDataset aDataset;
81+
82+
memset(&aDataset, 0, sizeof(otOperationalDataset));
83+
84+
/* Set Channel to 26 */
85+
aDataset.mChannel = 26;
86+
aDataset.mComponents.mIsChannelPresent = true;
87+
88+
/* Set Pan ID to abcd */
89+
aDataset.mPanId = (otPanId)0xabcd;
90+
aDataset.mComponents.mIsPanIdPresent = true;
91+
92+
/* Set network key to 00112233445566778899aabbccddeeff */
93+
uint8_t key[OT_NETWORK_KEY_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
94+
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
95+
memcpy(aDataset.mNetworkKey.m8, key, sizeof(aDataset.mNetworkKey));
96+
aDataset.mComponents.mIsNetworkKeyPresent = true;
97+
98+
otError error = otDatasetSetActive(aInstance, &aDataset);
99+
assert(error == 0);
100+
101+
}
102+
103+
// Helper method that registers a stateChangeCallback to print
104+
// when state changes occur (useful for debugging).
105+
static void stateChangeCallback(uint32_t flags, void* context) {
106+
otInstance* instance = (otInstance*)context;
107+
if (!(flags & OT_CHANGED_THREAD_ROLE)) {
108+
return;
109+
}
110+
111+
switch (otThreadGetDeviceRole(instance)) {
112+
case OT_DEVICE_ROLE_DISABLED:
113+
printf("[State Change] - Disabled.\n");
114+
break;
115+
case OT_DEVICE_ROLE_DETACHED:
116+
printf("[State Change] - Detached.\n");
117+
break;
118+
case OT_DEVICE_ROLE_CHILD:
119+
printf("[State Change] - Child.\n");
120+
printf("Successfully attached to Thread network as a child.\n");
121+
break;
122+
case OT_DEVICE_ROLE_ROUTER:
123+
printf("[State Change] - Router.\n");
124+
break;
125+
case OT_DEVICE_ROLE_LEADER:
126+
printf("[State Change] - Leader.\n");
127+
break;
128+
default:
129+
break;
130+
}
131+
}
132+
133+
// Helper method to print the given Thread node's registered
134+
// ipv6 address.
135+
static void print_ip_addr(otInstance* instance) {
136+
char addr_string[64];
137+
const otNetifAddress* unicastAddrs = otIp6GetUnicastAddresses(instance);
138+
139+
for (const otNetifAddress* addr = unicastAddrs; addr; addr = addr->mNext) {
140+
const otIp6Address ip6_addr = addr->mAddress;
141+
otIp6AddressToString(&ip6_addr, addr_string, sizeof(addr_string));
142+
printf("%s\n", addr_string);
143+
}
144+
145+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../../..
5+
6+
# Specify this app depends on the MTD OpenThread library.
7+
include $(TOCK_USERLAND_BASE_DIR)/libopenthread/libopenthread-mtd.mk
8+
9+
# set stack size to 8000 to support openthread app
10+
STACK_SIZE:=8000
11+
12+
# App name
13+
PACKAGE_NAME:=openthread_sensor
14+
15+
C_SRCS := main.c
16+
17+
# Include userland master makefile. Contains rules and flags for actually
18+
# building the application.
19+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../../..
5+
6+
# Specify this app depends on the MTD OpenThread library.
7+
include $(TOCK_USERLAND_BASE_DIR)/libopenthread/libopenthread-mtd.mk
8+
9+
# set stack size to 8000 to support openthread app
10+
STACK_SIZE:=8000
11+
12+
# App name
13+
PACKAGE_NAME:=openthread_sensor
14+
15+
C_SRCS := main.c
16+
17+
# Include userland master makefile. Contains rules and flags for actually
18+
# building the application.
19+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk

0 commit comments

Comments
 (0)