[Epic-ID: ODUHIGH-463][Task-ID: ODUHIGH-496] Dummy Xn Setup Request/Response
[o-du/l2.git] / src / cu_stub / cu_xnap_msg_hdl.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains F1AP message handler functions */
20 #include "common_def.h"
21 #include "cu_stub_sctp.h"
22 #include "cu_stub_egtp.h"
23 #include "OCTET_STRING.h"
24 #include "cu_f1ap_msg_hdl.h"
25 #include "cu_stub.h"
26
27 /*******************************************************************
28  *
29  * @brief Build And send dummy Xn Setup request
30  *
31  * @details
32  *
33  *    Function : BuildAndSendXnSetupReq
34  *
35  *    Functionality:
36  *       Build And send dummy Xn Setup request
37  *
38  * @params[in]
39  * @return void
40  *
41  ******************************************************************/
42 void BuildAndSendXnSetupReq()
43 {
44    uint8_t assocIdx = 0;
45    Buffer *mBuf = NULLP;
46    CuSctpAssocCb *assocCb = NULLP;
47
48    for(assocIdx = 0; assocIdx < sctpCb.numAssoc; assocIdx++)
49    {
50       assocCb = &sctpCb.assocCb[assocIdx];
51       if(assocCb->connUp && assocCb->intf == XN_INTERFACE && assocCb->destId == 0)
52       {
53          if(ODU_GET_MSG_BUF(1, 1, &mBuf) == ROK)
54          {
55             CMCHKPK(oduUnpackUInt8,(Data *)CU_ID, mBuf);
56             CMCHKPK(oduUnpackUInt8,XN_SETUP_REQ, mBuf);
57             ODU_PRINT_MSG(mBuf, 0,0);
58             if(sendOnSctpAssoc(assocCb, mBuf) != ROK)
59             {
60                DU_LOG("\nERROR  -->  CU_STUB: Failed to send XN setup request to peer CU");
61             }
62          }
63          break;
64       }
65    }
66 }
67
68 /*******************************************************************
69  *
70  * @brief Process received Xn Setup request
71  *
72  * @details
73  *
74  *    Function : XNAPProcXnSetupReq
75  *
76  *    Functionality:
77  *       Fetch peer CU ID and store in assocCb
78  *
79  * @params[in] Pointer to destination Id
80  *             Pointer to message buffer
81  * @return void
82  *
83  ******************************************************************/
84 void XNAPProcXnSetupReq(uint32_t *destId, Buffer *mBuf)
85 {
86    uint8_t cuId;
87
88    DU_LOG("\nINFO  -->  CU STUB : Received XN Setup Request");
89    CMCHKUNPK(oduPackUInt8, &(cuId), mBuf)
90    *destId = cuId;
91
92    BuildAndSendXnSetupRsp(*destId);
93 }
94
95 /*******************************************************************
96  *
97  * @brief Build And send dummy Xn Setup response
98  *
99  * @details
100  *
101  *    Function : BuildAndSendXnSetupRsp
102  *
103  *    Functionality:
104  *       Build And send dummy Xn Setup response
105  *
106  * @params[in]
107  * @return void
108  *
109  ******************************************************************/
110 void BuildAndSendXnSetupRsp(uint32_t destId)
111 {
112    Buffer *mBuf = NULLP;
113
114    if(ODU_GET_MSG_BUF(1, 1, &mBuf) == ROK)
115    {
116       CMCHKPK(oduUnpackUInt8, CU_ID, mBuf);
117       CMCHKPK(oduUnpackUInt8, XN_SETUP_RSP, mBuf);
118       if(sctpSend(XN_INTERFACE, destId, mBuf))
119       {
120          DU_LOG("\nERROR  -->  CU_STUB: Failed to send XN setup response to peer CU");
121       }   
122    }   
123 }
124
125 /*******************************************************************
126  *
127  * @brief Process received Xn Setup response
128  *
129  * @details
130  *
131  *    Function : XNAPProcXnSetupRsp
132  *
133  *    Functionality:
134  *       Fetch peer CU ID and store in assocCb
135  *
136  * @params[in] Pointer to destination Id
137  *             Pointer to message buffer
138  * @return void
139  *
140  ******************************************************************/
141 void XNAPProcXnSetupRsp(uint32_t *destId, Buffer *mBuf)
142 {
143    uint8_t cuId;
144
145    DU_LOG("\nINFO  -->  CU STUB : Received XN Setup Response");
146    CMCHKUNPK(oduPackUInt8, &(cuId), mBuf)
147    *destId = cuId;
148 }
149
150 /*******************************************************************
151  *
152  * @brief Build And send dummy Handover request to Peer CU
153  *
154  * @details
155  *
156  *    Function : BuildAndSendHOReq
157  *
158  *    Functionality:
159  *       Build And send dummy Handover request to Peer CU
160  *
161  * @params[in] Pointer to UE Cb
162  *             Event type
163  *             Message to be sent
164  *             Message Length
165  * @return void
166  *
167  ******************************************************************/
168 void BuildAndSendHOReq(CuUeCb *ueCb, XnEventType event, char *xnMsg, MsgLen xnMsgLen)
169 {
170    Buffer *mBuf = NULLP;
171
172    if(ODU_GET_MSG_BUF(1, 1, &mBuf) == ROK)
173    {
174       if(ODU_ADD_POST_MSG_MULT((Data *)xnMsg, xnMsgLen, mBuf) == ROK)
175       {     
176          CMCHKPK(oduUnpackUInt8, event, mBuf);
177          if(sctpSend(XN_INTERFACE, ueCb->hoInfo.targetId, mBuf) != ROK)
178          {
179             DU_LOG("\nERROR  -->  CU_STUB: Failed to send handover request to peer CU");
180             ueCb->state = UE_ACTIVE;
181             memset(&ueCb->hoInfo, 0, sizeof(HandoverInfo));
182          }
183       }     
184    }
185 }
186
187 /*******************************************************************
188  *
189  * @brief Handle incoming messages at Xn interface
190  *
191  * @details
192  *
193  *    Function : XNAPMsgHdlr
194  *
195  *    Functionality:
196  *       Fetch event type from message buffer and call appropriate
197  *       handler
198  *
199  * @params[in] Pointer to destination Id
200  *             Pointer to message buffer
201  * @return void
202  *
203  ******************************************************************/
204 void XNAPMsgHdlr(uint32_t *destId, Buffer *mBuf)
205 {
206    XnEventType event;
207
208    CMCHKUNPK(oduPackUInt8, &event, mBuf);
209
210    switch(event)
211    {
212       case XN_SETUP_REQ:
213          {
214              XNAPProcXnSetupReq(destId, mBuf);
215              break;
216          }
217
218     case XN_SETUP_RSP:
219          {
220              XNAPProcXnSetupRsp(destId, mBuf);
221              break;
222          }
223
224       case HO_REQ:
225          {
226             DU_LOG("\nINFO  -->  CU STUB : Received Handover Request at XNAP");
227
228 //Handling of HO Request to be added in next gerrit
229 #if 0
230             char *recvBuf;
231             MsgLen copyCnt;
232             MsgLen recvBufLen;
233             F1AP_PDU_t *f1apMsg = NULLP;
234             asn_dec_rval_t rval; /* Decoder return value */
235             F1AP_PDU_t f1apasnmsg ;
236
237             ODU_PRINT_MSG(mBuf, 0,0); 
238
239             /* Copy mBuf into char array to decode it */
240             ODU_GET_MSG_LEN(mBuf, &recvBufLen);
241             CU_ALLOC(recvBuf, (Size)recvBufLen);
242
243             if(recvBuf == NULLP)
244             {
245                DU_LOG("\nERROR  -->  F1AP : Memory allocation failed");
246                return;
247             }
248             if(ODU_COPY_MSG_TO_FIX_BUF(mBuf, 0, recvBufLen, (Data *)recvBuf, &copyCnt) != ROK)
249             {
250                DU_LOG("\nERROR  -->  F1AP : Failed while copying %d", copyCnt);
251                return;
252             }
253
254             DU_LOG("\nDEBUG  -->  F1AP : Received flat buffer to be decoded : ");
255             for(i=0; i< recvBufLen; i++)
256             {
257                DU_LOG("%x",recvBuf[i]);
258             }
259
260             /* Decoding flat buffer into F1AP messsage */
261             f1apMsg = &f1apasnmsg;
262             memset(f1apMsg, 0, sizeof(F1AP_PDU_t));
263             rval = aper_decode(0, &asn_DEF_F1AP_PDU, (void **)&f1apMsg, recvBuf, recvBufLen, 0, 0);
264             CU_FREE(recvBuf, (Size)recvBufLen);
265
266             if(rval.code == RC_FAIL || rval.code == RC_WMORE)
267             {
268                DU_LOG("\nERROR  -->  F1AP : ASN decode failed");
269                return;
270             }
271             DU_LOG("\n");
272             xer_fprint(stdout, &asn_DEF_F1AP_PDU, f1apMsg);
273
274             procUeContextModificationResponse(0, f1apMsg, recvBuf, recvBufLen);      
275 #endif            
276             break;
277          }
278       default:
279          DU_LOG("\nERROR  --> CU_STUB : Invalid event [%d] received at XN interface", event);
280          break;
281    }
282 }
283
284 /**********************************************************************
285   End of file
286  **********************************************************************/