/* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelNotDup */
kcg_bool CheckFirstTelNotDup_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
    /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelNotDup::telegramHeader_in */ TelegramHeader_T_BG_Types_Pkg *telegramHeader_in)
{
    /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelNotDup::_L4 */ kcg_int _L4;
    /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelNotDup::isTheFirst */ kcg_bool isTheFirst;

    _L4 = /* 1 */
        N_PIG2int_CheckBGConsistency_Pkg_SubFunction((*telegramHeader_in).n_pig);
    isTheFirst = (/* 1 */
                     N_TOTAL2int_CheckBGConsistency_Pkg_SubFunction(
                         (*telegramHeader_in).n_total) == _L4) | (_L4 == 1);
    return isTheFirst;
}
/* CheckBGConsistency_Pkg::SubFunction::CheckTelegramHeader */
kcg_bool CheckTelegramHeader_CheckBGConsistency_Pkg_SubFunction(
  /* CheckBGConsistency_Pkg::SubFunction::CheckTelegramHeader::telegramHeader_in */ TelegramHeader_T_BG_Types_Pkg *telegramHeader_in)
{
  /* CheckBGConsistency_Pkg::SubFunction::CheckTelegramHeader::_L48 */ kcg_int _L48;
  /* CheckBGConsistency_Pkg::SubFunction::CheckTelegramHeader::isConsistent */ kcg_bool isConsistent;
  
  _L48 = /* 1 */
    N_TOTAL2int_CheckBGConsistency_Pkg_SubFunction(
      (*telegramHeader_in).n_total);
  isConsistent = (/* 1 */
      N_PIG2int_CheckBGConsistency_Pkg_SubFunction(
        (*telegramHeader_in).n_pig) <= _L48) & (_L48 > 0) &
    (cTheTelegramFitsWithAll_CheckBGConsistency_Pkg >=
      (*telegramHeader_in).m_mcount) & ((*telegramHeader_in).m_mcount !=
      cTheTelegramNeverFitsAnyMessage_CheckBGConsistency_Pkg);
  return isConsistent;
}
/* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelDup */
kcg_bool CheckFirstTelDup_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelDup::telegramHeader_in */ TelegramHeader_T_BG_Types_Pkg *telegramHeader_in)
{
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelDup::_L20 */
  static kcg_int _L20;
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::CheckFirstTelDup::isTheFirst */
  static kcg_bool isTheFirst;
  
  _L20 = /* 3 */
    N_PIG2int_CheckBGConsistency_Pkg_SubFunction((*telegramHeader_in).n_pig);
  isTheFirst = ((M_DUP_This_balise_is_a_duplicate_of_the_next_balise ==
        (*telegramHeader_in).m_dup) & (/* 3 */
        N_TOTAL2int_CheckBGConsistency_Pkg_SubFunction(
          (*telegramHeader_in).n_total) - 1 == _L20)) | ((_L20 == 2) &
      ((*telegramHeader_in).m_dup ==
        M_DUP_This_balise_is_a_duplicate_of_the_previous_balise));
  return isTheFirst;
}
/* CheckBGConsistency_Pkg::CaseLinkingNotInUse */
void CaseLinkingNotInUse_CheckBGConsistency_Pkg(
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::trackSideForCheck */ TrackSide_ForCheck_T_Common_Types_Pkg *trackSideForCheck,
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::q_nvlocacc */ Q_NVLOCACC q_nvlocacc,
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::applyServiceBrake */ kcg_bool *applyServiceBrake,
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::badBaliseMessageToDMI */ kcg_bool *badBaliseMessageToDMI,
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::passedBG_out */ ReceivedMessage_T_Common_Types_Pkg *passedBG_out,
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::errorUnlinkedBG */ kcg_bool *errorUnlinkedBG)
{
  kcg_int tmp2;
  kcg_int tmp1;
  ReceivedMessage_T_Common_Types_Pkg tmp;
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::IfBlock1 */ kcg_bool IfBlock1_clock;
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::isComplete */ kcg_bool isComplete;
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::isSingle */ kcg_bool isSingle;
  /* CheckBGConsistency_Pkg::CaseLinkingNotInUse::lastTelegram */ Telegram_T_BG_Types_Pkg lastTelegram;
  
  /* 1 */
  CheckCompleteness_CheckBGConsistency_Pkg(
    &(*trackSideForCheck).telegramHeaders,
    &IfBlock1_clock,
    &isSingle,
    &lastTelegram);
  isComplete = IfBlock1_clock ^ isSingle;
  IfBlock1_clock = isComplete & isSingle;
  if (IfBlock1_clock) {
    *errorUnlinkedBG = kcg_false;
    /* 1 */
    BuildCheckedMessage_CheckBGConsistency_Pkg_SubFunction(
      trackSideForCheck,
      q_nvlocacc,
      passedBG_out);
  }
  else if (isComplete) {
    *errorUnlinkedBG = kcg_false;
    tmp2 = /* 16 */
      N_PIG2int_CheckBGConsistency_Pkg_SubFunction(
        lastTelegram.telegramheader.n_pig);
    tmp1 = /* 17 */
      N_PIG2int_CheckBGConsistency_Pkg_SubFunction(
        (*trackSideForCheck).telegramHeaders.Telegrams[0].telegramheader.n_pig);
    /* 2 */
    BuildCheckedMessage_CheckBGConsistency_Pkg_SubFunction(
      trackSideForCheck,
      q_nvlocacc,
      &tmp);
    /* 1 */
    WriteDirection2PassedBG_CheckBGConsistency_Pkg_SubFunction(
      (kcg_bool) (tmp2 > tmp1),
      &tmp,
      passedBG_out);
  }
  else {
    *errorUnlinkedBG = kcg_true;
    kcg_copy_ReceivedMessage_T_Common_Types_Pkg(
      passedBG_out,
      (ReceivedMessage_T_Common_Types_Pkg *)
        &cRecivedMesg_PlaceHolder_CheckBGConsistency_Pkg);
  }
  *applyServiceBrake = *errorUnlinkedBG;
  *badBaliseMessageToDMI = *errorUnlinkedBG;
}
/* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs */
kcg_bool Check2N_PIGs_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::n_pig_1 */N_PIG n_pig_1,
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::m_dup_1 */M_DUP m_dup_1,
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::n_pig_2 */N_PIG n_pig_2,
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::m_dup_2 */M_DUP m_dup_2,
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::crcCheck_1 */kcg_bool crcCheck_1,
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::crcCheck_2 */kcg_bool crcCheck_2)
{
  static kcg_int tmp1;
  static kcg_int tmp;
  /* CheckBGConsistency_Pkg::SubFunction::DuplicationCheck::Check2N_PIGs::valid */
  static kcg_bool valid;
  
  tmp1 = /* 2 */ N_PIG2int_CheckBGConsistency_Pkg_SubFunction(n_pig_2);
  tmp = /* 1 */ N_PIG2int_CheckBGConsistency_Pkg_SubFunction(n_pig_1);
  switch (tmp1 - tmp) {
    case 1 :
      valid = /* 1 */
        Dif1Nominal_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
          m_dup_1,
          m_dup_2,
          crcCheck_1,
          crcCheck_2);
      break;
    case 2 :
      valid = /* 1 */
        Dif2Nominal_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
          m_dup_1,
          m_dup_2,
          crcCheck_1,
          crcCheck_2);
      break;
    case 3 :
      valid = /* 1 */
        Dif3Nominal_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
          m_dup_1,
          m_dup_2,
          crcCheck_1,
          crcCheck_2);
      break;
    case - 1 :
      valid = /* 2 */
        Dif1Nominal_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
          m_dup_2,
          m_dup_1,
          crcCheck_2,
          crcCheck_1);
      break;
    case - 2 :
      valid = /* 2 */
        Dif2Nominal_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
          m_dup_2,
          m_dup_1,
          crcCheck_2,
          crcCheck_1);
      break;
    case - 3 :
      valid = /* 2 */
        Dif3Nominal_CheckBGConsistency_Pkg_SubFunction_DuplicationCheck(
          m_dup_2,
          m_dup_1,
          crcCheck_2,
          crcCheck_1);
      break;
    
    default :
      valid = kcg_false;
  }
  return valid;
}
/* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets */
void MergAllPackets_CheckBGConsistency_Pkg_SubFunction_MergFunction(
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::telegramACC */Telegram_T_BG_Types_Pkg *telegramACC,
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::telegram */Telegram_T_BG_Types_Pkg *telegram,
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::go_on */kcg_bool *go_on,
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::telegram_out */Telegram_T_BG_Types_Pkg *telegram_out)
{
  static kcg_int i;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::IfBlock1::else */
  static kcg_bool else_clock_IfBlock1;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::IfBlock1::else::else::then::_L17 */
  static array_int_500 _L17_IfBlock1;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::IfBlock1::else::else::then::_L13 */
  static kcg_int _L13_IfBlock1;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::IfBlock1::else::else::then::_L11 */
  static kcg_int _L11_IfBlock1;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::IfBlock1::else::else::then::_L12 */
  static kcg_int _L12_IfBlock1;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::IfBlock1 */
  static kcg_bool IfBlock1_clock;
  /* CheckBGConsistency_Pkg::SubFunction::MergFunction::MergAllPackets::diff */
  static kcg_int diff;
  
  i = /* 3 */
    N_PIG2int_CheckBGConsistency_Pkg_SubFunction(
      (*telegram).telegramheader.n_pig);
  diff = /* 4 */
    N_PIG2int_CheckBGConsistency_Pkg_SubFunction(
      (*telegramACC).telegramheader.n_pig) - i;
  *go_on = (*telegram).valid;
  IfBlock1_clock = (diff == - 1) & (((*telegramACC).telegramheader.m_dup ==
        M_DUP_This_balise_is_a_duplicate_of_the_next_balise) &
      ((*telegram).telegramheader.m_dup ==
        M_DUP_This_balise_is_a_duplicate_of_the_previous_balise)) & *go_on;
  if (IfBlock1_clock) {
    kcg_copy_Telegram_T_BG_Types_Pkg(telegram_out, telegram);
    kcg_copy_CompressedPacketData_T_Common_Types_Pkg(
      &(*telegram_out).packets.PacketData,
      &(*telegramACC).packets.PacketData);
  }
  else {
    else_clock_IfBlock1 = (diff == 1) & (((*telegramACC).telegramheader.m_dup ==
          M_DUP_This_balise_is_a_duplicate_of_the_previous_balise) &
        ((*telegram).telegramheader.m_dup ==
          M_DUP_This_balise_is_a_duplicate_of_the_next_balise)) & *go_on;
    if (else_clock_IfBlock1) {
      kcg_copy_Telegram_T_BG_Types_Pkg(telegram_out, telegram);
      kcg_copy_CompressedPacketData_T_Common_Types_Pkg(
        &(*telegram_out).packets.PacketData,
        &(*telegramACC).packets.PacketData);
    }
    else if (*go_on) {
      /* 1 */
      FindIndexOfMetaDataIteration_CheckBGConsistency_Pkg_SubFunction_MergFunction(
        &(*telegramACC).packets.PacketHeaders,
        &_L11_IfBlock1,
        &_L12_IfBlock1);
      _L13_IfBlock1 = _L12_IfBlock1 + 1;
      for (i = 0; i < 500; i++) {
        _L17_IfBlock1[i] = _L13_IfBlock1;
      }
      kcg_copy_Telegram_T_BG_Types_Pkg(telegram_out, telegram);
      /* 1 */
      Merg2MetaDataIteration_CheckBGConsistency_Pkg_SubFunction_MergFunction(
        &(*telegramACC).packets.PacketHeaders,
        telegram,
        &(*telegram_out).packets.PacketHeaders);
      /* 1 */
      Merg2PacketsListArray_CheckBGConsistency_Pkg_SubFunction_MergFunction(
        &(*telegramACC).packets.PacketData,
        &_L17_IfBlock1,
        &(*telegram).packets.PacketData,
        &(*telegram_out).packets.PacketData);
    }
    else {
      kcg_copy_Telegram_T_BG_Types_Pkg(telegram_out, telegramACC);
    }
  }
}