protocol.tlvs
Read, write, and manipulate TLV (type-length-value) data.
Reader functions take [buf start ctx] and return [end value] where end is the offset in buf after the read value(s).
Writer functions take [buf tlv-or-tlvs start ctx] and return end where end is the offset in buf after the written value(s).
https://en.wikipedia.org/wiki/Type%E2%80%93length%E2%80%93value
assoc-in-tlv
(assoc-in-tlv msg-map path value)Version of assoc-in-tlv* that takes a msg-map containing a :tlvs key where the TLV sequence will be updated.
assoc-in-tlv*
(assoc-in-tlv* tlvs path value)Replaces TLV values in the tlvs at path. tlvs is a TLV hierarchy/sequence and path is a sequence of TLV type names to use in traversing the hierachy. Any matching TLV values will be replaced with value.
get-in-tlv
(get-in-tlv msg-map path)Version of get-in-tlv* that takes a msg-map containing a :tlvs key where the TLV sequence will searched.
get-in-tlv*
(get-in-tlv* tlvs path)Takes a tlvs TLV hierachy/sequence and a path which is sequence of TLV type names to use in traversing the hierarchy. The return value is a sequence of matching TLV values and may be multiple if multiple TLVs of the same type are found at the leaf location.
read-tlv
(read-tlv buf start {:keys [readers path lookup tlv-tsize tlv-lsize], :as ctx})Read a TLV (type-length-value) field from buf starting at start. The byte sizes of the type and length parts are specified by tlv-tsize and tlv-lsize respectively. The type value is used to lookup the reader and context from the lookup map (which should be constructed using tlv-list->lookup) which will be used to read the TLV value part.
The returned value will be [tlv-name tlv-value] where tlv-name is the looked up type code and tlv-value is the value read from the field. If the looked up type is :tlv-stop then a true value will be appended to the returned tuple to indicate to looping readers that there are no more TLVs to read.
read-tlv-map
(read-tlv-map buf start ctx)Same as read-tlv-seq but returns a map of TLV names to values
Note: repeats of the same TLV code/name will overwrite earlier values.
read-tlv-seq
(read-tlv-seq buf start {:keys [length], :as ctx})Use fields/read-loop to read multiple TLVs from buf starting at start and ending when a stop value is read or length is reached.
tlv-list->lookup
(tlv-list->lookup tlist)Takes a sequence of [code, name, internal type, extra-context] and converts it into a TLV lookup map containing:
- :list- original list/sequence
- :types- map of codes and names to types
- :ctxs- map of codes and names to contexts
- :names- map of names to codes
- :codes- map of codes to names
update-in-tlv
(update-in-tlv msg-map & args)Version of update-in-tlv* that takes a msg-map containing a :tlvs key where the TLV sequence will be updated.
update-in-tlv*
(update-in-tlv* tlvs path f & args)Update TLV values in the tlvs at path. tlvs is a TLV hierarchy/sequence and path is a sequence of TLV type names to use in traversing the hierachy. Any matching TLV values will be updated using (apply f tlv-value args).
write-tlv
(write-tlv buf [tlv-name tlv-value] start {:keys [writers path lookup tlv-tsize tlv-lsize], :as ctx})Write a TLV (type-length-value) field into buf starting at start. The byte sizes of the type and length parts are specified by tlv-tsize and tlv-lsize respectively. The type-name is used to lookup the type code and also the writer and context from the lookup map (which should be constructed using tlv-list->lookup) which will be used to write the TLV value part.
If the type of the TLV is :tlv-stop then only the type part of the TLV will be written with the stop code (no length or value part will be written).
write-tlv-map
(write-tlv-map buf tlv-map tlv-start {:keys [lookup], :as ctx})Same as write-tlv-seq but takes a TLV map (tlv-map) rather than a a sequence of TLVs. Uses (:list lookup) to determine the order to write TLVs from tlv-map.
write-tlv-seq
(write-tlv-seq buf tlvs start ctx)Use fields/write-loop to write multiple TLVs (tlvs) into buf starting at start.