Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / E2AP / E2SM / e2sm.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 #include "e2sm.h"
20
21 #include "ActionParameter-Item.h"
22 #include "ActionParameter-Value.h"
23 #include "PLMN-Identity.h"
24 #include "GlobalENB-ID.h"
25 #include "GlobalGNB-ID.h"
26 #include "Interface-ID.h"
27 #include "ProtocolIE-Field.h"
28 #include "InterfaceMessageType.h"
29 #include "InterfaceProtocolIE-Item.h"
30
31 static PLMN_Identity_t *createPLMN_ID(const unsigned char *data) {
32
33     PLMN_Identity_t *plmnId = calloc(1, sizeof(PLMN_Identity_t));
34     ASN_STRUCT_RESET(asn_DEF_PLMN_Identity, plmnId);
35     plmnId->size = 3;
36     plmnId->buf = calloc(1, 3);
37     memcpy(plmnId->buf, data, 3);
38
39     return plmnId;
40 }
41
42 static GNB_ID_t *createGnb_id(const unsigned char *data, int numOfBits) {
43
44     if (numOfBits < 22 || numOfBits > 32) {
45         printf("[Error] GNB_ID_t number of bits = %d, needs to be 22 .. 32", numOfBits);
46         return NULL;
47     }
48
49     GNB_ID_t *gnb = calloc(1, sizeof(GNB_ID_t));
50     ASN_STRUCT_RESET(asn_DEF_GNB_ID, gnb);
51
52     gnb->present = GNB_ID_PR_gNB_ID;
53     gnb->choice.gNB_ID.size = numOfBits % 8 == 0 ? (unsigned int)(numOfBits / 8) : (unsigned int)(numOfBits / 8 + 1);
54     gnb->choice.gNB_ID.bits_unused = gnb->choice.gNB_ID.size * 8 - numOfBits;
55     gnb->choice.gNB_ID.buf = calloc(1, gnb->choice.gNB_ID.size);
56     memcpy(gnb->choice.gNB_ID.buf, data, gnb->choice.gNB_ID.size);
57     gnb->choice.gNB_ID.buf[gnb->choice.gNB_ID.size - 1] =
58             ((unsigned)(gnb->choice.gNB_ID.buf[gnb->choice.gNB_ID.size - 1] >> (unsigned)gnb->choice.gNB_ID.bits_unused)
59                   << (unsigned)gnb->choice.gNB_ID.bits_unused);
60
61     return gnb;
62 }
63
64 static GlobalGNB_ID_t *createGlobalGNB_ID(PLMN_Identity_t *plmnIdentity, GNB_ID_t *gnb) {
65
66     GlobalGNB_ID_t *ggnbId = calloc(1, sizeof(GlobalGNB_ID_t));
67     ASN_STRUCT_RESET(asn_DEF_GlobalGNB_ID, ggnbId);
68
69     memcpy(&ggnbId->pLMN_Identity, plmnIdentity, sizeof(PLMN_Identity_t));
70     memcpy(&ggnbId->gNB_ID, gnb, sizeof(GNB_ID_t));
71
72     return ggnbId;
73 }
74
75 static Interface_ID_t *createInterfaceIDForGnb(GlobalGNB_ID_t *gnb) {
76
77     Interface_ID_t *interfaceId = calloc(1, sizeof(Interface_ID_t));
78     ASN_STRUCT_RESET(asn_DEF_Interface_ID, interfaceId);
79
80     interfaceId->present = Interface_ID_PR_global_gNB_ID;
81     interfaceId->choice.global_gNB_ID = gnb;
82
83     return interfaceId;
84 }
85
86 static InterfaceMessageType_t *createInterfaceMessageInitiating(long procedureCode) {
87
88     InterfaceMessageType_t *intMsgT = calloc(1, sizeof(InterfaceMessageType_t));
89     ASN_STRUCT_RESET(asn_DEF_InterfaceMessageType, intMsgT);
90
91     intMsgT->procedureCode = procedureCode;
92     intMsgT->typeOfMessage = TypeOfMessage_initiating_message;
93
94     return intMsgT;
95 }
96
97 E2SM_gNB_X2_eventTriggerDefinition_t* create_eventTriggerDefinition(void)
98 {
99   //PLMN_Identity
100   unsigned char plmnidData[3] = {0x33, 0xF4, 0x55};
101   PLMN_Identity_t *plmnid = createPLMN_ID(plmnidData);
102   // xer_fprint(stdout, &asn_DEF_PLMN_Identity, (void *)plmnid);
103
104   //GNB_ID
105   unsigned char gnbData[3] = {0x99, 0xaa, 0xbb};
106   GNB_ID_t *gnb = createGnb_id(gnbData, 26);
107   // xer_fprint(stdout, &asn_DEF_GNB_ID, (void *)gnb);
108
109   //GlobalGNB_ID
110   GlobalGNB_ID_t *globaGnb = createGlobalGNB_ID(plmnid, gnb);
111   // xer_fprint(stdout, &asn_DEF_GlobalGNB_ID, (void *)globaGnb);
112
113   //Interface_ID
114   Interface_ID_t *gnbInterfaceId = createInterfaceIDForGnb(globaGnb);
115   // xer_fprint(stdout, &asn_DEF_Interface_ID, (void *)gnbInterfaceId);
116
117   //InterfaceMessageType
118   InterfaceMessageType_t *initiatingInterface = createInterfaceMessageInitiating(35);
119
120   //EventTriggerDefinition
121   E2SM_gNB_X2_eventTriggerDefinition_t *eventTrigger = calloc(1, sizeof(E2SM_gNB_X2_eventTriggerDefinition_t));
122   ASN_STRUCT_RESET(asn_DEF_E2SM_gNB_X2_eventTriggerDefinition, eventTrigger);
123
124   memcpy(&eventTrigger->interface_ID , gnbInterfaceId, sizeof(Interface_ID_t));
125
126   eventTrigger->interfaceDirection = 0;
127   memcpy(&eventTrigger->interfaceMessageType, initiatingInterface, sizeof(InterfaceMessageType_t));
128
129   return eventTrigger;
130 }
131
132 #define E2AP_ASN1C_FIND_PROTOCOLIE_BY_ID(IE_TYPE, ie, container, IE_ID) \
133   do {\
134     IE_TYPE **ptr; \
135     ie = NULL; \
136     for (ptr = container->protocolIEs.list.array; \
137          ptr < &container->protocolIEs.list.array[container->protocolIEs.list.count]; \
138          ptr++) { \
139       if((*ptr)->id == IE_ID) { \
140         ie = *ptr; \
141         break; \
142       } \
143     } \
144   } while(0)
145
146
147 void test_eventTriggerDefinition(void)
148 {
149   E2SM_gNB_X2_eventTriggerDefinition_t *eventTrigger = create_eventTriggerDefinition();
150
151   xer_fprint(stdout, &asn_DEF_E2SM_gNB_X2_eventTriggerDefinition, (void *)eventTrigger);
152   printf("\n");
153
154   uint8_t *eventBuff;
155   int eventBuff_len;
156
157   eventBuff_len = aper_encode_to_new_buffer(&asn_DEF_E2SM_gNB_X2_eventTriggerDefinition,
158                                         0, eventTrigger, (void **)&eventBuff);
159
160   LOG_I("Encoded eventBuff_len = %d", eventBuff_len);
161
162   //-----------------------------------------------------------------------
163   E2AP_PDU_t* pdu = e2ap_xml_to_pdu("E2AP_RICsubscriptionRequest.xml");
164   e2ap_asn1c_print_pdu(pdu);
165
166   InitiatingMessage_t *initiatingMessage = pdu->choice.initiatingMessage;
167   xer_fprint(stdout, &asn_DEF_InitiatingMessage, (void *)initiatingMessage);
168
169   RICsubscriptionRequest_t *request = &initiatingMessage->value.choice.RICsubscriptionRequest;
170   xer_fprint(stdout, &asn_DEF_RICsubscriptionRequest, (void *)request);
171
172   LOG_I("num of IEs = %d", request->protocolIEs.list.count);
173
174
175   RICsubscriptionRequest_IEs_t *ie;
176
177   ie = request->protocolIEs.list.array[2];
178
179   // E2AP_ASN1C_FIND_PROTOCOLIE_BY_ID(RICsubscriptionRequest_IEs_t, ie, request, ProtocolIE_ID_id_RICsubscription);
180   // E2AP_ASN1C_FIND_PROTOCOLIE_BY_ID(RICsubscriptionRequest_IEs_t, ie, request, ProtocolIE_ID_id_RICrequestID);
181
182   assert( ie != NULL);
183
184   xer_fprint(stdout, &asn_DEF_RICsubscription, (void *)ie);
185   // xer_fprint(stdout, &asn_DEF_RICrequestID, (void *)ie);
186
187
188 }
189
190 //
191 // typedef struct ProtocolIE_Container_119P0 {
192 //      A_SEQUENCE_OF(struct RICsubscriptionRequest_IEs) list;
193 //
194 //      /* Context for parsing across buffer boundaries */
195 //      asn_struct_ctx_t _asn_ctx;
196 // } ProtocolIE_Container_119P0_t;