2bff55f184e3c50bb93bf8b7129acf7645c1a2e0
[scp/ocu/5gnr.git] / Cu / CuUp / Pdcp / PdcpUp / Src / pdcpuUlDataProc.c
1 /******************************************************************************
2 ###############################################################################
3 #   Copyright (c) [2017-2020] [ICT/CAS]                                        #
4 #   Licensed under the ORAN Software License v1.0 (License)             #
5 ###############################################################################
6 ******************************************************************************/
7 #include "vos_types.h"
8 #include "vos_linklist.h"
9 #include "vos_module.h"
10 #include "vos_sem.h"
11 #include "cuupCommon.h"
12 #include "msgb.h"
13 #include "bitsPack.h"
14 #include "pdcpu.h"
15 #include "gnbCommon.h"
16 #include "pdcpCommon.h"
17 #include "pdcpuCore.h"
18 #include "cuupUeIdTransf.h"
19
20 #include "cuupTest.h"
21
22 extern PdcpuUeInfo_t *gPdcpuUeInfo[MAX_UE_NUM];
23 extern CuupUeIdxTable_t gPdcpuUeIdxTable;
24 extern ULONG gPdcpuModuleId;
25 ULONG gPdcpuUlModuleId;
26
27 extern INT32 sdapUlDataProc(UINT64 upE1apId, UINT64 sessId, UINT64 drbId, MsgbBuff_t *pMsgBuff);
28 INT32 pdcpuUlInit(ULONG userModuleId)
29 {
30         gPdcpuUlModuleId = userModuleId;
31         
32         return VOS_OK;
33 }
34
35 INT32 pdcpuUnpackPduHead(MsgbBuff_t *pMsgBuff, PdcpSnSize_e snSize, 
36                                                         UINT8 *pPdutype, UINT32 *pSn)
37 {       
38         BitOpt_t bit;
39         UINT8 *pMsgHead = msgbData(pMsgBuff);
40         UINT16 pduLen = msgbDataUsedLen(pMsgBuff);
41
42         initBits(&bit, pMsgHead, pduLen, 0);
43         *pPdutype = unpackBits(&bit, 1);
44         if(PDCP_CTRL_PDU == *pPdutype)
45         {               
46                 //*pCtrlPduType = unpackBits(&bit, 3);
47
48                 return 0;
49                 
50         }else if(PDCP_DATA_PDU == *pPdutype)
51         {               
52                 if(LEN12BITS == snSize)
53                 {
54                         /*************D/C + 3R + PDCP SN = 16bits*************/
55                         skipBits(&bit, 3, BIT_UNPACKED);
56                         *pSn = unpackBits(&bit, LEN12BITS);
57                         
58                         return PDCP_SN_12_HEAD_LEN;
59                         
60                 }else if(LEN18BITS == snSize)
61                 {
62                         /*************D/C + 5R + PDCP SN =24bits*************/
63                         skipBits(&bit, 5, BIT_UNPACKED);
64                         *pSn = unpackBits(&bit, LEN18BITS);
65                         
66                         return PDCP_SN_18_HEAD_LEN;
67                         
68                 }else
69                 {
70                         pdcpuLog(LOG_ERR,"[PDCPU] sn size is wrong!\n");
71                         return VOS_ERROR;
72                 }
73         }
74
75         return VOS_OK;
76 }
77
78
79 INT32 pdcpuDecideCountAndHFN(PdcpStateVar_t *pStateVar, INT32 rcvdSn, PdcpSnSize_e snSize)
80 {
81         UINT32 rcvdHFN = 0;
82         UINT32 rxDeliv = pStateVar->rxDelivery;
83         INT32  tmp = 0;
84         
85         if(LEN12BITS == snSize)
86         {
87                 tmp = GET_SN_12_SN(rxDeliv) - SN_12_WINDOW_SIZE;
88                 if(rcvdSn < tmp)
89                 {
90                         rcvdHFN = GET_HFN_12_SN(rxDeliv) + 1;
91                 }else if(rcvdSn >= GET_SN_12_SN(rxDeliv) + SN_12_WINDOW_SIZE)
92                 {
93                         rcvdHFN = GET_HFN_12_SN(rxDeliv) - 1;
94                 }else
95                 {
96                         rcvdHFN = GET_HFN_12_SN(rxDeliv);
97                 }
98                 pStateVar->rxCount = GET_COUNT_12_SN(rcvdHFN, rcvdSn);
99                                 
100         }else if(LEN18BITS == snSize)
101         {
102                 tmp = GET_SN_18_SN(rxDeliv) - SN_18_WINDOW_SIZE;
103                 if(rcvdSn < tmp)
104                 {
105                         rcvdHFN = GET_HFN_18_SN(rxDeliv) + 1;
106                         
107                 }else if(rcvdSn >= GET_SN_18_SN(rxDeliv) + SN_18_WINDOW_SIZE)
108                 {
109                         rcvdHFN = GET_HFN_18_SN(rxDeliv) - 1;                   
110                 }else
111                 {
112                         rcvdHFN = GET_HFN_18_SN(rxDeliv);
113                 }
114                 pStateVar->rxCount = GET_COUNT_18_SN(rcvdHFN, rcvdSn);  
115         }else
116         {
117                 pdcpuLog(LOG_ERR,"[PDCPU] sn size is wrong!\n");
118                 return VOS_ERROR;
119         }
120                 
121         return VOS_OK;
122 }
123
124
125 INT32 pdcpuCtrlPduProc(MsgbBuff_t *pMsgBuff, PdcpCtrlPduType_e ctrlPduType, PdcpDrbEntity_t *pPdcpDrbEntity)
126 {
127         pdcpuMsgbFree(pMsgBuff);
128
129         return VOS_OK;  
130 }
131
132
133 INT32 pdcpuDelivUldata(PdcpDrbEntity_t *pDrbEntity)
134 {
135         /* traverse list and send data to SDAP */       
136         struct cl_lib_listnode *pNode = NULL;
137         PdcpuDataBuffNode_t *pDataInBuff1 = NULL;
138         PdcpuDataBuffNode_t *pDataInBuff2 = NULL;
139
140         UINT32 count = 0;
141         
142         pNode = cl_lib_listhead(pDrbEntity->pRxSduList);        
143         if(NULL == pNode)
144         {
145                 /* There is no data in buffer */
146                 VOS_Printf("there is no data in buffer\n");
147                 return VOS_OK;
148         }
149         pDataInBuff1 = (PdcpuDataBuffNode_t *)cl_lib_getdata(pNode);            
150
151         if(pDataInBuff1->count == pDrbEntity->stateVar.rxDelivery)
152         {       
153                 while(pNode)
154                 {               
155                         cl_lib_nextnode (pNode);
156                         count = pDataInBuff1->count;
157                         
158                         /*+++++++++++++++++++++++++++test+++++++++++++++++++++++++++*/                                  
159                         //VOS_Printf("\n[pdcp ul data] pdcpuDelivUldata ueIdx:%d ueE1apId:%d drbId:%d count:%d \n",
160                         //                      pDrbEntity->ueIdx,pDrbEntity->ueE1apId,pDrbEntity->drbId,count);
161                         //printfMsgbBuff(pDataInBuff1->pData);          
162                         /*---------------------------test----------------------------*/
163                         
164                         /* delivery to sdap,if sdapUlDataProc return VOS_OK,free the data
165                            else, also free the data */
166                         sdapUlDataProc(pDrbEntity->ueE1apId,pDrbEntity->pduSessionId,pDrbEntity->drbId,pDataInBuff1->pData);
167                         
168                         /* update stateVar */
169                         pDrbEntity->stateVar.rxDelivery = count + 1;
170                                                 
171                         /* delete rx buffer */
172                         cl_lib_listnode_delete(pDrbEntity->pRxSduList, pDataInBuff1);
173                         pdcpuFreeDataBuffNode(pDataInBuff1);
174                         
175                         if(NULL == pNode)
176                         {
177                                 /* There is no more data in buffer */
178                                 return VOS_OK;
179                         }
180                         pDataInBuff2 = (PdcpuDataBuffNode_t *)cl_lib_getdata(pNode);                            
181                         if(NULL == pDataInBuff2)
182                         {
183                                 /* There is no more data in buffer */
184                                 pdcpuLog(LOG_ERR,"[PDCPU] data is null\n");
185                                 return VOS_ERROR;
186                         }
187                                         
188                         if(pDataInBuff2->count == count + 1)
189                         {
190                                 /* The data is continuously */
191                                 pDataInBuff1 = pDataInBuff2;
192                                 
193                         }else if(pDataInBuff2->count > count + 1)
194                         {       
195                                 /* The data is not continuously */
196                                 return VOS_OK;  
197                         }else
198                         {       
199                                 pdcpuLog(LOG_ERR,"[PDCPU] out of order in receiving buffer!\n");
200                                 return VOS_ERROR;
201                         }
202
203                         
204                 }
205
206                 return VOS_OK;
207         }else if(pDataInBuff1->count > pDrbEntity->stateVar.rxDelivery)
208         {
209                 /* the data whose count value is equal to rxDelivery,is still waited for */
210                 return VOS_OK;
211         }else
212         {
213                 pdcpuLog(LOG_ERR,"[PDCPU] rxbuff is wrong!\n");
214                 return VOS_ERROR;
215         }
216         
217         return VOS_OK;
218 }
219
220
221 /*******************************************************************************
222  * pdcp-u ul data process
223  * INPUT:
224  *       ueE1apId: E1AP ID of the UE
225  *       drbId: ID of data radio bearer
226  *       pMsgBuff: SDAP PDU
227  * OUTPUT:
228  *       0: success
229  *       -1:failed
230 *******************************************************************************/
231 INT32 pdcpuUlDataProc(UINT64 ueE1apId, UINT8 drbId, MsgbBuff_t *pMsgBuff)
232 {       
233         UINT8  pduType     = 0;
234         UINT32 rcvdSn      = 0;
235         UINT32 count       = 0;
236         INT8   pduHeadLen  = 0;
237         UINT16 ueIdx       = 0xffff;
238         INT32  ret         = -1;
239         PdcpSnSize_e            snSize;
240         PdcpDrbEntity_t         *pPdcpDrbEntity = NULL;
241         PdcpStateVar_t          *pStateVar              = NULL;
242                         
243         pdcpuNullCheck(pMsgBuff);
244         UINT8 *pMsgHead = msgbData(pMsgBuff);
245         UINT16 pduLen   = msgbDataUsedLen(pMsgBuff);
246         if(pduLen<1)
247         {
248                 pdcpuMsgbFree(pMsgBuff);
249                 pdcpuLog(LOG_ERR,"[PDCPU] data len is too small!\n");
250                 return VOS_ERROR;
251         }
252         
253         if(VOS_ERROR == cuupGetUeIdx(ueE1apId, &ueIdx, &gPdcpuUeIdxTable))
254         {
255                 pdcpuLog(LOG_ERR,"[PDCPU] add ueE1apId failed\n");
256                 pdcpuMsgbFree(pMsgBuff);
257                 return VOS_ERROR;
258         }
259         pdcpuGetEntity(ueIdx, drbId, pPdcpDrbEntity);
260         pdcpuNullCheck(pPdcpDrbEntity);
261         snSize    = pPdcpDrbEntity->pdcpSnSizeUl;
262         pStateVar = &pPdcpDrbEntity->stateVar;
263                 
264         /* get pdcp pdu type */
265         BitOpt_t bit;
266         initBits(&bit, pMsgHead, pduLen, 0);
267         pduType = unpackBits(&bit, 1);
268
269     if(PDCP_DATA_PDU == pduType)
270         {
271                 //1.analysis pdcp header: shall not move the data start pointer
272                 pduHeadLen = pdcpuUnpackPduHead(pMsgBuff, snSize, &pduType, &rcvdSn);
273                 if(VOS_ERROR == pduHeadLen)
274                 {
275                         pdcpuLog(LOG_ERR,"[PDCPU] unpack pdu head wrong!\n");
276                         pdcpuMsgbFree(pMsgBuff);
277                         return VOS_ERROR;
278                 }
279                 
280                 //make sure received HFN and received Count
281                 if(VOS_ERROR == pdcpuDecideCountAndHFN(pStateVar, rcvdSn, snSize))
282                 {
283                         pdcpuLog(LOG_ERR,"[PDCPU] get count wrong!\n");
284                         pdcpuMsgbFree(pMsgBuff);
285                         return VOS_ERROR;
286                 }
287         
288                 count = pStateVar->rxCount;
289         
290                 //2.deciphering : except pdcp pdu head, the SDAP header and the SDAP Control PDU
291                 //get sdap pdu type
292                 BitOpt_t SdapHeadbit;
293                 UINT8 *pSdapPduHead = msgbData(pMsgBuff) + pduHeadLen;
294                 initBits(&SdapHeadbit, pSdapPduHead, 1, 0);
295                 //5.remove pdcp pdu head
296                 msgbHeadPull(pMsgBuff, pduHeadLen);
297                 ret = sdapUlDataProc(ueE1apId,pPdcpDrbEntity->pduSessionId,drbId,pMsgBuff);
298                 pPdcpDrbEntity->stateVar.rxDelivery = count + 1;
299                 if(VOS_OK == ret)
300                 {
301                         pdcpuMsgbFree(pMsgBuff);
302                         return VOS_OK;
303                 }else
304                 {
305                         pdcpuMsgbFree(pMsgBuff);
306                         pdcpuLog(LOG_ERR,"[PDCPU] ul proc failed\n");
307                         return VOS_ERROR;
308                 }
309                 
310                                 
311         }
312
313         return VOS_OK;
314 }
315