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