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