forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordHeader.cs
241 lines (208 loc) · 7.09 KB
/
RecordHeader.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers.Binary;
namespace Iot.Device.Ndef
{
/// <summary>
/// Record header of NDEF message
/// </summary>
public class RecordHeader
{
private uint _payloadLength = 0;
private byte[]? _payloadType = null;
private byte[]? _payloadId = null;
/// <summary>
/// Message flag
/// </summary>
public MessageFlag MessageFlag { get; set; }
/// <summary>
/// Type of name format
/// </summary>
public TypeNameFormat TypeNameFormat { get; set; }
/// <summary>
/// Length of the Payload Type
/// </summary>
public byte PayloadTypeLength { get; internal set; }
/// <summary>
/// Payload length
/// </summary>
public uint PayloadLength
{
get
{
return _payloadLength;
}
set
{
SetPayloadLength(value);
}
}
/// <summary>
/// Id Length
/// </summary>
public byte IdLength { get; internal set; }
/// <summary>
/// Payload Type
/// </summary>
public byte[] PayloadType
{
get
{
return _payloadType;
}
set
{
if (value.Length > 255)
{
throw new ArgumentException($"{nameof(PayloadType)} length can only be less than 255 bytes.");
}
_payloadType = value;
PayloadTypeLength = (byte)(_payloadType == null ? 0 : _payloadType.Length);
}
}
/// <summary>
/// Payload Id
/// </summary>
public byte[]? PayloadId
{
get
{
return _payloadId;
}
set
{
if (value.Length > 255)
{
throw new ArgumentException($"{nameof(PayloadId)} length can only be less than 255 bytes.");
}
_payloadId = value;
if (_payloadId == null)
{
IdLength = 0;
MessageFlag &= ~MessageFlag.IdLength;
}
else
{
IdLength = (byte)_payloadId.Length;
MessageFlag |= MessageFlag.IdLength;
}
}
}
/// <summary>
/// True if it's the first NDEF Record in the Message
/// </summary>
public bool IsFirstMessage => MessageFlag.HasFlag(MessageFlag.MessageBegin);
/// <summary>
/// True if it's the last NDEF Record in the Message
/// </summary>
public bool IsLastMessage => MessageFlag.HasFlag(MessageFlag.MessageEnd);
/// <summary>
/// True if it's a composed message
/// </summary>
public bool IsComposedMessage => MessageFlag.HasFlag(MessageFlag.ChunkFlag);
/// <summary>
/// The Length of the Header
/// </summary>
// TNF+Flags (1), Type Length (1), Payload Length (1 or 4), ID Length (0 or 1), Payload Type (0+), Payload Id (0+)
public int Length => 2 + (MessageFlag.HasFlag(MessageFlag.ShortRecord) ? 1 : 4) + (MessageFlag.HasFlag(MessageFlag.IdLength) ? 1 : 0) + PayloadTypeLength + (IdLength > 0 ? IdLength : 0);
/// <summary>
/// Create a full empty Record Header
/// </summary>
public RecordHeader()
{
}
/// <summary>
/// Create a header from a span of bytes
/// </summary>
/// <param name="recordToDecode">A span of bytes</param>
public RecordHeader(SpanByte recordToDecode)
{
int idxRecord = 0;
// First byte is the Message flag and type name format
var header = recordToDecode[idxRecord++];
MessageFlag = (MessageFlag)(header & 0b1111_1000);
TypeNameFormat = (TypeNameFormat)(header & 0b0000_0111);
PayloadTypeLength = recordToDecode[idxRecord++];
if (MessageFlag.HasFlag(MessageFlag.ShortRecord))
{
_payloadLength = recordToDecode[idxRecord++];
}
else
{
_payloadLength = BinaryPrimitives.ReadUInt32BigEndian(recordToDecode.Slice(idxRecord, 4));
idxRecord += 4;
}
if (MessageFlag.HasFlag(MessageFlag.IdLength))
{
IdLength = recordToDecode[idxRecord++];
}
if (PayloadTypeLength > 0)
{
PayloadType = new byte[PayloadTypeLength];
recordToDecode.Slice(idxRecord, PayloadTypeLength).CopyTo(PayloadType);
idxRecord += PayloadTypeLength;
}
if (IdLength > 0)
{
PayloadId = new byte[IdLength];
recordToDecode.Slice(idxRecord, IdLength).CopyTo(PayloadId);
}
}
private void SetPayloadLength(uint payloadLength)
{
if (payloadLength <= 255)
{
if (!MessageFlag.HasFlag(MessageFlag.ShortRecord))
{
MessageFlag |= MessageFlag.ShortRecord;
}
}
else
{
if (MessageFlag.HasFlag(MessageFlag.ShortRecord))
{
MessageFlag &= ~MessageFlag.ShortRecord;
}
}
_payloadLength = payloadLength;
}
/// <summary>
/// Serialize the header
/// </summary>
/// <param name="header">Serialized byte span</param>
public void Serialize(SpanByte header)
{
if (header.Length < Length)
{
throw new ArgumentException($"Header span must be same or larger size than the header size");
}
int idxRecord = 0;
// Calculate the size of the header
header[idxRecord++] = (byte)((byte)MessageFlag | (byte)TypeNameFormat);
header[idxRecord++] = PayloadTypeLength;
if (MessageFlag.HasFlag(MessageFlag.ShortRecord))
{
header[idxRecord++] = (byte)PayloadLength;
}
else
{
BinaryPrimitives.WriteUInt32BigEndian(header.Slice(idxRecord, 4), PayloadLength);
idxRecord += 4;
}
if (MessageFlag.HasFlag(MessageFlag.IdLength))
{
header[idxRecord++] = IdLength;
}
if (PayloadTypeLength > 0)
{
new SpanByte(PayloadType).CopyTo(header.Slice(idxRecord, PayloadTypeLength));
idxRecord += PayloadTypeLength;
}
if (IsComposedMessage)
{
new SpanByte(PayloadId).CopyTo(header.Slice(idxRecord));
}
}
}
}