b26e5045417dd04189ef0d334a4f934f61d34a02
[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    /* The below variable is taken for sending specific number of UL Packets  
148     * For sendind 4500 Ul packets for three UEs the calculation of
149     * [counter * NUM_DRB_TO_PUMP_DATA * MAX_NUM_UE * NUM_UL_PACKETS] must be equal to 4500 */
150    uint32_t counter=500; 
151
152    while(true)
153    {
154       /* Send UL user data to DU when user enters 'd' on console */
155       ch = getchar();
156       if(ch == 'd')
157       {
158          while(counter)
159          {
160             /* Start Pumping data from PHY stub to DU */
161             for(ueIdx=0; ueIdx < MAX_NUM_UE; ueIdx++)
162             {
163                for(drbIdx = 0; drbIdx < NUM_DRB_TO_PUMP_DATA; drbIdx++) //Number of DRB times the loop will run
164                {
165                   DU_LOG("\nDEBUG  --> PHY STUB: Sending UL User Data[DrbId:%d] for UEId %d\n",drbIdx,ueIdx);
166                   l1SendUlUserData(drbIdx,ueIdx);
167                   /* TODO :- sleep(1) will be removed once we will be able to
168                    * send continuous data packet */
169                   sleep(1);
170                }
171             }
172             counter--;
173          }
174       }
175       else if(ch =='c')
176       {
177          /* Send Control PDU from PHY stub to DU */
178          DU_LOG("\nDEBUG  --> PHY STUB: Sending Status PDU");
179          l1SendStatusPdu();
180       }
181       else if(ch == 'b')
182       {
183          memset(lcgBS, 0, (MAX_NUM_LOGICAL_CHANNEL_GROUPS * sizeof(LcgBufferSize)));
184          /* Send Control PDU from PHY stub to DU */
185          ch1 = getchar();
186          for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
187          {
188             if(ch1 == 'l')
189             {
190                for(lcgIdx = 0; lcgIdx < NUM_DRB_TO_PUMP_DATA; lcgIdx++)
191                {
192                   lcgBS[lcgIdx].lcgId = MIN_DRB_LCID + lcgIdx;
193                   lcgBS[lcgIdx].bsIdx = lcgIdx + 1;
194                }
195                l1BuildAndSendBSR(ueIdx, LONG_BSR, lcgBS);
196             }
197             else if(ch1 == 's')
198             {
199                lcgIdx = 0;
200
201                lcgBS[lcgIdx].lcgId = MIN_DRB_LCID + lcgIdx;
202                lcgBS[lcgIdx].bsIdx = lcgIdx + 1;
203                l1BuildAndSendBSR(ueIdx, SHORT_BSR, lcgBS);
204             }
205          }
206       }
207       DU_LOG("\n");
208       continue;
209    }
210 }
211
212 /*******************************************************************
213  *
214  * @brief Creates thread for handling console input 
215  *
216  * @details
217  *
218  *    Function : l1StartConsoleHandler
219  *
220  *    Functionality: Creates thread for handling console input
221  *
222  * @params[in] 
223  * @return ROK     - success
224  *         RFAILED - failure
225  *
226  * ****************************************************************/
227 void l1StartConsoleHandler()
228 {
229    uint8_t retVal;
230    pthread_t conThrdId;
231    pthread_attr_t attr;
232
233    /* Start thread to receive console input */
234    pthread_attr_init(&attr);
235    pthread_attr_setstacksize(&attr, (size_t)NULLD);
236    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
237    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
238    retVal = pthread_create(&conThrdId, &attr, l1ConsoleHandler, NULLP);
239    if(retVal != 0)
240    {
241       DU_LOG("\nERROR  -->  PHY STUB : Thread creation failed. Cause %d", retVal);
242    }
243    pthread_attr_destroy(&attr);
244
245 }
246
247 /**********************************************************************
248          End of file
249 **********************************************************************/