예제 #1
0
        public static async Task <bool> UploadList(UInt64 list_id, ulong device_id, ProgressDialog pgd)
        {
            pgd.LabelText = "Compiling List From Database";

            byte[] compiled_list = await ListCompiler.GenerateGen2List(list_id);

            pgd.Step();
            await Task.Delay(100);

            pgd.Reset();
            pgd.LabelText = "Converting List Into Binary Format";

            byte[] binary_list = BinaryGenerator.GenerateIDList(compiled_list);

            pgd.Step();
            await Task.Delay(100);

            pgd.Reset();

            pgd.LabelText = "Acquiring Device Connection";

            ManagedStreamV2 stream_gen = await ManagedStreamV2.GenerateInstance(device_id);

            pgd.Step();
            await Task.Delay(100);

            pgd.Reset();

            pgd.LabelText = "Detecting Controller Revision";

            //each page is 128 bytes. 1000 pages can fit into memory.
            long pages_to_write = binary_list.Length / page_size;

            //progressBar1.Maximum = (int)pages_to_write;

            bool error_flag = false;
            //bool turbo_requires_reversal = false;

            //bool[] flags = new bool[2];

            int minor_rev = 0;

            await Task.Run(async() =>
            {
                //setLBLText(label1, "Determining Minor Version Support");

                //determine if the protocol version is 2 or 2.1
                try
                {
                    /*
                     * if (TimeCheck())
                     * {
                     *  //the doors are unlocked so let's speed things up
                     *  flags = await GetOverrideFlags();
                     *  if (!flags[0])
                     *  {
                     *      flags[0] = true;
                     *      turbo_requires_reversal = true;
                     *  }
                     *
                     *  await SetOverrides(flags);
                     *
                     *  setLBLText(label1, "Turbo Enabled");
                     * }
                     */

                    (await stream_gen.GetStream()).WriteByte((byte)'?'); //enter the management interface

                    (await stream_gen.GetStream()).WriteByte((byte)'S');

                    (await stream_gen.GetStream()).ReadByte();

                    minor_rev = 1;

                    //setLBLText(label1, "Enhanced Features Are Supported By This Device");
                }
                catch (Exception ex)
                {
                    //if an exception was cought then the minor protocol version is 0.
                }

                if (minor_rev == 0)
                {
                    (await stream_gen.GetStream()).WriteByte((byte)'?'); //enter the management interface

                    await Task.Delay(500);

                    (await stream_gen.GetStream()).WriteByte((byte)'L');

                    await Task.Delay(250);
                }

                pgd.Step();
                pgd.LabelText = minor_rev == 0 ? "A Revision 0 Controller Has Been Detected" : "A Revision 1 Controller Has Been Detected";
                await Task.Delay(100);
                pgd.Reset();

                pgd.Maximum = (int)pages_to_write;

                try
                {
                    using (BinaryReader binaryliststream = new BinaryReader(new MemoryStream(binary_list)))
                    {
                        byte[] page_buffer = new byte[128];

                        UInt16 page_counter        = 0;
                        bool cycle_completion_flag = false;

                        while (true)
                        {
                            try
                            {
                                while (page_counter < 1000)
                                {
                                    if (page_counter < pages_to_write)
                                    {
                                        if (!cycle_completion_flag)
                                        {
                                            binaryliststream.Read(page_buffer, 0, page_size);
                                            cycle_completion_flag = true;
                                        }
                                    }

                                    if (minor_rev == 0 || page_counter < pages_to_write)
                                    {
                                        pgd.LabelText = "List Uploading Is Now In Progress " + Math.Round(((float)page_counter / (minor_rev == 0 ? 1000 : pages_to_write)) * 100, 2) + "%";

                                        if ((byte)(await stream_gen.GetStream()).ReadByte() != (byte)'?') //read ?
                                        {
                                            throw new Exception();
                                        }

                                        if (minor_rev == 1)
                                        {
                                            (await stream_gen.GetStream()).Write(BitConverter.GetBytes(page_counter).Reverse().ToArray(), 0, 2);
                                        }
                                    }

                                    if (page_counter < pages_to_write)
                                    {
                                        (await stream_gen.GetStream()).Write(page_buffer, 0, page_size);

                                        if (minor_rev == 1)
                                        {
                                            (await stream_gen.GetStream()).WriteByte(CRC8(page_buffer, 128));

                                            if ((byte)(await stream_gen.GetStream()).ReadByte() != (byte)'!') //read ?
                                            {
                                                throw new Exception();                                        //read ! (operation completion flag)
                                            }
                                            if (await VerifyListPage(stream_gen, page_counter, page_buffer) == false)
                                            {
                                                throw new Exception();
                                            }

                                            if (page_counter + 1 < pages_to_write)
                                            {
                                                (await stream_gen.GetStream()).WriteByte((byte)'?');              //enter the management interface
                                                (await stream_gen.GetStream()).WriteByte((byte)'S');              //issue command

                                                if ((byte)(await stream_gen.GetStream()).ReadByte() != (byte)'?') //read ?
                                                {
                                                    throw new Exception();
                                                }
                                            }
                                        }
                                    }
                                    else
                                    if (minor_rev == 0)
                                    {
                                        byte[] null_buffer = new byte[128];
                                        (await stream_gen.GetStream()).Write(null_buffer, 0, page_size);
                                    }
                                    else
                                    if (minor_rev == 1)
                                    {
                                        pgd.Reset();
                                        pgd.LabelText = "Zeroing Out of Bounds List Memory";
                                        pgd.SetMarqueeStyle();

                                        (await stream_gen.GetStream()).WriteByte((byte)'?');              //enter the management interface
                                        (await stream_gen.GetStream()).WriteByte((byte)'N');              //issue command

                                        if ((byte)(await stream_gen.GetStream()).ReadByte() != (byte)'?') //read ?
                                        {
                                            throw new Exception();
                                        }

                                        (await stream_gen.GetStream()).Write(BitConverter.GetBytes(page_counter).Reverse().ToArray(), 0, 2);
                                        (await stream_gen.GetStream()).Write(BitConverter.GetBytes((UInt16)999).Reverse().ToArray(), 0, 2);

                                        await Task.Delay((1000 - page_counter) * 10);

                                        if ((byte)(await stream_gen.GetStream()).ReadByte() != (byte)'!')         //read !
                                        {
                                            throw new Exception();
                                        }

                                        pgd.Reset();

                                        break;
                                    }

                                    pgd.Step();

                                    cycle_completion_flag = false;

                                    page_counter++;
                                }

                                break;
                            }
                            catch (Exception ex2)
                            {
                                if (minor_rev == 1)
                                {
                                    int i = 0;
                                    for (; i < 5; i++)
                                    {
                                        try
                                        {
                                            if (page_counter < pages_to_write)
                                            {
                                                (await stream_gen.GetStream()).WriteByte((byte)'?'); //enter the management interface
                                                (await stream_gen.GetStream()).WriteByte((byte)'S');

                                                if ((byte)(await stream_gen.GetStream()).ReadByte() != (byte)'?') //read ?
                                                {
                                                    throw new Exception();
                                                }
                                            }

                                            break;
                                        }
                                        catch (Exception ex3) { }
                                    }

                                    if (i == 3)
                                    {
                                        throw;
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    error_flag = true;
                }
            });

            var config = MCv2Persistance.Instance.Config;

            if (MCv2Persistance.Instance.Config.UIConfiguration.ShowDialogOnMCV2OfflineControllerInteractionFailure && error_flag)
            {
                MessageBox.Show("An Error Occured While Uploading The List. The Operation Has Been Aborted.", "Error");
            }
            else
            {
                if (config.SyncTimeAfterUploadsFlag)
                {
                    pgd.Reset();
                    pgd.LabelText = "Setting Controller Time";

                    await SetControllerTime(stream_gen);

                    pgd.Step();
                    await Task.Delay(100);

                    pgd.Reset();
                }

                pgd.LabelText = "The List Was Uploaded Successfully";
            }

            await Task.Delay(100);

            return(error_flag);
        }
예제 #2
0
        public static async Task <object[]> VerifyList(ulong list_id, ulong device_id, ProgressDialog pgd)
        {
            int verification_index = 0;

            pgd.LabelText = "Compiling List From Database";

            byte[] compiled_list = await ListCompiler.GenerateGen2List(list_id);

            pgd.Step();
            await Task.Delay(100);

            pgd.Reset();
            pgd.LabelText = "Converting List Into Binary Format";

            byte[] binary_list = BinaryGenerator.GenerateIDList(compiled_list);

            pgd.Step();
            await Task.Delay(100);

            pgd.Reset();

            pgd.LabelText = "Acquiring Device Connection";

            ManagedStreamV2 stream_gen = await ManagedStreamV2.GenerateInstance(device_id);

            pgd.Step();
            await Task.Delay(100);

            pgd.Reset();

            int pages_available_to_compare = binary_list.Length / page_size;

            pgd.Maximum   = pages_available_to_compare;
            pgd.LabelText = "Beginning List Verification";

            await Task.Run(async() =>
            {
                try
                {
                    for (; verification_index < pages_available_to_compare; verification_index++)
                    {
                        byte[] comparing_to = new byte[128];

                        if (verification_index < pages_available_to_compare)
                        {
                            comparing_to = binary_list.SubArray(verification_index * 128, 128);
                        }

                        if (await VerifyListPage(stream_gen, (ushort)verification_index, comparing_to))
                        {
                            double pdone = (verification_index + 1);
                            pdone       /= pages_available_to_compare;
                            pdone       *= 100;

                            pgd.LabelText = "Memory page comparison in progress. " + (int)pdone + "% Complete";
                            pgd.Step();
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex) { }
            });

            if (verification_index >= pages_available_to_compare)
            {
                return new object[] { true, "The list was verified. All pages match." }
            }
            ;
            else
            {
                return new object[] { false, "Verification failed. Page " + (verification_index + 1) + " in controller memory does not match the local copy." }
            };
        }