本文整理汇总了Golang中github.com/brocaar/lorawan.PHYPayload.EncryptFRMPayload方法的典型用法代码示例。如果您正苦于以下问题:Golang PHYPayload.EncryptFRMPayload方法的具体用法?Golang PHYPayload.EncryptFRMPayload怎么用?Golang PHYPayload.EncryptFRMPayload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/brocaar/lorawan.PHYPayload
的用法示例。
在下文中一共展示了PHYPayload.EncryptFRMPayload方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleDataDownReply
//.........这里部分代码省略.........
MType: lorawan.UnconfirmedDataDown,
Major: lorawan.LoRaWANR1,
},
}
macPL = &lorawan.MACPayload{
FHDR: lorawan.FHDR{
DevAddr: ns.DevAddr,
FCtrl: lorawan.FCtrl{
ACK: rxPacket.PHYPayload.MHDR.MType == lorawan.ConfirmedDataUp, // set ACK to true when received packet needs an ACK
},
FCnt: ns.FCntDown,
},
}
phy.MACPayload = macPL
// add the payload from the queue
if txPayload != nil {
// validate the max payload size
if len(txPayload.Data) > Band.MaxPayloadSize[rx1DR].N {
// remove the payload from the queue regarding confirmed or not
if _, err := clearInProcessTXPayload(ctx.RedisPool, ns.DevEUI); err != nil {
return err
}
log.WithFields(log.Fields{
"dev_eui": ns.DevEUI,
"data_rate": rx1DR,
"frmpayload_size": len(txPayload.Data),
"max_frmpayload_size": Band.MaxPayloadSize[rx1DR].N,
}).Warning("downlink payload max size exceeded")
err = ctx.Application.SendNotification(ns.DevEUI, ns.AppEUI, models.ErrorNotificationType, models.ErrorNotification{
Reference: txPayload.Reference,
DevEUI: ns.DevEUI,
Message: fmt.Sprintf("downlink payload max size exceeded (dr: %d, allowed: %d, got: %d)", rx1DR, Band.MaxPayloadSize[rx1DR].N, len(txPayload.Data)),
})
if err != nil {
return err
}
} else {
// remove the payload from the queue when not confirmed
if !txPayload.Confirmed {
if _, err := clearInProcessTXPayload(ctx.RedisPool, ns.DevEUI); err != nil {
return err
}
}
macPL.FHDR.FCtrl.FPending = remaining
if txPayload.Confirmed {
phy.MHDR.MType = lorawan.ConfirmedDataDown
}
macPL.FPort = &txPayload.FPort
macPL.FRMPayload = []lorawan.Payload{
&lorawan.DataPayload{Bytes: txPayload.Data},
}
}
}
// when the payload did not pass the validation and there is no ACK set,
// there is nothing to send
if !macPL.FHDR.FCtrl.ACK && len(macPL.FRMPayload) == 0 {
return nil
}
// if there is no payload set, encrypt will just do nothing
if err := phy.EncryptFRMPayload(ns.AppSKey); err != nil {
return fmt.Errorf("encrypt FRMPayload error: %s", err)
}
if err := phy.SetMIC(ns.NwkSKey); err != nil {
return fmt.Errorf("set MIC error: %s", err)
}
txPacket := models.TXPacket{
TXInfo: models.TXInfo{
MAC: rxPacket.RXInfo.MAC,
Timestamp: rxPacket.RXInfo.Timestamp + uint32(rxDelay/time.Microsecond),
Frequency: Band.DownlinkChannels[rx1Channel].Frequency,
Power: Band.DefaultTXPower,
DataRate: Band.DataRates[rx1DR],
CodeRate: rxPacket.RXInfo.CodeRate,
},
PHYPayload: phy,
}
// window 1
if err := ctx.Gateway.Send(txPacket); err != nil {
return fmt.Errorf("send tx packet (rx window 1) to gateway error: %s", err)
}
// increment the FCntDown when MType != ConfirmedDataDown. In case of
// ConfirmedDataDown we increment on ACK.
if phy.MHDR.MType != lorawan.ConfirmedDataDown {
ns.FCntDown++
if err := saveNodeSession(ctx.RedisPool, ns); err != nil {
return err
}
}
return nil
}
示例2: handleDataDownReply
//.........这里部分代码省略.........
MType: lorawan.UnconfirmedDataDown,
Major: lorawan.LoRaWANR1,
},
}
macPL = &lorawan.MACPayload{
FHDR: lorawan.FHDR{
DevAddr: ns.DevAddr,
FCtrl: lorawan.FCtrl{
ACK: rxPacket.PHYPayload.MHDR.MType == lorawan.ConfirmedDataUp, // set ACK when uplink packet was of type ConfirmedDataUp
FPending: queueSize > 0 || len(allMACPayloads) != len(macPayloads), // items in the queue or not all mac commands being sent
},
FCnt: ns.FCntDown,
},
}
phy.MACPayload = macPL
if len(macCommmands) > 0 {
if frmMACCommands {
var fPort uint8 // 0
var frmPayload []lorawan.Payload
for i := range macCommmands {
frmPayload = append(frmPayload, &macCommmands[i])
}
macPL.FPort = &fPort
macPL.FRMPayload = frmPayload
} else {
macPL.FHDR.FOpts = macCommmands
}
}
// add the payload to FRMPayload field
// note that txPayload is by definition nil when there are mac commands
// to send in the FRMPayload field.
if txPayload != nil {
if txPayload.Confirmed {
phy.MHDR.MType = lorawan.ConfirmedDataDown
}
macPL.FPort = &txPayload.FPort
macPL.FRMPayload = []lorawan.Payload{
&lorawan.DataPayload{Bytes: txPayload.Data},
}
}
// if there is no payload set, encrypt will just do nothing
if len(macCommmands) > 0 && frmMACCommands {
if err := phy.EncryptFRMPayload(ns.NwkSKey); err != nil {
return fmt.Errorf("encrypt FRMPayload error: %s", err)
}
} else {
if err := phy.EncryptFRMPayload(ns.AppSKey); err != nil {
return fmt.Errorf("encrypt FRMPayload error: %s", err)
}
}
if err := phy.SetMIC(ns.NwkSKey); err != nil {
return fmt.Errorf("set MIC error: %s", err)
}
txPacket := models.TXPacket{
TXInfo: models.TXInfo{
MAC: rxPacket.RXInfo.MAC,
Timestamp: rxPacket.RXInfo.Timestamp + uint32(properties.rxDelay/time.Microsecond),
Frequency: properties.rx1Frequency,
Power: common.Band.DefaultTXPower,
DataRate: common.Band.DataRates[properties.rx1DR],
CodeRate: rxPacket.RXInfo.CodeRate,
},
PHYPayload: phy,
}
// window 1
if err := ctx.Gateway.SendTXPacket(txPacket); err != nil {
return fmt.Errorf("send tx packet (rx window 1) to gateway error: %s", err)
}
// increment the FCntDown when MType != ConfirmedDataDown and clear
// in-process queue. In case of ConfirmedDataDown we increment on ACK.
if phy.MHDR.MType != lorawan.ConfirmedDataDown {
ns.FCntDown++
if err = storage.SaveNodeSession(ctx.RedisPool, ns); err != nil {
return err
}
if txPayload != nil {
if _, err = storage.ClearInProcessTXPayload(ctx.RedisPool, ns.DevEUI); err != nil {
return err
}
}
}
// remove the mac commands from the queue
for _, pl := range macPayloads {
if err = storage.DeleteMACPayloadFromTXQueue(ctx.RedisPool, ns.DevAddr, pl); err != nil {
return fmt.Errorf("delete mac-payload from tx queue error: %s", err)
}
}
return nil
}
示例3: ConvertToLoRaWAN
func (h *handler) ConvertToLoRaWAN(ctx log.Interface, appDown *types.DownlinkMessage, ttnDown *pb_broker.DownlinkMessage) error {
// Find Device
dev, err := h.devices.Get(appDown.AppID, appDown.DevID)
if err != nil {
return err
}
// LoRaWAN: Unmarshal Downlink
var phyPayload lorawan.PHYPayload
err = phyPayload.UnmarshalBinary(ttnDown.Payload)
if err != nil {
return err
}
macPayload, ok := phyPayload.MACPayload.(*lorawan.MACPayload)
if !ok {
return errors.NewErrInvalidArgument("Downlink", "does not contain a MAC payload")
}
if ttnDown.DownlinkOption != nil && ttnDown.DownlinkOption.ProtocolConfig.GetLorawan() != nil {
macPayload.FHDR.FCnt = ttnDown.DownlinkOption.ProtocolConfig.GetLorawan().FCnt
}
// Abort when downlink not needed
if len(appDown.PayloadRaw) == 0 && !macPayload.FHDR.FCtrl.ACK && len(macPayload.FHDR.FOpts) == 0 {
return ErrNotNeeded
}
// Set FPort
if appDown.FPort != 0 {
macPayload.FPort = &appDown.FPort
}
// Set Payload
if len(appDown.PayloadRaw) > 0 {
macPayload.FRMPayload = []lorawan.Payload{&lorawan.DataPayload{Bytes: appDown.PayloadRaw}}
if macPayload.FPort == nil || *macPayload.FPort == 0 {
macPayload.FPort = pointer.Uint8(1)
}
} else {
macPayload.FRMPayload = []lorawan.Payload{}
}
// Encrypt
err = phyPayload.EncryptFRMPayload(lorawan.AES128Key(dev.AppSKey))
if err != nil {
return err
}
// Set MIC
err = phyPayload.SetMIC(lorawan.AES128Key(dev.NwkSKey))
if err != nil {
return err
}
// Marshal
phyPayloadBytes, err := phyPayload.MarshalBinary()
if err != nil {
return err
}
ttnDown.Payload = phyPayloadBytes
return nil
}