Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ws_mpx_header.c Source File

ws_mpx_header.c

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "nsconfig.h"
00019 #include <string.h>
00020 #include "ns_types.h"
00021 #include "ns_list.h"
00022 #include "ns_trace.h"
00023 #include "nsdynmemLIB.h"
00024 #include "common_functions.h"
00025 #include "mac_common_defines.h"
00026 #include "ws_mpx_header.h"
00027 
00028 bool ws_llc_mpx_header_frame_parse(uint8_t *ptr, uint16_t length, mpx_msg_t *msg)
00029 {
00030     if (!length) {
00031         return false;
00032     }
00033     memset(msg, 0, sizeof(mpx_msg_t));
00034     bool fragmented_number_present = false;
00035     bool multiplex_id_present = false;
00036     bool fragment_total_size = false;
00037 
00038     msg->transfer_type = *ptr & 7;
00039     msg->transaction_id = ((*ptr++ & 0xf8) >> 3);
00040     length--;
00041 
00042 
00043     switch (msg->transfer_type) {
00044         case MPX_FT_FULL_FRAME:
00045             multiplex_id_present = true;
00046             break;
00047         case MPX_FT_FULL_FRAME_SMALL_MULTILEX_ID:
00048             break;
00049         case MPX_FT_FIRST_OR_SUB_FRAGMENT:
00050         case MPX_FT_LAST_FRAGMENT:
00051             fragmented_number_present = true;
00052             if (length < 2) {
00053                 return false;
00054             }
00055             break;
00056         case MPX_FT_ABORT:
00057             if (length == 2) {
00058                 fragment_total_size = true;
00059             } else if (length) {
00060                 return false;
00061             }
00062             break;
00063         default:
00064             return false;
00065     }
00066 
00067     if (fragmented_number_present) {
00068 
00069         msg->fragment_number = *ptr++;
00070         length--;
00071         if (msg->fragment_number == 0) { //First fragment
00072             fragment_total_size = true;
00073             multiplex_id_present = true;
00074         }
00075     }
00076 
00077     if (fragment_total_size) {
00078         if (length < 2) {
00079             return false;
00080         }
00081         msg->total_upper_layer_size = common_read_16_bit_inverse(ptr);
00082         ptr += 2;
00083         length -= 2;
00084     }
00085 
00086     if (multiplex_id_present) {
00087         if (length < 3) {
00088             return false;
00089         }
00090         msg->multiplex_id = common_read_16_bit_inverse(ptr);
00091         ptr += 2;
00092         length -= 2;
00093     }
00094 
00095     msg->frame_ptr = ptr;
00096     msg->frame_length = length;
00097     return true;
00098 }
00099 
00100 
00101 uint8_t *ws_llc_mpx_header_write(uint8_t *ptr, const mpx_msg_t *msg)
00102 {
00103 
00104     bool fragmented_number_present = false;
00105     bool multiplex_id_present = false;
00106     bool fragment_total_size = false;
00107 
00108     *ptr = msg->transfer_type;
00109     *ptr++ |= ((msg->transaction_id << 3) & 0xf8);
00110 
00111     switch (msg->transfer_type) {
00112         case MPX_FT_FULL_FRAME:
00113             multiplex_id_present = true;
00114             break;
00115         case MPX_FT_FULL_FRAME_SMALL_MULTILEX_ID:
00116             break;
00117         case MPX_FT_FIRST_OR_SUB_FRAGMENT:
00118         case MPX_FT_LAST_FRAGMENT:
00119             fragmented_number_present = true;
00120             if (msg->fragment_number == 0) {
00121                 fragment_total_size = true;
00122                 multiplex_id_present = true;
00123             }
00124             break;
00125         case MPX_FT_ABORT:
00126             if (msg->total_upper_layer_size) {
00127                 fragment_total_size = true;
00128             }
00129             break;
00130         default:
00131             break;
00132     }
00133 
00134     if (fragmented_number_present) {
00135         *ptr++ = msg->fragment_number;
00136     }
00137 
00138     if (fragment_total_size) {
00139         ptr = common_write_16_bit_inverse(msg->total_upper_layer_size, ptr);
00140     }
00141 
00142     if (multiplex_id_present) {
00143         ptr = common_write_16_bit_inverse(msg->multiplex_id, ptr);
00144     }
00145     return ptr;
00146 }