-
Notifications
You must be signed in to change notification settings - Fork 253
/
JT808Package.cs
444 lines (443 loc) · 19.8 KB
/
JT808Package.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
using System;
using System.Text.Json;
using JT808.Protocol.Enums;
using JT808.Protocol.Exceptions;
using JT808.Protocol.Extensions;
using JT808.Protocol.Formatters;
using JT808.Protocol.Interfaces;
using JT808.Protocol.MessagePack;
namespace JT808.Protocol
{
/// <summary>
/// JT808数据包
/// </summary>
public class JT808Package : JT808MessagePackFormatter<JT808Package>, IJT808Analyze
{
/// <summary>
/// 起始符
/// </summary>
public const byte BeginFlag = 0x7e;
/// <summary>
/// 终止符
/// </summary>
public const byte EndFlag = 0x7e;
/// <summary>
/// 起始符
/// </summary>
public byte Begin { get; set; } = BeginFlag;
/// <summary>
/// 头数据
/// </summary>
public JT808Header Header { get; set; }
/// <summary>
/// 数据体
/// </summary>
public JT808Bodies Bodies { get; set; }
/// <summary>
/// 分包数据体
/// </summary>
public byte[] SubDataBodies { get; set; }
/// <summary>
/// 校验码
/// 从消息头开始,同后一字节异或,直到校验码前一个字节,占用一个字节。
/// </summary>
public byte CheckCode { get; set; }
/// <summary>
/// 终止符
/// </summary>
public byte End { get; set; } = EndFlag;
/// <summary>
/// 808版本号
/// </summary>
public JT808Version Version { get; set; }
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="config"></param>
/// <returns></returns>
public override JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
{
// 1. 验证校验和
if (!config.SkipCRCCode)
{
if (!reader.CheckXorCodeVali)
{
throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
}
}
JT808Package jT808Package = new JT808Package();
// ---------------开始解包--------------
// 2.读取起始位置
jT808Package.Begin = reader.ReadStart();
// 3.读取头部信息
jT808Package.Header = new JT808Header();
// 3.1.读取消息Id
jT808Package.Header.MsgId = reader.ReadUInt16();
// 3.2.读取消息体属性
jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16());
if (reader.Version == JT808Version.JTT2013Force)
{
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
reader.Version = JT808Version.JTT2013;
}
else
{
if (reader.Version == JT808Version.JTT2019 || jT808Package.Header.MessageBodyProperty.VersionFlag)
{
//2019版本
jT808Package.Header.ProtocolVersion = reader.ReadByte();
// 3.4.读取终端手机号
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim);
reader.Version = JT808Version.JTT2019;
}
else
{
//2013版本
// 3.3.读取终端手机号
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
}
}
jT808Package.Version = reader.Version;
// 3.4.读取消息流水号
jT808Package.Header.MsgNum = reader.ReadUInt16();
// 3.5.判断有无分包
if (jT808Package.Header.MessageBodyProperty.IsPackage)
{
//3.5.1.读取消息包总数
jT808Package.Header.PackgeCount = reader.ReadUInt16();
//3.5.2.读取消息包序号
jT808Package.Header.PackageIndex = reader.ReadUInt16();
}
// 4.处理数据体
// 4.1.判断有无数据体
if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
{
if (config.MsgIdFactory.TryGetValue(jT808Package.Header.MsgId, out object instance))
{
if (jT808Package.Header.MessageBodyProperty.IsPackage)
{
//读取分包的数据体
try
{
jT808Package.SubDataBodies = reader.ReadArray(jT808Package.Header.MessageBodyProperty.DataLength).ToArray();
if (config.EnableAutoMerge && config.Jt808PackageMerger.TryMerge(jT808Package.Header, jT808Package.SubDataBodies, config, out var body))
{
jT808Package.Bodies = body;
}
}
catch (Exception ex)
{
throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
}
}
else
{
try
{
//4.2.处理消息体
jT808Package.Bodies = instance.DeserializeExt<JT808Bodies>(ref reader, config);
}
catch (Exception ex)
{
throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
}
}
}
}
// 5.读取校验码
jT808Package.CheckCode = reader.ReadByte();
// 6.读取终止位置
jT808Package.End = reader.ReadEnd();
// ---------------解包完成--------------
return jT808Package;
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="config"></param>
public override void Serialize(ref JT808MessagePackWriter writer, JT808Package value, IJT808Config config)
{
// ---------------开始组包--------------
// 1.起始符
writer.WriteStart();
// 2.写入头部 //部分有带数据体的长度,那么先跳过写入头部部分
// 2.1.消息ID
if (value.Header.MsgId == default) value.Header.MsgId = value.Bodies.MsgId;
writer.WriteUInt16(value.Header.MsgId);
// 2.2.消息体属性(包含消息体长度所以先跳过)
writer.Skip(2, out int msgBodiesPropertyPosition);
if (writer.Version == JT808Version.JTT2019 || value.Header.MessageBodyProperty.VersionFlag)
{
//2019版本
// 2.3.协议版本号
writer.WriteByte(value.Header.ProtocolVersion);
// 2.4.终端手机号
writer.WriteBCD(value.Header.TerminalPhoneNo, 20);
writer.Version = JT808Version.JTT2019;
value.Header.MessageBodyProperty.VersionFlag = true;
}
else
{
//2013版本
// 2.3.终端手机号 (写死大陆手机号码)
writer.WriteBCD(value.Header.TerminalPhoneNo, config.TerminalPhoneNoLength);
}
if (value.Header.ManualMsgNum.HasValue)
{
// 2.4.消息流水号
writer.WriteUInt16(value.Header.ManualMsgNum.Value);
}
else
{
// 2.4.消息流水号
value.Header.MsgNum = config.MsgSNDistributed.Increment(value.Header.TerminalPhoneNo);
writer.WriteUInt16(value.Header.MsgNum);
}
// 2.5.判断是否分包
if (value.Header.MessageBodyProperty.IsPackage)
{
// 2.5.1.消息包总数
writer.WriteUInt16(value.Header.PackgeCount);
// 2.5.2.消息包序号
writer.WriteUInt16(value.Header.PackageIndex);
}
int headerLength = writer.GetCurrentPosition();
if (value.Header.MessageBodyProperty.IsPackage)
{
if (value.SubDataBodies != null)
{
//2.5.3.写入分包数据
writer.WriteArray(value.SubDataBodies);
}
}
else
{
// 3.处理数据体部分
if (value.Bodies != null)
{
if (!value.Bodies.SkipSerialization)
{
value.Bodies.SerializeExt(ref writer, value.Bodies, config);
}
}
}
// 3.1.处理数据体长度
// 2.2.回写消息体属性
value.Header.MessageBodyProperty.DataLength = (writer.GetCurrentPosition() - headerLength);
writer.WriteUInt16Return(value.Header.MessageBodyProperty.Wrap(), msgBodiesPropertyPosition);
// 4.校验码
writer.WriteXor();
// 5.终止符
writer.WriteEnd();
// 6.编码
writer.WriteEncode();
// ---------------组包结束--------------
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="writer"></param>
/// <param name="config"></param>
public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config)
{
// ---------------开始解析对象--------------
writer.WriteStartObject();
// 1. 验证校验和
if (!reader.CheckXorCodeVali)
{
writer.WriteString("检验和错误", $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
}
// 2.读取起始位置
byte start = reader.ReadEnd();
writer.WriteNumber($"[{start.ReadNumber()}]开始", start);
var msgid = reader.ReadUInt16();
writer.WriteNumber($"[{msgid.ReadNumber()}]消息Id", msgid);
ushort messageBodyPropertyValue = reader.ReadUInt16();
var headerMessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue);
//消息体属性对象 开始
writer.WriteStartObject("消息体属性对象");
ReadOnlySpan<char> messageBodyPropertyReadOnlySpan = messageBodyPropertyValue.ReadBinary();
writer.WriteNumber($"[{messageBodyPropertyReadOnlySpan.ToString()}]消息体属性", messageBodyPropertyValue);
if (reader.Version == JT808Version.JTT2013Force)
{
reader.Version = JT808Version.JTT2013;
writer.WriteString("版本号", JT808Version.JTT2013.ToString());
writer.WriteNumber("[bit15]保留", 0);
writer.WriteNumber("[bit14]保留", 0);
writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage);
writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString());
writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength);
writer.WriteEndObject();
//2013版本
// 3.3.读取终端手机号
var terminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, false);
//消息体属性对象 结束
writer.WriteString($"[{terminalPhoneNo.PadLeft(config.TerminalPhoneNoLength, '0')}]终端手机号", terminalPhoneNo);
}
else
{
if (reader.Version == JT808Version.JTT2019 || headerMessageBodyProperty.VersionFlag)
{
reader.Version = JT808Version.JTT2019;
writer.WriteString("版本号", JT808Version.JTT2019.ToString());
writer.WriteNumber("[bit15]保留", 0);
writer.WriteBoolean("[bit14]协议版本标识", headerMessageBodyProperty.VersionFlag);
writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage);
writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString());
writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength);
//消息体属性对象 结束
writer.WriteEndObject();
//2019版本
var protocolVersion = reader.ReadByte();
writer.WriteNumber($"[{protocolVersion.ReadNumber()}]协议版本号(2019)", protocolVersion);
// 3.4.读取终端手机号
var terminalPhoneNo = reader.ReadBCD(20, config.Trim);
writer.WriteString($"[{terminalPhoneNo.PadLeft(20, '0')}]终端手机号", terminalPhoneNo);
}
else
{
reader.Version = JT808Version.JTT2013;
writer.WriteString("版本号", JT808Version.JTT2013.ToString());
writer.WriteNumber("[bit15]保留", 0);
writer.WriteNumber("[bit14]保留", 0);
writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage);
writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString());
writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength);
writer.WriteEndObject();
//2013版本
// 3.3.读取终端手机号
var terminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, false);
//消息体属性对象 结束
writer.WriteString($"[{terminalPhoneNo.PadLeft(config.TerminalPhoneNoLength, '0')}]终端手机号", terminalPhoneNo);
}
}
// 3.4.读取消息流水号
var msgNum = reader.ReadUInt16();
writer.WriteNumber($"[{msgNum.ReadNumber()}]消息流水号", msgNum);
// 3.5.判断有无分包
uint packgeCount = 0, packageIndex = 0;
if (headerMessageBodyProperty.IsPackage)
{
//3.5.1.读取消息包总数
packgeCount = reader.ReadUInt16();
writer.WriteNumber($"[{packgeCount.ReadNumber()}]消息包总数", packgeCount);
//3.5.2.读取消息包序号
packageIndex = reader.ReadUInt16();
writer.WriteNumber($"[{packageIndex.ReadNumber()}]消息包序号", packageIndex);
}
// 4.处理数据体
// 4.1.判断有无数据体
if (headerMessageBodyProperty.DataLength > 0)
{
//数据体属性对象 开始
writer.WriteStartObject("数据体对象");
string description = "数据体";
if (headerMessageBodyProperty.IsPackage)
{
//读取分包的数据体
try
{
writer.WriteString($"[分包]数据体", reader.ReadArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString());
}
catch (IndexOutOfRangeException ex)
{
writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace);
}
catch (ArgumentOutOfRangeException ex)
{
writer.WriteString($"[分包]数据体解析异常,无可用数据体进行解析", ex.StackTrace);
}
catch (Exception ex)
{
writer.WriteString($"[分包]数据体异常", ex.StackTrace);
}
}
else
{
if (config.MsgIdFactory.TryGetValue(msgid, out object instance))
{
if (instance is IJT808Description jT808Description)
{
//4.2.处理消息体
description = jT808Description.Description;
}
try
{
//数据体长度正常
writer.WriteString($"{description}", reader.ReadVirtualArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString());
if (instance is IJT808Analyze analyze)
{
//4.2.处理消息体
analyze.Analyze(ref reader, writer, config);
}
}
catch (IndexOutOfRangeException ex)
{
writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace);
}
catch (ArgumentOutOfRangeException ex)
{
writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace);
}
catch (Exception ex)
{
writer.WriteString($"数据体异常", ex.StackTrace);
}
}
else
{
writer.WriteString($"[未知]数据体", reader.ReadArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString());
}
}
//数据体属性对象 结束
writer.WriteEndObject();
}
else
{
if (config.MsgIdFactory.TryGetValue(msgid, out object instance))
{
//数据体属性对象 开始
writer.WriteStartObject("数据体对象");
string description = "[Null]数据体";
if (instance is IJT808Description jT808Description)
{
//4.2.处理消息体
description = jT808Description.Description;
}
writer.WriteNull(description);
//数据体属性对象 结束
writer.WriteEndObject();
}
else
{
writer.WriteNull($"[Null]数据体");
}
}
try
{
// 5.读取校验码
reader.ReadByte();
writer.WriteNumber($"[{reader.RealCheckXorCode.ReadNumber()}]校验码", reader.RealCheckXorCode);
// 6.读取终止位置
byte end = reader.ReadEnd();
writer.WriteNumber($"[{end.ReadNumber()}]结束", end);
}
catch (ArgumentOutOfRangeException ex)
{
writer.WriteString($"数据解析异常,无可用数据进行解析", ex.StackTrace);
}
catch (Exception ex)
{
writer.WriteString($"数据解析异常", ex.StackTrace);
}
finally
{
writer.WriteEndObject();
}
}
}
}