blob: a26a5db4ab0afd693d5d0c78ffaaece77b5842c6 [file] [log] [blame]
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +02001/* encoding.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +020022#include <encoding.h>
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +020023#include <gprs_rlcmac.h>
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020024#include <bts.h>
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +020025#include <tbf.h>
26#include <gprs_debug.h>
27
Jacob Erlbeckf0e40392016-01-08 10:07:53 +010028#include <errno.h>
29#include <string.h>
30
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +020031// GSM 04.08 9.1.18 Immediate assignment
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +020032int Encoding::write_immediate_assignment(
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +020033 struct gprs_rlcmac_bts *bts,
34 bitvec * dest, uint8_t downlink, uint8_t ra,
35 uint32_t ref_fn, uint8_t ta, uint16_t arfcn, uint8_t ts, uint8_t tsc,
36 uint8_t tfi, uint8_t usf, uint32_t tlli,
37 uint8_t polling, uint32_t fn, uint8_t single_block, uint8_t alpha,
38 uint8_t gamma, int8_t ta_idx)
39{
40 unsigned wp = 0;
41 uint8_t plen;
42
43 bitvec_write_field(dest, wp,0x0,4); // Skip Indicator
44 bitvec_write_field(dest, wp,0x6,4); // Protocol Discriminator
45 bitvec_write_field(dest, wp,0x3F,8); // Immediate Assignment Message Type
46
47 // 10.5.2.25b Dedicated mode or TBF
48 bitvec_write_field(dest, wp,0x0,1); // spare
49 bitvec_write_field(dest, wp,0x0,1); // TMA : Two-message assignment: No meaning
50 bitvec_write_field(dest, wp,downlink,1); // Downlink : Downlink assignment to mobile in packet idle mode
51 bitvec_write_field(dest, wp,0x1,1); // T/D : TBF or dedicated mode: this message assigns a Temporary Block Flow (TBF).
52
53 bitvec_write_field(dest, wp,0x0,4); // Page Mode
54
55 // GSM 04.08 10.5.2.25a Packet Channel Description
56 bitvec_write_field(dest, wp,0x1,5); // Channel type
57 bitvec_write_field(dest, wp,ts,3); // TN
58 bitvec_write_field(dest, wp,tsc,3); // TSC
59 bitvec_write_field(dest, wp,0x0,3); // non-hopping RF channel configuraion
60 bitvec_write_field(dest, wp,arfcn,10); // ARFCN
61
62 //10.5.2.30 Request Reference
63 bitvec_write_field(dest, wp,ra,8); // RA
64 bitvec_write_field(dest, wp,(ref_fn / (26 * 51)) % 32,5); // T1'
65 bitvec_write_field(dest, wp,ref_fn % 51,6); // T3
66 bitvec_write_field(dest, wp,ref_fn % 26,5); // T2
67
68 // 10.5.2.40 Timing Advance
69 bitvec_write_field(dest, wp,0x0,2); // spare
70 bitvec_write_field(dest, wp,ta,6); // Timing Advance value
71
72 // No mobile allocation in non-hopping systems.
73 // A zero-length LV. Just write L=0.
74 bitvec_write_field(dest, wp,0,8);
75
76 if ((wp % 8)) {
77 LOGP(DRLCMACUL, LOGL_ERROR, "Length of IMM.ASS without rest "
78 "octets is not multiple of 8 bits, PLEASE FIX!\n");
79 exit (0);
80 }
81 plen = wp / 8;
82
83 if (downlink)
84 {
85 // GSM 04.08 10.5.2.16 IA Rest Octets
86 bitvec_write_field(dest, wp, 3, 2); // "HH"
87 bitvec_write_field(dest, wp, 1, 2); // "01" Packet Downlink Assignment
88 bitvec_write_field(dest, wp,tlli,32); // TLLI
89 bitvec_write_field(dest, wp,0x1,1); // switch TFI : on
90 bitvec_write_field(dest, wp,tfi,5); // TFI
91 bitvec_write_field(dest, wp,0x0,1); // RLC acknowledged mode
92 if (alpha) {
93 bitvec_write_field(dest, wp,0x1,1); // ALPHA = present
94 bitvec_write_field(dest, wp,alpha,4); // ALPHA
95 } else {
96 bitvec_write_field(dest, wp,0x0,1); // ALPHA = not present
97 }
98 bitvec_write_field(dest, wp,gamma,5); // GAMMA power control parameter
99 bitvec_write_field(dest, wp,polling,1); // Polling Bit
100 bitvec_write_field(dest, wp,!polling,1); // TA_VALID ???
101 if (ta_idx < 0) {
102 bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off
103 } else {
104 bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on
105 bitvec_write_field(dest, wp,ta_idx,4); // TIMING_ADVANCE_INDEX
106 }
107 if (polling) {
108 bitvec_write_field(dest, wp,0x1,1); // TBF Starting TIME present
109 bitvec_write_field(dest, wp,(fn / (26 * 51)) % 32,5); // T1'
110 bitvec_write_field(dest, wp,fn % 51,6); // T3
111 bitvec_write_field(dest, wp,fn % 26,5); // T2
112 } else {
113 bitvec_write_field(dest, wp,0x0,1); // TBF Starting TIME present
114 }
115 bitvec_write_field(dest, wp,0x0,1); // P0 not present
116// bitvec_write_field(dest, wp,0x1,1); // P0 not present
117// bitvec_write_field(dest, wp,0xb,4);
118 }
119 else
120 {
121 // GMS 04.08 10.5.2.37b 10.5.2.16
122 bitvec_write_field(dest, wp, 3, 2); // "HH"
123 bitvec_write_field(dest, wp, 0, 2); // "0" Packet Uplink Assignment
124 if (single_block) {
125 bitvec_write_field(dest, wp, 0, 1); // Block Allocation : Single Block Allocation
126 if (alpha) {
127 bitvec_write_field(dest, wp,0x1,1); // ALPHA = present
128 bitvec_write_field(dest, wp,alpha,4); // ALPHA = present
129 } else
130 bitvec_write_field(dest, wp,0x0,1); // ALPHA = not present
131 bitvec_write_field(dest, wp,gamma,5); // GAMMA power control parameter
132 if (ta_idx < 0) {
133 bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off
134 } else {
135 bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on
136 bitvec_write_field(dest, wp,ta_idx,4); // TIMING_ADVANCE_INDEX
137 }
138 bitvec_write_field(dest, wp, 1, 1); // TBF_STARTING_TIME_FLAG
139 bitvec_write_field(dest, wp,(fn / (26 * 51)) % 32,5); // T1'
140 bitvec_write_field(dest, wp,fn % 51,6); // T3
141 bitvec_write_field(dest, wp,fn % 26,5); // T2
142 } else {
143 bitvec_write_field(dest, wp, 1, 1); // Block Allocation : Not Single Block Allocation
144 bitvec_write_field(dest, wp, tfi, 5); // TFI_ASSIGNMENT Temporary Flow Identity
145 bitvec_write_field(dest, wp, 0, 1); // POLLING
146 bitvec_write_field(dest, wp, 0, 1); // ALLOCATION_TYPE: dynamic
147 bitvec_write_field(dest, wp, usf, 3); // USF
148 bitvec_write_field(dest, wp, 0, 1); // USF_GRANULARITY
149 bitvec_write_field(dest, wp, 0, 1); // "0" power control: Not Present
150 bitvec_write_field(dest, wp, bts->initial_cs_ul-1, 2); // CHANNEL_CODING_COMMAND
151 bitvec_write_field(dest, wp, 1, 1); // TLLI_BLOCK_CHANNEL_CODING
152 if (alpha) {
153 bitvec_write_field(dest, wp,0x1,1); // ALPHA = present
154 bitvec_write_field(dest, wp,alpha,4); // ALPHA
155 } else
156 bitvec_write_field(dest, wp,0x0,1); // ALPHA = not present
157 bitvec_write_field(dest, wp,gamma,5); // GAMMA power control parameter
158 /* note: there is no choise for TAI and no starting time */
159 bitvec_write_field(dest, wp, 0, 1); // switch TIMING_ADVANCE_INDEX = off
160 bitvec_write_field(dest, wp, 0, 1); // TBF_STARTING_TIME_FLAG
161 }
162 }
163
164 return plen;
165}
166
167/* generate uplink assignment */
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +0200168void Encoding::write_packet_uplink_assignment(
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200169 struct gprs_rlcmac_bts *bts,
170 bitvec * dest, uint8_t old_tfi,
171 uint8_t old_downlink, uint32_t tlli, uint8_t use_tlli,
Daniel Willmannf55e58f2014-08-07 15:02:16 +0200172 struct gprs_rlcmac_ul_tbf *tbf, uint8_t poll, uint8_t alpha,
Jacob Erlbeck76d767c2015-09-28 18:54:32 +0200173 uint8_t gamma, int8_t ta_idx, int8_t use_egprs)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200174{
175 // TODO We should use our implementation of encode RLC/MAC Control messages.
176 unsigned wp = 0;
177 uint8_t ts;
178
179 bitvec_write_field(dest, wp,0x1,2); // Payload Type
180 bitvec_write_field(dest, wp,0x0,2); // Uplink block with TDMA framenumber (N+13)
181 bitvec_write_field(dest, wp,poll,1); // Suppl/Polling Bit
182 bitvec_write_field(dest, wp,0x0,3); // Uplink state flag
183 bitvec_write_field(dest, wp,0xa,6); // MESSAGE TYPE
184
185 bitvec_write_field(dest, wp,0x0,2); // Page Mode
186
187 bitvec_write_field(dest, wp,0x0,1); // switch PERSIST_LEVEL: off
188 if (use_tlli) {
189 bitvec_write_field(dest, wp,0x2,2); // switch TLLI : on
190 bitvec_write_field(dest, wp,tlli,32); // TLLI
191 } else {
192 bitvec_write_field(dest, wp,0x0,1); // switch TFI : on
193 bitvec_write_field(dest, wp,old_downlink,1); // 0=UPLINK TFI, 1=DL TFI
194 bitvec_write_field(dest, wp,old_tfi,5); // TFI
195 }
196
Jacob Erlbeck76d767c2015-09-28 18:54:32 +0200197 if (!use_egprs) {
198 bitvec_write_field(dest, wp,0x0,1); // Message escape
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100199 bitvec_write_field(dest, wp,tbf->current_cs().to_num()-1, 2); // CHANNEL_CODING_COMMAND
Jacob Erlbeck76d767c2015-09-28 18:54:32 +0200200 bitvec_write_field(dest, wp,0x1,1); // TLLI_BLOCK_CHANNEL_CODING
201 bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on
202 bitvec_write_field(dest, wp,tbf->ta(),6); // TIMING_ADVANCE_VALUE
203 if (ta_idx < 0) {
204 bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off
205 } else {
206 bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on
207 bitvec_write_field(dest, wp,ta_idx,4); // TIMING_ADVANCE_INDEX
208 }
209
210 } else { /* EPGRS */
Jacob Erlbeck36df7742016-01-19 15:53:30 +0100211 unsigned int ws_enc = (tbf->m_window.ws() - 64) / 32;
Jacob Erlbeck76d767c2015-09-28 18:54:32 +0200212 bitvec_write_field(dest, wp,0x1,1); // Message escape
213 bitvec_write_field(dest, wp,0x0,2); // EGPRS message contents
214 bitvec_write_field(dest, wp,0x0,1); // No CONTENTION_RESOLUTION_TLLI
215 bitvec_write_field(dest, wp,0x0,1); // No COMPACT reduced MA
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100216 bitvec_write_field(dest, wp,tbf->current_cs().to_num()-1, 4); // EGPRS Modulation and Coding IE
Jacob Erlbeck76d767c2015-09-28 18:54:32 +0200217 bitvec_write_field(dest, wp,0x0,1); // No RESEGMENT
Jacob Erlbeck36df7742016-01-19 15:53:30 +0100218 bitvec_write_field(dest, wp,ws_enc,5); // EGPRS Window Size
Jacob Erlbeck76d767c2015-09-28 18:54:32 +0200219 bitvec_write_field(dest, wp,0x0,1); // No Access Technologies Request
220 bitvec_write_field(dest, wp,0x0,1); // No ARAC RETRANSMISSION REQUEST
221 bitvec_write_field(dest, wp,0x1,1); // TLLI_BLOCK_CHANNEL_CODING
222 bitvec_write_field(dest, wp,0x0,1); // No BEP_PERIOD2
223
224 bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_VALUE = on
225 bitvec_write_field(dest, wp,tbf->ta(),6); // TIMING_ADVANCE_VALUE
226 if (ta_idx < 0) {
227 bitvec_write_field(dest, wp,0x0,1); // switch TIMING_ADVANCE_INDEX = off
228 } else {
229 bitvec_write_field(dest, wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on
230 bitvec_write_field(dest, wp,ta_idx,4); // TIMING_ADVANCE_INDEX
231 }
232
233 bitvec_write_field(dest, wp,0x0,1); // No Packet Extended Timing Advance
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200234 }
235
236#if 1
237 bitvec_write_field(dest, wp,0x1,1); // Frequency Parameters information elements = present
Holger Hans Peter Freyther8f399de2013-12-25 20:22:35 +0100238 bitvec_write_field(dest, wp,tbf->tsc(),3); // Training Sequence Code (TSC)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200239 bitvec_write_field(dest, wp,0x0,2); // ARFCN = present
Holger Hans Peter Freytherfc498c92013-10-27 13:38:54 +0100240 bitvec_write_field(dest, wp,tbf->trx->arfcn,10); // ARFCN
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200241#else
242 bitvec_write_field(dest, wp,0x0,1); // Frequency Parameters = off
243#endif
244
245 bitvec_write_field(dest, wp,0x1,2); // Dynamic Allocation
Jacob Erlbeckedfd7e32015-11-17 16:30:58 +0100246
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200247 bitvec_write_field(dest, wp,0x0,1); // Extended Dynamic Allocation = off
248 bitvec_write_field(dest, wp,0x0,1); // P0 = off
Jacob Erlbeckedfd7e32015-11-17 16:30:58 +0100249
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200250 bitvec_write_field(dest, wp,0x0,1); // USF_GRANULARITY
251 bitvec_write_field(dest, wp,0x1,1); // switch TFI : on
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100252 bitvec_write_field(dest, wp,tbf->tfi(),5);// TFI
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200253
254 bitvec_write_field(dest, wp,0x0,1); //
255 bitvec_write_field(dest, wp,0x0,1); // TBF Starting Time = off
256 if (alpha || gamma) {
257 bitvec_write_field(dest, wp,0x1,1); // Timeslot Allocation with Power Control
258 bitvec_write_field(dest, wp,alpha,4); // ALPHA
259 } else
260 bitvec_write_field(dest, wp,0x0,1); // Timeslot Allocation
Jacob Erlbeckedfd7e32015-11-17 16:30:58 +0100261
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200262 for (ts = 0; ts < 8; ts++) {
263 if (tbf->pdch[ts]) {
264 bitvec_write_field(dest, wp,0x1,1); // USF_TN(i): on
Daniel Willmann7e994e32014-08-07 15:49:21 +0200265 bitvec_write_field(dest, wp,tbf->m_usf[ts],3); // USF_TN(i)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200266 if (alpha || gamma)
267 bitvec_write_field(dest, wp,gamma,5); // GAMMA power control parameter
268 } else
269 bitvec_write_field(dest, wp,0x0,1); // USF_TN(i): off
270 }
Jacob Erlbeckedfd7e32015-11-17 16:30:58 +0100271 // bitvec_write_field(dest, wp,0x0,1); // Measurement Mapping struct not present
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200272}
273
274
275/* generate downlink assignment */
Jacob Erlbeck166c9fc2016-01-07 16:04:29 +0100276void Encoding::write_packet_downlink_assignment(RlcMacDownlink_t * block,
277 uint8_t old_tfi, uint8_t old_downlink, struct gprs_rlcmac_tbf *tbf,
278 uint8_t poll, uint8_t alpha, uint8_t gamma, int8_t ta_idx, uint8_t ta_ts,
279 bool use_egprs)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200280{
281 // Packet downlink assignment TS 44.060 11.2.7
282
Jacob Erlbeck166c9fc2016-01-07 16:04:29 +0100283 PDA_AdditionsR99_t *pda_r99;
284
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200285 uint8_t tn;
Jacob Erlbeck36df7742016-01-19 15:53:30 +0100286 unsigned int ws_enc;
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200287
288 block->PAYLOAD_TYPE = 0x1; // RLC/MAC control block that does not include the optional octets of the RLC/MAC control header
289 block->RRBP = 0x0; // N+13
290 block->SP = poll; // RRBP field is valid
291 block->USF = 0x0; // Uplink state flag
292
293 block->u.Packet_Downlink_Assignment.MESSAGE_TYPE = 0x2; // Packet Downlink Assignment
294 block->u.Packet_Downlink_Assignment.PAGE_MODE = 0x0; // Normal Paging
295
296 block->u.Packet_Downlink_Assignment.Exist_PERSISTENCE_LEVEL = 0x0; // PERSISTENCE_LEVEL: off
297
298 block->u.Packet_Downlink_Assignment.ID.UnionType = 0x0; // TFI = on
299 block->u.Packet_Downlink_Assignment.ID.u.Global_TFI.UnionType = old_downlink; // 0=UPLINK TFI, 1=DL TFI
300 block->u.Packet_Downlink_Assignment.ID.u.Global_TFI.u.UPLINK_TFI = old_tfi; // TFI
301
302 block->u.Packet_Downlink_Assignment.MAC_MODE = 0x0; // Dynamic Allocation
303 block->u.Packet_Downlink_Assignment.RLC_MODE = 0x0; // RLC acknowledged mode
Daniel Willmann73191a42014-05-30 17:58:00 +0200304 block->u.Packet_Downlink_Assignment.CONTROL_ACK = tbf->was_releasing; // NW establishes no new DL TBF for the MS with running timer T3192
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200305 block->u.Packet_Downlink_Assignment.TIMESLOT_ALLOCATION = 0; // timeslot(s)
306 for (tn = 0; tn < 8; tn++) {
307 if (tbf->pdch[tn])
308 block->u.Packet_Downlink_Assignment.TIMESLOT_ALLOCATION |= 0x80 >> tn; // timeslot(s)
309 }
310
311 block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_TIMING_ADVANCE_VALUE = 0x1; // TIMING_ADVANCE_VALUE = on
Jacob Erlbeck9200ce62015-05-22 17:48:04 +0200312 block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_VALUE = tbf->ta(); // TIMING_ADVANCE_VALUE
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200313 if (ta_idx < 0) {
314 block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_IndexAndtimeSlot = 0x0; // TIMING_ADVANCE_INDEX = off
315 } else {
316 block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_IndexAndtimeSlot = 0x1; // TIMING_ADVANCE_INDEX = on
317 block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_INDEX = ta_idx; // TIMING_ADVANCE_INDEX
318 block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_TIMESLOT_NUMBER = ta_ts; // TIMING_ADVANCE_TS
319 }
320
321 block->u.Packet_Downlink_Assignment.Exist_P0_and_BTS_PWR_CTRL_MODE = 0x0; // POWER CONTROL = off
322
323 block->u.Packet_Downlink_Assignment.Exist_Frequency_Parameters = 0x1; // Frequency Parameters = on
Holger Hans Peter Freyther8f399de2013-12-25 20:22:35 +0100324 block->u.Packet_Downlink_Assignment.Frequency_Parameters.TSC = tbf->tsc(); // Training Sequence Code (TSC)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200325 block->u.Packet_Downlink_Assignment.Frequency_Parameters.UnionType = 0x0; // ARFCN = on
Holger Hans Peter Freytherfc498c92013-10-27 13:38:54 +0100326 block->u.Packet_Downlink_Assignment.Frequency_Parameters.u.ARFCN = tbf->trx->arfcn; // ARFCN
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200327
328 block->u.Packet_Downlink_Assignment.Exist_DOWNLINK_TFI_ASSIGNMENT = 0x1; // DOWNLINK TFI ASSIGNMENT = on
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100329 block->u.Packet_Downlink_Assignment.DOWNLINK_TFI_ASSIGNMENT = tbf->tfi(); // TFI
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200330
331 block->u.Packet_Downlink_Assignment.Exist_Power_Control_Parameters = 0x1; // Power Control Parameters = on
332 block->u.Packet_Downlink_Assignment.Power_Control_Parameters.ALPHA = alpha; // ALPHA
333
334 for (tn = 0; tn < 8; tn++)
335 {
336 if (tbf->pdch[tn])
337 {
338 block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[tn].Exist = 0x1; // Slot[i] = on
339 block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[tn].GAMMA_TN = gamma; // GAMMA_TN
340 }
341 else
342 {
343 block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[tn].Exist = 0x0; // Slot[i] = off
344 }
345 }
346
347 block->u.Packet_Downlink_Assignment.Exist_TBF_Starting_Time = 0x0; // TBF Starting TIME = off
348 block->u.Packet_Downlink_Assignment.Exist_Measurement_Mapping = 0x0; // Measurement_Mapping = off
Jacob Erlbeck166c9fc2016-01-07 16:04:29 +0100349 if (!use_egprs) {
350 block->u.Packet_Downlink_Assignment.Exist_AdditionsR99 = 0x0; // AdditionsR99 = off
351 return;
352 }
Jacob Erlbeck36df7742016-01-19 15:53:30 +0100353
354 ws_enc = (tbf->window()->ws() - 64) / 32;
355
Jacob Erlbeck166c9fc2016-01-07 16:04:29 +0100356 block->u.Packet_Downlink_Assignment.Exist_AdditionsR99 = 0x1; // AdditionsR99 = on
357 pda_r99 = &block->u.Packet_Downlink_Assignment.AdditionsR99;
358 pda_r99->Exist_EGPRS_Params = 1;
Jacob Erlbeck36df7742016-01-19 15:53:30 +0100359 pda_r99->EGPRS_WindowSize = ws_enc; /* see TS 44.060, table 12.5.2.1 */
Jacob Erlbeck166c9fc2016-01-07 16:04:29 +0100360 pda_r99->LINK_QUALITY_MEASUREMENT_MODE = 0x0; /* no meas, see TS 44.060, table 11.2.7.2 */
361 pda_r99->Exist_BEP_PERIOD2 = 0; /* No extra EGPRS BEP PERIOD */
362 pda_r99->Exist_Packet_Extended_Timing_Advance = 0;
363 pda_r99->Exist_COMPACT_ReducedMA = 0;
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200364}
365
366/* generate paging request */
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +0200367int Encoding::write_paging_request(bitvec * dest, uint8_t *ptmsi, uint16_t ptmsi_len)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200368{
369 unsigned wp = 0;
370 int plen;
371
372 bitvec_write_field(dest, wp,0x0,4); // Skip Indicator
373 bitvec_write_field(dest, wp,0x6,4); // Protocol Discriminator
374 bitvec_write_field(dest, wp,0x21,8); // Paging Request Message Type
375
376 bitvec_write_field(dest, wp,0x0,4); // Page Mode
377 bitvec_write_field(dest, wp,0x0,4); // Channel Needed
378
379 // Mobile Identity
380 bitvec_write_field(dest, wp,ptmsi_len+1,8); // Mobile Identity length
381 bitvec_write_field(dest, wp,0xf,4); // unused
382 bitvec_write_field(dest, wp,0x4,4); // PTMSI type
383 for (int i = 0; i < ptmsi_len; i++)
384 {
385 bitvec_write_field(dest, wp,ptmsi[i],8); // PTMSI
386 }
387 if ((wp % 8)) {
388 LOGP(DRLCMACUL, LOGL_ERROR, "Length of PAG.REQ without rest "
389 "octets is not multiple of 8 bits, PLEASE FIX!\n");
390 exit (0);
391 }
392 plen = wp / 8;
393 bitvec_write_field(dest, wp,0x0,1); // "L" NLN(PCH) = off
394 bitvec_write_field(dest, wp,0x0,1); // "L" Priority1 = off
395 bitvec_write_field(dest, wp,0x1,1); // "L" Priority2 = off
396 bitvec_write_field(dest, wp,0x0,1); // "L" Group Call information = off
397 bitvec_write_field(dest, wp,0x0,1); // "H" Packet Page Indication 1 = packet paging procedure
398 bitvec_write_field(dest, wp,0x1,1); // "H" Packet Page Indication 2 = packet paging procedure
399
400 return plen;
401}
402
Daniel Willmann5241c1a2013-12-11 14:35:29 +0100403/**
404 * The index of the array show_rbb is the bit position inside the rbb
405 * (show_rbb[63] relates to BSN ssn-1)
406 */
407void Encoding::encode_rbb(const char *show_rbb, uint8_t *rbb)
408{
409 uint8_t rbb_byte = 0;
410
411 // RECEIVE_BLOCK_BITMAP
412 for (int i = 0; i < 64; i++) {
413 /* Set bit at the appropriate position (see 3GPP TS 04.60 9.1.8.1) */
414 if (show_rbb[i] == 'R')
415 rbb_byte |= 1<< (7-(i%8));
416
417 if((i%8) == 7) {
418 rbb[i/8] = rbb_byte;
419 rbb_byte = 0;
420 }
421 }
422}
423
Jacob Erlbeck37005a12015-12-22 14:58:34 +0100424static void write_packet_ack_nack_desc_gprs(
425 struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp,
426 gprs_rlc_ul_window *window, bool is_final)
427{
428 char rbb[65];
429
430 window->update_rbb(rbb);
431
432 rbb[64] = 0;
433 LOGP(DRLCMACUL, LOGL_DEBUG, "- V(N): \"%s\" R=Received "
434 "I=Invalid\n", rbb);
435
436 bitvec_write_field(dest, wp, is_final, 1); // FINAL_ACK_INDICATION
437 bitvec_write_field(dest, wp, window->ssn(), 7); // STARTING_SEQUENCE_NUMBER
438
439 for (int i = 0; i < 64; i++) {
440 /* Set bit at the appropriate position (see 3GPP TS 04.60 9.1.8.1) */
441 bool is_ack = (rbb[i] == 'R');
442 bitvec_write_field(dest, wp, is_ack, 1);
443 }
444}
445
446static void write_packet_uplink_ack_gprs(
447 struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp,
448 struct gprs_rlcmac_ul_tbf *tbf, bool is_final)
449{
450
Jacob Erlbeckcb728902016-01-05 15:33:03 +0100451 bitvec_write_field(dest, wp, tbf->current_cs().to_num() - 1, 2); // CHANNEL_CODING_COMMAND
Jacob Erlbeck37005a12015-12-22 14:58:34 +0100452 write_packet_ack_nack_desc_gprs(bts, dest, wp, &tbf->m_window, is_final);
453
454 bitvec_write_field(dest, wp, 1, 1); // 1: have CONTENTION_RESOLUTION_TLLI
455 bitvec_write_field(dest, wp, tbf->tlli(), 32); // CONTENTION_RESOLUTION_TLLI
456
457 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Packet Timing Advance
458 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Power Control Parameters
459 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Extension Bits
460 bitvec_write_field(dest, wp, 0, 1); // fixed 0
461 bitvec_write_field(dest, wp, 1, 1); // 1: have Additions R99
462 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Packet Extended Timing Advance
463 bitvec_write_field(dest, wp, 1, 1); // TBF_EST (enabled)
464 bitvec_write_field(dest, wp, 0, 1); // 0: don't have REL 5
465};
466
467static void write_packet_ack_nack_desc_egprs(
468 struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp,
469 gprs_rlc_ul_window *window, bool is_final)
470{
471 int urbb_len = 0;
472 int crbb_len = 0;
473 int len;
474 bool bow = true;
475 bool eow = true;
476 int ssn = window->mod_sns(window->v_q() + 1);
477 int num_blocks = window->mod_sns(window->v_r() - window->v_q());
478 int esn_crbb = window->mod_sns(ssn - 1);
479 int rest_bits = dest->data_len * 8 - wp;
480
481 if (num_blocks > 0)
482 /* V(Q) is NACK and omitted -> SSN = V(Q) + 1 */
483 num_blocks -= 1;
484
485 if (num_blocks > window->ws())
486 num_blocks = window->ws();
487
488 if (num_blocks > rest_bits) {
489 eow = false;
490 urbb_len = rest_bits;
491 /* TODO: use compression, start encoding bits and stop when the
492 * space is exhausted. Use the first combination that encodes
493 * all bits. If there is none, use the combination that encodes
494 * the largest number of bits (e.g. by setting num_blocks to the
495 * max and repeating the construction).
496 */
497 } else if (num_blocks > rest_bits - 9) {
498 /* union bit and length field take 9 bits */
499 eow = false;
500 urbb_len = rest_bits - 9;
501 /* TODO: use compression (see above) */
502 }
503
504 if (urbb_len + crbb_len == rest_bits)
505 len = -1;
506 else if (crbb_len == 0)
507 len = urbb_len + 15;
508 else
509 len = urbb_len + crbb_len + 23;
510
511 /* EGPRS Ack/Nack Description IE */
512 if (len < 0) {
513 bitvec_write_field(dest, wp, 0, 1); // 0: don't have length
514 } else {
515 bitvec_write_field(dest, wp, 1, 1); // 1: have length
516 bitvec_write_field(dest, wp, len, 8); // length
517 }
518
519 bitvec_write_field(dest, wp, is_final, 1); // FINAL_ACK_INDICATION
520 bitvec_write_field(dest, wp, bow, 1); // BEGINNING_OF_WINDOW
521 bitvec_write_field(dest, wp, eow, 1); // END_OF_WINDOW
522 bitvec_write_field(dest, wp, ssn, 11); // STARTING_SEQUENCE_NUMBER
523 bitvec_write_field(dest, wp, 0, 1); // 0: don't have CRBB
524
525 /* TODO: Add CRBB support */
526
527 LOGP(DRLCMACUL, LOGL_DEBUG,
528 " - EGPRS URBB, len = %d, SSN = %d, ESN_CRBB = %d, "
529 "SNS = %d, WS = %d, V(Q) = %d, V(R) = %d%s%s\n",
530 urbb_len, ssn, esn_crbb,
531 window->sns(), window->ws(), window->v_q(), window->v_r(),
532 bow ? ", BOW" : "", eow ? ", EOW" : "");
533 for (int i = urbb_len; i > 0; i--) {
534 /* Set bit at the appropriate position (see 3GPP TS 04.60 12.3.1) */
535 bool is_ack = window->m_v_n.is_received(esn_crbb + i);
536 bitvec_write_field(dest, wp, is_ack, 1);
537 }
538}
539
540static void write_packet_uplink_ack_egprs(
541 struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp,
542 struct gprs_rlcmac_ul_tbf *tbf, bool is_final)
543{
544 bitvec_write_field(dest, wp, 0, 2); // fixed 00
545 bitvec_write_field(dest, wp, 2, 4); // CHANNEL_CODING_COMMAND: MCS-3
546 // bitvec_write_field(dest, wp, tbf->current_cs() - 1, 4); // CHANNEL_CODING_COMMAND
547 bitvec_write_field(dest, wp, 0, 1); // 0: no RESEGMENT (nyi)
548 bitvec_write_field(dest, wp, 1, 1); // PRE_EMPTIVE_TRANSMISSION, TODO: This resembles GPRS, change it?
549 bitvec_write_field(dest, wp, 0, 1); // 0: no PRR_RETRANSMISSION_REQUEST, TODO: clarify
550 bitvec_write_field(dest, wp, 0, 1); // 0: no ARAC_RETRANSMISSION_REQUEST, TODO: clarify
551 bitvec_write_field(dest, wp, 1, 1); // 1: have CONTENTION_RESOLUTION_TLLI
552 bitvec_write_field(dest, wp, tbf->tlli(), 32); // CONTENTION_RESOLUTION_TLLI
553 bitvec_write_field(dest, wp, 1, 1); // TBF_EST (enabled)
554 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Packet Timing Advance
555 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Packet Extended Timing Advance
556 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Power Control Parameters
557 bitvec_write_field(dest, wp, 0, 1); // 0: don't have Extension Bits
558
559 write_packet_ack_nack_desc_egprs(bts, dest, wp, &tbf->m_window, is_final);
560
561 bitvec_write_field(dest, wp, 0, 1); // fixed 0
562 bitvec_write_field(dest, wp, 0, 1); // 0: don't have REL 5
563};
564
565void Encoding::write_packet_uplink_ack(
566 struct gprs_rlcmac_bts *bts, bitvec * dest,
567 struct gprs_rlcmac_ul_tbf *tbf, bool is_final)
568{
569 unsigned wp = 0;
570
571 LOGP(DRLCMACUL, LOGL_DEBUG, "Encoding Ack/Nack for %s "
572 "(final=%d)\n", tbf_name(tbf), is_final);
573
574 bitvec_write_field(dest, wp, 0x1, 2); // Payload Type
575 bitvec_write_field(dest, wp, 0x0, 2); // Uplink block with TDMA framenumber (N+13)
576 bitvec_write_field(dest, wp, is_final, 1); // Suppl/Polling Bit
577 bitvec_write_field(dest, wp, 0x0, 3); // Uplink state flag
578 bitvec_write_field(dest, wp, 0x9, 6); // MESSAGE TYPE Uplink Ack/Nack
579 bitvec_write_field(dest, wp, 0x0, 2); // Page Mode
580
581 bitvec_write_field(dest, wp, 0x0, 2); // fixed 00
582 bitvec_write_field(dest, wp, tbf->tfi(), 5); // Uplink TFI
583
584 if (tbf->is_egprs_enabled()) {
585 /* PU_AckNack_EGPRS = on */
586 bitvec_write_field(dest, wp, 1, 1); // 1: EGPRS
587 write_packet_uplink_ack_egprs(bts, dest, wp, tbf, is_final);
588 } else {
589 /* PU_AckNack_GPRS = on */
590 bitvec_write_field(dest, wp, 0, 1); // 0: GPRS
591 write_packet_uplink_ack_gprs(bts, dest, wp, tbf, is_final);
592 }
593
594 LOGP(DRLCMACUL, LOGL_DEBUG,
595 "Uplink Ack/Nack bit count %d, max %d, message = %s\n",
596 wp, dest->data_len * 8,
597 osmo_hexdump(dest->data, dest->data_len));
598}
599
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +0200600unsigned Encoding::write_packet_paging_request(bitvec * dest)
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200601{
602 unsigned wp = 0;
603
604 bitvec_write_field(dest, wp,0x1,2); // Payload Type
605 bitvec_write_field(dest, wp,0x0,3); // No polling
606 bitvec_write_field(dest, wp,0x0,3); // Uplink state flag
607 bitvec_write_field(dest, wp,0x22,6); // MESSAGE TYPE
608
609 bitvec_write_field(dest, wp,0x0,2); // Page Mode
610
611 bitvec_write_field(dest, wp,0x0,1); // No PERSISTENCE_LEVEL
612 bitvec_write_field(dest, wp,0x0,1); // No NLN
613
614 return wp;
615}
616
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +0200617unsigned Encoding::write_repeated_page_info(bitvec * dest, unsigned& wp, uint8_t len,
Holger Hans Peter Freyther6ca0a902013-10-19 18:50:22 +0200618 uint8_t *identity, uint8_t chan_needed)
619{
620 bitvec_write_field(dest, wp,0x1,1); // Repeated Page info exists
621
622 bitvec_write_field(dest, wp,0x1,1); // RR connection paging
623
624 if ((identity[0] & 0x07) == 4) {
625 bitvec_write_field(dest, wp,0x0,1); // TMSI
626 identity++;
627 len--;
628 } else {
629 bitvec_write_field(dest, wp,0x0,1); // MI
630 bitvec_write_field(dest, wp,len,4); // MI len
631 }
632 while (len) {
633 bitvec_write_field(dest, wp,*identity++,8); // MI data
634 len--;
635 }
636 bitvec_write_field(dest, wp,chan_needed,2); // CHANNEL_NEEDED
637 bitvec_write_field(dest, wp,0x0,1); // No eMLPP_PRIORITY
638
639 return wp;
640}
641
Jacob Erlbeckf0e40392016-01-08 10:07:53 +0100642int Encoding::rlc_write_dl_data_header(const struct gprs_rlc_data_info *rlc,
643 uint8_t *data)
644{
645 struct gprs_rlc_dl_header_egprs_3 *egprs3;
646 struct rlc_dl_header *gprs;
647 unsigned int e_fbi_header;
648 GprsCodingScheme cs = rlc->cs;
649
650 switch(cs.headerTypeData()) {
651 case GprsCodingScheme::HEADER_GPRS_DATA:
652 gprs = static_cast<struct rlc_dl_header *>
653 ((void *)data);
654
655 gprs->usf = rlc->usf;
656 gprs->s_p = rlc->es_p != 0 ? 1 : 0;
657 gprs->rrbp = rlc->rrbp;
658 gprs->pt = 0;
659 gprs->tfi = rlc->tfi;
660 gprs->pr = rlc->pr;
661
662 gprs->fbi = rlc->block_info[0].cv == 0;
663 gprs->e = rlc->block_info[0].e;
664 gprs->bsn = rlc->block_info[0].bsn;
665 break;
666
667 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_3:
668 egprs3 = static_cast<struct gprs_rlc_dl_header_egprs_3 *>
669 ((void *)data);
670
671 egprs3->usf = rlc->usf;
672 egprs3->es_p = rlc->es_p;
673 egprs3->rrbp = rlc->rrbp;
674 egprs3->tfi_a = rlc->tfi >> 0; /* 1 bit LSB */
675 egprs3->tfi_b = rlc->tfi >> 1; /* 4 bits */
676 egprs3->pr = rlc->pr;
677 egprs3->cps = rlc->cps;
678
679 egprs3->bsn1_a = rlc->block_info[0].bsn >> 0; /* 2 bits LSB */
680 egprs3->bsn1_b = rlc->block_info[0].bsn >> 2; /* 8 bits */
681 egprs3->bsn1_c = rlc->block_info[0].bsn >> 10; /* 1 bit */
682
683 egprs3->spb = rlc->block_info[0].spb;
684
685 e_fbi_header = rlc->block_info[0].e ? 0x01 : 0;
686 e_fbi_header |= rlc->block_info[0].cv == 0 ? 0x02 : 0; /* FBI */
687 e_fbi_header <<= 7;
688 data[3] = (data[3] & 0b01111111) | (e_fbi_header >> 0);
689 data[4] = (data[4] & 0b11111110) | (e_fbi_header >> 8);
690 break;
691
692 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_1:
693 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_2:
694 /* TODO: Support both header types */
695 /* fall through */
696 default:
697 LOGP(DRLCMACDL, LOGL_ERROR,
698 "Encoding of uplink %s data blocks not yet supported.\n",
699 cs.name());
700 return -ENOTSUP;
701 };
702
703 return 0;
704}
705
706/**
707 * \brief Copy LSB bitstream RLC data block from byte aligned buffer.
708 *
709 * Note that the bitstream is encoded in LSB first order, so the two octets
710 * 654321xx xxxxxx87 contain the octet 87654321 starting at bit position 3
711 * (LSB has bit position 1). This is a different order than the one used by
712 * CSN.1.
713 *
714 * \param data_block_idx The block index, 0..1 for header type 1, 0 otherwise
715 * \param src A pointer to the start of the RLC block (incl. the header)
716 * \param buffer A data area of a least the size of the RLC block
717 * \returns the number of bytes copied
718 */
719unsigned int Encoding::rlc_copy_from_aligned_buffer(
720 const struct gprs_rlc_data_info *rlc,
721 unsigned int data_block_idx,
722 uint8_t *dst, const uint8_t *buffer)
723{
724 unsigned int hdr_bytes;
725 unsigned int extra_bits;
726 unsigned int i;
727
728 uint8_t c, last_c;
729 const uint8_t *src;
730 const struct gprs_rlc_data_block_info *rdbi;
731
732 OSMO_ASSERT(data_block_idx < rlc->num_data_blocks);
733 rdbi = &rlc->block_info[data_block_idx];
734
735 hdr_bytes = rlc->data_offs_bits[data_block_idx] / 8;
736 extra_bits = (rlc->data_offs_bits[data_block_idx] % 8);
737
738 if (extra_bits == 0) {
739 /* It is aligned already */
740 memmove(dst + hdr_bytes, buffer, rdbi->data_len);
741 return rdbi->data_len;
742 }
743
744 src = buffer;
745 dst = dst + hdr_bytes;
746 last_c = *dst << (8 - extra_bits);
747
748 for (i = 0; i < rdbi->data_len; i++) {
749 c = src[i];
750 *(dst++) = (last_c >> (8 - extra_bits)) | (c << extra_bits);
751 last_c = c;
752 }
753
754 /* overwrite the lower extra_bits */
755 *dst = (*dst & (0xff << extra_bits)) | (last_c >> (8 - extra_bits));
756
757 return rdbi->data_len;
758}
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100759
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100760static Encoding::AppendResult rlc_data_to_dl_append_gprs(
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100761 struct gprs_rlc_data_block_info *rdbi,
762 gprs_llc *llc, int *offset, int *num_chunks,
763 uint8_t *data_block,
764 bool is_final)
765{
766 int chunk;
767 int space;
768 struct rlc_li_field *li;
769 uint8_t *delimiter, *data, *e_pointer;
770
771 data = data_block + *offset;
772 delimiter = data_block + *num_chunks;
773 e_pointer = (*num_chunks ? delimiter - 1 : NULL);
774
775 chunk = llc->chunk_size();
776 space = rdbi->data_len - *offset;
777
778 /* if chunk will exceed block limit */
779 if (chunk > space) {
780 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
781 "larger than space (%d) left in block: copy "
782 "only remaining space, and we are done\n",
783 chunk, space);
784 /* block is filled, so there is no extension */
785 if (e_pointer)
786 *e_pointer |= 0x01;
787 /* fill only space */
788 llc->consume(data, space);
789 /* return data block as message */
790 *offset = rdbi->data_len;
791 (*num_chunks)++;
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100792 return Encoding::AR_NEED_MORE_BLOCKS;
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100793 }
794 /* if FINAL chunk would fit precisely in space left */
795 if (chunk == space && is_final)
796 {
797 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
798 "would exactly fit into space (%d): because "
799 "this is a final block, we don't add length "
800 "header, and we are done\n", chunk, space);
801 /* block is filled, so there is no extension */
802 if (e_pointer)
803 *e_pointer |= 0x01;
804 /* fill space */
805 llc->consume(data, space);
806 *offset = rdbi->data_len;
807 (*num_chunks)++;
808 rdbi->cv = 0;
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100809 return Encoding::AR_COMPLETED_BLOCK_FILLED;
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100810 }
811 /* if chunk would fit exactly in space left */
812 if (chunk == space) {
813 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
814 "would exactly fit into space (%d): add length "
815 "header with LI=0, to make frame extend to "
816 "next block, and we are done\n", chunk, space);
817 /* make space for delimiter */
818 if (delimiter != data)
819 memmove(delimiter + 1, delimiter,
820 data - delimiter);
821 data++;
822 (*offset)++;
823 space--;
824 /* add LI with 0 length */
825 li = (struct rlc_li_field *)delimiter;
826 li->e = 1; /* not more extension */
827 li->m = 0; /* shall be set to 0, in case of li = 0 */
828 li->li = 0; /* chunk fills the complete space */
829 rdbi->e = 0; /* 0: extensions present */
830 // no need to set e_pointer nor increase delimiter
831 /* fill only space, which is 1 octet less than chunk */
832 llc->consume(data, space);
833 /* return data block as message */
834 *offset = rdbi->data_len;
835 (*num_chunks)++;
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100836 return Encoding::AR_NEED_MORE_BLOCKS;
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100837 }
838
839 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
840 "than remaining space (%d): add length header to "
841 "to delimit LLC frame\n", chunk, space);
842 /* the LLC frame chunk ends in this block */
843 /* make space for delimiter */
844 if (delimiter != data)
845 memmove(delimiter + 1, delimiter, data - delimiter);
846 data++;
847 (*offset)++;
848 space--;
849 /* add LI to delimit frame */
850 li = (struct rlc_li_field *)delimiter;
851 li->e = 0; /* Extension bit, maybe set later */
852 li->m = 0; /* will be set later, if there is more LLC data */
853 li->li = chunk; /* length of chunk */
854 rdbi->e = 0; /* 0: extensions present */
855 (*num_chunks)++;
856 /* copy (rest of) LLC frame to space and reset later */
857 llc->consume(data, chunk);
858 data += chunk;
859 space -= chunk;
860 (*offset) += chunk;
861 /* if we have more data and we have space left */
862 if (space > 0 && !is_final) {
863 li->m = 1; /* we indicate more frames to follow */
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100864 return Encoding::AR_COMPLETED_SPACE_LEFT;
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100865 }
866 /* if we don't have more LLC frames */
867 if (is_final) {
868 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
869 "done.\n");
870 li->e = 1; /* we cannot extend */
871 rdbi->cv = 0;
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100872 return Encoding::AR_COMPLETED_BLOCK_FILLED;
Jacob Erlbeck14bb0942016-01-12 11:58:13 +0100873 }
874 /* we have no space left */
875 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
876 "done.\n");
877 li->e = 1; /* we cannot extend */
Jacob Erlbeck5058bd62016-01-13 10:51:25 +0100878 return Encoding::AR_COMPLETED_BLOCK_FILLED;
879}
880
Jacob Erlbecka88d0652016-01-13 11:28:10 +0100881static Encoding::AppendResult rlc_data_to_dl_append_egprs(
882 struct gprs_rlc_data_block_info *rdbi,
883 gprs_llc *llc, int *offset, int *num_chunks,
884 uint8_t *data_block,
885 bool is_final)
886{
887 int chunk;
888 int space;
889 struct rlc_li_field_egprs *li;
890 struct rlc_li_field_egprs *prev_li;
891 uint8_t *delimiter, *data;
892
893 data = data_block + *offset;
894 delimiter = data_block + *num_chunks;
895 prev_li = (struct rlc_li_field_egprs *)
896 (*num_chunks ? delimiter - 1 : NULL);
897
898 chunk = llc->chunk_size();
899 space = rdbi->data_len - *offset;
900
901 /* if chunk will exceed block limit */
902 if (chunk > space) {
903 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
904 "larger than space (%d) left in block: copy "
905 "only remaining space, and we are done\n",
906 chunk, space);
907 /* fill only space */
908 llc->consume(data, space);
909 /* return data block as message */
910 *offset = rdbi->data_len;
911 (*num_chunks)++;
912 return Encoding::AR_NEED_MORE_BLOCKS;
913 }
914 /* if FINAL chunk would fit precisely in space left */
915 if (chunk == space && is_final)
916 {
917 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
918 "would exactly fit into space (%d): because "
919 "this is a final block, we don't add length "
920 "header, and we are done\n", chunk, space);
921 /* fill space */
922 llc->consume(data, space);
923 *offset = rdbi->data_len;
924 (*num_chunks)++;
925 rdbi->cv = 0;
926 return Encoding::AR_COMPLETED_BLOCK_FILLED;
927 }
928 /* if chunk would fit exactly in space left */
929 if (chunk == space) {
930 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
931 "would exactly fit into space (%d): just copy "
932 "it, and we are done. The next block will have "
933 "to start with an empty chunk\n",
934 chunk, space);
935 /* fill space */
936 llc->consume(data, space);
937 *offset = rdbi->data_len;
938 (*num_chunks)++;
939 return Encoding::AR_NEED_MORE_BLOCKS;
940 }
941
942 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
943 "than remaining space (%d): add length header to "
944 "to delimit LLC frame\n", chunk, space);
945 /* the LLC frame chunk ends in this block */
946 /* make space for delimiter */
947
948 if (delimiter != data)
949 memmove(delimiter + 1, delimiter, data - delimiter);
950
951 data += 1;
952 (*offset) += 1;
953 space -= 1;
954 /* add LI to delimit frame */
955 li = (struct rlc_li_field_egprs *)delimiter;
956 li->e = 1; /* Extension bit, maybe set later */
957 li->li = chunk; /* length of chunk */
958 /* tell previous extension header about the new one */
959 if (prev_li)
960 prev_li->e = 0;
961 rdbi->e = 0; /* 0: extensions present */
962 delimiter++;
963 prev_li = li;
964 (*num_chunks)++;
965 /* copy (rest of) LLC frame to space and reset later */
966 llc->consume(data, chunk);
967 data += chunk;
968 space -= chunk;
969 (*offset) += chunk;
970 /* if we have more data and we have space left */
971 if (space > 0) {
972 if (!is_final)
973 return Encoding::AR_COMPLETED_SPACE_LEFT;
974
975 /* we don't have more LLC frames */
976 /* We will have to add another chunk with filling octets */
977 LOGP(DRLCMACDL, LOGL_DEBUG,
978 "-- There is remaining space (%d): add filling byte chunk\n",
979 space);
980
981 if (delimiter != data)
982 memmove(delimiter + 1, delimiter, data - delimiter);
983
984 data += 1;
985 (*offset) += 1;
986 space -= 1;
987
988 /* set filling bytes extension */
989 li = (struct rlc_li_field_egprs *)delimiter;
990 li->e = 1;
991 li->li = 127;
992
993 /* tell previous extension header about the new one */
994 if (prev_li)
995 prev_li->e = 0;
996
997 delimiter++;
998 (*num_chunks)++;
999
1000 rdbi->cv = 0;
1001
1002 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
1003 "are done.\n");
1004
1005 *offset = rdbi->data_len;
1006 return Encoding::AR_COMPLETED_BLOCK_FILLED;
1007 }
1008
1009 if (is_final) {
1010 /* we don't have more LLC frames */
1011 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
1012 "are done.\n");
1013 rdbi->cv = 0;
1014 return Encoding::AR_COMPLETED_BLOCK_FILLED;
1015 }
1016
1017 /* we have no space left */
1018 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
1019 "done.\n");
1020 return Encoding::AR_COMPLETED_BLOCK_FILLED;
1021}
1022
Jacob Erlbeck5058bd62016-01-13 10:51:25 +01001023Encoding::AppendResult Encoding::rlc_data_to_dl_append(
1024 struct gprs_rlc_data_block_info *rdbi, GprsCodingScheme cs,
1025 gprs_llc *llc, int *offset, int *num_chunks,
1026 uint8_t *data_block,
1027 bool is_final)
1028{
1029 if (cs.isGprs())
1030 return rlc_data_to_dl_append_gprs(rdbi,
1031 llc, offset, num_chunks, data_block, is_final);
1032
Jacob Erlbecka88d0652016-01-13 11:28:10 +01001033 if (cs.isEgprs())
1034 return rlc_data_to_dl_append_egprs(rdbi,
1035 llc, offset, num_chunks, data_block, is_final);
1036
Jacob Erlbeck5058bd62016-01-13 10:51:25 +01001037 LOGP(DRLCMACDL, LOGL_ERROR, "%s data block encoding not implemented\n",
1038 cs.name());
1039
1040 return AR_NEED_MORE_BLOCKS;
Jacob Erlbeck14bb0942016-01-12 11:58:13 +01001041}