Skip to content

Commit 1b4cedb

Browse files
[Thread Tutorial] Update OpenThread
- Remove IPC from communication app - Break into steps of join Thread network and add UDP interface
1 parent 6fc8231 commit 1b4cedb

File tree

6 files changed

+436
-5
lines changed

6 files changed

+436
-5
lines changed
File renamed without changes.

examples/tutorials/thread_network/02_openthread/main.c renamed to examples/tutorials/thread_network/03_openthread/main.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include <libtock/services/alarm.h>
1919
#include <libtock/tock.h>
2020

21-
#define UDP_PORT 1212
22-
static const char UDP_ROUTER_MULTICAST[] = "ff02::2";
23-
2421
// helper utility demonstrating network config setup
2522
static void setNetworkConfiguration(otInstance* aInstance);
2623

@@ -123,8 +120,6 @@ static void stateChangeCallback(uint32_t flags, void* context) {
123120
printf("[State Change] - Detached.\n");
124121
break;
125122
case OT_DEVICE_ROLE_CHILD:
126-
network_up = true;
127-
sendUdp(instance);
128123
printf("[State Change] - Child.\n");
129124
printf("Successfully attached to Thread network as a child.\n");
130125
break;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
# Specify this app depends on the MTD OpenThread library.
10+
include $(TOCK_USERLAND_BASE_DIR)/libopenthread/libopenthread-mtd.mk
11+
12+
# set stack size to 8000 to support openthread app
13+
STACK_SIZE:=8000
14+
APP_HEAP_SIZE:=5000
15+
16+
PACKAGE_NAME = org.tockos.thread-tutorial.openthread
17+
18+
# Include userland master makefile. Contains rules and flags for actually
19+
# building the application.
20+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#include <assert.h>
5+
6+
#include <libopenthread/platform/openthread-system.h>
7+
#include <libopenthread/platform/plat.h>
8+
#include <openthread/dataset_ftd.h>
9+
#include <openthread/instance.h>
10+
#include <openthread/ip6.h>
11+
#include <openthread/platform/alarm-milli.h>
12+
#include <openthread/tasklet.h>
13+
#include <openthread/thread.h>
14+
#include <openthread/udp.h>
15+
16+
#include <libtock-sync/services/alarm.h>
17+
#include <libtock/kernel/ipc.h>
18+
#include <libtock/services/alarm.h>
19+
#include <libtock/tock.h>
20+
21+
// helper utility demonstrating network config setup
22+
static void 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 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+
// Configure network.
47+
setNetworkConfiguration(instance);
48+
49+
// Enable network interface.
50+
while (otIp6SetEnabled(instance, true) != OT_ERROR_NONE) {
51+
printf("Failed to start Thread network interface!\n");
52+
libtocksync_alarm_delay_ms(100);
53+
}
54+
55+
// Print IPv6 address.
56+
print_ip_addr(instance);
57+
58+
// Start Thread network.
59+
while (otThreadSetEnabled(instance, true) != OT_ERROR_NONE) {
60+
printf("Failed to start Thread stack!\n");
61+
libtocksync_alarm_delay_ms(100);
62+
}
63+
64+
//
65+
////////////////////////////////////////////////////
66+
67+
// OpenThread main loop.
68+
for (;;) {
69+
// Execute any pending OpenThread related work.
70+
otTaskletsProcess(instance);
71+
72+
// Execute any platform related work (e.g. check
73+
// radio buffer for new packets).
74+
otSysProcessDrivers(instance);
75+
76+
// If there is not pending platform or OpenThread
77+
// related work -- yield.
78+
if (!otTaskletsArePending(instance) &&
79+
!openthread_platform_pending_work())
80+
{
81+
yield();
82+
}
83+
84+
}
85+
86+
return 0;
87+
}
88+
89+
// Helper method that configures the OpenThread network dataset
90+
// for the desired tutorial configuration.
91+
// We set the following dataset parameters:
92+
// -- Channel: 26
93+
// -- PanId: 0xabcd
94+
// -- Networkkey: 00112233445566778899aabbccddeeff
95+
void setNetworkConfiguration(otInstance* aInstance) {
96+
otOperationalDataset aDataset;
97+
98+
memset(&aDataset, 0, sizeof(otOperationalDataset));
99+
100+
/* Set Channel to 26 */
101+
aDataset.mChannel = 26;
102+
aDataset.mComponents.mIsChannelPresent = true;
103+
104+
/* Set Pan ID to abcd */
105+
aDataset.mPanId = (otPanId)0xabcd;
106+
aDataset.mComponents.mIsPanIdPresent = true;
107+
108+
/* Set network key to 00112233445566778899aabbccddeeff */
109+
uint8_t key[OT_NETWORK_KEY_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
110+
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
111+
memcpy(aDataset.mNetworkKey.m8, key, sizeof(aDataset.mNetworkKey));
112+
aDataset.mComponents.mIsNetworkKeyPresent = true;
113+
114+
otError error = otDatasetSetActive(aInstance, &aDataset);
115+
assert(error == 0);
116+
117+
}
118+
119+
// Helper method that registers a stateChangeCallback to print
120+
// when state changes occur (useful for debugging).
121+
static void stateChangeCallback(uint32_t flags, void* context) {
122+
otInstance* instance = (otInstance*)context;
123+
if (!(flags & OT_CHANGED_THREAD_ROLE)) {
124+
return;
125+
}
126+
127+
switch (otThreadGetDeviceRole(instance)) {
128+
case OT_DEVICE_ROLE_DISABLED:
129+
printf("[State Change] - Disabled.\n");
130+
break;
131+
case OT_DEVICE_ROLE_DETACHED:
132+
printf("[State Change] - Detached.\n");
133+
break;
134+
case OT_DEVICE_ROLE_CHILD:
135+
printf("[State Change] - Child.\n");
136+
printf("Successfully attached to Thread network as a child.\n");
137+
break;
138+
case OT_DEVICE_ROLE_ROUTER:
139+
printf("[State Change] - Router.\n");
140+
break;
141+
case OT_DEVICE_ROLE_LEADER:
142+
printf("[State Change] - Leader.\n");
143+
break;
144+
default:
145+
break;
146+
}
147+
}
148+
149+
// Helper method to print the given Thread node's registered
150+
// ipv6 address.
151+
static void print_ip_addr(otInstance* instance) {
152+
char addr_string[64];
153+
const otNetifAddress* unicastAddrs = otIp6GetUnicastAddresses(instance);
154+
155+
printf("[THREAD] Device IPv6 Addresses: ");
156+
for (const otNetifAddress* addr = unicastAddrs; addr; addr = addr->mNext) {
157+
const otIp6Address ip6_addr = addr->mAddress;
158+
otIp6AddressToString(&ip6_addr, addr_string, sizeof(addr_string));
159+
printf("%s\n", addr_string);
160+
}
161+
}
162+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
# Specify this app depends on the MTD OpenThread library.
10+
include $(TOCK_USERLAND_BASE_DIR)/libopenthread/libopenthread-mtd.mk
11+
12+
# set stack size to 8000 to support openthread app
13+
STACK_SIZE:=8000
14+
APP_HEAP_SIZE:=5000
15+
16+
PACKAGE_NAME = org.tockos.thread-tutorial.openthread
17+
18+
# Include userland master makefile. Contains rules and flags for actually
19+
# building the application.
20+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk

0 commit comments

Comments
 (0)