forked from kb1isz/linDmrMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPTC1969.c
252 lines (207 loc) · 7.55 KB
/
BPTC1969.c
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
/*
* Linux DMR Master server
Copyright (C) 2014 Wim Hofman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "master_server.h"
struct header {
bool responseRequested;
int dataPacketFormat;
int sapId;
int appendBlocks;
};
bool *extractInfo(bool bits[264]) {
static bool info[196];
int bytePos = 0;
int i;
unsigned char infoBits[200];
memset(infoBits, 0, 200);
//printf("Info bits: ");
for (i = 0; i < 98; i++) {
info[bytePos] = bits[i];
if (debug == 1) sprintf(infoBits, "%s%i", infoBits, info[bytePos]);
bytePos++;
}
for (i = 166; i < 264; i++) {
info[bytePos] = bits[i];
if (debug == 1) sprintf(infoBits, "%s%i", infoBits, info[bytePos]);
bytePos++;
}
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-extractInfo]%s", infoBits);
return info;
}
bool *deInterleave(bool *bits) {
static bool deInterleavedBits[196];
int a, interleaveSequence;
for (a = 0; a < 196; a++) {
interleaveSequence = (a * 181) % 196;
deInterleavedBits[a] = *(bits + interleaveSequence);
}
return deInterleavedBits;
}
bool *extractPayload(bool *deInterData) {
int a, pos = 0;
static bool outData[96];
for (a = 4; a <= 11; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 16; a <= 26; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 31; a <= 41; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 46; a <= 56; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 61; a <= 71; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 76; a <= 86; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 91; a <= 101; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 106; a <= 116; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
for (a = 121; a <= 131; a++) {
outData[pos] = *(deInterData + a);
pos++;
}
return outData;
}
struct header decodeDataHeader(bool bits[264]) {
bool *infoBits; //196 info bits
bool *deInterleavedBits; //196 bits
static bool *payloadBits; //96 bits
int blocksToFollow = 0, a;
unsigned char dpf = 0, sap = 0, bitPadding = 0;
struct header headerDecode;
unsigned char stringPayload[100];
memset(stringPayload, 0, 100);
infoBits = extractInfo(bits);
deInterleavedBits = deInterleave(infoBits);
payloadBits = extractPayload(deInterleavedBits);
if (debug == 1) {
for (a = 0; a < 96; a++) {
sprintf(stringPayload, "%s%i", stringPayload, *(payloadBits + a));
}
syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]%s", stringPayload);
}
if (*(payloadBits + 1) == 1) {
headerDecode.responseRequested = true;
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]response requested");
} else {
headerDecode.responseRequested = false;
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]NO response requested");
}
for (a = 4; a < 8; a++) {
if (*(payloadBits + a) == true) dpf = dpf + (char) (8 / pow(2, a - 4));
}
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Data Packet Format: ");
headerDecode.dataPacketFormat = dpf;
switch (dpf) {
case 0:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Unified Data Transport\n");
break;
case 1:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Response packet\n");
break;
case 2:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Data packet with unconfirmed delivery\n");
break;
case 3:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Data packet with confirmed delivery\n");
break;
case 13:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Short Data: Defined\n");
break;
case 14:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Short Data: Raw or Status/Precoded\n");
break;
case 15:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Proprietary Data Packet\n");
break;
}
for (a = 8; a < 12; a++) {
if (*(payloadBits + a) == true) sap = sap + (char) (8 / pow(2, a - 8));
}
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]SAP id: ");
headerDecode.sapId = sap;
switch (sap) {
case 0:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Unified Data Transport\n");
break;
case 2:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]TCP/IP header compression\n");
break;
case 3:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]UDP/IP header compression\n");
break;
case 4:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]IP based Packet data\n");
break;
case 5:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Address Resolution Protocol(ARP)\n");
break;
case 9:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Proprietary Packet data\n");
break;
case 10:
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Short Data\n");
break;
}
if (dpf == 13) {
for (a = 12; a < 16; a++) {//only AB in 2nd octet
if (*(payloadBits + a) == true) blocksToFollow = blocksToFollow + (char) (8 / pow(2, a - 12));
}
headerDecode.appendBlocks = blocksToFollow;
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeDataHeader]Appended blocks : %i\n", blocksToFollow);
for (a = 72; a < 80; a++) {
if (*(payloadBits + a) == true) bitPadding = bitPadding + (char) (128 / pow(2, a - 12));
}
}
return headerDecode;
}
unsigned char *decodeHalfRate(bool bits[264]) {
bool *infoBits; //196 info bits
bool *deInterleavedBits; //196 bits
static bool *payloadBits; //96 bits
int i, a, x = 0;
static unsigned char bb[12] = {0};
unsigned char bbb[100];
infoBits = extractInfo(bits);
deInterleavedBits = deInterleave(infoBits);
payloadBits = extractPayload(deInterleavedBits);
memset(bbb, 0, 100);
for (a = 0; a < 96; a = a + 8) {
bb[x] = 0;
for (i = 0; i < 8; i++) {
if (payloadBits[a + i] == true) bb[x] = bb[x] + (char) (128 / pow(2, i));
}
if (debug == 1) sprintf(bbb, "%s(%02X)%c", bbb, bb[x], bb[x]);
x++;
}
if (debug == 1) syslog(LOG_NOTICE, "[BPTC1969-decodeHalfRate]%s", bbb);
return bb;
}