func msgFromPayload(payload []byte) (*pb_protocol.Message, error) { var phy lorawan.PHYPayload if err := phy.UnmarshalBinary(payload); err != nil { return nil, err } msg := pb_lorawan.MessageFromPHYPayload(phy) return &pb_protocol.Message{Protocol: &pb_protocol.Message_Lorawan{Lorawan: &msg}}, nil }
// UnmarshalPayload unmarshals the Payload into Message if Message is nil func (m *DeviceActivationResponse) UnmarshalPayload() error { if m.GetMessage() == nil && m.GetDownlinkOption() != nil && m.DownlinkOption.GetProtocolConfig() != nil && m.DownlinkOption.ProtocolConfig.GetLorawan() != nil { var phy lorawan.PHYPayload if err := phy.UnmarshalBinary(m.Payload); err != nil { return err } msg := pb_lorawan.MessageFromPHYPayload(phy) m.Message = &pb_protocol.Message{Protocol: &pb_protocol.Message_Lorawan{Lorawan: &msg}} } return nil }
} time.Sleep(100 * time.Millisecond) ctx.Info("Sent uplink to Router") select { case downlinkMessage, ok := <-downlinkStream.Channel(): if !ok { ctx.Info("Did not receive downlink") break } downlinkMessage.UnmarshalPayload() resPhy := downlinkMessage.Message.GetLorawan().PHYPayload() resPhy.DecryptJoinAcceptPayload(lorawan.AES128Key(appKey)) res := pb_lorawan.MessageFromPHYPayload(resPhy) accept := res.GetJoinAcceptPayload() appSKey, nwkSKey, _ := otaa.CalculateSessionKeys(appKey, accept.AppNonce, accept.NetId, devNonce) ctx.WithFields(log.Fields{ "DevAddr": accept.DevAddr, "NwkSKey": nwkSKey, "AppSKey": appSKey, }).Info("Received JoinAccept") case <-time.After(6 * time.Second): ctx.Info("Did not receive downlink") } }, }