-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdPacket.cs
220 lines (181 loc) · 5.02 KB
/
PdPacket.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace USB_PD_Analyzer
{
internal class PdPacket
{
static readonly byte[] symbol5b4b = new byte[]
{
0xff, 0xff, 0xff, 0xff,
0xff, 0x02, 0xff, 0x0e,
0xff, 0x08, 0x04, 0x0c,
0xff, 0x0a, 0x06, 0x00,
0xff, 0xff, 0x01, 0xff,
0xff, 0x03, 0xff, 0x0f,
0x00, 0x09, 0x05, 0x0d,
0xff, 0x0b, 0x07, 0xff
};
public byte[] preamble;
public PdSop sop;
public PdHeader header;
public List<PdDataObject> data;
public uint crc;
public byte eop;
public PdPacket(string hex)
{
// Preamble: 64 bit
// Start Of Packet: 4 K-code * 5b
// Message Header: 16 bit / 4b * 5b
// Data Objects: 32 bit * 7 objs(Max) / 4b * 5b
// CRC: 32 bit / 4b * 5b
// End Of packet: 1 K-code * 5b
byte[] rawData;
byte[] sopBinary = new byte[4];
ushort headerBinary = 0;
rawData = HexStringToBinary(hex).ToArray();
int len = rawData.Length;
if (len < 8)
return;
preamble = new byte[8];
for (int i = 0; i < 8; i++)
{
preamble[i] = rawData[i];
}
int bitPosition = 8 * 8;
for (int i = 0; i < 4; i++)
{
if (Get5b(rawData, ref bitPosition, ref sopBinary[i]))
return;
}
sop = new PdSop(sopBinary);
if (Convert5bTo4b(rawData, ref bitPosition, ref headerBinary))
return;
if (sop.Sop == PdSop.SopType.Sop)
{
header = new PdHeaderSop(headerBinary);
}
else if (sop.Sop == PdSop.SopType.SopPrime || sop.Sop == PdSop.SopType.SopDoublePrime)
{
header = new PdHeaderSopPrime(headerBinary);
}
else
{
return;
}
data = new List<PdDataObject>();
for (int i = 0; i < header.NumberOfDataObjects; i++)
{
uint dataBinary = 0;
if (Convert5bTo4b(rawData, ref bitPosition, ref dataBinary))
return;
if (header.MessageType == PdHeader.MessageTypes.Source_Capabilities || header.MessageType == PdHeader.MessageTypes.Sink_Capabilities)
{
PdPowerDataObject pdo = new PdPowerDataObject(dataBinary);
if (pdo.SupplyType == PdPowerDataObject.SourceTypes.FixedSupply)
{
if (header.MessageType == PdHeader.MessageTypes.Source_Capabilities)
data.Add(new PdSourceFixedSupplyPdo(dataBinary));
else
data.Add(new PdSinkFixedSupplyPdo(dataBinary));
}
else if (pdo.SupplyType == PdPowerDataObject.SourceTypes.VariableSupply)
{
if (header.MessageType == PdHeader.MessageTypes.Source_Capabilities)
data.Add(new PdSourceVariableSupplyPdo(dataBinary));
else
data.Add(new PdSinkVariableSupplyPdo(dataBinary));
} else if (pdo.SupplyType == PdPowerDataObject.SourceTypes.Battery)
{
if (header.MessageType == PdHeader.MessageTypes.Source_Capabilities)
data.Add(new PdSourceBatterySupplyPdo(dataBinary));
else
data.Add(new PdSinkBatterySupplyPdo(dataBinary));
}
}
else if (header.MessageType == PdHeader.MessageTypes.Request)
{
data.Add(new PdFixedAndVariableRequestDataObject(dataBinary));
}
else
{
data.Add(new PdDataObject(dataBinary));
}
}
if (Convert5bTo4b(rawData, ref bitPosition, ref crc))
return;
if (Get5b(rawData, ref bitPosition, ref eop))
return;
}
private IEnumerable<byte> HexStringToBinary(string hex)
{
for (int i = 0; i < hex.Length - 1; i += 2)
{
yield return Convert.ToByte(hex.Substring(i, 2), 16);
}
}
private bool Get5b(byte[] data4b, ref int position, ref byte symbol)
{
int len = data4b.Length;
if (position % 8 > 3)
{
if ((position + 7) % 8 + 1 > len)
return true;
symbol = (byte)(((data4b[position / 8] << (position % 8 - 3)) | (data4b[position / 8 + 1] >> (11 - position % 8))) & 0x1f);
}
else
{
if ((position + 7) % 8 > len)
return true;
symbol = (byte)((data4b[position / 8] >> (3 - position % 8)) & 0x1f);
}
position += 5;
return false;
}
private bool Convert5bTo4bNibble(byte[] data4b, ref int position, ref byte decoded)
{
byte nibble = 0;
if (Get5b(data4b, ref position, ref nibble))
return true;
decoded = symbol5b4b[nibble];
if (decoded > 0x0f)
return true;
return false;
}
private bool Convert5bTo4b(byte[] data4b, ref int position, ref byte decoded)
{
byte hi = 0, lo = 0;
if (Convert5bTo4bNibble(data4b, ref position, ref lo))
return true;
if (Convert5bTo4bNibble(data4b, ref position, ref hi))
return true;
decoded = (byte)((hi << 4) | lo);
return false;
}
private bool Convert5bTo4b(byte[] data, ref int position, ref ushort decoded)
{
decoded = 0;
for (int i = 0; i < 2; i++)
{
byte d = 0;
if (Convert5bTo4b(data, ref position, ref d))
return true;
decoded += (ushort)(d << (8 * i));
}
return false;
}
private bool Convert5bTo4b(byte[] data, ref int position, ref uint decoded)
{
for (int i = 0; i < 4; i++)
{
byte d = 0;
if (Convert5bTo4b(data, ref position, ref d))
return true;
decoded += (uint)(d << (8 * i));
}
return false;
}
}
}