Adding new comments for Oran in all files with licenses
[ric-plt/e2mgr.git] / E2Manager / 3rdparty / asn1codec / src / x2setup_request_wrapper.c
1 /*
2  * Copyright 2019 AT&T Intellectual Property
3  * Copyright 2019 Nokia
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23
24 #include <string.h>
25 #include <errno.h>
26 #undef NDEBUG
27 #include <assert.h>
28 #include <asn_application.h>
29 #include <E2AP-PDU.h>
30 #include <ProcedureCode.h>
31 #include <InitiatingMessage.h>
32 #include <X2SetupRequest.h>
33 #include <GlobalENB-ID.h>
34 #include <PLMN-Identity.h>
35 #include <ENB-ID.h>
36 #include <FDD-Info.h>
37 #include <ServedCells.h>
38 #include <ProtocolIE-ID.h>
39 #include <ProtocolIE-Field.h>
40 #include <x2setup_request_wrapper.h>
41
42 static void assignPLMN_Identity (PLMN_Identity_t *pLMN_Identity, uint8_t const* pLMNId);
43 static void assignENB_ID(GlobalENB_ID_t *globalENB_ID,uint8_t const* eNBId, unsigned int bitqty);
44 static void assignServedCell_Information(ServedCell_Information_t *servedCell_Information,uint8_t const* pLMN_Identity, uint8_t const* eNBId, unsigned int bitqty,uint8_t const *ric_flag);
45
46 /*
47  * Build and pack X2 setup request.
48  * Abort the process on allocation failure.
49  *  packed_buf_size - in: size of packed_buf; out: number of chars used.
50  */
51
52 bool
53 build_pack_x2setup_request(
54                 uint8_t const* pLMN_Identity, uint8_t const* eNBId, unsigned int bitqty /*18, 20, 21, 28*/, uint8_t const *ric_flag,
55                 size_t* packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf
56 )
57 {
58         return build_pack_x2setup_request_aux(
59                         pLMN_Identity, eNBId, bitqty, ric_flag,
60                         packed_buf_size, packed_buf,err_buf_size,err_buf,ATS_ALIGNED_BASIC_PER);
61
62 }
63
64 bool
65 build_pack_x2setup_request_aux(
66                 uint8_t const* pLMN_Identity, uint8_t const* eNBId, unsigned int bitqty /*18, 20, 21, 28*/, uint8_t const *ric_flag,
67                 size_t* packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf,enum asn_transfer_syntax syntax
68 )
69 {
70         bool rc = true;
71         E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
72         InitiatingMessage_t *initiatingMessage = calloc(1, sizeof(InitiatingMessage_t));
73         X2SetupRequest_t *x2SetupRequest;
74
75     assert(pdu != 0);
76     assert(initiatingMessage != 0);
77
78
79     pdu->present = E2AP_PDU_PR_initiatingMessage;
80     pdu->choice.initiatingMessage = initiatingMessage;
81
82     initiatingMessage->procedureCode = ProcedureCode_id_x2Setup;
83     initiatingMessage->criticality = Criticality_reject;
84     initiatingMessage->value.present = InitiatingMessage__value_PR_X2SetupRequest;
85     x2SetupRequest = &initiatingMessage->value.choice.X2SetupRequest;
86
87     X2SetupRequest_IEs_t *globalENB_ID_ie = calloc(1, sizeof(X2SetupRequest_IEs_t));
88     assert(globalENB_ID_ie != 0);
89     ASN_SEQUENCE_ADD(&x2SetupRequest->protocolIEs, globalENB_ID_ie);
90
91     globalENB_ID_ie->id = ProtocolIE_ID_id_GlobalENB_ID;
92     globalENB_ID_ie->criticality = Criticality_reject;
93     globalENB_ID_ie->value.present = X2SetupRequest_IEs__value_PR_GlobalENB_ID;
94         GlobalENB_ID_t *globalENB_ID = &globalENB_ID_ie->value.choice.GlobalENB_ID;
95
96         assignPLMN_Identity(&globalENB_ID->pLMN_Identity, pLMN_Identity);
97         assignENB_ID(globalENB_ID, eNBId, bitqty);
98
99     X2SetupRequest_IEs_t *servedCells_ie = calloc(1, sizeof(X2SetupRequest_IEs_t));
100     assert(servedCells_ie != 0);
101     ASN_SEQUENCE_ADD(&x2SetupRequest->protocolIEs, servedCells_ie);
102
103     servedCells_ie->id = ProtocolIE_ID_id_ServedCells;
104     servedCells_ie->criticality = Criticality_reject;
105     servedCells_ie->value.present = X2SetupRequest_IEs__value_PR_ServedCells;
106
107     ServedCells__Member *servedCells__Member = calloc(1,sizeof(ServedCells__Member));
108     assert(servedCells__Member !=0);
109     ASN_SEQUENCE_ADD(&servedCells_ie->value.choice.ServedCells, servedCells__Member);
110
111     assignServedCell_Information(&servedCells__Member->servedCellInfo, pLMN_Identity,eNBId, bitqty,ric_flag);
112
113     rc = pack_pdu_aux(pdu, packed_buf_size, packed_buf,err_buf_size, err_buf,syntax);
114
115     ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
116     return rc;
117 }
118
119 static void assignPLMN_Identity (PLMN_Identity_t *pLMN_Identity, uint8_t const* pLMNId)
120 {
121         pLMN_Identity->size = pLMN_Identity_size;
122         pLMN_Identity->buf = calloc(1,pLMN_Identity->size);
123         assert(pLMN_Identity->buf != 0);
124         memcpy(pLMN_Identity->buf, pLMNId, pLMN_Identity->size);
125 }
126
127 /*
128  * Calculate and assign the value of ENB_ID.
129  * Abort the process on allocation failure.
130  */
131 static void assignENB_ID(GlobalENB_ID_t *globalENB_ID,uint8_t const* eNBId, unsigned int bitqty)
132 {
133         size_t size_in_bytes = (bitqty / 8) + ((bitqty % 8) > 0);
134         int unused_bits = 8 - (bitqty % 8);
135         uint8_t *tbuf;
136         switch (bitqty){
137         case shortMacro_eNB_ID_size:
138                 globalENB_ID->eNB_ID.present = ENB_ID_PR_short_Macro_eNB_ID;
139                 globalENB_ID->eNB_ID.choice.short_Macro_eNB_ID.size = size_in_bytes;
140                 globalENB_ID->eNB_ID.choice.short_Macro_eNB_ID.bits_unused = unused_bits;
141                 tbuf = globalENB_ID->eNB_ID.choice.short_Macro_eNB_ID.buf = calloc(1, size_in_bytes);
142                 assert(globalENB_ID->eNB_ID.choice.short_Macro_eNB_ID.buf  != 0);
143                 memcpy(globalENB_ID->eNB_ID.choice.short_Macro_eNB_ID.buf,eNBId, size_in_bytes) ;
144                 tbuf[size_in_bytes - 1] <<= unused_bits;
145                 break;
146         case macro_eNB_ID_size:
147                 globalENB_ID->eNB_ID.present =ENB_ID_PR_macro_eNB_ID;
148                 globalENB_ID->eNB_ID.choice.macro_eNB_ID.size = size_in_bytes;
149                 globalENB_ID->eNB_ID.choice.macro_eNB_ID.bits_unused = unused_bits;
150                 tbuf = globalENB_ID->eNB_ID.choice.macro_eNB_ID.buf = calloc(1, size_in_bytes);
151                 assert(globalENB_ID->eNB_ID.choice.macro_eNB_ID.buf != 0);
152                 memcpy(globalENB_ID->eNB_ID.choice.macro_eNB_ID.buf,eNBId,size_in_bytes);
153                 tbuf[size_in_bytes - 1] <<= unused_bits;
154                 break;
155         case longMacro_eNB_ID_size:
156                 globalENB_ID->eNB_ID.present =ENB_ID_PR_long_Macro_eNB_ID;
157                 globalENB_ID->eNB_ID.choice.long_Macro_eNB_ID.size = size_in_bytes;
158                 globalENB_ID->eNB_ID.choice.long_Macro_eNB_ID.bits_unused = unused_bits;
159                 tbuf = globalENB_ID->eNB_ID.choice.long_Macro_eNB_ID.buf = calloc(1, size_in_bytes);
160                 assert(globalENB_ID->eNB_ID.choice.long_Macro_eNB_ID.buf != 0);
161                 memcpy(globalENB_ID->eNB_ID.choice.long_Macro_eNB_ID.buf,eNBId,size_in_bytes);
162                 tbuf[size_in_bytes - 1] <<= unused_bits;
163                 break;
164         case home_eNB_ID_size:
165                 globalENB_ID->eNB_ID.present = ENB_ID_PR_home_eNB_ID;
166                 globalENB_ID->eNB_ID.choice.home_eNB_ID.size = size_in_bytes;
167                 globalENB_ID->eNB_ID.choice.home_eNB_ID.bits_unused =unused_bits;
168                 tbuf = globalENB_ID->eNB_ID.choice.home_eNB_ID.buf = calloc(1,size_in_bytes);
169                 assert(globalENB_ID->eNB_ID.choice.home_eNB_ID.buf != 0);
170                 memcpy(globalENB_ID->eNB_ID.choice.home_eNB_ID.buf,eNBId,size_in_bytes);
171                 tbuf[size_in_bytes - 1] <<= unused_bits;
172                 break;
173         default:
174                 break;
175         }
176
177 }
178
179 /*
180  * Calculate and assign the value of ServedCell_Information.
181  * Abort the process on allocation failure.
182  */
183 static void assignServedCell_Information(
184                 ServedCell_Information_t *servedCell_Information,
185                 uint8_t const* pLMN_Identity,
186                 uint8_t const* eNBId,
187                 unsigned int bitqty,
188                 uint8_t const *ric_flag)
189 {
190         size_t size_in_bytes =  (eUTRANcellIdentifier_size / 8) + ((eUTRANcellIdentifier_size % 8) > 0);
191         int unused_bits = 8 - (eUTRANcellIdentifier_size % 8);
192         size_t bitqty_size_in_bytes = (bitqty / 8) + ((bitqty % 8) > 0);
193         int bitqty_unused_bits = 8 - (bitqty % 8);
194
195         servedCell_Information->pCI = 503;
196         assignPLMN_Identity(&servedCell_Information->cellId.pLMN_Identity, pLMN_Identity);
197
198         servedCell_Information->cellId.eUTRANcellIdentifier.size = size_in_bytes;
199         servedCell_Information->cellId.eUTRANcellIdentifier.bits_unused = unused_bits;
200         servedCell_Information->cellId.eUTRANcellIdentifier.buf = calloc(1,servedCell_Information->cellId.eUTRANcellIdentifier.size);
201         assert(servedCell_Information->cellId.eUTRANcellIdentifier.buf != 0);
202         memcpy(servedCell_Information->cellId.eUTRANcellIdentifier.buf, eNBId, bitqty_size_in_bytes);
203         if (bitqty < eUTRANcellIdentifier_size) {
204                 servedCell_Information->cellId.eUTRANcellIdentifier.buf[bitqty_size_in_bytes - 1] <<= bitqty_unused_bits;
205         } else {
206                 servedCell_Information->cellId.eUTRANcellIdentifier.buf[size_in_bytes - 1] <<= unused_bits;
207         }
208
209         servedCell_Information->tAC.size = 2;
210         servedCell_Information->tAC.buf = calloc(1,servedCell_Information->tAC.size);
211         assert(servedCell_Information->tAC.buf != 0);
212
213
214         PLMN_Identity_t *broadcastPLMN_Identity = calloc(1, sizeof(PLMN_Identity_t));
215         assert(broadcastPLMN_Identity != 0);
216         ASN_SEQUENCE_ADD(&servedCell_Information->broadcastPLMNs, broadcastPLMN_Identity);
217
218         assignPLMN_Identity(broadcastPLMN_Identity, pLMN_Identity); //ric_flag: disabled because a real eNB rejects the message
219
220         servedCell_Information->eUTRA_Mode_Info.present= EUTRA_Mode_Info_PR_fDD;
221         servedCell_Information->eUTRA_Mode_Info.choice.fDD = calloc(1, sizeof(FDD_Info_t));
222         assert(servedCell_Information->eUTRA_Mode_Info.choice.fDD != 0);
223         servedCell_Information->eUTRA_Mode_Info.choice.fDD->uL_EARFCN = 0;
224         servedCell_Information->eUTRA_Mode_Info.choice.fDD->dL_EARFCN = 0;
225         servedCell_Information->eUTRA_Mode_Info.choice.fDD->uL_Transmission_Bandwidth = Transmission_Bandwidth_bw6;
226         servedCell_Information->eUTRA_Mode_Info.choice.fDD->dL_Transmission_Bandwidth = Transmission_Bandwidth_bw15;
227 }
228
229 /* Build and pack X2 setup request.
230  * Abort the process on allocation failure.
231  * packed_buf_size - in: size of packed_buf; out: number of chars used.
232  */
233 bool
234 build_pack_endc_x2setup_request(
235                 uint8_t const* pLMN_Identity, uint8_t const* eNBId, unsigned int bitqty /*18, 20, 21, 28*/, uint8_t const *ric_flag,
236                 size_t* packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf
237 )
238 {
239         return build_pack_endc_x2setup_request_aux(
240                         pLMN_Identity, eNBId, bitqty, ric_flag,
241                         packed_buf_size, packed_buf,err_buf_size,  err_buf,ATS_ALIGNED_BASIC_PER
242         );
243 }
244
245 bool
246 build_pack_endc_x2setup_request_aux(
247                 uint8_t const* pLMN_Identity, uint8_t const* eNBId, unsigned int bitqty /*18, 20, 21, 28*/, uint8_t const *ric_flag,
248                 size_t* packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf,enum asn_transfer_syntax syntax
249 )
250 {
251         bool rc = true;
252         E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
253         InitiatingMessage_t *initiatingMessage = calloc(1, sizeof(InitiatingMessage_t));
254         ENDCX2SetupRequest_t *endcX2SetupRequest;
255
256     assert(pdu != 0);
257     assert(initiatingMessage != 0);
258
259     pdu->present = E2AP_PDU_PR_initiatingMessage;
260     pdu->choice.initiatingMessage = initiatingMessage;
261
262     initiatingMessage->procedureCode = ProcedureCode_id_endcX2Setup;
263     initiatingMessage->criticality = Criticality_reject;
264     initiatingMessage->value.present = InitiatingMessage__value_PR_ENDCX2SetupRequest;
265     endcX2SetupRequest = &initiatingMessage->value.choice.ENDCX2SetupRequest;
266     ENDCX2SetupRequest_IEs_t *endcX2SetupRequest_IEs = calloc(1, sizeof(ENDCX2SetupRequest_IEs_t));
267     assert(endcX2SetupRequest_IEs != 0);
268         ASN_SEQUENCE_ADD(&endcX2SetupRequest->protocolIEs, endcX2SetupRequest_IEs);
269         endcX2SetupRequest_IEs->id = ProtocolIE_ID_id_InitiatingNodeType_EndcX2Setup;
270         endcX2SetupRequest_IEs->criticality = Criticality_reject;
271         endcX2SetupRequest_IEs->value.present = ENDCX2SetupRequest_IEs__value_PR_InitiatingNodeType_EndcX2Setup;
272         endcX2SetupRequest_IEs->value.choice.InitiatingNodeType_EndcX2Setup.present = InitiatingNodeType_EndcX2Setup_PR_init_eNB;
273
274         ProtocolIE_Container_119P85_t *enb_ENDCX2SetupReqIE_Container = calloc(1, sizeof(ProtocolIE_Container_119P85_t));
275         assert(enb_ENDCX2SetupReqIE_Container != 0);
276         endcX2SetupRequest_IEs->value.choice.InitiatingNodeType_EndcX2Setup.choice.init_eNB = (struct ProtocolIE_Container*)enb_ENDCX2SetupReqIE_Container;
277         ENB_ENDCX2SetupReqIEs_t *globalENB_ID_ie = calloc(1, sizeof(ENB_ENDCX2SetupReqIEs_t));
278         assert(globalENB_ID_ie != 0);
279         ASN_SEQUENCE_ADD(enb_ENDCX2SetupReqIE_Container,globalENB_ID_ie);
280         globalENB_ID_ie->id = ProtocolIE_ID_id_GlobalENB_ID;
281         globalENB_ID_ie->criticality = Criticality_reject;
282         globalENB_ID_ie->value.present = ENB_ENDCX2SetupReqIEs__value_PR_GlobalENB_ID;
283
284         GlobalENB_ID_t *globalENB_ID = &globalENB_ID_ie->value.choice.GlobalENB_ID;
285         assignPLMN_Identity(&globalENB_ID->pLMN_Identity, pLMN_Identity);
286         assignENB_ID(globalENB_ID, eNBId, bitqty);
287
288
289         ENB_ENDCX2SetupReqIEs_t *ServedEUTRAcellsENDCX2ManagementList_ie = calloc(1, sizeof(ENB_ENDCX2SetupReqIEs_t));
290         assert(ServedEUTRAcellsENDCX2ManagementList_ie != 0);
291     ASN_SEQUENCE_ADD(enb_ENDCX2SetupReqIE_Container, ServedEUTRAcellsENDCX2ManagementList_ie);
292
293     ServedEUTRAcellsENDCX2ManagementList_ie->id = ProtocolIE_ID_id_ServedEUTRAcellsENDCX2ManagementList;
294         ServedEUTRAcellsENDCX2ManagementList_ie->criticality = Criticality_reject;
295         ServedEUTRAcellsENDCX2ManagementList_ie->value.present = ENB_ENDCX2SetupReqIEs__value_PR_ServedEUTRAcellsENDCX2ManagementList;
296
297
298         ServedEUTRAcellsENDCX2ManagementList__Member *servedEUTRAcellsENDCX2ManagementList__Member = calloc(1, sizeof(ServedEUTRAcellsENDCX2ManagementList__Member));
299         assert(servedEUTRAcellsENDCX2ManagementList__Member != 0);
300         ASN_SEQUENCE_ADD(&ServedEUTRAcellsENDCX2ManagementList_ie->value.choice.ServedEUTRAcellsENDCX2ManagementList, servedEUTRAcellsENDCX2ManagementList__Member);
301
302         assignServedCell_Information(&servedEUTRAcellsENDCX2ManagementList__Member->servedEUTRACellInfo, pLMN_Identity, eNBId, bitqty,ric_flag);
303
304     rc = pack_pdu_aux(pdu, packed_buf_size, packed_buf,err_buf_size, err_buf, syntax);
305
306     ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
307     return rc;
308 }