Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #25 from thelsing/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
thelsing authored Apr 16, 2021
2 parents 7eb0d54 + 6f5bab6 commit 7dda7cf
Show file tree
Hide file tree
Showing 12 changed files with 33,022 additions and 13,748 deletions.
2 changes: 1 addition & 1 deletion App.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<applicationSettings>
<CreateKnxProd.Properties.Settings>
<setting name="ETSPath" serializeAs="String">
<value>C:\Program Files (x86)\ETS4</value>
<value>C:\Program Files (x86)\ETS5\CV\5.6.241.33672</value>
</setting>
<setting name="Culture" serializeAs="String">
<value>de</value>
Expand Down
5 changes: 5 additions & 0 deletions CreateKnxProd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -101,6 +102,10 @@
<Compile Include="Model\Parameter_t.cs" />
<Compile Include="Model\Xml.Project.cs" />
<Compile Include="RelayCommand.cs" />
<Compile Include="Signing\ApplicationProgramHasher.cs" />
<Compile Include="Signing\CatalogIdPatcher.cs" />
<Compile Include="Signing\HardwareSigner.cs" />
<Compile Include="Signing\XmlSigning.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
13 changes: 11 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Margin="3" Text="{lex:Loc Name}" Grid.Column="1" Grid.Row="0"/>
Expand All @@ -78,8 +81,14 @@
<ComboBoxItem Content="{lex:Loc TP}" Tag="MT-0"/>
<ComboBoxItem Content="{lex:Loc RF}" Tag="MT-2"/>
</ComboBox>
<TextBlock Margin="3" Text="{lex:Loc ReplacedVersion}" Grid.Column="1" Grid.Row="9"/>
<TextBox Margin="3" Grid.Column="2" Grid.Row="9" Text="{Binding ReplacedVersions}" />
<TextBlock Margin="3" Text="{lex:Loc ReplacesVersions}" Grid.Column="1" Grid.Row="9"/>
<TextBox Margin="3" Grid.Column="2" Grid.Row="9" Text="{Binding ReplacesVersions, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Margin="3" Text="{lex:Loc IsSecureEnabled}" Grid.Column="1" Grid.Row="10"/>
<CheckBox Margin="3" Grid.Column="2" Grid.Row="10" IsChecked="{Binding IsSecureEnabled}"/>
<TextBlock Margin="3" Text="{lex:Loc MaxSecurityIndividualAddressEntries}" Grid.Column="1" Grid.Row="11"/>
<TextBox Margin="3" Text="{Binding MaxSecurityIndividualAddressEntries, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="11" Width="200"/>
<TextBlock Margin="3" Text="{lex:Loc MaxSecurityGroupKeyTableEntries}" Grid.Column="1" Grid.Row="12"/>
<TextBox Margin="3" Text="{Binding MaxSecurityGroupKeyTableEntries, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Grid.Row="12" Width="200"/>
</Grid>
</TabItem>
<TabItem Header="{lex:Loc ParameterTypes}">
Expand Down
196 changes: 163 additions & 33 deletions MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CreateKnxProd.Extensions;
using CreateKnxProd.Model;
using CreateKnxProd.Model;
using CreateKnxProd.Properties;
using CreateKnxProd.Signing;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -11,10 +11,9 @@
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml;
using System.Xml.Serialization;
using System.IO.Compression;

namespace CreateKnxProd
{
Expand Down Expand Up @@ -70,7 +69,7 @@ private void DownloadKnxMasterXml()
if (!File.Exists("knx_master.xml"))
{
var client = new WebClient();
client.DownloadFile("https://update.knx.org/data/XML/project-11/knx_master.xml", "knx_master.xml");
client.DownloadFile("https://update.knx.org/data/XML/project-20/knx_master.xml", "knx_master.xml");
}
}
catch (Exception ex)
Expand Down Expand Up @@ -169,6 +168,9 @@ private void Save(object param)
if (_openFile == null)
SaveAs(param);

if (string.IsNullOrWhiteSpace(_applicationProgram.ReplacesVersions))
_applicationProgram.ReplacesVersions = null;

_product.RegistrationInfo = new RegistrationInfo_T() { RegistrationStatus = RegistrationStatus_T.Registered };
_hardware2Program.RegistrationInfo = new RegistrationInfo_T()
{
Expand Down Expand Up @@ -249,7 +251,7 @@ private void CreateLoadProcedures()
ldProc1.MergeId = 2;


var ldCtrlCreate = new LoadProcedure_TLdCtrlRelSegment();
var ldCtrlCreate = new LdCtrlRelSegment_T();
ldCtrlCreate.LsmIdx = 4;
ldCtrlCreate.Mode = 0;
ldCtrlCreate.Fill = 0;
Expand All @@ -260,7 +262,7 @@ private void CreateLoadProcedures()
var ldProc2 = new LoadProcedures_TLoadProcedure();
ldProc2.MergeId = 4;

var ldCtrlWrite = new LoadProcedure_TLdCtrlWriteRelMem();
var ldCtrlWrite = new LdCtrlWriteRelMem_T();
ldCtrlWrite.ObjIdx = 4;
ldCtrlWrite.Offset = 0;
ldCtrlWrite.Verify = true;
Expand All @@ -284,7 +286,7 @@ private void HandleParameters()
foreach (var item in Parameters)
{
appStatic.Parameters.Parameter.Add(item);
item.Memory = new Parameter_TMemory() { Offset = offset, BitOffset = 0 };
item.Memory = new MemoryParameter_T() { Offset = offset, BitOffset = 0 };
offset += item.Type.SizeInByte;
size += item.Type.SizeInByte;

Expand All @@ -299,7 +301,7 @@ private void RegenerateLoadProcedure()
var ldProc1 = new LoadProcedures_TLoadProcedure();
ldProc1.MergeId = 2;

var ldCtrlCreate = new LoadProcedure_TLdCtrlRelSegment();
var ldCtrlCreate = new LdCtrlRelSegment_T();
ldCtrlCreate.LsmIdx = 4;
ldCtrlCreate.Mode = 0;
ldCtrlCreate.Fill = 0;
Expand All @@ -309,7 +311,7 @@ private void RegenerateLoadProcedure()
var ldProc2 = new LoadProcedures_TLoadProcedure();
ldProc2.MergeId = 4;

var ldCtrlWrite = new LoadProcedure_TLdCtrlWriteRelMem();
var ldCtrlWrite = new LdCtrlWriteRelMem_T();
ldCtrlWrite.ObjIdx = 4;
ldCtrlWrite.Offset = 0;
ldCtrlWrite.Verify = true;
Expand All @@ -335,7 +337,7 @@ private void RegenerateDynamic()
appDynamic.ChannelIndependentBlock?.Clear();
appDynamic.Channel?.Clear();

var commonChannel = new ApplicationProgramDynamic_TChannelIndependentBlock();
var commonChannel = new ChannelIndependentBlock_T();
_parameterBlock = new ComObjectParameterBlock_T();
_parameterBlock.Name = "ParameterPage";
_parameterBlock.Text = Ressources.CommonParameters;
Expand Down Expand Up @@ -449,7 +451,11 @@ private void CreateNew(object param)
_applicationProgram.DefaultLanguage = lang;
_applicationProgram.DynamicTableManagement = false;
_applicationProgram.Linkable = false;
_applicationProgram.MinEtsVersion = ApplicationProgram_TMinEtsVersion.Item4Period0;
_applicationProgram.MinEtsVersion = "5.0";
_applicationProgram.ReplacesVersions = null;
_applicationProgram.IsSecureEnabled = false;
_applicationProgram.MaxSecurityIndividualAddressEntries = 32;
_applicationProgram.MaxSecurityGroupKeyTableEntries = 50;

var appStatic = new ApplicationProgramStatic_T();
_applicationProgram.Static = appStatic;
Expand Down Expand Up @@ -590,35 +596,105 @@ private void Export(object param)
if (cancel)
return;

// Remove all data secure related attributes
if (_model.ManufacturerData.First().ApplicationPrograms.First().IsSecureEnabled == false)
{
_model.ManufacturerData.First().ApplicationPrograms.First().MaxSecurityIndividualAddressEntries = 0;
_model.ManufacturerData.First().ApplicationPrograms.First().MaxSecurityGroupKeyTableEntries = 0;
}

var files = new string[] { _openFile };

var outputFile = _dialogService.ChooseSaveFile(".knxprod", "KNXProd|*.knxprod");
if (outputFile == null)
return;

var asmPath = Path.Combine(Properties.Settings.Default.ETSPath, "Knx.Ets.Converter.ConverterEngine.dll");
var asm = Assembly.LoadFrom(asmPath);
var eng = asm.GetType("Knx.Ets.Converter.ConverterEngine.ConverterEngine");
var bas = asm.GetType("Knx.Ets.Converter.ConverterEngine.ConvertBase");
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);

File.Copy("knx_master.xml", Path.Combine(tempDirectory, "knx_master.xml"));

string mfid = _model.ManufacturerData.First().RefId;
Directory.CreateDirectory(Path.Combine(tempDirectory, mfid));

KNX document = new KNX();
ManufacturerData_TManufacturer manufacturerData = new ManufacturerData_TManufacturer();

document.CreatedBy = _model.CreatedBy;
document.ToolVersion = _model.ToolVersion;
document.ManufacturerData.Add(manufacturerData);
document.ManufacturerData.First().RefId = _model.ManufacturerData.First().RefId;

manufacturerData.Catalog.Add(_model.ManufacturerData.First().Catalog.First());

XmlSerializer serializer = new XmlSerializer(typeof(KNX));

using (var xmlWriter = new StreamWriter(Path.Combine(tempDirectory, mfid, "Catalog.xml"), false, Encoding.UTF8))
{
serializer.Serialize(xmlWriter, document);
}

manufacturerData.Catalog.Clear();
manufacturerData.ApplicationPrograms.Add(_model.ManufacturerData.First().ApplicationPrograms.First());
string applicationProgramFilename = _model.ManufacturerData.First().ApplicationPrograms.First().Id + ".xml";

//ConvertBase.Uninitialize();
InvokeMethod(bas, "Uninitialize", null);
using (var xmlWriter = new StreamWriter(Path.Combine(tempDirectory, mfid, applicationProgramFilename), false, Encoding.UTF8))
{
serializer.Serialize(xmlWriter, document);
}

manufacturerData.ApplicationPrograms.Clear();
manufacturerData.Hardware.Add(_model.ManufacturerData.First().Hardware.First());

using (var xmlWriter = new StreamWriter(Path.Combine(tempDirectory, mfid, "Hardware.xml"), false, Encoding.UTF8))
{
serializer.Serialize(xmlWriter, document);
}

// Sign ApplicationProgram XML file and patch RefIds in ApplicationProgram XML file
IDictionary<string, string> applProgIdMappings = new Dictionary<string, string>();
IDictionary<string, string> applProgHashes = new Dictionary<string, string>();
IDictionary<string, string> mapBaggageIdToFileIntegrity = new Dictionary<string, string>(50);

FileInfo applProgFileInfo = new FileInfo(Path.Combine(tempDirectory, mfid, applicationProgramFilename));
FileInfo hwFileInfo = new FileInfo(Path.Combine(tempDirectory, mfid, "Hardware.xml"));
FileInfo catalogFileInfo = new FileInfo(Path.Combine(tempDirectory, mfid, "Catalog.xml"));

//var dset = ConverterEngine.BuildUpRawDocumentSet( files );
var dset = InvokeMethod(eng, "BuildUpRawDocumentSet", new object[] { files });
ApplicationProgramHasher aph = new ApplicationProgramHasher(applProgFileInfo, mapBaggageIdToFileIntegrity, true);
aph.HashFile();

//ConverterEngine.CheckOutputFileName(outputFile, ".knxprod");
InvokeMethod(eng, "CheckOutputFileName", new object[] { outputFile, ".knxprod" });
string oldApplProgId = aph.OldApplProgId;
string newApplProgId = aph.NewApplProgId;
string genHashString = aph.GeneratedHashString;

//ConvertBase.CleanUnregistered = false;
//SetProperty(bas, "CleanUnregistered", false);
/*
Console.WriteLine("OldApplProgId: " + oldApplProgId);
Console.WriteLine("NewApplProgId: " + newApplProgId);
Console.WriteLine("GeneratedHashString: " + genHashString);
Console.WriteLine("XML filename: " + applicationProgramFilename);
*/

//dset = ConverterEngine.ReOrganizeDocumentSet(dset);
dset = InvokeMethod(eng, "ReOrganizeDocumentSet", new object[] { dset });
applProgIdMappings.Add(oldApplProgId, newApplProgId);
if (!applProgHashes.ContainsKey(newApplProgId))
applProgHashes.Add(newApplProgId, genHashString);

//ConverterEngine.PersistDocumentSetAsXmlOutput(dset, outputFile, null, string.Empty, true, _toolName, _toolVersion);
InvokeMethod(eng, "PersistDocumentSetAsXmlOutput", new object[] { dset, outputFile, null,
"", true, _toolName, _toolVersion });
// Sign Hardware.xml and patch RefIds in Hardware.xml
HardwareSigner hws = new HardwareSigner(hwFileInfo, applProgIdMappings, applProgHashes, true);
hws.SignFile();
IDictionary<string, string> hardware2ProgramIdMapping = hws.OldNewIdMappings;

// Patch RefIds in Catalog.xml
CatalogIdPatcher cip = new CatalogIdPatcher(catalogFileInfo, hardware2ProgramIdMapping);
cip.Patch();

// Signing directory
XmlSigning.SignDirectory(Path.Combine(tempDirectory, mfid));

// Create ZIP file (aka knxprod) archive
ZipFile.CreateFromDirectory(tempDirectory, outputFile);

// Remove temporary working directory recursively
Directory.Delete(tempDirectory, true);

_dialogService.ShowMessage(Ressources.ExportSuccess);
}
Expand All @@ -643,7 +719,10 @@ private void RaiseChanged()
RaisePropertyChanged(nameof(Parameters));
RaisePropertyChanged(nameof(ComObjects));
RaisePropertyChanged(nameof(MediumType));
RaisePropertyChanged(nameof(ReplacedVersions));
RaisePropertyChanged(nameof(ReplacesVersions));
RaisePropertyChanged(nameof(IsSecureEnabled));
RaisePropertyChanged(nameof(MaxSecurityIndividualAddressEntries));
RaisePropertyChanged(nameof(MaxSecurityGroupKeyTableEntries));
}

#region Properties
Expand Down Expand Up @@ -754,7 +833,7 @@ public byte? ApplicationVersion
}
}

public string ReplacedVersions
public string ReplacesVersions
{
get
{
Expand All @@ -778,7 +857,58 @@ public string ReplacedVersions
// _applicationProgram.ReplacesVersions = null;
//}

RaisePropertyChanged(nameof(ReplacedVersions));
RaisePropertyChanged(nameof(ReplacesVersions));
}
}

public bool IsSecureEnabled
{
get
{
if (_applicationProgram == null)
return false;

return _applicationProgram.IsSecureEnabled;
}
set
{
_applicationProgram.IsSecureEnabled = value;

RaisePropertyChanged(nameof(IsSecureEnabled));
}
}

public ushort MaxSecurityIndividualAddressEntries
{
get
{
if (_applicationProgram == null)
return 0;

return _applicationProgram.MaxSecurityIndividualAddressEntries;
}
set
{
_applicationProgram.MaxSecurityIndividualAddressEntries = value;

RaisePropertyChanged(nameof(MaxSecurityIndividualAddressEntries));
}
}

public ushort MaxSecurityGroupKeyTableEntries
{
get
{
if (_applicationProgram == null)
return 0;

return _applicationProgram.MaxSecurityGroupKeyTableEntries;
}
set
{
_applicationProgram.MaxSecurityIndividualAddressEntries = value;

RaisePropertyChanged(nameof(MaxSecurityGroupKeyTableEntries));
}
}

Expand All @@ -787,7 +917,7 @@ public ObservableCollection<ParameterType_T> ParameterTypes
get => _applicationProgram?.Static?.ParameterTypes;
}

public ObservableCollection<Parameter_T> Parameters { get; private set; } = new ObservableCollection<Parameter_T>();
public ObservableCollection<ApplicationProgramStatic_TParametersParameter> Parameters { get; private set; } = new ObservableCollection<ApplicationProgramStatic_TParametersParameter>();

public ObservableCollection<ComObject_T> ComObjects
{
Expand Down
2 changes: 1 addition & 1 deletion Model/ParameterRef_t.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace CreateKnxProd.Model
public partial class ParameterRef_T : INotifyPropertyChanged
{
[XmlIgnore]
public Parameter_T Parameter { get; set; }
public ApplicationProgramStatic_TParametersParameter Parameter { get; set; }
}
}
2 changes: 1 addition & 1 deletion Model/Parameter_t.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace CreateKnxProd.Model
{
public partial class Parameter_T : INotifyPropertyChanged
public partial class ApplicationProgramStatic_TParametersParameter : INotifyPropertyChanged
{
[XmlIgnore]
public ParameterType_T Type { get; set; }
Expand Down
Loading

0 comments on commit 7dda7cf

Please sign in to comment.