|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include "nsconfig.h" |
| 18 | +#include <string.h> |
| 19 | +#include "ns_types.h" |
| 20 | +#include "ns_list.h" |
| 21 | +#include "ns_trace.h" |
| 22 | +#include "nsdynmemLIB.h" |
| 23 | +#include "common_functions.h" |
| 24 | +#include "mac_common_defines.h" |
| 25 | +#include "ws_mpx_header.h" |
| 26 | + |
| 27 | +bool ws_llc_mpx_header_frame_parse(uint8_t *ptr, uint16_t length, mpx_msg_t *msg) |
| 28 | +{ |
| 29 | + if (!length) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + memset(msg, 0, sizeof(mpx_msg_t)); |
| 33 | + bool fragmented_number_present = false; |
| 34 | + bool multiplex_id_present = false; |
| 35 | + bool fragment_total_size = false; |
| 36 | + |
| 37 | + msg->transfer_type = *ptr & 7; |
| 38 | + msg->transaction_id = ((*ptr++ & 0xf8) >> 3); |
| 39 | + length--; |
| 40 | + |
| 41 | + |
| 42 | + switch (msg->transfer_type) { |
| 43 | + case MPX_FT_FULL_FRAME: |
| 44 | + multiplex_id_present = true; |
| 45 | + break; |
| 46 | + case MPX_FT_FULL_FRAME_SMALL_MULTILEX_ID: |
| 47 | + break; |
| 48 | + case MPX_FT_FIRST_OR_SUB_FRAGMENT: |
| 49 | + case MPX_FT_LAST_FRAGMENT: |
| 50 | + fragmented_number_present = true; |
| 51 | + if (length < 2) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + break; |
| 55 | + case MPX_FT_ABORT: |
| 56 | + if (length == 2) { |
| 57 | + fragment_total_size = true; |
| 58 | + } else if (length) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + break; |
| 62 | + default: |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + if (fragmented_number_present) { |
| 67 | + |
| 68 | + msg->fragment_number = *ptr++; |
| 69 | + length--; |
| 70 | + if (msg->fragment_number == 0) { //First fragment |
| 71 | + fragment_total_size = true; |
| 72 | + multiplex_id_present = true; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (fragment_total_size) { |
| 77 | + if (length < 2) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + msg->total_upper_layer_size = common_read_16_bit_inverse(ptr); |
| 81 | + ptr += 2; |
| 82 | + length -= 2; |
| 83 | + } |
| 84 | + |
| 85 | + if (multiplex_id_present) { |
| 86 | + if (length < 3) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + msg->multiplex_id = common_read_16_bit_inverse(ptr); |
| 90 | + ptr += 2; |
| 91 | + length -= 2; |
| 92 | + } |
| 93 | + |
| 94 | + msg->frame_ptr = ptr; |
| 95 | + msg->frame_length = length; |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +uint8_t * ws_llc_mpx_header_write(uint8_t *ptr, const mpx_msg_t *msg) |
| 101 | +{ |
| 102 | + |
| 103 | + bool fragmented_number_present = false; |
| 104 | + bool multiplex_id_present = false; |
| 105 | + bool fragment_total_size = false; |
| 106 | + |
| 107 | + *ptr = msg->transfer_type; |
| 108 | + *ptr++ |= ((msg->transaction_id << 3) & 0xf8); |
| 109 | + |
| 110 | + switch (msg->transfer_type) { |
| 111 | + case MPX_FT_FULL_FRAME: |
| 112 | + multiplex_id_present = true; |
| 113 | + break; |
| 114 | + case MPX_FT_FULL_FRAME_SMALL_MULTILEX_ID: |
| 115 | + break; |
| 116 | + case MPX_FT_FIRST_OR_SUB_FRAGMENT: |
| 117 | + case MPX_FT_LAST_FRAGMENT: |
| 118 | + fragmented_number_present = true; |
| 119 | + if (msg->fragment_number == 0) { |
| 120 | + fragment_total_size = true; |
| 121 | + multiplex_id_present = true; |
| 122 | + } |
| 123 | + break; |
| 124 | + case MPX_FT_ABORT: |
| 125 | + if (msg->total_upper_layer_size) { |
| 126 | + fragment_total_size = true; |
| 127 | + } |
| 128 | + break; |
| 129 | + default: |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + if (fragmented_number_present) { |
| 134 | + *ptr++ = msg->fragment_number; |
| 135 | + } |
| 136 | + |
| 137 | + if (fragment_total_size) { |
| 138 | + ptr = common_write_16_bit_inverse(msg->total_upper_layer_size, ptr); |
| 139 | + } |
| 140 | + |
| 141 | + if (multiplex_id_present) { |
| 142 | + ptr = common_write_16_bit_inverse(msg->multiplex_id,ptr); |
| 143 | + } |
| 144 | + return ptr; |
| 145 | +} |
0 commit comments