Esempio n. 1
0
        public DecodedLoraPacket DecodeMessage(string encodedPhysicalPayload)
        {
            // conver to hex
            var byteArray     = Convert.FromBase64String(encodedPhysicalPayload);
            var hexValuesList = BitConverter.ToString(byteArray).Split("-").ToList();

            // create packet
            DecodedLoraPacket decodedPacket = new DecodedLoraPacket()
            {
                OriginalHexString = string.Join("", hexValuesList)
            };

            var physicalPayload = new PhysicalPayload
            {
                MacHeader = hexValuesList[0],
                MIC       = string.Join("", hexValuesList).Substring((hexValuesList.Count * 2) - 8, 8)
            };

            hexValuesList.RemoveRange(hexValuesList.Count - 4, 4);
            hexValuesList.RemoveAt(0);

            var frameHeader = new FrameHeader
            {
                DeviceAddress     = hexValuesList[3] + hexValuesList[2] + hexValuesList[1] + hexValuesList[0], // big endian
                FrameControlOctet = hexValuesList[4],
                FrameCounter      = hexValuesList[6] + hexValuesList[5]                                        // big endian
            };

            // frame options is optional, need to work out size / if its present using octet
            var binaryFrameControl = Convert.ToString(Convert.ToInt32(frameHeader.FrameControlOctet, 16), 2);
            var frameOptionsOffset = Convert.ToInt32(binaryFrameControl.Substring(binaryFrameControl.Length - 3, 3));

            if (frameOptionsOffset != 0)
            {
                for (int i = 1; i <= frameOptionsOffset; i++)
                {
                    frameHeader.FrameOptions += hexValuesList[6 + i];
                }
            }

            // FramePayload
            var values        = string.Join("", hexValuesList);
            var payloadOffset = 16 + (frameOptionsOffset * 2);

            var macPayload = new MacPayload
            {
                FramePort    = hexValuesList[7 + frameOptionsOffset],
                FramePayload = values.Substring(payloadOffset, values.Length - payloadOffset),
                FrameHeader  = frameHeader
            };

            // combine structures
            physicalPayload.MacPayload    = macPayload;
            decodedPacket.PhysicalPayload = physicalPayload;

            return(decodedPacket);
        }
Esempio n. 2
0
        public string ToVerboseString()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("Message Type = {0}", Mhdr.MessageType).AppendLine();
            sb.AppendFormat("  PHYPayload = {0}", Packet.ToHexString()).AppendLine();
            sb.AppendLine();
            sb.AppendFormat(" (PHYPayload = MHDR[1] | MACPayload[..] | MIC[4])").AppendLine();
            sb.AppendFormat("        MHDR = {0}", Mhdr.RawData.ToHexString()).AppendLine();
            sb.AppendFormat("  MACPayload = {0}", MacPayload.RawData.ToHexString()).AppendLine();
            sb.AppendFormat("         MIC = {0}", Mic.RawData.ToHexString()).AppendLine();
            sb.AppendLine();
            sb.Append(MacPayload.ToVerboseString());
            return(sb.ToString());
        }