<commit_before>/**
 *
 * \section COPYRIGHT
 *
 * Copyright 2013-2015 The srsLTE Developers. See the
 * COPYRIGHT file at the top-level directory of this distribution.
 *
 * \section LICENSE
 *
 * This file is part of the srsLTE library.
 *
 * srsLTE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * srsLTE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * A copy of the GNU Affero General Public License can be found in
 * the LICENSE file in the top-level directory of this distribution
 * and at http://www.gnu.org/licenses/.
 *
 */

#include <strings.h>
#include <string.h>
#include <stdlib.h>

#include "srsapps/ue/mac/pdu.h"
#include "srslte/srslte.h"

namespace srslte {
  namespace ue {
   
    
void sch_pdu::fprint(FILE* stream)
{
  fprintf(stream, "MAC SDU for UL/DL-SCH. ");
  pdu::fprint(stream);
}

void sch_subh::fprint(FILE* stream)
{
  if (is_sdu()) {
    fprintf(stream, "SDU LCHID=%d, SDU nof_bytes=%d\n", lcid, nof_bytes);
  } else {
    if (parent->is_ul()) {
      switch(lcid) {
        case C_RNTI:
          fprintf(stream, "C-RNTI CE: %d\n", get_c_rnti());
          break;
        case PHD_REPORT:
          fprintf(stream, "C-RNTI CE: %d\n", get_c_rnti());
          break;
        case TRUNC_BSR:
          fprintf(stream, "Truncated BSR CE\n");
          break;
        case SHORT_BSR:
          fprintf(stream, "Short BSR CE\n");
          break;
        case LONG_BSR:
          fprintf(stream, "Long BSR CE\n");
          break;
        case PADDING:
          fprintf(stream, "PADDING\n");
      }
    } else {
      switch(lcid) {
        case CON_RES_ID:
          fprintf(stream, "Contention Resolution ID CE: 0x%lx\n", get_con_res_id());
          break;
        case TA_CMD:
          fprintf(stream, "Time Advance Command CE: %d\n", get_ta_cmd());
          break;
        case DRX_CMD:
          fprintf(stream, "DRX Command CE: Not implemented\n");
          break;
        case PADDING:
          fprintf(stream, "PADDING\n");
      }      
    }
  }
}

void sch_pdu::parse_packet(uint8_t *ptr)
{
  pdu::parse_packet(ptr);
  
  // Correct size for last SDU 
  if (nof_subheaders > 0) {
    uint32_t read_len = 0; 
    for (int i=0;i<nof_subheaders-1;i++) {
      read_len += subheaders[i].size_plus_header();
    }
    if (pdu_len-read_len-1 >= 0) {
      subheaders[nof_subheaders-1].set_payload_size(pdu_len-read_len-1);
    } else {
      fprintf(stderr,"Reading MAC PDU: negative payload for last subheader\n");
    }
  }
}
    
// Section 6.1.2
bool sch_pdu::write_packet(uint8_t* ptr)
{
  uint8_t *init_ptr = ptr; 
  bool last_is_padding = false; 

  // Find last SDU or CE 
  int last_sh;
  int last_sdu = nof_subheaders-1; 
  while(!subheaders[last_sdu].is_sdu() && last_sdu >= 0) {
    last_sdu--;
  }
  int last_ce = nof_subheaders-1; 
  while(subheaders[last_ce].is_sdu() && last_ce >= 0) {
    last_ce--;
  }
  last_sh = subheaders[last_sdu].is_sdu()?last_sdu:last_ce;  
  
  // Add multi-byte padding if there are more than 2 bytes or there are 2 bytes 
  // and there is at least one SDU 
  if (rem_len > 2 || (rem_len==2 && subheaders[last_sdu].is_sdu())) {
    last_is_padding = true; 
  } else if (rem_len > 0) {
    if (subheaders[last_sdu].is_sdu()) {
      rem_len++; 
    }
    // Add single or two-byte padding if required
    if (rem_len == 1 || rem_len == 2) {
      sch_subh padding; 
      padding.set_padding(); 
      for (int i=0;i<rem_len;i++) {
        padding.write_subheader(&ptr, false);  
      }
      rem_len = 0;
    }     
  }<commit_msg>Fixed bug in MAC PDU generation missing 1 byte<commit_after>/**
 *
 * \section COPYRIGHT
 *
 * Copyright 2013-2015 The srsLTE Developers. See the
 * COPYRIGHT file at the top-level directory of this distribution.
 *
 * \section LICENSE
 *
 * This file is part of the srsLTE library.
 *
 * srsLTE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * srsLTE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * A copy of the GNU Affero General Public License can be found in
 * the LICENSE file in the top-level directory of this distribution
 * and at http://www.gnu.org/licenses/.
 *
 */

#include <strings.h>
#include <string.h>
#include <stdlib.h>

#include "srsapps/ue/mac/pdu.h"
#include "srslte/srslte.h"

namespace srslte {
  namespace ue {
   
    
void sch_pdu::fprint(FILE* stream)
{
  fprintf(stream, "MAC SDU for UL/DL-SCH. ");
  pdu::fprint(stream);
}

void sch_subh::fprint(FILE* stream)
{
  if (is_sdu()) {
    fprintf(stream, "SDU LCHID=%d, SDU nof_bytes=%d\n", lcid, nof_bytes);
  } else {
    if (parent->is_ul()) {
      switch(lcid) {
        case C_RNTI:
          fprintf(stream, "C-RNTI CE: %d\n", get_c_rnti());
          break;
        case PHD_REPORT:
          fprintf(stream, "C-RNTI CE: %d\n", get_c_rnti());
          break;
        case TRUNC_BSR:
          fprintf(stream, "Truncated BSR CE\n");
          break;
        case SHORT_BSR:
          fprintf(stream, "Short BSR CE\n");
          break;
        case LONG_BSR:
          fprintf(stream, "Long BSR CE\n");
          break;
        case PADDING:
          fprintf(stream, "PADDING\n");
      }
    } else {
      switch(lcid) {
        case CON_RES_ID:
          fprintf(stream, "Contention Resolution ID CE: 0x%lx\n", get_con_res_id());
          break;
        case TA_CMD:
          fprintf(stream, "Time Advance Command CE: %d\n", get_ta_cmd());
          break;
        case DRX_CMD:
          fprintf(stream, "DRX Command CE: Not implemented\n");
          break;
        case PADDING:
          fprintf(stream, "PADDING\n");
      }      
    }
  }
}

void sch_pdu::parse_packet(uint8_t *ptr)
{
  pdu::parse_packet(ptr);
  
  // Correct size for last SDU 
  if (nof_subheaders > 0) {
    uint32_t read_len = 0; 
    for (int i=0;i<nof_subheaders-1;i++) {
      read_len += subheaders[i].size_plus_header();
    }
    if (pdu_len-read_len-1 >= 0) {
      subheaders[nof_subheaders-1].set_payload_size(pdu_len-read_len-1);
    } else {
      fprintf(stderr,"Reading MAC PDU: negative payload for last subheader\n");
    }
  }
}
    
// Section 6.1.2
bool sch_pdu::write_packet(uint8_t* ptr)
{
  uint8_t *init_ptr = ptr; 
  bool last_is_padding = false; 

  // Find last SDU or CE 
  int last_sh;
  int last_sdu = nof_subheaders-1; 
  while(!subheaders[last_sdu].is_sdu() && last_sdu >= 0) {
    last_sdu--;
  }
  int last_ce = nof_subheaders-1; 
  while(subheaders[last_ce].is_sdu() && last_ce >= 0) {
    last_ce--;
  }
  last_sh = subheaders[last_sdu].is_sdu()?last_sdu:last_ce;  
  
  // Add multi-byte padding if there are more than 2 bytes or there are 2 bytes 
  // and there is at least one SDU 
  if (rem_len > 2) {
    last_is_padding = true; 
  } else if (rem_len > 0) {
    // Add single or two-byte padding if required
    if (rem_len == 1 || rem_len == 2) {
      sch_subh padding; 
      padding.set_padding(); 
      for (int i=0;i<rem_len;i++) {
        padding.write_subheader(&ptr, false);  
      }
      rem_len = 0;
    }     
  }
{'fraction_non_alphanumeric': 0.11943189154293092, 'fraction_numerical': 0.010071013557133635, 'mean_word_length': 2.5321477428180574, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2728702', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2755, 'n_words': 751}