|
| 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 | +} |
0 commit comments