Changes for Multi-UE support and DL_MSG scheduling using K0 and K1 [Issue-ID: ODUHIGH...
[o-du/l2.git] / src / phy_stub / phy_stub_thread_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 handles slot indication */
20
21 #include "common_def.h"
22 #include "phy_stub_utils.h"
23 #ifdef INTEL_FAPI
24 #include "fapi.h"
25 #include "fapi_vendor_extension.h"
26 #endif
27 #include "phy_stub.h"
28
29 uint8_t l1SendUlUserData();
30 uint8_t l1SendStatusPdu();
31 uint16_t l1BuildAndSendSlotIndication();
32 uint16_t l1BuildAndSendStopInd();
33 pthread_t thread = 0;
34
35 /*******************************************************************
36  *
37  * @brief Generates slot indications
38  *
39  * @details
40  *
41  *    Function : GenerateTicks
42  *
43  *    Functionality: Generates slot indications
44  *
45  * @params[in] 
46  * @return ROK     - success
47  *         RFAILED - failure
48  *
49  * ****************************************************************/
50 void GenerateTicks()
51 {
52 #ifdef NR_TDD
53    float     milisec = 0.5;        /* 0.5ms */
54 #else
55    float     milisec = 1;          /* 1ms */
56 #endif
57    struct timespec req = {0};
58    uint8_t ratio = 2;
59
60    slotIndicationStarted = true;
61    req.tv_sec = 0;
62
63    /* Currently the code takes longer that one slot indication to execute.
64     * Hence, multiplying slot time interval by 2 in order to give enough time 
65     * for L2 to complete one slot processing.
66     * The ratio must be removed once code optimization is complete */
67    req.tv_nsec = milisec * 1000000L * ratio;
68
69    DU_LOG("\nPHY_STUB : GenerateTicks : Starting to generate slot indications");
70
71    while(slotIndicationStarted)
72    {
73       clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL); 
74       /* Send Slot indication indication to lower mac */
75       if(l1BuildAndSendSlotIndication() != ROK)
76       {
77          DU_LOG("\nERROR  --> PHY_STUB : GenerateTicks(): Failed to build and send Slot Indication");
78          return;
79       }
80    }
81
82    DU_LOG("\nINFO  --> PHY_STUB : Slot indication stopped");
83
84    /* Initialize all global variables */
85    sfnValue = 0;
86    slotValue = 0;
87    memset(&ueDb, 0, sizeof(UeDb));
88
89    /* Send Stop indication to MAC */
90    sleep(1);
91    l1BuildAndSendStopInd();
92 }
93
94 /*******************************************************************
95  *
96  * @brief Create/cancel thread for generating slot indication 
97  *
98  * @details
99  *
100  *    Function : l1HdlSlotIndicaion
101  *
102  *    Functionality: Create/cancel thread for generating slot indication
103  *
104  * @params[in] 
105  * @return ROK     - success
106  *         RFAILED - failure
107  *
108  * ****************************************************************/
109 void l1HdlSlotIndicaion(bool stopSlotInd)
110 {
111    Pst pst;
112    Buffer *mBuf = NULLP;
113
114    if(!stopSlotInd)
115    {
116       DU_LOG("\nPHY_STUB: Sending start slot indication event to self");
117       memset(&pst, 0, sizeof(Pst));
118       FILL_PST_PHY_TO_PHY(pst, EVT_PHY_START_SLOT_IND);
119       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
120       ODU_POST_TASK(&pst, mBuf);
121    }
122    else
123    {
124       slotIndicationStarted = false;
125    }
126 }
127
128 /*******************************************************************
129  *
130  * @brief Handles Console input
131  *
132  * @details
133  *
134  *    Function : l1ConsoleHandler
135  *
136  *    Functionality: Handles Console input
137  *
138  * @params[in]
139  * @return ROK     - success
140  *         RFAILED - failure
141  *
142  * ****************************************************************/
143 void *l1ConsoleHandler(void *args)
144 {
145    char ch;
146    uint8_t drbIdx = 0;
147
148    while(true)
149    {
150       /* Send UL user data to DU when user enters 'd' on console */
151       if((ch = getchar()) == 'd')
152       {
153          /* Start Pumping data from PHY stub to DU */
154          for(drbIdx = 0; drbIdx < NUM_DRB_TO_PUMP_DATA; drbIdx++) //Number of DRB times the loop will run
155          {
156             DU_LOG("\nDEBUG  --> PHY STUB: Sending UL User Data[DrbId:%d]",drbIdx);
157             l1SendUlUserData(drbIdx);
158          }
159       }
160       else if((ch = getchar()) == 'c')
161       {
162          /* Send Control PDU from PHY stub to DU */
163           DU_LOG("\nDEBUG  --> PHY STUB: Sending Status PDU");
164                l1SendStatusPdu();
165       }
166       DU_LOG("\n");
167       continue;
168    }
169 }
170
171 /*******************************************************************
172  *
173  * @brief Creates thread for handling console input 
174  *
175  * @details
176  *
177  *    Function : l1StartConsoleHandler
178  *
179  *    Functionality: Creates thread for handling console input
180  *
181  * @params[in] 
182  * @return ROK     - success
183  *         RFAILED - failure
184  *
185  * ****************************************************************/
186 void l1StartConsoleHandler()
187 {
188    uint8_t retVal;
189    pthread_t conThrdId;
190    pthread_attr_t attr;
191
192    /* Start thread to receive console input */
193    pthread_attr_init(&attr);
194    pthread_attr_setstacksize(&attr, (size_t)NULLD);
195    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
196    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
197    retVal = pthread_create(&conThrdId, &attr, l1ConsoleHandler, NULLP);
198    if(retVal != 0)
199    {
200       DU_LOG("\nERROR  -->  PHY STUB : Thread creation failed. Cause %d", retVal);
201    }
202    pthread_attr_destroy(&attr);
203
204 }
205
206 /**********************************************************************
207          End of file
208 **********************************************************************/