/*
 * Generated by asn1c-0.9.29 (http://lionet.info/asn1c)
 * From ASN.1 module "X2AP-IEs"
 * 	found in "Spec/e2ap-v031ex.asn"
 * 	`asn1c -fcompound-names -fno-include-deps -findirect-choice -gen-PER`
 */

#include "ProtectedFootprintTimePattern.h"

#include "ProtocolExtensionContainer.h"
static int
memb_protectedFootprintTimePeriodicity_constraint_1(const asn_TYPE_descriptor_t *td, const void *sptr,
			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
	long value;
	
	if(!sptr) {
		ASN__CTFAIL(app_key, td, sptr,
			"%s: value not given (%s:%d)",
			td->name, __FILE__, __LINE__);
		return -1;
	}
	
	value = *(const long *)sptr;
	
	if((value >= 1 && value <= 320)) {
		/* Constraint check succeeded */
		return 0;
	} else {
		ASN__CTFAIL(app_key, td, sptr,
			"%s: constraint failed (%s:%d)",
			td->name, __FILE__, __LINE__);
		return -1;
	}
}

static int
memb_protectedFootprintStartTime_constraint_1(const asn_TYPE_descriptor_t *td, const void *sptr,
			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
	long value;
	
	if(!sptr) {
		ASN__CTFAIL(app_key, td, sptr,
			"%s: value not given (%s:%d)",
			td->name, __FILE__, __LINE__);
		return -1;
	}
	
	value = *(const long *)sptr;
	
	if((value >= 1 && value <= 20)) {
		/* Constraint check succeeded */
		return 0;
	} else {
		ASN__CTFAIL(app_key, td, sptr,
			"%s: constraint failed (%s:%d)",
			td->name, __FILE__, __LINE__);
		return -1;
	}
}

static asn_oer_constraints_t asn_OER_memb_protectedFootprintTimePeriodicity_constr_2 CC_NOTUSED = {
	{ 0, 0 },
	-1};
static asn_per_constraints_t asn_PER_memb_protectedFootprintTimePeriodicity_constr_2 CC_NOTUSED = {
	{ APC_CONSTRAINED | APC_EXTENSIBLE,  9,  9,  1,  320 }	/* (1..320,...) */,
	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
	0, 0	/* No PER value map */
};
static asn_oer_constraints_t asn_OER_memb_protectedFootprintStartTime_constr_3 CC_NOTUSED = {
	{ 0, 0 },
	-1};
static asn_per_constraints_t asn_PER_memb_protectedFootprintStartTime_constr_3 CC_NOTUSED = {
	{ APC_CONSTRAINED | APC_EXTENSIBLE,  5,  5,  1,  20 }	/* (1..20,...) */,
	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
	0, 0	/* No PER value map */
};
asn_TYPE_member_t asn_MBR_ProtectedFootprintTimePattern_1[] = {
	{ ATF_NOFLAGS, 0, offsetof(struct ProtectedFootprintTimePattern, protectedFootprintTimePeriodicity),
		(ASN_TAG_CLASS_CONTEXT | (0 << 2)),
		-1,	/* IMPLICIT tag at current level */
		&asn_DEF_NativeInteger,
		0,
		{ &asn_OER_memb_protectedFootprintTimePeriodicity_constr_2, &asn_PER_memb_protectedFootprintTimePeriodicity_constr_2,  memb_protectedFootprintTimePeriodicity_constraint_1 },
		0, 0, /* No default value */
		"protectedFootprintTimePeriodicity"
		},
	{ ATF_NOFLAGS, 0, offsetof(struct ProtectedFootprintTimePattern, protectedFootprintStartTime),
		(ASN_TAG_CLASS_CONTEXT | (1 << 2)),
		-1,	/* IMPLICIT tag at current level */
		&asn_DEF_NativeInteger,
		0,
		{ &asn_OER_memb_protectedFootprintStartTime_constr_3, &asn_PER_memb_protectedFootprintStartTime_constr_3,  memb_protectedFootprintStartTime_constraint_1 },
		0, 0, /* No default value */
		"protectedFootprintStartTime"
		},
	{ ATF_POINTER, 1, offsetof(struct ProtectedFootprintTimePattern, iE_Extensions),
		(ASN_TAG_CLASS_CONTEXT | (2 << 2)),
		-1,	/* IMPLICIT tag at current level */
		&asn_DEF_ProtocolExtensionContainer_9566P181,
		0,
		{ 0, 0, 0 },
		0, 0, /* No default value */
		"iE-Extensions"
		},
};
static const int asn_MAP_ProtectedFootprintTimePattern_oms_1[] = { 2 };
static const ber_tlv_tag_t asn_DEF_ProtectedFootprintTimePattern_tags_1[] = {
	(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_ProtectedFootprintTimePattern_tag2el_1[] = {
    { (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* protectedFootprintTimePeriodicity */
    { (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 }, /* protectedFootprintStartTime */
    { (ASN_TAG_CLASS_CONTEXT | (2 << 2)), 2, 0, 0 } /* iE-Extensions */
};
asn_SEQUENCE_specifics_t asn_SPC_ProtectedFootprintTimePattern_specs_1 = {
	sizeof(struct ProtectedFootprintTimePattern),
	offsetof(struct ProtectedFootprintTimePattern, _asn_ctx),
	asn_MAP_ProtectedFootprintTimePattern_tag2el_1,
	3,	/* Count of tags in the map */
	asn_MAP_ProtectedFootprintTimePattern_oms_1,	/* Optional members */
	1, 0,	/* Root/Additions */
	3,	/* First extension addition */
};
asn_TYPE_descriptor_t asn_DEF_ProtectedFootprintTimePattern = {
	"ProtectedFootprintTimePattern",
	"ProtectedFootprintTimePattern",
	&asn_OP_SEQUENCE,
	asn_DEF_ProtectedFootprintTimePattern_tags_1,
	sizeof(asn_DEF_ProtectedFootprintTimePattern_tags_1)
		/sizeof(asn_DEF_ProtectedFootprintTimePattern_tags_1[0]), /* 1 */
	asn_DEF_ProtectedFootprintTimePattern_tags_1,	/* Same as above */
	sizeof(asn_DEF_ProtectedFootprintTimePattern_tags_1)
		/sizeof(asn_DEF_ProtectedFootprintTimePattern_tags_1[0]), /* 1 */
	{ 0, 0, SEQUENCE_constraint },
	asn_MBR_ProtectedFootprintTimePattern_1,
	3,	/* Elements count */
	&asn_SPC_ProtectedFootprintTimePattern_specs_1	/* Additional specs */
};

