Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 1 | /* 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 Freyther | 63f29d6 | 2013-10-19 19:04:03 +0200 | [diff] [blame] | 22 | #include <encoding.h> |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 23 | #include <gprs_rlcmac.h> |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 24 | #include <bts.h> |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 25 | #include <tbf.h> |
| 26 | #include <gprs_debug.h> |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 27 | #include <egprs_rlc_compression.h> |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 28 | |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 29 | extern "C" { |
| 30 | #include <osmocom/gprs/protocol/gsm_04_60.h> |
| 31 | } |
| 32 | |
Max | d572054 | 2016-07-14 15:41:17 +0200 | [diff] [blame] | 33 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 34 | |
| 35 | #include <stdbool.h> |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 36 | #include <errno.h> |
| 37 | #include <string.h> |
| 38 | |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 39 | /* { 0 | 1 < TIMING_ADVANCE_INDEX : bit (4) > } */ |
| 40 | static inline bool write_tai(bitvec *dest, unsigned& wp, int8_t tai) |
| 41 | { |
| 42 | if (tai < 0) { /* No TIMING_ADVANCE_INDEX: */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 43 | bitvec_write_field(dest, &wp, 0, 1); |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | /* TIMING_ADVANCE_INDEX: */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 47 | bitvec_write_field(dest, &wp, 1, 1); |
| 48 | bitvec_write_field(dest, &wp, tai, 4); |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
| 52 | /* { 0 | 1 < TIMING_ADVANCE_VALUE : bit (6) > } */ |
| 53 | static inline void write_ta(bitvec *dest, unsigned& wp, int8_t ta) |
| 54 | { |
| 55 | if (ta < 0) /* No TIMING_ADVANCE_VALUE: */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 56 | bitvec_write_field(dest, &wp, 0, 1); |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 57 | else { /* TIMING_ADVANCE_VALUE: */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 58 | bitvec_write_field(dest, &wp, 1, 1); |
| 59 | bitvec_write_field(dest, &wp, ta, 6); |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Max | a4f570f | 2017-12-15 11:21:57 +0100 | [diff] [blame] | 63 | /* 3GPP TS 44.060 Table 12.5.2.1 */ |
| 64 | static inline uint16_t enc_ws(uint16_t ws) |
| 65 | { |
| 66 | return (ws - 64) / 32; |
| 67 | } |
| 68 | |
| 69 | static inline void write_ws(bitvec *dest, unsigned int *write_index, uint16_t ws) |
| 70 | { |
| 71 | dest->cur_bit = *write_index; |
| 72 | |
| 73 | bitvec_set_u64(dest, enc_ws(ws), 5, false); |
| 74 | |
| 75 | *write_index += 5; |
| 76 | } |
| 77 | |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 78 | /* 3GPP TS 44.060 ยง 12.12: |
| 79 | { 0 | 1 < TIMING_ADVANCE_VALUE : bit (6) > } |
| 80 | { 0 | 1 < TIMING_ADVANCE_INDEX : bit (4) > |
| 81 | < TIMING_ADVANCE_TIMESLOT_NUMBER : bit (3) > } |
| 82 | */ |
| 83 | static inline void write_ta_ie(bitvec *dest, unsigned& wp, |
| 84 | int8_t ta, int8_t tai, uint8_t ts) |
| 85 | { |
| 86 | write_ta(dest, wp, ta); |
| 87 | if (write_tai(dest, wp, tai)) /* TIMING_ADVANCE_TIMESLOT_NUMBER: */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 88 | bitvec_write_field(dest, &wp, ts, 3); |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 91 | static int write_ia_rest_downlink( |
| 92 | gprs_rlcmac_dl_tbf *tbf, |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 93 | bitvec * dest, unsigned& wp, |
Max | d572054 | 2016-07-14 15:41:17 +0200 | [diff] [blame] | 94 | uint8_t polling, bool ta_valid, uint32_t fn, |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 95 | uint8_t alpha, uint8_t gamma, int8_t ta_idx) |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 96 | { |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 97 | if (!tbf) { |
| 98 | LOGP(DRLCMACDL, LOGL_ERROR, |
| 99 | "Cannot encode DL IMMEDIATE ASSIGNMENT without TBF\n"); |
| 100 | return -EINVAL; |
| 101 | } |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 102 | // GSM 04.08 10.5.2.16 IA Rest Octets |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 103 | bitvec_write_field(dest, &wp, 3, 2); // "HH" |
| 104 | bitvec_write_field(dest, &wp, 1, 2); // "01" Packet Downlink Assignment |
| 105 | bitvec_write_field(dest, &wp,tbf->tlli(),32); // TLLI |
| 106 | bitvec_write_field(dest, &wp,0x1,1); // switch TFI : on |
| 107 | bitvec_write_field(dest, &wp,tbf->tfi(),5); // TFI |
| 108 | bitvec_write_field(dest, &wp,0x0,1); // RLC acknowledged mode |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 109 | if (alpha) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 110 | bitvec_write_field(dest, &wp,0x1,1); // ALPHA = present |
| 111 | bitvec_write_field(dest, &wp,alpha,4); // ALPHA |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 112 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 113 | bitvec_write_field(dest, &wp,0x0,1); // ALPHA = not present |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 114 | } |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 115 | bitvec_write_field(dest, &wp,gamma,5); // GAMMA power control parameter |
| 116 | bitvec_write_field(dest, &wp,polling,1); // Polling Bit |
| 117 | bitvec_write_field(dest, &wp, ta_valid, 1); // N. B: NOT related to TAI! |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 118 | write_tai(dest, wp, ta_idx); |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 119 | if (polling) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 120 | bitvec_write_field(dest, &wp,0x1,1); // TBF Starting TIME present |
| 121 | bitvec_write_field(dest, &wp,(fn / (26 * 51)) % 32,5); // T1' |
| 122 | bitvec_write_field(dest, &wp,fn % 51,6); // T3 |
| 123 | bitvec_write_field(dest, &wp,fn % 26,5); // T2 |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 124 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 125 | bitvec_write_field(dest, &wp,0x0,1); // TBF Starting TIME present |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 126 | } |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 127 | bitvec_write_field(dest, &wp,0x0,1); // P0 not present |
| 128 | // bitvec_write_field(dest, &wp,0x1,1); // P0 not present |
| 129 | // bitvec_write_field(dest, &wp,,0xb,4); |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 130 | if (tbf->is_egprs_enabled()) { |
| 131 | /* see GMS 44.018, 10.5.2.16 */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 132 | bitvec_write_field(dest, &wp, 1, 1); // "H" |
Max | a4f570f | 2017-12-15 11:21:57 +0100 | [diff] [blame] | 133 | write_ws(dest, &wp, tbf->window_size()); // EGPRS Window Size |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 134 | bitvec_write_field(dest, &wp, 0x0, 2); // LINK_QUALITY_MEASUREMENT_MODE |
| 135 | bitvec_write_field(dest, &wp, 0, 1); // BEP_PERIOD2 not present |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 136 | } |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 137 | |
| 138 | return 0; |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 141 | static int write_ia_rest_uplink( |
| 142 | gprs_rlcmac_ul_tbf *tbf, |
| 143 | bitvec * dest, unsigned& wp, |
| 144 | uint8_t usf, uint32_t fn, |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 145 | uint8_t alpha, uint8_t gamma, int8_t ta_idx) |
| 146 | { |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 147 | OSMO_ASSERT(!tbf || !tbf->is_egprs_enabled()); |
| 148 | |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 149 | // GMS 04.08 10.5.2.37b 10.5.2.16 |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 150 | bitvec_write_field(dest, &wp, 3, 2); // "HH" |
| 151 | bitvec_write_field(dest, &wp, 0, 2); // "0" Packet Uplink Assignment |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 152 | if (tbf == NULL) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 153 | bitvec_write_field(dest, &wp, 0, 1); // Block Allocation : Single Block Allocation |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 154 | if (alpha) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 155 | bitvec_write_field(dest, &wp,0x1,1); // ALPHA = present |
| 156 | bitvec_write_field(dest, &wp,alpha,4); // ALPHA = present |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 157 | } else |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 158 | bitvec_write_field(dest, &wp,0x0,1); // ALPHA = not present |
| 159 | bitvec_write_field(dest, &wp,gamma,5); // GAMMA power control parameter |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 160 | write_tai(dest, wp, ta_idx); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 161 | bitvec_write_field(dest, &wp, 1, 1); // TBF_STARTING_TIME_FLAG |
| 162 | bitvec_write_field(dest, &wp,(fn / (26 * 51)) % 32,5); // T1' |
| 163 | bitvec_write_field(dest, &wp,fn % 51,6); // T3 |
| 164 | bitvec_write_field(dest, &wp,fn % 26,5); // T2 |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 165 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 166 | bitvec_write_field(dest, &wp, 1, 1); // Block Allocation : Not Single Block Allocation |
| 167 | bitvec_write_field(dest, &wp, tbf->tfi(), 5); // TFI_ASSIGNMENT Temporary Flow Identity |
| 168 | bitvec_write_field(dest, &wp, 0, 1); // POLLING |
| 169 | bitvec_write_field(dest, &wp, 0, 1); // ALLOCATION_TYPE: dynamic |
| 170 | bitvec_write_field(dest, &wp, usf, 3); // USF |
| 171 | bitvec_write_field(dest, &wp, 0, 1); // USF_GRANULARITY |
| 172 | bitvec_write_field(dest, &wp, 0, 1); // "0" power control: Not Present |
| 173 | bitvec_write_field(dest, &wp, tbf->current_cs().to_num()-1, 2); // CHANNEL_CODING_COMMAND |
| 174 | bitvec_write_field(dest, &wp, 1, 1); // TLLI_BLOCK_CHANNEL_CODING |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 175 | if (alpha) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 176 | bitvec_write_field(dest, &wp,0x1,1); // ALPHA = present |
| 177 | bitvec_write_field(dest, &wp,alpha,4); // ALPHA |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 178 | } else |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 179 | bitvec_write_field(dest, &wp,0x0,1); // ALPHA = not present |
| 180 | bitvec_write_field(dest, &wp,gamma,5); // GAMMA power control parameter |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 181 | /* note: there is no choise for TAI and no starting time */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 182 | bitvec_write_field(dest, &wp, 0, 1); // switch TIMING_ADVANCE_INDEX = off |
| 183 | bitvec_write_field(dest, &wp, 0, 1); // TBF_STARTING_TIME_FLAG |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 184 | } |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 185 | return 0; |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 188 | static int write_ia_rest_egprs_uplink( |
| 189 | gprs_rlcmac_ul_tbf *tbf, |
| 190 | bitvec * dest, unsigned& wp, |
| 191 | uint8_t usf, uint32_t fn, |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 192 | uint8_t alpha, uint8_t gamma, int8_t ta_idx, |
| 193 | enum ph_burst_type burst_type, uint16_t ra) |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 194 | { |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 195 | uint8_t extended_ra = 0; |
| 196 | |
| 197 | extended_ra = (ra & 0x1F); |
| 198 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 199 | bitvec_write_field(dest, &wp, 1, 2); /* LH */ |
| 200 | bitvec_write_field(dest, &wp, 0, 2); /* 0 EGPRS Uplink Assignment */ |
| 201 | bitvec_write_field(dest, &wp, extended_ra, 5); /* Extended RA */ |
| 202 | bitvec_write_field(dest, &wp, 0, 1); /* Access technology Request */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 203 | |
| 204 | if (tbf == NULL) { |
| 205 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 206 | bitvec_write_field(dest, &wp, 0, 1); /* multiblock allocation */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 207 | |
| 208 | if (alpha) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 209 | bitvec_write_field(dest, &wp, 0x1, 1); /* ALPHA =yes */ |
| 210 | bitvec_write_field(dest, &wp, alpha, 4); /* ALPHA */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 211 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 212 | bitvec_write_field(dest, &wp, 0x0, 1); /* ALPHA = no */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 213 | } |
| 214 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 215 | bitvec_write_field(dest, &wp, gamma, 5); /* GAMMA power contrl */ |
| 216 | bitvec_write_field(dest, &wp, (fn / (26 * 51)) % 32, 5);/* T1' */ |
| 217 | bitvec_write_field(dest, &wp, fn % 51, 6); /* T3 */ |
| 218 | bitvec_write_field(dest, &wp, fn % 26, 5); /* T2 */ |
| 219 | bitvec_write_field(dest, &wp, 0, 2); /* Radio block allocation */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 220 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 221 | bitvec_write_field(dest, &wp, 0, 1); |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 222 | |
| 223 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 224 | bitvec_write_field(dest, &wp, 1, 1); /* single block alloc */ |
| 225 | bitvec_write_field(dest, &wp, tbf->tfi(), 5);/* TFI assignment */ |
| 226 | bitvec_write_field(dest, &wp, 0, 1); /* polling bit */ |
| 227 | bitvec_write_field(dest, &wp, 0, 1); /* constant */ |
| 228 | bitvec_write_field(dest, &wp, usf, 3); /* USF bit */ |
| 229 | bitvec_write_field(dest, &wp, 0, 1); /* USF granularity */ |
| 230 | bitvec_write_field(dest, &wp, 0, 1); /* P0 */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 231 | /* MCS */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 232 | bitvec_write_field(dest, &wp, tbf->current_cs().to_num()-1, 4); |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 233 | /* tlli channel block */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 234 | bitvec_write_field(dest, &wp, tbf->tlli(), 1); |
| 235 | bitvec_write_field(dest, &wp, 0, 1); /* BEP period present */ |
| 236 | bitvec_write_field(dest, &wp, 0, 1); /* resegmentation */ |
Max | a4f570f | 2017-12-15 11:21:57 +0100 | [diff] [blame] | 237 | write_ws(dest, &wp, tbf->window_size()); /* EGPRS window size */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 238 | |
| 239 | if (alpha) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 240 | bitvec_write_field(dest, &wp, 0x1, 1); /* ALPHA =yes */ |
| 241 | bitvec_write_field(dest, &wp, alpha, 4); /* ALPHA */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 242 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 243 | bitvec_write_field(dest, &wp, 0x0, 1); /* ALPHA = no */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 244 | } |
| 245 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 246 | bitvec_write_field(dest, &wp, gamma, 5); /* GAMMA power contrl */ |
| 247 | bitvec_write_field(dest, &wp, 0, 1); /* TIMING_ADVANCE_INDEX */ |
| 248 | bitvec_write_field(dest, &wp, 0, 1); /* TBF_STARTING_TIME_FLAG */ |
| 249 | bitvec_write_field(dest, &wp, 0, 1); /* NULL */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return 0; |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 255 | /* |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 256 | * Immediate assignment reject, sent on the CCCH/AGCH |
| 257 | * see GSM 44.018, 9.1.20 + 10.5.2.30 |
| 258 | */ |
| 259 | int Encoding::write_immediate_assignment_reject( |
| 260 | bitvec *dest, uint16_t ra, |
| 261 | uint32_t ref_fn, |
| 262 | enum ph_burst_type burst_type) |
| 263 | { |
| 264 | unsigned wp = 0; |
| 265 | int plen; |
| 266 | int i; |
| 267 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 268 | bitvec_write_field(dest, &wp, 0x0, 4); // Skip Indicator |
| 269 | bitvec_write_field(dest, &wp, 0x6, 4); // Protocol Discriminator |
| 270 | bitvec_write_field(dest, &wp, 0x3A, 8); // Immediate Assign Message Type |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 271 | |
| 272 | // feature indicator |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 273 | bitvec_write_field(dest, &wp, 0x0, 1); // spare |
| 274 | bitvec_write_field(dest, &wp, 0x0, 1); // spare |
| 275 | bitvec_write_field(dest, &wp, 0x0, 1); // no cs |
| 276 | bitvec_write_field(dest, &wp, 0x1, 1); // implicit detach for PS |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 277 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 278 | bitvec_write_field(dest, &wp, 0x0, 4); // Page Mode |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 279 | /* |
| 280 | * 9.1.20.2 of 44.018 version 11.7.0 Release 11 |
| 281 | * Filling of the message |
| 282 | * If necessary the request reference information element and the |
| 283 | * wait indication information element should be duplicated to |
| 284 | * fill the message. |
| 285 | * TODO: group rejection for multiple MS |
| 286 | */ |
| 287 | for (i = 0; i < 4; i++) { |
| 288 | //10.5.2.30 Request Reference |
| 289 | if (((burst_type == GSM_L1_BURST_TYPE_ACCESS_1) || |
| 290 | (burst_type == GSM_L1_BURST_TYPE_ACCESS_2))) { |
| 291 | //9.1.20.2a of 44.018 version 11.7.0 Release 11 |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 292 | bitvec_write_field(dest, &wp, 0x7f, 8); /* RACH value */ |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 293 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 294 | bitvec_write_field(dest, &wp, ra, 8); /* RACH value */ |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 295 | } |
| 296 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 297 | bitvec_write_field(dest, &wp, |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 298 | (ref_fn / (26 * 51)) % 32, 5); // T1' |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 299 | bitvec_write_field(dest, &wp, ref_fn % 51, 6); // T3 |
| 300 | bitvec_write_field(dest, &wp, ref_fn % 26, 5); // T2 |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 301 | |
| 302 | /* TODO: Make it configurable */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 303 | bitvec_write_field(dest, &wp, 20, 8); //Wait Indication 1 |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | plen = wp / 8; |
| 307 | |
| 308 | if ((wp % 8)) { |
| 309 | LOGP(DRLCMACUL, LOGL_ERROR, "Length of IMM.ASS.Rej without" |
| 310 | "rest octets is not multiple of 8 bits, PLEASE FIX!\n"); |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | // Extended RA |
| 315 | else if (((burst_type == GSM_L1_BURST_TYPE_ACCESS_1) || |
| 316 | (burst_type == GSM_L1_BURST_TYPE_ACCESS_2))) { |
| 317 | //9.1.20.2a of 44.018 version 11.7.0 Release 11 |
| 318 | uint8_t extended_ra = 0; |
| 319 | |
| 320 | extended_ra = (ra & 0x1F); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 321 | bitvec_write_field(dest, &wp, 0x1, 1); |
| 322 | bitvec_write_field(dest, &wp, extended_ra, 5); /* Extended RA */ |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 323 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 324 | bitvec_write_field(dest, &wp, 0x0, 1); |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 325 | } |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 326 | bitvec_write_field(dest, &wp, 0x0, 1); |
| 327 | bitvec_write_field(dest, &wp, 0x0, 1); |
| 328 | bitvec_write_field(dest, &wp, 0x0, 1); |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 329 | |
| 330 | return plen; |
| 331 | } |
| 332 | |
Max | 0a8fae8 | 2017-03-08 18:53:30 +0100 | [diff] [blame] | 333 | static inline void log_alert_exit(const char * error) |
| 334 | { |
| 335 | LOGP(DRLCMACUL, LOGL_ERROR, error); |
| 336 | pcu_tx_txt_ind(PCU_OML_ALERT, error); |
| 337 | exit(1); |
| 338 | } |
| 339 | |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 340 | /* |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 341 | * Immediate assignment, sent on the CCCH/AGCH |
| 342 | * see GSM 04.08, 9.1.18 and GSM 44.018, 9.1.18 + 10.5.2.16 |
| 343 | */ |
Holger Hans Peter Freyther | 63f29d6 | 2013-10-19 19:04:03 +0200 | [diff] [blame] | 344 | int Encoding::write_immediate_assignment( |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 345 | struct gprs_rlcmac_tbf *tbf, |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 346 | bitvec * dest, uint8_t downlink, uint16_t ra, |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 347 | uint32_t ref_fn, uint8_t ta, uint16_t arfcn, uint8_t ts, uint8_t tsc, |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 348 | uint8_t usf, uint8_t polling, uint32_t fn, uint8_t alpha, |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 349 | uint8_t gamma, int8_t ta_idx, enum ph_burst_type burst_type, uint8_t sb) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 350 | { |
| 351 | unsigned wp = 0; |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 352 | int plen; |
| 353 | int rc; |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 354 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 355 | bitvec_write_field(dest, &wp,0x0,4); // Skip Indicator |
| 356 | bitvec_write_field(dest, &wp,0x6,4); // Protocol Discriminator |
| 357 | bitvec_write_field(dest, &wp,0x3F,8); // Immediate Assignment Message Type |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 358 | |
| 359 | // 10.5.2.25b Dedicated mode or TBF |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 360 | bitvec_write_field(dest, &wp,0x0,1); // spare |
| 361 | bitvec_write_field(dest, &wp,0x0,1); // TMA : Two-message assignment: No meaning |
| 362 | bitvec_write_field(dest, &wp,downlink,1); // Downlink : Downlink assignment to mobile in packet idle mode |
| 363 | bitvec_write_field(dest, &wp,0x1,1); // T/D : TBF or dedicated mode: this message assigns a Temporary Block Flow (TBF). |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 364 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 365 | bitvec_write_field(dest, &wp,0x0,4); // Page Mode |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 366 | |
| 367 | // GSM 04.08 10.5.2.25a Packet Channel Description |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 368 | bitvec_write_field(dest, &wp,0x1,5); // Channel type |
| 369 | bitvec_write_field(dest, &wp,ts,3); // TN |
| 370 | bitvec_write_field(dest, &wp,tsc,3); // TSC |
| 371 | bitvec_write_field(dest, &wp,0x0,3); // non-hopping RF channel configuraion |
| 372 | bitvec_write_field(dest, &wp,arfcn,10); // ARFCN |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 373 | |
| 374 | //10.5.2.30 Request Reference |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 375 | if (((burst_type == GSM_L1_BURST_TYPE_ACCESS_1) || |
| 376 | (burst_type == GSM_L1_BURST_TYPE_ACCESS_2))) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 377 | bitvec_write_field(dest, &wp, 0x7f, 8); /* RACH value */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 378 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 379 | bitvec_write_field(dest, &wp, ra, 8); /* RACH value */ |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 380 | } |
| 381 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 382 | bitvec_write_field(dest, &wp,(ref_fn / (26 * 51)) % 32,5); // T1' |
| 383 | bitvec_write_field(dest, &wp,ref_fn % 51,6); // T3 |
| 384 | bitvec_write_field(dest, &wp,ref_fn % 26,5); // T2 |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 385 | |
| 386 | // 10.5.2.40 Timing Advance |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 387 | bitvec_write_field(dest, &wp,0x0,2); // spare |
| 388 | bitvec_write_field(dest, &wp,ta,6); // Timing Advance value |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 389 | |
| 390 | // No mobile allocation in non-hopping systems. |
| 391 | // A zero-length LV. Just write L=0. |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 392 | bitvec_write_field(dest, &wp,0,8); |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 393 | |
Max | 0a8fae8 | 2017-03-08 18:53:30 +0100 | [diff] [blame] | 394 | if ((wp % 8)) |
| 395 | log_alert_exit("Length of IMM.ASS without rest octets is not " |
| 396 | "multiple of 8 bits, PLEASE FIX!\n"); |
| 397 | |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 398 | plen = wp / 8; |
| 399 | |
| 400 | if (downlink) |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 401 | rc = write_ia_rest_downlink(as_dl_tbf(tbf), dest, wp, |
Max | d572054 | 2016-07-14 15:41:17 +0200 | [diff] [blame] | 402 | polling, gsm48_ta_is_valid(ta), fn, |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 403 | alpha, gamma, ta_idx); |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 404 | else if (((burst_type == GSM_L1_BURST_TYPE_ACCESS_1) || |
| 405 | (burst_type == GSM_L1_BURST_TYPE_ACCESS_2))) |
Jacob Erlbeck | be80c36 | 2016-02-01 16:39:52 +0100 | [diff] [blame] | 406 | rc = write_ia_rest_egprs_uplink(as_ul_tbf(tbf), dest, wp, |
| 407 | usf, fn, |
bhargava | 465f5bb | 2016-09-02 16:50:43 +0530 | [diff] [blame] | 408 | alpha, gamma, ta_idx, burst_type, ra); |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 409 | else |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 410 | rc = write_ia_rest_uplink(as_ul_tbf(tbf), dest, wp, |
| 411 | usf, fn, |
Jacob Erlbeck | 18831c3 | 2016-02-01 15:23:35 +0100 | [diff] [blame] | 412 | alpha, gamma, ta_idx); |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 413 | |
Jacob Erlbeck | 2647a33 | 2016-02-01 16:13:38 +0100 | [diff] [blame] | 414 | if (rc < 0) { |
| 415 | LOGP(DRLCMAC, LOGL_ERROR, |
| 416 | "Failed to create IMMEDIATE ASSIGMENT (%s) for %s\n", |
| 417 | downlink ? "downlink" : "uplink", |
| 418 | tbf ? tbf->name() : "single block allocation"); |
| 419 | return rc; |
| 420 | } |
| 421 | |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 422 | return plen; |
| 423 | } |
| 424 | |
| 425 | /* generate uplink assignment */ |
Holger Hans Peter Freyther | 63f29d6 | 2013-10-19 19:04:03 +0200 | [diff] [blame] | 426 | void Encoding::write_packet_uplink_assignment( |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 427 | struct gprs_rlcmac_bts *bts, |
| 428 | bitvec * dest, uint8_t old_tfi, |
| 429 | uint8_t old_downlink, uint32_t tlli, uint8_t use_tlli, |
Jacob Erlbeck | f2694b7 | 2016-01-26 21:46:26 +0100 | [diff] [blame] | 430 | struct gprs_rlcmac_ul_tbf *tbf, uint8_t poll, uint8_t rrbp, uint8_t alpha, |
Jacob Erlbeck | 76d767c | 2015-09-28 18:54:32 +0200 | [diff] [blame] | 431 | uint8_t gamma, int8_t ta_idx, int8_t use_egprs) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 432 | { |
| 433 | // TODO We should use our implementation of encode RLC/MAC Control messages. |
| 434 | unsigned wp = 0; |
| 435 | uint8_t ts; |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 436 | /* timeslot assigned for the Continuous Timing Advance procedure */ |
| 437 | uint8_t ta_ts = 0; /* FIXME: supply it as parameter from caller */ |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 438 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 439 | bitvec_write_field(dest, &wp,0x1,2); // Payload Type |
| 440 | bitvec_write_field(dest, &wp,0x0,2); // Uplink block with TDMA framenumber (N+13) |
| 441 | bitvec_write_field(dest, &wp,poll,1); // Suppl/Polling Bit |
| 442 | bitvec_write_field(dest, &wp,0x0,3); // Uplink state flag |
| 443 | bitvec_write_field(dest, &wp,0xa,6); // MESSAGE TYPE |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 444 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 445 | bitvec_write_field(dest, &wp,0x0,2); // Page Mode |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 446 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 447 | bitvec_write_field(dest, &wp,0x0,1); // switch PERSIST_LEVEL: off |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 448 | if (use_tlli) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 449 | bitvec_write_field(dest, &wp,0x2,2); // switch TLLI : on |
| 450 | bitvec_write_field(dest, &wp,tlli,32); // TLLI |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 451 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 452 | bitvec_write_field(dest, &wp,0x0,1); // switch TFI : on |
| 453 | bitvec_write_field(dest, &wp,old_downlink,1); // 0=UPLINK TFI, 1=DL TFI |
| 454 | bitvec_write_field(dest, &wp,old_tfi,5); // TFI |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Jacob Erlbeck | 76d767c | 2015-09-28 18:54:32 +0200 | [diff] [blame] | 457 | if (!use_egprs) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 458 | bitvec_write_field(dest, &wp,0x0,1); // Message escape |
| 459 | bitvec_write_field(dest, &wp,tbf->current_cs().to_num()-1, 2); // CHANNEL_CODING_COMMAND |
| 460 | bitvec_write_field(dest, &wp,0x1,1); // TLLI_BLOCK_CHANNEL_CODING |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 461 | write_ta_ie(dest, wp,tbf->ta(), ta_idx, ta_ts); |
Jacob Erlbeck | 76d767c | 2015-09-28 18:54:32 +0200 | [diff] [blame] | 462 | } else { /* EPGRS */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 463 | bitvec_write_field(dest, &wp,0x1,1); // Message escape |
| 464 | bitvec_write_field(dest, &wp,0x0,2); // EGPRS message contents |
| 465 | bitvec_write_field(dest, &wp,0x0,1); // No CONTENTION_RESOLUTION_TLLI |
| 466 | bitvec_write_field(dest, &wp,0x0,1); // No COMPACT reduced MA |
| 467 | bitvec_write_field(dest, &wp,tbf->current_cs().to_num()-1, 4); // EGPRS Modulation and Coding IE |
Aravind Sirsikar | 505a86d | 2016-07-26 18:26:21 +0530 | [diff] [blame] | 468 | /* 0: no RESEGMENT, 1: Segmentation*/ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 469 | bitvec_write_field(dest, &wp, 0x1, 1); |
Max | a4f570f | 2017-12-15 11:21:57 +0100 | [diff] [blame] | 470 | write_ws(dest, &wp, tbf->window_size()); // EGPRS Window Size |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 471 | bitvec_write_field(dest, &wp,0x0,1); // No Access Technologies Request |
| 472 | bitvec_write_field(dest, &wp,0x0,1); // No ARAC RETRANSMISSION REQUEST |
| 473 | bitvec_write_field(dest, &wp,0x1,1); // TLLI_BLOCK_CHANNEL_CODING |
| 474 | bitvec_write_field(dest, &wp,0x0,1); // No BEP_PERIOD2 |
Max | ae48381 | 2016-07-28 12:03:08 +0200 | [diff] [blame] | 475 | write_ta_ie(dest, wp,tbf->ta(), ta_idx, ta_ts); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 476 | bitvec_write_field(dest, &wp,0x0,1); // No Packet Extended Timing Advance |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | #if 1 |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 480 | bitvec_write_field(dest, &wp,0x1,1); // Frequency Parameters information elements = present |
| 481 | bitvec_write_field(dest, &wp,tbf->tsc(),3); // Training Sequence Code (TSC) |
| 482 | bitvec_write_field(dest, &wp,0x0,2); // ARFCN = present |
| 483 | bitvec_write_field(dest, &wp,tbf->trx->arfcn,10); // ARFCN |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 484 | #else |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 485 | bitvec_write_field(dest, &wp,0x0,1); // Frequency Parameters = off |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 486 | #endif |
| 487 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 488 | bitvec_write_field(dest, &wp,0x1,2); // Dynamic Allocation |
Jacob Erlbeck | edfd7e3 | 2015-11-17 16:30:58 +0100 | [diff] [blame] | 489 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 490 | bitvec_write_field(dest, &wp,0x0,1); // Extended Dynamic Allocation = off |
| 491 | bitvec_write_field(dest, &wp,0x0,1); // P0 = off |
Jacob Erlbeck | edfd7e3 | 2015-11-17 16:30:58 +0100 | [diff] [blame] | 492 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 493 | bitvec_write_field(dest, &wp,0x0,1); // USF_GRANULARITY |
| 494 | bitvec_write_field(dest, &wp,0x1,1); // switch TFI : on |
| 495 | bitvec_write_field(dest, &wp,tbf->tfi(),5);// TFI |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 496 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 497 | bitvec_write_field(dest, &wp,0x0,1); // |
| 498 | bitvec_write_field(dest, &wp,0x0,1); // TBF Starting Time = off |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 499 | if (alpha || gamma) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 500 | bitvec_write_field(dest, &wp,0x1,1); // Timeslot Allocation with Power Control |
| 501 | bitvec_write_field(dest, &wp,alpha,4); // ALPHA |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 502 | } else |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 503 | bitvec_write_field(dest, &wp,0x0,1); // Timeslot Allocation |
Jacob Erlbeck | edfd7e3 | 2015-11-17 16:30:58 +0100 | [diff] [blame] | 504 | |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 505 | for (ts = 0; ts < 8; ts++) { |
| 506 | if (tbf->pdch[ts]) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 507 | bitvec_write_field(dest, &wp,0x1,1); // USF_TN(i): on |
| 508 | bitvec_write_field(dest, &wp,tbf->m_usf[ts],3); // USF_TN(i) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 509 | if (alpha || gamma) |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 510 | bitvec_write_field(dest, &wp,gamma,5); // GAMMA power control parameter |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 511 | } else |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 512 | bitvec_write_field(dest, &wp,0x0,1); // USF_TN(i): off |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 513 | } |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 514 | // bitvec_write_field(dest, &wp,0x0,1); // Measurement Mapping struct not present |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
| 518 | /* generate downlink assignment */ |
Jacob Erlbeck | 166c9fc | 2016-01-07 16:04:29 +0100 | [diff] [blame] | 519 | void Encoding::write_packet_downlink_assignment(RlcMacDownlink_t * block, |
Jacob Erlbeck | 6b356a5 | 2016-01-29 16:39:21 +0100 | [diff] [blame] | 520 | bool old_tfi_is_valid, uint8_t old_tfi, uint8_t old_downlink, |
Max | a4f570f | 2017-12-15 11:21:57 +0100 | [diff] [blame] | 521 | struct gprs_rlcmac_dl_tbf *tbf, uint8_t poll, uint8_t rrbp, |
Jacob Erlbeck | 6b356a5 | 2016-01-29 16:39:21 +0100 | [diff] [blame] | 522 | uint8_t alpha, uint8_t gamma, int8_t ta_idx, |
Jacob Erlbeck | f2694b7 | 2016-01-26 21:46:26 +0100 | [diff] [blame] | 523 | uint8_t ta_ts, bool use_egprs) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 524 | { |
| 525 | // Packet downlink assignment TS 44.060 11.2.7 |
| 526 | |
Jacob Erlbeck | 166c9fc | 2016-01-07 16:04:29 +0100 | [diff] [blame] | 527 | PDA_AdditionsR99_t *pda_r99; |
| 528 | |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 529 | uint8_t tn; |
| 530 | |
| 531 | block->PAYLOAD_TYPE = 0x1; // RLC/MAC control block that does not include the optional octets of the RLC/MAC control header |
Jacob Erlbeck | f2694b7 | 2016-01-26 21:46:26 +0100 | [diff] [blame] | 532 | block->RRBP = rrbp; // 0: N+13 |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 533 | block->SP = poll; // RRBP field is valid |
| 534 | block->USF = 0x0; // Uplink state flag |
| 535 | |
| 536 | block->u.Packet_Downlink_Assignment.MESSAGE_TYPE = 0x2; // Packet Downlink Assignment |
| 537 | block->u.Packet_Downlink_Assignment.PAGE_MODE = 0x0; // Normal Paging |
| 538 | |
| 539 | block->u.Packet_Downlink_Assignment.Exist_PERSISTENCE_LEVEL = 0x0; // PERSISTENCE_LEVEL: off |
| 540 | |
Jacob Erlbeck | 6b356a5 | 2016-01-29 16:39:21 +0100 | [diff] [blame] | 541 | if (old_tfi_is_valid) { |
| 542 | block->u.Packet_Downlink_Assignment.ID.UnionType = 0x0; // TFI = on |
| 543 | block->u.Packet_Downlink_Assignment.ID.u.Global_TFI.UnionType = old_downlink; // 0=UPLINK TFI, 1=DL TFI |
| 544 | block->u.Packet_Downlink_Assignment.ID.u.Global_TFI.u.UPLINK_TFI = old_tfi; // TFI |
| 545 | } else { |
| 546 | block->u.Packet_Downlink_Assignment.ID.UnionType = 0x1; // TLLI |
| 547 | block->u.Packet_Downlink_Assignment.ID.u.TLLI = tbf->tlli(); |
| 548 | } |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 549 | |
| 550 | block->u.Packet_Downlink_Assignment.MAC_MODE = 0x0; // Dynamic Allocation |
| 551 | block->u.Packet_Downlink_Assignment.RLC_MODE = 0x0; // RLC acknowledged mode |
Daniel Willmann | 73191a4 | 2014-05-30 17:58:00 +0200 | [diff] [blame] | 552 | 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 Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 553 | block->u.Packet_Downlink_Assignment.TIMESLOT_ALLOCATION = 0; // timeslot(s) |
| 554 | for (tn = 0; tn < 8; tn++) { |
| 555 | if (tbf->pdch[tn]) |
| 556 | block->u.Packet_Downlink_Assignment.TIMESLOT_ALLOCATION |= 0x80 >> tn; // timeslot(s) |
| 557 | } |
| 558 | |
| 559 | block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_TIMING_ADVANCE_VALUE = 0x1; // TIMING_ADVANCE_VALUE = on |
Jacob Erlbeck | 9200ce6 | 2015-05-22 17:48:04 +0200 | [diff] [blame] | 560 | block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_VALUE = tbf->ta(); // TIMING_ADVANCE_VALUE |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 561 | if (ta_idx < 0) { |
| 562 | block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_IndexAndtimeSlot = 0x0; // TIMING_ADVANCE_INDEX = off |
| 563 | } else { |
| 564 | block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.Exist_IndexAndtimeSlot = 0x1; // TIMING_ADVANCE_INDEX = on |
| 565 | block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_INDEX = ta_idx; // TIMING_ADVANCE_INDEX |
| 566 | block->u.Packet_Downlink_Assignment.Packet_Timing_Advance.TIMING_ADVANCE_TIMESLOT_NUMBER = ta_ts; // TIMING_ADVANCE_TS |
| 567 | } |
| 568 | |
| 569 | block->u.Packet_Downlink_Assignment.Exist_P0_and_BTS_PWR_CTRL_MODE = 0x0; // POWER CONTROL = off |
| 570 | |
| 571 | block->u.Packet_Downlink_Assignment.Exist_Frequency_Parameters = 0x1; // Frequency Parameters = on |
Holger Hans Peter Freyther | 8f399de | 2013-12-25 20:22:35 +0100 | [diff] [blame] | 572 | block->u.Packet_Downlink_Assignment.Frequency_Parameters.TSC = tbf->tsc(); // Training Sequence Code (TSC) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 573 | block->u.Packet_Downlink_Assignment.Frequency_Parameters.UnionType = 0x0; // ARFCN = on |
Holger Hans Peter Freyther | fc498c9 | 2013-10-27 13:38:54 +0100 | [diff] [blame] | 574 | block->u.Packet_Downlink_Assignment.Frequency_Parameters.u.ARFCN = tbf->trx->arfcn; // ARFCN |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 575 | |
| 576 | block->u.Packet_Downlink_Assignment.Exist_DOWNLINK_TFI_ASSIGNMENT = 0x1; // DOWNLINK TFI ASSIGNMENT = on |
Holger Hans Peter Freyther | 34f6e5e | 2013-10-27 20:31:47 +0100 | [diff] [blame] | 577 | block->u.Packet_Downlink_Assignment.DOWNLINK_TFI_ASSIGNMENT = tbf->tfi(); // TFI |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 578 | |
| 579 | block->u.Packet_Downlink_Assignment.Exist_Power_Control_Parameters = 0x1; // Power Control Parameters = on |
| 580 | block->u.Packet_Downlink_Assignment.Power_Control_Parameters.ALPHA = alpha; // ALPHA |
| 581 | |
| 582 | for (tn = 0; tn < 8; tn++) |
| 583 | { |
| 584 | if (tbf->pdch[tn]) |
| 585 | { |
| 586 | block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[tn].Exist = 0x1; // Slot[i] = on |
| 587 | block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[tn].GAMMA_TN = gamma; // GAMMA_TN |
| 588 | } |
| 589 | else |
| 590 | { |
| 591 | block->u.Packet_Downlink_Assignment.Power_Control_Parameters.Slot[tn].Exist = 0x0; // Slot[i] = off |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | block->u.Packet_Downlink_Assignment.Exist_TBF_Starting_Time = 0x0; // TBF Starting TIME = off |
| 596 | block->u.Packet_Downlink_Assignment.Exist_Measurement_Mapping = 0x0; // Measurement_Mapping = off |
Jacob Erlbeck | 166c9fc | 2016-01-07 16:04:29 +0100 | [diff] [blame] | 597 | if (!use_egprs) { |
| 598 | block->u.Packet_Downlink_Assignment.Exist_AdditionsR99 = 0x0; // AdditionsR99 = off |
| 599 | return; |
| 600 | } |
Jacob Erlbeck | 36df774 | 2016-01-19 15:53:30 +0100 | [diff] [blame] | 601 | |
Jacob Erlbeck | 166c9fc | 2016-01-07 16:04:29 +0100 | [diff] [blame] | 602 | block->u.Packet_Downlink_Assignment.Exist_AdditionsR99 = 0x1; // AdditionsR99 = on |
| 603 | pda_r99 = &block->u.Packet_Downlink_Assignment.AdditionsR99; |
| 604 | pda_r99->Exist_EGPRS_Params = 1; |
Max | a4f570f | 2017-12-15 11:21:57 +0100 | [diff] [blame] | 605 | pda_r99->EGPRS_WindowSize = enc_ws(tbf->window_size()); /* see TS 44.060, table 12.5.2.1 */ |
Jacob Erlbeck | 166c9fc | 2016-01-07 16:04:29 +0100 | [diff] [blame] | 606 | pda_r99->LINK_QUALITY_MEASUREMENT_MODE = 0x0; /* no meas, see TS 44.060, table 11.2.7.2 */ |
| 607 | pda_r99->Exist_BEP_PERIOD2 = 0; /* No extra EGPRS BEP PERIOD */ |
| 608 | pda_r99->Exist_Packet_Extended_Timing_Advance = 0; |
| 609 | pda_r99->Exist_COMPACT_ReducedMA = 0; |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* generate paging request */ |
Holger Hans Peter Freyther | 63f29d6 | 2013-10-19 19:04:03 +0200 | [diff] [blame] | 613 | int Encoding::write_paging_request(bitvec * dest, uint8_t *ptmsi, uint16_t ptmsi_len) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 614 | { |
| 615 | unsigned wp = 0; |
| 616 | int plen; |
| 617 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 618 | bitvec_write_field(dest, &wp,0x0,4); // Skip Indicator |
| 619 | bitvec_write_field(dest, &wp,0x6,4); // Protocol Discriminator |
| 620 | bitvec_write_field(dest, &wp,0x21,8); // Paging Request Message Type |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 621 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 622 | bitvec_write_field(dest, &wp,0x0,4); // Page Mode |
| 623 | bitvec_write_field(dest, &wp,0x0,4); // Channel Needed |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 624 | |
| 625 | // Mobile Identity |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 626 | bitvec_write_field(dest, &wp,ptmsi_len+1,8); // Mobile Identity length |
| 627 | bitvec_write_field(dest, &wp,0xf,4); // unused |
| 628 | bitvec_write_field(dest, &wp,0x4,4); // PTMSI type |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 629 | for (int i = 0; i < ptmsi_len; i++) |
| 630 | { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 631 | bitvec_write_field(dest, &wp,ptmsi[i],8); // PTMSI |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 632 | } |
Max | 0a8fae8 | 2017-03-08 18:53:30 +0100 | [diff] [blame] | 633 | |
| 634 | if ((wp % 8)) |
| 635 | log_alert_exit("Length of PAG.REQ without rest octets is not " |
| 636 | "multiple of 8 bits, PLEASE FIX!\n"); |
| 637 | |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 638 | plen = wp / 8; |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 639 | bitvec_write_field(dest, &wp,0x0,1); // "L" NLN(PCH) = off |
| 640 | bitvec_write_field(dest, &wp,0x0,1); // "L" Priority1 = off |
| 641 | bitvec_write_field(dest, &wp,0x1,1); // "L" Priority2 = off |
| 642 | bitvec_write_field(dest, &wp,0x0,1); // "L" Group Call information = off |
| 643 | bitvec_write_field(dest, &wp,0x0,1); // "H" Packet Page Indication 1 = packet paging procedure |
| 644 | bitvec_write_field(dest, &wp,0x1,1); // "H" Packet Page Indication 2 = packet paging procedure |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 645 | |
| 646 | return plen; |
| 647 | } |
| 648 | |
Daniel Willmann | 5241c1a | 2013-12-11 14:35:29 +0100 | [diff] [blame] | 649 | /** |
| 650 | * The index of the array show_rbb is the bit position inside the rbb |
| 651 | * (show_rbb[63] relates to BSN ssn-1) |
| 652 | */ |
| 653 | void Encoding::encode_rbb(const char *show_rbb, uint8_t *rbb) |
| 654 | { |
| 655 | uint8_t rbb_byte = 0; |
| 656 | |
| 657 | // RECEIVE_BLOCK_BITMAP |
| 658 | for (int i = 0; i < 64; i++) { |
| 659 | /* Set bit at the appropriate position (see 3GPP TS 04.60 9.1.8.1) */ |
| 660 | if (show_rbb[i] == 'R') |
| 661 | rbb_byte |= 1<< (7-(i%8)); |
| 662 | |
| 663 | if((i%8) == 7) { |
| 664 | rbb[i/8] = rbb_byte; |
| 665 | rbb_byte = 0; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 670 | static void write_packet_ack_nack_desc_gprs( |
| 671 | struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp, |
| 672 | gprs_rlc_ul_window *window, bool is_final) |
| 673 | { |
| 674 | char rbb[65]; |
| 675 | |
| 676 | window->update_rbb(rbb); |
| 677 | |
| 678 | rbb[64] = 0; |
| 679 | LOGP(DRLCMACUL, LOGL_DEBUG, "- V(N): \"%s\" R=Received " |
| 680 | "I=Invalid\n", rbb); |
| 681 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 682 | bitvec_write_field(dest, &wp, is_final, 1); // FINAL_ACK_INDICATION |
| 683 | bitvec_write_field(dest, &wp, window->ssn(), 7); // STARTING_SEQUENCE_NUMBER |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 684 | |
| 685 | for (int i = 0; i < 64; i++) { |
| 686 | /* Set bit at the appropriate position (see 3GPP TS 04.60 9.1.8.1) */ |
| 687 | bool is_ack = (rbb[i] == 'R'); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 688 | bitvec_write_field(dest, &wp, is_ack, 1); |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | static void write_packet_uplink_ack_gprs( |
| 693 | struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp, |
| 694 | struct gprs_rlcmac_ul_tbf *tbf, bool is_final) |
| 695 | { |
| 696 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 697 | bitvec_write_field(dest, &wp, tbf->current_cs().to_num() - 1, 2); // CHANNEL_CODING_COMMAND |
Max | ea98b7d | 2018-01-04 15:13:27 +0100 | [diff] [blame] | 698 | write_packet_ack_nack_desc_gprs(bts, dest, wp, tbf->window(), is_final); |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 699 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 700 | bitvec_write_field(dest, &wp, 1, 1); // 1: have CONTENTION_RESOLUTION_TLLI |
| 701 | bitvec_write_field(dest, &wp, tbf->tlli(), 32); // CONTENTION_RESOLUTION_TLLI |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 702 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 703 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Packet Timing Advance |
| 704 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Power Control Parameters |
| 705 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Extension Bits |
| 706 | bitvec_write_field(dest, &wp, 0, 1); // fixed 0 |
| 707 | bitvec_write_field(dest, &wp, 1, 1); // 1: have Additions R99 |
| 708 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Packet Extended Timing Advance |
| 709 | bitvec_write_field(dest, &wp, 1, 1); // TBF_EST (enabled) |
| 710 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have REL 5 |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | static void write_packet_ack_nack_desc_egprs( |
| 714 | struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp, |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 715 | gprs_rlc_ul_window *window, bool is_final, unsigned& rest_bits) |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 716 | { |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 717 | unsigned int urbb_len = 0; |
| 718 | uint8_t crbb_len = 0; |
| 719 | uint8_t len; |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 720 | bool bow = true; |
| 721 | bool eow = true; |
| 722 | int ssn = window->mod_sns(window->v_q() + 1); |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 723 | unsigned int num_blocks = window->mod_sns(window->v_r() - window->v_q()); |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 724 | int esn_crbb = window->mod_sns(ssn - 1); |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 725 | static uint8_t rbb[RLC_EGPRS_MAX_WS] = {'\0'}; |
| 726 | uint8_t iter = 0; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 727 | int is_compressed = 0; |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 728 | bool try_compression = false; |
| 729 | uint8_t ucmp_bmplen; |
| 730 | uint8_t crbb_bitmap[23] = {'\0'}; |
| 731 | bitvec ucmp_vec; |
| 732 | bitvec crbb_vec; |
| 733 | uint8_t uclen_crbb = 0; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 734 | bool len_coded = true; |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 735 | uint8_t crbb_start_clr_code; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 736 | uint8_t i; |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 737 | #if 0 |
| 738 | /* static size of 16 bits*/ |
| 739 | ..0. .... = ACKNACK: (Union) |
| 740 | Desc |
| 741 | |
| 742 | ...0 .... = FINAL_ACK_INDICATION: False |
| 743 | |
| 744 | .... 1... = BEGINNING_OF_WINDOW: 1 |
| 745 | |
| 746 | .... .1.. = END_OF_WINDOW: 1 |
| 747 | |
| 748 | .... ..10 0101 0001 1... .... = STARTING_SEQUENCE_NUMBER: 1187 |
| 749 | |
| 750 | .0.. .... = CRBB Exist: 0 |
| 751 | #endif |
| 752 | rest_bits -= 16; |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 753 | |
| 754 | if (num_blocks > 0) |
| 755 | /* V(Q) is NACK and omitted -> SSN = V(Q) + 1 */ |
| 756 | num_blocks -= 1; |
| 757 | |
| 758 | if (num_blocks > window->ws()) |
| 759 | num_blocks = window->ws(); |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 760 | /* Try Compression as number of blocks does not fit */ |
| 761 | if (num_blocks > rest_bits) { |
| 762 | try_compression = true; |
| 763 | } |
| 764 | if (try_compression == true) { |
| 765 | ucmp_bmplen = window->update_egprs_rbb(rbb); |
| 766 | ucmp_vec.data = rbb; |
| 767 | ucmp_vec.cur_bit = ucmp_bmplen; |
| 768 | ucmp_vec.data_len = 127; |
| 769 | crbb_vec.data = crbb_bitmap; |
| 770 | crbb_vec.cur_bit = 0; |
| 771 | crbb_vec.data_len = 127; |
| 772 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 773 | "rest_bits=%d uncompressed len %d and uncompressed bitmap = %s\n", |
| 774 | rest_bits, ucmp_bmplen, |
| 775 | osmo_hexdump(ucmp_vec.data, (ucmp_bmplen+7)/8)); |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 776 | |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 777 | is_compressed = egprs_compress::compress_rbb(&ucmp_vec, /* Uncompressed bitmap*/ |
| 778 | &crbb_vec, /*Compressed bitmap vector */ |
| 779 | &uclen_crbb, |
| 780 | (rest_bits - 16));/* CRBBlength:7 colourcode:1 dissector length:8*/ |
| 781 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 782 | "the ucmp len=%d uclen_crbb=%d num_blocks=%d crbb length %d, " |
| 783 | "and the CRBB bitmap = %s\n", |
| 784 | ucmp_bmplen, uclen_crbb, num_blocks, crbb_vec.cur_bit, |
| 785 | osmo_hexdump(crbb_bitmap, (crbb_vec.cur_bit+7)/8)); |
| 786 | crbb_len = crbb_vec.cur_bit; |
| 787 | } |
| 788 | |
| 789 | if (is_compressed == 0) { |
| 790 | /* length field takes 8 bits*/ |
| 791 | if (num_blocks > rest_bits - 8) { |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 792 | eow = false; |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 793 | urbb_len = rest_bits; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 794 | len_coded = false; |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 795 | } else if (num_blocks == rest_bits) { |
| 796 | urbb_len = rest_bits; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 797 | len_coded = false; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 798 | } else |
| 799 | urbb_len = num_blocks; |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 800 | |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 801 | len = urbb_len + 15; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 802 | } else { |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 803 | if (num_blocks > uclen_crbb) { |
| 804 | eow = false; |
| 805 | urbb_len = num_blocks - uclen_crbb; |
| 806 | } |
| 807 | /* Union bit takes 1 bit */ |
| 808 | /* Other fields in descr of compresed bitmap takes 23 bits |
| 809 | * -8 = CRBB_STARTING_COLOR_CODE + CRBB_LENGTH */ |
| 810 | if (urbb_len > (rest_bits - crbb_len - 8)) { |
| 811 | eow = false; |
| 812 | len_coded = false; |
| 813 | urbb_len = rest_bits - crbb_len - 8; |
| 814 | /* -16 = ACKNACK Dissector length + CRBB_STARTING_COLOR_CODE + CRBB_LENGTH */ |
| 815 | } else if (urbb_len > (rest_bits - crbb_len - 16)) { |
| 816 | eow = false; |
| 817 | len_coded = false; |
| 818 | urbb_len = rest_bits - crbb_len - 16; |
| 819 | } |
| 820 | len = urbb_len + crbb_len + 23; |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 821 | } |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 822 | |
| 823 | /* EGPRS Ack/Nack Description IE */ |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 824 | if (len_coded == false) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 825 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have length |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 826 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 827 | bitvec_write_field(dest, &wp, 1, 1); // 1: have length |
| 828 | bitvec_write_field(dest, &wp, len, 8); // length |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 831 | bitvec_write_field(dest, &wp, is_final, 1); // FINAL_ACK_INDICATION |
| 832 | bitvec_write_field(dest, &wp, bow, 1); // BEGINNING_OF_WINDOW |
| 833 | bitvec_write_field(dest, &wp, eow, 1); // END_OF_WINDOW |
| 834 | bitvec_write_field(dest, &wp, ssn, 11); // STARTING_SEQUENCE_NUMBER |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 835 | if (is_compressed) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 836 | bitvec_write_field(dest, &wp, 1, 1); // CRBB_Exist |
| 837 | bitvec_write_field(dest, &wp, crbb_len, 7); // CRBB_LENGTH |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 838 | crbb_start_clr_code = (0x80 & ucmp_vec.data[0])>>7; |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 839 | bitvec_write_field(dest, &wp, crbb_start_clr_code, 1); // CRBB_clr_code |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 840 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 841 | "EGPRS CRBB, crbb_len = %d, crbb_start_clr_code = %d\n", |
| 842 | crbb_len, crbb_start_clr_code); |
| 843 | while (crbb_len != 0) { |
| 844 | if (crbb_len > 8) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 845 | bitvec_write_field(dest, &wp, crbb_bitmap[iter], 8); |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 846 | crbb_len = crbb_len - 8; |
| 847 | iter++; |
| 848 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 849 | bitvec_write_field(dest, &wp, crbb_bitmap[iter], crbb_len); |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 850 | crbb_len = 0; |
| 851 | } |
| 852 | } |
| 853 | esn_crbb = window->mod_sns(esn_crbb + uclen_crbb); |
| 854 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 855 | bitvec_write_field(dest, &wp, 0, 1); // CRBB_Exist |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 856 | } |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 857 | LOGP(DRLCMACUL, LOGL_DEBUG, |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 858 | "EGPRS URBB, urbb len = %d, SSN = %d, ESN_CRBB = %d, " |
| 859 | "len present = %s,desc len = %d, " |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 860 | "SNS = %d, WS = %d, V(Q) = %d, V(R) = %d%s%s\n", |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 861 | urbb_len, ssn, esn_crbb, len_coded ? "yes" : "No" , len, |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 862 | window->sns(), window->ws(), window->v_q(), window->v_r(), |
| 863 | bow ? ", BOW" : "", eow ? ", EOW" : ""); |
Pravin Kumarvel | 06bdb35 | 2016-10-24 12:21:21 +0530 | [diff] [blame] | 864 | |
| 865 | for (i = urbb_len; i > 0; i--) { |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 866 | /* Set bit at the appropriate position (see 3GPP TS 04.60 12.3.1) */ |
| 867 | bool is_ack = window->m_v_n.is_received(esn_crbb + i); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 868 | bitvec_write_field(dest, &wp, is_ack, 1); |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
| 872 | static void write_packet_uplink_ack_egprs( |
| 873 | struct gprs_rlcmac_bts *bts, bitvec * dest, unsigned& wp, |
| 874 | struct gprs_rlcmac_ul_tbf *tbf, bool is_final) |
| 875 | { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 876 | bitvec_write_field(dest, &wp, 0, 2); // fixed 00 |
Aravind Sirsikar | 99ab0a8 | 2016-06-15 17:46:41 +0530 | [diff] [blame] | 877 | /* CHANNEL_CODING_COMMAND */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 878 | bitvec_write_field(dest, &wp, |
Aravind Sirsikar | 99ab0a8 | 2016-06-15 17:46:41 +0530 | [diff] [blame] | 879 | tbf->current_cs().to_num() - 1, 4); |
Aravind Sirsikar | 505a86d | 2016-07-26 18:26:21 +0530 | [diff] [blame] | 880 | /* 0: no RESEGMENT, 1: Segmentation*/ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 881 | bitvec_write_field(dest, &wp, 1, 1); |
| 882 | bitvec_write_field(dest, &wp, 1, 1); // PRE_EMPTIVE_TRANSMISSION, TODO: This resembles GPRS, change it? |
| 883 | bitvec_write_field(dest, &wp, 0, 1); // 0: no PRR_RETRANSMISSION_REQUEST, TODO: clarify |
| 884 | bitvec_write_field(dest, &wp, 0, 1); // 0: no ARAC_RETRANSMISSION_REQUEST, TODO: clarify |
| 885 | bitvec_write_field(dest, &wp, 1, 1); // 1: have CONTENTION_RESOLUTION_TLLI |
| 886 | bitvec_write_field(dest, &wp, tbf->tlli(), 32); // CONTENTION_RESOLUTION_TLLI |
| 887 | bitvec_write_field(dest, &wp, 1, 1); // TBF_EST (enabled) |
| 888 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Packet Timing Advance |
| 889 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Packet Extended Timing Advance |
| 890 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Power Control Parameters |
| 891 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have Extension Bits |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 892 | |
sivasankari | 8adfcd0 | 2017-01-16 15:41:21 +0530 | [diff] [blame] | 893 | /* -2 for last bit 0 mandatory and REL5 not supported */ |
| 894 | unsigned bits_ack_nack = dest->data_len * 8 - wp - 2; |
Max | ea98b7d | 2018-01-04 15:13:27 +0100 | [diff] [blame] | 895 | write_packet_ack_nack_desc_egprs(bts, dest, wp, tbf->window(), is_final, bits_ack_nack); |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 896 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 897 | bitvec_write_field(dest, &wp, 0, 1); // fixed 0 |
| 898 | bitvec_write_field(dest, &wp, 0, 1); // 0: don't have REL 5 |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 899 | }; |
| 900 | |
| 901 | void Encoding::write_packet_uplink_ack( |
| 902 | struct gprs_rlcmac_bts *bts, bitvec * dest, |
Jacob Erlbeck | f2694b7 | 2016-01-26 21:46:26 +0100 | [diff] [blame] | 903 | struct gprs_rlcmac_ul_tbf *tbf, bool is_final, |
| 904 | uint8_t rrbp) |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 905 | { |
| 906 | unsigned wp = 0; |
| 907 | |
| 908 | LOGP(DRLCMACUL, LOGL_DEBUG, "Encoding Ack/Nack for %s " |
| 909 | "(final=%d)\n", tbf_name(tbf), is_final); |
| 910 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 911 | bitvec_write_field(dest, &wp, 0x1, 2); // Payload Type |
| 912 | bitvec_write_field(dest, &wp, rrbp, 2); // Uplink block with TDMA framenumber |
| 913 | bitvec_write_field(dest, &wp, is_final, 1); // Suppl/Polling Bit |
| 914 | bitvec_write_field(dest, &wp, 0x0, 3); // Uplink state flag |
| 915 | bitvec_write_field(dest, &wp, 0x9, 6); // MESSAGE TYPE Uplink Ack/Nack |
| 916 | bitvec_write_field(dest, &wp, 0x0, 2); // Page Mode |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 917 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 918 | bitvec_write_field(dest, &wp, 0x0, 2); // fixed 00 |
| 919 | bitvec_write_field(dest, &wp, tbf->tfi(), 5); // Uplink TFI |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 920 | |
| 921 | if (tbf->is_egprs_enabled()) { |
| 922 | /* PU_AckNack_EGPRS = on */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 923 | bitvec_write_field(dest, &wp, 1, 1); // 1: EGPRS |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 924 | write_packet_uplink_ack_egprs(bts, dest, wp, tbf, is_final); |
| 925 | } else { |
| 926 | /* PU_AckNack_GPRS = on */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 927 | bitvec_write_field(dest, &wp, 0, 1); // 0: GPRS |
Jacob Erlbeck | 37005a1 | 2015-12-22 14:58:34 +0100 | [diff] [blame] | 928 | write_packet_uplink_ack_gprs(bts, dest, wp, tbf, is_final); |
| 929 | } |
| 930 | |
| 931 | LOGP(DRLCMACUL, LOGL_DEBUG, |
| 932 | "Uplink Ack/Nack bit count %d, max %d, message = %s\n", |
| 933 | wp, dest->data_len * 8, |
| 934 | osmo_hexdump(dest->data, dest->data_len)); |
| 935 | } |
| 936 | |
Holger Hans Peter Freyther | 63f29d6 | 2013-10-19 19:04:03 +0200 | [diff] [blame] | 937 | unsigned Encoding::write_packet_paging_request(bitvec * dest) |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 938 | { |
| 939 | unsigned wp = 0; |
| 940 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 941 | bitvec_write_field(dest, &wp,0x1,2); // Payload Type |
| 942 | bitvec_write_field(dest, &wp,0x0,3); // No polling |
| 943 | bitvec_write_field(dest, &wp,0x0,3); // Uplink state flag |
| 944 | bitvec_write_field(dest, &wp,0x22,6); // MESSAGE TYPE |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 945 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 946 | bitvec_write_field(dest, &wp,0x0,2); // Page Mode |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 947 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 948 | bitvec_write_field(dest, &wp,0x0,1); // No PERSISTENCE_LEVEL |
| 949 | bitvec_write_field(dest, &wp,0x0,1); // No NLN |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 950 | |
| 951 | return wp; |
| 952 | } |
| 953 | |
Holger Hans Peter Freyther | 63f29d6 | 2013-10-19 19:04:03 +0200 | [diff] [blame] | 954 | unsigned Encoding::write_repeated_page_info(bitvec * dest, unsigned& wp, uint8_t len, |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 955 | uint8_t *identity, uint8_t chan_needed) |
| 956 | { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 957 | bitvec_write_field(dest, &wp,0x1,1); // Repeated Page info exists |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 958 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 959 | bitvec_write_field(dest, &wp,0x1,1); // RR connection paging |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 960 | |
| 961 | if ((identity[0] & 0x07) == 4) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 962 | bitvec_write_field(dest, &wp,0x0,1); // TMSI |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 963 | identity++; |
| 964 | len--; |
| 965 | } else { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 966 | bitvec_write_field(dest, &wp,0x0,1); // MI |
| 967 | bitvec_write_field(dest, &wp,len,4); // MI len |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 968 | } |
| 969 | while (len) { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 970 | bitvec_write_field(dest, &wp,*identity++,8); // MI data |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 971 | len--; |
| 972 | } |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 973 | bitvec_write_field(dest, &wp,chan_needed,2); // CHANNEL_NEEDED |
| 974 | bitvec_write_field(dest, &wp,0x0,1); // No eMLPP_PRIORITY |
Holger Hans Peter Freyther | 6ca0a90 | 2013-10-19 18:50:22 +0200 | [diff] [blame] | 975 | |
| 976 | return wp; |
| 977 | } |
| 978 | |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 979 | int Encoding::rlc_write_dl_data_header(const struct gprs_rlc_data_info *rlc, |
| 980 | uint8_t *data) |
| 981 | { |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 982 | struct gprs_rlc_dl_header_egprs_1 *egprs1; |
| 983 | struct gprs_rlc_dl_header_egprs_2 *egprs2; |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 984 | struct gprs_rlc_dl_header_egprs_3 *egprs3; |
| 985 | struct rlc_dl_header *gprs; |
| 986 | unsigned int e_fbi_header; |
| 987 | GprsCodingScheme cs = rlc->cs; |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 988 | unsigned int offs; |
| 989 | unsigned int bsn_delta; |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 990 | |
| 991 | switch(cs.headerTypeData()) { |
| 992 | case GprsCodingScheme::HEADER_GPRS_DATA: |
| 993 | gprs = static_cast<struct rlc_dl_header *> |
| 994 | ((void *)data); |
| 995 | |
| 996 | gprs->usf = rlc->usf; |
| 997 | gprs->s_p = rlc->es_p != 0 ? 1 : 0; |
| 998 | gprs->rrbp = rlc->rrbp; |
| 999 | gprs->pt = 0; |
| 1000 | gprs->tfi = rlc->tfi; |
| 1001 | gprs->pr = rlc->pr; |
| 1002 | |
| 1003 | gprs->fbi = rlc->block_info[0].cv == 0; |
| 1004 | gprs->e = rlc->block_info[0].e; |
| 1005 | gprs->bsn = rlc->block_info[0].bsn; |
| 1006 | break; |
| 1007 | |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1008 | case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_1: |
| 1009 | egprs1 = static_cast<struct gprs_rlc_dl_header_egprs_1 *> |
| 1010 | ((void *)data); |
| 1011 | |
| 1012 | egprs1->usf = rlc->usf; |
| 1013 | egprs1->es_p = rlc->es_p; |
| 1014 | egprs1->rrbp = rlc->rrbp; |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1015 | egprs1->tfi_hi = rlc->tfi >> 0; /* 1 bit LSB */ |
| 1016 | egprs1->tfi_lo = rlc->tfi >> 1; /* 4 bits */ |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1017 | egprs1->pr = rlc->pr; |
| 1018 | egprs1->cps = rlc->cps; |
| 1019 | |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1020 | egprs1->bsn1_hi = rlc->block_info[0].bsn >> 0; /* 2 bits LSB */ |
| 1021 | egprs1->bsn1_mid = rlc->block_info[0].bsn >> 2; /* 8 bits */ |
| 1022 | egprs1->bsn1_lo = rlc->block_info[0].bsn >> 10; /* 1 bit */ |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1023 | |
| 1024 | bsn_delta = (rlc->block_info[1].bsn - rlc->block_info[0].bsn) & |
| 1025 | (RLC_EGPRS_SNS - 1); |
| 1026 | |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1027 | egprs1->bsn2_hi = bsn_delta >> 0; /* 7 bits LSB */ |
| 1028 | egprs1->bsn2_lo = bsn_delta >> 7; /* 3 bits */ |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1029 | |
| 1030 | /* first FBI/E header */ |
| 1031 | e_fbi_header = rlc->block_info[0].e ? 0x01 : 0; |
| 1032 | e_fbi_header |= rlc->block_info[0].cv == 0 ? 0x02 : 0; /* FBI */ |
Jacob Erlbeck | 0f5c695 | 2016-02-03 18:35:27 +0100 | [diff] [blame] | 1033 | offs = rlc->data_offs_bits[0] / 8; |
| 1034 | OSMO_ASSERT(rlc->data_offs_bits[0] % 8 == 2); |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1035 | e_fbi_header <<= 0; |
Jacob Erlbeck | 0f5c695 | 2016-02-03 18:35:27 +0100 | [diff] [blame] | 1036 | data[offs] = (data[offs] & 0b11111100) | e_fbi_header; |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1037 | |
| 1038 | /* second FBI/E header */ |
| 1039 | e_fbi_header = rlc->block_info[1].e ? 0x01 : 0; |
| 1040 | e_fbi_header |= rlc->block_info[1].cv == 0 ? 0x02 : 0; /* FBI */ |
| 1041 | offs = rlc->data_offs_bits[1] / 8; |
| 1042 | OSMO_ASSERT(rlc->data_offs_bits[1] % 8 == 4); |
| 1043 | e_fbi_header <<= 2; |
| 1044 | data[offs] = (data[offs] & 0b11110011) | e_fbi_header; |
| 1045 | break; |
| 1046 | |
| 1047 | case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_2: |
| 1048 | egprs2 = static_cast<struct gprs_rlc_dl_header_egprs_2 *> |
| 1049 | ((void *)data); |
| 1050 | |
| 1051 | egprs2->usf = rlc->usf; |
| 1052 | egprs2->es_p = rlc->es_p; |
| 1053 | egprs2->rrbp = rlc->rrbp; |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1054 | egprs2->tfi_hi = rlc->tfi >> 0; /* 1 bit LSB */ |
| 1055 | egprs2->tfi_lo = rlc->tfi >> 1; /* 4 bits */ |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1056 | egprs2->pr = rlc->pr; |
| 1057 | egprs2->cps = rlc->cps; |
| 1058 | |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1059 | egprs2->bsn1_hi = rlc->block_info[0].bsn >> 0; /* 2 bits LSB */ |
| 1060 | egprs2->bsn1_mid = rlc->block_info[0].bsn >> 2; /* 8 bits */ |
| 1061 | egprs2->bsn1_lo = rlc->block_info[0].bsn >> 10; /* 1 bit */ |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1062 | |
| 1063 | e_fbi_header = rlc->block_info[0].e ? 0x01 : 0; |
| 1064 | e_fbi_header |= rlc->block_info[0].cv == 0 ? 0x02 : 0; /* FBI */ |
Jacob Erlbeck | 0f5c695 | 2016-02-03 18:35:27 +0100 | [diff] [blame] | 1065 | offs = rlc->data_offs_bits[0] / 8; |
| 1066 | OSMO_ASSERT(rlc->data_offs_bits[0] % 8 == 6); |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1067 | e_fbi_header <<= 4; |
Jacob Erlbeck | 0f5c695 | 2016-02-03 18:35:27 +0100 | [diff] [blame] | 1068 | data[offs] = (data[offs] & 0b11001111) | e_fbi_header; |
Jacob Erlbeck | 9e85939 | 2016-02-02 11:48:37 +0100 | [diff] [blame] | 1069 | break; |
| 1070 | |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 1071 | case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_3: |
| 1072 | egprs3 = static_cast<struct gprs_rlc_dl_header_egprs_3 *> |
| 1073 | ((void *)data); |
| 1074 | |
| 1075 | egprs3->usf = rlc->usf; |
| 1076 | egprs3->es_p = rlc->es_p; |
| 1077 | egprs3->rrbp = rlc->rrbp; |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1078 | egprs3->tfi_hi = rlc->tfi >> 0; /* 1 bit LSB */ |
| 1079 | egprs3->tfi_lo = rlc->tfi >> 1; /* 4 bits */ |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 1080 | egprs3->pr = rlc->pr; |
| 1081 | egprs3->cps = rlc->cps; |
| 1082 | |
Tom Tsou | df69809 | 2016-07-11 17:05:19 -0700 | [diff] [blame] | 1083 | egprs3->bsn1_hi = rlc->block_info[0].bsn >> 0; /* 2 bits LSB */ |
| 1084 | egprs3->bsn1_mid = rlc->block_info[0].bsn >> 2; /* 8 bits */ |
| 1085 | egprs3->bsn1_lo = rlc->block_info[0].bsn >> 10; /* 1 bit */ |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 1086 | |
| 1087 | egprs3->spb = rlc->block_info[0].spb; |
| 1088 | |
| 1089 | e_fbi_header = rlc->block_info[0].e ? 0x01 : 0; |
| 1090 | e_fbi_header |= rlc->block_info[0].cv == 0 ? 0x02 : 0; /* FBI */ |
Jacob Erlbeck | 0f5c695 | 2016-02-03 18:35:27 +0100 | [diff] [blame] | 1091 | offs = rlc->data_offs_bits[0] / 8; |
| 1092 | OSMO_ASSERT(rlc->data_offs_bits[0] % 8 == 1); |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 1093 | e_fbi_header <<= 7; |
Jacob Erlbeck | 0f5c695 | 2016-02-03 18:35:27 +0100 | [diff] [blame] | 1094 | data[offs-1] = (data[offs-1] & 0b01111111) | (e_fbi_header >> 0); |
| 1095 | data[offs] = (data[offs] & 0b11111110) | (e_fbi_header >> 8); |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 1096 | break; |
| 1097 | |
Jacob Erlbeck | f0e4039 | 2016-01-08 10:07:53 +0100 | [diff] [blame] | 1098 | default: |
| 1099 | LOGP(DRLCMACDL, LOGL_ERROR, |
| 1100 | "Encoding of uplink %s data blocks not yet supported.\n", |
| 1101 | cs.name()); |
| 1102 | return -ENOTSUP; |
| 1103 | }; |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * \brief Copy LSB bitstream RLC data block from byte aligned buffer. |
| 1110 | * |
| 1111 | * Note that the bitstream is encoded in LSB first order, so the two octets |
| 1112 | * 654321xx xxxxxx87 contain the octet 87654321 starting at bit position 3 |
| 1113 | * (LSB has bit position 1). This is a different order than the one used by |
| 1114 | * CSN.1. |
| 1115 | * |
| 1116 | * \param data_block_idx The block index, 0..1 for header type 1, 0 otherwise |
| 1117 | * \param src A pointer to the start of the RLC block (incl. the header) |
| 1118 | * \param buffer A data area of a least the size of the RLC block |
| 1119 | * \returns the number of bytes copied |
| 1120 | */ |
| 1121 | unsigned int Encoding::rlc_copy_from_aligned_buffer( |
| 1122 | const struct gprs_rlc_data_info *rlc, |
| 1123 | unsigned int data_block_idx, |
| 1124 | uint8_t *dst, const uint8_t *buffer) |
| 1125 | { |
| 1126 | unsigned int hdr_bytes; |
| 1127 | unsigned int extra_bits; |
| 1128 | unsigned int i; |
| 1129 | |
| 1130 | uint8_t c, last_c; |
| 1131 | const uint8_t *src; |
| 1132 | const struct gprs_rlc_data_block_info *rdbi; |
| 1133 | |
| 1134 | OSMO_ASSERT(data_block_idx < rlc->num_data_blocks); |
| 1135 | rdbi = &rlc->block_info[data_block_idx]; |
| 1136 | |
| 1137 | hdr_bytes = rlc->data_offs_bits[data_block_idx] / 8; |
| 1138 | extra_bits = (rlc->data_offs_bits[data_block_idx] % 8); |
| 1139 | |
| 1140 | if (extra_bits == 0) { |
| 1141 | /* It is aligned already */ |
| 1142 | memmove(dst + hdr_bytes, buffer, rdbi->data_len); |
| 1143 | return rdbi->data_len; |
| 1144 | } |
| 1145 | |
| 1146 | src = buffer; |
| 1147 | dst = dst + hdr_bytes; |
| 1148 | last_c = *dst << (8 - extra_bits); |
| 1149 | |
| 1150 | for (i = 0; i < rdbi->data_len; i++) { |
| 1151 | c = src[i]; |
| 1152 | *(dst++) = (last_c >> (8 - extra_bits)) | (c << extra_bits); |
| 1153 | last_c = c; |
| 1154 | } |
| 1155 | |
| 1156 | /* overwrite the lower extra_bits */ |
| 1157 | *dst = (*dst & (0xff << extra_bits)) | (last_c >> (8 - extra_bits)); |
| 1158 | |
| 1159 | return rdbi->data_len; |
| 1160 | } |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1161 | |
Alexander Couzens | cb846ec | 2016-05-30 19:10:48 +0200 | [diff] [blame] | 1162 | /*! |
| 1163 | * \brief (GPRS) put llc pdu into an rlc/mac block. fragment the llc pdu if needed |
| 1164 | * \param rdbi rlc/mac block info |
| 1165 | * \param llc llc pdu |
| 1166 | * \param offset given offset within the rlc/mac block |
| 1167 | * \param num_chunks count the chunks (llc pdu data) within rlc/mac |
| 1168 | * \param data_block buffer holds rlc/mac data |
| 1169 | * \param is_final if this is the last rlc/mac within a TBF |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1170 | * \param count_payload if not NULL save the written size of payload in bytes into it |
Alexander Couzens | cb846ec | 2016-05-30 19:10:48 +0200 | [diff] [blame] | 1171 | * \return the state of the rlc/mac like if there is more space for another chunk |
| 1172 | */ |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1173 | static Encoding::AppendResult rlc_data_to_dl_append_gprs( |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1174 | struct gprs_rlc_data_block_info *rdbi, |
| 1175 | gprs_llc *llc, int *offset, int *num_chunks, |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1176 | uint8_t *data_block, bool is_final, int *count_payload) |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1177 | { |
| 1178 | int chunk; |
| 1179 | int space; |
| 1180 | struct rlc_li_field *li; |
| 1181 | uint8_t *delimiter, *data, *e_pointer; |
| 1182 | |
| 1183 | data = data_block + *offset; |
| 1184 | delimiter = data_block + *num_chunks; |
| 1185 | e_pointer = (*num_chunks ? delimiter - 1 : NULL); |
| 1186 | |
| 1187 | chunk = llc->chunk_size(); |
| 1188 | space = rdbi->data_len - *offset; |
| 1189 | |
| 1190 | /* if chunk will exceed block limit */ |
| 1191 | if (chunk > space) { |
| 1192 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1193 | "larger than space (%d) left in block: copy " |
| 1194 | "only remaining space, and we are done\n", |
| 1195 | chunk, space); |
| 1196 | /* block is filled, so there is no extension */ |
| 1197 | if (e_pointer) |
| 1198 | *e_pointer |= 0x01; |
| 1199 | /* fill only space */ |
| 1200 | llc->consume(data, space); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1201 | if (count_payload) |
| 1202 | *count_payload = space; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1203 | /* return data block as message */ |
| 1204 | *offset = rdbi->data_len; |
| 1205 | (*num_chunks)++; |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1206 | return Encoding::AR_NEED_MORE_BLOCKS; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1207 | } |
| 1208 | /* if FINAL chunk would fit precisely in space left */ |
| 1209 | if (chunk == space && is_final) |
| 1210 | { |
| 1211 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1212 | "would exactly fit into space (%d): because " |
| 1213 | "this is a final block, we don't add length " |
| 1214 | "header, and we are done\n", chunk, space); |
| 1215 | /* block is filled, so there is no extension */ |
| 1216 | if (e_pointer) |
| 1217 | *e_pointer |= 0x01; |
| 1218 | /* fill space */ |
| 1219 | llc->consume(data, space); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1220 | if (count_payload) |
| 1221 | *count_payload = space; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1222 | *offset = rdbi->data_len; |
| 1223 | (*num_chunks)++; |
| 1224 | rdbi->cv = 0; |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1225 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1226 | } |
| 1227 | /* if chunk would fit exactly in space left */ |
| 1228 | if (chunk == space) { |
| 1229 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1230 | "would exactly fit into space (%d): add length " |
| 1231 | "header with LI=0, to make frame extend to " |
| 1232 | "next block, and we are done\n", chunk, space); |
| 1233 | /* make space for delimiter */ |
| 1234 | if (delimiter != data) |
| 1235 | memmove(delimiter + 1, delimiter, |
| 1236 | data - delimiter); |
| 1237 | data++; |
| 1238 | (*offset)++; |
| 1239 | space--; |
| 1240 | /* add LI with 0 length */ |
| 1241 | li = (struct rlc_li_field *)delimiter; |
| 1242 | li->e = 1; /* not more extension */ |
| 1243 | li->m = 0; /* shall be set to 0, in case of li = 0 */ |
| 1244 | li->li = 0; /* chunk fills the complete space */ |
| 1245 | rdbi->e = 0; /* 0: extensions present */ |
| 1246 | // no need to set e_pointer nor increase delimiter |
| 1247 | /* fill only space, which is 1 octet less than chunk */ |
| 1248 | llc->consume(data, space); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1249 | if (count_payload) |
| 1250 | *count_payload = space; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1251 | /* return data block as message */ |
| 1252 | *offset = rdbi->data_len; |
| 1253 | (*num_chunks)++; |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1254 | return Encoding::AR_NEED_MORE_BLOCKS; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less " |
| 1258 | "than remaining space (%d): add length header to " |
| 1259 | "to delimit LLC frame\n", chunk, space); |
| 1260 | /* the LLC frame chunk ends in this block */ |
| 1261 | /* make space for delimiter */ |
| 1262 | if (delimiter != data) |
| 1263 | memmove(delimiter + 1, delimiter, data - delimiter); |
| 1264 | data++; |
| 1265 | (*offset)++; |
| 1266 | space--; |
| 1267 | /* add LI to delimit frame */ |
| 1268 | li = (struct rlc_li_field *)delimiter; |
| 1269 | li->e = 0; /* Extension bit, maybe set later */ |
| 1270 | li->m = 0; /* will be set later, if there is more LLC data */ |
| 1271 | li->li = chunk; /* length of chunk */ |
| 1272 | rdbi->e = 0; /* 0: extensions present */ |
| 1273 | (*num_chunks)++; |
| 1274 | /* copy (rest of) LLC frame to space and reset later */ |
| 1275 | llc->consume(data, chunk); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1276 | if (count_payload) |
| 1277 | *count_payload = chunk; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1278 | data += chunk; |
| 1279 | space -= chunk; |
| 1280 | (*offset) += chunk; |
| 1281 | /* if we have more data and we have space left */ |
| 1282 | if (space > 0 && !is_final) { |
| 1283 | li->m = 1; /* we indicate more frames to follow */ |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1284 | return Encoding::AR_COMPLETED_SPACE_LEFT; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1285 | } |
| 1286 | /* if we don't have more LLC frames */ |
| 1287 | if (is_final) { |
| 1288 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we " |
| 1289 | "done.\n"); |
| 1290 | li->e = 1; /* we cannot extend */ |
| 1291 | rdbi->cv = 0; |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1292 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1293 | } |
| 1294 | /* we have no space left */ |
| 1295 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are " |
| 1296 | "done.\n"); |
| 1297 | li->e = 1; /* we cannot extend */ |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1298 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
| 1299 | } |
| 1300 | |
Alexander Couzens | cb846ec | 2016-05-30 19:10:48 +0200 | [diff] [blame] | 1301 | /*! |
| 1302 | * \brief (EGPRS) put llc pdu into an rlc/mac block. fragment the llc pdu if needed |
| 1303 | * \param rdbi rlc/mac block info |
| 1304 | * \param llc llc pdu |
| 1305 | * \param offset given offset within the rlc/mac block |
| 1306 | * \param num_chunks count the chunks (llc pdu data) within rlc/mac |
| 1307 | * \param data_block buffer holds rlc/mac data |
| 1308 | * \param is_final if this is the last rlc/mac within a TBF |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1309 | * \param count_payload if not NULL save the written size of payload in bytes into it |
Alexander Couzens | cb846ec | 2016-05-30 19:10:48 +0200 | [diff] [blame] | 1310 | * \return the state of the rlc/mac like if there is more space for another chunk |
| 1311 | */ |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1312 | static Encoding::AppendResult rlc_data_to_dl_append_egprs( |
| 1313 | struct gprs_rlc_data_block_info *rdbi, |
| 1314 | gprs_llc *llc, int *offset, int *num_chunks, |
| 1315 | uint8_t *data_block, |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1316 | bool is_final, int *count_payload) |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1317 | { |
| 1318 | int chunk; |
| 1319 | int space; |
| 1320 | struct rlc_li_field_egprs *li; |
| 1321 | struct rlc_li_field_egprs *prev_li; |
| 1322 | uint8_t *delimiter, *data; |
| 1323 | |
| 1324 | data = data_block + *offset; |
| 1325 | delimiter = data_block + *num_chunks; |
| 1326 | prev_li = (struct rlc_li_field_egprs *) |
| 1327 | (*num_chunks ? delimiter - 1 : NULL); |
| 1328 | |
| 1329 | chunk = llc->chunk_size(); |
| 1330 | space = rdbi->data_len - *offset; |
| 1331 | |
| 1332 | /* if chunk will exceed block limit */ |
| 1333 | if (chunk > space) { |
| 1334 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1335 | "larger than space (%d) left in block: copy " |
| 1336 | "only remaining space, and we are done\n", |
| 1337 | chunk, space); |
| 1338 | /* fill only space */ |
| 1339 | llc->consume(data, space); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1340 | if (count_payload) |
| 1341 | *count_payload = space; |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1342 | /* return data block as message */ |
| 1343 | *offset = rdbi->data_len; |
| 1344 | (*num_chunks)++; |
| 1345 | return Encoding::AR_NEED_MORE_BLOCKS; |
| 1346 | } |
| 1347 | /* if FINAL chunk would fit precisely in space left */ |
| 1348 | if (chunk == space && is_final) |
| 1349 | { |
| 1350 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1351 | "would exactly fit into space (%d): because " |
| 1352 | "this is a final block, we don't add length " |
| 1353 | "header, and we are done\n", chunk, space); |
| 1354 | /* fill space */ |
| 1355 | llc->consume(data, space); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1356 | if (count_payload) |
| 1357 | *count_payload = space; |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1358 | *offset = rdbi->data_len; |
| 1359 | (*num_chunks)++; |
| 1360 | rdbi->cv = 0; |
| 1361 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
| 1362 | } |
| 1363 | /* if chunk would fit exactly in space left */ |
| 1364 | if (chunk == space) { |
| 1365 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d " |
| 1366 | "would exactly fit into space (%d): just copy " |
| 1367 | "it, and we are done. The next block will have " |
| 1368 | "to start with an empty chunk\n", |
| 1369 | chunk, space); |
| 1370 | /* fill space */ |
| 1371 | llc->consume(data, space); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1372 | if (count_payload) |
| 1373 | *count_payload = space; |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1374 | *offset = rdbi->data_len; |
| 1375 | (*num_chunks)++; |
| 1376 | return Encoding::AR_NEED_MORE_BLOCKS; |
| 1377 | } |
| 1378 | |
| 1379 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less " |
| 1380 | "than remaining space (%d): add length header to " |
| 1381 | "to delimit LLC frame\n", chunk, space); |
| 1382 | /* the LLC frame chunk ends in this block */ |
| 1383 | /* make space for delimiter */ |
| 1384 | |
| 1385 | if (delimiter != data) |
| 1386 | memmove(delimiter + 1, delimiter, data - delimiter); |
| 1387 | |
| 1388 | data += 1; |
| 1389 | (*offset) += 1; |
| 1390 | space -= 1; |
| 1391 | /* add LI to delimit frame */ |
| 1392 | li = (struct rlc_li_field_egprs *)delimiter; |
| 1393 | li->e = 1; /* Extension bit, maybe set later */ |
| 1394 | li->li = chunk; /* length of chunk */ |
| 1395 | /* tell previous extension header about the new one */ |
| 1396 | if (prev_li) |
| 1397 | prev_li->e = 0; |
| 1398 | rdbi->e = 0; /* 0: extensions present */ |
| 1399 | delimiter++; |
| 1400 | prev_li = li; |
| 1401 | (*num_chunks)++; |
| 1402 | /* copy (rest of) LLC frame to space and reset later */ |
| 1403 | llc->consume(data, chunk); |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1404 | if (count_payload) |
| 1405 | *count_payload = chunk; |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1406 | data += chunk; |
| 1407 | space -= chunk; |
| 1408 | (*offset) += chunk; |
| 1409 | /* if we have more data and we have space left */ |
| 1410 | if (space > 0) { |
| 1411 | if (!is_final) |
| 1412 | return Encoding::AR_COMPLETED_SPACE_LEFT; |
| 1413 | |
| 1414 | /* we don't have more LLC frames */ |
| 1415 | /* We will have to add another chunk with filling octets */ |
| 1416 | LOGP(DRLCMACDL, LOGL_DEBUG, |
| 1417 | "-- There is remaining space (%d): add filling byte chunk\n", |
| 1418 | space); |
| 1419 | |
| 1420 | if (delimiter != data) |
| 1421 | memmove(delimiter + 1, delimiter, data - delimiter); |
| 1422 | |
| 1423 | data += 1; |
| 1424 | (*offset) += 1; |
| 1425 | space -= 1; |
| 1426 | |
| 1427 | /* set filling bytes extension */ |
| 1428 | li = (struct rlc_li_field_egprs *)delimiter; |
| 1429 | li->e = 1; |
| 1430 | li->li = 127; |
| 1431 | |
| 1432 | /* tell previous extension header about the new one */ |
| 1433 | if (prev_li) |
| 1434 | prev_li->e = 0; |
| 1435 | |
| 1436 | delimiter++; |
| 1437 | (*num_chunks)++; |
| 1438 | |
| 1439 | rdbi->cv = 0; |
| 1440 | |
| 1441 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we " |
| 1442 | "are done.\n"); |
| 1443 | |
| 1444 | *offset = rdbi->data_len; |
| 1445 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
| 1446 | } |
| 1447 | |
| 1448 | if (is_final) { |
| 1449 | /* we don't have more LLC frames */ |
| 1450 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we " |
| 1451 | "are done.\n"); |
| 1452 | rdbi->cv = 0; |
| 1453 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
| 1454 | } |
| 1455 | |
| 1456 | /* we have no space left */ |
| 1457 | LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are " |
| 1458 | "done.\n"); |
| 1459 | return Encoding::AR_COMPLETED_BLOCK_FILLED; |
| 1460 | } |
| 1461 | |
Alexander Couzens | cb846ec | 2016-05-30 19:10:48 +0200 | [diff] [blame] | 1462 | /*! |
| 1463 | * \brief Encoding::rlc_data_to_dl_append |
| 1464 | * \param rdbi rlc/mac block info |
| 1465 | * \param cs the coding scheme to use |
| 1466 | * \param llc llc pdu |
| 1467 | * \param offset given offset within the rlc/mac block |
| 1468 | * \param num_chunks count the chunks (llc pdu data) within rlc/mac |
| 1469 | * \param data_block buffer holds rlc/mac data |
| 1470 | * \param is_final if this is the last rlc/mac within a TBF |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1471 | * \param count_payload if not NULL save the written size of payload in bytes into it |
Alexander Couzens | cb846ec | 2016-05-30 19:10:48 +0200 | [diff] [blame] | 1472 | * \return the state of the rlc/mac like if there is more space for another chunk |
| 1473 | */ |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1474 | Encoding::AppendResult Encoding::rlc_data_to_dl_append( |
| 1475 | struct gprs_rlc_data_block_info *rdbi, GprsCodingScheme cs, |
| 1476 | gprs_llc *llc, int *offset, int *num_chunks, |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1477 | uint8_t *data_block, bool is_final, int *count_payload) |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1478 | { |
| 1479 | if (cs.isGprs()) |
| 1480 | return rlc_data_to_dl_append_gprs(rdbi, |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1481 | llc, offset, num_chunks, data_block, is_final, |
| 1482 | count_payload); |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1483 | |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1484 | if (cs.isEgprs()) |
| 1485 | return rlc_data_to_dl_append_egprs(rdbi, |
Alexander Couzens | 6f0dc96 | 2016-05-30 19:30:21 +0200 | [diff] [blame] | 1486 | llc, offset, num_chunks, data_block, is_final, |
| 1487 | count_payload); |
Jacob Erlbeck | a88d065 | 2016-01-13 11:28:10 +0100 | [diff] [blame] | 1488 | |
Jacob Erlbeck | 5058bd6 | 2016-01-13 10:51:25 +0100 | [diff] [blame] | 1489 | LOGP(DRLCMACDL, LOGL_ERROR, "%s data block encoding not implemented\n", |
| 1490 | cs.name()); |
| 1491 | |
| 1492 | return AR_NEED_MORE_BLOCKS; |
Jacob Erlbeck | 14bb094 | 2016-01-12 11:58:13 +0100 | [diff] [blame] | 1493 | } |
aravind sirsikar | ed3413e | 2016-11-11 17:15:10 +0530 | [diff] [blame] | 1494 | |
| 1495 | /* |
| 1496 | * Refer 44.060 version 7.27.0 Release 7 |
| 1497 | * section 7.1.3.2.1 On receipt of a PACKET RESOURCE REQUEST message |
| 1498 | * 8.1.2.5 Establishment of uplink TBF |
| 1499 | */ |
| 1500 | void Encoding::write_packet_access_reject( |
| 1501 | bitvec * dest, uint32_t tlli) |
| 1502 | { |
| 1503 | unsigned wp = 0; |
| 1504 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1505 | bitvec_write_field(dest, &wp, 0x1, 2); // Payload Type |
| 1506 | bitvec_write_field(dest, &wp, 0x0, 2); // Uplink block with TDMA FN |
| 1507 | bitvec_write_field(dest, &wp, 0, 1); // No Polling Bit |
| 1508 | bitvec_write_field(dest, &wp, 0x0, 3); // Uplink state flag |
| 1509 | bitvec_write_field(dest, &wp, |
aravind sirsikar | ed3413e | 2016-11-11 17:15:10 +0530 | [diff] [blame] | 1510 | MT_PACKET_ACCESS_REJECT, 6); // MESSAGE TYPE |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1511 | bitvec_write_field(dest, &wp, 0, 2); // fixed 00 |
| 1512 | bitvec_write_field(dest, &wp, 0x0, 1); // TLLI / G-RNTI : bit (32) |
| 1513 | bitvec_write_field(dest, &wp, tlli, 32); // CONTENTION_RESOLUTION_TLLI |
| 1514 | bitvec_write_field(dest, &wp, 1, 1); // WAIT_INDICATION size in seconds |
aravind sirsikar | ed3413e | 2016-11-11 17:15:10 +0530 | [diff] [blame] | 1515 | /* TODO: make it configurable */ |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1516 | bitvec_write_field(dest, &wp, 5, 8); // WAIT_INDICATION value |
| 1517 | bitvec_write_field(dest, &wp, 0, 1); // WAIT_INDICATION size in seconds |
aravind sirsikar | ed3413e | 2016-11-11 17:15:10 +0530 | [diff] [blame] | 1518 | } |