blob: 5f36e1bbb6317aa8e3416f30d0e171bb6deae8fa [file]
/*
* 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_NODE_HPP_
#define OT_NEXUS_PLATFORM_NEXUS_NODE_HPP_
#include "instance/instance.hpp"
#include "nexus_alarm.hpp"
#include "nexus_core.hpp"
#include "nexus_infra_if.hpp"
#include "nexus_mdns.hpp"
#include "nexus_radio.hpp"
#include "nexus_settings.hpp"
#include "nexus_trel.hpp"
#include "nexus_utils.hpp"
namespace ot {
namespace Nexus {
class Platform
{
public:
Radio mRadio;
Alarm mAlarmMilli;
Alarm mAlarmMicro;
Mdns mMdns;
InfraIf mInfraIf;
Settings mSettings;
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
Trel mTrel;
#endif
bool mPendingTasklet;
protected:
Platform(void)
: mPendingTasklet(false)
{
}
};
class Node : public Platform, public Heap::Allocatable<Node>, public LinkedListEntry<Node>, public Instance
{
friend class Heap::Allocatable<Node>;
public:
// Defines the device role and Network Data request behavior
// during joining:
//
// - `kAsFtd`, `kAsFed`, and `kAsMed` all request full netdata.
// - `kAsSed` requests only stable netdata (default for SED).
// - `kAsSedWithFullNetData` explicitly request full netdata.
enum JoinMode : uint8_t
{
kAsFtd,
kAsFed,
kAsMed,
kAsSed,
kAsSedWithFullNetData,
};
void Reset(void);
void Form(void);
void Join(Node &aNode, JoinMode aJoinMode = kAsFtd);
void AllowList(Node &aNode);
void UnallowList(Node &aNode);
void SendEchoRequest(const Ip6::Address &aDestination,
uint16_t aIdentifier = 0,
uint16_t aPayloadSize = 0,
uint8_t aHopLimit = 64,
const Ip6::Address *aSrcAddress = nullptr);
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
void GetTrelSockAddr(Ip6::SockAddr &aSockAddr) const;
#endif
// Finds and returns the address on device matching the given `aPrefix`.
// It requires a matching prefix to be found, otherwise it is treated as
// a test failure (emits error message and exits the program.)
const Ip6::Address &FindMatchingAddress(const char *aPrefix);
/**
* Finds and returns a global scope address on the device.
*
* It requires a global scope address to be found, otherwise it is treated as a test failure (emits error message
* and exits the program).
*
* @returns A reference to the global scope address.
*/
const Ip6::Address &FindGlobalAddress(void);
void SetName(const char *aName) { mName.Clear().Append("%s", aName); }
void SetName(const char *aPrefix, uint16_t aIndex);
const char *GetName(void) const { return mName.AsCString(); }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <typename Type> Type &Get(void)
{
Core::Get().SetActiveNode(this);
return Instance::Get<Type>();
}
Instance &GetInstance(void)
{
Core::Get().SetActiveNode(this);
return *this;
}
uint32_t GetId(void) { return GetInstance().GetId(); }
static Node &From(otInstance *aInstance) { return static_cast<Node &>(*aInstance); }
static void HandleIp6Receive(otMessage *aMessage, void *aContext);
void HandleReceive(otMessage *aMessage);
using Platform::mAlarmMicro;
using Platform::mAlarmMilli;
using Platform::mInfraIf;
using Platform::mMdns;
using Platform::mPendingTasklet;
using Platform::mRadio;
using Platform::mSettings;
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
using Platform::mTrel;
#endif
Node *mNext;
private:
Node(void) {}
String<32> mName;
};
inline Node &AsNode(otInstance *aInstance) { return Node::From(aInstance); }
} // namespace Nexus
} // namespace ot
#endif // OT_NEXUS_PLATFORM_NEXUS_NODE_HPP_