MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / UnmarshalMessage

Function UnmarshalMessage

pkg/encoding/lorawan/messages.go:564–636  ·  view source on GitHub ↗

UnmarshalMessage unmarshals b into msg.

(b []byte, msg *ttnpb.Message)

Source from the content-addressed store, hash-verified

562
563// UnmarshalMessage unmarshals b into msg.
564func UnmarshalMessage(b []byte, msg *ttnpb.Message) error {
565 n := len(b)
566 if n == 0 {
567 return errMissing("PHYPayload")
568 }
569 msg.MHdr = &ttnpb.MHDR{}
570 if err := UnmarshalMHDR(b[0:1], msg.MHdr); err != nil {
571 return errFailedDecoding("MHDR").WithCause(err)
572 }
573 switch msg.MHdr.MType {
574 case ttnpb.MType_CONFIRMED_DOWN, ttnpb.MType_UNCONFIRMED_DOWN:
575 if n < 12 {
576 return errExpectedLengthHigherOrEqual("FHDR", 7)(n - 5)
577 }
578 pld := &ttnpb.MACPayload{}
579 if err := UnmarshalMACPayload(b[1:n-4], pld, false); err != nil {
580 return errFailedDecoding("MACPayload").WithCause(err)
581 }
582 msg.Payload = &ttnpb.Message_MacPayload{MacPayload: pld}
583 msg.Mic = b[n-4:]
584 case ttnpb.MType_CONFIRMED_UP, ttnpb.MType_UNCONFIRMED_UP:
585 if n < 12 {
586 return errExpectedLengthHigherOrEqual("FHDR", 7)(n - 5)
587 }
588 pld := &ttnpb.MACPayload{}
589 if err := UnmarshalMACPayload(b[1:n-4], pld, true); err != nil {
590 return errFailedDecoding("MACPayload").WithCause(err)
591 }
592 msg.Payload = &ttnpb.Message_MacPayload{MacPayload: pld}
593 msg.Mic = b[n-4:]
594 case ttnpb.MType_JOIN_REQUEST:
595 if n != 23 {
596 return errExpectedLengthEqual("JoinRequestPHYPayload", 23)(n)
597 }
598 pld := &ttnpb.JoinRequestPayload{}
599 if err := UnmarshalJoinRequestPayload(b[1:19], pld); err != nil {
600 return errFailedDecoding("JoinRequestPayload").WithCause(err)
601 }
602 msg.Payload = &ttnpb.Message_JoinRequestPayload{JoinRequestPayload: pld}
603 msg.Mic = b[19:]
604 case ttnpb.MType_REJOIN_REQUEST:
605 if n < 2 {
606 return errExpectedLengthTwoChoices("RejoinRequestPHYPayload", 19, 24)(n)
607 }
608 var micIdx int
609 if b[1] == 1 {
610 // MHDR(1) + Payload(19)
611 micIdx = 20
612 } else {
613 // MHDR(1) + Payload(14)
614 micIdx = 15
615 }
616 if n != micIdx+4 {
617 return errExpectedLengthTwoChoices("RejoinRequestPHYPayload", 19, 24)(n)
618 }
619 pld := &ttnpb.RejoinRequestPayload{}
620 if err := UnmarshalRejoinRequestPayload(b[1:micIdx], pld); err != nil {
621 return errFailedDecoding("RejoinRequestPayload").WithCause(err)

Callers 15

toPBUplinkFunction · 0.92
HandleJoinMethod · 0.92
handleUpstreamMethod · 0.92
TestTrafficFunction · 0.92
TestRTTFunction · 0.92
TestJoinRequestFunction · 0.92
TestFrontendFunction · 0.92
FromUplinkMessageMethod · 0.92
FromUplinkMessageMethod · 0.92
TestProtobufV2DownlinkFunction · 0.92
TestProcessDownlinkTaskFunction · 0.92

Calls 13

UnmarshalMHDRFunction · 0.85
errFailedDecodingFunction · 0.85
UnmarshalMACPayloadFunction · 0.85
errExpectedLengthEqualFunction · 0.85
errUnknownFunction · 0.85
errMissingFunction · 0.70
NewMethod · 0.65
StringMethod · 0.65

Tested by 10

TestTrafficFunction · 0.74
TestRTTFunction · 0.74
TestJoinRequestFunction · 0.74
TestFrontendFunction · 0.74
TestProtobufV2DownlinkFunction · 0.74
TestProcessDownlinkTaskFunction · 0.74
TestLoRaWANEncodingRawFunction · 0.68
TestUnmarshalResilienceFunction · 0.68