Magic Packet sender with Openthread

In this guide, we will use the OpenThread - SoC CLI FTD example to send magic packet. In that context, you will have to stop the Thread stack before sending the raw 802.15.4 packet.

startup

Building the OpenThread sender

Follow these steps to send raw 802.15.4 packets from a Thread FTD node:

  1. Create an OpenThread - SoC CLI FTD project
  2. In the "Stack (FTD)" component, enable the Link Raw Service. This option will provide otlinkRaw API
  3. In the build configurator of the project define the OPENTHREAD_RADIO macro to 0
  4. A custom CLI command, raw-wakeup, was added in app.c to transmit an IEEE 802.15.4 frame in raw radio mode and then bring the Thread stack back online.

The modified app.c file can be found there:

Usage

Run this command from the OpenThread CLI:

> raw-wakeup

The command takes no arguments. It reads the active Thread link configuration (PAN ID, source short address, and channel), disables Thread and then IPv6, enables raw radio reception, and prepares a data frame. The frame requests an ACK, uses 0x0002 as its destination short address, and carries the DE AD BE EF payload. Its sequence number increments with each transmission. The active Thread link channel is used.

The length passed to the radio accounts for the two FCS bytes generated by the hardware. When transmission completes, the callback disables raw reception and re-enables IPv6 and then Thread. If setup or transmission startup fails, the recovery path also attempts to restore the stack.

The destination 0x0002 and payload are fixed values in this example. To reach another device, change the destination address and, if needed, the frame format in cli_raw_wakeup().

This is the format of a 802.15.4 frame:

startup

In our example, the frame currently has no Auxiliary Security Header. Its Frame Control Field (FCF) has the MAC security bit cleared, so the receiver does not expect a MAC security header, frame counter, or key identifier. The payload is sent without MAC-layer encryption or authentication.

The frame also has no Header Information Elements (Header IEs). These are optional MAC fields used by features such as TSCH or synchronization. The FCF does not indicate that Header IEs are present, so the payload begins immediately after the addressing fields. To add Header IEs, define their format and identifiers, set the appropriate FCF bit, and insert them before the payload; the receiver must support and expect those IEs.

Code description

The following code shows the main additions in app.c.

Register the CLI command

static const otCliCommand sRawWakeupCommand[] = raw-wakeup;

void sl_ot_cli_init(void)
{
    otAppCliInit(sInstance);
    (void)otCliSetUserCommands(sRawWakeupCommand,
                               (uint8_t)(sizeof(sRawWakeupCommand) / sizeof(sRawWakeupCommand[0])),
                               sInstance);
}

This registers raw-wakeup as a custom OpenThread CLI command.

Switch from Thread to raw radio mode

panId         = otLinkGetPanId(instance);
sourceAddress = otLinkGetShortAddress(instance);
channel       = otLinkGetChannel(instance);

error = otThreadSetEnabled(instance, false);
if (error != OT_ERROR_NONE) return error;

error = otIp6SetEnabled(instance, false);
if (error != OT_ERROR_NONE) goto restore_stack;

error = otLinkRawSetReceiveDone(instance, raw_receive_done);
if (error != OT_ERROR_NONE) goto restore_stack;

error = otLinkRawReceive(instance);
if (error != OT_ERROR_NONE) goto restore_stack;

This saves the active Thread link parameters, pauses Thread and IPv6, and starts raw radio reception before preparing the transmission.

Build and transmit the wakeup frame

psdu[idx++] = 0x61;
psdu[idx++] = 0x88;
psdu[idx++] = sWakeupSequenceNumber++;
psdu[idx++] = (uint8_t)(panId & 0xff);
psdu[idx++] = (uint8_t)(panId >> 8);
psdu[idx++] = 0x02; /* Destination short address */
psdu[idx++] = 0x00;
psdu[idx++] = (uint8_t)(sourceAddress & 0xff);
psdu[idx++] = (uint8_t)(sourceAddress >> 8);
memcpy(&psdu[idx], payload, sizeof(payload));
idx += sizeof(payload);

txFrame->mLength  = idx + 2; /* Hardware generates the FCS. */
txFrame->mChannel = channel;
error = otLinkRawTransmit(instance, raw_transmit_done);

This assembles the MAC header, destination and source short addresses, and the DE AD BE EF payload, then requests transmission on the active Thread channel.

Restore Thread after transmission

otError rawDisableError = otLinkRawSetReceiveDone(aInstance, NULL);
otError restartError = restart_thread_stack(aInstance);

Going Further

To protect the custom wakeup payload, confidentiality and authentication should be used. The packet must be encrypted before transmission and decrypted at the receiver. The encryption key and the initial or current sequence state used for replay protection must first be established between the devices over the Thread network, for example using CoAP or a UDP socket.

Certification

Adding the Link Raw Service can impact Thread Certification by Inheritance proposed by Silicon Labs. This will need to be discussed with Thread group.

Disclaimer

This example is considered to be EXPERIMENTAL QUALITY which implies that the code provided in the repos has not been formally tested and is provided as-is. It is not suitable for production environments. In addition, this code will not be maintained and there may be no bug maintenance planned for these resources. Silicon Labs may update projects from time to time.