Struct lorawan::creator::DataPayloadCreator
source · pub struct DataPayloadCreator<D, F> { /* private fields */ }
Expand description
DataPayloadCreator serves for creating binary representation of Physical Payload of DataUp or DataDown messages.
Example
let mut phy = lorawan::creator::DataPayloadCreator::new();
let nwk_skey = lorawan::keys::NewSKey::from([2; 16]);
let app_skey = lorawan::keys::AppSKey::from([1; 16]);
phy.set_confirmed(true)
.set_uplink(true)
.set_f_port(42)
.set_dev_addr(&[4, 3, 2, 1])
.set_fctrl(&lorawan::parser::FCtrl::new(0x80, true)) // ADR: true, all others: false
.set_fcnt(76543);
phy.build(b"hello lora", &[], &nwk_skey, &app_skey).unwrap();
Implementations§
source§impl<D: AsMut<[u8]>, F: CryptoFactory + Default> DataPayloadCreator<D, F>
impl<D: AsMut<[u8]>, F: CryptoFactory + Default> DataPayloadCreator<D, F>
sourcepub fn with_options(data: D, factory: F) -> Result<Self, Error>
pub fn with_options(data: D, factory: F) -> Result<Self, Error>
Creates a well initialized DataPayloadCreator with specific crypto functions.
By default the packet is unconfirmed data up packet.
sourcepub fn set_uplink(&mut self, uplink: bool) -> &mut Self
pub fn set_uplink(&mut self, uplink: bool) -> &mut Self
Sets whether the packet is uplink or downlink.
Argument
- uplink - whether the packet is uplink or downlink.
sourcepub fn set_confirmed(&mut self, confirmed: bool) -> &mut Self
pub fn set_confirmed(&mut self, confirmed: bool) -> &mut Self
Sets whether the packet is confirmed or unconfirmed.
Argument
- confirmed - whether the packet is confirmed or unconfirmed.
sourcepub fn set_dev_addr<H: AsRef<[u8]>, T: Into<DevAddr<H>>>(
&mut self,
dev_addr: T
) -> &mut Self
pub fn set_dev_addr<H: AsRef<[u8]>, T: Into<DevAddr<H>>>( &mut self, dev_addr: T ) -> &mut Self
Sets the device address of the DataPayload to the provided value.
Argument
- dev_addr - instance of lorawan::parser::DevAddr or anything that can be converted into it.
sourcepub fn set_fctrl(&mut self, fctrl: &FCtrl) -> &mut Self
pub fn set_fctrl(&mut self, fctrl: &FCtrl) -> &mut Self
Sets the FCtrl header of the DataPayload packet to the specified value.
Argument
- fctrl - the FCtrl to be set.
sourcepub fn set_fcnt(&mut self, fcnt: u32) -> &mut Self
pub fn set_fcnt(&mut self, fcnt: u32) -> &mut Self
Sets the FCnt header of the DataPayload packet to the specified value.
NOTE: In the packet header the value will be truncated to u16.
Argument
- fctrl - the FCtrl to be set.
sourcepub fn set_f_port(&mut self, f_port: u8) -> &mut Self
pub fn set_f_port(&mut self, f_port: u8) -> &mut Self
Sets the FPort header of the DataPayload packet to the specified value.
If f_port == 0, automatically sets encrypt_mac_commands
to true
.
Argument
- f_port - the FPort to be set.
sourcepub fn can_piggyback(cmds: &[&dyn SerializableMacCommand]) -> bool
pub fn can_piggyback(cmds: &[&dyn SerializableMacCommand]) -> bool
Whether a set of mac commands can be piggybacked.
sourcepub fn build(
&mut self,
payload: &[u8],
cmds: &[&dyn SerializableMacCommand],
nwk_skey: &NewSKey,
app_skey: &AppSKey
) -> Result<&[u8], Error>
pub fn build( &mut self, payload: &[u8], cmds: &[&dyn SerializableMacCommand], nwk_skey: &NewSKey, app_skey: &AppSKey ) -> Result<&[u8], Error>
Provides the binary representation of the DataPayload physical payload with the MIC set and payload encrypted.
Argument
- payload - the FRMPayload (application) to be sent.
- nwk_skey - the key to be used for setting the MIC and possibly for MAC command encryption.
- app_skey - the key to be used for payload encryption if fport not 0, otherwise nwk_skey is only used.
Example
let mut phy = lorawan::creator::DataPayloadCreator::new();
let mac_cmd1 = lorawan::maccommands::UplinkMacCommand::LinkCheckReq(
lorawan::maccommands::LinkCheckReqPayload());
let mut mac_cmd2 = lorawan::maccommandcreator::LinkADRAnsCreator::new();
mac_cmd2
.set_channel_mask_ack(true)
.set_data_rate_ack(false)
.set_tx_power_ack(true);
let mut cmds: Vec<&dyn lorawan::maccommands::SerializableMacCommand> = Vec::new();
cmds.push(&mac_cmd1);
cmds.push(&mac_cmd2);
let nwk_skey = lorawan::keys::NewSKey::from([2; 16]);
let app_skey = lorawan::keys::AppSKey::from([1; 16]);
phy.build(&[], &cmds, &nwk_skey, &app_skey).unwrap();
source§impl DataPayloadCreator<GenericArray<u8, U256>, DefaultFactory>
impl DataPayloadCreator<GenericArray<u8, U256>, DefaultFactory>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a well initialized DataPayloadCreator.
By default the packet is unconfirmed data up packet.
Examples
let mut phy = lorawan::creator::DataPayloadCreator::new();
let nwk_skey = lorawan::keys::NewSKey::from([2; 16]);
let app_skey = lorawan::keys::AppSKey::from([1; 16]);
let fctrl = lorawan::parser::FCtrl::new(0x80, true);
phy.set_confirmed(false).
set_uplink(true).
set_f_port(1).
set_dev_addr(&[4, 3, 2, 1]).
set_fctrl(&fctrl). // ADR: true, all others: false
set_fcnt(1);
let payload = phy.build(b"hello", &[], &nwk_skey, &app_skey).unwrap();