-
Notifications
You must be signed in to change notification settings - Fork 3
/
gprs.c
258 lines (208 loc) · 5.61 KB
/
gprs.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
253
254
255
256
257
#include "string.h"
#include <arpa/inet.h>
#include "bit_func.h"
#include "cch.h"
#include "punct.h"
#include "viterbi.h"
#include "gprs.h"
#include "rlcmac.h"
#include "gsm_interleave.h"
#include "crc.h"
static unsigned map_cs2[456];
static unsigned map_cs3[456];
void gprs_init()
{
fill_punct_cs2(map_cs2);
fill_punct_cs3(map_cs3);
memset(tbf_table, 0, sizeof(tbf_table));
}
inline unsigned distance(const uint8_t *a, const uint8_t *b, const unsigned size)
{
int i, distance = 0;
for (i=0; i<size; i++) {
distance += !!(a[i] ^ b[i]);
}
return distance;
}
enum {CS1 = 0, CS2, CS3, CS4};
int cs_estimate(const uint8_t *sflags)
{
int i;
unsigned cs_dist[4];
const uint8_t cs_pattern[][8] = {{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 1, 0}};
for (i=0;i<4;i++) {
cs_dist[i] = distance(sflags, cs_pattern[i], 8);
}
if (cs_dist[0] < cs_dist[1])
i = CS1;
else
i = CS2;
if (cs_dist[2] < cs_dist[i])
i = CS3;
if (cs_dist[3] < cs_dist[i])
i = CS4;
return i;
}
int usf6_estimate(const uint8_t *data)
{
int i, min;
unsigned usf_dist[8];
const uint8_t usf_pattern[][6] = {{0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 1},
{0, 1, 0, 1, 1, 0},
{0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 0, 0, 0}};
for (i=0; i<8; i++) {
usf_dist[i] = distance(data, usf_pattern[i], 6);
}
for (i=1, min=0; i<8; i++) {
if (usf_dist[i] < usf_dist[min])
min = i;
}
return min;
}
int usf12_estimate(const uint8_t *data)
{
int i, min;
unsigned usf_dist[8];
const uint8_t usf_pattern[][12] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0},
{0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
{1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1},
{1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0},
{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0}};
for (i=0; i<8; i++) {
usf_dist[i] = distance(data, usf_pattern[i], 12);
}
for (i=1, min=0; i<8; i++) {
if (usf_dist[i] < usf_dist[min])
min = i;
}
return min;
}
int process_pdch(struct session_info *s, struct l1ctl_burst_ind *bi, uint8_t *gprs_msg)
{
int len, ret, usf;
uint8_t ts, ul;
uint32_t fn;
uint16_t arfcn;
struct burst_buf *bb;
struct radio_message m;
uint8_t conv_data[CONV_SIZE];
int8_t depunct_data[2*CONV_SIZE];
uint8_t decoded_data[2*CONV_SIZE];
const uint8_t ccitt_poly[16 + 1] = {1, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0,
1};
const uint8_t ccitt_rem[16] = {1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1};
/* get burst parameters */
fn = ntohl(bi->frame_nr);
arfcn = ntohs(bi->band_arfcn);
ul = !!(arfcn & ARFCN_UPLINK);
ts = bi->chan_nr & 7;
/* select frame queue */
if (ul)
bb = &s->gprs[2*ts + 0];
else
bb = &s->gprs[2*ts + 1];
/* check burst alignment */
if (((fn % 13) % 4) != bb->count)
return 0;
/* enqueue data into message buffer */
expand_msb(bi->bits, bb->data + bb->count * 114, 114);
/* save stealing flags */
bb->sbit[bb->count * 2 + 0] = !!(bi->bits[14] & 0x10);
bb->sbit[bb->count * 2 + 1] = !!(bi->bits[14] & 0x20);
bb->snr[bb->count] = bi->snr;
bb->rxl[bb->count] = bi->rx_level;
bb->arfcn[bb->count] = arfcn;
bb->fn[bb->count] = fn;
bb->count++;
/* Return if not enough bursts for a full message */
if (bb->count < 4)
return 0;
/* de-interleaving */
memset(conv_data, 0, sizeof(conv_data));
gsm_deinter_sacch(bb->data, conv_data);
len = 0;
switch (cs_estimate(bb->sbit)) {
case CS1:
len = decode_signalling(conv_data, gprs_msg);
break;
case CS2:
/* depuncture and convert to soft bits */
depunct(conv_data, depunct_data, 294*2, map_cs2);
/* Viterbi decode */
conv_cch_decode(depunct_data, decoded_data, 294);
/* decode USF bits */
usf = usf6_estimate(decoded_data);
/* rebuild original data string for CRC check */
decoded_data[3] = (usf >> 2) & 1;
decoded_data[4] = (usf >> 1) & 1;
decoded_data[5] = (usf >> 0) & 1;
/* compute CRC-16 (CCITT) */
ret = parity_check(decoded_data + 3, 271, ccitt_poly, ccitt_rem, 16);
if (!ret) {
compress_lsb(decoded_data + 3, gprs_msg, 33 * 8);
len = 33;
}
break;
case CS3:
/* depuncture and convert to soft bits */
depunct(conv_data, depunct_data, 338*2, map_cs3);
/* Viterbi decode */
conv_cch_decode(depunct_data, decoded_data, 338);
/* decode USF bits */
usf = usf6_estimate(decoded_data);
/* rebuild original data string for CRC check */
decoded_data[3] = (usf >> 2) & 1;
decoded_data[4] = (usf >> 1) & 1;
decoded_data[5] = (usf >> 0) & 1;
/* compute CRC-16 (CCITT) */
ret = parity_check(decoded_data + 3, 315, ccitt_poly, ccitt_rem, 16);
if (!ret) {
compress_lsb(decoded_data + 3, gprs_msg, 39 * 8);
len = 39;
}
break;
case CS4:
/* decode USF bits */
usf = usf12_estimate(conv_data);
/* rebuild original data string for CRC check */
conv_data[9] = (usf >> 2) & 1;
conv_data[10] = (usf >> 1) & 1;
conv_data[11] = (usf >> 0) & 1;
/* compute CRC-16 (CCITT) */
ret = parity_check(conv_data + 9, 431, ccitt_poly, ccitt_rem, 16);
if (!ret) {
compress_lsb(conv_data + 9, gprs_msg, 53 * 8);
len = 53; // last byte not used (0x2b)
}
break;
}
/* if a message is decoded */
if (len) {
/* fill gprs message struct */
memcpy(&m.bb, bb, sizeof(*bb));
m.rat = RAT_GSM;
m.domain = DOMAIN_PS;
m.chan_nr = ts;
m.msg_len = len;
memcpy(m.msg, gprs_msg, len);
/* call handler */
rlc_type_handler(&m);
}
/* reset buffer */
memset(bb->data, 0, sizeof(bb->data));
bb->count = 0;
return len;
}