forked from jugglingcats/tachograph-reader
/
DataFile.cs
840 lines (704 loc) · 22.7 KB
/
DataFile.cs
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
835
836
837
838
839
840
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using DataFileReader;
namespace DataFileReader
{
/// <summary>
/// The core class that can read a configuration describing the tachograph data structure (one
/// for driver cards, one for vehicle cards), then read the file itself into memory, then finally
/// write the data as XML.
/// </summary>
public class DataFile : Region
{
public ArrayList regions=new ArrayList();
/// <summary>
/// This method loads the XML config file into a new instance
/// of VuDataFile, prior to processing.
/// </summary>
/// <param name="configFile">Config to load</param>
/// <returns>A new instance ready for processing</returns>
//
public static DataFile Create(string configFile)
{
XmlTextReader xtr=new XmlTextReader(configFile);
return Create(xtr);
}
protected static DataFile Create(XmlTextReader xtr)
{
XmlSerializer xs=new XmlSerializer(typeof(DataFile));
try
{
return (DataFile) xs.Deserialize(xtr);
}
finally
{
xtr.Close();
}
}
/// Convenience method to open a file and process it
public void Process(string dataFile, XmlWriter writer)
{
WriteLine(LogLevel.INFO, "Processing {0}", dataFile);
Stream s = new FileStream(dataFile, FileMode.Open, FileAccess.Read);
Process(s, writer);
}
public void Process(Stream s, XmlWriter writer)
{
CustomBinaryReader r = new CustomBinaryReader(s);
try
{
Process(r, writer);
}
finally
{
s.Close();
}
}
/// This is the core method overridden by all subclasses of Region
// TODO: M: very inefficient if no matches found - will iterate over WORDs to end of file
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
WriteLine(LogLevel.DEBUG, "Processing...");
// in this case we read a magic and try to process it
while ( true )
{
byte[] magic=new byte[2];
int bytesRead=reader.BaseStream.Read(magic, 0, 2);
if ( bytesRead == 0 )
// end of file - nothing more to read
break;
if ( bytesRead == 1 )
// this can happen if zipping over unmatched bytes at end of file - should handle better
// throw new InvalidOperationException("Could only read one byte of identifier at end of stream");
break;
// test whether the magic matches one of our child objects
string magicString=string.Format("0x{0:X2}{1:X2}", magic[0], magic[1]);
foreach ( IdentifiedObjectRegion r in regions )
{
if ( r.Matches(magicString) )
{
r.Process(reader, writer);
break;
}
}
}
}
/// This defines what children we can have from the XML config
[XmlElement("IdentifiedObject", typeof(IdentifiedObjectRegion)),
XmlElement("ElementaryFile", typeof(ElementaryFileRegion))]
public ArrayList Regions
{
get { return regions; }
set { regions = value; }
}
}
/// Simple subclass to hold the identified or 'magic' for the region (used by VuDataFile above)
public class IdentifiedObjectRegion : ContainerRegion
{
[XmlAttribute]
public string Identifier;
public bool Matches(string s)
{
// match a magic if we have null identifier or it actually matches
// (allows provision of a catch all region which is only really useful during development)
return Identifier == null || Identifier.Length == 0 || s.Equals(Identifier);
}
}
public class ElementaryFileRegion : IdentifiedObjectRegion
{
protected override bool SuppressElement(CustomBinaryReader reader)
{
int type=reader.PeekChar();
return type == 0x01;
}
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
// read the type
byte type=reader.ReadByte();
regionLength=reader.ReadSInt16();
long fileLength=regionLength;
if ( type == 0x01 )
{
// this is just the signature
}
else
{
long start=reader.BaseStream.Position;
base.ProcessInternal(reader, writer);
long amountProcessed=reader.BaseStream.Position-start;
fileLength -= amountProcessed;
}
if ( fileLength > 0 )
{
// deal with a remaining fileLength that is greater than int
while ( fileLength > int.MaxValue )
{
reader.ReadBytes(int.MaxValue);
fileLength-=int.MaxValue;
}
reader.ReadBytes((int) fileLength);
}
}
}
public class CyclicalActivityRegion : ContainerRegion
{
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
uint oldest=reader.ReadSInt16();
uint newest=reader.ReadSInt16();
// length is length of region minus the bytes we've just read
long effectiveLength=regionLength - 4;
long position=reader.BaseStream.Position;
WriteLine(LogLevel.INFO, "Oldest {0:X4} (offset {2:X4}), newest {1:X4} (offset {3:X4})",
position+oldest, position+newest,
oldest, newest);
if ( oldest == newest && oldest == 0 )
// no data in file
return;
CyclicStream cyclicStream=new CyclicStream(reader.BaseStream, reader.BaseStream.Position, effectiveLength);
CustomBinaryReader cyclicReader=new CustomBinaryReader(cyclicStream);
reader.BaseStream.Seek(oldest, SeekOrigin.Current);
bool last=false;
while ( !last )
{
long pos=cyclicStream.Position;
if ( pos == newest )
last=true;
base.ProcessInternal(cyclicReader, writer);
}
writer.WriteElementString("DataBufferIsWrapped", cyclicStream.Wrapped.ToString());
}
}
public class DriverCardDailyActivityRegion : Region
{
private uint previousRecordLength;
private uint currentRecordLength;
private DateTime recordDate;
private uint dailyPresenceCounter;
private uint distance;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
previousRecordLength=reader.ReadSInt16();
currentRecordLength=reader.ReadSInt16();
recordDate=reader.ReadTimeReal();
dailyPresenceCounter=reader.ReadSInt16();
distance=reader.ReadSInt16();
writer.WriteAttributeString("DateTime", recordDate.ToString());
writer.WriteAttributeString("DailyPresenceCounter", dailyPresenceCounter.ToString());
writer.WriteAttributeString("Distance", distance.ToString());
uint recordCount=(currentRecordLength-12)/2;
WriteLine(LogLevel.DEBUG, "Reading {0} activity records", recordCount);
while (recordCount > 0)
{
ActivityChangeRegion acr=new ActivityChangeRegion();
acr.Name="ActivityChangeInfo";
acr.Process(reader, writer);
recordCount--;
}
}
}
/// Simple logging - can be set on any region. Could be improved
// TODO: M: make log level command line option
public enum LogLevel
{
NONE=0,
DEBUG=1,
INFO=2,
WARN=3,
ERROR=4
}
/// Abstract base class for all regions. Holds some convenience methods
public abstract class Region
{
// All regions have a name which becomes the XML element on output
[XmlAttribute]
public string Name;
[XmlAttribute]
public bool GlobalValue;
[XmlAttribute]
public LogLevel LogLevel=LogLevel.INFO;
protected long byteOffset;
protected long regionLength=0;
protected static Hashtable globalValues=new Hashtable();
protected static readonly String[] countries = new string[] {"No information available",
"Austria","Albania","Andorra","Armenia","Azerbaijan","Belgium","Bulgaria","Bosnia and Herzegovina",
"Belarus","Switzerland","Cyprus","Czech Republic","Germany","Denmark","Spain","Estonia","France",
"Finland","Liechtenstein","Faeroe Islands","United Kingdom","Georgia","Greece","Hungary","Croatia",
"Italy","Ireland","Iceland","Kazakhstan","Luxembourg","Lithuania","Latvia","Malta","Monaco",
"Republic of Moldova","Macedonia","Norway","Netherlands","Portugal","Poland","Romania","San Marino",
"Russian Federation","Sweden","Slovakia","Slovenia","Turkmenistan","Turkey","Ukraine","Vatican City",
"Yugoslavia"};
public void Process(CustomBinaryReader reader, XmlWriter writer)
{
// Store start of region (for logging only)
byteOffset=reader.BaseStream.Position;
bool suppress=SuppressElement(reader);
// Write a new output element
if ( !suppress )
writer.WriteStartElement(Name);
// Call subclass process method
ProcessInternal(reader, writer);
// End the element
if ( !suppress )
writer.WriteEndElement();
long endPosition=reader.BaseStream.Position;
if ( reader.BaseStream is CyclicStream )
endPosition=((CyclicStream) reader.BaseStream).ActualPosition;
WriteLine(LogLevel.DEBUG, "{0} [0x{1:X4}-0x{2:X4}/0x{3:X4}] {4}", Name, byteOffset,
endPosition, endPosition-byteOffset, ToString());
if ( GlobalValue )
{
globalValues[Name] = ToString();
}
}
protected void WriteLine(LogLevel level, string format, params object[] args)
{
if ( level >= LogLevel )
Console.WriteLine(format, args);
}
protected abstract void ProcessInternal(CustomBinaryReader reader, XmlWriter writer);
protected virtual bool SuppressElement(CustomBinaryReader reader)
{
// derived classes can override this to suppress the writing of
// a wrapper element. Used by the ElementaryFileRegion to suppress
// altogether the signature blocks that occur for some regions
return false;
}
public long RegionLength
{
set { regionLength=value; }
}
}
/// Generic class for a region that contains other regions. Used as a simple wrapper
/// where this is indicated in the specification
public class ContainerRegion : Region
{
public ArrayList regions=new ArrayList();
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
// iterate over all child regions and process them
foreach ( Region r in regions )
{
r.RegionLength=regionLength;
r.Process(reader, writer);
}
}
// these are the valid regions this class can contain, along with XML name mappings
[XmlElement("Padding", typeof(PaddingRegion)),
XmlElement("Collection", typeof(CollectionRegion)),
XmlElement("Cycle", typeof(CyclicalActivityRegion)),
XmlElement("DriverCardDailyActivity", typeof(DriverCardDailyActivityRegion)),
XmlElement("Repeat", typeof(RepeatingRegion)),
XmlElement("Name", typeof(NameRegion)),
XmlElement("SimpleString", typeof(SimpleStringRegion)),
XmlElement("InternationalString", typeof(CodePageStringRegion)),
XmlElement("ExtendedSerialNumber", typeof(ExtendedSerialNumberRegion)),
XmlElement("Object", typeof(ContainerRegion)),
XmlElement("TimeReal", typeof(TimeRealRegion)),
XmlElement("Datef", typeof(DatefRegion)),
XmlElement("ActivityChange", typeof(ActivityChangeRegion)),
XmlElement("FullCardNumber", typeof(FullCardNumberRegion)),
XmlElement("Flag", typeof(FlagRegion)),
XmlElement("UInt24", typeof(UInt24Region)),
XmlElement("UInt16", typeof(UInt16Region)),
XmlElement("UInt8", typeof(UInt8Region)),
XmlElement("Country", typeof(CountryRegion)),
XmlElement("HexValue", typeof(HexValueRegion))]
public ArrayList Regions
{
get { return regions; }
set { regions = value; }
}
}
/// Simple class to "eat" a specified number of bytes in the file
public class PaddingRegion : Region
{
[XmlAttribute]
public int Size;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
byte[] buf=new byte[Size];
int amountRead=reader.Read(buf, 0, Size);
if ( amountRead != Size )
throw new InvalidOperationException("End of file reading padding (size "+Size+")");
}
public override string ToString()
{
return string.Format("{0} bytes (0x{0:X4})", Size);
}
}
// A string that is non-international consisting of a specified number of bytes
public class SimpleStringRegion : Region
{
[XmlAttribute]
public int Length;
protected string text;
public SimpleStringRegion()
{
}
// this is for the benefit of subclasses
public SimpleStringRegion(int length)
{
this.Length=length;
}
// method that will read string from file in specified encoding
protected void ProcessInternal(CustomBinaryReader s, Encoding enc)
{
text=s.ReadString(Length, enc).Trim();
}
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
// we just use the default encoding in the default case
this.ProcessInternal(reader, Encoding.Default);
writer.WriteString(text);
}
public override string ToString()
{
return text;
}
}
// A string that is prefixed by a code page byte
// TODO: H: the codepages in the data are not valid code pages for windows
public class CodePageStringRegion : SimpleStringRegion
{
private int codepage;
public CodePageStringRegion()
{
}
public CodePageStringRegion(int size) : base(size)
{
}
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
// get the codepage
codepage=reader.ReadByte();
Encoding enc=Encoding.Default;
try
{
// try to get the encoding
enc=Encoding.GetEncoding(codepage);
}
catch ( Exception )
{
// TODO: H: work out what the code page should be
// Console.WriteLine("WARN: Failed to work with codepage {0}, '{1}'", codepage, e.Message);
}
// read string using encoding
base.ProcessInternal(reader, enc);
writer.WriteString(text);
}
}
// A name is a string with codepage with fixed length = 35
public class NameRegion : CodePageStringRegion
{
private static readonly int SIZE=35;
public NameRegion() : base(SIZE)
{
}
}
public class HexValueRegion : Region
{
[XmlAttribute]
public int Length;
private byte[] values;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
values=new byte[Length];
for ( int n=0; n< Length; n++ )
values[n]=reader.ReadByte();
writer.WriteAttributeString("Value", this.ToString());
}
public override string ToString()
{
if ( values == null )
return "(null)";
return ToHexString(values);
}
public static string ToHexString(byte[] values)
{
StringBuilder sb=new StringBuilder(values.Length*2+2);
sb.Append("0x");
foreach ( byte b in values )
sb.AppendFormat("{0:X2}", b);
return sb.ToString();
}
}
// See page 72. Have written class for this as seen in multiple places
public class ExtendedSerialNumberRegion : Region
{
private uint serialNumber;
private byte month;
private byte year;
private byte type;
private byte manufacturerCode;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
serialNumber=reader.ReadSInt32();
month=reader.ReadByte();
year=reader.ReadByte();
type=reader.ReadByte();
manufacturerCode=reader.ReadByte();
writer.WriteAttributeString("Month", month.ToString());
writer.WriteAttributeString("Year", year.ToString());
writer.WriteAttributeString("Type", type.ToString());
writer.WriteAttributeString("ManufacturerCode", manufacturerCode.ToString());
writer.WriteString(serialNumber.ToString());
}
public override string ToString()
{
return string.Format("{0}, {1}/{2}, type={3}, manuf code={4}",
serialNumber, month, year, type, manufacturerCode);
}
}
// see page 83 - 4 byte second offset from midnight 1 January 1970
public class TimeRealRegion : Region
{
private DateTime dateTime;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
dateTime=reader.ReadTimeReal();
writer.WriteAttributeString("DateTime", dateTime.ToString());
}
public override string ToString()
{
return string.Format("{0}", dateTime);
}
}
// see page 56 (BCDString) and page 69 (Datef) - 2 byte encoded date in yyyy mm dd format
public class DatefRegion : Region
{
private DateTime dateTime;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
byte yy1=reader.ReadByte();
byte yy2 = reader.ReadByte();
byte mm = reader.ReadByte();
byte dd = reader.ReadByte();
int year = ((yy1 & 0xF0) >> 4) * 1000 + (yy1 & 0xF) * 100 + ((yy2 & 0xF0) >> 4) * 10 + (yy2 & 0xF);
int month = ((mm & 0xF0) >> 4) * 10 + (mm & 0xF);
int day = ((dd & 0xF0) >> 4) * 10 + (dd & 0xF);
dateTime = new DateTime(year, month, day);
writer.WriteAttributeString("Datef", dateTime.ToString());
}
public override string ToString()
{
return string.Format("{0}", dateTime);
}
}
public enum EquipmentType
{
DriverCard=1,
WorkshopCard=2,
ControlCard=3
// TODO: M: there are more
}
// see page 72 - we only support driver cards
public class FullCardNumberRegion : Region
{
EquipmentType type;
byte issuingMemberState;
string driverIdentification;
byte replacementIndex;
byte renewalIndex;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
type=(EquipmentType) reader.ReadByte();
issuingMemberState=reader.ReadByte();
driverIdentification=reader.ReadString(14);
replacementIndex=reader.ReadByte();
renewalIndex=reader.ReadByte();
writer.WriteAttributeString("Type", type.ToString());
writer.WriteAttributeString("IssuingMemberState", issuingMemberState.ToString());
writer.WriteAttributeString("ReplacementIndex", replacementIndex.ToString());
writer.WriteAttributeString("RenewalIndex", renewalIndex.ToString());
writer.WriteString(driverIdentification);
}
public override string ToString()
{
return string.Format("type={0}, {1}, {2}, {3}, {4}",
type, issuingMemberState, driverIdentification, replacementIndex, renewalIndex);
}
}
// 3 byte number, as used by OdometerShort (see page 86)
public class UInt24Region : Region
{
private uint uintValue;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
uintValue=reader.ReadSInt24();
writer.WriteString(uintValue.ToString());
}
public override string ToString()
{
return uintValue.ToString();
}
}
public class UInt16Region : Region
{
private uint uintValue;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
uintValue=reader.ReadSInt16();
writer.WriteString(uintValue.ToString());
}
public override string ToString()
{
return uintValue.ToString();
}
}
public class CountryRegion : Region
{
private string countryName;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
byte byteValue=reader.ReadByte();
if ( byteValue < countries.Length )
countryName=countries[byteValue];
else
countryName="UNKNOWN";
writer.WriteAttributeString("Code", HexValueRegion.ToHexString(new byte[] {byteValue}));
writer.WriteAttributeString("Name", countryName);
}
public override string ToString()
{
return countryName;
}
}
public class UInt8Region : Region
{
private byte byteValue;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
byteValue=reader.ReadByte();
writer.WriteString(byteValue.ToString());
}
public override string ToString()
{
return byteValue.ToString();
}
}
// Simple class to represent a boolean (0 or 1 in specification)
public class FlagRegion : Region
{
private bool boolValue;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
boolValue=reader.ReadByte() > 0;
writer.WriteString(boolValue.ToString());
}
public override string ToString()
{
return boolValue.ToString();
}
}
public enum SizeAllocation
{
BYTE,
WORD
}
// A collection region is a repeating region prefixed by the count of number of
// items in the region. The count can be represented by a single byte or a word,
// depending on the collection, so this supports a SizeAllocation property to specify
// which it is.
public class CollectionRegion : ContainerRegion
{
[XmlAttribute]
public SizeAllocation SizeAlloc=SizeAllocation.BYTE;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
// get the count according to allocation size
uint count;
switch (SizeAlloc)
{
case SizeAllocation.BYTE:
count=reader.ReadByte();
break;
case SizeAllocation.WORD:
count=reader.ReadSInt16();
break;
default:
throw new InvalidOperationException("Bad size allocation");
}
ProcessItems(reader, writer, count);
}
protected void ProcessItems(CustomBinaryReader reader, XmlWriter writer, uint count)
{
WriteLine(LogLevel.DEBUG, "Processing repeating {0}, count={1}", Name, count);
// repeat processing of child objects
while ( count > 0 )
{
base.ProcessInternal(reader, writer);
count--;
}
}
public override string ToString()
{
return string.Format("<< end {0}", Name);
}
}
public class RepeatingRegion : CollectionRegion
{
[XmlAttribute]
public uint Count;
[XmlAttribute]
public string CountRef;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
if ( Count == 0 && CountRef != null )
{
string refName=CountRef.Substring(1);
Count=uint.Parse((string) globalValues[refName]);
}
ProcessItems(reader, writer, Count);
}
}
public enum Activity
{
Break,
Available,
Work,
Driving
}
// This is the activity change class. It has own class because the fields
// are packed into two bytes which we need to unpack (see page 55).
public class ActivityChangeRegion : Region
{
byte slot;
byte status;
bool inserted;
Activity activity;
uint time;
protected override void ProcessInternal(CustomBinaryReader reader, XmlWriter writer)
{
byte b1=reader.ReadByte();
byte b2=reader.ReadByte();
slot=(byte) ((b1 & 0x80) >> 7);
// TODO: H: not sure why this is always returning zero (simple logic bug?)
status=(byte) ((b1 & 0x40));
inserted=(b1 & 0x20) == 0;
activity=(Activity) ((b1 & 0x18) >> 3);
time=(uint) (((b1 & 0x07) << 8) | b2);
if ( this.LogLevel == LogLevel.DEBUG || this.LogLevel == LogLevel.INFO )
{
long position=reader.BaseStream.Position;
if ( reader.BaseStream is CyclicStream )
position=((CyclicStream) reader.BaseStream).ActualPosition;
writer.WriteAttributeString("FileOffset", string.Format("0x{0:X4}", position));
}
writer.WriteAttributeString("Slot", slot.ToString());
writer.WriteAttributeString("Status", status.ToString());
writer.WriteAttributeString("Inserted", inserted.ToString());
writer.WriteAttributeString("Activity", activity.ToString());
writer.WriteAttributeString("Time", string.Format("{0:d2}:{1:d2}", time / 60, time % 60));
}
public override string ToString()
{
return string.Format("slot={0}, status={1}, inserted={2}, activity={3}, time={4:d2}:{5:d2}",
slot, status, inserted, activity, time / 60, time % 60);
}
}
}