######################################################################
# Packets used in Pia peer-to-peer communication.
# -----
# Licensed under Apache License 2.0
module Packets
import ExtraFun: Ident

# ===== Packet Prefixes =====
# Always write fixed enum values for backward compatibility! Never delete packet IDs either, even when deprecated.
# This way, order of packet IDs does not matter and we are free to rearrange and insert packets as needed.

export PacketClass
@enum PacketClass begin
    ConnectivityClass = 0x01
    CryptographyClass = 0x02
    DiscoveryClass    = 0x03
    DataClass         = 0x04
    UtilityClass      = 0x10
end

@enum ConnectivityPrefix begin
    HandshakeStart    = 0x01
    HandshakeResponse = 0x02
    HandshakeConfirm  = 0x03
    Ping              = 0x04
    Pong              = 0x05
    WantPromote       = 0x06
    AcceptPromote     = 0x07
    Demote            = 0x08
    Goodbye           = 0xFF
end

@enum DiscoveryPrefix begin
    QueryPeer = 0x01
end

@enum UtilityPrefix begin
    Wakeup = 0x01
end

@enum DataPrefix begin
    AppData = 0x01
end

classid(::ConnectivityPrefix) = ConnectivityClass
classid(::DiscoveryPrefix)    = DiscoveryClass
classid(::UtilityPrefix)      = UtilityClass
classid(::DataPrefix)         = DataClass
packetid(cls, pktid::UInt8) = packetid(Ident{cls}(), pktid)
packetid(::Ident{ConnectivityClass}, pktid::UInt8) = ConnectivityPrefix(pktid)
packetid(::Ident{DiscoveryClass},    pktid::UInt8) = DiscoveryPrefix(pktid)
packetid(::Ident{UtilityClass},      pktid::UInt8) = UtilityPrefix(pktid)
packetid(::Ident{DataClass},         pktid::UInt8) = DataPrefix(pktid)

end # module Packets
