Files
Jonathan Hui 27321a2110 [nexus] add WebAssembly support using Emscripten (#12904)
This commit adds support for building the Nexus simulator for
WebAssembly (WASM) using the Emscripten toolchain. This enables the
simulator to run in a web browser environment with a JavaScript-based
control interface and visualization.

Key implementation details:
- Introduced `nexus_wasm.cpp` which defines Emscripten bindings (using
  Embind) for core simulation controls, including stepping time,
  node creation, topology orchestration, and state manipulation.
- Implemented a `WasmObserver` and a global event queue to capture
  simulation events (node state changes, link updates, packet events)
  and expose them to JavaScript via a polling mechanism (`pollEvent`).
- Updated the CMake build system to support the `EMSCRIPTEN` platform,
  configuring specific linker options for ES6 module export,
  modularization, and memory growth.
- Enhanced `build.sh` to allow targeting WASM via `emcmake`.
- Guarded file-system-dependent operations in `nexus_pcap.cpp` and
  adjusted `nexus_core.cpp` to handle WASM-specific constraints where
  standard I/O or multiple observers might not be applicable.
- Added `test_wasm_bindings.mjs`, a Node.js-based smoke test that
  verifies the integrity of the WASM bindings and event pipeline.
- Integrated `nexus-wasm-tests` into the GitHub Actions workflow to
  ensure continuous verification of the WASM build and functionality.
2026-04-17 15:25:28 -05:00

114 lines
4.1 KiB
C++

/*
* Copyright (c) 2026, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef OT_NEXUS_PLATFORM_NEXUS_PCAP_HPP_
#define OT_NEXUS_PLATFORM_NEXUS_PCAP_HPP_
#include <cstdint>
#include <cstdio>
#include "nexus_infra_if.hpp"
#include "nexus_radio.hpp"
namespace ot {
namespace Nexus {
class Pcap
{
public:
Pcap(void);
~Pcap(void);
/**
* Opens a pcapng file.
*
* @param[in] aFilename The filename to open.
*/
void Open(const char *aFilename);
/**
* Closes the pcapng file.
*/
void Close(void);
/**
* Writes a frame to the pcapng file.
*
* @param[in] aFrame The frame to write.
* @param[in] aTimeUs The timestamp in microseconds.
*/
void WriteFrame(const otRadioFrame &aFrame, uint64_t aTimeUs);
/**
* Writes a packet to the pcapng file.
*
* @param[in] aSrcAddr The source link-layer address.
* @param[in] aDstAddr The destination link-layer address.
* @param[in] aBuffer The packet buffer to write.
* @param[in] aLength The packet length.
* @param[in] aTimeUs The timestamp in microseconds.
*/
void WritePacket(const InfraIf::LinkLayerAddress &aSrcAddr,
const InfraIf::LinkLayerAddress &aDstAddr,
const uint8_t *aBuffer,
uint16_t aLength,
uint64_t aTimeUs);
private:
static constexpr uint32_t kPcapngShbType = 0x0a0d0d0a;
static constexpr uint32_t kPcapngByteOrderMagic = 0x1a2b3c4d;
static constexpr uint16_t kPcapngVersionMajor = 1;
static constexpr uint16_t kPcapngVersionMinor = 0;
static constexpr uint32_t kPcapngIdbType = 0x00000001;
static constexpr uint32_t kPcapngEpbType = 0x00000006;
static constexpr uint32_t kPcapngSnapLen = 65535;
static constexpr uint32_t kPcapngLinkTypeIeee802154 = 283; // DLT_IEEE802_15_4_TAP
static constexpr uint32_t kPcapngLinkTypeEthernet = 1; // LINKTYPE_ETHERNET
static constexpr uint16_t kEtherTypeIPv6 = 0x86dd;
static constexpr uint8_t kTapVersion = 0;
static constexpr uint16_t kTapFcsType = 0;
static constexpr uint16_t kTapFcsLength = 1;
static constexpr uint8_t kTapFcsValue = 1; // 16-bit CRC
static constexpr uint16_t kTapChannelType = 3;
static constexpr uint16_t kTapChannelLength = 3;
static constexpr uint8_t kTapChannelPage = 0;
#ifndef __EMSCRIPTEN__
FILE *mFile;
#endif
};
} // namespace Nexus
} // namespace ot
#endif // OT_NEXUS_PLATFORM_NEXUS_PCAP_HPP_