1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /* This file handles slot indication */
21 #include "common_def.h"
22 #include "phy_stub_utils.h"
25 #include "fapi_vendor_extension.h"
28 #include "mac_sch_interface.h"
30 extern uint16_t l1BuildAndSendBSR(uint8_t ueIdx, BsrType bsrType,\
31 LcgBufferSize lcgBsIdx[MAX_NUM_LOGICAL_CHANNEL_GROUPS]);
35 /*******************************************************************
37 * @brief Generates slot indications
41 * Function : GenerateTicks
43 * Functionality: Generates slot indications
46 * @return ROK - success
49 * ****************************************************************/
53 float milisec = 0.5; /* 0.5ms */
55 float milisec = 1; /* 1ms */
57 struct timespec req = {0};
60 slotIndicationStarted = true;
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;
69 DU_LOG("\nPHY_STUB : GenerateTicks : Starting to generate slot indications");
71 while(slotIndicationStarted)
73 clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL);
74 /* Send Slot indication indication to lower mac */
75 if(l1BuildAndSendSlotIndication() != ROK)
77 DU_LOG("\nERROR --> PHY_STUB : GenerateTicks(): Failed to build and send Slot Indication");
82 DU_LOG("\nINFO --> PHY_STUB : Slot indication stopped");
84 /* Initialize all global variables */
87 memset(&phyDb.ueDb, 0, sizeof(UeDb));
89 /* Send Stop indication to MAC */
91 l1BuildAndSendStopInd();
94 /*******************************************************************
96 * @brief Create/cancel thread for generating slot indication
100 * Function : l1HdlSlotIndicaion
102 * Functionality: Create/cancel thread for generating slot indication
105 * @return ROK - success
108 * ****************************************************************/
109 void l1HdlSlotIndicaion(bool stopSlotInd)
112 Buffer *mBuf = NULLP;
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);
124 slotIndicationStarted = false;
128 /*******************************************************************
130 * @brief Handles Console input
134 * Function : l1ConsoleHandler
136 * Functionality: Handles Console input
139 * @return ROK - success
142 * ****************************************************************/
143 void *l1ConsoleHandler(void *args)
146 uint8_t drbIdx = 0, lcgIdx = 0, ueIdx = 0;
147 LcgBufferSize lcgBS[MAX_NUM_LOGICAL_CHANNEL_GROUPS];
148 /* The below variable is taken for sending specific number of UL Packets
149 * For sendind 4500 Ul packets for three UEs the calculation of
150 * [counter * NUM_DRB_TO_PUMP_DATA * MAX_NUM_UE * NUM_UL_PACKETS] must be equal to 4500 */
151 uint32_t counter=500;
155 /* Send UL user data to DU when user enters 'd' on console */
161 /* Start Pumping data from PHY stub to DU */
162 for(ueIdx=0; ueIdx < MAX_NUM_UE; ueIdx++)
164 for(drbIdx = 0; drbIdx < NUM_DRB_TO_PUMP_DATA; drbIdx++) //Number of DRB times the loop will run
166 DU_LOG("\nDEBUG --> PHY STUB: Sending UL User Data[DrbId:%d] for UEIdx %d\n",drbIdx,ueIdx);
167 l1SendUlUserData(drbIdx,ueIdx);
168 /* TODO :- sleep(1) will be removed once we will be able to
169 * send continuous data packet */
178 /* Send Control PDU from PHY stub to DU */
179 DU_LOG("\nDEBUG --> PHY STUB: Sending Status PDU");
184 memset(lcgBS, 0, (MAX_NUM_LOGICAL_CHANNEL_GROUPS * sizeof(LcgBufferSize)));
185 /* Send Control PDU from PHY stub to DU */
187 for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
191 for(lcgIdx = 0; lcgIdx < NUM_DRB_TO_PUMP_DATA; lcgIdx++)
193 lcgBS[lcgIdx].lcgId = MIN_DRB_LCID + lcgIdx;
194 lcgBS[lcgIdx].bsIdx = lcgIdx + 1;
196 l1BuildAndSendBSR(ueIdx, LONG_BSR, lcgBS);
202 lcgBS[lcgIdx].lcgId = MIN_DRB_LCID + lcgIdx;
203 lcgBS[lcgIdx].bsIdx = lcgIdx + 1;
204 l1BuildAndSendBSR(ueIdx, SHORT_BSR, lcgBS);
213 /*******************************************************************
215 * @brief Creates thread for handling console input
219 * Function : l1StartConsoleHandler
221 * Functionality: Creates thread for handling console input
224 * @return ROK - success
227 * ****************************************************************/
228 void l1StartConsoleHandler()
230 uint8_t retVal, threadIdx;
231 pthread_t conThrdId[NUM_THREADS];
234 /* Start thread to receive console input */
235 pthread_attr_init(&attr);
236 pthread_attr_setstacksize(&attr, (size_t)NULLD);
237 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
238 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
239 for(threadIdx =0 ; threadIdx <NUM_THREADS; threadIdx++)
243 retVal = pthread_create(&conThrdId[threadIdx], &attr, l1ConsoleHandler, NULLP);
247 retVal = pthread_create(&conThrdId[threadIdx], &attr, establishConnectionWithPeerL1, NULLP);
252 DU_LOG("\nERROR --> PHY STUB : Thread creation failed. Cause %d", retVal);
255 pthread_attr_destroy(&attr);
258 /*******************************************************************
260 * @brief function reads the recevied information send by peer PHY
264 * Function : receiveMsgFromPeerL1
266 * Functionality: This function reads the recevied information
267 * and prints at console
270 * @return ROK - success
273 * ****************************************************************/
274 void receiveMsgFromPeerL1()
278 char buffer[1024] = {0};
280 if(read(socket_fd, buffer, 1024)>0)
283 DU_LOG("%s\n",buffer);
284 if (strncmp("HANDOVER_IN_PROGRESS", buffer, 19) == 0)
286 DU_LOG("\nINFO --> PHY_STUB : Communication completed in between the source and destination PHY\n");
287 //TODO: Trigger for other handover process in target PHY
293 /*******************************************************************
295 * @brief function build the message which need to send to target PHY
301 * Functionality: function build the message which need to send
304 * @return ROK - success
307 * ****************************************************************/
310 char *msgToDestinationPhy = "HANDOVER_IN_PROGRESS";
312 send(socket_fd, msgToDestinationPhy , strlen(msgToDestinationPhy) , 0 );
315 /*******************************************************************
317 * @brief This function handles the server functionalities like
318 * binding socket, listen and accept
322 * Function : startL1AsServer
324 * Functionality: This function handles the server functionalities like
325 * binding socket, listen and accept
327 * @params[in] struct sockaddr_in serverPhy, struct sockaddr_in
329 * @return ROK - success
332 * ****************************************************************/
333 int8_t startL1AsServer(struct sockaddr_in serverPhy, struct sockaddr_in clientPhy)
335 int addrlen= sizeof(struct sockaddr_in);
337 if (bind(socket_fd, (struct sockaddr *)&serverPhy, sizeof(struct sockaddr_in))<0)
339 DU_LOG("\nERROR --> PHY_STUB : bind failed");
342 if (listen(socket_fd, 3) < 0)
344 DU_LOG("\nERROR --> PHY_STUB : listen failed");
349 if ((socket_fd = accept(socket_fd, (struct sockaddr *)&clientPhy,
350 (socklen_t*)&addrlen))<0)
352 DU_LOG("\nINFO --> PHY_STUB : Server is waiting");
356 DU_LOG("\nINFO --> PHY_STUB : Server Connected");
363 /*******************************************************************
365 * @brief this function includes all the functionalities of client side
366 * like binding the client side socket and connecting to the server
370 * Function : startL1AsClient
372 * Functionality: this function includes all the functionalities of client
373 * side like binding the client side socket and connecting to the server
376 * @return ROK - success
379 * ****************************************************************/
380 int8_t startL1AsClient(struct sockaddr_in serverPhy, struct sockaddr_in destinationPhy)
382 if (bind(socket_fd, (struct sockaddr *)&serverPhy, sizeof(struct sockaddr_in ))<0)
384 DU_LOG("\nERROR --> PHY_STUB : bind failed");
390 if (connect(socket_fd, (struct sockaddr *)&destinationPhy, sizeof(struct sockaddr_in)) < 0)
392 DU_LOG("\nERROR --> PHY_STUB : Connection Failed");
396 DU_LOG("\nINFO --> PHY_STUB : Client connected to sever");
404 /*******************************************************************
406 * @brief this function creates the socket for commincation between source and
411 * Function : establishConnectionWithPeerL1
413 * Functionality: creates the socket for commincation between source and
414 * target PHY, allocated the ip addresses and sends the request for connection
415 * establisshement and sends the information to each other
418 * @return ROK - success
421 * ****************************************************************/
422 void *establishConnectionWithPeerL1(void *args)
425 struct sockaddr_in sourcePhy, destinationPhy;
427 if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
429 DU_LOG("\nERROR --> PHY_STUB : socket failed");
433 sourcePhy.sin_family = AF_INET;
434 sourcePhy.sin_port = htons(phyDb.ipCfgInfo.portNumber);
435 sourcePhy.sin_addr.s_addr = phyDb.ipCfgInfo.sourceDu;
437 destinationPhy.sin_family = AF_INET;
438 destinationPhy.sin_port = htons(phyDb.ipCfgInfo.portNumber);
439 destinationPhy.sin_addr.s_addr = phyDb.ipCfgInfo.destinationDu;
443 if(startL1AsServer(sourcePhy, destinationPhy) != ROK)
445 DU_LOG("\nERROR --> PHY_STUB : Failed to start server");
451 if(startL1AsClient(sourcePhy, destinationPhy) != ROK)
453 DU_LOG("\nERROR --> PHY_STUB : Failed to start client");
458 DU_LOG("\nINFO --> PHY_STUB : Connection established");
459 receiveMsgFromPeerL1();
462 /**********************************************************************
464 **********************************************************************/