Example #1
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));
            }
        }
 private void Action()
 {
     if (service.WriteCommand(ServiceEvent.CheckTrailers, new byte[0]))
     {
         service.WaitForEndOfOperation();
     }
 }
 private void Action()
 {
     if (!service.WriteCommand(ServiceEvent.ChangeTrailers, KeyA.Concat(AccessBits).Concat(KeyB).Concat(Encoding.ASCII.GetBytes(new char[] { (char)SelectedKey })).ToArray()))
     {
         return;
     }
     if (service.WaitForEndOfOperation() && service.CurrentState == ProgrammerState.OperationSuccess && service.CurrentCard != null)
     {
         service.CurrentAccess = service.rfidFactory.CreateRfidAccess(KeyA, KeyB, AccessBits, SelectedKey);
     }
 }
        private void Action()
        {
            var newAccess = service.rfidFactory.CreateRfidAccess();
            var args      = newAccess.KeyA.Concat(newAccess.AccessBits).Concat(newAccess.KeyB).Concat(Encoding.ASCII.GetBytes(new char[] { (char)newAccess.SelectedKey })).ToArray();

            if (!service.WriteCommand(ServiceEvent.SetTrailers, args))
            {
                return;
            }
            if (service.WaitForEndOfOperation() && service.CurrentState == ProgrammerState.OperationSuccess)
            {
                service.CurrentAccess = newAccess;
            }
        }
Example #5
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);
            }
        }
        private void Action()
        {
            var success = true;
            var args    = new byte[] { 0, 3, 1 };

            byte[] compare = null;
            for (var sector = 0; success && sector < ProgrammerService.UsableSectors; sector++)
            {
                args[0] = (byte)sector;
                success = service.WriteCommand(ServiceEvent.ReadCard, args) && service.WaitForEndOfOperation() && service.CurrentState == ProgrammerState.OperationSuccess &&
                          service.CurrentCard != null && service.result != null;
                if (success)
                {
                    if (compare != null)
                    {
                        for (var i = 0; success && i < compare.Length; i++)
                        {
                            success = compare[i] == service.result[i];
                        }

                        if (!success)
                        {
                            service.AppendOutput("^--- ERROR: Access bits differ!", true);
                            service.stateMachine.MoveNext(ServiceEvent.Failure); // this is not allowed to happen, the user needs to fix it manually
                        }
                    }
                    else
                    {
                        compare = service.result.ToArray();
                    }
                }
            }
            if (success)
            {
                var keyA = new byte[6];
                Array.Copy(compare, 0, keyA, 0, 6);
                var accessBits = new byte[4];
                Array.Copy(compare, 6, accessBits, 0, 4);
                var keyB = new byte[6];
                Array.Copy(compare, 10, keyB, 0, 6);
                service.CurrentAccess = service.rfidFactory.CreateRfidAccess(keyA, keyB, accessBits, service.CurrentAccess.SelectedKey);
            }
        }
Example #7
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));
            }
        }