-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProcessInclude.cs
More file actions
835 lines (771 loc) · 43.7 KB
/
ProcessInclude.cs
File metadata and controls
835 lines (771 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
namespace MultiplyChannels {
public class ProcessInclude {
const string cOwnNamespace = "http://github.com/mumpf/multiply-channels";
private class DefineContent
{
public string prefix;
public int KoOffset;
public string[] ReplaceKeys = {};
public string[] ReplaceValues = {};
public int NumChannels;
public int ModuleType;
public string header;
public bool IsTemplate;
public bool IsParameter;
private DefineContent(string iPrefix, string iHeader, int iKoOffset, int iNumChannels, string iReplaceKeys, string iReplaceValues, int iModuleType) {
prefix = iPrefix;
header = iHeader;
KoOffset = iKoOffset;
if (iReplaceKeys.Length > 0) {
ReplaceKeys = iReplaceKeys.Split(" ");
ReplaceValues = iReplaceValues.Split(" ");
}
NumChannels = iNumChannels;
ModuleType = iModuleType;
}
public static DefineContent Factory(XmlNode iDefineNode) {
DefineContent lResult;
int lChannelCount = 1;
int lKoOffset = 1;
string lPrefix = "";
string lHeader = "";
string lReplaceKeys = "";
string lReplaceValues = "";
int lModuleType = 1;
lPrefix = iDefineNode.Attributes.GetNamedItemValueOrEmpty("prefix");
if (lPrefix == "") lPrefix = "LOG"; // backward compatibility
if (sDefines.ContainsKey(lPrefix)) {
lResult = sDefines[lPrefix];
} else {
lHeader = iDefineNode.Attributes.GetNamedItemValueOrEmpty("header");
lChannelCount = int.Parse(iDefineNode.Attributes.GetNamedItemValueOrEmpty("NumChannels"));
lKoOffset = int.Parse(iDefineNode.Attributes.GetNamedItemValueOrEmpty("KoOffset"));
lReplaceKeys = iDefineNode.Attributes.GetNamedItemValueOrEmpty("ReplaceKeys");
lReplaceValues = iDefineNode.Attributes.GetNamedItemValueOrEmpty("ReplaceValues");
lModuleType = int.Parse(iDefineNode.Attributes.GetNamedItemValueOrEmpty("ModuleType"));
lResult = new DefineContent(lPrefix, lHeader, lKoOffset, lChannelCount, lReplaceKeys, lReplaceValues, lModuleType);
sDefines.Add(lPrefix, lResult);
}
return lResult;
}
static public DefineContent Empty = new DefineContent("LOG", "", 1, 1, "", "", 1);
public static DefineContent GetDefineContent(string iPrefix) {
DefineContent lResult;
if (sDefines.ContainsKey(iPrefix)) {
lResult = sDefines[iPrefix];
} else {
lResult = Empty;
}
return lResult;
}
}
private XmlNamespaceManager nsmgr;
private XmlDocument mDocument = new XmlDocument();
private bool mLoaded = false;
StringBuilder mHeaderGenerated = new StringBuilder();
private XmlNode mParameterTypesNode = null;
private static Dictionary<string, ProcessInclude> gIncludes = new Dictionary<string, ProcessInclude>();
private static Dictionary<string, DefineContent> sDefines = new Dictionary<string, DefineContent>();
private string mXmlFileName;
private string mHeaderFileName;
private string mHeaderPrefixName;
private bool mHeaderParameterStartGenerated;
private bool mHeaderParameterBlockGenerated;
private bool mHeaderKoStartGenerated;
private bool mHeaderKoBlockGenerated;
private int mChannelCount = 1;
private int mParameterBlockOffset = 0;
private int mParameterBlockSize = -1;
private int mKoOffset = 0;
private int mModuleType = 1;
private string[] mReplaceKeys = {};
private string[] mReplaceValues = {};
private int mKoBlockSize = 0;
public int ParameterBlockOffset {
get { return mParameterBlockOffset; }
set { mParameterBlockOffset = value; }
}
public int ParameterBlockSize {
get { return mParameterBlockSize; }
set { mParameterBlockSize = value; }
}
public int ChannelCount {
get { return mChannelCount; }
set { mChannelCount = value; }
}
public int KoOffset {
get { return mKoOffset; }
set { mKoOffset = value; }
}
public int ModuleType {
get { return mModuleType; }
set { mModuleType = value; }
}
public string[] ReplaceKeys {
get { return mReplaceKeys; }
set { mReplaceKeys = value; }
}
public string[] ReplaceValues {
get { return mReplaceValues; }
set { mReplaceValues = value; }
}
public string HeaderGenerated {
get {
mHeaderGenerated.Insert(0, "#pragma once\n#include <knx.h>\n\n");
return mHeaderGenerated.ToString();
}
}
public static ProcessInclude Factory(string iXmlFileName, string iHeaderFileName, string iHeaderPrefixName) {
ProcessInclude lInclude = null;
if (gIncludes.ContainsKey(iXmlFileName)) {
lInclude = gIncludes[iXmlFileName];
} else {
Console.WriteLine("Processing include {0}", iXmlFileName);
lInclude = new ProcessInclude(iXmlFileName, iHeaderFileName, iHeaderPrefixName);
gIncludes.Add(iXmlFileName, lInclude);
}
return lInclude;
}
private ProcessInclude(string iXmlFileName, string iHeaderFileName, string iHeaderPrefixName) {
mXmlFileName = iXmlFileName;
mHeaderFileName = iHeaderFileName;
if (iHeaderPrefixName != "" && !iHeaderPrefixName.EndsWith('_')) iHeaderPrefixName += "_";
mHeaderPrefixName = iHeaderPrefixName;
}
int GetHeaderParameter(string iHeaderFileContent, string iDefineName) {
string lPattern = "#define.*" + iDefineName + @"\s*(\d{1,4})";
Match m = Regex.Match(iHeaderFileContent, lPattern, RegexOptions.None);
int lResult = -1;
if (m.Groups.Count > 1) {
int.TryParse(m.Groups[1].Value, out lResult);
}
return lResult;
}
// bool ParseHeaderFile(string iHeaderFileName) {
// if (File.Exists(iHeaderFileName)) {
// StreamReader lHeaderFile = File.OpenText(iHeaderFileName);
// string lHeaderFileContent = lHeaderFile.ReadToEnd();
// lHeaderFile.Close();
// mChannelCount = GetHeaderParameter(lHeaderFileContent, mHeaderPrefixName + "Channels");
// mKoOffset = GetHeaderParameter(lHeaderFileContent, mHeaderPrefixName + "KoOffset");
// } else {
// mChannelCount = 1;
// mKoOffset = 1;
// }
// // mKoBlockSize = GetHeaderParameter(lHeaderFileContent, mHeaderPrefixName + "KoBlockSize");
// return (mChannelCount >= 0) && (mKoOffset > 0);
// }
public XmlNodeList SelectNodes(string iXPath) {
return mDocument.SelectNodes(iXPath);
}
static string CalculateId(int iApplicationNumber, int iApplicationVersion) {
return string.Format("-{0:X4}-{1:X2}-0000", iApplicationNumber, iApplicationVersion);
}
public XmlDocument GetDocument() {
return mDocument;
}
public void DocumentDebugOutput() {
mDocument.Save(Path.ChangeExtension(mXmlFileName, "out.xml"));
}
public void SetNamespace() {
// we restor the original namespace, if necessary
if (mDocument.DocumentElement.GetAttribute("xmlns") == "") {
string lXmlns = mDocument.DocumentElement.GetAttribute("oldxmlns");
if (lXmlns != "") mDocument.DocumentElement.SetAttribute("xmlns", lXmlns);
}
}
public string GetNamespace() {
return mDocument.DocumentElement.GetAttribute("xmlns");
}
public void Expand() {
// here we recursively process all includes and all channel repetitions
LoadAdvanced(mXmlFileName);
// we use here an empty DefineContent, just for startup
ExportHeader(DefineContent.Empty, mHeaderFileName, mHeaderPrefixName, this);
// finally we do all processing necessary for the whole (resolved) document
ProcessFinish(mDocument);
// DocumentDebugOutput();
}
string ReplaceChannelTemplate(string iValue, int iChannel) {
string lResult = iValue;
Match lMatch = Regex.Match(iValue, @"%(C{1,3})%");
if (lMatch.Captures.Count > 0) {
int lLen = lMatch.Groups[1].Value.Length;
string lFormat = string.Format("D{0}", lLen);
lResult = iValue.Replace(lMatch.Value, iChannel.ToString(lFormat));
}
lMatch = Regex.Match(lResult, @"%(Z{1,3})%");
if (lMatch.Captures.Count > 0)
{
int lLen = lMatch.Groups[1].Value.Length;
string channelName = "";
int temp_Channel = iChannel-1;
for (int i = 0; i < lLen; i++) {
if(temp_Channel >= 0) channelName = Convert.ToChar((temp_Channel) % 26 + 65) + channelName;
temp_Channel /= 26;
temp_Channel--;
}
lResult = iValue.Replace(lMatch.Value, channelName);
}
return lResult;
}
string ReplaceKoTemplate(string iValue, int iChannel, ProcessInclude iInclude) {
string lResult = iValue;
Match lMatch = Regex.Match(iValue, @"%K(\d{1,3})%");
int lBlockSize = 0;
int lOffset = 0;
if (iInclude != null) {
lBlockSize = iInclude.mKoBlockSize;
lOffset = iInclude.KoOffset;
}
// MatchCollection lMatches = Regex.Matches(iValue, @"%K(\d{1,3})%");
if (lMatch.Captures.Count > 0) {
int lShift = int.Parse(lMatch.Groups[1].Value);
lResult = iValue.Replace(lMatch.Value, ((iChannel - 1) * lBlockSize + lOffset + lShift).ToString());
}
return lResult;
}
void ProcessAttributes(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) {
foreach (XmlAttribute lAttr in iTargetNode.Attributes) {
lAttr.Value = lAttr.Value.Replace("%T%", iInclude.ModuleType.ToString());
lAttr.Value = ReplaceChannelTemplate(lAttr.Value, iChannel);
lAttr.Value = ReplaceKoTemplate(lAttr.Value, iChannel, iInclude);
// lAttr.Value = lAttr.Value.Replace("%N%", mChannelCount.ToString());
}
}
void ProcessParameter(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) {
//calculate new offset
XmlNode lMemory = iTargetNode.SelectSingleNode("Memory");
if (lMemory != null) {
XmlNode lAttr = lMemory.Attributes.GetNamedItem("Offset");
int lOffset = int.Parse(lAttr.Value);
if (iInclude.ChannelCount > 1) // parameters with single occurence are not used for template size processing // TODO: Relative single occuance parameters
lOffset += iInclude.ParameterBlockOffset + (iChannel - 1) * iInclude.ParameterBlockSize;
lAttr.Value = lOffset.ToString();
}
}
void ProcessUnion(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) {
//calculate new offset
ProcessParameter(iChannel, iTargetNode, iInclude);
XmlNodeList lChildren = iTargetNode.ChildNodes;
foreach (XmlNode lChild in lChildren) {
if (lChild.Name=="Parameter")
ProcessAttributes(iChannel, lChild, iInclude);
}
}
void ProcessChannel(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) {
//attributes of the node
if (iTargetNode.Attributes != null) {
ProcessAttributes(iChannel, iTargetNode, iInclude);
}
//Print individual children of the node, gets only direct children of the node
XmlNodeList lChildren = iTargetNode.ChildNodes;
foreach (XmlNode lChild in lChildren) {
ProcessChannel(iChannel, lChild, iInclude);
}
}
void ProcessTemplate(int iChannel, XmlNode iTargetNode, ProcessInclude iInclude) {
ProcessAttributes(iChannel, iTargetNode, iInclude);
if (iTargetNode.Name == "Parameter") {
ProcessParameter(iChannel, iTargetNode, iInclude);
} else
if (iTargetNode.Name == "Union") {
ProcessUnion(iChannel, iTargetNode, iInclude);
} else
if (iTargetNode.Name == "Channel" || iTargetNode.Name == "ParameterBlock" || iTargetNode.Name == "choose") {
ProcessChannel(iChannel, iTargetNode, iInclude);
}
}
void ProcessIncludeFinish(XmlNode iTargetNode) {
// set number of Channels
XmlNodeList lNodes = iTargetNode.SelectNodes("//*[@Value='%N%']");
foreach (XmlNode lNode in lNodes) {
lNode.Attributes.GetNamedItem("Value").Value = mChannelCount.ToString();
}
lNodes = iTargetNode.SelectNodes("//*[@maxInclusive='%N%']");
foreach (XmlNode lNode in lNodes) {
lNode.Attributes.GetNamedItem("maxInclusive").Value = mChannelCount.ToString();
}
// // set the max channel value
// ReplaceDocumentStrings(mDocument, "%N%", mChannelCount.ToString());
}
void ReplaceDocumentStrings(XmlNodeList iNodeList, string iSourceText, string iTargetText) {
foreach (XmlNode lNode in iNodeList) {
if (lNode.Attributes != null) {
foreach (XmlNode lAttribute in lNode.Attributes) {
lAttribute.Value = lAttribute.Value.ToString().Replace(iSourceText, iTargetText);
}
}
ReplaceDocumentStrings(lNode, iSourceText, iTargetText);
}
}
void ReplaceDocumentStrings(XmlNode iNode, string iSourceText, string iTargetText) {
ReplaceDocumentStrings(iNode.ChildNodes, iSourceText, iTargetText);
}
void ReplaceDocumentStrings(string iSourceText, string iTargetText) {
ReplaceDocumentStrings(mDocument.ChildNodes, iSourceText, iTargetText);
}
void ProcessFinish(XmlNode iTargetNode) {
XmlNode lApplicationProgramNode = iTargetNode.SelectSingleNode("/KNX/ManufacturerData/Manufacturer/ApplicationPrograms/ApplicationProgram");
// evaluate mc:version, if available
XmlNode lMcVersionNode = iTargetNode.SelectSingleNode("//mc:version", nsmgr);
string lInlineData = "";
if (lMcVersionNode != null) {
// found mc:version, we apply its attributes to knxprod-xml
int lOpenKnxId = Convert.ToInt32(lMcVersionNode.Attributes.GetNamedItem("OpenKnxId").Value, 16);
int lAppNumber = Convert.ToInt32(lMcVersionNode.Attributes.GetNamedItem("ApplicationNumber").Value, 10);
int lCalcAppNumber = (lAppNumber + (lOpenKnxId << 8));
lApplicationProgramNode.Attributes.GetNamedItem("ApplicationNumber").Value = lCalcAppNumber.ToString();
int lAppVersion = Convert.ToInt32(lMcVersionNode.Attributes.GetNamedItem("ApplicationVersion").Value, 10);
lApplicationProgramNode.Attributes.GetNamedItem("ApplicationVersion").Value = lAppVersion.ToString();
string lReplVersions = lMcVersionNode.Attributes.GetNamedItem("ReplacesVersions").Value;
lApplicationProgramNode.Attributes.GetNamedItem("ReplacesVersions").Value = lReplVersions;;
int lAppRevision = Convert.ToInt32(lMcVersionNode.Attributes.GetNamedItem("ApplicationRevision").Value, 10);
// now we calculate according versioning verification string
lInlineData = string.Format("0000{0:X4}{1:X2}00", lCalcAppNumber, lAppVersion-lAppRevision);
XmlNode lLdCtrlCompareProp = iTargetNode.SelectSingleNode("//LdCtrlCompareProp");
if (lLdCtrlCompareProp != null) {
lLdCtrlCompareProp.Attributes.GetNamedItem("InlineData").Value = lInlineData;
}
// we create a comment from versio node
string lVersion = " " + string.Join(" ", lMcVersionNode.OuterXml.Split().Skip(1).SkipLast(2)) + " ";
XmlNode lVersionComment = ((XmlDocument)iTargetNode).CreateComment(lVersion);
lMcVersionNode.ParentNode.ReplaceChild(lVersionComment, lMcVersionNode);
// finally, we make some version info available in header file
mHeaderGenerated.AppendFormat("#define MAIN_OpenKnxId 0x{0:X2}", lOpenKnxId);
mHeaderGenerated.AppendLine();
mHeaderGenerated.AppendFormat("#define MAIN_ApplicationNumber {0}", lAppNumber);
mHeaderGenerated.AppendLine();
mHeaderGenerated.AppendFormat("#define MAIN_ApplicationVersion {0}", lAppVersion-lAppRevision);
mHeaderGenerated.AppendLine();
}
// set the right Size attributes
XmlNodeList lNodes = iTargetNode.SelectNodes("(//RelativeSegment | //LdCtrlRelSegment | //LdCtrlWriteRelMem)[@Size]");
// string lSize = (mChannelCount * mParameterBlockSize + mParameterBlockOffset).ToString();
string lSize = mParameterBlockSize.ToString();
foreach (XmlNode lNode in lNodes) {
lNode.Attributes.GetNamedItem("Size").Value = lSize;
}
Console.WriteLine("- Final parameter size is {0}", lSize);
// change all Id-Attributes / renumber ParameterSeparator and ParameterBlock
string lApplicationId = lApplicationProgramNode.Attributes.GetNamedItem("Id").Value;
int lApplicationNumber = -1;
bool lIsInt = int.TryParse(lApplicationProgramNode.Attributes.GetNamedItem("ApplicationNumber").Value, out lApplicationNumber);
int lApplicationVersion = -1;
lIsInt = int.TryParse(lApplicationProgramNode.Attributes.GetNamedItem("ApplicationVersion").Value, out lApplicationVersion);
XmlNode lReplacesVersionsAttribute = lApplicationProgramNode.Attributes.GetNamedItem("ReplacesVersions");
string lOldId = lApplicationId;//.Replace("M-00FA_A", ""); // CalculateId(1, 1);
if(lOldId.StartsWith("M-")) lOldId = lOldId.Substring(8);
string lNewId = CalculateId(lApplicationNumber, lApplicationVersion);
int lParameterSeparatorCount = 1;
int lParameterBlockCount = 1;
XmlNodeList lAttrs = iTargetNode.SelectNodes("//*/@*[string-length() > '13']");
foreach (XmlNode lAttr in lAttrs) {
if (lAttr.Value != null) {
lAttr.Value = lAttr.Value.Replace(lOldId, lNewId);
// ParameterSeparator is renumbered
if (lAttr.Value.Contains("_PS-")) {
lAttr.Value = string.Format("{0}-{1}", lAttr.Value.Substring(0, lAttr.Value.LastIndexOf('-')), lParameterSeparatorCount);
lParameterSeparatorCount += 1;
}
// ParameterBlock is renumbered
if (lAttr.Value.Contains("_PB-")) {
lParameterBlockCount = RenumberParameterBlock(lParameterBlockCount, lAttr);
}
}
}
Console.WriteLine("- ApplicationNumber: {0}, ApplicationVersion: {1}, old ID is: {3}, new (calculated) ID is: {2}", lApplicationNumber, lApplicationVersion, lNewId, lOldId);
if (lInlineData != "") Console.WriteLine("- Calculated InlineData for Versioning: {0}", lInlineData);
// create registration entry
XmlNode lHardwareVersionAttribute = iTargetNode.SelectSingleNode("/KNX/ManufacturerData/Manufacturer/Hardware/Hardware/@VersionNumber");
int lHardwareVersion = 1;
int.TryParse(lHardwareVersionAttribute.Value, out lHardwareVersion);
XmlNode lRegistrationNumber = iTargetNode.SelectSingleNode("/KNX/ManufacturerData/Manufacturer/Hardware/Hardware/Hardware2Programs/Hardware2Program/RegistrationInfo/@RegistrationNumber");
if (lRegistrationNumber == null) {
Console.WriteLine("- Missing 'RegistrationVersion', no updates via 'ReplacesVersion' in ETS possible!");
} else {
lRegistrationNumber.Value = string.Format("0001/{0}{1}", lHardwareVersion, lApplicationVersion);
Console.WriteLine("- RegistrationVersion is: {0}", lRegistrationNumber.Value);
}
// Add ReplacesVersions
if (lReplacesVersionsAttribute != null) {
string lReplacesVersions = lReplacesVersionsAttribute.Value;
Console.WriteLine("- ReplacesVersions entry is: {0}", lReplacesVersions);
// string lOldVerion = string.Format(" {0}", lApplicationVersion - 1);
// if (!lReplacesVersions.Contains(lOldVerion) && lReplacesVersions != (lApplicationVersion - 1).ToString()) lReplacesVersionsAttribute.Value += lOldVerion;
}
}
Dictionary<string, string> mParameterBlockMap = new Dictionary<string, string>();
string mLastParameterBlockId = "";
private int RenumberParameterBlock(int lParameterBlockCount, XmlNode lAttr) {
// for inline parameter blocks like grid and table we need
// the same PB number in all subblocks.
// we assume, that the iterator first provides the PB and lateron the subPB,
// before the next PB is offered.
int lPos = lAttr.Value.IndexOf("_PB-");
string lValue = "";
if (lAttr.Value.Substring(lPos+4).Contains("_")) {
// this is a subblock, we assume, that its main block was already renumbered
lValue = lAttr.Value.Substring(0, lAttr.Value.LastIndexOf("_"));
// lAttr.Value = lAttr.Value.Replace(lValue, mParameterBlockMap[lValue]);
lAttr.Value = lAttr.Value.Replace(lValue, mLastParameterBlockId);
} else {
// it is a main block, renumber it and store the result
lValue = string.Format("{0}-{1}", lAttr.Value.Substring(0, lAttr.Value.LastIndexOf('-')), lParameterBlockCount);
// if (mParameterBlockMap.ContainsKey(lAttr.Value)) {
// // number collision? Further checks should find this out
// } else {
// mParameterBlockMap.Add(lAttr.Value, lValue);
// }
mLastParameterBlockId = lValue;
lAttr.Value = lValue;
lParameterBlockCount += 1;
}
return lParameterBlockCount;
}
public int CalcParamSize(XmlNode iParameter, XmlNode iParameterTypesNode) {
int lResult = 0;
if (iParameterTypesNode != null) {
// we calcucalte the size only, if the parameter uses some memory in the device storage
XmlNode lMemory = iParameter.SelectSingleNode("Memory");
if (lMemory != null) {
XmlNode lSizeNode = null;
XmlNode lSizeInBitAttribute = null;
if (iParameter.Name == "Union") {
lSizeNode = iParameter;
lSizeInBitAttribute = lSizeNode.Attributes.GetNamedItem("SizeInBit");
} else {
string lParameterTypeId = iParameter.NodeAttr("ParameterType");
lSizeNode = iParameterTypesNode.SelectSingleNode(string.Format("ParameterType[@Id='{0}']", lParameterTypeId));
if (lSizeNode != null) lSizeInBitAttribute = lSizeNode.SelectSingleNode("*/@SizeInBit");
}
if (lSizeNode != null) {
if (lSizeInBitAttribute != null) {
lResult = 8;
bool lIsInt = int.TryParse(lSizeInBitAttribute.Value, out lResult);
lResult = (lResult - 1) / 8 + 1;
if (!lIsInt) {
Console.WriteLine("Parse error in include {0} in line {1}", mXmlFileName, lSizeNode.InnerXml);
}
} else if (lSizeNode.SelectSingleNode("TypeFloat") != null) {
lResult = 2;
} else if (lSizeNode.SelectSingleNode("TypeColor") != null) {
lResult = 3;
}
}
}
}
return lResult;
}
public int CalcParamSize(XmlNodeList iParameterList, XmlNode iParameterTypesNode) {
int lResult = 0;
foreach (XmlNode lNode in iParameterList) {
int lSize = CalcParamSize(lNode, iParameterTypesNode);
if (lSize > 0) {
// at this point we know there is a memory reference, we look at the offset
XmlNode lOffset = lNode.SelectSingleNode("*/@Offset");
lResult = Math.Max(lResult, int.Parse(lOffset.Value) + lSize);
}
}
return lResult;
}
private string ReplaceChannelName(string iName) {
string lResult = iName;
// if (iName.Contains("%C%")) lResult = iName.Remove(0, iName.IndexOf("%C%") + 3);
lResult = iName.Replace("%C%", "");
lResult = lResult.Replace(" ", "_");
return lResult;
}
private void ExportHeaderKoStart(DefineContent iDefine, StringBuilder cOut, string iHeaderPrefixName) {
if (!mHeaderKoStartGenerated) {
StringBuilder lOut = new StringBuilder();
mHeaderKoStartGenerated = ExportHeaderKo(lOut, iHeaderPrefixName);
if (mHeaderKoStartGenerated && iDefine.IsParameter) {
cOut.AppendLine("// Communication objects with single occurance");
cOut.Append(lOut);
}
}
}
private void ExportHeaderKoBlock(DefineContent iDefine, StringBuilder cOut, string iHeaderPrefixName) {
if (!mHeaderKoBlockGenerated) {
XmlNodeList lComObjects = mDocument.SelectNodes("//ComObjectTable/ComObject");
mKoBlockSize = lComObjects.Count;
StringBuilder lOut = new StringBuilder();
mHeaderKoBlockGenerated = ExportHeaderKo(lOut, iHeaderPrefixName);
if (mHeaderKoBlockGenerated) {
if (iDefine.IsTemplate) {
cOut.AppendLine("// Communication objects per channel (multiple occurance)");
cOut.AppendFormat("#define {0}KoOffset {1}", iHeaderPrefixName, mKoOffset);
cOut.AppendLine();
cOut.AppendFormat("#define {0}KoBlockSize {1}", iHeaderPrefixName, mKoBlockSize);
cOut.AppendLine();
}
cOut.Append(lOut);
}
}
}
public bool ExportHeaderKo(StringBuilder cOut, string iHeaderPrefixName) {
XmlNodeList lNodes = mDocument.SelectNodes("//ComObject");
bool lResult = false;
foreach (XmlNode lNode in lNodes) {
string lNumber = ReplaceKoTemplate(lNode.Attributes.GetNamedItemValueOrEmpty("Number"), 1, null);
cOut.AppendFormat("#define {0}Ko{1} {2}", iHeaderPrefixName, ReplaceChannelName(lNode.NodeAttr("Name")), lNumber);
cOut.AppendLine();
lResult = true;
}
if (lResult) cOut.AppendLine();
return lResult;
}
private void ExportHeaderParameterStart(DefineContent iDefine, StringBuilder cOut, XmlNode iParameterTypesNode, string iHeaderPrefixName) {
if (!mHeaderParameterStartGenerated && iDefine.IsParameter) {
cOut.AppendLine("// Parameter with single occurance");
ExportHeaderParameter(cOut, iParameterTypesNode, iHeaderPrefixName);
mHeaderParameterStartGenerated = true;
}
}
private void ExportHeaderParameterBlock(DefineContent iDefine, StringBuilder cOut, XmlNode iParameterTypesNode, string iHeaderPrefixName) {
if (!mHeaderParameterBlockGenerated) {
if (iDefine.IsTemplate) {
cOut.AppendFormat("#define {0}ChannelCount {1}", iHeaderPrefixName, mChannelCount);
cOut.AppendLine();
cOut.AppendLine();
cOut.AppendLine("// Parameter per channel");
cOut.AppendFormat("#define {0}ParamBlockOffset {1}", iHeaderPrefixName, mParameterBlockOffset);
cOut.AppendLine();
cOut.AppendFormat("#define {0}ParamBlockSize {1}", iHeaderPrefixName, mParameterBlockSize);
cOut.AppendLine();
}
int lSize = ExportHeaderParameter(cOut, iParameterTypesNode, iHeaderPrefixName);
// if (lSize != mParameterBlockSize) throw new ArgumentException(string.Format("ParameterBlockSize {0} calculation differs from header filie calculated ParameterBlockSize {1}", mParameterBlockSize, lSize));
mHeaderParameterBlockGenerated = true;
}
}
public int ExportHeaderParameter(StringBuilder cOut, XmlNode iParameterTypesNode, string iHeaderPrefixName) {
int lMaxSize = 0;
XmlNodeList lNodes = mDocument.SelectNodes("//Parameter");
foreach (XmlNode lNode in lNodes) {
XmlNode lMemoryNode;
string lName = lNode.Attributes.GetNamedItem("Name").Value;
lName = ReplaceChannelName(lName);
lMemoryNode = lNode.ParentNode;
if (lMemoryNode != null && lMemoryNode.Name!="Union") {
lMemoryNode = lNode;
}
XmlNode lMemory = lMemoryNode.FirstChild;
while (lMemory != null && lMemory.NodeType == XmlNodeType.Comment) lMemory = lMemory.NextSibling;
if (lMemory != null && iParameterTypesNode != null) {
// parse parameter type to fill additional information
string lParameterTypeId = lNode.NodeAttr("ParameterType");
XmlNode lParameterType = iParameterTypesNode.SelectSingleNode(string.Format("//ParameterType[@Id='{0}']", lParameterTypeId));
XmlNode lTypeNumber = null;
if (lParameterType != null) lTypeNumber = lParameterType.FirstChild;
while (lTypeNumber != null && lTypeNumber.NodeType == XmlNodeType.Comment) lTypeNumber = lTypeNumber.NextSibling;
int lBits = 0;
string lType = "";
bool lDirectType = false;
if (lTypeNumber != null) {
XmlNode lBitsAttribute = lTypeNumber.Attributes.GetNamedItem("SizeInBit");
if (lBitsAttribute != null) lBits = int.Parse(lBitsAttribute.Value);
XmlNode lTypeAttribute = lTypeNumber.Attributes.GetNamedItem("Type");
if (lTypeAttribute != null) {
lType = lTypeAttribute.Value;
lType = (lType == "signedInt") ? "int" : (lType == "unsignedInt") ? "uint" : "xxx";
} else {
lType = "enum";
if (lBits > 8) {
lType = string.Format("char*, {0} Byte", lBits / 8);
lDirectType = true;
}
}
if (lTypeNumber.Name == "TypeFloat") {
lType = "float";
lBits = 16;
lDirectType = true;
}
if (lTypeNumber.Name == "TypeColor") {
lType = "color, uint, 3 Byte";
lBits = 24;
lDirectType = true;
}
}
int lOffset = int.Parse(lMemory.Attributes.GetNamedItem("Offset").Value);
int lBitOffset = int.Parse(lMemory.Attributes.GetNamedItem("BitOffset").Value);
// Offset and BitOffset might be also defined in Parameter
XmlNode lParamOffsetNode = lNode.Attributes.GetNamedItem("Offset");
if (lParamOffsetNode != null) lOffset += int.Parse(lParamOffsetNode.Value);
XmlNode lParamBitOffsetNode = lNode.Attributes.GetNamedItem("BitOffset");
if (lParamBitOffsetNode != null) lBitOffset += int.Parse(lParamBitOffsetNode.Value);
lMaxSize = Math.Max(lMaxSize, lOffset + (lBits - 1) / 8 + 1);
if (lBits <= 7 || lType == "enum") {
//output for bit based parameters
int lShift = (8 - lBits - lBitOffset);
lType = string.Format("{0} Bit{1}, Bit {2}", lBits, (lBits == 1) ? "" : "s", (7 - lBitOffset));
if (lBits > 1) lType = string.Format("{0}-{1}", lType, lShift);
cOut.AppendFormat("#define {3}{0,-25} {1,2} // {2}", lName, lOffset, lType, iHeaderPrefixName);
if (lBits < 7 && lShift >= 0) {
cOut.AppendLine();
int lMask = ((int)Math.Pow(2, lBits) - 1) << lShift;
cOut.AppendFormat("#define {0}{1}Mask 0x{2:X2}", iHeaderPrefixName, lName, lMask);
cOut.AppendLine();
cOut.AppendFormat("#define {0}{1}Shift {2}", iHeaderPrefixName, lName, lShift);
}
} else if (lDirectType) {
cOut.AppendFormat("#define {3}{0,-25} {1,2} // {2}", lName, lOffset, lType, iHeaderPrefixName);
} else {
cOut.AppendFormat("#define {4}{0,-25} {1,2} // {3}{2}_t", lName, lOffset, lBits, lType, iHeaderPrefixName);
}
cOut.AppendLine();
}
}
cOut.AppendLine();
return lMaxSize;
}
/// <summary>
/// Load xml document from file resolving xincludes recursivly
/// </summary>
public void LoadAdvanced(string iFileName) {
if (!mLoaded) {
string lCurrentDir = Path.GetDirectoryName(Path.GetFullPath(iFileName));
string lFileData = File.ReadAllText(iFileName);
if (lFileData.Contains("oldxmlns")) {
// we get rid of default namespace, we already have an original (this file was already processed by our processor)
int lStart = lFileData.IndexOf(" xmlns=\"");
if (lStart < 0) {
lFileData = lFileData.Replace("oldxmlns", "xmlns");
} else {
// int lEnd = lFileData.IndexOf("\"", lStart + 8) + 1;
lFileData = lFileData.Remove(lStart, 38);
// lFileData = lFileData.Substring(0, lStart) + lFileData.Substring(lEnd);
}
} else {
// we get rid of default namespace, but remember the original
lFileData = lFileData.Replace(" xmlns=\"", " oldxmlns=\"");
}
using (StringReader sr = new StringReader(lFileData)) {
mDocument.Load(sr);
mLoaded = true;
ResolveIncludes(lCurrentDir);
}
}
}
/// <summary>
/// Resolves Includes inside xml document
/// </summary>
/// <param name="iCurrentDir">Directory to use for relative href expressions</param>
public void ResolveIncludes(string iCurrentDir) {
nsmgr = new XmlNamespaceManager(mDocument.NameTable);
nsmgr.AddNamespace("mc", cOwnNamespace);
// process define node
XmlNodeList lDefineNodes = mDocument.SelectNodes("//mc:define", nsmgr);
if (lDefineNodes != null && lDefineNodes.Count > 0) {
foreach (XmlNode lDefineNode in lDefineNodes)
DefineContent.Factory(lDefineNode);
}
//find all XIncludes in a copy of the document
XmlNodeList lIncludeNodes = mDocument.SelectNodes("//mc:include", nsmgr); // get all <include> nodes
foreach (XmlNode lIncludeNode in lIncludeNodes)
// try
{
//Load document...
string lIncludeName = lIncludeNode.Attributes.GetNamedItemValueOrEmpty("href");
string lHeaderPrefixName = lIncludeNode.Attributes.GetNamedItemValueOrEmpty("prefix");
DefineContent lDefine = DefineContent.GetDefineContent(lHeaderPrefixName);
lDefine.IsTemplate = (lIncludeNode.Attributes.GetNamedItemValueOrEmpty("type") == "template");
lDefine.IsParameter = (lIncludeNode.Attributes.GetNamedItemValueOrEmpty("type") == "parameter");
ProcessInclude lInclude = ProcessInclude.Factory(lIncludeName, lDefine.header, lHeaderPrefixName);
string lTargetPath = Path.Combine(iCurrentDir, lIncludeName);
lInclude.LoadAdvanced(lTargetPath);
lHeaderPrefixName = lInclude.mHeaderPrefixName;
lDefine = DefineContent.GetDefineContent(lHeaderPrefixName.Trim('_'));
//...find include in real document...
XmlNode lParent = lIncludeNode.ParentNode;
string lXPath = lIncludeNode.Attributes.GetNamedItemValueOrEmpty("xpath");
XmlNodeList lChildren = lInclude.SelectNodes(lXPath);
string lHeaderFileName = Path.Combine(iCurrentDir, lDefine.header);
lInclude.ModuleType = lDefine.ModuleType;
if (lChildren.Count > 0 && "Parameter | Union | ComObject | SNIPPET".Contains(lChildren[0].LocalName)) {
if (lDefine.IsTemplate) {
// at this point we are including a template file
// ChannelCount and KoOffset are taken from correct prefix
lInclude.ChannelCount = lDefine.NumChannels;
lInclude.KoOffset = lDefine.KoOffset;
lInclude.ReplaceKeys = lDefine.ReplaceKeys;
lInclude.ReplaceValues = lDefine.ReplaceValues;
ExportHeader(lDefine, lHeaderFileName, lHeaderPrefixName, lInclude, lChildren);
}
else if (lDefine.IsParameter) {
ExportHeader(lDefine, lHeaderFileName, lHeaderPrefixName, lInclude, lChildren);
}
}
// here we do template processing and repeat the template as many times as
// the Channels parameter in header file
for (int lChannel = 1; lChannel <= lInclude.ChannelCount; lChannel++) {
foreach (XmlNode lChild in lChildren) {
if (lChild.LocalName != "SNIPPET") {
//necessary for move between XmlDocument contexts
XmlNode lImportNode = lParent.OwnerDocument.ImportNode(lChild, true);
// for any Parameter node we do offset recalculation
// if there is no prefixname, we do no template replacement
if (lHeaderPrefixName != "") ProcessTemplate(lChannel, lImportNode, lInclude);
lParent.InsertBefore(lImportNode, lIncludeNode);
}
}
}
lParent.RemoveChild(lIncludeNode);
if (lInclude.ChannelCount > 1) ReplaceDocumentStrings("%N%", lInclude.ChannelCount.ToString());
// we replace also all additional replace key value paris
for (int lCount = 0; lCount < lInclude.ReplaceKeys.Length; lCount++)
{
ReplaceDocumentStrings(mDocument, lInclude.ReplaceKeys[lCount], lInclude.ReplaceValues[lCount]);
}
// if (lHeaderPrefixName != "") ProcessIncludeFinish(lChildren);
//if this fails, something is wrong
}
if (lDefineNodes != null && lDefineNodes.Count > 0) {
foreach (XmlNode lDefineNode in lDefineNodes)
{
lDefineNode.ParentNode.RemoveChild(lDefineNode);
}
}
// catch { }
}
private void ExportHeader(DefineContent iDefine, string iHeaderFileName, string iHeaderPrefixName, ProcessInclude iInclude, XmlNodeList iChildren = null) {
// iInclude.ParseHeaderFile(iHeaderFileName);
if (mParameterTypesNode == null) {
// before we start with template processing, we calculate all Parameter relevant info
mParameterTypesNode = mDocument.SelectSingleNode("//ParameterTypes");
}
if (mParameterTypesNode != null) {
// the main document contains necessary ParameterTypes definitions
// there are new parameters in include, we have to calculate a new parameter offset
XmlNodeList lParameterNodes = mDocument.SelectNodes("//Parameters/Parameter|//Parameters/Union");
if (lParameterNodes != null) {
mParameterBlockSize = CalcParamSize(lParameterNodes, mParameterTypesNode);
}
if (iChildren != null) {
// ... and we do parameter processing, so we calculate ParamBlockSize for this include
int lBlockSize = iInclude.CalcParamSize(iChildren, mParameterTypesNode);
if (lBlockSize > 0) {
iInclude.ParameterBlockSize = lBlockSize;
// we calculate also ParamOffset
iInclude.ParameterBlockOffset = mParameterBlockSize;
}
}
}
// Header file generation is only possible before we resolve includes
// First we serialize local parameters of this instance
ExportHeaderParameterStart(iDefine, mHeaderGenerated, mParameterTypesNode, iHeaderPrefixName);
// followed by template parameters of the include
if (iInclude != this) iInclude.ExportHeaderParameterBlock(iDefine, mHeaderGenerated, mParameterTypesNode, iHeaderPrefixName);
ExportHeaderKoStart(iDefine, mHeaderGenerated, iHeaderPrefixName);
if (iInclude != this) iInclude.ExportHeaderKoBlock(iDefine, mHeaderGenerated, iHeaderPrefixName);
}
}
}