2 * Copyright 2019 AT&T Intellectual Property
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
29 #include <asn1codec_utils.h>
30 #include <constr_TYPE.h>
31 #include <xer_encoder.h>
34 * Printer for the e2ap pdu.
35 * The string representation of the pdu stored in buf.
38 * pdu - the pdu to print.
39 * buf_size - the size of the storage buffer.
40 * buf - hold the string representation of the pdu.
43 asn1_pdu_printer(E2AP_PDU_t const *pdu, size_t buf_size, char *buf)
49 FILE *stream = open_memstream(&bufloc, &sizeloc);
52 if (asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu)){
53 snprintf(buf, buf_size, "#%s.%s - Failed to print %s, error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, errno);
54 strerror_r(errno, buf+strlen(buf), buf_size - strlen(buf));
57 buf_size = buf_size > sizeloc ? sizeloc: buf_size -1;
58 memcpy(buf, bufloc, buf_size);
69 * XML Printer for the e2ap pdu.
70 * The string representation of the pdu stored in buf.
73 * pdu - the pdu to print.
74 * buf_size - the size of the storage buffer.
75 * buf - hold the string representation of the pdu.
78 asn1_pdu_xer_printer(E2AP_PDU_t const *pdu, size_t buf_size, char *buf)
84 FILE *stream = open_memstream(&bufloc, &sizeloc);
87 if (xer_fprint(stream, &asn_DEF_E2AP_PDU, pdu)){
88 snprintf(buf, buf_size, "#%s.%s - Failed to print %s, error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, errno);
89 strerror_r(errno, buf+strlen(buf), buf_size - strlen(buf));
92 buf_size = buf_size > sizeloc ? sizeloc: buf_size -1;
93 memcpy(buf, bufloc, buf_size);
103 * Unpack the pdu from ASN.1 PER encoding.
106 * pdu - storage for unpacked pdu.
107 * packed_buf_size - size of the encoded data.
108 * packed_buf - storage of the packed pdu
109 * err_buf_size - size of the err_buf which may hold the error string in case of
110 * an error. err_buf - storage for the error string
112 * Return: true in case of success, false in case of failure.
115 per_unpack_pdu(E2AP_PDU_t *pdu, size_t packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf)
117 return unpack_pdu_aux(pdu, packed_buf_size, packed_buf,err_buf_size, err_buf,ATS_ALIGNED_BASIC_PER);
121 unpack_pdu_aux(E2AP_PDU_t *pdu, size_t packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf,enum asn_transfer_syntax syntax)
124 size_t err_msg_size = err_buf_size;
126 //ATS_BASIC_XER ATS_ALIGNED_BASIC_PER, ATS_UNALIGNED_BASIC_PER,ATS_ALIGNED_CANONICAL_PER
128 asn_dec_rval_t rval =
129 asn_decode(0,syntax , &asn_DEF_E2AP_PDU, (void **)&pdu, packed_buf, packed_buf_size);
132 if (asn_check_constraints(&asn_DEF_E2AP_PDU, pdu,err_buf, &err_msg_size)){
133 snprintf(spec, sizeof(spec), "#%s.%s - Constraint check failed: ", __FILE__, __func__);
134 size_t spec_actual_size = strlen(spec);
135 if (spec_actual_size + err_msg_size < err_buf_size){
136 memmove(err_buf + spec_actual_size, err_buf, err_msg_size + 1);
137 memcpy(err_buf, spec, spec_actual_size);
150 snprintf(err_buf, err_buf_size, "#%s.%s - Failed to decode %s (consumed %zu), error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, rval.consumed, errno);
151 strerror_r(errno, err_buf+strlen(err_buf), err_buf_size - strlen(err_buf));
156 * Pack the pdu using ASN.1 PER encoding.
159 * pdu - the pdu to pack.
160 * packed_buf_size - in: size of packed_buf; out: number of chars used.
161 * packed_buf - storage for the packed pdu
162 * err_buf_size - size of the err_buf which may hold the error string in case of
163 * an error. err_buf - storage for the error string
165 * Return: true in case of success, false in case of failure.
168 per_pack_pdu(E2AP_PDU_t *pdu, size_t *packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf)
170 return pack_pdu_aux(pdu, packed_buf_size, packed_buf,err_buf_size, err_buf,ATS_ALIGNED_BASIC_PER);
174 pack_pdu_aux(E2AP_PDU_t *pdu, size_t *packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf,enum asn_transfer_syntax syntax)
177 size_t err_msg_size = err_buf_size;
179 if (asn_check_constraints(&asn_DEF_E2AP_PDU, pdu,err_buf, &err_msg_size)){
180 snprintf(spec, sizeof(spec), "#%s.%s - Constraint check failed: ", __FILE__, __func__);
181 size_t spec_actual_size = strlen(spec);
182 if (spec_actual_size + err_msg_size < err_buf_size){
183 memmove(err_buf + spec_actual_size, err_buf, err_msg_size + 1);
184 memcpy(err_buf, spec, spec_actual_size);
191 asn_encode_to_buffer(0, syntax, &asn_DEF_E2AP_PDU, pdu, packed_buf, *packed_buf_size);
192 if(res.encoded == -1) {
193 snprintf(err_buf, err_buf_size, "#%s.%s - Failed to encode %s, error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, errno);
194 strerror_r(errno, err_buf+strlen(err_buf), err_buf_size - strlen(err_buf));
197 /* Encoded successfully. */
198 if (*packed_buf_size < res.encoded){
199 snprintf(err_buf, err_buf_size, "#%s.%s - Encoded output of %s, is too big:%zu", __FILE__, __func__, asn_DEF_E2AP_PDU.name,res.encoded);
202 *packed_buf_size = res.encoded;
210 * Abort the process on allocation failure.
212 E2AP_PDU_t *new_pdu(size_t sz /*ignored (may be used for a custom allocator)*/)
214 E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
219 void delete_pdu(E2AP_PDU_t *pdu)
221 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);