Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 1 | #include "RLCMAC_Types.hh" |
| 2 | #include "GSM_Types.hh" |
| 3 | /* Decoding of TS 44.060 GPRS RLC/MAC blocks, portions requiring manual functions |
| 4 | * beyond what TITAN RAW coder can handle internally. |
| 5 | * |
| 6 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 7 | */ |
| 8 | |
| 9 | namespace RLCMAC__Types { |
| 10 | |
| 11 | OCTETSTRING enc__RlcmacDlDataBlock(const RlcmacDlDataBlock& si) |
| 12 | { |
| 13 | RlcmacDlDataBlock in = si; |
| 14 | OCTETSTRING ret_val; |
| 15 | TTCN_Buffer ttcn_buffer; |
| 16 | int i; |
| 17 | |
| 18 | /* Fix 'e' bit of initial header based on following blocks */ |
Harald Welte | 244cd8a | 2017-08-26 09:25:20 +0200 | [diff] [blame] | 19 | if (!in.blocks().is_bound() || |
| 20 | (in.blocks().size_of() == 1 && !in.blocks()[0].hdr().is_bound())) |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 21 | in.mac__hdr().hdr__ext().e() = true; |
| 22 | else |
| 23 | in.mac__hdr().hdr__ext().e() = false; |
| 24 | |
| 25 | /* use automatic/generated decoder for header */ |
| 26 | in.mac__hdr().encode(DlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 27 | |
| 28 | /* Add LI octets, if any */ |
| 29 | if (in.blocks().is_bound() && |
| 30 | (in.blocks().size_of() != 1 || in.blocks()[0].hdr().is_bound())) { |
| 31 | /* first write LI octets */ |
| 32 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 33 | /* fix the 'E' bit in case it is not clear */ |
| 34 | if (i < in.blocks().size_of()-1) |
| 35 | in.blocks()[i].hdr().e() = false; |
| 36 | else |
| 37 | in.blocks()[i].hdr().e() = true; |
| 38 | in.blocks()[i].hdr().encode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 39 | } |
| 40 | } |
| 41 | if (in.blocks().is_bound()) { |
| 42 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 43 | if (!in.blocks()[i].is_bound()) |
| 44 | continue; |
| 45 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | ttcn_buffer.get_string(ret_val); |
| 50 | return ret_val; |
| 51 | } |
| 52 | |
| 53 | RlcmacDlDataBlock dec__RlcmacDlDataBlock(const OCTETSTRING& stream) |
| 54 | { |
| 55 | RlcmacDlDataBlock ret_val; |
| 56 | TTCN_Buffer ttcn_buffer(stream); |
| 57 | int num_llc_blocks = 0; |
| 58 | |
| 59 | /* use automatic/generated decoder for header */ |
| 60 | ret_val.mac__hdr().decode(DlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 61 | |
| 62 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 63 | if (ret_val.mac__hdr().hdr__ext().e() == false) { |
| 64 | /* extension octet follows, i.e. optional Llc length octets */ |
| 65 | while (1) { |
| 66 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 67 | LlcBlock lb; |
| 68 | lb.hdr().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 69 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 70 | |
| 71 | /* if E == '1'B, we can proceed further */ |
| 72 | if (lb.hdr().e() == true) |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /* RLC blocks at end */ |
| 78 | if (ret_val.mac__hdr().hdr__ext().e() == true) { |
| 79 | LlcBlock lb; |
| 80 | unsigned int length = ttcn_buffer.get_read_len(); |
| 81 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 82 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 83 | * data block */ |
| 84 | lb.payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 85 | ttcn_buffer.increase_pos(length); |
| 86 | ret_val.blocks()[0] = lb; |
| 87 | } else { |
| 88 | if (ret_val.blocks().is_bound()) { |
| 89 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 90 | unsigned int length = ret_val.blocks()[i].hdr().length__ind(); |
| 91 | if (length > ttcn_buffer.get_read_len()) |
| 92 | length = ttcn_buffer.get_read_len(); |
| 93 | ret_val.blocks()[i].payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 94 | ttcn_buffer.increase_pos(length); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return ret_val; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | OCTETSTRING enc__RlcmacUlDataBlock(const RlcmacUlDataBlock& si) |
| 104 | { |
| 105 | RlcmacUlDataBlock in = si; |
| 106 | OCTETSTRING ret_val; |
| 107 | TTCN_Buffer ttcn_buffer; |
| 108 | int i; |
| 109 | |
| 110 | /* Fix 'e' bit of initial header based on following blocks */ |
Harald Welte | 244cd8a | 2017-08-26 09:25:20 +0200 | [diff] [blame] | 111 | if (!in.blocks().is_bound() || |
| 112 | (in.blocks().size_of() == 1 && !in.blocks()[0].hdr().is_bound())) |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 113 | in.mac__hdr().e() = true; |
| 114 | else |
| 115 | in.mac__hdr().e() = false; |
| 116 | |
| 117 | /* Fix other presence indications */ |
Harald Welte | 2072ab6 | 2017-07-31 18:33:35 +0200 | [diff] [blame] | 118 | in.mac__hdr().tlli__ind() = in.tlli().is_bound() && in.tlli() != OMIT_VALUE; |
| 119 | in.mac__hdr().pfi__ind() = in.pfi().is_bound() && in.pfi() != OMIT_VALUE; |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 120 | |
| 121 | /* use automatic/generated decoder for header */ |
| 122 | in.mac__hdr().encode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 123 | |
| 124 | /* Add LI octets, if any */ |
| 125 | if (in.blocks().is_bound() && |
| 126 | (in.blocks().size_of() != 1 || in.blocks()[0].hdr().is_bound())) { |
| 127 | /* first write LI octets */ |
| 128 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 129 | /* fix the 'E' bit in case it is not clear */ |
| 130 | if (i < in.blocks().size_of()-1) |
| 131 | in.blocks()[i].hdr().e() = false; |
| 132 | else |
| 133 | in.blocks()[i].hdr().e() = true; |
| 134 | in.blocks()[i].hdr().encode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (in.mac__hdr().tlli__ind()) { |
Harald Welte | acc93ab | 2018-03-02 21:39:09 +0100 | [diff] [blame^] | 139 | ttcn_buffer.put_string(in.tlli()); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (in.mac__hdr().pfi__ind()) { |
| 143 | in.pfi().encode(RlcmacUlDataBlock_pfi_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 144 | } |
| 145 | |
| 146 | if (in.blocks().is_bound()) { |
| 147 | for (i = 0; i < in.blocks().size_of(); i++) { |
| 148 | if (!in.blocks()[i].is_bound()) |
| 149 | continue; |
| 150 | ttcn_buffer.put_string(in.blocks()[i].payload()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | ttcn_buffer.get_string(ret_val); |
| 155 | return ret_val; |
| 156 | } |
| 157 | |
| 158 | RlcmacUlDataBlock dec__RlcmacUlDataBlock(const OCTETSTRING& stream) |
| 159 | { |
| 160 | RlcmacUlDataBlock ret_val; |
| 161 | TTCN_Buffer ttcn_buffer(stream); |
| 162 | int num_llc_blocks = 0; |
| 163 | |
| 164 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 165 | TTCN_Logger::log_event_str("==================================\n" |
| 166 | "dec_RlcmacUlDataBlock(): Stream before decoding: "); |
| 167 | stream.log(); |
| 168 | TTCN_Logger::end_event(); |
| 169 | |
| 170 | /* use automatic/generated decoder for header */ |
| 171 | ret_val.mac__hdr().decode(UlMacDataHeader_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 172 | |
| 173 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 174 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream after decoding hdr: "); |
| 175 | ttcn_buffer.log(); |
| 176 | TTCN_Logger::end_event(); |
| 177 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 178 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val after decoding hdr: "); |
| 179 | ret_val.log(); |
| 180 | TTCN_Logger::end_event(); |
| 181 | |
| 182 | /* Manually decoder remainder of ttcn_buffer, containing optional header octets, |
| 183 | * optional tlli, optional pfi and LLC Blocks */ |
| 184 | |
| 185 | /* optional extension octets, containing LI+M+E of Llc blocks */ |
| 186 | if (ret_val.mac__hdr().e() == false) { |
| 187 | /* extension octet follows, i.e. optional Llc length octets */ |
| 188 | while (1) { |
| 189 | /* decode one more extension octet with LlcBlocHdr inside */ |
| 190 | LlcBlock lb; |
| 191 | lb.hdr().decode(LlcBlockHdr_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 192 | ret_val.blocks()[num_llc_blocks++] = lb; |
| 193 | |
| 194 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 195 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream after decoding ExtOct: "); |
| 196 | ttcn_buffer.log(); |
| 197 | TTCN_Logger::end_event(); |
| 198 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 199 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val after decoding ExtOct: "); |
| 200 | ret_val.log(); |
| 201 | TTCN_Logger::end_event(); |
| 202 | |
| 203 | /* if E == '1'B, we can proceed further */ |
| 204 | if (lb.hdr().e() == true) |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* parse optional TLLI */ |
| 210 | if (ret_val.mac__hdr().tlli__ind()) { |
Harald Welte | acc93ab | 2018-03-02 21:39:09 +0100 | [diff] [blame^] | 211 | ret_val.tlli() = OCTETSTRING(4, ttcn_buffer.get_read_data()); |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 212 | ttcn_buffer.increase_pos(4); |
| 213 | } |
| 214 | /* parse optional PFI */ |
| 215 | if (ret_val.mac__hdr().pfi__ind()) { |
| 216 | ret_val.pfi().decode(RlcmacUlDataBlock_pfi_descr_, ttcn_buffer, TTCN_EncDec::CT_RAW); |
| 217 | } |
| 218 | |
| 219 | /* RLC blocks at end */ |
| 220 | if (ret_val.mac__hdr().e() == true) { |
| 221 | LlcBlock lb; |
| 222 | unsigned int length = ttcn_buffer.get_read_len(); |
| 223 | /* LI not present: The Upper Layer PDU that starts with the current RLC data block either |
| 224 | * fills the current RLC data block precisely or continues in the following in-sequence RLC |
| 225 | * data block */ |
| 226 | lb.payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 227 | ttcn_buffer.increase_pos(length); |
| 228 | ret_val.blocks()[0] = lb; |
| 229 | } else { |
| 230 | if (ret_val.blocks().is_bound()) { |
| 231 | for (int i = 0; i < ret_val.blocks().size_of(); i++) { |
| 232 | unsigned int length = ret_val.blocks()[i].hdr().length__ind(); |
| 233 | if (length > ttcn_buffer.get_read_len()) |
| 234 | length = ttcn_buffer.get_read_len(); |
| 235 | ret_val.blocks()[i].payload() = OCTETSTRING(length, ttcn_buffer.get_read_data()); |
| 236 | ttcn_buffer.increase_pos(length); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 242 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): Stream before return: "); |
| 243 | ttcn_buffer.log(); |
| 244 | TTCN_Logger::end_event(); |
| 245 | TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC); |
| 246 | TTCN_Logger::log_event_str("dec_RlcmacUlDataBlock(): ret_val before return: "); |
| 247 | ret_val.log(); |
| 248 | TTCN_Logger::end_event(); |
| 249 | |
| 250 | return ret_val; |
| 251 | } |
| 252 | |
Harald Welte | 78a1af6 | 2017-07-31 17:33:56 +0200 | [diff] [blame] | 253 | OCTETSTRING enc__RlcmacUlBlock(const RlcmacUlBlock& si) |
| 254 | { |
| 255 | if (si.ischosen(RlcmacUlBlock::ALT_data)) |
| 256 | return enc__RlcmacUlDataBlock(si.data()); |
| 257 | else |
| 258 | return enc__RlcmacUlCtrlBlock(si.ctrl()); |
| 259 | } |
| 260 | |
| 261 | OCTETSTRING enc__RlcmacDlBlock(const RlcmacDlBlock& si) |
| 262 | { |
| 263 | if (si.ischosen(RlcmacDlBlock::ALT_data)) |
| 264 | return enc__RlcmacDlDataBlock(si.data()); |
| 265 | else |
| 266 | return enc__RlcmacDlCtrlBlock(si.ctrl()); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | RlcmacUlBlock dec__RlcmacUlBlock(const OCTETSTRING& stream) |
| 271 | { |
| 272 | RlcmacUlBlock ret_val; |
| 273 | unsigned char pt = stream[0].get_octet() >> 6; |
| 274 | |
| 275 | if (pt == MacPayloadType::MAC__PT__RLC__DATA) |
| 276 | ret_val.data() = dec__RlcmacUlDataBlock(stream); |
| 277 | else |
| 278 | ret_val.ctrl() = dec__RlcmacUlCtrlBlock(stream); |
| 279 | |
| 280 | return ret_val; |
| 281 | } |
| 282 | |
| 283 | RlcmacDlBlock dec__RlcmacDlBlock(const OCTETSTRING& stream) |
| 284 | { |
| 285 | RlcmacDlBlock ret_val; |
| 286 | unsigned char pt = stream[0].get_octet() >> 6; |
| 287 | |
| 288 | if (pt == MacPayloadType::MAC__PT__RLC__DATA) |
| 289 | ret_val.data() = dec__RlcmacDlDataBlock(stream); |
| 290 | else |
| 291 | ret_val.ctrl() = dec__RlcmacDlCtrlBlock(stream); |
| 292 | |
| 293 | return ret_val; |
| 294 | } |
| 295 | |
Harald Welte | 43e060a | 2017-07-30 22:38:03 +0200 | [diff] [blame] | 296 | |
| 297 | } // namespace |