int encode_authentication_request(authentication_request_msg *authentication_request, uint8_t *buffer, uint32_t len)
{
  int encoded = 0;
  int encode_result = 0;

  /* Checking IEI and pointer */
  CHECK_PDU_POINTER_AND_LENGTH_ENCODER(buffer, AUTHENTICATION_REQUEST_MINIMUM_LENGTH, len);

  *(buffer + encoded) = ((encode_u8_nas_key_set_identifier(&authentication_request->naskeysetidentifierasme) & 0x0f) << 4) | 0x00;
  encoded++;

  if ((encode_result =
         encode_authentication_parameter_rand(&authentication_request->authenticationparameterrand,
             0, buffer + encoded, len - encoded)) < 0)        //Return in case of error
    return encode_result;
  else
    encoded += encode_result;

  if ((encode_result =
         encode_authentication_parameter_autn(&authentication_request->authenticationparameterautn,
             0, buffer + encoded, len - encoded)) < 0)        //Return in case of error
    return encode_result;
  else
    encoded += encode_result;

  return encoded;
}
int
encode_security_mode_command (
  security_mode_command_msg * security_mode_command,
  uint8_t * buffer,
  uint32_t len)
{
  int                                     encoded = 0;
  int                                     encode_result = 0;

  /*
   * Checking IEI and pointer
   */
  CHECK_PDU_POINTER_AND_LENGTH_ENCODER (buffer, SECURITY_MODE_COMMAND_MINIMUM_LENGTH, len);

  if ((encode_result = encode_nas_security_algorithms (&security_mode_command->selectednassecurityalgorithms, 0, buffer + encoded, len - encoded)) < 0) //Return in case of error
    return encode_result;
  else
    encoded += encode_result;

  *(buffer + encoded) = (encode_u8_nas_key_set_identifier (&security_mode_command->naskeysetidentifier) & 0x0f);
  encoded++;

  if ((encode_result = encode_ue_security_capability (&security_mode_command->replayeduesecuritycapabilities, 0, buffer + encoded, len - encoded)) < 0) //Return in case of error
    return encode_result;
  else
    encoded += encode_result;

  if ((security_mode_command->presencemask & SECURITY_MODE_COMMAND_IMEISV_REQUEST_PRESENT)
      == SECURITY_MODE_COMMAND_IMEISV_REQUEST_PRESENT) {
    if ((encode_result = encode_imeisv_request (&security_mode_command->imeisvrequest, SECURITY_MODE_COMMAND_IMEISV_REQUEST_IEI, buffer + encoded, len - encoded)) < 0)
      // Return in case of error
      return encode_result;
    else
      encoded += encode_result;
  }

  if ((security_mode_command->presencemask & SECURITY_MODE_COMMAND_REPLAYED_NONCEUE_PRESENT)
      == SECURITY_MODE_COMMAND_REPLAYED_NONCEUE_PRESENT) {
    if ((encode_result = encode_nonce (&security_mode_command->replayednonceue, SECURITY_MODE_COMMAND_REPLAYED_NONCEUE_IEI, buffer + encoded, len - encoded)) < 0)
      // Return in case of error
      return encode_result;
    else
      encoded += encode_result;
  }

  if ((security_mode_command->presencemask & SECURITY_MODE_COMMAND_NONCEMME_PRESENT)
      == SECURITY_MODE_COMMAND_NONCEMME_PRESENT) {
    if ((encode_result = encode_nonce (&security_mode_command->noncemme, SECURITY_MODE_COMMAND_NONCEMME_IEI, buffer + encoded, len - encoded)) < 0)
      // Return in case of error
      return encode_result;
    else
      encoded += encode_result;
  }

  return encoded;
}