Ejemplo n.º 1
0
        public List <byte> Encode(XElement xParentElem)
        {
            packet = new List <byte>();
            XElement xElem = (XElement)xParentElem.Element("NetworkCtl");

            packet.Add(0x01); // Version
            packet.Add(0x00); // default values
            var value = xElem.Element("MessageType").Value;

            messageType    = (MTYPE)System.Enum.Parse(typeof(MTYPE), value, true);
            packet[1]      = EncodeMType(messageType, packet[1]);
            value          = xElem.Element("ExpectingReply").Value;
            expectingReply = (CONFIRM)System.Enum.Parse(typeof(CONFIRM), value, true);
            if (xElem.Element("Destination") != null)
            {
                packet[1] = EncodeDestonation(1, packet[1]);
                AddrUnit a = new AddrUnit(AddrUnit.ADDR_TYPE.DESTONATION);
                packet.AddRange(a.Encode(xElem.Element("Destination")));
            }
            if (xElem.Element("Source") != null)
            {
                packet[1] = EncodeSource(1, packet[1]);
                AddrUnit a = new AddrUnit(AddrUnit.ADDR_TYPE.SOURCE);
                packet.AddRange(a.Encode(xElem.Element("Destination")));
            }
            if (xElem.Element("Destination") != null)
            {
                byte hops = 0;
                if (xElem.Element("Destination").Element("HopCount") != null)
                {
                    value = xElem.Element("Destination").Element("HopCount").Value;
                    hops  = Convert.ToByte(value, 16);
                }
                packet.Add(hops);
            }
            if (messageType == MTYPE.APPICATION)
            {
                packet.AddRange(pdu.Encode(xParentElem));
            }
            else
            {
                nsdu = nsdu.Encode(xParentElem);
            }

            return(packet);
        }
Ejemplo n.º 2
0
        public Boolean Decode(BACPacket cm)
        {
            pciVersion = cm.getNextByte(); // PIC Version is allways 0x01

            if (pciVersion != 0x01)
            {
                return(false);
            }

            byte picByt = cm.getNextByte();

            messageType    = this.DecodeMType(picByt);
            destPresent    = this.DecodeDestonation(picByt);
            srcPresent     = this.DecodeSource(picByt);
            expectingReply = this.DecodeExpectingReply(picByt);
            priority       = this.DecodePriority(picByt);

            //IF it contains the destonation addr get it
            if (destPresent == PRESENCES.EXIST)
            {
                destAddr.Decode(cm);
            }

            //IF it contains the source addr get it
            if (srcPresent == PRESENCES.EXIST)
            {
                srcAddr.Decode(cm);
            }

            //IF it contains the destonation don't forget the hop count
            if (destPresent == PRESENCES.EXIST)
            {
                hopCnt = cm.getNextByte();
            }

            if (messageType == MTYPE.APPICATION)
            {
                pdu = pdu.Decode(cm);
            }
            else
            {
                nsdu = nsdu.Decode(cm);
            }

            return(true);
        }