示例#1
0
        private void Action()
        {
            var block     = service.GetBlockByContentIndex(Start);
            var lastBlock = service.GetBlockByContentIndex(Length != 0 ? Start + Length - 1 : Start + ProgrammerService.UsableBytes - 1);

            var args       = new byte[3]; // sector, block, #blocks to read
            var tmpContent = new List <byte>();

            var success = true;

            while (success && block < lastBlock)
            {
                // read sector for sector
                args[0] = (byte)(block / 4);                                      // sector
                args[1] = (byte)(block % 4);                                      // block
                args[2] = (byte)Math.Min(3 - (block % 4), lastBlock - block + 1); // number of blocks: 3 or 2 in the first sector
                if (!service.WriteCommand(ServiceEvent.ReadCard, args))
                {
                    return;
                }
                if (success = (service.WaitForEndOfOperation() && service.CurrentState == ProgrammerState.OperationSuccess && service.result != null))
                {
                    block += (byte)(args[2] + 1); // ignore access bits
                    tmpContent.AddRange(service.result);
                    if (Length == 0 && service.result.Contains(service.EndMarker))
                    {
                        break;
                    }
                }
            }

            // cut from start to length or trim if no length was given
            if (success)
            {
                byte[] result;
                if (Length != 0)
                {
                    result = new byte[Length];
                    Array.Copy(tmpContent.ToArray(), (int)Start % 16, result, 0, Length);
                }
                else
                {
                    result = service.TrimContent(tmpContent.ToArray());
                }
                service.CurrentCard = service.rfidFactory.CreateRfidCard(service.CurrentCard.Id, result);
            }
        }
示例#2
0
        private void Action()
        {
            var block   = service.GetBlockByContentIndex(Start);
            var args    = new byte[18]; // default: zero
            var success = true;

            // first block might be different (if start is set to non zero)
            if (Start % 16 != 0 && Start < service.CurrentCard.Content.Length) // we need to keep a few bytes intact
            {
                var a = new byte[18];
                a[0] = (byte)(block / 4); // sector
                a[1] = (byte)(block % 4); // block
                Array.Copy(service.CurrentCard.Content, 0, a, 2, (int)Start % 16);
                if (success = service.WriteCommand(ServiceEvent.WriteCard, a))
                {
                    block++;
                    if (block % 4 == 3)
                    {
                        block++;
                    }
                    success = service.WaitForEndOfOperation();
                }
                else
                {
                    service.stateMachine.MoveNext(ServiceEvent.Failure);
                }
            }
            while (success && block < ProgrammerService.Blocks)
            {
                // prepare new args
                args[0] = (byte)(block / 4); // sector
                args[1] = (byte)(block % 4); // block
                if (success = service.WriteCommand(ServiceEvent.WriteCard, args))
                {
                    block++;
                    if (block % 4 == 3)
                    {
                        block++;
                    }
                    success = service.WaitForEndOfOperation();
                }
                else
                {
                    service.stateMachine.MoveNext(ServiceEvent.Failure);
                }
            }
            if (success && Start < service.CurrentCard.Content.Length) // we cut the current content
            {
                var content = new byte[Start];
                Array.Copy(service.CurrentCard.Content, content, Start);
                service.CurrentCard = service.rfidFactory.CreateRfidCard(service.CurrentCard.Id, service.TrimContent(content));
            }
        }
示例#3
0
        private void Action()
        {
            var contentEnd = Start + Content.Length;
            var newContent = IgnorePreviousEnd && Start == 0 ? Content : new byte[IgnorePreviousEnd ? contentEnd : Math.Max(contentEnd, service.CurrentCard.Content.Length)];

            //  add missing beginning
            if (Start > 0 && service.CurrentCard.Content.Length > 0) // copy current content if possible
            {
                Array.Copy(service.CurrentCard.Content, newContent, Math.Min(Start, service.CurrentCard.Content.Length));
            }
            for (var i = service.CurrentCard.Content.Length; i < Start; i++) // add zeros for the rest
            {
                newContent[i] = 0;
            }

            if (Content != newContent) // add content to new array
            {
                Array.Copy(Content, 0, newContent, Start, Content.Length);
            }

            if (!IgnorePreviousEnd && service.CurrentCard.Content.Length > contentEnd)
            {
                Array.Copy(service.CurrentCard.Content, contentEnd, newContent, contentEnd, service.CurrentCard.Content.Length - contentEnd); // add trailing content from before
            }
            var endMarkerNeeded = !IgnoreEndMarker && (IgnorePreviousEnd || (service.CurrentCard.Content.Length / 16) <= (contentEnd / 16)) && (newContent.Length == 0 || newContent[newContent.Length - 1] != service.EndMarker);
            var block           = service.GetBlockByContentIndex(Start); // current (real) block

            if (endMarkerNeeded || block * 16 != Start || contentEnd % 16 != 0)
            {
                if (endMarkerNeeded)
                {
                    contentEnd++;
                }
                // content must be multiple of 16 bytes, we have to make it longer to transmit it
                // old length + missing bytes at start + missing bytes at end (possbibly one more for endmarker)
                var tmpContent = newContent;
                newContent = new byte[newContent.Length + (endMarkerNeeded ? 1 : 0) + (Start % 16) + (contentEnd % 16 != 0 ? 16 - (contentEnd % 16) : 0)];
                var s = (Start / 16) * 16;
                Array.Copy(tmpContent, 0, newContent, 0, Math.Min(tmpContent.Length, newContent.Length - (endMarkerNeeded ? 1 : 0))); // add content (adds missing start and end already)
                if (endMarkerNeeded)
                {
                    newContent[contentEnd - 1] = service.EndMarker;
                }
            }

            var b       = Start / 16;            // block pos in content
            var lastB   = (contentEnd - 1) / 16; // last block in content
            var args    = new byte[18];
            var success = true;

            while (success && b <= lastB)
            {
                // prepare new args
                args[0] = (byte)(block / 4); // sector
                args[1] = (byte)(block % 4); // block
                Array.Copy(newContent, b * 16, args, 2, 16);
                if (success = service.WriteCommand(ServiceEvent.WriteCard, args))
                {
                    b++;
                    block++;
                    if (block % 4 == 3)
                    {
                        block++; // skip access bits
                    }
                    success = service.WaitForEndOfOperation();
                }
                else
                {
                    service.stateMachine.MoveNext(ServiceEvent.Failure);
                }
            }
            if (success)
            {
                service.CurrentCard = service.rfidFactory.CreateRfidCard(service.CurrentCard.Id, IgnoreEndMarker ? newContent : service.TrimContent(newContent));
            }
        }