from enum import Enum
from base64 import b64encode, b64decode
from dataclasses import dataclass, field
from typing import Any, Optional
from .position_vector import LongPositionVector
from .gn_address import GNAddress
from ..security.security_profiles import SecurityProfile
[docs]class CommonNH(Enum):
"""
Common Next Header class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Section 9.7
Attributes
----------
ANY :
Any Next Header.
BTP-A :
BTP-A Next Header.
BTP-B :
BTP-B Next Header.
IPV6 :
IPv6 Next Header.
"""
ANY = 0
BTP_A = 1
BTP_B = 2
IPV6 = 3
[docs]class GeoAnycastHST(Enum):
"""
Geographically-Scoped Anycast (GAC) Header Subtype class. As specified in
ETSI TS 103 836-4-1 V2.2.1 (2026-04). Section 9.7.4
Attributes
----------
GEOANYCAST_CIRCLE :
Geographically-Scoped Anycast (GAC) Circle Header Subtype.
GEOANYCAST_RECT :
Geographically-Scoped Anycast (GAC) Rectangle Header Subtype.
GEOANYCAST_ELIP :
Geographically-Scoped Anycast (GAC) Ellipse Header Subtype.
"""
GEOANYCAST_CIRCLE = 0
GEOANYCAST_RECT = 1
GEOANYCAST_ELIP = 2
[docs]class GeoBroadcastHST(Enum):
"""
Geographically-Scoped broadcast (GBC) Header Subtype class.
As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04).
Section 9.7.4
Attributes
----------
GEOBROADCAST_CIRCLE :
Geographically-Scoped broadcast (GBC) Circle Header Subtype.
GEOBROADCAST_RECT :
Geographically-Scoped broadcast (GBC) Rectangle Header Subtype.
GEOBROADCAST_ELIP :
Geographically-Scoped broadcast (GBC) Ellipse Header Subtype.
"""
GEOBROADCAST_CIRCLE = 0
GEOBROADCAST_RECT = 1
GEOBROADCAST_ELIP = 2
[docs]class TopoBroadcastHST(Enum):
"""
Topologically-scoped broadcast (TSB) Header Subtype class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04).
Section 9.7.4
Attributes
----------
SINGLE_HOP :
Single Hop Header Subtype.
MULTI_HOP :
Multi Hop Header Subtype.
"""
SINGLE_HOP = 0
MULTI_HOP = 1
[docs]class LocationServiceHST(Enum):
"""
Location Service Header Subtype class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Section 9.7.4
Attributes
----------
LS_REQUEST :
Location Service Request Header Subtype.
LS_REPLY :
Location Service Reply Header Subtype.
"""
LS_REQUEST = 0
LS_REPLY = 1
[docs]@dataclass(frozen=True)
class TrafficClass:
"""
Common Traffic class class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Section 9.7.5
Attributes
----------
scf : bool
(1 bit) Store Carry Forward (SCF) flag. Indicates whether the packet shall be buffered when no suitable
neighbour exists
channel_offload : bool
(1 bit) Channel Offload flag. Indicates whether the packet may be offloaded to another channel than
specified in the TC ID
tc_id : int
(6 bit unsigned integer) Traffic class identifier. TC ID as specified in the media-dependent part of
GeoNetworking corresponding to the interface over which the packet will be transmitted
"""
scf: bool = False
channel_offload: bool = False
tc_id: int = 0
[docs] def set_scf(self, scf: bool) -> "TrafficClass":
"""
Set the SCF flag.
Parameters
----------
scf : bool
SCF flag.
"""
return TrafficClass(scf=scf, channel_offload=self.channel_offload, tc_id=self.tc_id)
[docs] def set_tc_id(self, tc_id: int) -> "TrafficClass":
"""
Set the traffic class identifier.
Parameters
----------
tc_id : int
Traffic class identifier.
"""
if tc_id < 0 or tc_id > 63:
raise ValueError(
"Traffic class identifier must be between 0 and 63")
return TrafficClass(scf=self.scf, channel_offload=self.channel_offload, tc_id=tc_id)
[docs] def set_channel_offload(self, channel_offload: bool) -> "TrafficClass":
"""
Set the channel offload flag.
Parameters
----------
channel_offload : bool
Channel offload flag.
"""
return TrafficClass(scf=self.scf, channel_offload=channel_offload, tc_id=self.tc_id)
[docs] def encode_to_int(self) -> int:
"""
Encodes the traffic class to an integer.
Returns
-------
int :
Encoded traffic class. 1 byte.
"""
return (self.scf << 7) | (self.channel_offload << 6) | self.tc_id
[docs] def encode_to_bytes(self) -> bytes:
"""
Encodes the traffic class to bytes.
Returns
-------
bytes :
Encoded traffic class. 1 byte.
"""
return self.encode_to_int().to_bytes(1, "big")
[docs] @classmethod
def decode_from_int(cls, tc: int) -> "TrafficClass":
"""
Decodes the traffic class from an integer.
Parameters
----------
tc : int
Encoded traffic class. 1 byte.
"""
scf = bool((tc >> 7) & 1)
channel_offload = bool((tc >> 6) & 1)
tc_id = tc & 63
return cls(scf=scf, channel_offload=channel_offload, tc_id=tc_id)
[docs] @classmethod
def decode_from_bytes(cls, tc: bytes) -> "TrafficClass":
"""
Decodes the traffic class from a byte array.
Parameters
----------
tc : bytes
Byte array containing the traffic class.
"""
return cls.decode_from_int(int.from_bytes(tc, "big"))
[docs]@dataclass(frozen=True)
class PacketTransportType:
"""
Packet Transport Type class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Annex J2
Uses the Header Type and Header Subtype fields from the Common Header. As specified in ETSI TS 103 836-4-1
V2.2.1 (2026-04). Section 9.7.4
Attributes
----------
header_type : HeaderType
Header Type.
header_subtype : Enum
Header Subtype.
"""
header_type: HeaderType = HeaderType.TSB
header_subtype: Any = TopoBroadcastHST.SINGLE_HOP
[docs] def to_dict(self) -> dict:
"""
Returns a dictionary representation of the Packet Transport Type.
Returns
-------
dict :
Dictionary representation of the Packet Transport Type.
"""
return {
"header_type": self.header_type.value,
"header_subtype": self.header_subtype.value,
}
[docs] @classmethod
def from_dict(cls, packet_transport_type: dict) -> "PacketTransportType":
"""
Initialize the Packet Transport Type from a dictionary.
Parameters
----------
packet_transport_type : dict
Dictionary containing the Packet Transport Type.
"""
header_type = HeaderType(packet_transport_type["header_type"])
if header_type == HeaderType.GEOANYCAST:
header_subtype = GeoAnycastHST(
packet_transport_type["header_subtype"])
elif header_type == HeaderType.GEOBROADCAST:
header_subtype = GeoBroadcastHST(
packet_transport_type["header_subtype"])
elif header_type == HeaderType.TSB:
header_subtype = TopoBroadcastHST(
packet_transport_type["header_subtype"])
elif header_type == HeaderType.LS:
header_subtype = LocationServiceHST(
packet_transport_type["header_subtype"])
else:
header_subtype = HeaderSubType(
packet_transport_type["header_subtype"])
return cls(header_type=header_type, header_subtype=header_subtype)
[docs]class CommunicationProfile(Enum):
"""
Communication Profile class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Annex J2
Attributes
----------
UNSPECIFIED (0) :
Unspecified
"""
UNSPECIFIED = 0
[docs]@dataclass(frozen=True)
class Area:
"""
Area class. Not specified in the standard
Attributes
----------
latitude : int
Latitude of the center of the area. In 1/10 micro degree.
longitude : int
Longitude of the center of the area. In 1/10 micro degree.
a : int
Length of the semi-major axis. In meters.
b : int
Length of the semi-minor axis. In meters.
angle : int
In degrees from North
"""
latitude: int = 0
longitude: int = 0
a: int = 0
b: int = 0
angle: int = 0
[docs] def to_dict(self) -> dict:
"""
Returns a dictionary representation of the Area.
Returns
-------
dict :
Dictionary representation of the Area.
"""
return {
"latitude": self.latitude,
"longitude": self.longitude,
"a": self.a,
"b": self.b,
"angle": self.angle,
}
[docs] @classmethod
def from_dict(cls, area: dict) -> "Area":
return cls(
latitude=area["latitude"],
longitude=area["longitude"],
a=area["a"],
b=area["b"],
angle=area["angle"],
)
[docs]@dataclass(frozen=True)
class GNDataRequest:
"""
GN Data Request class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Annex J2.2
Attributes
----------
upper_protocol_entity : CommonNH
Upper Protocol Entity.
packet_transport_type : PacketTransportType
Packet Transport Type.
communication_profile : CommunicationProfile
Communication Profile.
security_profile : SecurityProfile
Security Profile.
its_aid : int
ITS AID.
security_permissions : bytes
Security Permissions.
traffic_class : TrafficClass
Traffic Class.
length : int
Length of the payload.
data : bytes
Payload.
area : Area
Area of the GBC algorithm. Only used when the packet transport type is GBC.
max_hop_limit : int
Maximum hop limit for the packet. Default is 10.
THIS CLASS WILL BE EXTENDED WHEN FURTHER PACKET TYPES ARE IMPLEMENTED
"""
upper_protocol_entity: CommonNH = CommonNH.ANY
packet_transport_type: PacketTransportType = field(
default_factory=PacketTransportType)
communication_profile: CommunicationProfile = CommunicationProfile.UNSPECIFIED
security_profile: SecurityProfile = SecurityProfile.NO_SECURITY
its_aid: int = 0
security_permissions: bytes = b"\x00"
traffic_class: TrafficClass = field(default_factory=TrafficClass)
length: int = 0
data: bytes = b""
area: Area = field(default_factory=Area)
max_hop_limit: int = 1
max_packet_lifetime: Optional[float] = None
destination: Optional[GNAddress] = None
[docs] def to_dict(self) -> dict:
"""
Returns a dictionary representation of the GN Data Request.
Returns
-------
dict :
Dictionary representation of the GN Data Request.
"""
return {
"upper_protocol_entity": self.upper_protocol_entity.value,
"packet_transport_type": self.packet_transport_type.to_dict(),
"communication_profile": self.communication_profile.value,
"traffic_class": b64encode(self.traffic_class.encode_to_bytes()).decode(
"utf-8"
),
"length": self.length,
"data": b64encode(self.data).decode("utf-8"),
"area": self.area.to_dict(),
"max_hop_limit": self.max_hop_limit,
"max_packet_lifetime": self.max_packet_lifetime,
}
[docs] @classmethod
def from_dict(cls, gn_data_request: dict) -> "GNDataRequest":
"""
Initialize the GN Data Request from a dictionary.
Parameters
----------
gn_data_request : dict
Dictionary containing the GN Data Request.
"""
upper_protocol_entity = CommonNH(
gn_data_request["upper_protocol_entity"])
packet_transport_type = PacketTransportType.from_dict(
gn_data_request["packet_transport_type"]
)
communication_profile = CommunicationProfile(
gn_data_request["communication_profile"]
)
traffic_class = TrafficClass.decode_from_bytes(
b64decode(gn_data_request["traffic_class"])
)
length = gn_data_request["length"]
data = b64decode(gn_data_request["data"])
area = Area.from_dict(gn_data_request["area"])
max_hop_limit = gn_data_request.get("max_hop_limit", 0)
max_packet_lifetime = gn_data_request.get("max_packet_lifetime")
return cls(
upper_protocol_entity=upper_protocol_entity,
packet_transport_type=packet_transport_type,
communication_profile=communication_profile,
traffic_class=traffic_class,
length=length,
data=data,
area=area,
max_hop_limit=max_hop_limit,
max_packet_lifetime=max_packet_lifetime,
)
[docs]class ResultCode(Enum):
"""
Result Code class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Annex J3
Attributes
----------
ACCEPTED (1) :
Accepted
MAXIMUM_LENGTH_EXCEEDED (2) :
The size of the T/GN6-PDU exceeds the GN protocol constant itsGnMaxSduSize;
MAXIMUM_LIFETIME_EXCEEDED (3) :
The lifetime exceeds the maximum value of the GN protocol constant itsGnMaxPacketLifetime;
REPETITION_INTERVAL_TOO_SMALL (4) :
The repetition interval is too small;
UNSUPPORTED_TRAFFIC_CLASS (5) :
The traffic class is not supported;
GEOGRAPHICAL_SCOPE_TOO_LARGE (6) :
The geographical scope is too large;
UNSPECIFIED (7) :
Unspecified
"""
ACCEPTED = 1
MAXIMUM_LENGTH_EXCEEDED = 2
MAXIMUM_LIFETIME_EXCEEDED = 3
REPETITION_INTERVAL_TOO_SMALL = 4
UNSUPPORTED_TRAFFIC_CLASS = 5
GEOGRAPHICAL_SCOPE_TOO_LARGE = 6
UNSPECIFIED = 7
[docs]@dataclass(frozen=True)
class GNDataConfirm:
"""
GN Data Confirm class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Annex J3
Attributes
----------
result_code : ResultCode
Result Code.
"""
result_code: ResultCode = ResultCode.UNSPECIFIED
[docs]@dataclass(frozen=True)
class GNDataIndication:
"""
GN Data Indication class. As specified in ETSI TS 103 836-4-1 V2.2.1 (2026-04). Annex J4
Attributes
----------
upper_protocol_entity : CommonNH
Upper Protocol Entity.
packet_transport_type : PacketTransportType
Packet Transport Type.
destination_area : Area or None
Destination geographical area (GBC/GAC only). Optional per Annex J4.
destination : GNAddress or None
Destination address of a GeoUnicast, as generated by the source (Annex J4).
source_position_vector : LongPositionVector
Source Position Vector.
traffic_class : TrafficClass
Traffic Class.
remaining_packet_lifetime : float or None
Remaining lifetime of the packet in seconds. Optional per Annex J4.
remaining_hop_limit : int or None
Remaining hop limit of the packet. Optional per Annex J4.
security_report : int or None
Report of the security verification (ReportVerify value) of a secured
packet; None for an unsecured packet (Table 38).
certificate_id : bytes or None
HashedId8 of the signer's authorization ticket (Table 38).
its_aid : int or None
ITS-AID (PSID) of the secured packet (Table 38).
security_permissions : bytes or None
SSP of the signer's authorization ticket for that ITS-AID (Table 38).
length : int
Length of the payload.
data : bytes
Payload.
"""
upper_protocol_entity: CommonNH = CommonNH.ANY
packet_transport_type: PacketTransportType = field(
default_factory=PacketTransportType)
destination_area: Optional[Any] = None
destination: Optional[GNAddress] = None
source_position_vector: LongPositionVector = field(
default_factory=LongPositionVector)
traffic_class: TrafficClass = field(default_factory=TrafficClass)
remaining_packet_lifetime: Optional[float] = None
remaining_hop_limit: Optional[int] = None
security_report: Optional[int] = None
certificate_id: Optional[bytes] = None
its_aid: Optional[int] = None
security_permissions: Optional[bytes] = None
length: int = 0
data: bytes = b""
[docs] def to_dict(self) -> dict:
"""
Returns a dictionary representation of the GN Data Indication.
Returns
-------
dict :
Dictionary representation of the GN Data Indication.
"""
return {
"upper_protocol_entity": self.upper_protocol_entity.value,
"packet_transport_type": self.packet_transport_type.to_dict(),
"destination_area": self.destination_area.to_dict() if self.destination_area is not None else None,
"destination": b64encode(self.destination.encode()).decode("utf-8") if self.destination is not None else None,
"source_position_vector": b64encode(
self.source_position_vector.encode()
).decode("utf-8"),
"traffic_class": b64encode(self.traffic_class.encode_to_bytes()).decode(
"utf-8"
),
"remaining_packet_lifetime": self.remaining_packet_lifetime,
"remaining_hop_limit": self.remaining_hop_limit,
"security_report": self.security_report,
"certificate_id": b64encode(self.certificate_id).decode("utf-8")
if self.certificate_id is not None else None,
"its_aid": self.its_aid,
"security_permissions": b64encode(self.security_permissions).decode("utf-8")
if self.security_permissions is not None else None,
"length": self.length,
"data": b64encode(self.data).decode("utf-8"),
}
[docs] @classmethod
def from_dict(cls, gn_data_indication: dict) -> "GNDataIndication":
"""
Initialize the GN Data Indication from a dictionary.
Parameters
----------
gn_data_indication : dict
Dictionary containing the GN Data Indication.
"""
upper_protocol_entity = CommonNH(
gn_data_indication["upper_protocol_entity"])
packet_transport_type = PacketTransportType.from_dict(
gn_data_indication["packet_transport_type"]
)
raw_dest = gn_data_indication.get("destination_area")
destination_area = Area.from_dict(raw_dest) if raw_dest is not None else None
source_position_vector = LongPositionVector.decode(
b64decode(gn_data_indication["source_position_vector"])
)
traffic_class = TrafficClass.decode_from_bytes(
b64decode(gn_data_indication["traffic_class"])
)
remaining_packet_lifetime = gn_data_indication.get("remaining_packet_lifetime")
remaining_hop_limit = gn_data_indication.get("remaining_hop_limit")
certificate_id = gn_data_indication.get("certificate_id")
permissions = gn_data_indication.get("security_permissions")
length = gn_data_indication["length"]
data = b64decode(gn_data_indication["data"])
return cls(
upper_protocol_entity=upper_protocol_entity,
packet_transport_type=packet_transport_type,
destination_area=destination_area,
destination=(GNAddress.decode(b64decode(gn_data_indication["destination"]))
if gn_data_indication.get("destination") is not None else None),
source_position_vector=source_position_vector,
traffic_class=traffic_class,
remaining_packet_lifetime=remaining_packet_lifetime,
remaining_hop_limit=remaining_hop_limit,
security_report=gn_data_indication.get("security_report"),
certificate_id=b64decode(certificate_id) if certificate_id is not None else None,
its_aid=gn_data_indication.get("its_aid"),
security_permissions=b64decode(permissions) if permissions is not None else None,
length=length,
data=data,
)