forked from E3V3A/gsm-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccch.c
172 lines (136 loc) · 3.48 KB
/
ccch.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
#include "cch.h"
#include "bit_func.h"
#include "l3_handler.h"
#include "gsm_interleave.h"
#include "output.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <osmocom/gsm/a5.h>
#include <assert.h>
uint8_t compute_ber(struct session_info *s, struct radio_message *m)
{
unsigned i, j, uplink;
unsigned bit_errors = 0;
uint8_t raw_coded[CONV_SIZE];
encode_signalling(m->msg, raw_coded);
uplink = !!(m->bb.arfcn[0] & ARFCN_UPLINK);
if (m->flags & MSG_CIPHERED) {
uint8_t ks[114];
for (j=0; j<4; j++) {
if (uplink)
osmo_a5(s->cipher, s->key, m->bb.fn[j], 0, ks);
else
osmo_a5(s->cipher, s->key, m->bb.fn[j], ks, 0);
for (i=0; i<114; i++) {
if ((raw_coded[j*114+i]^ks[i]) != m->bb.data[j*114+i])
bit_errors++;
}
}
} else {
for (j=0; j<4; j++) {
for (i=0; i<114; i++) {
if (raw_coded[j*114+i] != m->bb.data[j*114+i])
bit_errors++;
}
}
}
return (bit_errors > CONV_SIZE/2 ? CONV_SIZE/2 : bit_errors);
}
int try_decode(struct session_info *s, struct radio_message *m)
{
int ret, uplink;
unsigned i, j;
int8_t snr_amp;
int8_t deciphered[CONV_SIZE];
int8_t conv_data[CONV_SIZE];
uplink = !!(m->bb.arfcn[0] & ARFCN_UPLINK);
if (m->flags & MSG_CIPHERED) {
uint8_t ks[114];
for (j=0; j<4; j++) {
if (uplink)
osmo_a5(s->cipher, s->key, m->bb.fn[j], 0, ks);
else
osmo_a5(s->cipher, s->key, m->bb.fn[j], ks, 0);
snr_amp = m->bb.snr[j] >> 1;
for (i=0; i<114; i++) {
deciphered[j*114+i] = (m->bb.data[j*114+i]^ks[i] ? -snr_amp : snr_amp);
}
}
} else {
for (j=0; j<4; j++) {
snr_amp = m->bb.snr[j] >> 1;
for (i=0; i<114; i++) {
deciphered[j*114+i] = (m->bb.data[j*114+i] ? -snr_amp : snr_amp);
}
}
}
gsm_deinter_sacch((uint8_t*)deciphered, (uint8_t*)conv_data);
ret = decode_signalling(conv_data, m->msg);
if (ret) {
m->flags |= MSG_DECODED;
m->msg_len = 23;
m->rat = RAT_GSM;
if (m->flags & MSG_SACCH)
handle_lapdm(s, &s->chan_sacch[uplink], &m->msg[2], 21, m->bb.fn[0], uplink);
else
handle_lapdm(s, &s->chan_sdcch[uplink], m->msg, 23, m->bb.fn[0], uplink);
}
return ret;
}
uint8_t chan_burst_id(uint8_t chan_nr)
{
return 0;
}
void process_ccch(struct session_info *s, struct burst_buf *bb, struct l1ctl_burst_ind *bi)
{
struct radio_message *m;
uint32_t fn;
uint16_t arfcn;
uint8_t type;
/* append data to message buffer */
assert(bb->count <= 3);
expand_msb(bi->bits, bb->data + bb->count * 114, 114);
fn = ntohl(bi->frame_nr);
arfcn = ntohs(bi->band_arfcn);
//type = chan_detect(fn, bi->chan_nr, &subch);
type = (bi->chan_nr < 0x20) ? 1 : 0;
/* if a burst is already present */
if (bb->count) {
if (!type) {
uint32_t next_fn = (bb->fn[bb->count-1] + 1) % (2048*26*51);
/* check burst ordering */
if (fn != next_fn) {
bb->count = 0;
return;
}
}
}
bb->snr[bb->count] = bi->snr;
bb->rxl[bb->count] = bi->rx_level;
bb->fn[bb->count] = fn;
bb->arfcn[bb->count] = arfcn;
bb->count++;
/* Return if not enough bursts for a full gsm message */
if (bb->count < 4)
return;
/* fill new message structure */
m = malloc(sizeof(struct radio_message));
m->chan_nr = bi->chan_nr;
if (bi->flags & BI_FLG_SACCH) {
m->flags = MSG_SACCH;
} else {
m->flags = MSG_SDCCH;
}
if (s->cipher || (type && not_zero(s->key, 8)))
m->flags |= MSG_CIPHERED;
memcpy(&m->bb, bb, sizeof(*bb));
m->info[0] = 0;
s->new_msg = m;
/* ready for decoding */
try_decode(s, m);
/* reset burst buffer */
bb->count = 0;
return;
}