Files
openthread/tests/nexus/platform/nexus_core.hpp
T
Jonathan Hui 254043deec [nexus] add gRPC support and live demo (#12898)
This commit introduces gRPC support to the Nexus simulator, enabling
remote control and monitoring of simulations. This infrastructure allows
external tools and visualizers to interact with the simulated network
in real-time.

Key changes:
- Defined `simulation.proto` providing the `NexusService` definition for
  simulation control and event streaming.
- Implemented `GrpcServer` in `nexus_grpc.cpp` which functions as a
  Nexus simulation observer, pushing events to connected clients.
- Added RPCs for dynamic node creation, position updates, node state
  control, and network orchestration (forming and joining).
- Implemented a real-time event stream that includes node state changes,
  link updates, and packet captures (with basic protocol decoding).
- Introduced `nexus_native.cpp` as an entry point for a persistent
  simulation server that can be controlled via gRPC.
- Updated `Core` and `Observer` interfaces to support a list of
  concurrent observers instead of a single instance.
- Enhanced the CMake build system to optionally find and link against
  gRPC and Protobuf, including automatic source generation.
- Updated CI (GitHub Actions) to include build and test steps for the
  new gRPC functionality.
- Added comprehensive unit tests in `test_grpc.cpp` to verify all
  exposed gRPC service methods.
2026-04-16 22:05:19 -05:00

165 lines
5.8 KiB
C++

/*
* Copyright (c) 2024, 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_CORE_HPP_
#define OT_NEXUS_PLATFORM_NEXUS_CORE_HPP_
#include "openthread-core-config.h"
#include <stdio.h>
#include "nexus_alarm.hpp"
#include "nexus_observer.hpp"
#include "nexus_pcap.hpp"
#include "nexus_radio.hpp"
#include "nexus_utils.hpp"
#include "common/array.hpp"
#include "common/owning_list.hpp"
#include "instance/instance.hpp"
#include "thread/key_manager.hpp"
namespace ot {
namespace Nexus {
class Node;
class Core
{
public:
Core(void);
~Core(void);
static Core &Get(void) { return *sCore; }
void AddObserver(Observer &aObserver) { mObservers.Push(aObserver); }
void RemoveObserver(Observer &aObserver) { IgnoreError(mObservers.Remove(aObserver)); }
void NotifyHeartbeat(void);
void NotifyDumpState(void);
Node &CreateNode(void);
Node *FindNodeById(uint32_t aNodeId);
LinkedList<Node> &GetNodes(void) { return mNodes; }
TimeMilli GetNow(void) { return TimeMilli(static_cast<uint32_t>(mNow / 1000u)); }
TimeMicro GetNowMicro(void) { return TimeMicro(static_cast<uint32_t>(mNow)); }
uint64_t GetNowMicro64(void) const { return mNow; }
void AdvanceTime(uint32_t aDuration);
bool IsUiConnected(void) const;
void Reset(void);
void SetNodeEnabled(uint32_t aNodeId, bool aEnabled);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Test specific helper methods
void SaveTestInfo(const char *aFilename, Node *aLeaderNode = nullptr);
void AddNetworkKey(const NetworkKey &aKey);
void AddTestVar(const char *aName, const char *aValue);
void AddTestVar(const char *aName, uint32_t aValue);
void AddOmrPrefixTestVar(const char *aName, Node &aNode);
void SendAndVerifyEchoRequest(Node &aSender,
const Ip6::Address &aDestination,
uint16_t aPayloadSize = 0,
uint8_t aHopLimit = Ip6::kDefaultHopLimit,
uint32_t aResponseTimeout = 1000);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Used by platform implementation
void UpdateNextAlarmMilli(const Alarm &aAlarm);
void UpdateNextAlarmMicro(const Alarm &aAlarm);
void MarkPendingAction(void) { mPendingAction = true; }
Node *FindNodeByAddress(const Ip6::Address &aAddress);
bool IsThreadAddress(const Ip6::Address &aAddress);
Node *FindNodeByThreadAddress(const Ip6::Address &aAddress);
Node *FindNodeByInfraIfAddress(const Ip6::Address &aAddress);
static void HandleNeighborTableChanged(otNeighborTableEvent aEvent, const otNeighborTableEntryInfo *aInfo);
static void HandleStateChanged(otChangedFlags aFlags, void *aContext);
private:
static constexpr int8_t kDefaultRxRssi = -20;
static constexpr uint8_t kDefaultRxLqi = 255;
enum AckMode : uint8_t
{
kNoAck,
kSendAckNoFramePending,
kSendAckFramePending,
};
struct IcmpEchoResponseContext
{
IcmpEchoResponseContext(Node &aNode, uint16_t aIdentifier);
Node &mNode;
uint16_t mIdentifier;
bool mResponseReceived;
};
struct TestVar
{
String<32> mName;
String<64> mValue;
};
TestVar &NewTestVar(const char *aName);
void Process(Node &aNode);
void ProcessRadio(Node &aNode);
void ProcessInfraIf(Node &aNode);
static void HandleIcmpResponse(void *aContext,
otMessage *aMessage,
const otMessageInfo *aMessageInfo,
const otIcmp6Header *aIcmpHeader);
static Core *sCore;
static bool sInUse;
OwningList<Node> mNodes;
Pcap mPcap;
Array<NetworkKey, 16> mNetworkKeys;
Array<TestVar, 128> mTestVars;
uint16_t mCurNodeId;
bool mPendingAction;
bool mSaveNodeLogs;
uint64_t mNow;
uint64_t mNextAlarmTime;
LinkedList<Observer> mObservers;
};
} // namespace Nexus
} // namespace ot
#endif // OT_NEXUS_PLATFORM_NEXUS_CORE_HPP_