Esempio n. 1
0
        static bool GeneratePrivateKeyPublicKey(string pw)
        {
            var ok = false;

            Console.Clear();
            ConsoleEx.TitleBar(0, "Generate new private/public key", ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            Console.Clear();
            ConsoleEx.TitleBar(0, "Generate Private/Public Key", ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            var keyInfo = ConsoleEx.Question(3, "Re generate the private key and publick key Y)es N)o", new List <char> {
                'Y', 'N'
            });

            if (keyInfo == 'N')
            {
                return(ok);
            }

            keyInfo = ConsoleEx.Question(3, "Are you sure you want to generate Private/Public Key Y)es N)o", new List <char> {
                'Y', 'N'
            });
            if (keyInfo == 'N')
            {
                return(ok);
            }

            ConsoleEx.WriteLine(0, 4, "Formatting...", ConsoleColor.Yellow);
            var ppk = new PrivatePublicKeyHelper(true);

            if (!_sfsManager.Format())
            {
                ConsoleEx.WriteLine(0, 3, "Format failed", ConsoleColor.Red);
            }

            ConsoleEx.WriteLine(0, 5, "Loading and saving files...", ConsoleColor.Yellow);
            var privatek = _sfsManager.AddFile("PrivateKey");

            privatek.SetContentAsString(ppk.PrivateKey);
            var publicK = _sfsManager.AddFile("PublicKey");

            publicK.SetContentAsString(ppk.PublicKey);
            var readmeTxt = _sfsManager.AddFile("ReadMe.txt");

            readmeTxt.SetContentAsString(LoremIpsum);
            var rr = _sfsManager.UpdateFileSystem();

            if (rr)
            {
                Console.WriteLine(string.Format("Keys were generated"), ConsoleColor.Cyan);
                ok = ReloadFileSystem(pw, ppk);
            }
            else
            {
                Console.WriteLine(string.Format("Error generating the keys"), ConsoleColor.Red);
            }

            Pause();
            return(ok);
        }
Esempio n. 2
0
        static APA102LEDStrip.Extensions.LedPerMeter AskForStripType()
        {
            Console.Clear();

            ConsoleEx.TitleBar(0, GetAssemblyProduct(), ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            ConsoleEx.TitleBar(ConsoleEx.WindowHeight - 2, Nusbio.GetAssemblyCopyright(), ConsoleColor.White, ConsoleColor.DarkBlue);
            var r = ConsoleEx.Question(ConsoleEx.WindowHeight - 3, "Strip Type?    3)0 LED/Meter   6)0 LED/Meter  I) do not know", new List <Char>()
            {
                '3', '6', 'I'
            });

            switch (r)
            {
            case '3': return(APA102LEDStrip.Extensions.LedPerMeter._30LedPerMeter);

            case '6': return(APA102LEDStrip.Extensions.LedPerMeter._60LedPerMeter);
            }
            return(APA102LEDStrip.Extensions.LedPerMeter._30LedPerMeter);
        }
Esempio n. 3
0
        public static T WaitOnComponentToBePlugged <T>(string componentName, Func <T> initComponentCode)
        {
            while (true)
            {
                var component = initComponentCode();
                if (component != null)
                {
                    return(component);
                }

                var r = ConsoleEx.Question(1, string.Format("Component {0} not found. R)etry A)bandon", componentName), new List <char> {
                    'R', 'A'
                });
                if (r == 'A')
                {
                    return(default(T));
                }
            }
        }
Esempio n. 4
0
        static int AskForLedCount()
        {
            Console.Clear();

            ConsoleEx.TitleBar(0, GetAssemblyProduct(), ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            ConsoleEx.TitleBar(ConsoleEx.WindowHeight - 2, Nusbio.GetAssemblyCopyright(), ConsoleColor.White, ConsoleColor.DarkBlue);
            var r = ConsoleEx.Question(ConsoleEx.WindowHeight - 3, "Led Count?    1)0  3)0  6)0", new List <Char>()
            {
                '3', '6', '1'
            });

            switch (r)
            {
            case '1': return(10);

            case '3': return(30);

            case '6': return(60);
            }
            return(10);
        }
Esempio n. 5
0
        static bool FormatAndInitDisk()
        {
            var ok = false;

            Console.Clear();
            ConsoleEx.TitleBar(0, "Format and initialization", ConsoleColor.Yellow, ConsoleColor.DarkBlue);
            var keyInfo = ConsoleEx.Question(3, "Format and initialize disk Y)es N)o", new List <char> {
                'Y', 'N'
            });

            if (keyInfo == 'N')
            {
                return(ok);
            }

            ConsoleEx.WriteLine(0, 4, "Formatting...", ConsoleColor.Yellow);
            if (!_sfsManager.Format())
            {
                ConsoleEx.WriteLine(0, 3, "Format failed", ConsoleColor.Red);
                return(false);
            }

            ConsoleEx.WriteLine(0, 5, "Loading and saving files...", ConsoleColor.Yellow);
            _sfsManager.AddFile(@".\files\Readme.txt");
            _sfsManager.AddFile(@".\files\Shadow.jpg");
            var rr = _sfsManager.UpdateFileSystem();

            if (rr)
            {
                Console.WriteLine(string.Format("Keys were generated"), ConsoleColor.Cyan);
                ok = ReloadFileSystem();
            }
            else
            {
                Console.WriteLine(string.Format("Error generating the keys"), ConsoleColor.Red);
            }

            Pause();
            return(ok);
        }
Esempio n. 6
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);

                _flash = new FLASH_S25FL1xxK(
                    nusbio: nusbio,
                    clockPin: NusbioGpio.Gpio0,
                    mosiPin: NusbioGpio.Gpio1,
                    misoPin: NusbioGpio.Gpio2,
                    selectPin: NusbioGpio.Gpio3
                    );

                _flash.Begin();


                var r1 = _flash.GetRegister1();

                while (nusbio.Loop())
                {
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.W)
                        {
                            var a = ConsoleEx.Question(23, "Execute Write/Read/Write Test 2k now Y)es, N)o", new List <char>()
                            {
                                'Y', 'N'
                            });
                            if (a == 'Y')
                            {
                                var testValue = 1 + 4 + 16 + 64;
                                WriteFlashNORMemory(_flash.MaxPage, testValue);
                                ReadAndVerifyFlashNORMemory(_flash.MaxPage, testValue);
                                WriteFlashNORMemory(_flash.MaxPage);
                                ReadAndVerifyFlashNORMemory(_flash.MaxPage);
                            }
                        }

                        if (k == ConsoleKey.R)
                        {
                            ReadAndVerifyFlashNORMemory(_flash.MaxPage);
                        }

                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }

                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Esempio n. 7
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);

                _eeprom = new EEPROM_25AA256(
                    nusbio: nusbio,
                    clockPin: NusbioGpio.Gpio0,
                    mosiPin: NusbioGpio.Gpio1,
                    misoPin: NusbioGpio.Gpio2,
                    selectPin: NusbioGpio.Gpio3
                    );

                _eeprom.Begin();

                while (nusbio.Loop())
                {
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.W)
                        {
                            var a = ConsoleEx.Question(23, "Execute Write/Read/Write Test 32k now Y)es, N)o", new List <char>()
                            {
                                'Y', 'N'
                            });
                            if (a == 'Y')
                            {
                                var testValue = 1 + 4 + 16 + 64;
                                WriteEEPROMPage(512, testValue);
                                ReadAndVerifyEEPROMPage(512, testValue);
                                WriteEEPROMPage(512);
                                ReadAndVerifyEEPROMPage(512);
                            }
                        }

                        if (k == ConsoleKey.R)
                        {
                            ReadAndVerifyEEPROMPage(10);
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.A)
                        {
                            ReadAndVerifyEEPROMPage(512);
                            ReadAndVerifyEEPROMPage_BatchRead(512);
                        }
                        if (k == ConsoleKey.B)
                        {
                            // This mode is slow, only read the first 64 pages out of 512
                            ReadAndVerifyEEPROMOnyByteAtTheTime(64);
                        }

                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }

                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Esempio n. 8
0
        public static void Run(string[] args)
        {
            var pw = string.Empty;

            Console.WriteLine("Nusbio initialization");
            Nusbio.ActivateFastMode();
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                pw          = AskForPW(nusbio, VolumeName);
                _sfsManager = new sFs.sFsManager(VolumeName, pw, nusbio: nusbio, clockPin: NusbioGpio.Gpio0, mosiPin: NusbioGpio.Gpio1, misoPin: NusbioGpio.Gpio2, selectPin: NusbioGpio.Gpio3);
                _sfsManager.Begin();

                Cls(nusbio, true);
                Console.WriteLine(Environment.NewLine + Environment.NewLine);

                Console.WriteLine("Initializing private public key module");
                var ppk = new PrivatePublicKeyHelper(true);

                //GeneratePrivateKeyPublicKey(pw);

                var ok = ReloadFileSystem(pw, ppk);
                if (!ok)
                {
                    Cls(nusbio, true);

                    if (ConsoleEx.Question(3, string.Format("Cannot access {0} with this password, retry Y)es N)o",
                                                            _sfsManager.VolumeName), new List <char> {
                        'Y', 'N'
                    }) == 'Y')
                    {
                        Environment.Exit(1);
                    }

                    Cls(nusbio, true); // Re format - re init key

                    var keyInfo = ConsoleEx.Question(4, string.Format("Would you like to initialize {0} Y)es N)o",
                                                                      _sfsManager.VolumeName), new List <char> {
                        'Y', 'N'
                    });
                    if (keyInfo == 'N')
                    {
                        return;
                    }
                    GeneratePrivateKeyPublicKey(pw);
                }

                Cls(nusbio);


                while (nusbio.Loop())
                {
                    if (Console.KeyAvailable)
                    {
                        var kk = Console.ReadKey(true);
                        var k  = kk.Key;

                        if (kk.Modifiers == ConsoleModifiers.Control)
                        {
                            if (DigitKeys.Contains(k))
                            {
                                var a = (int)k - 48;
                                if (a == 0)
                                {
                                    a = 10;
                                }
                                a--;
                                ShowFile(a);
                            }
                        }
                        else
                        {
                            if (k == ConsoleKey.A)
                            {
                                AddNewTextFile(pw);
                            }
                            if (k == ConsoleKey.B)
                            {
                                AddNewTextFile(pw, @"C:\Users\fredericaltorres\Dropbox\FTO\MonteChristro.jpg");
                            }


                            if (k == ConsoleKey.D1)
                            {
                                EncrypteFile(ppk, ppk.PublicKey, "PublicKey");
                            }
                            if (k == ConsoleKey.D2)
                            {
                                EncrypteFile(ppk, ppk.PrivateKey, "PrivateKey");
                            }
                            if (k == ConsoleKey.D3)
                            {
                                DecrypteFile(ppk, ppk.PrivateKey, "PrivateKey");
                            }
                            if (k == ConsoleKey.D4)
                            {
                                ExportKey(ppk.PublicKey, "PublicKey");
                            }
                            if (k == ConsoleKey.D5)
                            {
                                ExportKey(ppk.PrivateKey, "PrivateKey");
                            }
                            if (k == ConsoleKey.D6)
                            {
                                GeneratePrivateKeyPublicKey(pw);
                            }
                            if (k == ConsoleKey.U)
                            {
                                UnitTests(ppk, _sfsManager);
                            }
                            if (k == ConsoleKey.Q)
                            {
                                break;
                            }
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Esempio n. 9
0
        public static void Run(string[] args)
        {
            SecureString pw;

            Console.WriteLine("Nusbio initialization");
            Nusbio.ActivateFastMode();
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                pw              = AskForPW(nusbio, VolumeName);
                _sfsManager     = new sFs.sFsManager(VolumeName, pw, nusbio: nusbio, clockPin: NusbioGpio.Gpio0, mosiPin: NusbioGpio.Gpio1, misoPin: NusbioGpio.Gpio2, selectPin: NusbioGpio.Gpio3);
                _sFsREPLCommand = new sFs.sFsREPLCommand(_sfsManager);

                Cls(nusbio, true);
                Console.WriteLine(Environment.NewLine + Environment.NewLine);

                var ok = ReloadFileSystem();
                if (!ok)
                {
                    Cls(nusbio, true);
                    if (ConsoleEx.Question(3, string.Format("Cannot access {0} with this password, retry password Y)es N)o",
                                                            _sfsManager.VolumeName), new List <char> {
                        'Y', 'N'
                    }) == 'Y')
                    {
                        Environment.Exit(1);
                    }

                    Cls(nusbio, true); // Re format - re init key
                    var keyInfo = ConsoleEx.Question(4, string.Format("Would you like to format {0} Y)es N)o", _sfsManager.VolumeName), new List <char> {
                        'Y', 'N'
                    });
                    if (keyInfo == 'N')
                    {
                        return;
                    }
                    FormatAndInitDisk();
                }

                Cls(nusbio);

                while (nusbio.Loop())
                {
                    Console.Write(string.Format(@"{0}:\>", _sfsManager.VolumeName));
                    var cmd    = Console.ReadLine();
                    var cmdLC  = cmd.ToLowerInvariant();
                    var tokens = cmdLC.Split(' ');
                    if (tokens.Length > 0)
                    {
                        if (tokens[0] == "exit")
                        {
                            nusbio.ExitLoop();
                        }
                        if (tokens[0] == "format")
                        {
                            FormatAndInitDisk();
                        }
                        if (tokens[0] == "cls")
                        {
                            Console.Clear();
                        }
                        if (tokens[0] == "help")
                        {
                            Console.WriteLine(COMMAND_HELP);
                        }
                        if (tokens[0] == "del")
                        {
                            if (_sfsManager.Delete(cmd.Substring(4)))
                            {
                                if (_sfsManager.UpdateFileSystem())
                                {
                                    Console.WriteLine(SFS_COMMAND_MESSAGE_FILE_DELETED);
                                }
                                else
                                {
                                    Console.WriteLine(SFS_COMMAND_ERR_SAVING_FAT);
                                }
                            }
                            else
                            {
                                Console.WriteLine(SFS_COMMAND_ERR_CANNOT_FIND_FILE);
                            }
                        }
                        if (tokens[0] == "compact")
                        {
                            if (_sfsManager.Compact())
                            {
                                Console.WriteLine(SFS_COMMAND_MESSAGE_COMPACTAGE_DONE);
                            }
                            else
                            {
                                Console.WriteLine(SFS_COMMAND_ERR_COMPACTAGE_FAILED);
                            }
                        }
                        if (tokens[0] == "type")
                        {
                            Console.WriteLine(_sFsREPLCommand.TypeCommand(cmd.Substring(5)));
                        }
                        if (tokens[0] == "open")
                        {
                            Console.WriteLine(_sFsREPLCommand.OpenCommand(cmd.Substring(5)));
                        }
                        if (tokens[0] == "dir")
                        {
                            Console.WriteLine(_sFsREPLCommand.DirCommand());
                        }
                        if (tokens[0] == "dirall")
                        {
                            Console.WriteLine(_sFsREPLCommand.DirCommand(true));
                        }
                        if (tokens[0] == "copy")
                        {
                            Console.WriteLine(_sFsREPLCommand.CopyFileCommand(cmd.Substring(5)));
                        }
                    }
                }
            }
            Console.Clear();
        }
Esempio n. 10
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");

            Nusbio.ActivateFastMode();

            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                //_eeprom = new EEPROM_25AA1024(
                //   nusbio: nusbio,
                //   clockPin: NusbioGpio.Gpio4,
                //   mosiPin: NusbioGpio.Gpio5,
                //   misoPin: NusbioGpio.Gpio2,
                //   selectPin: NusbioGpio.Gpio6
                //   );


                _eeprom = new EEPROM_25AA1024(
                    nusbio: nusbio,
                    clockPin: NusbioGpio.Gpio0,
                    mosiPin: NusbioGpio.Gpio1,
                    misoPin: NusbioGpio.Gpio2,
                    selectPin: NusbioGpio.Gpio3
                    );

                _eeprom.Begin();

                // Noticed that we cannot detect if the EEPROM is connected or
                // not based on SPI command. The SPI command does not fail.
                // But return a buffer with garbage mostly all byte set to 255
                // So based on the expected data of the first page we could
                // detect of the EEPROM is connected or not
                var r = _eeprom.ReadPage(0, _eeprom.PAGE_SIZE * 4);
                if (r.Succeeded)
                {
                    if (r.Buffer[0] == 255)
                    {
                        Console.WriteLine("SPI EEPROM 25AA1024 not detected, hit enter to continue");
                        Console.ReadLine();
                    }
                }

                Cls(nusbio);

                while (nusbio.Loop())
                {
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.W)
                        {
                            var a = ConsoleEx.Question(23, string.Format("Execute Write/Read/Write Test {0}k now Y)es, N)o", _eeprom.MaxKByte), new List <char>()
                            {
                                'Y', 'N'
                            });
                            if (a == 'Y')
                            {
                                Console.Clear(); ConsoleEx.TitleBar(0, "Writing EEPROM");

                                var testValue = 1 + 4 + 16 + 64;
                                _eeprom.WriteAll(0, EEPROM_25AA1024.MakeBuffer((byte)testValue, _eeprom.MaxByte), (pageIndex, maxPage) => ConsoleEx.WriteLine(0, 2, string.Format("{0:000} % done", 1.0 * pageIndex / maxPage * 100.0), ConsoleColor.Yellow));

                                Console.Clear(); ConsoleEx.TitleBar(0, "Reading EEPROM");
                                var dataRead = _eeprom.ReadAll(16, (pageIndex, maxPage) => ConsoleEx.WriteLine(0, 2, string.Format("{0:000} % done", 1.0 * pageIndex / maxPage * 100.0), ConsoleColor.Yellow));
                                for (var z = 0; z < _eeprom.MaxByte; z++)
                                {
                                    if (dataRead[z] != testValue)
                                    {
                                        throw new ArgumentException("Writing/Reading error");
                                    }
                                }
                                WriteEEPROMPage(512);
                                ReadAndVerifyEEPROMPage_BatchRead(512);
                            }
                        }

                        if (k == ConsoleKey.R)
                        {
                            ReadAndVerifyEEPROMPage(10);
                            Cls(nusbio);
                        }

                        if (k == ConsoleKey.A)
                        {
                            ReadAndVerifyEEPROMPage_BatchRead(512);
                            ReadAndVerifyEEPROMPage(512);
                        }
                        if (k == ConsoleKey.B)
                        {
                            // This mode is slow, only read the first 64 pages out of 512
                            ReadAndVerifyEEPROMOnyByteAtTheTime(64);
                        }

                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }

                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Esempio n. 11
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            var clockPin = NusbioGpio.Gpio0;
            var dataPin  = NusbioGpio.Gpio1;

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);

                var mask = nusbio.GetGpioMask();

                byte EEPROM1_WR = 80; // 0xA0;

                _eeprom = new EEPROM_24LC256(nusbio, dataPin, clockPin);
                _eeprom.Begin(EEPROM1_WR);

                while (nusbio.Loop())
                {
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.W)
                        {
                            var a = ConsoleEx.Question(23, "Write 32k now Y)es, N)o", new List <char>()
                            {
                                'Y', 'N'
                            });
                            if (a == 'Y')
                            {
                                WriteEEPROMPage(512);
                            }
                        }

                        if (k == ConsoleKey.R)
                        {
                            ReadAndVerifyEEPROMPage(10);
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.A)
                        {
                            ReadAndVerifyEEPROMPage(512);
                        }
                        if (k == ConsoleKey.B)
                        {
                            ReadAndVerifyEEPROMPageInBatch(512);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }

                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }