Пример #1
0
        protected virtual void BorIPProcessInput(BorIPClient borIPClient, StreamWriter sw, string str)
        {
            if (str.StartsWith("DEVICE -", StringComparison.CurrentCultureIgnoreCase) ||
                str.StartsWith("DEVICE 0", StringComparison.CurrentCultureIgnoreCase))
            {
                // <DEVICE NAME>
                //	|<MIN GAIN>
                //	|<MAX GAIN>
                //	|<GAIN STEP>
                //	|<FPGA FREQ IN HZ>
                //	|<COMPLEX SAMPLE PAIRS PER PACKET>
                //	|<CSV LIST OF VALID ANTENNAS>
                //	[|<DEVICE SERIAL NUMBER>]
                string s = $"DEVICE FX2" +
                           $"|{MIN_GAIN}.000" +
                           $"|{MAX_GAIN}.000" +
                           $"|1.000" +
                           $"|{MIN_RATE << MAX_RATE_MUL}.000" +
                           $"|{NUM_SAMPLES}" +
                           $"|RX" +
                           $"|00000000" +
                           $"|default" +
                           $"|default";
                BorIPWriteLine(sw, s);
            }
            else if (str.StartsWith("DEVICE !", StringComparison.CurrentCultureIgnoreCase))
            {
                BorIPWriteLine(sw, "DEVICE -");
            }
            else if (str.StartsWith("RATE", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    string s = str.Split(new char[] { ' ' }, 2)[1];
                    Rate = (int)Math.Round(double.Parse(s));
                    if (RunningState == ERunningState.Continued)
                    {
                        sequence     = 0;
                        RunningState = ERunningState.Start;
                    }
                    BorIPWriteLine(sw, $"RATE OK {Rate}.000");
                }
                else
                {
                    BorIPWriteLine(sw, $"RATE {Rate}.000");
                }
            }
            else if (str.StartsWith("FREQ", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    string s     = str.Split(new char[] { ' ' }, 2)[1];
                    int    value = (int)Math.Round(double.Parse(s));
                    Freq = value;
                    if (RunningState == ERunningState.Continued)
                    {
                        sequence     = 0;
                        RunningState = ERunningState.Start;
                    }

                    string ret = "OK";
                    if (value < MIN_FREQ)
                    {
                        ret = "LOW";
                    }
                    if (MAX_FREQ < value)
                    {
                        ret = "HIGH";
                    }

                    BorIPWriteLine(sw, $"FREQ {ret} {Freq}.000 {Freq}.000 0.000 0.000");
                }
                else
                {
                    BorIPWriteLine(sw, $"FREQ {Freq}.000");
                }
            }
            else if (str.StartsWith("GAIN", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    string s = str.Split(new char[] { ' ' }, 2)[1];
                    Gain = (int)Math.Round(double.Parse(s));
                    if (RunningState == ERunningState.Continued)
                    {
                        sequence     = 0;
                        RunningState = ERunningState.Start;
                    }
                    BorIPWriteLine(sw, "GAIN OK");
                }
                else
                {
                    BorIPWriteLine(sw, $"GAIN {Gain}.000");
                }
            }
            else if (str.StartsWith("ANTENNA", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    BorIPWriteLine(sw, "ANTENNA OK");
                }
                else
                {
                    BorIPWriteLine(sw, "ANTENNA RX");
                }
            }
            else if (str.StartsWith("CLOCK_SRC", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    BorIPWriteLine(sw, "CLOCK_SRC OK");
                }
                else
                {
                    BorIPWriteLine(sw, "CLOCK_SRC default");
                }
            }
            else if (str.StartsWith("TIME_SRC", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    BorIPWriteLine(sw, "TIME_SRC OK");
                }
                else
                {
                    BorIPWriteLine(sw, "TIME_SRC default");
                }
            }
            else if (str.StartsWith("DEST", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    string   s      = str.Split(new char[] { ' ' }, 2)[1];
                    string[] sArray = s.Split(new char[] { ':' }, 2);
                    try
                    {
                        borIPClient.DestAddr = sArray[0];
                        if (sArray.Length > 1)
                        {
                            borIPClient.DestPort = int.Parse(sArray[1]);
                        }
                        BorIPWriteLine(sw, $"DEST OK {borIPClient.DestAddr}:{borIPClient.DestPort}");
                    }
                    catch
                    {
                        BorIPWriteLine(sw, "DEST FAIL Failed to set destination");
                    }
                }
                else
                {
                    BorIPWriteLine(sw, $"DEST {borIPClient.DestAddr}:{borIPClient.DestPort}");
                }
            }
            else if (str.StartsWith("HEADER", StringComparison.CurrentCultureIgnoreCase))
            {
                if (str.Contains(" "))
                {
                    string s = str.Split(new char[] { ' ' }, 2)[1];
                    borIPClient.Header = (s == "ON");
                    BorIPWriteLine(sw, "HEADER OK");
                }
                else
                {
                    string header = borIPClient.Header ? "ON" : "OFF";
                    BorIPWriteLine(sw, $"HEADER {header}");
                }
            }
            else if (str.StartsWith("GO", StringComparison.CurrentCultureIgnoreCase))
            {
                sequence     = 0;
                RunningState = ERunningState.Start;
                BorIPWriteLine(sw, "GO OK");
            }
            else if (str.StartsWith("STOP", StringComparison.CurrentCultureIgnoreCase))
            {
                RunningState = ERunningState.Stop;
                BorIPWriteLine(sw, "STOP OK");
            }
            else
            {
                BorIPWriteLine(sw, $"{str} UNKNOWN");
            }
        }
Пример #2
0
        public BorIPDevice(CyUSBDevice usbDevice, MonoUsbProfile usbProfile, EDeviceType deviceType)
            : base(usbDevice, usbProfile, deviceType)
        {
            if (deviceType == EDeviceType.ADC)
            {
                byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 2);
                dataPortNo = (ushort)(response[0] + (response[1] << 8));
            }
            else
            {
                byte[] response = ReceiveVendorResponse((byte)EVendorRequests.DeviceParam, 4);
                dataPortNo    = (ushort)(response[0] + (response[1] << 8));
                ControlPortNo = (ushort)(response[2] + (response[3] << 8));
            }

            Console.WriteLine($"+ {this}");


            var ct = Cts.Token;

            Task.Run(() =>
            {
                TcpListener listener = BorIPCreateListener(BORIP_SERVERPORT);
                try
                {
                    listener.Start();
                    var addresses = Dns.GetHostAddresses(Dns.GetHostName())
                                    .Where(p => p.ToString().Contains('.'));
                    Console.WriteLine($"{BORIP_SERVERPORT}: {string.Join(" ", addresses)}");

                    CancellationTokenSource tcpCts = null;
                    while (!ct.IsCancellationRequested)
                    {
                        TcpClient client = listener.AcceptTcpClient();
                        Console.WriteLine($"{BORIP_SERVERPORT}: accepted");

                        if (tcpCts != null)
                        {
                            tcpCts.Cancel();
                        }

                        tcpCts    = new CancellationTokenSource();
                        var tcpCt = tcpCts.Token;
                        Task.Run(() =>
                        {
                            BorIPClient borIPClient = new BorIPClient(client);
                            borIPClients.Add(borIPClient);
                            try
                            {
                                using (NetworkStream ns = client.GetStream())
                                    using (StreamReader sr = new StreamReader(ns, Encoding.ASCII))
                                        using (StreamWriter sw = new StreamWriter(ns, Encoding.ASCII))
                                        {
                                            BorIPWriteLine(sw, "DEVICE -");

                                            while (!tcpCt.IsCancellationRequested)
                                            {
                                                string str = sr.ReadLine();
                                                if (string.IsNullOrWhiteSpace(str))
                                                {
                                                    return;                                     // keep alive
                                                }
                                                Console.WriteLine($"{BORIP_SERVERPORT}: [in] {str.Trim()}");

                                                BorIPProcessInput(borIPClient, sw, str);
                                            }
                                        }
                            }
                            catch (Exception)
                            {
                                // nothing to do
                            }
                            finally
                            {
                                borIPClients.Remove(borIPClient);
                                Console.WriteLine($"{BORIP_SERVERPORT}: closed");
                            }
                        }, tcpCt);
                    }
                }
                catch (SocketException ex) when(ex.ErrorCode == 10004)
                {
                    // nothing to do
                }
                catch (OperationCanceledException)
                {
                    // nothing to do
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{BORIP_SERVERPORT}: {ex.Message}");
                }
                finally
                {
                    listener.Stop();
                    //Console.WriteLine($"{BORIP_PORTNO}: listener stopped");
                }
            }, ct);


            if (USBDevice != null)
            {
                endpoint2 = USBDevice.EndPointOf(0x82) as CyBulkEndPoint;
            }

            Task.Run(() =>
            {
                while (!ct.IsCancellationRequested)
                {
                    if (borIPClients.Count == 0 && (ControlPortNo > 0 && controlClients.Count == 0))
                    {
                        Thread.Sleep(100);
                        continue;
                    }

                    UdpClient udp = new UdpClient();
                    try
                    {
                        int maxPacketSize;
                        if (USBDevice != null)
                        {
                            maxPacketSize = endpoint2.MaxPktSize;
                        }
                        else
                        {
                            maxPacketSize = MonoUsbApi.GetMaxPacketSize(USBProfile.ProfileHandle, 0x82);
                        }
                        byte[] inData    = new byte[maxPacketSize];
                        byte[] outData   = null;
                        int outDataPos   = 0;
                        byte[] borIPData = null;
                        int borIPDataPos = 0;

                        while (!ct.IsCancellationRequested &&
                               !(borIPClients.Count == 0 && (ControlPortNo > 0 && controlClients.Count == 0)))
                        {
                            int xferLen = inData.Length;
                            bool ret    = false;
                            if (USBDevice != null)
                            {
                                ret = endpoint2.XferData(ref inData, ref xferLen);
                            }
                            else
                            {
                                ret = MonoUsbApi.BulkTransfer(MonoDeviceHandle, 0x82, inData, inData.Length, out xferLen, TIMEOUT) == 0;
                            }
                            if (ret == false)
                            {
                                break;
                            }

                            int inDataPos = 0;
                            while (!ct.IsCancellationRequested && inDataPos < xferLen)
                            {
                                if (outData == null)
                                {
                                    outData = new byte[1472];
                                }
                                if (borIPData == null)
                                {
                                    borIPData = new byte[4 + 4 * NUM_SAMPLES];
                                    borIPData[borIPDataPos++] = (byte)((RunningState == ERunningState.Start) ? 0x10 : 0x00);
                                    RunningState = ERunningState.Continued;
                                    borIPData[borIPDataPos++] = 0;
                                    borIPData[borIPDataPos++] = (byte)(sequence & 0xff);
                                    borIPData[borIPDataPos++] = (byte)((sequence >> 8) & 0xff);
                                }

                                while (outDataPos < outData.Length && borIPDataPos < borIPData.Length && inDataPos < xferLen)
                                {
                                    byte b = inData[inDataPos++];
                                    outData[outDataPos++]     = b;
                                    borIPData[borIPDataPos++] = b;
                                }

                                if (borIPDataPos == borIPData.Length)
                                {
                                    List <string> remoteAddrList = new List <string>();
                                    foreach (var client in borIPClients.ToArray())
                                    {
                                        if (client.Header)
                                        {
                                            string remoteAddr;
                                            try
                                            {
                                                remoteAddr = client.DestAddr;
                                            }
                                            catch
                                            {
                                                continue;
                                            }

                                            if (remoteAddrList.Contains(remoteAddr) == false)
                                            {
                                                remoteAddrList.Add(remoteAddr);
                                                udp.Send(borIPData, borIPData.Length, remoteAddr, client.DestPort);
                                            }
                                        }
                                    }

                                    sequence++;
                                    if (sequence == 0x10000)
                                    {
                                        sequence = 0;
                                    }

                                    borIPData    = null;
                                    borIPDataPos = 0;
                                }

                                if (outDataPos == outData.Length)
                                {
                                    List <string> remoteAddrList = new List <string>();
                                    foreach (var client in borIPClients.ToArray())
                                    {
                                        if (client.Header == false)
                                        {
                                            string remoteAddr;
                                            try
                                            {
                                                remoteAddr = client.DestAddr;
                                            }
                                            catch
                                            {
                                                continue;
                                            }

                                            if (remoteAddrList.Contains(remoteAddr) == false)
                                            {
                                                remoteAddrList.Add(remoteAddr);
                                                udp.Send(outData, outData.Length, remoteAddr, client.DestPort);
                                            }
                                        }
                                    }

                                    remoteAddrList.Clear();
                                    foreach (var client in controlClients.ToArray())
                                    {
                                        string remoteAddr;
                                        try
                                        {
                                            remoteAddr = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
                                        }
                                        catch
                                        {
                                            continue;
                                        }

                                        if (remoteAddrList.Contains(remoteAddr) == false)
                                        {
                                            remoteAddrList.Add(remoteAddr);
                                            udp.Send(outData, outData.Length, remoteAddr, dataPortNo);
                                        }
                                    }

                                    if (ControlPortNo == 0)
                                    {
                                        udp.Send(outData, outData.Length, "127.0.0.1", dataPortNo);
                                    }

                                    outData    = null;
                                    outDataPos = 0;
                                }
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        // nothing to do
                    }
                    //catch (Exception ex)
                    //{
                    //	Console.WriteLine($"BorIP: {ex.Message}");
                    //}
                    finally
                    {
                        udp.Close();
                    }

                    Thread.Sleep(1000);
                }
            }, ct);
        }