Esempio n. 1
0
 public ErosResponseBuilder()
 {
     ResponseMessage = new ErosMessage();
 }
        public List <RadioPacket> GetRadioPackets(ErosMessage message)
        {
            // this.message_str_prefix = $"{this.address:%08X} {this.sequence:%02X} {this.expect_critical_followup} ";

            var message_body_len = 0;

            foreach (var p in message.GetParts())
            {
                var ep       = p as ErosRequest;
                var cmd_body = ep.PartData;
                var nonce    = ep.Nonce;
                message_body_len += (int)cmd_body.Length + 2;
                if (ep.RequiresNonce)
                {
                    message_body_len += 4;
                }
            }

            byte b0 = 0;

            if (MessageExchangeParameters.CriticalWithFollowupRequired)
            {
                b0 = 0x80;
            }

            var msgSequence = Pod.MessageSequence;

            if (MessageExchangeParameters.MessageSequenceOverride.HasValue)
            {
                msgSequence = MessageExchangeParameters.MessageSequenceOverride.Value;
            }

            b0 |= (byte)(msgSequence << 2);
            b0 |= (byte)((message_body_len >> 8) & 0x03);
            byte b1 = (byte)(message_body_len & 0xff);

            var msgAddress = Pod.RadioAddress;

            if (MessageExchangeParameters.AddressOverride.HasValue)
            {
                msgAddress = MessageExchangeParameters.AddressOverride.Value;
            }

            var message_body = new Bytes(msgAddress);

            message_body.Append(b0);
            message_body.Append(b1);

            foreach (var p in message.GetParts())
            {
                var ep       = p as ErosRequest;
                var cmd_type = (byte)ep.PartType;
                var cmd_body = ep.PartData;
                var nonce    = ep.Nonce;

                if (ep.RequiresNonce)
                {
                    message_body.Append(cmd_type);
                    message_body.Append((byte)(cmd_body.Length + 4));
                    message_body.Append(nonce);
                }
                else
                {
                    if (cmd_type == (byte)PartType.ResponseStatus)
                    {
                        message_body.Append(cmd_type);
                    }
                    else
                    {
                        message_body.Append(cmd_type);
                        message_body.Append((byte)cmd_body.Length);
                    }
                }
                message_body.Append(cmd_body[0]);
            }
            var crc_calculated = CrcUtil.Crc16(message_body.ToArray());

            message_body.Append(crc_calculated);

            int  index          = 0;
            bool first_packet   = true;
            int  sequence       = Pod.PacketSequence;
            int  total_body_len = (int)message_body.Length;
            var  radio_packets  = new List <RadioPacket>();
            var  ackAddress     = msgAddress;

            if (MessageExchangeParameters.AckAddressOverride.HasValue)
            {
                ackAddress = MessageExchangeParameters.AckAddressOverride.Value;
            }

            while (index < message_body.Length)
            {
                var to_write    = Math.Min(31, message_body.Length - index);
                var packet_body = message_body.Sub(index, index + to_write);
                radio_packets.Add(new RadioPacket(ackAddress,
                                                  first_packet ? PacketType.PDM : PacketType.CON,
                                                  sequence,
                                                  packet_body));
                first_packet = false;
                sequence     = (sequence + 2) % 32;
                index       += to_write;
            }

            if (MessageExchangeParameters.RepeatFirstPacket)
            {
                var fp = radio_packets[0];
                radio_packets.Insert(0, fp);
            }
            return(radio_packets);
        }