Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / X2AP / x2ap_generate_messages.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 <stdio.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include "x2ap_generate_messages.h"
26 #include "x2ap_asn_codec.h"
27 #include "e2sim_defs.h"
28
29 int asn1_xer_print = 0;
30 int MAX_XML_BUFFER = 10000;
31
32 void x2ap_fetch_pdu_from_XML(X2AP_PDU_t **pdu, char *filename)
33 {
34
35   uint8_t         buf[MAX_XML_BUFFER];
36
37   asn_dec_rval_t  rval;
38   size_t          size;
39   FILE            *f;
40
41   char XML_path[200];
42   char *work_dir = getenv(WORKDIR_ENV);
43
44   strcpy(XML_path, work_dir);
45   strcat(XML_path, "/src/X2AP/MESSAGES/XML/");
46   strcat(XML_path, filename);
47
48   //printf("Generate X2AP PDU from XML file: %s\n", XML_path);
49
50   f = fopen(XML_path, "r");
51   assert(f);
52   size = fread(buf, 1, sizeof(buf), f);
53   if(size == 0 || size == sizeof(buf))
54   {
55     fprintf(stderr, "%s: Too large input\n", XML_path);
56     exit(1);
57   }
58
59   fclose(f);
60
61   //printf("Decode the XER buffer\n");
62   rval = xer_decode(NULL, &asn_DEF_X2AP_PDU, (void **)pdu, buf, size);
63   assert(rval.code == RC_OK);
64
65 }
66
67 uint32_t x2ap_generate_x2_setup_request(uint8_t **buffer)
68 {
69   X2AP_PDU_t *pdu;
70   pdu = (X2AP_PDU_t *)calloc(1, sizeof(X2AP_PDU_t));
71
72   uint32_t len;
73
74   char *XML_file = "sample_x2_setup_request.xml";
75
76   x2ap_fetch_pdu_from_XML(&pdu, XML_file);
77
78   if(asn1_xer_print)
79     xer_fprint(stdout, &asn_DEF_X2AP_PDU, (void *)pdu);
80
81   if(X2AP_ASN_encode(pdu, buffer, &len) < 0)
82   {
83     fprintf(stderr, "Failed to APER encode X2 Setup Request\n");
84     return -1;
85   }
86
87   return len;
88
89 }
90
91 uint32_t x2ap_generate_x2_setup_response(uint8_t **buffer)
92 {
93   X2AP_PDU_t *pdu;
94   pdu = (X2AP_PDU_t *)calloc(1, sizeof(X2AP_PDU_t));
95
96   uint32_t len;
97
98   char *XML_file = "sample_x2_setup_response.xml";
99
100   x2ap_fetch_pdu_from_XML(&pdu, XML_file);
101
102   if(asn1_xer_print)
103     xer_fprint(stdout, &asn_DEF_X2AP_PDU, (void *)pdu);
104
105   if(X2AP_ASN_encode(pdu, buffer, &len) < 0)
106   {
107     fprintf(stderr, "Failed to APER encode X2 Setup Request\n");
108     return -1;
109   }
110
111   return len;
112
113 }