Esempio n. 1
0
        // Basic parsing of avrdude.conf to get programmers & MCUs
        private void loadConfig(string confLoc)
        {
            string conf_loc = null;

            if (!String.IsNullOrEmpty(confLoc))
            {
                conf_loc = Path.Combine(confLoc, FILE_AVRDUDECONF);
            }
            else
            {
                // If on Unix check /etc/ and /usr/local/etc/ first
                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    conf_loc = "/etc/" + FILE_AVRDUDECONF;
                    if (!File.Exists(conf_loc))
                    {
                        conf_loc = "/usr/local/etc/" + FILE_AVRDUDECONF;
                        if (!File.Exists(conf_loc))
                        {
                            conf_loc = null;
                        }
                    }
                }

                if (conf_loc == null)
                {
                    conf_loc = Path.Combine(AssemblyData.directory, FILE_AVRDUDECONF);
                    if (!File.Exists(conf_loc))
                    {
                        conf_loc = Path.Combine(Directory.GetCurrentDirectory(), FILE_AVRDUDECONF);
                    }
                }
            }

            // Config file not found
            if (String.IsNullOrEmpty(conf_loc) || !File.Exists(conf_loc))
            {
                Util.consoleError("_AVRCONFMISSING", FILE_AVRDUDECONF);
                //throw new System.IO.FileNotFoundException("File is missing", FILE_AVRDUDECONF);
                return;
            }

            // Load config
            string[] lines;
            try
            {
                lines = File.ReadAllLines(conf_loc);
            }
            catch (Exception ex)
            {
                Util.consoleError("_AVRCONFREADERROR", FILE_AVRDUDECONF, ex.Message);
                return;
            }

            char[] trimChars = new char[3] {
                ' ', '"', ';'
            };

            string parentId  = null;
            string id        = null;
            string desc      = null;
            string signature = null;
            int    flash     = -1;
            int    eeprom    = -1;

            ParseMemType memType      = ParseMemType.None;
            bool         isProgrammer = false;

            for (int i = 0; i < lines.Length; i++)
            {
                string s = lines[i].Trim();

                bool lineProgrammer = s.StartsWith("programmer");
                bool linePart       = s.StartsWith("part");

                if (lineProgrammer || linePart)
                {
                    savePart(isProgrammer, parentId, id, desc, signature, flash, eeprom);

                    parentId  = null;
                    id        = null;
                    desc      = null;
                    signature = null;
                    flash     = -1;
                    eeprom    = -1;
                    memType   = ParseMemType.None;

                    // Get parent ID
                    string[] parts = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length > 2 && parts[1].Trim(trimChars) == "parent")
                    {
                        parentId = parts[2].Trim(trimChars);
                    }

                    isProgrammer = lineProgrammer;
                }
                else if (s == ";")
                {
                    memType = ParseMemType.None;
                }
                else
                {
                    int pos = s.IndexOf('=');
                    if (pos > -1)
                    {
                        string key = s.Substring(0, pos - 1).Trim();
                        string val = s.Substring(pos + 1).Trim(trimChars);

                        if (key == "id")
                        {
                            id = val;
                        }
                        else if (key == "desc")
                        {
                            desc = val;
                        }
                        else if (key == "signature")
                        {
                            signature = val;

                            // Remove 0x and spaces from signature (0xAA 0xAA 0xAA -> AAAAAA)
                            signature = signature.Replace("0x", "").Replace(" ", "");
                        }
                        else if (key == "size" && memType != ParseMemType.None)
                        {
                            // Parse to int
                            int memTmp = 0;
                            if (!int.TryParse(val, out memTmp))
                            {
                                // Probably hex
                                if (val.StartsWith("0x"))
                                {
                                    val = val.Substring(2); // Remove 0x
                                }
                                int.TryParse(val, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out memTmp);
                            }

                            if (memType == ParseMemType.Flash)
                            {
                                flash = memTmp;
                            }
                            else if (memType == ParseMemType.Eeprom)
                            {
                                eeprom = memTmp;
                            }

                            memType = ParseMemType.None;
                        }
                    }
                    else if (s.StartsWith("memory")) // Found memory section
                    {
                        pos = s.IndexOf('"');
                        if (pos > -1)
                        {
                            // Figure out memory type
                            string mem = s.Substring(pos - 1).Trim(trimChars).ToLower();
                            if (mem == "flash")
                            {
                                memType = ParseMemType.Flash;
                            }
                            else if (mem == "eeprom")
                            {
                                memType = ParseMemType.Eeprom;
                            }
                        }
                    }
                }
            }

            savePart(isProgrammer, parentId, id, desc, signature, flash, eeprom);
        }
Esempio n. 2
0
        // Basic parsing of avrdude.conf to get programmers & MCUs
        private void LoadConfig(string confLoc)
        {
            string conf_loc = null;

            if (!String.IsNullOrEmpty(confLoc))
            {
                conf_loc = Path.Combine(confLoc, FILE_AVRDUDECONF);
            }
            else
            {
                // If on Unix check /etc/ and /usr/local/etc/ first
                if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    conf_loc = "/etc/" + FILE_AVRDUDECONF;
                    if (!File.Exists(conf_loc))
                    {
                        conf_loc = "/usr/local/etc/" + FILE_AVRDUDECONF;
                        if (!File.Exists(conf_loc))
                        {
                            conf_loc = null;
                        }
                    }
                }

                if (conf_loc == null)
                {
                    conf_loc = Path.Combine(AssemblyData.directory, FILE_AVRDUDECONF);
                    if (!File.Exists(conf_loc))
                    {
                        conf_loc = Path.Combine(Directory.GetCurrentDirectory(), FILE_AVRDUDECONF);
                    }
                }
            }

            // Config file not found
            if (String.IsNullOrEmpty(conf_loc) || !File.Exists(conf_loc))
            {
                throw new System.IO.FileNotFoundException(FILE_AVRDUDECONF + " is not found in the application folder.");
                return;
            }

            // Load config
            string[] lines;
            try {
                lines = File.ReadAllLines(conf_loc);
            }
            catch (Exception ex) {
                MsgBox.error("Error reading " + FILE_AVRDUDECONF, ex);
                return;
            }

            char[] trimChars = new char[3] {
                ' ', '"', ';'
            };

            for (int i = 0; i < lines.Length - 3; i++)
            {
                string s = lines[i].Trim();

                bool isProgrammer = s.StartsWith("programmer");
                bool isPart       = s.StartsWith("part");
                if (!isPart && !isProgrammer)
                {
                    continue;
                }

                // Get parent ID
                string partentId = null;
                if (isPart && s.Contains("parent"))
                {
                    string[] parts = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length > 2)
                    {
                        partentId = parts[2].Trim(trimChars);
                    }
                }

                i++; // next line
                     // Does line have id key?
                int pos = lines[i].IndexOf('=');
                if (pos < 0 || lines[i].Substring(1, pos - 1).Trim() != "id")
                {
                    continue;
                }

                // Get ID value
                string id = lines[i].Substring(pos + 1).Trim(trimChars);

                i++; // next line
                     // Does line have desc key?
                pos = lines[i].IndexOf('=');
                if (pos < 0 || lines[i].Substring(1, pos - 1).Trim() != "desc")
                {
                    continue;
                }

                // Get description value
                string desc = lines[i].Substring(pos + 1).Trim(trimChars);

                // If its a programmer then add to programmers and go back to the top
                if (isProgrammer)
                {
                    _programmers.Add(new Programmer(id, desc));
                    continue;
                }

                // Otherwise its an MCU

                // Part is a common value thing or deprecated
                if (id.StartsWith(".") || desc.StartsWith("deprecated"))
                {
                    continue;
                }

                // Here we get the MCU signature, flash and EEPROM sizes

                string       signature = "";
                int          flash     = -1;
                int          eeprom    = -1;
                ParseMemType memType   = ParseMemType.None;

                // Loop through lines looking for "signature" and "memory"
                // Abort if "part" or "programmer" is found
                for ( ; i < lines.Length; i++)
                {
                    s = lines[i].Trim();

                    // Too far
                    if (s.StartsWith("part") || s.StartsWith("programmer"))
                    {
                        i--;
                        break;
                    }

                    // Found memory section
                    if (s.StartsWith("memory"))
                    {
                        pos = lines[i].IndexOf('"');
                        if (pos > -1)
                        {
                            // What type of memory is this?
                            string mem = lines[i].Substring(pos - 1).Trim(trimChars).ToLower();
                            if (mem == "flash")
                            {
                                memType = ParseMemType.Flash;
                            }
                            else if (mem == "eeprom")
                            {
                                memType = ParseMemType.Eeprom;
                            }
                        }
                    }
                    else if (memType != ParseMemType.None)
                    {
                        // See if this line defines the memory size
                        pos = lines[i].IndexOf('=');
                        if (pos > -1 && lines[i].Substring(1, pos - 1).Trim() == "size")
                        {
                            // Get size value
                            string memStr = lines[i].Substring(pos + 1).Trim(trimChars);

                            // Parse to int
                            int memTmp = 0;
                            if (!int.TryParse(memStr, out memTmp))
                            {
                                // Probably hex
                                if (memStr.StartsWith("0x"))
                                {
                                    memStr = memStr.Substring(2); // Remove 0x
                                }
                                int.TryParse(memStr, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out memTmp);
                            }

                            if (memType == ParseMemType.Flash)
                            {
                                flash = memTmp;
                            }
                            else if (memType == ParseMemType.Eeprom)
                            {
                                eeprom = memTmp;
                            }

                            memType = ParseMemType.None;
                        }
                    }

                    // Does line have signature key?
                    pos = lines[i].IndexOf('=');
                    if (pos > -1 && lines[i].Substring(1, pos - 1).Trim() == "signature")
                    {
                        // Get signature value
                        signature = lines[i].Substring(pos + 1).Trim(trimChars);

                        // Remove 0x and spaces from signature (0xAA 0xAA 0xAA -> AAAAAA)
                        signature = signature.Replace("0x", "").Replace(" ", "");
                    }
                }

                // Some formatting
                desc = desc.ToUpper().Replace("XMEGA", "xmega").Replace("MEGA", "mega").Replace("TINY", "tiny");

                // Find parent
                MCU parent = null;
                if (partentId != null)
                {
                    parent = _mcus.Find(m => m.name == partentId);
                }

                // Add to MCUs
                _mcus.Add(new MCU(id, desc, signature, flash, eeprom, parent));
            }
        }