#include "RLC.h"
#include <string.h>
#include "stdio.h"
#include <stdlib.h>
/*==========================================================================
函数功能:解接口数据与L2层的MAC,RLC,PDCP头
==========================================================================*/
int String2Hex(char str[], u8 senddata[])
{
int hexdata,lowhexdata;
int hexdatalen=0;
int len=strlen((char*)str);
for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' '||hstr=='\r'||hstr=='\n')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
hexdata=ConvertHexChar(hstr);
lowhexdata=ConvertHexChar(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=(char)hexdata;
hexdatalen++;
}
return hexdatalen;
}
char ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else
{
return (-1);
//MessageBoxW(_T("there is a illegal char!")+ch);
}
}
u16 remainLowBit(u8 Num)
{
u16 retn=0x0001;
retn=retn<<Num;
retn--;
return retn;
}
u16 shiftBit(u8 *buf,u16 *beginBit,u8 bitNum)
{/*beginBit为0表示从buf所指向的字节的第一个bit开始*/
u16 value = 0;
u16 temp1 = 0,temp2 = 0;
u8 *beginBytePoint;
u16 totalBits=0;
u16 relowbit=0;
if(bitNum == 0||bitNum >= 5000)/*不取任何内容,返回0即可*/
return 0;
beginBytePoint = buf +(*beginBit)/8;/*取得开始bit所在的字节的指针*/
totalBits = (*beginBit)%8 + bitNum;
if(totalBits <= 8)
{
value = (*beginBytePoint) >> (8 - totalBits);
relowbit=remainLowBit(bitNum);
value &= relowbit;
}
else if(totalBits <= 16)
{
/*取高比特位*/
temp1 = (u16)(*beginBytePoint);
temp1 &= remainLowBit(8 - ((*beginBit)%8));
temp1=temp1<<(totalBits - 8);
/*取低比特位*/
temp2 = (u16)*(beginBytePoint + 1);
temp2 = ((*beginBit) + bitNum)%8 ? temp2>>(8 - ((*beginBit) + bitNum)%8) : temp2;
value = temp1 | temp2;
}
else
{
/**/
temp1 = (u16)(*beginBytePoint);
temp1 = temp1 << 8;
temp1 = temp1 | (u16)(*(beginBytePoint+1));
temp1 &= remainLowBit(16 - ((*beginBit)%8));
temp1=temp1<<(totalBits - 16);
/*取低比特位*/
temp2 = (u16)*(beginBytePoint + 2);
temp2 = ((*beginBit) + bitNum)%8 ? temp2>>(8 - ((*beginBit) + bitNum)%8) : temp2;
value = temp1 | temp2;
}
(*beginBit)+=bitNum;
return value;
}
void Message_Decoding(u8 *Msg32)
{
u8 *data_p;
u8 *BufPoint; //基地址
u16 MsgLength;
u16 subframe;
u16 numUes;
u16 pbchSize;
u16 i;//数据个数
u16 j;//控制用户个数
u16 k;
u16 Size;
u16 UL_size[MAX_NUM_UES];
u16 ueId;
u16 R;
u16 timeStamp;
u16 NumTb;
u16 valid;
u16 puschMcs;
u16 sinr;
u16 rssi;
u16 NumCodeBlocks;
u16 NumCodeErrors;
u32 DBuffer;
u16 HIGH_DBuffer;
u16 LOW_DBuffer;
u16 *countBit; //偏移地址
u16 countBit0=0;
//--------------------------------------变量初始化
countBit =(u16 *)&countBit0;
BufPoint =Msg32;
data_p =BufPoint;
*countBit =0;
i =0;
j =0;
k =0;
//======================================================================函数主体
DBuffer =shiftBit(BufPoint,countBit,32);
MsgType =DBuffer;
MsgLength =shiftBit(BufPoint,countBit,16);
data_p =data_p+ 4;
if(MsgType==2)
{
subframe =shiftBit(BufPoint,countBit,16);
numUes =shiftBit(BufPoint,countBit,16);
HIGH_DBuffer =shiftBit(BufPoint,countBit,16);
LOW_DBuffer =shiftBit(BufPoint,countBit,16);
pbchSize =HIGH_DBuffer+LOW_DBuffer;
data_p =data_p+ 8;
if(pbchSize!=0)
{
memcpy(BCH_MAC_PDU,data_p,pbchSize);
printf("BCH-BCCH:\n");
do{
printf("%x ",BCH_MAC_PDU[i]);
i++;
}
while(i<pbchSize);
printf("\n");
}
else
{
while(j<numUes)
{
HIGH_DBuffer =shiftBit(BufPoint,countBit,16);
LOW_DBuffer =shiftBit(BufPoint,countBit,16);
Size =HIGH_DBuffer+LOW_DBuffer;
ueId =shiftBit(BufPoint,countBit,16);
R =shiftBit(BufPoint,countBit,16);
data_p =data_p+ 8;
if(ueId==65534)
{
memcpy(PCH_MAC_PDU,data_p,Size);
printf("PCH-PCCH:\n");
do{
printf("%x ",PCH_MAC_PDU[i]);
i++;
}
while(i<Size);
printf("\n");
}
else if(ueId==65535)
{
memcpy(DBCH_MAC_PDU,data_p,Size);
printf("DLSCH-BCCH:\n");
do{
printf("%x ",DBCH_MAC_PDU[i]);
i++;
}
while(i<Size);
printf("\n");
}
else
{
memcpy(MAC_PDU,data_p,Size);
DL_MAC_Decoding(MAC_PDU,Size);
}
data_p =data_p+ Size;
switch(Size%4)
{
case(1):
data_p =data_p+ 3;
break;
case(2):
data_p =data_p+ 2;
break;
case(3):
data_p =data_p+ 1;
break;
}
if(j<numUes-1)
{
data_p =data_p+ 4;
}
BufPoint =data_p;
*countBit =0;
j++;
}
}
}
else if(MsgType==15)
{
HIGH_DBuffer =shiftBit(BufPoint,countBit,16);
LOW_DBuffer =shiftBit(BufPoint,countBit,16);
DBuffer =HIGH_DBuffer+LOW_DBuffer;
timeStamp =DBuffer;
subframe =shiftBit(BufPoint,countBit,16);
NumTb =shiftBit(BufPoint,countBit,16);
data_p =data_p+ 8;
while(j<NumTb)
{
ueId =shiftBit(BufPoint,countBit,16);
valid =shiftBit(BufPoint,countBit,8);
puschMcs =shiftBit(BufPoint,countBit,8);
HIGH_DBuffer =shiftBit(BufPoint,countBit,16);
LOW_DBuffer =shiftBit(BufPoint,countBit,16);
sinr =HIGH_DBuffer+LOW_DBuffer;
HIGH_DBuffer =shiftBit(BufPoint,countBit,16);
LOW_DBuffer =shiftBit(BufPoint,countBit,16);
rssi =HIGH_DBuffer+LOW_DBuffer;
HIGH_DBuffer =shiftBit(BufPoint,countBit,16);
LOW_DBuffer =shiftBit(BufPoint,countBit,16);
UL_size[k]