Example #1
0
        public async Task <DVBTResponse> SetPIDs(List <long> PIDs)
        {
            _log.Debug($"Setting PIDs: {String.Join(",", PIDs)}");

            var responseSize = 10;

            var req      = new DVBTRequest(DVBTDriverRequestTypeEnum.REQ_SET_PIDS, PIDs, responseSize);
            var response = await SendRequest(req, 5);

            if (response.Bytes.Count < responseSize)
            {
                throw new Exception($"Bad response, expected {responseSize} bytes, received {response.Bytes.Count  }");
            }

            var requestNumber        = response.Bytes[0];
            var longsCountInResponse = response.Bytes[1];
            var successFlag          = DVBTStatus.GetBigEndianLongFromByteArray(response.Bytes.ToArray(), 2);

            _log.Debug($"Set PIDS response: {successFlag}");

            return(new DVBTResponse()
            {
                SuccessFlag = successFlag == 1,
                RequestTime = response.RequestTime,
                ResponseTime = response.ResponseTime
            });
        }
Example #2
0
        public async Task <DVBTCapabilities> GetCapabalities()
        {
            _log.Debug($"Getting capabilities");

            var cap = new DVBTCapabilities();

            var responseSize = 2 + 7 * 8;

            var req      = new DVBTRequest(DVBTDriverRequestTypeEnum.REQ_GET_CAPABILITIES, new List <long>(), responseSize);
            var response = await SendRequest(req, 5);

            if (response.Bytes.Count < responseSize)
            {
                throw new Exception($"Bad response, expected {responseSize} bytes, received {response.Bytes.Count  }");
            }

            var requestNumber        = response.Bytes[0];
            var longsCountInResponse = response.Bytes[1];
            var successFlag          = DVBTStatus.GetBigEndianLongFromByteArray(response.Bytes.ToArray(), 2);

            cap.ParseFromByteArray(response.Bytes.ToArray(), 2);

            cap.RequestTime  = response.RequestTime;
            cap.ResponseTime = response.ResponseTime;

            _log.Debug($"Capabilities response: {cap.ToString()}");

            return(cap);
        }
Example #3
0
        public async Task <DVBTVersion> GetVersion()
        {
            _log.Debug($"Getting version");

            var version = new DVBTVersion();

            var responseSize = 26;  // 1 + 1 + 8 + 8 + 8

            var req      = new DVBTRequest(DVBTDriverRequestTypeEnum.REQ_PROTOCOL_VERSION, new List <long>(), responseSize);
            var response = await SendRequest(req, 5);

            if (response.Bytes.Count < responseSize)
            {
                throw new Exception($"Bad response, expected {responseSize} bytes, received {response.Bytes.Count  }");
            }

            var requestNumber        = response.Bytes[0];
            var longsCountInResponse = response.Bytes[1];

            var ar = response.Bytes.ToArray();

            version.SuccessFlag       = DVBTStatus.GetBigEndianLongFromByteArray(ar, 2) == 1;
            version.Version           = DVBTStatus.GetBigEndianLongFromByteArray(ar, 10);
            version.AllRequestsLength = DVBTStatus.GetBigEndianLongFromByteArray(ar, 18);

            version.RequestTime  = response.RequestTime;
            version.ResponseTime = response.ResponseTime;

            _log.Debug($"Version response: {version.ToString()}");

            return(version);
        }
Example #4
0
        public async Task <DVBTResponse> Tune(long frequency, long bandwidth, int deliverySystem)
        {
            _log.Debug($"Tuning {frequency} Mhz, type: {deliverySystem}");

            if (frequency == _lastTunedFreq && deliverySystem == _lastTunedDeliverySystem)
            {
                _log.Debug($"Frequency already tuned");

                return(new DVBTResponse()
                {
                    SuccessFlag = true,
                    RequestTime = DateTime.Now,
                    ResponseTime = DateTime.Now
                });
            }

            // 26 bytes

            //List<byte> bytesToSend = new List<byte>();

            //bytesToSend.Add(2); // REQ_TUNE
            //bytesToSend.Add(3); // Payload for 3 longs

            //bytesToSend.AddRange(DVBTStatus.GetByteArrayFromBigEndianLong(frequency)); // Payload[0] => frequency
            //bytesToSend.AddRange(DVBTStatus.GetByteArrayFromBigEndianLong(bandwidth)); // Payload[1] => bandWidth
            //bytesToSend.AddRange(DVBTStatus.GetByteArrayFromBigEndianLong(deliverySyetem));         // Payload[2] => DeliverySystem DVBT

            var payload = new List <long>()
            {
                frequency, bandwidth, Convert.ToInt64(deliverySystem)
            };

            var responseSize = 10;

            var req      = new DVBTRequest(DVBTDriverRequestTypeEnum.REQ_TUNE, payload, responseSize);
            var response = await SendRequest(req, 10);

            if (response.Bytes.Count < responseSize)
            {
                throw new Exception($"Bad response, expected {responseSize} bytes, received {response.Bytes.Count  }");
            }

            var successFlag = DVBTStatus.GetBigEndianLongFromByteArray(response.Bytes.ToArray(), 2);

            if (successFlag == 1)
            {
                _lastTunedFreq           = frequency;
                _lastTunedDeliverySystem = deliverySystem;
            }

            _log.Debug($"Tune response: {successFlag}");

            return(new DVBTResponse()
            {
                SuccessFlag = successFlag == 1,
                RequestTime = response.RequestTime,
                ResponseTime = response.ResponseTime
            });
        }
Example #5
0
        public void Send(NetworkStream stream)
        {
            stream.Write(new byte[] { (byte)DVBTDriverRequestType }, 0, 1);
            stream.Flush();
            stream.Write(new byte[] { (byte)Payload.Count }, 0, 1);
            stream.Flush();

            foreach (var payload in Payload)
            {
                stream.Write(DVBTStatus.GetByteArrayFromBigEndianLong(payload), 0, 8);
                stream.Flush();
            }
        }
Example #6
0
        private async Task <DVBTResponse> SendCloseConnection()
        {
            _log.Debug($"Closing connection");

            var responseSize = 10;

            var req      = new DVBTRequest(DVBTDriverRequestTypeEnum.REQ_EXIT, new List <long>(), responseSize);
            var response = await SendRequest(req, 5);

            var requestNumber        = response.Bytes[0];
            var longsCountInResponse = response.Bytes[1];
            var successFlag          = DVBTStatus.GetBigEndianLongFromByteArray(response.Bytes.ToArray(), 2);

            _log.Debug($"Close connection response: {successFlag}");

            return(new DVBTResponse()
            {
                SuccessFlag = successFlag == 1,
                RequestTime = response.RequestTime,
                ResponseTime = response.ResponseTime
            });
        }
Example #7
0
        public async Task <DVBTStatus> GetStatus()
        {
            _log.Debug("Getting status");

            var status = new DVBTStatus();

            /*
             *      (long) snr, // parameter 1
             *      (long) bitErrorRate, // parameter 2
             *      (long) droppedUsbFps, // parameter 3
             *      (long) rfStrengthPercentage, // parameter 4
             *      hasSignal ? 1L : 0L, // parameter 5
             *      hasCarrier ? 1L : 0L, // parameter 6
             *      hasSync ? 1L : 0L, // parameter 7
             *      hasLock ? 1L : 0L // parameter 8
             */

            var responseSize = 2 + 9 * 8;

            var req = new DVBTRequest(DVBTDriverRequestTypeEnum.REQ_GET_STATUS, new List <long>(), responseSize);

            var response = await SendRequest(req);

            if (response.Bytes.Count < responseSize)
            {
                throw new Exception($"Bad response, expected {responseSize} bytes, received {response.Bytes.Count  }");
            }

            var requestNumber        = response.Bytes[0];
            var longsCountInResponse = response.Bytes[1];

            status.ParseFromByteArray(response.Bytes.ToArray(), 2);
            status.RequestTime  = response.RequestTime;
            status.ResponseTime = response.ResponseTime;

            _log.Debug($"Status response: {status.ToString()}");

            return(status);
        }
Example #8
0
        /// <summary>
        /// Tuning with timeout
        /// </summary>
        /// <param name="frequency"></param>
        /// <param name="bandWidth"></param>
        /// <param name="deliverySystem"></param>
        /// <returns>Signal strength
        /// 0 .. no signal</returns>
        public async Task <TuneResult> TuneEnhanced(long frequency, long bandWidth, int deliverySystem)
        {
            _log.Debug($"Tuning enhanced freq: {frequency} Mhz, type: {deliverySystem}");

            var res = new TuneResult();

            try
            {
                DVBTResponse tuneRes   = null;
                DVBTResponse setPIDres = null;

                // five attempts
                for (var i = 1; i <= 5; i++)
                {
                    tuneRes = await Tune(frequency, bandWidth, deliverySystem);

                    if (tuneRes.SuccessFlag)
                    {
                        break;
                    }
                    else
                    {
                        await Task.Delay(500);
                    }
                }

                if (!tuneRes.SuccessFlag)
                {
                    res.Result = SearchProgramResultEnum.Error;
                    return(res);
                }

                // set PIDs 0 and 17
                for (var i = 1; i <= 5; i++)
                {
                    setPIDres = await SetPIDs(new List <long>() { 0, 17, 18 });

                    if (setPIDres.SuccessFlag)
                    {
                        break;
                    }
                    else
                    {
                        await Task.Delay(100);
                    }
                }

                if (!setPIDres.SuccessFlag)
                {
                    res.Result = SearchProgramResultEnum.Error;
                    return(res);
                }

                // freq tuned

                // timeout for get signal:
                var startTime = DateTime.Now;

                DVBTStatus status = new DVBTStatus();

                while ((DateTime.Now - startTime).TotalSeconds < 10)
                {
                    status = await GetStatus();

                    if (!status.SuccessFlag)
                    {
                        res.Result = SearchProgramResultEnum.Error;
                        return(res);
                    }

                    if (status.hasSignal == 0 && status.hasCarrier == 0 && (DateTime.Now - startTime).TotalSeconds > 5)
                    {
                        res.Result = SearchProgramResultEnum.NoSignal;
                        break;
                    }

                    if (status.hasSignal == 1 && status.hasSync == 1 && status.hasLock == 1)
                    {
                        res.Result = SearchProgramResultEnum.OK;
                        break;
                    }

                    // waiting
                    await Task.Delay(850);
                }

                if (status.hasSignal != 1 || status.hasSync != 1 || status.hasLock != 1)
                {
                    res.Result = SearchProgramResultEnum.NoSignal;
                    return(res);
                }

                res.SignalPercentStrength = status.rfStrengthPercentage;

                return(res);
            }
            catch (Exception ex)
            {
                _log.Error(ex);

                res.Result = SearchProgramResultEnum.Error;
                return(res);
            }
        }