Merge "Fixes in current code for Multi-UE support [Issue-ID: ODUHIGH-354]"
[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    int     milisec = 0.5;        /* 0.5ms */
54 #else
55    int     milisec = 1;          /* 1ms */
56 #endif
57    struct timespec req = {0};
58
59    slotIndicationStarted = true;
60    req.tv_sec = 0;
61    req.tv_nsec = milisec * 1000000L;
62
63    DU_LOG("\nPHY_STUB : GenerateTicks : Starting to generate slot indications");
64
65    while(slotIndicationStarted)
66    {
67       clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL); 
68       /* Send Slot indication indication to lower mac */
69       if(l1BuildAndSendSlotIndication() != ROK)
70       {
71          DU_LOG("\nERROR  --> PHY_STUB : GenerateTicks(): Failed to build and send Slot Indication");
72          return;
73       }
74    }
75
76    DU_LOG("\nINFO  --> PHY_STUB : Slot indication stopped");
77
78    /* Initialize all global variables */
79    sfnValue = 0;
80    slotValue = 0;
81    memset(&ueDb, 0, sizeof(UeDb));
82
83    /* Send Stop indication to MAC */
84    sleep(1);
85    l1BuildAndSendStopInd();
86 }
87
88 /*******************************************************************
89  *
90  * @brief Create/cancel thread for generating slot indication 
91  *
92  * @details
93  *
94  *    Function : l1HdlSlotIndicaion
95  *
96  *    Functionality: Create/cancel thread for generating slot indication
97  *
98  * @params[in] 
99  * @return ROK     - success
100  *         RFAILED - failure
101  *
102  * ****************************************************************/
103 void l1HdlSlotIndicaion(bool stopSlotInd)
104 {
105    Pst pst;
106    Buffer *mBuf = NULLP;
107
108    if(!stopSlotInd)
109    {
110       DU_LOG("\nPHY_STUB: Sending start slot indication event to self");
111       memset(&pst, 0, sizeof(Pst));
112       FILL_PST_PHY_TO_PHY(pst, EVT_PHY_START_SLOT_IND);
113       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
114       ODU_POST_TASK(&pst, mBuf);
115    }
116    else
117    {
118       slotIndicationStarted = false;
119    }
120 }
121
122 /*******************************************************************
123  *
124  * @brief Handles Console input
125  *
126  * @details
127  *
128  *    Function : l1ConsoleHandler
129  *
130  *    Functionality: Handles Console input
131  *
132  * @params[in]
133  * @return ROK     - success
134  *         RFAILED - failure
135  *
136  * ****************************************************************/
137 void *l1ConsoleHandler(void *args)
138 {
139    char ch;
140    while(true)
141    {
142       /* Send UL user data to DU when user enters 'd' on console */
143       if((ch = getchar()) == 'd')
144       {
145          /* Start Pumping data from PHY stub to DU */
146          DU_LOG("\nDEBUG  --> PHY STUB: Sending UL User Data");
147          l1SendUlUserData();
148       }
149       else if((ch = getchar()) == 'c')
150       {
151          /* Send Control PDU from PHY stub to DU */
152          DU_LOG("\nDEBUG  --> PHY STUB: Sending Status PDU");
153          l1SendStatusPdu();
154       }
155    }
156 }
157
158 /*******************************************************************
159  *
160  * @brief Creates thread for handling console input 
161  *
162  * @details
163  *
164  *    Function : l1StartConsoleHandler
165  *
166  *    Functionality: Creates thread for handling console input
167  *
168  * @params[in] 
169  * @return ROK     - success
170  *         RFAILED - failure
171  *
172  * ****************************************************************/
173 void l1StartConsoleHandler()
174 {
175    uint8_t retVal;
176    pthread_t conThrdId;
177    pthread_attr_t attr;
178
179    /* Start thread to receive console input */
180    pthread_attr_init(&attr);
181    pthread_attr_setstacksize(&attr, (size_t)NULLD);
182    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
183    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
184    retVal = pthread_create(&conThrdId, &attr, l1ConsoleHandler, NULLP);
185    if(retVal != 0)
186    {
187       DU_LOG("\nERROR  -->  PHY STUB : Thread creation failed. Cause %d", retVal);
188    }
189    pthread_attr_destroy(&attr);
190
191 }
192
193 /**********************************************************************
194          End of file
195 **********************************************************************/