using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml;
using System.Xml.Serialization;
using DebugToolInstrumentInterface;
using QC.QMSLPhone;
using QRCT_modem_RF_Debug_tool;

namespace QFDT_Components;

public partial class Wlan_WCN39XX : UserControl, IComponentConnector
{
	public delegate void SendDebugMessage(MessageLevel level, string message);

	public Phone phone;

	public ISignalGenerator pSG;

	public ISpectrumAnalyzer pSA;

	public PCF m_PCF;

	private Thread thread;

	private Thread thread_app;

	public bool m_bContinueFlag;

	private List<WCN39xx_TxRxScenario> WCN39xx_scenarioList;

	private WCN39xx_TxRxParams m_TxRxParams;

	public SendDebugMessage DebugMessageCB;

	public string m_PCFPath { get; set; }

	private void SetLanguageDictionary()
	{
		ResourceDictionary resourceDictionary = new ResourceDictionary();
		string text = Thread.CurrentThread.CurrentCulture.ToString();
		try
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "..\\Resources\\UI_" + text + ".xaml", UriKind.Absolute);
		}
		catch
		{
		}
		base.Resources.MergedDictionaries.Add(resourceDictionary);
	}

	public void SetLanguage(string culture)
	{
		ResourceDictionary resourceDictionary = new ResourceDictionary();
		try
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "\\Resources\\UI_" + culture + ".xaml", UriKind.Absolute);
		}
		catch
		{
			resourceDictionary.Source = new Uri(Thread.GetDomain().BaseDirectory + "..\\Resources\\UI_en-US.xaml", UriKind.Absolute);
		}
		base.Resources.MergedDictionaries.Clear();
		base.Resources.MergedDictionaries.Add(resourceDictionary);
	}

	public void Register_DebugMessageCB(SendDebugMessage me)
	{
		DebugMessageCB = me;
	}

	public Wlan_WCN39XX()
	{
		m_TxRxParams = new WCN39xx_TxRxParams();
		InitializeComponent();
		InitUI();
	}

	private void InitScenarioList()
	{
		WCN39xx_scenarioList = new List<WCN39xx_TxRxScenario>();
		WCN39xx_scenarioList.Add(new WCN39xx_TxRxScenario(ChainOriention.Tx));
		WCN39xx_scenarioList.Add(new WCN39xx_TxRxScenario(ChainOriention.Rx));
	}

	private bool SetSelectedScenario(WCN39xx_TxRxScenario scenarioItem)
	{
		bool result = false;
		foreach (WCN39xx_TxRxScenario item in (IEnumerable)cboTxRxScenarioSelect.Items)
		{
			if (item.CheckEqual(scenarioItem))
			{
				cboTxRxScenarioSelect.SelectedItem = item;
				result = true;
				break;
			}
		}
		return result;
	}

	private void updateComboBoxScenarioList()
	{
		bool flag = false;
		WCN39xx_TxRxScenario selectedScenario = null;
		if (cboTxRxScenarioSelect.SelectedItem != null)
		{
			flag = true;
			selectedScenario = (WCN39xx_TxRxScenario)cboTxRxScenarioSelect.SelectedItem;
		}
		cboTxRxScenarioSelect.Items.Clear();
		foreach (WCN39xx_TxRxScenario wCN39xx_scenario in WCN39xx_scenarioList)
		{
			cboTxRxScenarioSelect.Items.Add(wCN39xx_scenario);
		}
		if (!flag || !SetSelectedScenario(selectedScenario))
		{
			cboTxRxScenarioSelect.SelectedIndex = 0;
		}
	}

	private void InitUI()
	{
		WCN39XXStatic.RefreshComboBox(loadChain, cboTxRxChainSelect);
		WCN39XXStatic.RefreshComboBox(loadHTMode, cboTxHtMode);
		WCN39XXStatic.RefreshComboBox(loadPwrCtrlMode, cboPwrCtrlMode);
		WCN39XXStatic.RefreshComboBox(loadChannel1, cboTxRxChan);
		WCN39XXStatic.RefreshComboBox(loadTxRate, cboTxRate);
		DefaultTxScenario();
		InitScenarioList();
		updateComboBoxScenarioList();
		cbxControlSA_Tx.IsChecked = true;
		cbxRxCtrlEquipment.IsChecked = true;
		RefreshVisibilityOfTxResultComponents();
	}

	private void cboTxHtMode_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		WCN39XXStatic.RefreshComboBox(loadChannel1, cboTxRxChan);
		WCN39XXStatic.RefreshComboBox(loadTxRate, cboTxRate);
	}

	private void cboTxRxCh_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		WCN39XXStatic.RefreshComboBox(loadTxRate, cboTxRate);
	}

	private void WCN39xx_LoadPCFScenario()
	{
		if (m_PCF == null || m_PCF.WLANConfiguraion == null || m_PCF.WLANConfiguraion.WCN39xxConfig == null || m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray == null || m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray.Length <= 0)
		{
			return;
		}
		List<WCN39xx_TxRxScenario> list = new List<WCN39xx_TxRxScenario>();
		foreach (WCN39xx_TxRxScenario wCN39xx_scenario in WCN39xx_scenarioList)
		{
			list.Add(wCN39xx_scenario.DeepCopy());
		}
		WCN39xx_scenarioList = new List<WCN39xx_TxRxScenario>();
		for (int i = 0; i < m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray.Length; i++)
		{
			WCN39xx_TxRxScenario wCN39xx_TxRxScenario = new WCN39xx_TxRxScenario(ChainOriention.Tx);
			wCN39xx_TxRxScenario.txScenario = m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray[i].DeepCopy();
			WCN39xx_scenarioList.Add(wCN39xx_TxRxScenario);
		}
		foreach (WCN39xx_TxRxScenario item in list)
		{
			if (item.orientation == ChainOriention.Rx)
			{
				WCN39xx_scenarioList.Add(item.DeepCopy());
			}
		}
	}

	public void UpdateUIFromLoadedPCF()
	{
		if (m_PCF != null && m_PCF.WLANConfiguraion != null && m_PCF.WLANConfiguraion.WCN39xxConfig != null)
		{
			WCN39xx_LoadPCFScenario();
			updateComboBoxScenarioList();
		}
	}

	private void cboPwtCtrlMode_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		updatePwrCtrlTargetVisibility();
	}

	private void updatePwrCtrlTargetVisibility()
	{
		if (cboPwrCtrlMode.SelectedItem != null && (cboPwrCtrlMode.SelectedItem as obJectForComboBox).showContent == "ForcedDesiredGain")
		{
			spTxForcedGain.Visibility = Visibility.Visible;
			spTargetPwr.Visibility = Visibility.Collapsed;
		}
		else
		{
			spTxForcedGain.Visibility = Visibility.Collapsed;
			spTargetPwr.Visibility = Visibility.Visible;
		}
	}

	private void updateScenarioComponentsVisibility()
	{
		if (cboTxRxScenarioSelect.SelectedItem == null)
		{
			return;
		}
		WCN39xx_TxRxScenario wCN39xx_TxRxScenario = cboTxRxScenarioSelect.SelectedItem as WCN39xx_TxRxScenario;
		if (wCN39xx_TxRxScenario.orientation == ChainOriention.Rx)
		{
			sPanelPwrCtrl.Visibility = Visibility.Collapsed;
			sPanelTxScenario.Visibility = Visibility.Collapsed;
			return;
		}
		sPanelPwrCtrl.Visibility = Visibility.Visible;
		sPanelTxScenario.Visibility = Visibility.Visible;
		if (wCN39xx_TxRxScenario.txScenario != null && wCN39xx_TxRxScenario.txScenario.txPwrCtrl.show == "ForcedDesiredGain")
		{
			spTxForcedGain.Visibility = Visibility.Visible;
			spTargetPwr.Visibility = Visibility.Collapsed;
		}
		else
		{
			spTxForcedGain.Visibility = Visibility.Collapsed;
			spTargetPwr.Visibility = Visibility.Visible;
		}
	}

	private void updateOpPanelVisibility()
	{
		if (cboTxRxScenarioSelect.SelectedItem != null && (cboTxRxScenarioSelect.SelectedItem as WCN39xx_TxRxScenario).orientation == ChainOriention.Rx)
		{
			spTx.Visibility = Visibility.Collapsed;
			spRx.Visibility = Visibility.Visible;
		}
		else
		{
			spTx.Visibility = Visibility.Visible;
			spRx.Visibility = Visibility.Collapsed;
		}
	}

	private void cboTxRxScenarioSelect_selectionChanged(object sender, SelectionChangedEventArgs e)
	{
		updateScenarioComponentsVisibility();
		updateOpPanelVisibility();
		update_Detailed_Selected_Scenario_UI();
	}

	private void loadChain()
	{
		cboTxRxChainSelect.Items.Clear();
		WCN39XXStatic.AddItemToComboBox(cboTxRxChainSelect, WCN39XXStatic.TxChainList);
		cboTxRxChainSelect.SelectedIndex = 0;
	}

	private void loadHTMode()
	{
		cboTxHtMode.Items.Clear();
		WCN39XXStatic.AddItemToComboBox(cboTxHtMode, WCN39XXStatic.htModeList);
		cboTxHtMode.SelectedIndex = 0;
	}

	private void loadChannel1()
	{
		cboTxRxChan.Items.Clear();
		if (cboTxRxScenarioSelect.SelectedItem != null && (cboTxRxScenarioSelect.SelectedItem as WCN39xx_TxRxScenario).orientation == ChainOriention.Rx)
		{
			WCN39XXStatic.AddItemToComboBox(cboTxRxChan, WCN39XXStatic.channelNumbers);
			WCN39XXStatic.AddItemToComboBox(cboTxRxChan, WCN39XXStatic.channelNumbers11p);
		}
		else if (cboTxHtMode.SelectedItem != null)
		{
			obJectForComboBox obJectForComboBox2 = (obJectForComboBox)cboTxHtMode.SelectedItem;
			if (obJectForComboBox2.showContent.StartsWith("DSRC"))
			{
				WCN39XXStatic.AddItemToComboBox(cboTxRxChan, WCN39XXStatic.channelNumbers11p);
			}
			else
			{
				WCN39XXStatic.AddItemToComboBox(cboTxRxChan, WCN39XXStatic.channelNumbers);
			}
		}
		else
		{
			WCN39XXStatic.AddItemToComboBox(cboTxRxChan, WCN39XXStatic.channelNumbers);
		}
	}

	private void loadPwrCtrlMode()
	{
		cboPwrCtrlMode.Items.Clear();
		WCN39XXStatic.AddItemToComboBox(cboPwrCtrlMode, WCN39XXStatic.txPwrCtrlList);
	}

	private void loadTxRate()
	{
		cboTxRate.Items.Clear();
		obJectForComboBox obJectForComboBox2 = ((cboTxHtMode.SelectedItem != null) ? ((obJectForComboBox)cboTxHtMode.SelectedItem) : null);
		if (cboTxRxChan.SelectedItem != null)
		{
			_ = (obJectForComboBox)cboTxRxChan.SelectedItem;
		}
		if (obJectForComboBox2.showContent == "No HT")
		{
			LoadTxRateOFDMLegacy();
		}
		else if (obJectForComboBox2.showContent == "CCK")
		{
			LoadTxRateCCK();
		}
		else if (obJectForComboBox2.showContent == "DSRC5")
		{
			LoadTxRateDSRC5();
		}
	}

	private void LoadTxRateCCK_5m10m()
	{
		WCN39XXStatic.AddItemToComboBox(cboTxRate, WCN39XXStatic.TxRateCCK_5m10m);
	}

	private void LoadTxRateCCK()
	{
		WCN39XXStatic.AddItemToComboBox(cboTxRate, WCN39XXStatic.TxRateCCK);
		LoadTxRateCCK_5m10m();
	}

	private void LoadTxRateNoHT_5m10m()
	{
		WCN39XXStatic.AddItemToComboBox(cboTxRate, WCN39XXStatic.TxRateNoHT_5m10m);
	}

	private void LoadTxRateOFDMLegacy()
	{
		WCN39XXStatic.AddItemToComboBox(cboTxRate, WCN39XXStatic.TxRateOFDMLegacy);
		LoadTxRateNoHT_5m10m();
	}

	private void LoadTxRateDSRC5()
	{
		WCN39XXStatic.AddItemToComboBox(cboTxRate, WCN39XXStatic.TxRateDSRC5);
	}

	private void DefaultTxScenario()
	{
		WCN39XXStatic.SetSelectedItem(cboTxHtMode, new obJectForComboBox("CCK", "4"));
		WCN39XXStatic.SetSelectedItem(cboPwrCtrlMode, new obJectForComboBox("TxPowerForce_CLPC", "0"));
		WCN39XXStatic.SetSelectedItem(cboTxRxChan, new obJectForComboBox("1(2412)", "2412"));
		WCN39XXStatic.SetSelectedItem(cboTxRate, new obJectForComboBox("RATE_1Mbps_L", "0"));
		txtTxPower.Text = "20";
		txtDacGain.Text = "0";
		txtGainIndex.Text = "9";
		txtPaCfg.Text = "0";
		txtSGPower.Text = "-60";
	}

	private void update_Detailed_Selected_Scenario_UI()
	{
		if (cboTxRxScenarioSelect.SelectedItem == null)
		{
			return;
		}
		WCN39xx_TxRxScenario wCN39xx_TxRxScenario = (WCN39xx_TxRxScenario)cboTxRxScenarioSelect.SelectedItem;
		if (wCN39xx_TxRxScenario.orientation == ChainOriention.Tx)
		{
			if (wCN39xx_TxRxScenario.txScenario != null)
			{
				WCN39XXStatic.SetSelectedItem(cboTxHtMode, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxRxScenario.txScenario.htMode));
				WCN39XXStatic.SetSelectedItem(cboTxRxChan, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxRxScenario.txScenario.chan));
				WCN39XXStatic.SetSelectedItem(cboTxRate, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxRxScenario.txScenario.txRate));
				WCN39XXStatic.SetSelectedItem(cboPwrCtrlMode, WCN39XXStatic.getCbxTypeFromScenarioType(wCN39xx_TxRxScenario.txScenario.txPwrCtrl));
				if (wCN39xx_TxRxScenario.txScenario.txPwrCtrl.show == "ForcedDesiredGain")
				{
					txtDacGain.Text = wCN39xx_TxRxScenario.txScenario.dacGain;
					txtGainIndex.Text = wCN39xx_TxRxScenario.txScenario.gainIndex;
					txtPaCfg.Text = wCN39xx_TxRxScenario.txScenario.paCfg;
				}
				else
				{
					txtTxPower.Text = wCN39xx_TxRxScenario.txScenario.targetPower;
				}
			}
			else
			{
				DefaultTxScenario();
			}
		}
		else
		{
			WCN39XXStatic.RefreshComboBox(loadChannel1, cboTxRxChan);
		}
	}

	private WCN39xx_TxScenario getTxScenarioFromUI()
	{
		WCN39xx_TxScenario wCN39xx_TxScenario = new WCN39xx_TxScenario();
		wCN39xx_TxScenario.chan = WCN39XXStatic.parseTxScenarioParamsFromUI(cboTxRxChan);
		wCN39xx_TxScenario.htMode = WCN39XXStatic.parseTxScenarioParamsFromUI(cboTxHtMode);
		wCN39xx_TxScenario.txRate = WCN39XXStatic.parseTxScenarioParamsFromUI(cboTxRate);
		wCN39xx_TxScenario.txPwrCtrl = WCN39XXStatic.parseTxScenarioParamsFromUI(cboPwrCtrlMode);
		wCN39xx_TxScenario.dacGain = txtDacGain.Text;
		wCN39xx_TxScenario.gainIndex = txtGainIndex.Text;
		wCN39xx_TxScenario.paCfg = txtPaCfg.Text;
		wCN39xx_TxScenario.targetPower = txtTxPower.Text;
		return wCN39xx_TxScenario;
	}

	private void btAddCurTxScenarioToList_Click(object sender, RoutedEventArgs e)
	{
		AddCurTxScenarioToList();
	}

	private void AddCurTxScenarioToList()
	{
		WCN39xx_TxScenario txScenarioFromUI = getTxScenarioFromUI();
		if (m_PCF == null || m_PCF.WLANConfiguraion == null || m_PCF.WLANConfiguraion.WCN39xxConfig == null)
		{
			return;
		}
		List<WCN39xx_TxScenario> list = new List<WCN39xx_TxScenario>();
		if (m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray != null)
		{
			for (int i = 0; i < m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray.Length; i++)
			{
				list.Add(m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray[i]);
			}
		}
		if (WlanConst.CheckExist(list, txScenarioFromUI))
		{
			return;
		}
		list.Add(txScenarioFromUI);
		m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray = new WCN39xx_TxScenario[list.Count];
		for (int j = 0; j < list.Count; j++)
		{
			m_PCF.WLANConfiguraion.WCN39xxConfig.ScenarioArray[j] = list[j].DeepCopy();
		}
		WCN39xx_LoadPCFScenario();
		updateComboBoxScenarioList();
		foreach (WCN39xx_TxRxScenario wCN39xx_scenario in WCN39xx_scenarioList)
		{
			if (wCN39xx_scenario.txScenario != null && wCN39xx_scenario.txScenario.IsEqual(txScenarioFromUI))
			{
				SetSelectedScenario(wCN39xx_scenario);
				break;
			}
		}
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(PCF));
		XmlWriter xmlWriter = XmlWriter.Create(m_PCFPath, new XmlWriterSettings
		{
			Indent = true
		});
		xmlSerializer.Serialize(xmlWriter, m_PCF);
		xmlWriter.Close();
	}

	public void WCN39xx_Unloaded(object sender, RoutedEventArgs e)
	{
		m_bContinueFlag = false;
	}

	private void btLaunchWlanApp_Click(object sender, RoutedEventArgs e)
	{
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btLaunchWlanApp, SetAttribute.IsEnable, bEnableOrVisbile: false);
		thread_app = new Thread(runStartWlanApp);
		thread_app.Name = "Start ptt_socket_app";
		thread_app.Start();
	}

	private void runStartWlanApp()
	{
		if (phone != null)
		{
			try
			{
				WlanConst.SendDebugMessage funcCB = DebugMessageCB.Invoke;
				WlanConst.startWlanApp_Diag(phone, funcCB);
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "Unkonwn Exception " + ex2.Message);
			}
		}
		else
		{
			DebugMessageCB(MessageLevel.EXCEPTION, "Invalid phone handle");
		}
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btLaunchWlanApp, SetAttribute.IsEnable, bEnableOrVisbile: true);
	}

	private void btRxStop_Click(object sender, RoutedEventArgs e)
	{
		m_bContinueFlag = false;
	}

	private void btRxStart_Click(object sender, RoutedEventArgs e)
	{
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, cboTxRxChan, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStart, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStart, SetAttribute.Visibility, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStop, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStop, SetAttribute.Visibility, bEnableOrVisbile: true);
		m_TxRxParams = GetTxParamsFromUI();
		m_bContinueFlag = true;
		thread = new Thread(runRxStart);
		thread.Name = "Rx Get RSSI";
		thread.Start();
	}

	private void btTxStop_Click(object sender, RoutedEventArgs e)
	{
		m_bContinueFlag = false;
	}

	private void btTxStart_Click(object sender, RoutedEventArgs e)
	{
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, cboTxRxChan, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStartTx, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStartTx, SetAttribute.Visibility, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStopTx, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStopTx, SetAttribute.Visibility, bEnableOrVisbile: true);
		m_TxRxParams = GetTxParamsFromUI();
		m_bContinueFlag = true;
		thread = new Thread(runTxStart);
		thread.Name = "Tx Start";
		thread.Start();
	}

	private WCN39xx_TxRxParams GetTxParamsFromUI()
	{
		WCN39xx_TxRxParams wCN39xx_TxRxParams = new WCN39xx_TxRxParams();
		wCN39xx_TxRxParams.defaultValue();
		wCN39xx_TxRxParams.htMode = (AccessComponentInThreadHelper.GetSelectorSelectedItem(this, cboTxHtMode, SetAttribute.SelectedItem) as obJectForComboBox).realValue;
		wCN39xx_TxRxParams.txCH1 = (AccessComponentInThreadHelper.GetSelectorSelectedItem(this, cboTxRxChan, SetAttribute.SelectedItem) as obJectForComboBox).realValue;
		wCN39xx_TxRxParams.dataRate = (AccessComponentInThreadHelper.GetSelectorSelectedItem(this, cboTxRate, SetAttribute.SelectedItem) as obJectForComboBox).realValue;
		wCN39xx_TxRxParams.tpcm = (AccessComponentInThreadHelper.GetSelectorSelectedItem(this, cboPwrCtrlMode, SetAttribute.SelectedItem) as obJectForComboBox).realValue;
		wCN39xx_TxRxParams.gainIdx = AccessComponentInThreadHelper.GetText(this, txtGainIndex, SetAttribute.Text);
		wCN39xx_TxRxParams.dacGain = AccessComponentInThreadHelper.GetText(this, txtDacGain, SetAttribute.Text);
		wCN39xx_TxRxParams.paConfig = AccessComponentInThreadHelper.GetText(this, txtPaCfg, SetAttribute.Text);
		wCN39xx_TxRxParams.txPower0 = AccessComponentInThreadHelper.GetText(this, txtTxPower, SetAttribute.Text);
		wCN39xx_TxRxParams.curOrient = (AccessComponentInThreadHelper.GetSelectorSelectedItem(this, cboTxRxScenarioSelect, SetAttribute.SelectedItem) as WCN39xx_TxRxScenario).orientation;
		wCN39xx_TxRxParams.chain = (AccessComponentInThreadHelper.GetSelectorSelectedItem(this, cboTxRxChainSelect, SetAttribute.SelectedItem) as obJectForComboBox).realValue;
		wCN39xx_TxRxParams.dlSigGenPwr = AccessComponentInThreadHelper.GetText(this, txtSGPower, SetAttribute.Text);
		return wCN39xx_TxRxParams;
	}

	private void LoadDUT()
	{
		string s = "qc6180";
		string value = "0x0040";
		string s2 = "  ";
		int nVMemOption = 0;
		phone.FTM_WLAN_Atheros_LoadDUT(Encoding.ASCII.GetBytes(s), Encoding.ASCII.GetBytes(s2), nVMemOption, Convert.ToUInt32(value, 16));
	}

	private void runRxStart()
	{
		if (phone != null)
		{
			try
			{
				AccessComponentInThreadHelper.SetText(this, txtRssi0, SetAttribute.Text, "");
				LoadDUT();
				SetPhyRFMode();
				bool bContinueFlag = m_bContinueFlag;
				bool flag = m_TxRxParams.curOrient == ChainOriention.Rx;
				bContinueFlag = bContinueFlag && flag;
				bool flag2 = true;
				while (bContinueFlag)
				{
					if (AccessComponentInThreadHelper.getIsChecked(this, cbxRxCtrlEquipment, SetAttribute.IsChecked) && flag2)
					{
						double num = 1.0;
						double? num2 = null;
						double? num3 = null;
						if (double.TryParse(m_TxRxParams.txCH1, out var result))
						{
							num2 = result;
						}
						if (double.TryParse(m_TxRxParams.dlSigGenPwr, out result))
						{
							num3 = result;
						}
						if (!num2.HasValue)
						{
							throw new Exception("Can't get the correct frequency to set up SA/SG");
						}
						if (!num3.HasValue)
						{
							throw new Exception("Incorrect Signal Generator Power Level! ");
						}
						WlanConst.SendDebugMessage funcCB = DebugMessageCB.Invoke;
						WlanConst.ConfigSigGen(pSG, num2.Value + num, num3.Value, funcCB);
					}
					ConfigRx();
					string rxRSSI = GetRxRSSI();
					AccessComponentInThreadHelper.SetText(this, txtRssi0, SetAttribute.Text, rxRSSI);
					WCN39xx_TxRxParams txParamsFromUI = GetTxParamsFromUI();
					if (txParamsFromUI.IsEqual(m_TxRxParams))
					{
						flag2 = false;
					}
					else
					{
						flag2 = true;
						m_TxRxParams = txParamsFromUI;
					}
					bContinueFlag = m_bContinueFlag && m_TxRxParams.curOrient == ChainOriention.Rx;
				}
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex2.Message);
			}
		}
		else
		{
			DebugMessageCB(MessageLevel.EXCEPTION, "Invalid phone handle.");
		}
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, cboTxRxChan, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStart, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStart, SetAttribute.Visibility, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStop, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btRxStop, SetAttribute.Visibility, bEnableOrVisbile: false);
	}

	private void runTxStart()
	{
		if (phone != null)
		{
			try
			{
				AccessComponentInThreadHelper.SetText(this, txtTxPwr, SetAttribute.Text, "");
				LoadDUT();
				SetPhyRFMode();
				bool bContinueFlag = m_bContinueFlag;
				bool flag = m_TxRxParams.curOrient == ChainOriention.Tx;
				bContinueFlag = bContinueFlag && flag;
				bool flag2 = true;
				while (bContinueFlag)
				{
					if (flag2)
					{
						ConfigTx();
					}
					if (AccessComponentInThreadHelper.getIsChecked(this, cbxControlSA_Tx, SetAttribute.IsChecked))
					{
						double? num = null;
						if (double.TryParse(m_TxRxParams.txCH1, out var result))
						{
							num = result;
						}
						if (!num.HasValue)
						{
							throw new Exception("Can't get the correct frequency to set up SA/SG");
						}
						if (pSA == null)
						{
							throw new Exception("SA/SG handle is null!");
						}
						pSA.SetupMeasurement(Technology.NONE);
						pSA.SetSpan(10.0);
						pSA.SetCenterFrequency(num.Value);
						double referenceLevel = 25.0;
						if (double.TryParse(m_TxRxParams.txPower0, out result))
						{
							referenceLevel = result + 5.0;
						}
						pSA.SetReferenceLevel(referenceLevel);
						AccessComponentInThreadHelper.SetText(this, txtTxPwr, SetAttribute.Text, pSA.GetPower());
					}
					Thread.Sleep(400);
					WCN39xx_TxRxParams txParamsFromUI = GetTxParamsFromUI();
					if (txParamsFromUI.IsEqual(m_TxRxParams))
					{
						flag2 = false;
					}
					else
					{
						flag2 = true;
						m_TxRxParams = txParamsFromUI;
					}
					bContinueFlag = m_bContinueFlag && m_TxRxParams.curOrient == ChainOriention.Tx;
				}
				StopTx();
			}
			catch (PhoneException ex)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex.Message);
			}
			catch (Exception ex2)
			{
				DebugMessageCB(MessageLevel.EXCEPTION, "PhoneException " + ex2.Message);
			}
		}
		else
		{
			DebugMessageCB(MessageLevel.EXCEPTION, "Invalid phone handle.");
		}
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, cboTxRxChan, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStartTx, SetAttribute.IsEnable, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStartTx, SetAttribute.Visibility, bEnableOrVisbile: true);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStopTx, SetAttribute.IsEnable, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, btStopTx, SetAttribute.Visibility, bEnableOrVisbile: false);
	}

	private void SetPhyRFMode()
	{
		if (phone != null)
		{
			phone.FTM_WLAN_TLV2_Create(169);
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("phyRFMode"), Encoding.Default.GetBytes(m_TxRxParams.phyRFMode));
			phone.FTM_WLAN_TLV2_Complete();
			return;
		}
		throw new PhoneException("Inconrrect phonehandle!");
	}

	private void SetMacAndBssid()
	{
		byte[] array = new byte[7] { 1, 2, 3, 204, 255, 238, 0 };
		byte[] array2 = new byte[7] { 17, 18, 19, 204, 255, 238, 0 };
		byte[] array3 = new byte[7] { 33, 34, 35, 204, 255, 238, 0 };
		phone.FTM_WLAN_TLV2_AddMAC(Encoding.Default.GetBytes("txStation"), array[0], array[1], array[2], array[3], array[4], array[5]);
		phone.FTM_WLAN_TLV2_AddMAC(Encoding.Default.GetBytes("rxStation"), array2[0], array2[1], array2[2], array2[3], array2[4], array2[5]);
		phone.FTM_WLAN_TLV2_AddMAC(Encoding.Default.GetBytes("bssid"), array3[0], array3[1], array3[2], array3[3], array3[4], array3[5]);
	}

	private string GetRxRSSI()
	{
		string text = null;
		if (phone != null)
		{
			phone.FTM_WLAN_TLV2_Create(209);
			int num = Convert.ToInt32(m_TxRxParams.chain);
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("chain"), Encoding.Default.GetBytes(0.ToString()));
			phone.FTM_WLAN_TLV2_Complete();
			byte[] array = new byte[20];
			phone.FTM_WLAN_TLV2_GetRspParam(Encoding.Default.GetBytes("rssi_cw"), array);
			return Encoding.Default.GetString(GeneralHandle.TrimWithFirstNonZero(array));
		}
		throw new PhoneException("Invalid phone handle!");
	}

	private void ConfigRx()
	{
		if (phone != null)
		{
			uint num = 48u;
			phone.FTM_WLAN_TLV2_Create(2);
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("wlanMode"), Encoding.Default.GetBytes("0"));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("bandwidth"), Encoding.Default.GetBytes("0"));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("rxMode"), Encoding.Default.GetBytes("0"));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("channel"), Encoding.Default.GetBytes(m_TxRxParams.txCH1));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("flags"), Encoding.Default.GetBytes(num.ToString()));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("rateMask"), Encoding.Default.GetBytes("-1"));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("rxChain"), Encoding.Default.GetBytes(m_TxRxParams.chain));
			phone.FTM_WLAN_TLV2_Complete();
			return;
		}
		throw new PhoneException("Invalid phone handle!");
	}

	private void ConfigTx()
	{
		if (phone != null)
		{
			phone.FTM_WLAN_TLV2_Create(1);
			SetMacAndBssid();
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("phyId"), Encoding.Default.GetBytes(m_TxRxParams.phyId));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("txMode"), Encoding.Default.GetBytes(m_TxRxParams.txMode));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("channel"), Encoding.Default.GetBytes(m_TxRxParams.txCH1));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("tpcm"), Encoding.Default.GetBytes(m_TxRxParams.tpcm));
			if (m_TxRxParams.tpcm == WCN39XXStatic.getRealValueAcFromShowContent(WCN39XXStatic.txPwrCtrlList, "TxPowerForce_CLPC"))
			{
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("txPower0"), Encoding.Default.GetBytes(m_TxRxParams.txPower0));
			}
			else
			{
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("gainIdx"), Encoding.Default.GetBytes(m_TxRxParams.gainIdx));
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("dacGain"), Encoding.Default.GetBytes(m_TxRxParams.dacGain));
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("paConfig"), Encoding.Default.GetBytes(m_TxRxParams.paConfig));
			}
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("antenna"), Encoding.Default.GetBytes(m_TxRxParams.antenna));
			int num = Convert.ToInt32(m_TxRxParams.dataRate);
			if (num > 1200)
			{
				num -= 1200;
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("bandwidth"), Encoding.Default.GetBytes("0"));
			}
			else if (num >= 1100)
			{
				num -= 1100;
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("bandwidth"), Encoding.Default.GetBytes("50"));
			}
			else if (num >= 1000)
			{
				num -= 1000;
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("bandwidth"), Encoding.Default.GetBytes("51"));
			}
			else
			{
				phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("bandwidth"), Encoding.Default.GetBytes("0"));
			}
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("rateBitIndex0"), Encoding.Default.GetBytes(num.ToString()));
			string text = m_TxRxParams.htMode;
			switch (text)
			{
			case "12":
			case "13":
			case "14":
				text = "0";
				break;
			}
			if (text == "15")
			{
				text = "12";
			}
			else if (text == "16")
			{
				text = "13";
			}
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("wlanMode"), Encoding.Default.GetBytes(text));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("txChain0"), Encoding.Default.GetBytes(m_TxRxParams.chain));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("pktLen0"), Encoding.Default.GetBytes(m_TxRxParams.txPacketSize));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("shortGuard"), Encoding.Default.GetBytes(m_TxRxParams.shortGuard));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("dutyCycle"), Encoding.Default.GetBytes(m_TxRxParams.dutyCycle));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("numPackets"), Encoding.Default.GetBytes(m_TxRxParams.numPackets));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("txPattern"), Encoding.Default.GetBytes(m_TxRxParams.txPattern));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("scramblerOff"), Encoding.Default.GetBytes(m_TxRxParams.scrambler));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("agg"), Encoding.Default.GetBytes(m_TxRxParams.agg));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("aifsn"), Encoding.Default.GetBytes(m_TxRxParams.aifsn));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("broadcast"), Encoding.Default.GetBytes(m_TxRxParams.broadcast));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("flags"), Encoding.Default.GetBytes(m_TxRxParams.flags));
			phone.FTM_WLAN_TLV2_Complete();
			return;
		}
		throw new PhoneException("Inconrrect phonehandle!");
	}

	private void StopTx()
	{
		if (phone != null)
		{
			phone.FTM_WLAN_TLV2_Create(10);
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("phyId"), Encoding.Default.GetBytes(m_TxRxParams.phyId));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("stopTx"), Encoding.Default.GetBytes("1"));
			phone.FTM_WLAN_TLV2_AddParam(Encoding.Default.GetBytes("needReport"), Encoding.Default.GetBytes("0"));
			phone.FTM_WLAN_TLV2_Complete();
			return;
		}
		throw new PhoneException("Inconrrect phonehandle!");
	}

	private void cbCtrlSA_TxPwr_Checked(object sender, RoutedEventArgs e)
	{
		RefreshVisibilityOfTxResultComponents();
	}

	private void RefreshVisibilityOfTxResultComponents()
	{
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, lblTxPwr, SetAttribute.Visibility, bEnableOrVisbile: false);
		AccessComponentInThreadHelper.SetEnableOrVisibility(this, txtTxPwr, SetAttribute.Visibility, bEnableOrVisbile: false);
		if (AccessComponentInThreadHelper.getIsChecked(this, cbxControlSA_Tx, SetAttribute.IsChecked))
		{
			AccessComponentInThreadHelper.SetEnableOrVisibility(this, lblTxPwr, SetAttribute.Visibility, bEnableOrVisbile: true);
			AccessComponentInThreadHelper.SetEnableOrVisibility(this, txtTxPwr, SetAttribute.Visibility, bEnableOrVisbile: true);
		}
	}
}
