Skip to content

Commit

Permalink
doc: documentation update to show how parametrized PDUs work
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Sep 30, 2023
1 parent 2c1766f commit b189e77
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,37 @@ pdu2 = AppPDU(bytes)
@assert pdu == pdu2
```

## PDUs as parametrized types

For type stability, it is often desirable not to use a union type as a field in the `struct`, but instead use a parametrized `struct`. We support parametrized PDUs too:
```julia
struct ParamAppPDU{T} <: PDU
hdrlen::UInt8
hdr::T
payload::Vector{UInt8}
end

# convenience constructors to auto-populate hdrlen
ParamAppPDU(hdr::Header_v1, payload) = ParamAppPDU{Header_v1}(9, hdr, payload)
ParamAppPDU(hdr::Header_v2, payload) = ParamAppPDU{Header_v2}(18, hdr, payload)

# hdr is v2 if hdrlen field matches it's size, otherwise default to v1
function ProtocolDataUnits.fieldtype(::Type{<:ParamAppPDU}, ::Val{:hdr}, info)
info.get(:hdrlen) == 18 && return Header_v2
Header_v1
end

# payload length is the frame length less the header
Base.length(::Type{<:ParamAppPDU}, ::Val{:payload}, info) = info.length - info.get(:hdrlen) - 1

pdu = ParamAppPDU(Header_v1(1, 2, 3), UInt8[4, 5, 6])
bytes = Vector{UInt8}(pdu)
@assert length(bytes) == 13
pdu2 = ParamAppPDU(bytes)
@assert pdu.hdr isa Header_v1
@assert pdu == pdu2
```

## PDUs with optional fields

Extending the idea of union fields, we can define PDUs with optional fields:
Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,29 @@ end
@test pdu.hdr isa Header_v2
@test pdu == pdu2

struct ParamAppPDU{T} <: PDU
hdrlen::UInt8
hdr::T
payload::Vector{UInt8}
end

ParamAppPDU(hdr::Header_v1, payload) = ParamAppPDU{Header_v1}(9, hdr, payload)
ParamAppPDU(hdr::Header_v2, payload) = ParamAppPDU{Header_v2}(18, hdr, payload)

function ProtocolDataUnits.fieldtype(::Type{<:ParamAppPDU}, ::Val{:hdr}, info)
info.get(:hdrlen) == 18 && return Header_v2
Header_v1
end

Base.length(::Type{<:ParamAppPDU}, ::Val{:payload}, info) = info.length - info.get(:hdrlen) - 1

pdu = ParamAppPDU(Header_v1(1, 2, 3), UInt8[4, 5, 6])
bytes = Vector{UInt8}(pdu)
@test length(bytes) == 13
pdu2 = ParamAppPDU(bytes)
@test pdu.hdr isa Header_v1
@test pdu == pdu2

struct App2PDU <: PDU
hdrlen::UInt8
hdr::Union{Header_v1,Header_v2,Nothing}
Expand Down

2 comments on commit b189e77

@mchitre
Copy link
Member Author

@mchitre mchitre commented on b189e77 Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92624

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" b189e777178c3ac9d731e958dcb0c31a0a66c005
git push origin v0.2.0

Please sign in to comment.