SatCat5
net_type.h
Go to the documentation of this file.
1 // Copyright 2023-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
8 
9 #pragma once
10 
11 #include <satcat5/types.h>
12 
13 namespace satcat5 {
14  namespace net {
38  struct Type {
39  public:
42  explicit constexpr Type(u8 val)
43  : m_mask(0x000000FFu), m_value(val) {}
44  explicit constexpr Type(u16 val)
45  : m_mask(0x0000FFFFu), m_value(val) {}
46  explicit constexpr Type(u32 val)
47  : m_mask(0xFFFFFFFFu), m_value(val) {}
49 
51  explicit constexpr Type(u16 val1, u16 val2)
52  : m_mask(0xFFFFFFFFu), m_value(65536ul * val1 + val2) {}
53 
55  explicit constexpr Type(u32 val, u32 mask)
56  : m_mask(mask), m_value(val) {}
57 
58  // Default copy operators.
59  Type(const Type& t) = default;
60  Type& operator=(const Type& t) = default;
61 
64  inline u8 as_u8() const {return (u8) m_value;}
65  inline u16 as_u16() const {return (u16)m_value;}
66  inline u32 as_u32() const {return (u32)m_value;}
68 
71  inline void as_u8(u8& a) const {a = as_u8();}
72  inline void as_u16(u16& a) const {a = as_u16();}
73  inline void as_u32(u32& a) const {a = as_u32();}
74  inline void as_pair(u16& a, u16& b) const
75  {a = (u16)(m_value >> 16); b = (u16)(m_value & 0xFFFF);}
77 
79  inline bool bound() const {return m_value || !m_mask;}
80 
82  inline bool match(const Type& other) const
83  {return (m_value & other.m_mask) == (other.m_value & m_mask);}
84 
85  private:
87  u32 m_mask;
88  u32 m_value;
89  };
90 
92  constexpr satcat5::net::Type TYPE_ANY = Type(u32(0), u32(0));
93 
97  constexpr satcat5::net::Type TYPE_NONE = Type(u32(0));
98  }
99 }
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
constexpr satcat5::net::Type TYPE_NONE
The TYPE_NONE mask blocks all incoming frames.
Definition: net_type.h:97
constexpr satcat5::net::Type TYPE_ANY
The TYPE_ANY mask accepts any incoming frame.
Definition: net_type.h:92
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38
void as_u16(u16 &a) const
Sets the given parameter to m_value.
Definition: net_type.h:72
constexpr Type(u8 val)
Construct a Type from a single value.
Definition: net_type.h:42
constexpr Type(u32 val)
Construct a Type from a single value.
Definition: net_type.h:46
void as_u32(u32 &a) const
Sets the given parameter to m_value.
Definition: net_type.h:73
constexpr Type(u32 val, u32 mask)
Construct a Type with a custom bitwise filter-mask.
Definition: net_type.h:55
bool match(const Type &other) const
Check if this Type matches other.
Definition: net_type.h:82
void as_u8(u8 &a) const
Sets the given parameter to m_value.
Definition: net_type.h:71
u16 as_u16() const
Accessors for m_value.
Definition: net_type.h:65
u32 m_mask
32-bit mask for m_value.
Definition: net_type.h:87
void as_pair(u16 &a, u16 &b) const
Sets the given parameter to m_value.
Definition: net_type.h:74
u32 m_value
Underlying value to check for a match.
Definition: net_type.h:88
constexpr Type(u16 val1, u16 val2)
Construct a Type from a pair of values, concatenated.
Definition: net_type.h:51
bool bound() const
Is this Type actively filtering or is it TYPE_NONE?
Definition: net_type.h:79
constexpr Type(u16 val)
Construct a Type from a single value.
Definition: net_type.h:44
u8 as_u8() const
Accessors for m_value.
Definition: net_type.h:64
u32 as_u32() const
Accessors for m_value.
Definition: net_type.h:66
Basic type aliases and prototypes used throughout SatCat5.