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 }); }
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); }
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); }
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 }); }
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 }); }
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); }
private async Task <DVBTResponse> SendRequest(DVBTRequest request, int secondsTimeout = 20) { _log.Debug($"Sending request {request}"); var startTime = DateTime.Now; var bufferSize = 1024; return(await Task.Run(async() => { var _responseBuffer = new List <byte>(); try { byte[] buffer = new byte[bufferSize]; bool reading = true; request.Send(_controlStream); do { if (_controlClient.Client.Available > 0) { //_log.Debug("Reading from stream ..."); var readByteCount = _controlStream.Read(buffer, 0, bufferSize); if (readByteCount > 0) { _log.Debug($"Received {readByteCount} bytes"); for (var i = 0; i < readByteCount; i++) { _responseBuffer.Add(buffer[i]); } if (request.ResponseBytesExpectedCount >= _responseBuffer.Count) { reading = false; } } } else { //_log.Debug("No data available ..."); } var timeSpan = Math.Abs((DateTime.Now - startTime).TotalSeconds); if (timeSpan > secondsTimeout) { throw new TimeoutException("TimeOut"); } await Task.Delay(200); }while (reading); } catch (TimeoutException) { _log.Debug("Timeout for reading data ..."); return new DVBTResponse() { SuccessFlag = false }; } catch (Exception ex) { _log.Debug($"General error: {ex}"); return new DVBTResponse() { SuccessFlag = false }; } _log.Debug($"Request sent"); var response = new DVBTResponse() { SuccessFlag = true, RequestTime = startTime, ResponseTime = DateTime.Now }; response.Bytes.AddRange(_responseBuffer); return response; })); }