-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSIReader.cs
169 lines (141 loc) · 5.71 KB
/
CSIReader.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
using System;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading;
namespace CSILib
{
public class CSIReader
{
private readonly int CSIBufferSize;
private readonly string CSIDev;
private Thread ReadThread;
private CancellationTokenSource ThreadCancellation = new CancellationTokenSource();
public event EventHandler<CSIPacket> OnNewPacket;
object Locker = new object();
public CSIReader(string csiDev, int csiBufferSize)
{
CSIDev = csiDev;
CSIBufferSize = csiBufferSize;
}
public void StartReading()
{
lock (Locker)
{
if (ReadThread != null)
{
Console.WriteLine("Already Started");
return;
}
ReadThread = new Thread(Read);
ReadThread.Start();
}
}
void Read()
{
var fileStream = File.Open(CSIDev, FileMode.Open, FileAccess.Read);
while(!ThreadCancellation.IsCancellationRequested)
{
var packet = ReadCSIPacket(fileStream);
if (packet != null)
{
OnNewPacket?.Invoke(this, packet);
}
}
fileStream.Dispose();
}
public void Stop()
{
lock (Locker)
{
if (ReadThread == null)
{
Console.WriteLine("Already Stopped");
return;
}
ThreadCancellation.Cancel();
ReadThread.Join();
ReadThread = null;
}
}
CSIPacket ReadCSIPacket(FileStream file)
{
var buffer = new byte[CSIBufferSize];
var bytesRead = file.Read(buffer, 0, CSIBufferSize);
if (bytesRead <= 0) //(bytesRead < CSIBufferSize)
{
//Console.WriteLine("No bytes read");
return null;
}
var span = new Span<byte>(buffer);
var csiPacket = new CSIPacket();
int csiPacketLength = Marshal.SizeOf(typeof(CSIStatusPacket));
//var statusBuffer = new Span<byte>(buffer, csiPacketLength);
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
var statusPacket = (CSIStatusPacket) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(CSIStatusPacket));
csiPacket.StatusPacket = statusPacket;
if (statusPacket.NumReceivingAntennas == 0 || statusPacket.NumTransmittingAntennas == 0) {
Console.WriteLine("Num transmitting or receiving antennas is 0, skipping packet");
return null;
}
if (statusPacket.PhyError != PHYErrorCode.OK) {
Console.WriteLine("Error in packet: " + statusPacket.PhyError.ToString());
}
var matrixSlice = span.Slice(csiPacketLength);
var csiDataLength = statusPacket.CSIDataLength;
if (csiDataLength == 0)
{
//Console.WriteLine("CSI Data length is 0, skipping packet");
return null;
}
csiPacket.CSIMatrix = ReadCSIMatrix(matrixSlice, statusPacket.NumReceivingAntennas, statusPacket.NumTransmittingAntennas, statusPacket.NumSubcarriers);
//var copyStartIndex = csiPacketLength + csiDataLength;
var payloadSlice = matrixSlice.Slice(statusPacket.CSIDataLength, statusPacket.PayloadLength);
csiPacket.PayloadData = new byte[statusPacket.PayloadLength];
payloadSlice.CopyTo(csiPacket.PayloadData);
//Array.Copy(buffer, copyStartIndex, csiPayload.Data, 0, payloadLength);
return csiPacket;
}
Complex[,,] ReadCSIMatrix (Span<byte> buffer, int numRx, int numTx, int numSubcarriers)
{
var matrix = new Complex[numRx, numTx, numSubcarriers];
int currentByte = 0;
uint currentData = 0;
byte bitsLeft = 0;
for (int sub = 0; sub < numSubcarriers; sub++)
{
for (int tx = 0; tx < numTx; tx++)
{
for (int rx = 0; rx < numRx; rx++)
{
var number = new int[2];
for (int i = 0; i < 2; i++)
{
if(bitsLeft < 10) {
ushort nextData = buffer[currentByte++];
nextData += (ushort) (buffer[currentByte++] << 8);
currentData += (uint)(nextData << bitsLeft);
bitsLeft += 16;
}
ushort current10BitNr = (ushort)(currentData & ((1 << 10) - 1));
number[i] = convertNegative(current10BitNr, 10);
bitsLeft -= 10;
currentData = currentData >> 10;
}
matrix[rx, tx, sub] = new Complex(number[1], number[0]);
}
}
}
return matrix;
}
int convertNegative(int data, int maxbit)
{
if ((data & (1 << (maxbit - 1))) != 0)
{
/* negative */
data -= (1 << maxbit);
}
return data;
}
}
}