private static SnmpTreeNode GenerateSnmpTreeNode(MibTreeNode mibTreeNode, SnmpTreeNode parentNode) { SnmpTreeNode result = new SnmpTreeNode(parentNode); result.Name = _alphaNumericRegex.Replace(mibTreeNode.Entity.Name, ""); result.Oid = mibTreeNode.Entity.Value; result.FullOid = MibTypesResolver.ResolveOid(mibTreeNode.Entity).GetOidString(); return(result); }
private void treeMib_AfterSelect(object sender, TreeViewEventArgs e) { listviewNodeDetails.Items.Clear(); if (e.Node != null) { MibTreeNode mtn = e.Node.Tag as MibTreeNode; if (mtn != null) { listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Abstract", mtn.NodeType.ToString() }, this.listviewgroupAbstract)); listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Module", (mtn.Entity.Module != null) ? mtn.Entity.Module.Name : "" }, this.listviewgroupElement)); listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Type", mtn.Entity.GetType().Name }, this.listviewgroupElement)); listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Name", mtn.Entity.Name }, this.listviewgroupElement)); listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Description", mtn.Entity.Description }, this.listviewgroupElement)); listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "OID", mtn.Entity.Value.ToString() }, this.listviewgroupElement)); listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Full OID", MibTypesResolver.ResolveOid(mtn.Entity).GetOidString() }, this.listviewgroupElement)); if (mtn.Entity is ObjectType) { listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Access", (mtn.Entity as ObjectType).Access.ToString() }, this.listviewgroupElement)); } ITypeReferrer tr = mtn.Entity as ITypeReferrer; if (tr != null) { ShowTypeDetails(listviewNodeDetails, this.listviewgroupBaseType, tr.BaseType); ShowTypeChain(listviewNodeDetails, tr.ReferredType); } } } }
static void Main(string[] args) { Console.WriteLine("lwIP MIB Compiler"); Console.WriteLine(""); // check args if ((args.Length < 2) || String.IsNullOrWhiteSpace(args[0]) || String.IsNullOrWhiteSpace(args[1])) { PrintUsage(); return; } string mibFile = args[0]; if (!File.Exists(mibFile)) { Console.WriteLine(String.Format("Unable to find file '{0}'!", mibFile)); } string destFile = args[1]; string destHeaderFile; if (Directory.Exists(destFile)) { // only directory passed -> create dest filename from mib filename string mibFileName = Path.GetFileNameWithoutExtension(mibFile).ToLowerInvariant(); destFile = Path.Combine(destFile, mibFileName + ".c"); } string destFileExt = Path.GetExtension(destFile); if (!String.IsNullOrEmpty(destFileExt)) { destHeaderFile = destFile.Substring(0, destFile.Length - destFileExt.Length); } else { destHeaderFile = destFile; } destHeaderFile += ".h"; for (int i = 2; i < args.Length; i++) { if (!String.IsNullOrWhiteSpace(args[i]) && Directory.Exists(args[i])) { MibTypesResolver.RegisterResolver(new FileSystemMibResolver(args[i], true)); } } // read and resolve MIB Console.WriteLine(" Reading MIB file..."); MibDocument md = new MibDocument(mibFile); MibTypesResolver.ResolveTypes(md.Modules[0]); MibTree mt = new MibTree(md.Modules[0] as MibModule); if (mt.Root.Count == 0) { Console.WriteLine("No root element found inside MIB!"); return; } MibCFile generatedFile = new MibCFile(); MibHeaderFile generatedHeaderFile = new MibHeaderFile(); foreach (MibTreeNode mibTreeNode in mt.Root) { // create LWIP object tree from MIB structure Console.WriteLine(" Creating lwIP object tree " + mibTreeNode.Entity.Name); SnmpMib snmpMib = new SnmpMib(); snmpMib.Oid = mibTreeNode.Entity.Value; snmpMib.BaseOid = MibTypesResolver.ResolveOid(mibTreeNode.Entity).GetOidValues(); snmpMib.Name = mibTreeNode.Entity.Name; ProcessMibTreeNode(mibTreeNode, snmpMib); // let the tree transform itself depending on node structure snmpMib.Analyze(); if (snmpMib.ChildNodes.Count != 0) { // generate code from LWIP object tree Console.WriteLine(" Generating code " + snmpMib.Name); snmpMib.Generate(generatedFile, generatedHeaderFile); } } string preservedCode = MibCFile.GetPreservedCode(destFile); if (!string.IsNullOrEmpty(preservedCode)) { generatedFile.PreservedCode.Add(new PlainText(preservedCode)); } else { generatedFile.PreservedCode.AddRange(generatedFile.Implementation); } generatedFile.Implementation.Clear(); using (StreamWriter fileWriter = new StreamWriter(destHeaderFile)) { CGenerator cGenerator = new CGenerator(fileWriter, destHeaderFile, 3, " ", Environment.NewLine); generatedHeaderFile.Save(cGenerator); } using (StreamWriter fileWriter = new StreamWriter(destFile)) { CGenerator cGenerator = new CGenerator(fileWriter, destFile, 3, " ", Environment.NewLine); generatedFile.Save(cGenerator); } Console.WriteLine(" Done"); }