add example of en-gnb setup
[ric-plt/e2.git] / RIC-E2-TERMINATION / TEST / T1 / E2Builder.h
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 // Created by adi ENZEL on 12/10/19.
20 //
21
22 #ifndef E2_E2BUILDER_H
23 #define E2_E2BUILDER_H
24
25 #include <cstring>
26 #include <cstdio>
27 #include <cerrno>
28 #include <cstdlib>
29 #include <sys/types.h>
30 #include <error.h>
31 #include <algorithm>
32 #include <3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h>
33 #include <3rdparty/oranE2SM/RIC-InsertStyle-List.h>
34 #include <3rdparty/oranE2SM/RANparameterDef-Item.h>
35 #include <3rdparty/oranE2/GlobalE2node-en-gNB-ID.h>
36
37
38 //#include <mdclog/mdclog.h>
39
40
41 #include "oranE2/E2AP-PDU.h"
42 #include "oranE2/InitiatingMessage.h"
43 #include "oranE2/SuccessfulOutcome.h"
44 #include "oranE2/UnsuccessfulOutcome.h"
45
46 #include "oranE2/ProtocolIE-Field.h"
47 #include "oranE2/ENB-ID.h"
48 #include "oranE2/GlobalENB-ID.h"
49 #include "oranE2/GlobalE2node-gNB-ID.h"
50 #include "oranE2/constr_TYPE.h"
51 #include "oranE2/asn_constant.h"
52
53 using namespace std;
54
55 #define printEntry(type, function);  fprintf(stdout, "start Test %s , %s", type, function);
56
57
58 static void checkAndPrint(asn_TYPE_descriptor_t *typeDescriptor, void *data, char *dataType, const char *function) {
59     char errbuf[128]; /* Buffer for error message */
60     size_t errlen = sizeof(errbuf); /* Size of the buffer */
61     if (asn_check_constraints(typeDescriptor, data, errbuf, &errlen) != 0) {
62         fprintf(stderr, "%s Constraint validation failed: %s", dataType, errbuf);
63     }
64     fprintf(stdout, "%s successes function %s", dataType, function);
65 }
66
67 void createPLMN_IDByMCCandMNC(PLMN_Identity_t *plmnId, int mcc, int mnc) {
68
69     //printEntry("PLMN_Identity_t", __func__)
70
71     ASN_STRUCT_RESET(asn_DEF_PLMN_Identity, plmnId);
72     plmnId->size = 3;
73     plmnId->buf = (uint8_t *) calloc(1, 3);
74     volatile auto mcc1 = (unsigned int) (mcc / 100);
75     volatile auto mcc2 = (unsigned int) (mcc / 10 % 10);
76     volatile auto mcc3 = (unsigned int) (mcc % 10);
77     plmnId->buf[0] = mcc2 << 4 | mcc1;
78
79     volatile auto mnc1 = (unsigned int)0;
80     volatile auto mnc2 = (unsigned int)0;
81     volatile auto mnc3 = (unsigned int)0;
82
83     if (mnc >= 100) {
84         mnc1 = (unsigned int) (mnc / 100);
85         mnc2 = (unsigned int) (mnc / 10 % 10);
86         mnc3 = (unsigned int) (mnc % 10);
87     } else {
88         mnc1 = (unsigned int) (mnc / 10);
89         mnc2 = (unsigned int) (mnc % 10);
90         mnc3 = 15;
91     }
92     plmnId->buf[1] = mcc3 << 4 | mnc3 ;
93     plmnId->buf[2] = mnc2 << 4 | mnc1 ;
94
95     //checkAndPrint(&asn_DEF_PLMN_Identity, plmnId, (char *) "PLMN_Identity_t", __func__);
96
97 }
98
99
100 PLMN_Identity_t *createPLMN_ID(const unsigned char *data) {
101     printEntry("PLMN_Identity_t", __func__)
102     auto *plmnId = (PLMN_Identity_t *) calloc(1, sizeof(PLMN_Identity_t));
103     ASN_STRUCT_RESET(asn_DEF_PLMN_Identity, plmnId);
104     plmnId->size = 3;
105     plmnId->buf = (uint8_t *) calloc(1, 3);
106     memcpy(plmnId->buf, data, 3);
107
108     checkAndPrint(&asn_DEF_PLMN_Identity, plmnId, (char *) "PLMN_Identity_t", __func__);
109
110     return plmnId;
111 }
112
113 BIT_STRING_t *createBIT_STRING(int size, int unusedBits, uint8_t *data) {
114     printEntry("BIT_STRING_t", __func__)
115     auto *bitString = (BIT_STRING_t *) calloc(1, sizeof(BIT_STRING_t));
116     ASN_STRUCT_RESET(asn_DEF_BIT_STRING, bitString);
117     bitString->size = size;
118     bitString->bits_unused = unusedBits;
119     bitString->buf = (uint8_t *) calloc(1, size);
120     // set bits to zero
121     data[bitString->size - 1] = ((unsigned) (data[bitString->size - 1] >>
122                                                                        (unsigned) bitString->bits_unused)
123             << (unsigned) bitString->bits_unused);
124     memcpy(bitString->buf, data, size);
125
126     checkAndPrint(&asn_DEF_BIT_STRING, bitString, (char *) "BIT_STRING_t", __func__);
127
128     return bitString;
129 }
130
131
132 OCTET_STRING_t *createOCTET_STRING(const unsigned char *data, int size) {
133     printEntry("OCTET_STRING_t", __func__)
134     auto *octs = (PLMN_Identity_t *) calloc(1, sizeof(PLMN_Identity_t));
135     ASN_STRUCT_RESET(asn_DEF_OCTET_STRING, octs);
136     octs->size = size;
137     octs->buf = (uint8_t *) calloc(1, size);
138     memcpy(octs->buf, data, size);
139
140     checkAndPrint(&asn_DEF_OCTET_STRING, octs, (char *) "OCTET_STRING_t", __func__);
141     return octs;
142 }
143
144
145 ENB_ID_t *createENB_ID(ENB_ID_PR enbType, unsigned char *data) {
146     printEntry("ENB_ID_t", __func__)
147     auto *enb = (ENB_ID_t *) calloc(1, sizeof(ENB_ID_t));
148     ASN_STRUCT_RESET(asn_DEF_ENB_ID, enb);
149
150     enb->present = enbType;
151
152     switch (enbType) {
153         case ENB_ID_PR_macro_eNB_ID: { // 20 bit 3 bytes
154             enb->choice.macro_eNB_ID.size = 3;
155             enb->choice.macro_eNB_ID.bits_unused = 4;
156
157             enb->present = ENB_ID_PR_macro_eNB_ID;
158
159             enb->choice.macro_eNB_ID.buf = (uint8_t *) calloc(1, enb->choice.macro_eNB_ID.size);
160             data[enb->choice.macro_eNB_ID.size - 1] = ((unsigned) (data[enb->choice.macro_eNB_ID.size - 1] >>
161                                                                                                            (unsigned) enb->choice.macro_eNB_ID.bits_unused)
162                     << (unsigned) enb->choice.macro_eNB_ID.bits_unused);
163             memcpy(enb->choice.macro_eNB_ID.buf, data, enb->choice.macro_eNB_ID.size);
164
165             break;
166         }
167         case ENB_ID_PR_home_eNB_ID: { // 28 bit 4 bytes
168             enb->choice.home_eNB_ID.size = 4;
169             enb->choice.home_eNB_ID.bits_unused = 4;
170             enb->present = ENB_ID_PR_home_eNB_ID;
171
172             enb->choice.home_eNB_ID.buf = (uint8_t *) calloc(1, enb->choice.home_eNB_ID.size);
173             data[enb->choice.home_eNB_ID.size - 1] = ((unsigned) (data[enb->choice.home_eNB_ID.size - 1] >>
174                                                                                                          (unsigned) enb->choice.home_eNB_ID.bits_unused)
175                     << (unsigned) enb->choice.home_eNB_ID.bits_unused);
176             memcpy(enb->choice.home_eNB_ID.buf, data, enb->choice.home_eNB_ID.size);
177             break;
178         }
179         case ENB_ID_PR_short_Macro_eNB_ID: { // 18 bit - 3 bytes
180             enb->choice.short_Macro_eNB_ID.size = 3;
181             enb->choice.short_Macro_eNB_ID.bits_unused = 6;
182             enb->present = ENB_ID_PR_short_Macro_eNB_ID;
183
184             enb->choice.short_Macro_eNB_ID.buf = (uint8_t *) calloc(1, enb->choice.short_Macro_eNB_ID.size);
185             data[enb->choice.short_Macro_eNB_ID.size - 1] = (
186                     (unsigned) (data[enb->choice.short_Macro_eNB_ID.size - 1] >>
187                                                                               (unsigned) enb->choice.short_Macro_eNB_ID.bits_unused)
188                             << (unsigned) enb->choice.short_Macro_eNB_ID.bits_unused);
189             memcpy(enb->choice.short_Macro_eNB_ID.buf, data, enb->choice.short_Macro_eNB_ID.size);
190             break;
191         }
192         case ENB_ID_PR_long_Macro_eNB_ID: { // 21
193             enb->choice.long_Macro_eNB_ID.size = 3;
194             enb->choice.long_Macro_eNB_ID.bits_unused = 3;
195             enb->present = ENB_ID_PR_long_Macro_eNB_ID;
196
197             enb->choice.long_Macro_eNB_ID.buf = (uint8_t *) calloc(1, enb->choice.long_Macro_eNB_ID.size);
198             data[enb->choice.long_Macro_eNB_ID.size - 1] =
199                     ((unsigned) (data[enb->choice.long_Macro_eNB_ID.size - 1] >> (unsigned) enb->choice.long_Macro_eNB_ID.bits_unused)
200                             << (unsigned) enb->choice.long_Macro_eNB_ID.bits_unused);
201             memcpy(enb->choice.long_Macro_eNB_ID.buf, data, enb->choice.long_Macro_eNB_ID.size);
202             break;
203         }
204         default:
205             free(enb);
206             return nullptr;
207     }
208
209     checkAndPrint(&asn_DEF_ENB_ID, enb, (char *) "ENB_ID_t", __func__);
210     return enb;
211 }
212
213 GlobalENB_ID_t *createGlobalENB_ID(PLMN_Identity_t *plmnIdentity, ENB_ID_t *enbId) {
214     printEntry("GlobalENB_ID_t", __func__)
215     auto *genbId = (GlobalENB_ID_t *) calloc(1, sizeof(GlobalENB_ID_t));
216     ASN_STRUCT_RESET(asn_DEF_GlobalENB_ID, genbId);
217     memcpy(&genbId->pLMN_Identity, plmnIdentity, sizeof(PLMN_Identity_t));
218     memcpy(&genbId->eNB_ID, enbId, sizeof(ENB_ID_t));
219
220     checkAndPrint(&asn_DEF_GlobalENB_ID, genbId, (char *) "GlobalENB_ID_t", __func__);
221     return genbId;
222 }
223
224
225 //static void buildInitiatingMessagePDU(E2AP_PDU_t &pdu, InitiatingMessage_t *initMsg) {
226 //    pdu.present = E2AP_PDU_PR_initiatingMessage;
227 //    pdu.choice.initiatingMessage = initMsg;
228 //}
229
230 template<typename T>
231 static void buildInitMsg(InitiatingMessage_t &initMsg,
232                          InitiatingMessage__value_PR present,
233                          ProcedureCode_t procedureCode,
234                          Criticality_t criticality,
235                          T *value) {
236     initMsg.value.present = present;
237     initMsg.procedureCode = procedureCode;
238     initMsg.criticality = criticality;
239
240     switch (present) {
241         case InitiatingMessage__value_PR_RICsubscriptionRequest: {
242             memcpy(&initMsg.value.choice.RICsubscriptionRequest, value, sizeof(*value));
243             break;
244         }
245         case InitiatingMessage__value_PR_RICsubscriptionDeleteRequest: {
246             memcpy(&initMsg.value.choice.RICsubscriptionDeleteRequest, value, sizeof(*value));
247             break;
248         }
249         case InitiatingMessage__value_PR_RICserviceUpdate: {
250             memcpy(&initMsg.value.choice.RICserviceUpdate, value, sizeof(*value));
251             break;
252         }
253         case InitiatingMessage__value_PR_RICcontrolRequest: {
254             memcpy(&initMsg.value.choice.RICcontrolRequest, value, sizeof(*value));
255             break;
256         }
257         case InitiatingMessage__value_PR_RICindication: {
258             memcpy(&initMsg.value.choice.RICindication, value, sizeof(*value));
259             break;
260         }
261         case InitiatingMessage__value_PR_RICserviceQuery: {
262             memcpy(&initMsg.value.choice.RICserviceQuery, value, sizeof(*value));
263             break;
264         }
265         case InitiatingMessage__value_PR_NOTHING:
266         default : {
267             break;
268         }
269     }
270 }
271
272 //static void buildSuccsesfulMessagePDU(E2AP_PDU_t &pdu, SuccessfulOutcome_t *succMsg) {
273 //    pdu.present = E2AP_PDU_PR_successfulOutcome;
274 //    pdu.choice.successfulOutcome = succMsg;
275 //}
276
277 template<typename T>
278 static void buildSuccMsg(SuccessfulOutcome_t &succMsg,
279                          SuccessfulOutcome__value_PR present,
280                          ProcedureCode_t procedureCode,
281                          Criticality_t criticality,
282                          T *value) {
283     succMsg.value.present = present;
284     succMsg.procedureCode = procedureCode;
285     succMsg.criticality = criticality;
286
287     switch (present) {
288         case SuccessfulOutcome__value_PR_RICsubscriptionResponse: {
289             memcpy(&succMsg.value.choice.RICsubscriptionResponse, value, sizeof(*value));
290             break;
291         }
292         case SuccessfulOutcome__value_PR_RICsubscriptionDeleteResponse: {
293             memcpy(&succMsg.value.choice.RICsubscriptionDeleteResponse, value, sizeof(*value));
294             break;
295         }
296         case SuccessfulOutcome__value_PR_RICserviceUpdateAcknowledge: {
297             memcpy(&succMsg.value.choice.RICserviceUpdateAcknowledge, value, sizeof(*value));
298             break;
299         }
300         case SuccessfulOutcome__value_PR_RICcontrolAcknowledge: {
301             memcpy(&succMsg.value.choice.RICcontrolAcknowledge, value, sizeof(*value));
302             break;
303         }
304         case SuccessfulOutcome__value_PR_ResetResponse: {
305             memcpy(&succMsg.value.choice.ResetResponse, value, sizeof(*value));
306             break;
307         }
308         case SuccessfulOutcome__value_PR_NOTHING:
309         default:
310             break;
311     }
312 }
313
314
315 //static void buildUnSucssesfullMessagePDU(E2AP_PDU_t &pdu, UnsuccessfulOutcome_t *unSuccMsg) {
316 //    pdu.present = E2AP_PDU_PR_unsuccessfulOutcome;
317 //    pdu.choice.unsuccessfulOutcome = unSuccMsg;
318 //}
319
320 template<typename T>
321 static void buildUnSuccMsg(UnsuccessfulOutcome_t &unSuccMsg,
322                            UnsuccessfulOutcome__value_PR present,
323                            ProcedureCode_t procedureCode,
324                            Criticality_t criticality,
325                            T *value) {
326     unSuccMsg.value.present = present;
327     unSuccMsg.procedureCode = procedureCode;
328     unSuccMsg.criticality = criticality;
329
330     switch (present) {
331         case UnsuccessfulOutcome__value_PR_RICsubscriptionFailure: {
332             memcpy(&unSuccMsg.value.choice.RICsubscriptionFailure, value, sizeof(*value));
333             break;
334         }
335         case UnsuccessfulOutcome__value_PR_RICsubscriptionDeleteFailure: {
336             memcpy(&unSuccMsg.value.choice.RICsubscriptionDeleteFailure, value, sizeof(*value));
337             break;
338         }
339         case UnsuccessfulOutcome__value_PR_RICserviceUpdateFailure: {
340             memcpy(&unSuccMsg.value.choice.RICserviceUpdateFailure, value, sizeof(*value));
341             break;
342         }
343         case UnsuccessfulOutcome__value_PR_RICcontrolFailure: {
344             memcpy(&unSuccMsg.value.choice.RICcontrolFailure, value, sizeof(*value));
345             break;
346
347         }
348         case UnsuccessfulOutcome__value_PR_NOTHING:
349         default:
350             break;
351     }
352 }
353
354
355 //static void createPLMN_ID(PLMN_Identity_t &plmnId, const unsigned char *data) {
356 //    //printEntry("PLMN_Identity_t", __func__)
357 //    //PLMN_Identity_t *plmnId = calloc(1, sizeof(PLMN_Identity_t));
358 //    ASN_STRUCT_RESET(asn_DEF_PLMN_Identity, &plmnId);
359 //    plmnId.size = 3;//    uint64_t st = 0;
360 ////    uint32_t aux1 = 0;
361 ////    st = rdtscp(aux1);
362 //
363 //    plmnId.buf = (uint8_t *) calloc(1, 3);
364 //    memcpy(plmnId.buf, data, 3);
365 //
366 //    checkAndPrint(&asn_DEF_PLMN_Identity, &plmnId, (char *) "PLMN_Identity_t", __func__);
367 //
368 //}
369
370 static void createENB_ID(ENB_ID_t &enb, ENB_ID_PR enbType, unsigned char *data) {
371     //printEntry("ENB_ID_t", __func__)
372     ASN_STRUCT_RESET(asn_DEF_ENB_ID, &enb);
373     enb.present = enbType;
374     switch (enbType) {
375         case ENB_ID_PR_macro_eNB_ID: { // 20 bit 3 bytes
376             enb.choice.macro_eNB_ID.size = 3;
377             enb.choice.macro_eNB_ID.bits_unused = 4;
378
379             enb.present = ENB_ID_PR_macro_eNB_ID;
380
381             enb.choice.macro_eNB_ID.buf = (uint8_t *) calloc(1, enb.choice.macro_eNB_ID.size);
382             data[enb.choice.macro_eNB_ID.size - 1] = ((unsigned) (data[enb.choice.macro_eNB_ID.size - 1]
383                     >> (unsigned) enb.choice.macro_eNB_ID.bits_unused)
384                     << (unsigned) enb.choice.macro_eNB_ID.bits_unused);
385             memcpy(enb.choice.macro_eNB_ID.buf, data, enb.choice.macro_eNB_ID.size);
386
387             break;
388         }
389         case ENB_ID_PR_home_eNB_ID: { // 28 bit 4 bytes
390             enb.choice.home_eNB_ID.size = 4;
391             enb.choice.home_eNB_ID.bits_unused = 4;
392             enb.present = ENB_ID_PR_home_eNB_ID;
393
394             enb.choice.home_eNB_ID.buf = (uint8_t *) calloc(1, enb.choice.home_eNB_ID.size);
395             data[enb.choice.home_eNB_ID.size - 1] = ((unsigned) (data[enb.choice.home_eNB_ID.size - 1]
396                     >> (unsigned) enb.choice.home_eNB_ID.bits_unused)
397                     << (unsigned) enb.choice.home_eNB_ID.bits_unused);
398             memcpy(enb.choice.home_eNB_ID.buf, data, enb.choice.home_eNB_ID.size);
399             break;
400         }
401         case ENB_ID_PR_short_Macro_eNB_ID: { // 18 bit - 3 bytes
402             enb.choice.short_Macro_eNB_ID.size = 3;
403             enb.choice.short_Macro_eNB_ID.bits_unused = 6;
404             enb.present = ENB_ID_PR_short_Macro_eNB_ID;
405
406             enb.choice.short_Macro_eNB_ID.buf = (uint8_t *) calloc(1, enb.choice.short_Macro_eNB_ID.size);
407             data[enb.choice.short_Macro_eNB_ID.size - 1] = ((unsigned) (data[enb.choice.short_Macro_eNB_ID.size - 1]
408                     >> (unsigned) enb.choice.short_Macro_eNB_ID.bits_unused)
409                     << (unsigned) enb.choice.short_Macro_eNB_ID.bits_unused);
410             memcpy(enb.choice.short_Macro_eNB_ID.buf, data, enb.choice.short_Macro_eNB_ID.size);
411             break;
412         }
413         case ENB_ID_PR_long_Macro_eNB_ID: { // 21
414             enb.choice.long_Macro_eNB_ID.size = 3;
415             enb.choice.long_Macro_eNB_ID.bits_unused = 3;
416             enb.present = ENB_ID_PR_long_Macro_eNB_ID;
417
418             enb.choice.long_Macro_eNB_ID.buf = (uint8_t *) calloc(1, enb.choice.long_Macro_eNB_ID.size);
419             data[enb.choice.long_Macro_eNB_ID.size - 1] = ((unsigned) (data[enb.choice.long_Macro_eNB_ID.size - 1]
420                     >> (unsigned) enb.choice.long_Macro_eNB_ID.bits_unused)
421                     << (unsigned) enb.choice.long_Macro_eNB_ID.bits_unused);
422             memcpy(enb.choice.long_Macro_eNB_ID.buf, data, enb.choice.long_Macro_eNB_ID.size);
423             break;
424         }
425         default:
426             break;
427     }
428
429     checkAndPrint(&asn_DEF_ENB_ID, &enb, (char *) "ENB_ID_t", __func__);
430 }
431
432
433 static void buildGlobalENB_ID(GlobalENB_ID_t *gnbId,
434                               const unsigned char *gnbData,
435                               ENB_ID_PR enbType,
436                               unsigned char *enbData) {
437     auto *plmnID = createPLMN_ID(gnbData);
438     memcpy(&gnbId->pLMN_Identity, plmnID, sizeof(PLMN_Identity_t));
439     createENB_ID(gnbId->eNB_ID, enbType, enbData);
440     checkAndPrint(&asn_DEF_GlobalENB_ID, gnbId, (char *) "GlobalENB_ID_t", __func__);
441 }
442
443
444 void buildSetupRequest(E2AP_PDU_t *pdu, int mcc, int mnc) {
445     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
446
447     pdu->choice.initiatingMessage = (InitiatingMessage_t *)calloc(1, sizeof(InitiatingMessage_t));
448     auto *initiatingMessage = pdu->choice.initiatingMessage;
449     ASN_STRUCT_RESET(asn_DEF_InitiatingMessage, initiatingMessage);
450     initiatingMessage->procedureCode = ProcedureCode_id_E2setup;
451     initiatingMessage->criticality = Criticality_reject;
452     initiatingMessage->value.present = InitiatingMessage__value_PR_E2setupRequest;
453
454     auto *e2SetupRequestIEs = (E2setupRequestIEs_t *)calloc(1, sizeof(E2setupRequestIEs_t));
455     ASN_STRUCT_RESET(asn_DEF_E2setupRequestIEs, e2SetupRequestIEs);
456
457     e2SetupRequestIEs->value.choice.GlobalE2node_ID.present = GlobalE2node_ID_PR_gNB;
458     e2SetupRequestIEs->value.choice.GlobalE2node_ID.choice.gNB = (GlobalE2node_gNB_ID_t *)calloc(1, sizeof(GlobalE2node_gNB_ID_t));
459     auto *globalE2NodeGNbId = e2SetupRequestIEs->value.choice.GlobalE2node_ID.choice.gNB;
460     ASN_STRUCT_RESET(asn_DEF_GlobalE2node_gNB_ID, globalE2NodeGNbId);
461
462     createPLMN_IDByMCCandMNC(&globalE2NodeGNbId->global_gNB_ID.plmn_id, mcc, mnc);
463     globalE2NodeGNbId->global_gNB_ID.gnb_id.present = GNB_ID_Choice_PR_gnb_ID;
464     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size = 4;
465     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf =
466             (uint8_t *) calloc(1, globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size); //22..32 bits
467     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.bits_unused = 0;
468     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[0] = 0xB5;
469     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[1] = 0xC6;
470     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[2] = 0x77;
471     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[3] = 0x88;
472
473     e2SetupRequestIEs->criticality = Criticality_reject;
474     e2SetupRequestIEs->id = ProtocolIE_ID_id_GlobalE2node_ID;
475     e2SetupRequestIEs->value.present = E2setupRequestIEs__value_PR_GlobalE2node_ID;
476
477
478     auto *e2SetupRequest = &initiatingMessage->value.choice.E2setupRequest;
479
480     ASN_STRUCT_RESET(asn_DEF_E2setupRequest, e2SetupRequest);
481     ASN_SEQUENCE_ADD(&e2SetupRequest->protocolIEs.list, e2SetupRequestIEs);
482
483     pdu->present = E2AP_PDU_PR_initiatingMessage;
484 }
485
486 void buildSetupRequesteenGNB(E2AP_PDU_t *pdu, int mcc, int mnc) {
487     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
488
489     pdu->choice.initiatingMessage = (InitiatingMessage_t *)calloc(1, sizeof(InitiatingMessage_t));
490     auto *initiatingMessage = pdu->choice.initiatingMessage;
491     ASN_STRUCT_RESET(asn_DEF_InitiatingMessage, initiatingMessage);
492     initiatingMessage->procedureCode = ProcedureCode_id_E2setup;
493     initiatingMessage->criticality = Criticality_reject;
494     initiatingMessage->value.present = InitiatingMessage__value_PR_E2setupRequest;
495
496     auto *e2SetupRequestIEs = (E2setupRequestIEs_t *)calloc(1, sizeof(E2setupRequestIEs_t));
497     ASN_STRUCT_RESET(asn_DEF_E2setupRequestIEs, e2SetupRequestIEs);
498
499     e2SetupRequestIEs->value.choice.GlobalE2node_ID.present = GlobalE2node_ID_PR_en_gNB;
500     e2SetupRequestIEs->value.choice.GlobalE2node_ID.choice.en_gNB = (GlobalE2node_en_gNB_ID_t *)calloc(1, sizeof(GlobalE2node_en_gNB_ID_t));
501     auto *globalE2NodeEN_GNb = e2SetupRequestIEs->value.choice.GlobalE2node_ID.choice.en_gNB;
502     ASN_STRUCT_RESET(asn_DEF_GlobalE2node_en_gNB_ID, globalE2NodeEN_GNb);
503
504     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.present = ENGNB_ID_PR_gNB_ID;
505     createPLMN_IDByMCCandMNC(&globalE2NodeEN_GNb->global_gNB_ID.pLMN_Identity, mcc, mnc);
506     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.size = 4;
507     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.buf =
508             (uint8_t *) calloc(1, globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.size); //22..32 bits
509     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.buf[0] = 0xC5;
510     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.buf[1] = 0xC6;
511     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.buf[2] = 0xC7;
512     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.buf[3] = 0xF8;
513     globalE2NodeEN_GNb->global_gNB_ID.gNB_ID.choice.gNB_ID.bits_unused = 0;
514     e2SetupRequestIEs->criticality = Criticality_reject;
515     e2SetupRequestIEs->id = ProtocolIE_ID_id_GlobalE2node_ID;
516     e2SetupRequestIEs->value.present = E2setupRequestIEs__value_PR_GlobalE2node_ID;
517
518
519     auto *e2SetupRequest = &initiatingMessage->value.choice.E2setupRequest;
520
521     ASN_STRUCT_RESET(asn_DEF_E2setupRequest, e2SetupRequest);
522     ASN_SEQUENCE_ADD(&e2SetupRequest->protocolIEs.list, e2SetupRequestIEs);
523
524     pdu->present = E2AP_PDU_PR_initiatingMessage;
525 }
526
527
528
529 void buildSetupRequestWithFunc(E2AP_PDU_t *pdu, int mcc, int mnc) {
530     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
531
532     pdu->choice.initiatingMessage = (InitiatingMessage_t *)calloc(1, sizeof(InitiatingMessage_t));
533     auto *initiatingMessage = pdu->choice.initiatingMessage;
534     ASN_STRUCT_RESET(asn_DEF_InitiatingMessage, initiatingMessage);
535     initiatingMessage->procedureCode = ProcedureCode_id_E2setup;
536     initiatingMessage->criticality = Criticality_reject;
537     initiatingMessage->value.present = InitiatingMessage__value_PR_E2setupRequest;
538
539     auto *e2SetupRequestIEs = (E2setupRequestIEs_t *)calloc(1, sizeof(E2setupRequestIEs_t));
540     ASN_STRUCT_RESET(asn_DEF_E2setupRequestIEs, e2SetupRequestIEs);
541
542     e2SetupRequestIEs->value.choice.GlobalE2node_ID.choice.gNB = (GlobalE2node_gNB_ID_t *)calloc(1, sizeof(GlobalE2node_gNB_ID_t));
543     auto *globalE2NodeGNbId = e2SetupRequestIEs->value.choice.GlobalE2node_ID.choice.gNB;
544     ASN_STRUCT_RESET(asn_DEF_GlobalE2node_gNB_ID, globalE2NodeGNbId);
545
546     createPLMN_IDByMCCandMNC(&globalE2NodeGNbId->global_gNB_ID.plmn_id, mcc, mnc);
547     globalE2NodeGNbId->global_gNB_ID.gnb_id.present = GNB_ID_Choice_PR_gnb_ID;
548     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size = 4;
549     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf =
550             (uint8_t *) calloc(1, globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.size); //22..32 bits
551     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.bits_unused = 0;
552     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[0] = 0xB5;
553     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[1] = 0xC6;
554     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[2] = 0x77;
555     globalE2NodeGNbId->global_gNB_ID.gnb_id.choice.gnb_ID.buf[3] = 0x88;
556
557     e2SetupRequestIEs->criticality = Criticality_reject;
558     e2SetupRequestIEs->id = ProtocolIE_ID_id_GlobalE2node_ID;
559     e2SetupRequestIEs->value.present = E2setupRequestIEs__value_PR_GlobalE2node_ID;
560     e2SetupRequestIEs->value.choice.GlobalE2node_ID.present = GlobalE2node_ID_PR_gNB;
561
562
563     auto *e2SetupRequest = &initiatingMessage->value.choice.E2setupRequest;
564
565     ASN_STRUCT_RESET(asn_DEF_E2setupRequest, e2SetupRequest);
566     ASN_SEQUENCE_ADD(&e2SetupRequest->protocolIEs.list, e2SetupRequestIEs);
567
568     auto *ranFlistIEs = (E2setupRequestIEs_t *)calloc(1, sizeof(E2setupRequestIEs_t));
569     ASN_STRUCT_RESET(asn_DEF_E2setupRequestIEs, ranFlistIEs);
570     ranFlistIEs->criticality = Criticality_reject;
571     ranFlistIEs->id = ProtocolIE_ID_id_RANfunctionsAdded;
572     ranFlistIEs->value.present = E2setupRequestIEs__value_PR_RANfunctions_List;
573
574     auto *itemIes = (RANfunction_ItemIEs_t *)calloc(1, sizeof(RANfunction_ItemIEs_t));
575     ASN_STRUCT_RESET(asn_DEF_RANfunction_ItemIEs, itemIes);
576
577
578     E2SM_gNB_NRT_RANfunction_Definition_t ranFunDef;
579     uint8_t funcDes[] = "asdfghjklpoiuytrewq\0";
580     ranFunDef.ranFunction_Name.ranFunction_Description.buf = (uint8_t *)calloc(1, strlen((char *)funcDes));
581     ranFunDef.ranFunction_Name.ranFunction_Description.size = strlen((char *)funcDes);
582     memcpy(ranFunDef.ranFunction_Name.ranFunction_Description.buf, funcDes, strlen((char *)funcDes));
583
584     uint8_t funcOID[] = "ABCDEFGHIJ1234567890\0";
585     ranFunDef.ranFunction_Name.ranFunction_E2SM_OID.buf = (uint8_t *)calloc(1, strlen((char *)funcOID));
586     ranFunDef.ranFunction_Name.ranFunction_E2SM_OID.size = strlen((char *)funcOID);
587     memcpy(ranFunDef.ranFunction_Name.ranFunction_E2SM_OID.buf, funcOID, strlen((char *)funcOID));
588
589     uint8_t shortName[] = "Nothing to declare\0";
590     ranFunDef.ranFunction_Name.ranFunction_ShortName.buf = (uint8_t *)calloc(1, strlen((char *)shortName));
591     ranFunDef.ranFunction_Name.ranFunction_ShortName.size = strlen((char *)shortName);
592     memcpy(ranFunDef.ranFunction_Name.ranFunction_ShortName.buf, shortName, strlen((char *)shortName));
593
594
595     RIC_InsertStyle_List_t insertStyleList;
596     insertStyleList.ric_CallProcessIDFormat_Type = 28l;
597     insertStyleList.ric_IndicationHeaderFormat_Type = 29;
598     insertStyleList.ric_IndicationMessageFormat_Type = 30;
599     insertStyleList.ric_InsertActionFormat_Type = 31l;
600
601     uint8_t styleName[] = "What a style\0";
602
603     insertStyleList.ric_InsertStyle_Name.buf = (uint8_t *)calloc(1, strlen((char *)styleName));
604     insertStyleList.ric_InsertStyle_Name.size = strlen((char *)styleName);
605     memcpy(insertStyleList.ric_InsertStyle_Name.buf, styleName, strlen((char *)styleName));
606
607
608     insertStyleList.ric_InsertStyle_Type = 23;
609
610     RANparameterDef_Item_t raNparameterDefItem;
611     raNparameterDefItem.ranParameter_ID = 8;
612     raNparameterDefItem.ranParameter_Type = 12;
613
614     uint8_t ItemName[] = "What a style\0";
615     raNparameterDefItem.ranParameter_Name.buf = (uint8_t *)calloc(1, strlen((char *)ItemName));
616     raNparameterDefItem.ranParameter_Name.size = strlen((char *)ItemName);
617     memcpy(raNparameterDefItem.ranParameter_Name.buf, ItemName, strlen((char *)ItemName));
618
619     ASN_SEQUENCE_ADD(&insertStyleList.ric_InsertRanParameterDef_List.list, &raNparameterDefItem);
620
621     ASN_SEQUENCE_ADD(&ranFunDef.ric_InsertStyle_List->list, &insertStyleList);
622     //ranFunDef.ric_InsertStyle_List.
623
624     uint8_t buffer[8192];
625     size_t buffer_size = 8192;
626     auto *ranDef = &itemIes->value.choice.RANfunction_Item.ranFunctionDefinition;
627
628     auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition, &ranFunDef, buffer, buffer_size);
629     if (er.encoded == -1) {
630         cerr << "encoding of " << asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name << " failed, " << strerror(errno) << endl;
631         exit(-1);
632     } else if (er.encoded > (ssize_t) buffer_size) {
633         cerr << "Buffer of size " << buffer_size << " is to small for " << asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name << endl;
634         exit(-1);
635     } else {
636         ranDef->buf = (uint8_t *)calloc(1, er.encoded);
637         ranDef->size = er.encoded;
638         memcpy(ranDef->buf, buffer, ranDef->size);
639     }
640
641
642
643     itemIes->id = ProtocolIE_ID_id_RANfunction_Item;
644     itemIes->criticality = Criticality_reject;
645     itemIes->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item;
646     itemIes->value.choice.RANfunction_Item.ranFunctionID = 1;
647 //    auto *ranDef = &itemIes->value.choice.RANfunction_Item.ranFunctionDefinition;
648 //    ranDef->size = 3;
649 //    ranDef->buf = (uint8_t *)calloc(1, ranDef->size);
650 //    memcpy(ranDef->buf, buf, ranDef->size);
651
652     ASN_SEQUENCE_ADD(&ranFlistIEs->value.choice.RANfunctions_List.list, itemIes);
653
654     auto *itemIes1 = (RANfunction_ItemIEs_t *)calloc(1, sizeof(RANfunction_ItemIEs_t));
655     ASN_STRUCT_RESET(asn_DEF_RANfunction_ItemIEs, itemIes1);
656     itemIes1->id = ProtocolIE_ID_id_RANfunction_Item;
657     itemIes1->criticality = Criticality_reject;
658     itemIes1->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item;
659     itemIes1->value.choice.RANfunction_Item.ranFunctionID = 7;
660     ranDef = &itemIes1->value.choice.RANfunction_Item.ranFunctionDefinition;
661
662     ranDef->buf = (uint8_t *)calloc(1, er.encoded);
663     ranDef->size = er.encoded;
664     memcpy(ranDef->buf, buffer, ranDef->size);
665
666     ASN_SEQUENCE_ADD(&ranFlistIEs->value.choice.RANfunctions_List.list, itemIes1);
667
668
669     ASN_SEQUENCE_ADD(&e2SetupRequest->protocolIEs.list, ranFlistIEs);
670
671     pdu->present = E2AP_PDU_PR_initiatingMessage;
672 }
673
674
675
676
677
678 void buildSetupSuccsessfulResponse(E2AP_PDU_t *pdu, int mcc, int mnc, uint8_t *data) {
679     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
680
681     pdu->choice.successfulOutcome = (SuccessfulOutcome_t *)calloc(1, sizeof(SuccessfulOutcome_t));
682     SuccessfulOutcome_t *successfulOutcome = pdu->choice.successfulOutcome;
683     ASN_STRUCT_RESET(asn_DEF_E2setupResponse, &successfulOutcome->value.choice.E2setupResponse);
684     successfulOutcome->procedureCode = ProcedureCode_id_E2setup;
685     successfulOutcome->criticality = Criticality_reject;
686     successfulOutcome->value.present = SuccessfulOutcome__value_PR_E2setupResponse;
687
688
689     auto *globalRicidIE = (E2setupResponseIEs_t *)calloc(1, sizeof(E2setupResponseIEs_t));
690     ASN_STRUCT_RESET(asn_DEF_E2setupResponseIEs, globalRicidIE);
691
692     globalRicidIE->criticality = Criticality_reject;
693     globalRicidIE->id = ProtocolIE_ID_id_GlobalRIC_ID;
694     globalRicidIE->value.present = E2setupResponseIEs__value_PR_GlobalRIC_ID;
695     createPLMN_IDByMCCandMNC(&globalRicidIE->value.choice.GlobalRIC_ID.pLMN_Identity, mcc, mnc);
696
697     globalRicidIE->value.choice.GlobalRIC_ID.ric_ID = {nullptr, 3, 4};
698     globalRicidIE->value.choice.GlobalRIC_ID.ric_ID.buf = (uint8_t *)calloc(1, globalRicidIE->value.choice.GlobalRIC_ID.ric_ID.size);
699     memcpy(globalRicidIE->value.choice.GlobalRIC_ID.ric_ID.buf, data, globalRicidIE->value.choice.GlobalRIC_ID.ric_ID.size);
700     globalRicidIE->value.choice.GlobalRIC_ID.ric_ID.buf[2] &= (unsigned)0xF0;
701
702
703     ASN_STRUCT_RESET(asn_DEF_E2setupResponse, &successfulOutcome->value.choice.E2setupResponse);
704     ASN_SEQUENCE_ADD(&successfulOutcome->value.choice.E2setupResponse.protocolIEs.list, globalRicidIE);
705
706     auto *ranFunctionAdd = (E2setupResponseIEs_t *)calloc(1, sizeof(E2setupResponseIEs_t));
707     ASN_STRUCT_RESET(asn_DEF_E2setupResponseIEs, ranFunctionAdd);
708     ranFunctionAdd->criticality = Criticality_reject;
709     ranFunctionAdd->id = ProtocolIE_ID_id_RANfunctionsAccepted;
710     ranFunctionAdd->value.present = E2setupResponseIEs__value_PR_RANfunctionsID_List;
711
712     auto *ranFuncIdItemIEs = (RANfunctionID_ItemIEs_t *)calloc(1, sizeof(RANfunctionID_ItemIEs_t));
713
714     ranFuncIdItemIEs->criticality = Criticality_ignore;
715     ranFuncIdItemIEs->id = ProtocolIE_ID_id_RANfunctionID_Item;
716     ranFuncIdItemIEs->value.present = RANfunctionID_ItemIEs__value_PR_RANfunctionID_Item;
717     ranFuncIdItemIEs->value.choice.RANfunctionID_Item.ranFunctionID = 10;
718     ranFuncIdItemIEs->value.choice.RANfunctionID_Item.ranFunctionRevision = 1;
719
720     ASN_SEQUENCE_ADD(&ranFunctionAdd->value.choice.RANfunctionsID_List.list, ranFuncIdItemIEs);
721     ASN_SEQUENCE_ADD(&successfulOutcome->value.choice.E2setupResponse.protocolIEs.list, ranFunctionAdd);
722
723
724
725
726
727     pdu->present = E2AP_PDU_PR_successfulOutcome;
728 }
729
730
731 void buildSetupUnSuccsessfulResponse(E2AP_PDU_t *pdu) {
732     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
733
734     pdu->choice.unsuccessfulOutcome = (UnsuccessfulOutcome_t *)calloc(1, sizeof(UnsuccessfulOutcome_t));
735     UnsuccessfulOutcome_t *uns = pdu->choice.unsuccessfulOutcome;
736     uns->procedureCode = ProcedureCode_id_E2setup;
737     uns->criticality = Criticality_reject;
738     uns->value.present = UnsuccessfulOutcome__value_PR_E2setupFailure;
739
740     ASN_STRUCT_RESET(asn_DEF_E2setupFailure, &uns->value.choice.E2setupFailure);
741
742
743     {
744         auto *e2SetupFIE = (E2setupFailureIEs_t *) calloc(1, sizeof(E2setupFailureIEs_t));
745         ASN_STRUCT_RESET(asn_DEF_E2setupFailureIEs, e2SetupFIE);
746
747         e2SetupFIE->criticality = Criticality_ignore;
748         e2SetupFIE->id = ProtocolIE_ID_id_Cause;
749         e2SetupFIE->value.present = E2setupFailureIEs__value_PR_Cause;
750         e2SetupFIE->value.choice.Cause.present = Cause_PR_transport;
751         e2SetupFIE->value.choice.Cause.choice.transport = CauseTransport_transport_resource_unavailable;
752
753
754         ASN_SEQUENCE_ADD(&uns->value.choice.E2setupFailure.protocolIEs.list, e2SetupFIE);
755     }
756
757     {
758         auto *e2SetupFIE = (E2setupFailureIEs_t *) calloc(1, sizeof(E2setupFailureIEs_t));
759         ASN_STRUCT_RESET(asn_DEF_E2setupFailureIEs, e2SetupFIE);
760
761         e2SetupFIE->criticality = Criticality_ignore;
762         e2SetupFIE->id = ProtocolIE_ID_id_TimeToWait;
763         e2SetupFIE->value.present = E2setupFailureIEs__value_PR_TimeToWait;
764         e2SetupFIE->value.choice.TimeToWait = TimeToWait_v60s;
765
766         ASN_SEQUENCE_ADD(&uns->value.choice.E2setupFailure.protocolIEs.list, e2SetupFIE);
767     }
768     {
769         auto *e2SetupFIE = (E2setupFailureIEs_t *) calloc(1, sizeof(E2setupFailureIEs_t));
770         ASN_STRUCT_RESET(asn_DEF_E2setupFailureIEs, e2SetupFIE);
771
772         e2SetupFIE->criticality = Criticality_ignore;
773         e2SetupFIE->id = ProtocolIE_ID_id_CriticalityDiagnostics;
774         e2SetupFIE->value.present = E2setupFailureIEs__value_PR_CriticalityDiagnostics;
775         e2SetupFIE->value.choice.CriticalityDiagnostics.procedureCode = (ProcedureCode_t *)calloc(1,sizeof(ProcedureCode_t));
776         *e2SetupFIE->value.choice.CriticalityDiagnostics.procedureCode = ProcedureCode_id_E2setup;
777         e2SetupFIE->value.choice.CriticalityDiagnostics.triggeringMessage = (TriggeringMessage_t *)calloc(1,sizeof(TriggeringMessage_t));
778         *e2SetupFIE->value.choice.CriticalityDiagnostics.triggeringMessage = TriggeringMessage_initiating_message;
779         e2SetupFIE->value.choice.CriticalityDiagnostics.procedureCriticality = (Criticality_t *)calloc(1, sizeof(Criticality_t));
780         *e2SetupFIE->value.choice.CriticalityDiagnostics.procedureCriticality = Criticality_reject;
781         ASN_SEQUENCE_ADD(&uns->value.choice.E2setupFailure.protocolIEs.list, e2SetupFIE);
782     }
783
784     pdu->present = E2AP_PDU_PR_unsuccessfulOutcome;
785 }
786
787 #endif //E2_E2BUILDER_H