Exemplo n.º 1
0
        public static async Task <FidoU2FHidMessage> ReadFidoU2FHidMessageAsync([NotNull] this IHidDevice device, CancellationToken cancellationToken)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            var initReport = await device.GetInputReportAsync(cancellationToken);

            var init = InitializationPacket.ReadFrom(initReport.Data);

            var  dataSizeReceived = init.Data.Count;
            var  continuations    = new List <ContinuationPacket>();
            byte index            = 0;

            while (dataSizeReceived < init.PayloadLength)
            {
                var continuationReport = await device.GetInputReportAsync(cancellationToken);

                var continuation = ContinuationPacket.ReadFrom(continuationReport.Data, init.PayloadLength - dataSizeReceived);
                if (continuation.PaketSequence != index)
                {
                    throw new InvalidSequenceNumberException(continuationReport.Data,
                                                             $"The sequence number isn't the expected one, expected {index} but read {continuation.PaketSequence}");
                }
                continuations.Add(continuation);
                dataSizeReceived += continuation.Data.Count;
                index            += 1;
            }

            return(BuildMessage(init, continuations));
        }