using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Timers;
using System.Windows.Forms;
using QCRPG.Framework;
using QCRPG.TSL.BT.Packets;

namespace QCRPG.TSL.BT;

public class ProdTestUserControl : UserControlBase
{
	public struct BTRxStatsChannel
	{
		public uint packetReceived;

		public uint packetAccessError;

		public uint hecError;

		public uint crcError;

		public uint totalPacketBitErrors;

		public uint firstHalfBitErrors;

		public uint lastHalfBitErrors;

		public int rssiReading;
	}

	public struct BTRXStatsTotal
	{
		public uint packetReceivedTotal;

		public uint totalPacketBitErrorsTotal;

		public uint GoodPacketTotal;

		public uint GoodPacketCRCTotal;

		public uint packetAccessErrorTotal;

		public uint hecErrorTotal;

		public uint crcErrorTotal;

		public uint firstHalfBitErrorsTotal;

		public uint lastHalfBitErrorsTotal;
	}

	private string BDAddress = "BD,35,9C,BD,35,9C";

	private bool FixedChannel = true;

	private int[] Channel = new int[5];

	public bool HopChannel;

	private int ConnectionChannel;

	private ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum PacketType = ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DH5;

	private int PayloadLength = 339;

	private byte LTAddress;

	private byte Power = 9;

	public bool WhiteningEnable;

	private ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum TransmitPattern = ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum.PSEUDO_RANDOM;

	private byte ContinuousPower = 7;

	private int ContinuousChannel = 41;

	private ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum TransmitType;

	private byte PatternLength;

	private string Pattern = "00000000";

	private bool RXReady;

	private System.Timers.Timer getStatsTimer = new System.Timers.Timer();

	private BTMainWorkerThread btMainWorkerThread;

	private BTLERemoteDeviceInfo BTLERemoteDeviceInfoList;

	private SpecificPacketNotifier specificPacketNotifier;

	private BTGooier btGooier;

	private BTRXStatsTotal RxStatsTotal = default(BTRXStatsTotal);

	private BTRXStatsTotal[] RXStatsChanTotal = new BTRXStatsTotal[5];

	private DateTime timeRecord = DateTime.MinValue;

	private IContainer components;

	private ListView infoListView;

	private GroupBox testModeGroupBox;

	private RadioButton rxBurstRadioButton;

	private RadioButton txContRadioButton;

	private RadioButton txBurstRadioButton;

	private RadioButton masterRadioButton;

	private RadioButton slaveRadioButton;

	private CheckBox periodicCheckBox;

	private CheckBox percentCheckBox;

	private CheckBox drwithCRCCheckBox;

	private GroupBox dataRateGroupBox;

	private TextBox dataRateTextBox;

	private Button optionButton;

	private Button startButton;

	private Button stopButton;

	private Button getStatsButton;

	public SpecificPacketNotifier.PacketNotifier UpdateProdTestInfo => ProdTest_UpdateProdTestInfo;

	public ProdTestUserControl(BTMainWorkerThread btMainWorkerThread, SpecificPacketNotifier specificPacketNotifier, BTLERemoteDeviceInfo BTLERemoteDeviceInfoList, BTGooier btGooier)
		: base("ProdTest")
	{
		InitializeComponent();
		this.btMainWorkerThread = btMainWorkerThread;
		this.specificPacketNotifier = specificPacketNotifier;
		this.BTLERemoteDeviceInfoList = BTLERemoteDeviceInfoList;
		this.btGooier = btGooier;
	}

	private void ProdTestUserControl_Load(object sender, EventArgs e)
	{
		infoListView.GridLines = false;
		infoListView.FullRowSelect = true;
		infoListView.View = View.Details;
		infoListView.Scrollable = true;
		infoListView.MultiSelect = false;
		infoListView.Columns.Add("Hop Channel");
		infoListView.Columns.Add("RcvdPkts");
		infoListView.Columns.Add("RSSI");
		infoListView.Columns.Add("BitErr");
		infoListView.Columns.Add("GoodPkts");
		infoListView.Columns.Add("ACErr");
		infoListView.Columns.Add("HECErr");
		infoListView.Columns.Add("CRCErr");
		infoListView.Columns.Add("BitErrBegin");
		infoListView.Columns.Add("BitErrEnd");
		slaveRadioButton.Checked = true;
		masterRadioButton.Checked = false;
		txBurstRadioButton.Checked = false;
		txContRadioButton.Checked = false;
		rxBurstRadioButton.Checked = false;
		getStatsTimer.Enabled = false;
		dataRateTextBox.Text = "0";
		getStatsTimer.Interval = 2000.0;
		getStatsTimer.Enabled = false;
		getStatsTimer.Elapsed += getStatsTimer_Elapsed;
		btMainWorkerThread.GetRxStatsEventHandler += btMainWorkerThread_GetRxStatsEventHandler;
		btMainWorkerThread.ErrEventHandler += btMainWorkerThread_ErrEventHandler;
		btMainWorkerThread.RxBurstModeReadyHandler += btMainWorkerThread_RxBurstModeReadyHandler;
		specificPacketNotifier.AddPacketNotification(typeof(ConnectionComplete), ProdTest_UpdateProdTestInfo);
	}

	private void btMainWorkerThread_GetRxStatsEventHandler(object sender, List<PacketBase> packet)
	{
		DateTime now = DateTime.Now;
		double timeElapse = (now - timeRecord).TotalMilliseconds;
		int num = 2;
		if (packet[0] is CommandCompleteForProdTestStats)
		{
			num = 5;
		}
		BTRxStatsChannel[] array = new BTRxStatsChannel[5];
		for (int i = 0; i < 5; i++)
		{
			array[i].packetReceived = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].packetAccessError = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].hecError = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].crcError = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].totalPacketBitErrors = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].firstHalfBitErrors = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].lastHalfBitErrors = uint.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
			array[i].rssiReading = int.Parse(packet[0].Info.FieldInfoList[num].Value, NumberStyles.HexNumber);
			num++;
		}
		if (timeRecord == DateTime.MinValue)
		{
			timeElapse = 0.0;
		}
		UpdateView(infoListView, dataRateTextBox, array, timeElapse);
		timeRecord = now;
	}

	private void btMainWorkerThread_ErrEventHandler(object sender)
	{
		startButton.Enabled = true;
		stopButton.Enabled = false;
		getStatsButton.Enabled = false;
	}

	private void btMainWorkerThread_RxBurstModeReadyHandler(object sender, bool btimer)
	{
		if (btimer && periodicCheckBox.Checked)
		{
			getStatsTimer.Enabled = true;
		}
		else
		{
			getStatsTimer.Enabled = false;
		}
	}

	private void txBurstRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (txBurstRadioButton.Checked)
		{
			periodicCheckBox.Visible = false;
			percentCheckBox.Visible = false;
			drwithCRCCheckBox.Visible = false;
			dataRateGroupBox.Visible = false;
		}
	}

	private void txContRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (txContRadioButton.Checked)
		{
			periodicCheckBox.Visible = false;
			percentCheckBox.Visible = false;
			drwithCRCCheckBox.Visible = false;
			dataRateGroupBox.Visible = false;
		}
	}

	private void slaveRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (slaveRadioButton.Checked)
		{
			periodicCheckBox.Visible = true;
			percentCheckBox.Visible = true;
			drwithCRCCheckBox.Visible = true;
			dataRateGroupBox.Visible = true;
		}
	}

	private void masterRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (masterRadioButton.Checked)
		{
			periodicCheckBox.Visible = true;
			percentCheckBox.Visible = true;
			drwithCRCCheckBox.Visible = true;
			dataRateGroupBox.Visible = true;
		}
	}

	private void rxBurstRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (rxBurstRadioButton.Checked)
		{
			periodicCheckBox.Visible = true;
			percentCheckBox.Visible = true;
			drwithCRCCheckBox.Visible = true;
			dataRateGroupBox.Visible = true;
		}
	}

	private void optionButton_Click(object sender, EventArgs e)
	{
		if (txContRadioButton.Checked)
		{
			startButton.Enabled = true;
			ContinuousModeOption continuousModeOption = new ContinuousModeOption();
			continuousModeOption.continuousPower = ContinuousPower;
			continuousModeOption.continuousChannel = ContinuousChannel;
			continuousModeOption.transmitType = TransmitType;
			continuousModeOption.patternLength = PatternLength;
			continuousModeOption.pattern = Pattern;
			continuousModeOption.ShowDialog();
			ContinuousPower = continuousModeOption.continuousPower;
			ContinuousChannel = continuousModeOption.continuousChannel;
			TransmitType = continuousModeOption.transmitType;
			PatternLength = continuousModeOption.patternLength;
			Pattern = continuousModeOption.pattern;
		}
		else
		{
			startButton.Enabled = true;
			BurstModeOption burstModeOption = new BurstModeOption();
			burstModeOption.bdAddress = BDAddress;
			burstModeOption.fixedChannel = FixedChannel;
			burstModeOption.channel = Channel;
			burstModeOption.hopChannel = HopChannel;
			burstModeOption.connectionChannel = ConnectionChannel;
			burstModeOption.packetType = PacketType;
			burstModeOption.payloadLength = PayloadLength;
			burstModeOption.ltAddress = LTAddress;
			burstModeOption.power = Power;
			burstModeOption.whiteningEnable = WhiteningEnable;
			burstModeOption.transmitPattern = TransmitPattern;
			burstModeOption.ShowDialog();
			BDAddress = burstModeOption.bdAddress;
			FixedChannel = burstModeOption.fixedChannel;
			Channel = burstModeOption.channel;
			HopChannel = burstModeOption.hopChannel;
			ConnectionChannel = burstModeOption.connectionChannel;
			PacketType = burstModeOption.packetType;
			PayloadLength = burstModeOption.payloadLength;
			LTAddress = burstModeOption.ltAddress;
			Power = burstModeOption.power;
			WhiteningEnable = burstModeOption.whiteningEnable;
			TransmitPattern = burstModeOption.transmitPattern;
		}
	}

	private void startButton_Click(object sender, EventArgs e)
	{
		byte[] bluetoothAddress = ParseBluetoothAddress(BDAddress);
		int packetType = PacketTypeValueExchange(PacketType);
		int transmitPattern = TransmitPatternValueExchange(TransmitPattern);
		int tranmitType = TransmitTypeValueExchange(TransmitType);
		long pattern = TransmitPatternValueExchange(Pattern);
		optionButton.Enabled = true;
		if (slaveRadioButton.Checked)
		{
			btMainWorkerThread.Slave(bluetoothAddress, Channel, packetType, PayloadLength, LTAddress, Power, WhiteningEnable, transmitPattern);
		}
		else if (masterRadioButton.Checked)
		{
			btMainWorkerThread.Master(bluetoothAddress, Channel, packetType, PayloadLength, LTAddress, Power, WhiteningEnable, transmitPattern);
		}
		else if (txBurstRadioButton.Checked)
		{
			startButton.Enabled = false;
			stopButton.Enabled = true;
			btMainWorkerThread.TxBurst(bluetoothAddress, Channel, packetType, PayloadLength, LTAddress, Power, WhiteningEnable, transmitPattern, HopChannel, ConnectionChannel);
		}
		else if (txContRadioButton.Checked)
		{
			startButton.Enabled = false;
			stopButton.Enabled = true;
			btMainWorkerThread.TxContinuous(ContinuousPower, ContinuousChannel, tranmitType, PatternLength, pattern);
		}
		else if (rxBurstRadioButton.Checked)
		{
			startButton.Enabled = false;
			stopButton.Enabled = true;
			getStatsButton.Enabled = true;
			btMainWorkerThread.RxBurst(bluetoothAddress, Channel, packetType, PayloadLength, LTAddress, Power, WhiteningEnable, transmitPattern, HopChannel, ConnectionChannel);
			specificPacketNotifier.SetPacketNotification(typeof(ConnectionComplete), ProdTest_UpdateProdTestInfo, enabled: true);
		}
	}

	private byte[] ParseBluetoothAddress(string btAddress)
	{
		if (btAddress == null)
		{
			throw new Exception("Failed to find Bluetooth address from the config file");
		}
		string[] array = btAddress.Split(',');
		if (array.Length != 6)
		{
			throw new Exception("Bluetooth address listed in the waveform file is not in correct format, Sample : BD,35,9C,BD,35,9C");
		}
		byte[] array2 = new byte[6];
		for (int i = 0; i < 6; i++)
		{
			array2[i] = byte.Parse(array[i], NumberStyles.HexNumber);
		}
		return array2;
	}

	private int PacketTypeValueExchange(ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum PacketType)
	{
		int result = 0;
		switch (PacketType)
		{
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.NULL:
			result = 0;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.POLL:
			result = 1;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.FHS:
			result = 2;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DM1:
			result = 3;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DH1:
			result = 4;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DM3:
			result = 10;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DH3:
			result = 11;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DM5:
			result = 14;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DH5:
			result = 15;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.AUX1:
			result = 9;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.TWO_DH1:
			result = 36;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.TWO_DH3:
			result = 42;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.TWO_DH5:
			result = 46;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.THREE_DH1:
			result = 40;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.THREE_DH3:
			result = 43;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.THREE_DH5:
			result = 47;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.HV1:
			result = 5;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.HV2:
			result = 6;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.HV3:
			result = 7;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.DV:
			result = 8;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.EV3:
			result = 23;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.EV4:
			result = 28;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.EV5:
			result = 29;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.TWO_EV3:
			result = 54;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.TWO_EV5:
			result = 60;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.THREE_EV3:
			result = 55;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.PacketTypeEnum.THREE_EV5:
			result = 61;
			break;
		}
		return result;
	}

	private int TransmitPatternValueExchange(ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum TransmitPattern)
	{
		int result = 0;
		switch (TransmitPattern)
		{
		case ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum.ALL_ZEROES:
			result = 0;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum.ALL_ONES:
			result = 1;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum.ALTERNATING_BIT:
			result = 2;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum.ALTERNATING_NIBBLE:
			result = 3;
			break;
		case ProdTestTxBurst.UserFieldsWrapper.TxPatternPayloadEnum.PSEUDO_RANDOM:
			result = 4;
			break;
		}
		return result;
	}

	private int TransmitTypeValueExchange(ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum TransmitType)
	{
		int result = 0;
		switch (TransmitType)
		{
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.CW_ONLY:
			result = 4;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.ONE_PRBS9_GFSK:
			result = 5;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.ONE_PRBS15_GFSK:
			result = 6;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.ONE_PATTERN_GFSK:
			result = 7;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.TWO_PRBS9_PI_DIV_4_DQPSK:
			result = 8;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.TWO_PRBS15_PI_DIV_4_DQPSK:
			result = 9;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.TWO_PATTERN_PI_DIV_4_DQPSK:
			result = 10;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.THREE_PRBS9_8DPSK:
			result = 11;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.THREE_PRBS15_8DPSK:
			result = 12;
			break;
		case ProdTestTxContinuous.UserFieldsWrapper.TransmitTypeEnum.THREE_PATTERN_8DPSK:
			result = 13;
			break;
		}
		return result;
	}

	private long TransmitPatternValueExchange(string Pattern)
	{
		return Convert.ToInt64(Pattern, 16);
	}

	public void Stop()
	{
		if (stopButton.Enabled)
		{
			stopButton_Click(this, null);
		}
	}

	private void stopButton_Click(object sender, EventArgs e)
	{
		btMainWorkerThread.Reset();
		startButton.Enabled = true;
		stopButton.Enabled = false;
		getStatsButton.Enabled = false;
		optionButton.Enabled = true;
		if (getStatsTimer.Enabled)
		{
			getStatsTimer.Enabled = false;
		}
		if (BTLERemoteDeviceInfoList != null)
		{
			string bdAddress = BDAddress.Replace(",", "");
			BTLERemoteDeviceInfoList.RemoveBTRemoteDeviceByBDAddress(bdAddress);
			btGooier.RemoveBTDevice(bdAddress);
		}
		RxStatsTotal = default(BTRXStatsTotal);
		for (int i = 0; i < 5; i++)
		{
			RXStatsChanTotal[i] = default(BTRXStatsTotal);
		}
		if (rxBurstRadioButton.Checked)
		{
			specificPacketNotifier.SetPacketNotification(typeof(ConnectionComplete), ProdTest_UpdateProdTestInfo, enabled: false);
			RXReady = false;
		}
	}

	private void getStatsButton_Click(object sender, EventArgs e)
	{
		if (RXReady)
		{
			infoListView.Items.Clear();
			btMainWorkerThread.GetRxStats();
		}
	}

	public void UpdateView(ListView listview, TextBox textbox, BTRxStatsChannel[] RxStatistics, double timeElapse)
	{
		if (listview.InvokeRequired)
		{
			listview.Invoke((MethodInvoker)delegate
			{
				UpdateView(listview, textbox, RxStatistics, timeElapse);
			});
			return;
		}
		int num = 0;
		uint goodPacketTotal = RxStatsTotal.GoodPacketTotal;
		uint goodPacketCRCTotal = RxStatsTotal.GoodPacketCRCTotal;
		for (int i = 0; i < 5; i++)
		{
			RxStatsTotal.packetReceivedTotal += RxStatistics[i].packetReceived;
			num += RxStatistics[i].rssiReading;
			RxStatsTotal.totalPacketBitErrorsTotal += RxStatistics[i].totalPacketBitErrors;
			RxStatsTotal.GoodPacketTotal += RxStatistics[i].packetReceived - RxStatistics[i].packetAccessError - RxStatistics[i].crcError - RxStatistics[i].hecError;
			RxStatsTotal.GoodPacketCRCTotal += RxStatistics[i].packetReceived - RxStatistics[i].packetAccessError - RxStatistics[i].hecError;
			RxStatsTotal.packetAccessErrorTotal += RxStatistics[i].packetAccessError;
			RxStatsTotal.hecErrorTotal += RxStatistics[i].hecError;
			RxStatsTotal.crcErrorTotal += RxStatistics[i].crcError;
			RxStatsTotal.firstHalfBitErrorsTotal += RxStatistics[i].firstHalfBitErrors;
			RxStatsTotal.lastHalfBitErrorsTotal += RxStatistics[i].lastHalfBitErrors;
		}
		if (timeElapse == 0.0)
		{
			textbox.Text = "0";
		}
		else
		{
			uint num2 = 0u;
			num2 = ((!drwithCRCCheckBox.Checked) ? (RxStatsTotal.GoodPacketTotal - goodPacketTotal) : (RxStatsTotal.GoodPacketCRCTotal - goodPacketCRCTotal));
			double num3 = (double)(num2 * PayloadLength) * 8.0 / timeElapse;
			textbox.Text = num3.ToString("F3");
		}
		if (!percentCheckBox.Checked)
		{
			ListViewItem listViewItem = new ListViewItem();
			listViewItem.Text = "Total";
			listViewItem.SubItems.Add(RxStatsTotal.packetReceivedTotal.ToString());
			num /= 5;
			listViewItem.SubItems.Add(num.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.totalPacketBitErrorsTotal.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.GoodPacketTotal.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.packetAccessErrorTotal.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.hecErrorTotal.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.crcErrorTotal.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.firstHalfBitErrorsTotal.ToString());
			listViewItem.SubItems.Add(RxStatsTotal.lastHalfBitErrorsTotal.ToString());
			listview.Items.Add(listViewItem);
			listview.Items.Add(new ListViewItem());
			for (int j = 0; j < 5; j++)
			{
				ListViewItem listViewItem2 = new ListViewItem();
				listViewItem2.Text = Channel[j].ToString();
				RXStatsChanTotal[j].packetReceivedTotal += RxStatistics[j].packetReceived;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].packetReceivedTotal.ToString());
				listViewItem2.SubItems.Add(RxStatistics[j].rssiReading.ToString());
				RXStatsChanTotal[j].totalPacketBitErrorsTotal += RxStatistics[j].totalPacketBitErrors;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].totalPacketBitErrorsTotal.ToString());
				uint num4 = RxStatistics[j].packetReceived - RxStatistics[j].packetAccessError - RxStatistics[j].crcError - RxStatistics[j].hecError;
				RXStatsChanTotal[j].GoodPacketTotal += num4;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].GoodPacketTotal.ToString());
				RXStatsChanTotal[j].packetAccessErrorTotal += RxStatistics[j].packetAccessError;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].packetAccessErrorTotal.ToString());
				RXStatsChanTotal[j].hecErrorTotal += RxStatistics[j].hecError;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].hecErrorTotal.ToString());
				RXStatsChanTotal[j].crcErrorTotal += RxStatistics[j].crcError;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].crcErrorTotal.ToString());
				RXStatsChanTotal[j].firstHalfBitErrorsTotal += RxStatistics[j].firstHalfBitErrors;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].firstHalfBitErrorsTotal.ToString());
				RXStatsChanTotal[j].lastHalfBitErrorsTotal += RxStatistics[j].lastHalfBitErrors;
				listViewItem2.SubItems.Add(RXStatsChanTotal[j].lastHalfBitErrorsTotal.ToString());
				listview.Items.Add(listViewItem2);
			}
			return;
		}
		string text = "";
		ListViewItem listViewItem3 = new ListViewItem();
		listViewItem3.Text = "Total";
		listViewItem3.SubItems.Add(RxStatsTotal.packetReceivedTotal.ToString());
		num /= 5;
		listViewItem3.SubItems.Add(num.ToString());
		text = ((double)RxStatsTotal.totalPacketBitErrorsTotal * 100.0 / (double)(RxStatsTotal.packetReceivedTotal * PayloadLength * 8)).ToString("F6");
		listViewItem3.SubItems.Add(text);
		text = ((double)RxStatsTotal.GoodPacketTotal * 100.0 / (double)RxStatsTotal.packetReceivedTotal).ToString("F6");
		listViewItem3.SubItems.Add(text);
		text = ((double)RxStatsTotal.packetAccessErrorTotal * 100.0 / (double)RxStatsTotal.packetReceivedTotal).ToString("F6");
		listViewItem3.SubItems.Add(text);
		text = ((double)RxStatsTotal.hecErrorTotal * 100.0 / (double)RxStatsTotal.packetReceivedTotal).ToString("F6");
		listViewItem3.SubItems.Add(text);
		text = ((double)RxStatsTotal.crcErrorTotal * 100.0 / (double)RxStatsTotal.packetReceivedTotal).ToString("F6");
		listViewItem3.SubItems.Add(text);
		double num5 = (double)RxStatsTotal.firstHalfBitErrorsTotal * 100.0 / (double)(RxStatsTotal.packetReceivedTotal * PayloadLength * 8);
		text = num5.ToString("F6");
		listViewItem3.SubItems.Add(text);
		_ = (double)RxStatsTotal.lastHalfBitErrorsTotal * 100.0 / (double)(RxStatsTotal.packetReceivedTotal * PayloadLength * 8);
		text = num5.ToString("F6");
		listViewItem3.SubItems.Add(text);
		listview.Items.Add(listViewItem3);
		listview.Items.Add(new ListViewItem());
		for (int k = 0; k < 5; k++)
		{
			ListViewItem listViewItem4 = new ListViewItem();
			listViewItem4.Text = Channel[k].ToString();
			RXStatsChanTotal[k].packetReceivedTotal += RxStatistics[k].packetReceived;
			listViewItem4.SubItems.Add(RXStatsChanTotal[k].packetReceivedTotal.ToString());
			listViewItem4.SubItems.Add(RxStatistics[k].rssiReading.ToString());
			RXStatsChanTotal[k].totalPacketBitErrorsTotal += RxStatistics[k].totalPacketBitErrors;
			string text2 = ((double)RXStatsChanTotal[k].totalPacketBitErrorsTotal * 100.0 / (double)(RXStatsChanTotal[k].packetReceivedTotal * PayloadLength * 8)).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			uint num6 = RxStatistics[k].packetReceived - RxStatistics[k].packetAccessError - RxStatistics[k].crcError - RxStatistics[k].hecError;
			RXStatsChanTotal[k].GoodPacketTotal += num6;
			text2 = ((double)RXStatsChanTotal[k].GoodPacketTotal * 100.0 / (double)RXStatsChanTotal[k].packetReceivedTotal).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			RXStatsChanTotal[k].packetAccessErrorTotal += RxStatistics[k].packetAccessError;
			text2 = ((double)RXStatsChanTotal[k].packetAccessErrorTotal * 100.0 / (double)RXStatsChanTotal[k].packetReceivedTotal).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			RXStatsChanTotal[k].hecErrorTotal += RxStatistics[k].hecError;
			text2 = ((double)RXStatsChanTotal[k].hecErrorTotal * 100.0 / (double)RXStatsChanTotal[k].packetReceivedTotal).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			RXStatsChanTotal[k].crcErrorTotal += RxStatistics[k].crcError;
			text2 = ((double)RXStatsChanTotal[k].crcErrorTotal * 100.0 / (double)RXStatsChanTotal[k].packetReceivedTotal).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			RXStatsChanTotal[k].firstHalfBitErrorsTotal += RxStatistics[k].firstHalfBitErrors;
			text2 = ((double)RXStatsChanTotal[k].firstHalfBitErrorsTotal * 100.0 / (double)(RXStatsChanTotal[k].packetReceivedTotal * PayloadLength * 8)).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			RXStatsChanTotal[k].lastHalfBitErrorsTotal += RxStatistics[k].lastHalfBitErrors;
			text2 = ((double)RXStatsChanTotal[k].lastHalfBitErrorsTotal * 100.0 / (double)(RXStatsChanTotal[k].packetReceivedTotal * PayloadLength * 8)).ToString("F6");
			listViewItem4.SubItems.Add(text2);
			listview.Items.Add(listViewItem4);
		}
	}

	private void periodicCheckBox_CheckedChanged(object sender, EventArgs e)
	{
		if (periodicCheckBox.Checked)
		{
			if (rxBurstRadioButton.Checked && getStatsButton.Enabled)
			{
				getStatsTimer.Enabled = true;
			}
		}
		else
		{
			getStatsTimer.Enabled = false;
		}
	}

	private void ProdTest_UpdateProdTestInfo(PacketBase packet)
	{
		if (packet is ConnectionComplete && packet.Info.FieldInfoList[2].Value.ToString() == BDAddress.Replace(",", ""))
		{
			RXReady = true;
		}
	}

	private void getStatsTimer_Elapsed(object sender, EventArgs e)
	{
		if (RXReady)
		{
			infoListView.Items.Clear();
			btMainWorkerThread.GetRxStats();
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.infoListView = new System.Windows.Forms.ListView();
		this.testModeGroupBox = new System.Windows.Forms.GroupBox();
		this.rxBurstRadioButton = new System.Windows.Forms.RadioButton();
		this.txContRadioButton = new System.Windows.Forms.RadioButton();
		this.txBurstRadioButton = new System.Windows.Forms.RadioButton();
		this.masterRadioButton = new System.Windows.Forms.RadioButton();
		this.slaveRadioButton = new System.Windows.Forms.RadioButton();
		this.periodicCheckBox = new System.Windows.Forms.CheckBox();
		this.percentCheckBox = new System.Windows.Forms.CheckBox();
		this.drwithCRCCheckBox = new System.Windows.Forms.CheckBox();
		this.dataRateGroupBox = new System.Windows.Forms.GroupBox();
		this.dataRateTextBox = new System.Windows.Forms.TextBox();
		this.optionButton = new System.Windows.Forms.Button();
		this.startButton = new System.Windows.Forms.Button();
		this.stopButton = new System.Windows.Forms.Button();
		this.getStatsButton = new System.Windows.Forms.Button();
		this.testModeGroupBox.SuspendLayout();
		this.dataRateGroupBox.SuspendLayout();
		base.SuspendLayout();
		this.infoListView.Location = new System.Drawing.Point(4, 4);
		this.infoListView.Name = "infoListView";
		this.infoListView.Size = new System.Drawing.Size(659, 149);
		this.infoListView.TabIndex = 0;
		this.infoListView.UseCompatibleStateImageBehavior = false;
		this.testModeGroupBox.Controls.Add(this.rxBurstRadioButton);
		this.testModeGroupBox.Controls.Add(this.txContRadioButton);
		this.testModeGroupBox.Controls.Add(this.txBurstRadioButton);
		this.testModeGroupBox.Controls.Add(this.masterRadioButton);
		this.testModeGroupBox.Controls.Add(this.slaveRadioButton);
		this.testModeGroupBox.Location = new System.Drawing.Point(4, 168);
		this.testModeGroupBox.Name = "testModeGroupBox";
		this.testModeGroupBox.Size = new System.Drawing.Size(353, 72);
		this.testModeGroupBox.TabIndex = 1;
		this.testModeGroupBox.TabStop = false;
		this.testModeGroupBox.Text = "Test Mode";
		this.rxBurstRadioButton.AutoSize = true;
		this.rxBurstRadioButton.Location = new System.Drawing.Point(128, 43);
		this.rxBurstRadioButton.Name = "rxBurstRadioButton";
		this.rxBurstRadioButton.Size = new System.Drawing.Size(95, 17);
		this.rxBurstRadioButton.TabIndex = 4;
		this.rxBurstRadioButton.TabStop = true;
		this.rxBurstRadioButton.Text = "Rx Only (Burst)";
		this.rxBurstRadioButton.UseVisualStyleBackColor = true;
		this.rxBurstRadioButton.CheckedChanged += new System.EventHandler(rxBurstRadioButton_CheckedChanged);
		this.txContRadioButton.AutoSize = true;
		this.txContRadioButton.Location = new System.Drawing.Point(228, 20);
		this.txContRadioButton.Name = "txContRadioButton";
		this.txContRadioButton.Size = new System.Drawing.Size(123, 17);
		this.txContRadioButton.TabIndex = 3;
		this.txContRadioButton.TabStop = true;
		this.txContRadioButton.Text = "Tx Only (Continuous)";
		this.txContRadioButton.UseVisualStyleBackColor = true;
		this.txContRadioButton.CheckedChanged += new System.EventHandler(txContRadioButton_CheckedChanged);
		this.txBurstRadioButton.AutoSize = true;
		this.txBurstRadioButton.Location = new System.Drawing.Point(128, 19);
		this.txBurstRadioButton.Name = "txBurstRadioButton";
		this.txBurstRadioButton.Size = new System.Drawing.Size(94, 17);
		this.txBurstRadioButton.TabIndex = 2;
		this.txBurstRadioButton.TabStop = true;
		this.txBurstRadioButton.Text = "Tx Only (Burst)";
		this.txBurstRadioButton.UseVisualStyleBackColor = true;
		this.txBurstRadioButton.CheckedChanged += new System.EventHandler(txBurstRadioButton_CheckedChanged);
		this.masterRadioButton.AutoSize = true;
		this.masterRadioButton.Location = new System.Drawing.Point(65, 20);
		this.masterRadioButton.Name = "masterRadioButton";
		this.masterRadioButton.Size = new System.Drawing.Size(57, 17);
		this.masterRadioButton.TabIndex = 1;
		this.masterRadioButton.TabStop = true;
		this.masterRadioButton.Text = "Master";
		this.masterRadioButton.UseVisualStyleBackColor = true;
		this.masterRadioButton.CheckedChanged += new System.EventHandler(masterRadioButton_CheckedChanged);
		this.slaveRadioButton.AutoSize = true;
		this.slaveRadioButton.Location = new System.Drawing.Point(7, 20);
		this.slaveRadioButton.Name = "slaveRadioButton";
		this.slaveRadioButton.Size = new System.Drawing.Size(52, 17);
		this.slaveRadioButton.TabIndex = 0;
		this.slaveRadioButton.TabStop = true;
		this.slaveRadioButton.Text = "Slave";
		this.slaveRadioButton.UseVisualStyleBackColor = true;
		this.slaveRadioButton.CheckedChanged += new System.EventHandler(slaveRadioButton_CheckedChanged);
		this.periodicCheckBox.AutoSize = true;
		this.periodicCheckBox.Checked = true;
		this.periodicCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
		this.periodicCheckBox.Location = new System.Drawing.Point(380, 174);
		this.periodicCheckBox.Name = "periodicCheckBox";
		this.periodicCheckBox.Size = new System.Drawing.Size(123, 17);
		this.periodicCheckBox.TabIndex = 2;
		this.periodicCheckBox.Text = "Get stats periodically";
		this.periodicCheckBox.UseVisualStyleBackColor = true;
		this.periodicCheckBox.CheckedChanged += new System.EventHandler(periodicCheckBox_CheckedChanged);
		this.percentCheckBox.AutoSize = true;
		this.percentCheckBox.Checked = true;
		this.percentCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
		this.percentCheckBox.Location = new System.Drawing.Point(380, 197);
		this.percentCheckBox.Name = "percentCheckBox";
		this.percentCheckBox.Size = new System.Drawing.Size(110, 17);
		this.percentCheckBox.TabIndex = 3;
		this.percentCheckBox.Text = "Display stats in % ";
		this.percentCheckBox.UseVisualStyleBackColor = true;
		this.drwithCRCCheckBox.AutoSize = true;
		this.drwithCRCCheckBox.Location = new System.Drawing.Point(380, 220);
		this.drwithCRCCheckBox.Name = "drwithCRCCheckBox";
		this.drwithCRCCheckBox.Size = new System.Drawing.Size(141, 17);
		this.drwithCRCCheckBox.TabIndex = 4;
		this.drwithCRCCheckBox.Text = "Data rate with CRC error";
		this.drwithCRCCheckBox.UseVisualStyleBackColor = true;
		this.dataRateGroupBox.Controls.Add(this.dataRateTextBox);
		this.dataRateGroupBox.Location = new System.Drawing.Point(520, 168);
		this.dataRateGroupBox.Name = "dataRateGroupBox";
		this.dataRateGroupBox.Size = new System.Drawing.Size(130, 52);
		this.dataRateGroupBox.TabIndex = 5;
		this.dataRateGroupBox.TabStop = false;
		this.dataRateGroupBox.Text = "Data Rate (kbps)";
		this.dataRateTextBox.Location = new System.Drawing.Point(7, 20);
		this.dataRateTextBox.Name = "dataRateTextBox";
		this.dataRateTextBox.ReadOnly = true;
		this.dataRateTextBox.Size = new System.Drawing.Size(117, 20);
		this.dataRateTextBox.TabIndex = 0;
		this.optionButton.Location = new System.Drawing.Point(51, 270);
		this.optionButton.Name = "optionButton";
		this.optionButton.Size = new System.Drawing.Size(75, 23);
		this.optionButton.TabIndex = 6;
		this.optionButton.Text = "Option ...";
		this.optionButton.UseVisualStyleBackColor = true;
		this.optionButton.Click += new System.EventHandler(optionButton_Click);
		this.startButton.Location = new System.Drawing.Point(205, 270);
		this.startButton.Name = "startButton";
		this.startButton.Size = new System.Drawing.Size(75, 23);
		this.startButton.TabIndex = 7;
		this.startButton.Text = "Start";
		this.startButton.UseVisualStyleBackColor = true;
		this.startButton.Click += new System.EventHandler(startButton_Click);
		this.stopButton.Enabled = false;
		this.stopButton.Location = new System.Drawing.Point(370, 270);
		this.stopButton.Name = "stopButton";
		this.stopButton.Size = new System.Drawing.Size(75, 23);
		this.stopButton.TabIndex = 8;
		this.stopButton.Text = "Stop";
		this.stopButton.UseVisualStyleBackColor = true;
		this.stopButton.Click += new System.EventHandler(stopButton_Click);
		this.getStatsButton.Enabled = false;
		this.getStatsButton.Location = new System.Drawing.Point(527, 270);
		this.getStatsButton.Name = "getStatsButton";
		this.getStatsButton.Size = new System.Drawing.Size(75, 23);
		this.getStatsButton.TabIndex = 9;
		this.getStatsButton.Text = "Get Stats";
		this.getStatsButton.UseVisualStyleBackColor = true;
		this.getStatsButton.Click += new System.EventHandler(getStatsButton_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.getStatsButton);
		base.Controls.Add(this.stopButton);
		base.Controls.Add(this.startButton);
		base.Controls.Add(this.optionButton);
		base.Controls.Add(this.dataRateGroupBox);
		base.Controls.Add(this.drwithCRCCheckBox);
		base.Controls.Add(this.percentCheckBox);
		base.Controls.Add(this.periodicCheckBox);
		base.Controls.Add(this.testModeGroupBox);
		base.Controls.Add(this.infoListView);
		base.Name = "ProdTestUserControl";
		base.Size = new System.Drawing.Size(666, 318);
		base.Load += new System.EventHandler(ProdTestUserControl_Load);
		this.testModeGroupBox.ResumeLayout(false);
		this.testModeGroupBox.PerformLayout();
		this.dataRateGroupBox.ResumeLayout(false);
		this.dataRateGroupBox.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
