Beispiel #1
0
        private async Task <byte[]> WaitForPIVData(Guid connectionId, byte address, GetPIVData getPIVData, TimeSpan timeout,
                                                   CancellationToken cancellationToken)
        {
            bool     complete = false;
            DateTime endTime  = DateTime.UtcNow + timeout;

            byte[] data = null;

            void Handler(object sender, PIVDataReplyEventArgs args)
            {
                // Only process matching replies
                if (args.ConnectionId != connectionId || args.Address != address)
                {
                    return;
                }

                var pivData = args.PIVData;

                data ??= new byte[pivData.WholeMessageLength];

                complete = Message.BuildMultiPartMessageData(pivData.WholeMessageLength, pivData.Offset,
                                                             pivData.LengthOfFragment, pivData.Data, data);
            }

            PIVDataReplyReceived += Handler;

            try
            {
                await SendCommand(connectionId,
                                  new GetPIVDataCommand(address, getPIVData), cancellationToken).ConfigureAwait(false);

                while (DateTime.UtcNow <= endTime)
                {
                    if (complete)
                    {
                        return(data);
                    }

                    // Delay for default poll interval
                    await Task.Delay(TimeSpan.FromMilliseconds(250), cancellationToken);
                }

                throw new TimeoutException("Timeout waiting to receive PIV data.");
            }
            finally
            {
                PIVDataReplyReceived -= Handler;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Request to get PIV data from PD.
        /// </summary>
        /// <param name="connectionId">Identify the connection for communicating to the device.</param>
        /// <param name="address">Address assigned to the device.</param>
        /// <param name="getPIVData">Describe the PIV data to retrieve.</param>
        /// <param name="timeout">A TimeSpan that represents the number of milliseconds to wait, a TimeSpan that represents -1 milliseconds to wait indefinitely, or a TimeSpan that represents 0 milliseconds to test the wait handle and return immediately.</param>
        /// <param name="cancellationToken">The CancellationToken token to observe.</param>
        /// <returns>A response with the PIV data requested.</returns>
        public async Task <byte[]> GetPIVData(Guid connectionId, byte address, GetPIVData getPIVData, TimeSpan timeout,
                                              CancellationToken cancellationToken = default)
        {
            var pivDataLock = GetPIVDataLock(connectionId, address);

            if (!await pivDataLock.WaitAsync(timeout, cancellationToken))
            {
                throw new TimeoutException("Timeout waiting for another PIV data request to complete.");
            }

            try
            {
                return(await WaitForPIVData(connectionId, address, getPIVData, timeout, cancellationToken));
            }
            finally
            {
                pivDataLock.Release();
            }
        }
        public string BuildCommand_TestCases(byte address, bool useCrc, bool useSecureChannel, GetPIVData getPIVData)
        {
            var getPIVDataCommand = new GetPIVDataCommand(address, getPIVData);

            return(BitConverter.ToString(getPIVDataCommand.BuildCommand(new Device(address, useCrc, useSecureChannel))));
        }
Beispiel #4
0
 public GetPIVDataCommand(byte address, GetPIVData getPivData)
 {
     _getPivData = getPivData;
     Address     = address;
 }