Revert "[Epic-ID: ODUHIGH-576][Task-ID: ODUHIGH-579] | Databse Creation"
[o-du/l2.git] / src / du_app / du_sctp.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 all SCTP related functionality */
20 #include "common_def.h"
21 #include "du_tmr.h"
22 #include "lrg.h"
23 #include "legtp.h"
24 #include "lrg.x"
25 #include "lkw.x"
26 #include "cm_inet.h"
27 #include "cm_inet.x"
28 #include "du_app_mac_inf.h"
29 #include "du_e2ap_mgr.h"
30 #include "du_cfg.h"
31 #include "du_sctp.h"
32 #include "lsctp.h"
33 #include "du_app_rlc_inf.h"
34 #include "du_mgr.h"
35 #include "du_utils.h"
36 /* Global variable declaration */
37 uint8_t   socket_type;      /* Socket type */
38 bool nonblocking;      /* Blocking/Non-blocking socket */
39 bool connUp;           /* Is connection up */
40 bool pollingState; 
41 CmInetNetAddrLst localAddrLst;
42 CmInetNetAddrLst remoteAddrLst;
43 /* Global variable declaration */
44 DuSctpDestCb f1Params;     /* SCTP configurations at DU */ 
45 DuSctpDestCb ricParams;    /* SCTP configurations at DU */ 
46
47 /**************************************************************************
48  * @brief Task Initiation callback function. 
49  *
50  * @details
51  *
52  *     Function : sctpActvInit 
53  *    
54  *     Functionality:
55  *             This function is supplied as one of parameters during SCTP's 
56  *             task registration. SSI will invoke this function once, after
57  *             it creates and attaches this TAPA Task to a system task.
58  *     
59  * @param[in]  Ent entity, the entity ID of this task.     
60  * @param[in]  Inst inst, the instance ID of this task.
61  * @param[in]  Region region, the region ID registered for memory 
62  *              usage of this task.
63  * @param[in]  Reason reason.
64  * @return ROK     - success
65  *         RFAILED - failure
66  ***************************************************************************/
67 uint8_t sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
68 {
69    DU_LOG("\n\nDEBUG  -->  SCTP : Initializing");
70    ODU_SET_PROC_ID(DU_PROC);
71    connUp = FALSE;
72    
73    memset(&f1Params, 0, sizeof(DuSctpDestCb));
74    f1Params.assocId = -1;
75    memset(&ricParams, 0, sizeof(DuSctpDestCb));
76    ricParams.assocId = -1;
77    nonblocking = FALSE;
78    return ROK;
79
80 }
81
82 /**************************************************************************
83 * @brief Function prints src, dest, msg infor about all the msgs that received.
84 *
85 * @details
86 *
87 *     Function : callFlowSctpActvTsk
88 *
89 *     Function prints src, dest, msg infor about all the msgs that received
90 *
91 *  @param[in]  Pst     *pst, Post structure of the primitive.
92 *
93 *  @return  void
94 ***************************************************************************/
95 void callFlowSctpActvTsk(Pst *pst)
96 {
97    char sourceTask[50];
98    char destTask[50]="ENTSCTP";
99    char message[100];
100
101    switch(pst->srcEnt)
102    {
103       case ENTDUAPP:
104          {
105             strcpy(sourceTask,"ENTDUAPP");
106             switch(pst->event)
107             {
108                case EVTSTARTPOLL:
109                   {
110                      strcpy(message,"EVTSTARTPOLL");
111                      break;
112                   }
113             }
114             break;
115          }
116    }
117    DU_LOG("\nCall Flow: %s -> %s : %s\n", sourceTask, destTask, message);
118 }
119
120 /**************************************************************************
121  * @brief Task Activation callback function. 
122  *
123  * @details
124  *
125  *      Function : sctpActvTsk 
126  * 
127  *      Functionality:
128  *           This function handles all SCTP messages received
129  *           This API is registered with SSI during the 
130  *           Task Registration of DU APP.
131  *     
132  * @param[in]  Pst     *pst, Post structure of the primitive.     
133  * @param[in]  Buffer *mBuf, Packed primitive parameters in the
134  *  buffer.
135  * @return ROK     - success
136  *         RFAILED - failure
137  *
138  ***************************************************************************/
139 uint8_t sctpActvTsk(Pst *pst, Buffer *mBuf)
140 {
141
142 #ifdef CALL_FLOW_DEBUG_LOG
143    callFlowSctpActvTsk(pst);
144 #endif
145
146
147    switch(pst->srcEnt)
148    {
149       case ENTDUAPP:
150          {
151             switch(pst->event)
152             {
153                case EVTSTARTPOLL:
154                {
155                   sctpSockPoll();
156                   break;
157                } 
158             }
159             break;
160          }
161    }
162    ODU_EXIT_TASK();
163    return ROK;
164 }
165 /*******************************************************************
166  *
167  * @brief Checks the status of the received information
168  *
169  * @details
170  *
171  *    Function : duCheckReqStatus
172  *
173  *    Functionality:
174  *       Checks the status of the received information
175  *
176  * @params[in] Confirm status
177  * @return ROK     - success
178  *         RFAILED - failure
179  *
180  ******************************************************************/
181 uint8_t duCheckReqStatus(CmStatus *cfm)
182 {
183    uint8_t ret = ROK;
184    if(cfm->status != LCM_PRIM_OK)
185    {
186       DU_LOG("\nERROR  -->  DU_APP : Failed to process the request successfully");
187       ret = RFAILED;
188    }
189    return (ret); 
190 }
191
192 /**************************************************************************
193  * @brief Function to configure the Sctp Params during config Request
194  *
195  * @details
196  *
197  *      Function : duSctpCfgReq
198  * 
199  *      Functionality:
200  *           This function configures SCTP Params during the config Request
201  *     
202  * @param[in]  SctpParams sctpCfg, common structure which has all the configuration
203  * @param[in]  CmStatus cfm, Builds the cfm status and reason
204  *  buffer.
205  *
206  * @return ROK     - success
207  *         RFAILED - failure
208  *
209  ***************************************************************************/
210
211 uint8_t duSctpCfgReq(SctpParams sctpCfg)
212 {
213    uint8_t  ret = ROK;
214    CmStatus cfm;
215
216 #ifdef CALL_FLOW_DEBUG_LOG
217     DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_CFG_REQ_TO_SCTP\n");
218 #endif
219 /* Fill F1 Params */
220    f1Params.destIpAddr.ipV4Pres  = sctpCfg.cuIpAddr.ipV4Pres;
221    f1Params.destIpAddr.ipV4Addr  = sctpCfg.cuIpAddr.ipV4Addr;
222    f1Params.destPort             = sctpCfg.cuPort;
223    f1Params.itfState             = DU_SCTP_DOWN;
224    f1Params.srcPort              = sctpCfg.duPort[F1_INTERFACE];
225    f1Params.recvMsgSet           = ROK;
226    memset (&f1Params.sockFd, -1, sizeof(CmInetFd));
227    fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
228    fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
229
230 /* Fill RIC Params */
231    ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
232    ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
233    ricParams.destPort            = sctpCfg.ricPort;
234    ricParams.itfState            = DU_SCTP_DOWN;
235    ricParams.srcPort             = sctpCfg.duPort[E2_INTERFACE];
236    ricParams.recvMsgSet          = ROK;
237    memset (&ricParams.sockFd, -1, sizeof(CmInetFd));
238    fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
239    fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
240
241 /* Fill AddressList */
242    fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
243
244 /* Set polling to FALSE */
245    pollingState = FALSE;  
246
247 /* Fill Cfm Status */
248    cfm.status = LCM_PRIM_OK;
249    cfm.reason = LCM_REASON_NOT_APPL;
250
251    ret = duCheckReqStatus(&cfm);
252
253    return (ret);
254 }
255
256 /*******************************************************************
257  *
258  * @brief Fills the address List of the source Ip Address
259  *
260  * @details
261  *
262  *    Function : fillAddrLst
263  *
264  *    Functionality:
265  *       Fills the address List of source Ip Address
266  *
267  * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
268  * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
269  *
270  * @return ROK     - success
271  *         RFAILED - failure
272  *
273  ******************************************************************/
274
275 uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
276
277    addrLstPtr->count++;
278    addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
279    addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
280
281    return ROK;
282 }
283
284 /******************************************************************************
285  *
286  * @brief Fills the address List of the source Ip Address
287  *
288  * @details
289  *
290  *    Function : fillDestNetAddr
291  *
292  *    Functionality:
293  *       Fills the address List of destinatoion Ip Address
294  *
295  * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
296  * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
297  *
298  * @return ROK     - success
299  *         RFAILED - failure
300  *
301  *******************************************************************************/
302 uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
303 {
304    /* Filling destination address */
305    destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
306    destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
307    return ROK;
308 }
309
310 /******************************************************************************
311  *
312  * @brief Establishes the Assoc Req for the received interface type
313  *
314  * @details
315  *
316  *    Function : establishAssocReq
317  *
318  *    Functionality:
319  *       Eastablishes the Assoc Req for the received interface type
320  *
321  * @params[in] DuSctpDestCb *paramPtr
322  *
323  * @return ROK     - success
324  *         RFAILED - failure
325  *
326  *******************************************************************************/
327
328 uint8_t establishReq(DuSctpDestCb *paramPtr)
329 {
330    Pst pst;
331    uint8_t ret = ROK;
332    socket_type = CM_INET_STREAM;
333
334    if((ret = cmInetSocket(socket_type, &paramPtr->sockFd, IPPROTO_SCTP)) != ROK)
335    {
336       DU_LOG("\nERROR  -->  SCTP : Failed while opening a socket in ODU");
337    } 
338    else if((ret = cmInetSctpBindx(&paramPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
339    {
340       DU_LOG("\nERROR  -->  SCTP:  Failed during Binding in ODU");
341    }
342    else if((ret = sctpSetSockOpts(&paramPtr->sockFd)) != ROK)
343    {
344       DU_LOG("\nERROR  -->  SCTP : Failed to set Socket Opt in ODU");
345    }     
346    else
347    { 
348       if(ret != ROK)
349       {
350          DU_LOG("\nERROR  -->  SCTP : Failed while establishing Req at DU");
351          ret = RFAILED;
352       }
353       else 
354       {
355          ret = cmInetSctpConnectx(&paramPtr->sockFd, &paramPtr->destIpNetAddr, &paramPtr->destAddrLst, paramPtr->destPort);
356          /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
357          if(ret == 18)             
358          {
359            ret = ROK; 
360          }
361       }
362    }
363    if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
364    {
365       paramPtr->itfState = DU_SCTP_CONNECTING;
366    }
367
368    /* Post the EVTSTARTPOLL Msg */
369    if(!pollingState)
370    {
371       pollingState = TRUE;
372       duFillSctpPst(&pst, EVTSTARTPOLL);
373    }
374    
375    return (ret);
376 }
377
378 /******************************************************************************
379  *
380  * @brief Processes the assoc Req for the received interface type
381  *
382  * @details
383  *
384  *    Function : duSctpAssocReq
385  *
386  *    Functionality:
387  *       Processes the Assoc Req for the received interface type
388  *
389  * @params[in] itfType interface Type, 
390  * @params[in] cfm , represents the status of request
391  * 
392  * @return ROK     - success
393  *         RFAILED - failure
394  *
395  *******************************************************************************/
396
397 uint8_t duSctpAssocReq(uint8_t itfType)
398 {
399    uint8_t      ret = ROK;
400    CmStatus     cfm;
401    DuSctpDestCb *paramPtr = NULLP;
402
403 #ifdef CALL_FLOW_DEBUG_LOG
404    DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_ASSOC_REQ_TO_SCTP\n");
405 #endif
406  
407    DU_ALLOC(paramPtr, sizeof(DuSctpDestCb));
408    if(paramPtr == NULLP)
409    {
410       DU_LOG("\nERROR  -->  DU_APP : Failed to allocate memory");
411       return RFAILED;
412    }
413    switch(itfType)
414    {
415       case F1_INTERFACE:
416       {
417          paramPtr = &f1Params;
418          ret = establishReq(paramPtr);
419          break;
420       }
421       case E2_INTERFACE:
422       {
423          paramPtr = &ricParams;
424          ret = establishReq(paramPtr);
425          break;
426       }
427       default:
428       {
429          DU_LOG("\nERROR  -->  SCTP : Invalid Interface Type %d", itfType);
430          break;
431       }
432    }  
433    if(ret != ROK)
434    { 
435       DU_LOG("\nERROR  -->  SCTP : ASSOC Req Failed.");
436       cfm.status = LCM_PRIM_NOK;
437       cfm.reason = LCM_REASON_NOT_APPL;
438    }
439    else
440    {
441       cfm.status = LCM_PRIM_OK;
442       cfm.reason = LCM_REASON_NOT_APPL;
443    }
444    ret = duCheckReqStatus(&cfm);
445
446    return (ret);
447 }
448 /*******************************************************************
449  *
450  * @brief  Fills Pst struct for ENTSCTP
451  *
452  * @details
453  *
454  *    Function : duFillSctpPst
455  *
456  *    Functionality:
457  *       Fills Pst struct for ENTSCTP
458  *
459  * @params[in] 
460  * @return ROK     - success
461  *         RFAILED - failure
462  *
463  * ****************************************************************/
464 uint8_t duFillSctpPst(Pst *pst, Event event)
465 {
466    Buffer *mBuf;
467    if(ODU_GET_MSG_BUF(DFLT_REGION, DU_POOL, &mBuf) != ROK)
468    {
469       DU_LOG("\nERROR  -->  DU_APP : Failed to allocate memory");
470       return RFAILED;
471    }
472    memset(pst, 0, sizeof(Pst));
473    pst->srcEnt = (Ent)ENTDUAPP;
474    pst->srcInst = (Inst)DU_INST;
475    pst->srcProcId = DU_PROC;
476    pst->dstEnt = (Ent)ENTSCTP;
477    pst->dstInst = (Inst)SCTP_INST;
478    pst->dstProcId = pst->srcProcId;
479    pst->event = event;
480    pst->selector = ODU_SELECTOR_LC;
481    pst->pool= DU_POOL;
482    ODU_POST_TASK(pst, mBuf); 
483
484    return ROK;
485 }
486
487 /*******************************************************************
488  *
489  * @brief Sets socket options as per requirement
490  *
491  * @details
492  *
493  *    Function : sctpSetSockOpts
494  *
495  *    Functionality: 
496  *       Sets socket options as per requirement
497  *
498  * @params[in] sock_Fd  determines the sockFd to be set
499  * @return ROK     - success
500  *         RFAILED - failure
501  *
502  * ****************************************************************/
503 uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
504 {
505     uint8_t ret = ROK;
506     CmSctpEvent sctpEvent;
507
508    sctpEvent.dataIoEvent          = TRUE;
509    sctpEvent.associationEvent     = TRUE;
510    sctpEvent.addressEvent         = TRUE;
511    sctpEvent.sendFailureEvent     = TRUE;
512    sctpEvent.peerErrorEvent       = TRUE;
513    sctpEvent.shutdownEvent        = TRUE;
514    sctpEvent.partialDeliveryEvent = TRUE;
515    sctpEvent.adaptationLayerEvent = TRUE;
516
517    if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
518    {
519      ret = RFAILED;
520    }
521
522    return (ret);
523 }
524
525 /*******************************************************************
526  *
527  * @brief Post received data/notification to DU APP 
528  *
529  * @details
530  *
531  *    Function : sendToDuApp 
532  *
533  *    Functionality:
534  *         Post received data/notification to DU APP
535  *
536  * @params[in]  Message buffer
537  *              Message event
538  *
539  * @return ROK     - success
540  *         RFAILED - failure
541  *
542  * ****************************************************************/
543 void sendToDuApp(Buffer *mBuf, Event event)
544 {
545    Pst pst;
546    DU_LOG("\nDEBUG   -->  SCTP : Forwarding received message to duApp");
547    ODU_PRINT_MSG(mBuf, 0, 0);
548
549
550    memset(&(pst), 0, sizeof(Pst));
551    pst.srcEnt = (Ent)ENTSCTP;
552    pst.srcInst = (Inst)SCTP_INST;
553    pst.srcProcId = DU_PROC;
554    pst.dstEnt = (Ent)ENTDUAPP;
555    pst.dstInst = (Inst)DU_INST;
556    pst.dstProcId = pst.srcProcId;
557    pst.event = event;
558    pst.selector = ODU_SELECTOR_LC;
559    pst.pool= DU_POOL;
560    pst.region = DFLT_REGION;
561
562    if (ODU_POST_TASK(&pst, mBuf) != ROK)
563    {
564       DU_LOG("\nERROR  -->  SCTP : ODU_POST_TASK failed in duReadCfg");
565    }
566 }
567
568 /*******************************************************************
569  *
570  * @brief Handles an SCTP notification message
571  *
572  * @Sending *
573  *    Function : sctpNtfyHdlr
574  *
575  *    Functionality:
576  *      Handles an SCTP notification message
577  *
578  * @params[in] Notify message
579  *
580  * @return ROK     - success
581  *         RFAILED - failure
582  *
583  * ****************************************************************/
584 uint8_t sctpNtfyHdlr(CmInetSctpNotification *ntfy, uint8_t *itfState, uint8_t interface)
585 {
586    Pst pst;
587
588    switch(ntfy->header.nType)
589    {
590       case CM_INET_SCTP_ASSOC_CHANGE :
591          DU_LOG("\nDEBUG   -->  SCTP : Assoc change notification received");
592          switch(ntfy->u.assocChange.state)
593          {
594             case CM_INET_SCTP_COMM_UP:
595                DU_LOG("INFO   -->  Event : COMMUNICATION UP");
596                *itfState = DU_SCTP_UP;
597                break;
598             case CM_INET_SCTP_COMM_LOST:
599                DU_LOG("INFO   -->  Event : COMMUNICATION LOST");
600                *itfState = DU_SCTP_DOWN;
601                break;
602             case CM_INET_SCTP_RESTART:
603                DU_LOG("INFO   -->  Event : SCTP RESTART");
604                *itfState = DU_SCTP_DOWN;
605                break;
606             case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
607                DU_LOG("INFO   -->  Event : SHUTDOWN COMPLETE");
608                *itfState = DU_SCTP_DOWN;
609                break;
610             case CM_INET_SCTP_CANT_STR_ASSOC:
611                DU_LOG("INFO   -->  Event : CANT START ASSOC");
612                *itfState = DU_SCTP_DOWN;
613                break;
614             default:
615                DU_LOG("\nERROR  -->  Invalid event %d", ntfy->u.assocChange.state);
616                break;
617          }
618          break;
619       case CM_INET_SCTP_PEER_ADDR_CHANGE :
620          DU_LOG("\nINFO   -->  SCTP : Peer Address Change notificarion received");
621          /* Need to add handler */
622          break;
623       case CM_INET_SCTP_REMOTE_ERROR :
624          DU_LOG("\nINFO   -->  SCTP : Remote Error notification received");
625          break;
626       case CM_INET_SCTP_SEND_FAILED :
627          DU_LOG("\nINFO   -->  SCTP : Send Failed notification received\n");
628          break;
629       case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
630          {
631             DU_LOG("\nINFO   -->  SCTP : Shutdown Event notification received\n");
632             *itfState = DU_SCTP_DOWN;
633             switch(interface)
634             {
635                case F1_INTERFACE:
636                   break;
637                case E2_INTERFACE:
638                   removeE2NodeInformation();
639                   break;
640                default:
641                   exit(0);
642             }
643             break;
644          }
645       case CM_INET_SCTP_ADAPTATION_INDICATION :
646          DU_LOG("\nINFO   -->  SCTP : Adaptation Indication received\n");
647          break;
648       case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
649          DU_LOG("\nINFO   -->  SCTP : Partial Delivery Event received\n");
650          break;
651       default:
652          DU_LOG("\nERROR  -->  SCTP : Invalid sctp notification type %d", ntfy->header.nType);
653          break;
654    }
655
656    /* Pack notification and send to APP */
657    DU_LOG("\nDEBUG   -->  SCTP : Forwarding received message to duApp");
658     
659    memset(&(pst), 0, sizeof(Pst));
660    pst.srcEnt = (Ent)ENTSCTP;
661    pst.srcInst = (Inst)SCTP_INST;
662    pst.srcProcId = DU_PROC;
663    pst.dstEnt = (Ent)ENTDUAPP;
664    pst.dstInst = (Inst)DU_INST;
665    pst.dstProcId = pst.srcProcId;
666    pst.event = EVENT_SCTP_NTFY;
667    pst.selector = ODU_SELECTOR_LC;
668    pst.pool= DU_POOL;
669    pst.region = DU_APP_MEM_REGION;
670    
671    if(cmPkSctpNtfy(&pst, ntfy) != ROK)
672    {
673       DU_LOG("\nERROR  -->  SCTP : Failed to pack SCTP notification");
674       return RFAILED;
675    }
676    return ROK;
677 }
678
679 /*******************************************************************
680  *
681  * @brief checks for valid readFd and process the InetSctpRecvMsg
682  * during polling 
683  *
684  * @details
685  *
686  *    Function : processPolling
687  *
688  *    Functionality:
689  *         checks for valid readFd and process the InetSctpRecvMsg
690  *         during polling
691  *
692  * @params[in]  Params required for polling
693  * @params[in]  SockFd for file descriptor
694  * @params[in]  timeoutPtr indicates the timeout value
695  * @params[in]  MemInfo, recvMsgSet
696  *
697  * @return ROK     - success
698  *         RFAILED - failure
699  *
700  * ****************************************************************/
701
702 uint8_t  processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo, bool recvMsgSet)
703 {
704    uint8_t ret = ROK;
705    CM_INET_FD_SET(sockFd, &pollParams->readFd);
706    ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, (int16_t *)&pollParams->numFd);
707    if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
708    {
709       CM_INET_FD_CLR(sockFd, &pollParams->readFd);
710       ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
711         
712       if(ret != ROK)
713       {
714          DU_LOG("\nERROR   -->  SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
715          recvMsgSet = RFAILED;
716  
717       }
718       else
719       {
720 #ifdef CALL_FLOW_DEBUG_LOG
721          if(pollParams->port == f1Params.destPort)
722             DU_LOG("\nCall Flow: CU -> ENTSCTP : EVENT_SCTP_MSG\n");
723          else
724             DU_LOG("\nCall Flow: RIC -> ENTSCTP : EVENT_SCTP_MSG\n");
725 #endif
726          if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
727          {
728             if(pollParams->port == f1Params.destPort)
729             {
730                f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
731                DU_LOG("\nDEBUG   -->  SCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
732                ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState, F1_INTERFACE);
733             }
734             else if(pollParams->port == ricParams.destPort)
735             {
736                ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
737                DU_LOG("\nDEBUG   -->  SCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
738                ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState, E2_INTERFACE);
739             }
740             else
741             {
742                DU_LOG("\nERROR  -->  SCTP : Failed to fill AssocId\n");
743                return RFAILED;
744             }
745             if(ret != ROK)
746             {
747                DU_LOG("\nERROR  -->  SCTP : Failed to process sctp notify msg\n");
748             }
749          }
750          else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
751          {  
752             sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
753          }
754          else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
755          {  
756             sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
757          }
758
759          else
760          {
761             ODU_PUT_MSG_BUF(pollParams->mBuf);
762          }
763       }
764   }
765   return ROK;
766 }
767 /*******************************************************************
768  *
769  * @brief Receives message on the socket
770  *
771  * @details
772  *
773  *    Function : sctpSockPoll
774  *
775  *    Functionality:
776  *      Receives message on the socket
777  *
778  * @params[in] 
779  * @return ROK     - success
780  *         RFAILED - failure
781  *
782  * ****************************************************************/
783 uint8_t sctpSockPoll()
784 {
785    uint8_t  ret       = ROK;
786    uint32_t timeout;
787    uint32_t *timeout_Ptr;
788    CmInetMemInfo memInfo;
789    sctpSockPollParams f1PollParams, e2PollParams;
790
791    memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
792    memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
793
794    if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
795    {
796       /* blocking */
797       timeout_Ptr = NULLP;
798    }
799    else
800    {
801       /* non-blocking */
802       timeout = 0;
803       timeout_Ptr = &timeout;
804    }
805    memInfo.region = DU_APP_MEM_REGION;
806    memInfo.pool   = DU_POOL;
807
808    CM_INET_FD_ZERO(&f1PollParams.readFd);
809    CM_INET_FD_ZERO(&e2PollParams.readFd);
810
811    DU_LOG("\nINFO   -->  SCTP : Polling started at DU\n");
812    while(true)
813    {
814       if(f1Params.itfState)
815       {
816          if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
817          {
818             DU_LOG("\nERROR  -->  SCTP : Failed to RecvMsg for F1\n");
819          }
820       }
821       if(ricParams.itfState)
822       {
823          if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
824          {
825             DU_LOG("\nERROR  -->  SCTP : Failed to RecvMsg for E2\n");
826          }
827       }
828    };
829    return (ret);
830 }/* End of sctpSockPoll() */
831
832 /*******************************************************************
833  *
834  * @brief Send message on SCTP socket
835  *
836  * @details
837  *
838  *    Function : sctpSend 
839  *
840  *    Functionality:
841  *        Send message on SCTP socket
842  *
843  * @params[in] 
844  * @return ROK     - success
845  *         RFAILED - failure
846  *
847  * ****************************************************************/
848 uint8_t sctpSend(Buffer *mBuf, uint8_t itfType)
849 {
850    uint8_t          ret =0;
851    MsgLen           len =0;          /* number of actually sent octets */
852    CmInetMemInfo    memInfo;                        
853    
854    memInfo.region = DU_APP_MEM_REGION;               
855    memInfo.pool   = DU_POOL;
856    
857 #ifdef CALL_FLOW_DEBUG_LOG
858    if(itfType == F1_INTERFACE)
859    {
860       DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_F1AP_MSG_TO_SCTP\n");
861    }
862    else
863    {
864       DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_E2AP_MSG_TO_SCTP\n");
865    }
866 #endif
867
868    if(itfType == F1_INTERFACE)
869    {
870       DU_LOG("\nDEBUG  --> SCTP : sending the message to CU");
871 #ifdef CALL_FLOW_DEBUG_LOG
872       DU_LOG("\nCall Flow: ENTSCTP -> CU : EVENT_F1AP_MSG_TO_CU\n");
873 #endif
874       ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
875    }
876
877    if(itfType == E2_INTERFACE)
878    {
879       DU_LOG("\nDEBUG  -->  SCTP : sending the message to ric");
880 #ifdef CALL_FLOW_DEBUG_LOG
881       DU_LOG("\nCall Flow: ENTSCTP -> RIC : EVENT_E2AP_MSG_TO_RIC\n");
882 #endif
883       ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
884    }
885
886    if(ret != ROK && ret != RWOULDBLOCK)
887    {
888       DU_LOG("\nERROR  -->  SCTP : Failed sending the message");
889       return RFAILED;
890    }
891
892    return ROK;
893 } /* End of sctpSend */
894
895 /**********************************************************************
896          End of file
897 **********************************************************************/