本文整理汇总了Golang中github.com/brocaar/lorawan.PHYPayload.ValidateMIC方法的典型用法代码示例。如果您正苦于以下问题:Golang PHYPayload.ValidateMIC方法的具体用法?Golang PHYPayload.ValidateMIC怎么用?Golang PHYPayload.ValidateMIC使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/brocaar/lorawan.PHYPayload
的用法示例。
在下文中一共展示了PHYPayload.ValidateMIC方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandleUplink
func (b *broker) HandleUplink(uplink *pb.UplinkMessage) (err error) {
ctx := b.Ctx.WithField("GatewayID", uplink.GatewayMetadata.GatewayId)
start := time.Now()
defer func() {
if err != nil {
ctx.WithError(err).Warn("Could not handle uplink")
} else {
ctx.WithField("Duration", time.Now().Sub(start)).Info("Handled uplink")
}
}()
time := time.Now()
b.status.uplink.Mark(1)
// De-duplicate uplink messages
duplicates := b.deduplicateUplink(uplink)
if len(duplicates) == 0 {
return nil
}
b.status.uplinkUnique.Mark(1)
ctx = ctx.WithField("Duplicates", len(duplicates))
base := duplicates[0]
if base.ProtocolMetadata.GetLorawan() == nil {
return errors.NewErrInvalidArgument("Uplink", "does not contain LoRaWAN metadata")
}
// LoRaWAN: Unmarshal
var phyPayload lorawan.PHYPayload
err = phyPayload.UnmarshalBinary(base.Payload)
if err != nil {
return err
}
macPayload, ok := phyPayload.MACPayload.(*lorawan.MACPayload)
if !ok {
return errors.NewErrInvalidArgument("Uplink", "does not contain a MAC payload")
}
// Request devices from NS
devAddr := types.DevAddr(macPayload.FHDR.DevAddr)
ctx = ctx.WithFields(log.Fields{
"DevAddr": devAddr,
"FCnt": macPayload.FHDR.FCnt,
})
var getDevicesResp *networkserver.DevicesResponse
getDevicesResp, err = b.ns.GetDevices(b.Component.GetContext(b.nsToken), &networkserver.DevicesRequest{
DevAddr: &devAddr,
FCnt: macPayload.FHDR.FCnt,
})
if err != nil {
return errors.Wrap(errors.FromGRPCError(err), "NetworkServer did not return devices")
}
b.status.deduplication.Update(int64(len(getDevicesResp.Results)))
if len(getDevicesResp.Results) == 0 {
return errors.NewErrNotFound(fmt.Sprintf("Device with DevAddr %s and FCnt <= %d", devAddr, macPayload.FHDR.FCnt))
}
ctx = ctx.WithField("DevAddrResults", len(getDevicesResp.Results))
// Sort by FCntUp to optimize the number of MIC checks
sort.Sort(ByFCntUp(getDevicesResp.Results))
// Find AppEUI/DevEUI through MIC check
var device *pb_lorawan.Device
var micChecks int
var originalFCnt uint32
for _, candidate := range getDevicesResp.Results {
nwkSKey := lorawan.AES128Key(*candidate.NwkSKey)
// First check with the 16 bit counter
micChecks++
ok, err = phyPayload.ValidateMIC(nwkSKey)
if err != nil {
return err
}
if ok {
device = candidate
break
}
originalFCnt = macPayload.FHDR.FCnt
if candidate.Uses32BitFCnt {
macPayload.FHDR.FCnt = fcnt.GetFull(candidate.FCntUp, uint16(originalFCnt))
// If 32 bit counter has different value, perform another MIC check
if macPayload.FHDR.FCnt != originalFCnt {
micChecks++
ok, err = phyPayload.ValidateMIC(nwkSKey)
if err != nil {
return err
}
if ok {
device = candidate
break
}
}
}
//.........这里部分代码省略.........
示例2: ConvertFromLoRaWAN
func (h *handler) ConvertFromLoRaWAN(ctx log.Interface, ttnUp *pb_broker.DeduplicatedUplinkMessage, appUp *types.UplinkMessage) error {
// Find Device
dev, err := h.devices.Get(ttnUp.AppId, ttnUp.DevId)
if err != nil {
return err
}
// Check for LoRaWAN
if lorawan := ttnUp.ProtocolMetadata.GetLorawan(); lorawan == nil {
return errors.NewErrInvalidArgument("Activation", "does not contain LoRaWAN metadata")
}
// LoRaWAN: Unmarshal Uplink
var phyPayload lorawan.PHYPayload
err = phyPayload.UnmarshalBinary(ttnUp.Payload)
if err != nil {
return err
}
macPayload, ok := phyPayload.MACPayload.(*lorawan.MACPayload)
if !ok {
return errors.NewErrInvalidArgument("Uplink", "does not contain a MAC payload")
}
macPayload.FHDR.FCnt = ttnUp.ProtocolMetadata.GetLorawan().FCnt
appUp.FCnt = macPayload.FHDR.FCnt
ctx = ctx.WithField("FCnt", appUp.FCnt)
// LoRaWAN: Validate MIC
ok, err = phyPayload.ValidateMIC(lorawan.AES128Key(dev.NwkSKey))
if err != nil {
return err
}
if !ok {
return errors.NewErrNotFound("device that validates MIC")
}
// LoRaWAN: Decrypt
if macPayload.FPort != nil && *macPayload.FPort != 0 && len(macPayload.FRMPayload) == 1 {
appUp.FPort = *macPayload.FPort
ctx = ctx.WithField("FCnt", appUp.FPort)
if err := phyPayload.DecryptFRMPayload(lorawan.AES128Key(dev.AppSKey)); err != nil {
return errors.NewErrInternal("Could not decrypt payload")
}
if len(macPayload.FRMPayload) == 1 {
payload, ok := macPayload.FRMPayload[0].(*lorawan.DataPayload)
if !ok {
return errors.NewErrInvalidArgument("Uplink FRMPayload", "must be of type *lorawan.DataPayload")
}
appUp.PayloadRaw = payload.Bytes
}
}
// LoRaWAN: Publish ACKs as events
if macPayload.FHDR.FCtrl.ACK {
h.mqttEvent <- &types.DeviceEvent{
AppID: appUp.AppID,
DevID: appUp.DevID,
Event: types.DownlinkAckEvent,
}
}
return nil
}