Skip to content

Commit f37de86

Browse files
[Thread Tutorial] Add Openthread starter code
1 parent c9e0a16 commit f37de86

File tree

3 files changed

+155
-247
lines changed

3 files changed

+155
-247
lines changed
File renamed without changes.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
#define UDP_PORT 1212
22+
static const char UDP_ROUTER_MULTICAST[] = "ff02::2";
23+
24+
// helper utility demonstrating network config setup
25+
static void setNetworkConfiguration(otInstance* aInstance);
26+
27+
// callback for Thread state change events
28+
static void stateChangeCallback(uint32_t flags, void* context);
29+
30+
// helper utility to print ip address
31+
static void print_ip_addr(otInstance* instance);
32+
33+
int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[]) {
34+
// Initialize OpenThread instance.
35+
otSysInit(argc, argv);
36+
otInstance* instance;
37+
instance = otInstanceInitSingle();
38+
assert(instance);
39+
40+
// set child timeout to 60 seconds.
41+
otThreadSetChildTimeout(instance, 60);
42+
43+
// Set callback to be notified when thread state changes.
44+
otSetStateChangedCallback(instance, stateChangeCallback, instance);
45+
46+
///////////////////////////////////////////////////
47+
// THREAD NETWORK SETUP HERE
48+
49+
// TODO: Configure network.
50+
51+
// TODO: Enable network interface.
52+
53+
// TODO: Start Thread network.
54+
55+
//
56+
////////////////////////////////////////////////////
57+
58+
// OpenThread main loop.
59+
for (;;) {
60+
// Execute any pending OpenThread related work.
61+
otTaskletsProcess(instance);
62+
63+
// Execute any platform related work (e.g. check
64+
// radio buffer for new packets).
65+
otSysProcessDrivers(instance);
66+
67+
// If there is not pending platform or OpenThread
68+
// related work -- yield.
69+
if (!otTaskletsArePending(instance) &&
70+
!openthread_platform_pending_work())
71+
{
72+
yield();
73+
}
74+
75+
}
76+
77+
return 0;
78+
}
79+
80+
// Helper method that configures the OpenThread network dataset
81+
// for the desired tutorial configuration.
82+
// We set the following dataset parameters:
83+
// -- Channel: 26
84+
// -- PanId: 0xabcd
85+
// -- Networkkey: 00112233445566778899aabbccddeeff
86+
void setNetworkConfiguration(otInstance* aInstance) {
87+
otOperationalDataset aDataset;
88+
89+
memset(&aDataset, 0, sizeof(otOperationalDataset));
90+
91+
/* Set Channel to 26 */
92+
aDataset.mChannel = 26;
93+
aDataset.mComponents.mIsChannelPresent = true;
94+
95+
/* Set Pan ID to abcd */
96+
aDataset.mPanId = (otPanId)0xabcd;
97+
aDataset.mComponents.mIsPanIdPresent = true;
98+
99+
/* Set network key to 00112233445566778899aabbccddeeff */
100+
uint8_t key[OT_NETWORK_KEY_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
101+
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
102+
memcpy(aDataset.mNetworkKey.m8, key, sizeof(aDataset.mNetworkKey));
103+
aDataset.mComponents.mIsNetworkKeyPresent = true;
104+
105+
otError error = otDatasetSetActive(aInstance, &aDataset);
106+
assert(error == 0);
107+
108+
}
109+
110+
// Helper method that registers a stateChangeCallback to print
111+
// when state changes occur (useful for debugging).
112+
static void stateChangeCallback(uint32_t flags, void* context) {
113+
otInstance* instance = (otInstance*)context;
114+
if (!(flags & OT_CHANGED_THREAD_ROLE)) {
115+
return;
116+
}
117+
118+
switch (otThreadGetDeviceRole(instance)) {
119+
case OT_DEVICE_ROLE_DISABLED:
120+
printf("[State Change] - Disabled.\n");
121+
break;
122+
case OT_DEVICE_ROLE_DETACHED:
123+
printf("[State Change] - Detached.\n");
124+
break;
125+
case OT_DEVICE_ROLE_CHILD:
126+
network_up = true;
127+
sendUdp(instance);
128+
printf("[State Change] - Child.\n");
129+
printf("Successfully attached to Thread network as a child.\n");
130+
break;
131+
case OT_DEVICE_ROLE_ROUTER:
132+
printf("[State Change] - Router.\n");
133+
break;
134+
case OT_DEVICE_ROLE_LEADER:
135+
printf("[State Change] - Leader.\n");
136+
break;
137+
default:
138+
break;
139+
}
140+
}
141+
142+
// Helper method to print the given Thread node's registered
143+
// ipv6 address.
144+
static void print_ip_addr(otInstance* instance) {
145+
char addr_string[64];
146+
const otNetifAddress* unicastAddrs = otIp6GetUnicastAddresses(instance);
147+
148+
printf("[THREAD] Device IPv6 Addresses: ");
149+
for (const otNetifAddress* addr = unicastAddrs; addr; addr = addr->mNext) {
150+
const otIp6Address ip6_addr = addr->mAddress;
151+
otIp6AddressToString(&ip6_addr, addr_string, sizeof(addr_string));
152+
printf("%s\n", addr_string);
153+
}
154+
}
155+

0 commit comments

Comments
 (0)