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