1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 #if !defined(LEVEL2_H) 19 #define LEVEL2_H 20 #include "pduparcom.h" 21 22 #define PDU_HDR_SIZE_MAX 8 23 #define LEVEL2_STUFFING_SZ 5 24 #define LEVEL2OH_STUFFING_SZ 6 25 #define LEVEL2_HDR_SZ 5 26 #define LEVEL2OH_HDR_SZ 6 27 #define LEVEL2_THRESHOLD_SYNC 15 28 #define LEVEL2_THRESHOLD_DATA 13 29 30 #define LEVEL2_FLAG_SZ 2 31 #define LEVEL2_MAX_PDU_SZ 254 32 #define LEVEL2_FLAG 0xE14D 33 #define LEVEL2_CLOSING_FLAG 0x1EB2 34 35 class Level2PduParcom : public H223PduParcomBase 36 { 37 public: 38 typedef enum 39 { 40 ECopyHdr = 0, 41 ECopyData 42 } EOperationId; 43 44 Level2PduParcom(bool oh = false, int closing_cur = 0); ~Level2PduParcom()45 ~Level2PduParcom() 46 { 47 OSCL_DEFAULT_FREE(iEncTab); 48 OSCL_DEFAULT_FREE(iDecTab); 49 OSCL_DEFAULT_FREE(iBsbuf[0]); 50 OSCL_DEFAULT_FREE(iBsbuf[1]); 51 } GetLevel()52 TPVH223Level GetLevel() 53 { 54 if (iOh) 55 return H223_LEVEL2_OH; 56 return H223_LEVEL2; 57 } 58 UseOh(bool oh)59 bool UseOh(bool oh) 60 { 61 bool ret = iOh; 62 iOh = oh; 63 return ret; 64 } 65 66 uint32 GetStuffing(uint8* buf, uint32 buf_size, uint8 mux_code = 0); GetHeaderSz()67 uint32 GetHeaderSz() 68 { 69 return (uint16)((iOh) ? LEVEL2OH_HDR_SZ : LEVEL2_HDR_SZ); 70 } GetStuffingSz()71 uint32 GetStuffingSz() 72 { 73 return (uint16)((iOh) ? LEVEL2OH_STUFFING_SZ : LEVEL2_STUFFING_SZ); 74 } 75 uint32 Parse(uint8* bsbuf, uint32 bsbsz); 76 PVMFStatus CompletePdu(OsclSharedPtr<PVMFMediaDataImpl>& pdu, int8 mt, uint8 pm); 77 void ResetStats(); 78 void LogStats(TPVDirection dir); 79 void SetClosingCur(int32 closing); 80 protected: 81 void Construct(uint16 max_outstanding_pdus); 82 void GetHdrFragment(OsclRefCounterMemFrag& frag); 83 private: 84 PVMFBufferPoolAllocator iHdrFragmentAlloc; 85 86 uint8 iHecCrc[20]; 87 int iThreshold; 88 bool iOh; 89 bool iUseOh; 90 int iCurrentInt32; 91 int iCnt; 92 int iRecoverCnt; 93 int iMpl; 94 int iMplRemaining; 95 int iPktsInUse; 96 int iMuxCode; 97 int iClosingCur; 98 int iClosingNext; 99 int iClosingNextRx; 100 101 uint8 iPduHdr[PDU_HDR_SIZE_MAX]; 102 uint8* iPduHdrPos; 103 uint8* iBsbuf[2]; 104 unsigned iCurBsBuf; 105 106 int iPrevMuxTblNum; 107 int iPrevPm; 108 EOperationId iCurrentOp; 109 bool CheckFlag(uint8* buf, int* fClosing, int trsld); 110 void IndicatePdu(uint8 optional_header); 111 int ParseHdr(int* fClosing, int* mpl, int* muxCode, int* numErrors, uint8* optional_header); 112 ResetPdu()113 inline void ResetPdu() 114 { 115 iPduPos = iPdu; 116 iMpl = 0; 117 iMplRemaining = 0; 118 iMuxCode = -1; 119 iClosingCur = 0; 120 } 121 ResetPduHdr()122 inline void ResetPduHdr() 123 { 124 iPduHdrPos = iPduHdr; 125 } 126 CopyOctetToPdu(uint8 c)127 inline void CopyOctetToPdu(uint8 c) 128 { 129 if (iPduPos >= iPduEndPos) 130 { 131 /* Reset the pdu */ 132 ResetPdu(); 133 } 134 /* Copy the byte to the pdu */ 135 *iPduPos++ = c; 136 } 137 138 uint8* FindSync(uint8* data, int len, int* closing); 139 bool RecoverPduData(uint8*& bsbuf, int* bsbsz); 140 GolayDec(int received,int * num_errors)141 int GolayDec(int received, int* num_errors) 142 { 143 received = ((received >> 1) & 0xfffff800) + (received & 0x7ff); 144 int syndrome = get_syndrome(received); 145 received ^= iDecTab[syndrome]; 146 *num_errors = iNumOnes[syndrome&0xFF] + iNumOnes[(syndrome >> 8)&0xFF] + 147 iNumOnes[(syndrome >> 16)&0xFF] + iNumOnes[(syndrome >> 24)&0xFF]; 148 return received >> 11; 149 } 150 get_syndrome(int pattern)151 int get_syndrome(int pattern) 152 { 153 154 //static const long gen_poly = 0x00000c75; 155 156 long junk = 0x00400000; 157 if (pattern >= 0x00000800) 158 { 159 while (pattern & 0xfffff800) 160 { 161 while (!(junk & pattern)) junk >>= 1; 162 pattern ^= (junk + (junk >> 1) + (junk >> 5) + (junk >> 6) + (junk >> 7) + (junk >> 9) + (junk >> 11)); 163 } 164 } 165 return(pattern); 166 } 167 168 uint8 iLastHdr[8]; 169 int* iEncTab; 170 int* iDecTab; 171 uint8 iNumOnes[256]; 172 // Outgoing 173 uint32 iNumClosingFlagsTx; 174 uint32 iNumFlagEmulation; 175 176 // Incoming 177 uint32 iNumClosingFlagsRx; 178 uint32 iNumGolayCblePduHdrErrorsRx; 179 uint32 iNumPduHdrErrorsMplRx; 180 uint32 iNumCorruptedOhRx; 181 uint32 iNumHeadersCorrectedByOh; 182 bool iCopyPduWithSync; 183 }; 184 185 #endif 186 187 188