Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / X2AP / x2ap_asn_codec.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "x2ap_asn_codec.h"
21 #include "per_decoder.h"
22 #include "per_encoder.h"
23
24 /* Encode X2AP PDU ASN1 buffer*/
25 int X2AP_ASN_encode(X2AP_PDU_t *pdu, uint8_t **buffer, uint32_t *len)
26 {
27   ssize_t    encoded;
28
29   assert(pdu != NULL);
30   assert(buffer != NULL);
31   assert(len != NULL);
32
33   encoded = aper_encode_to_new_buffer(&asn_DEF_X2AP_PDU, 0, pdu, (void **)buffer);
34
35   if (encoded < 0)
36   {
37     return -1;
38   }
39
40   *len = encoded;
41
42   ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_X2AP_PDU, pdu);
43   return encoded;
44 }
45
46 /* Decode XASN1 buffer to X2AP PDU*/
47 int X2AP_ASN_decode(X2AP_PDU_t *pdu, const uint8_t *const buffer, const int len)
48 {
49   asn_dec_rval_t dec_ret;
50
51   assert(buffer != NULL);
52
53   dec_ret = aper_decode(NULL, &asn_DEF_X2AP_PDU, (void **)&pdu, buffer,
54                     len, 0, 0);
55
56   //xer_fprint(stdout, &asn_DEF_X2AP_PDU, pdu);
57
58   if (dec_ret.code != RC_OK) {
59     fprintf(stderr, "ERROR: Failed to decode pdu\n");
60     return -1;
61   }
62
63   return 0;
64 }