if(first2) {
IT_OUT(1);
nr=0;
rxFrameFlag=1;
rack(0) = 0x80 | nr;
}
first2=0;
while(IT_IN(0)) {
rxBitFlag=1;
/*
* output the bit check if address+cntrl+info+FCS
* also ignore if removing a zero
*/
if(!removeZero && begFrame) {
/*
*/
fcsshreg |= bit;
if(startFCS) {
fcsbit = (0x100 & fcsshreg) ? 1:0;
{
carry=(acc&0x8000) ? 1:0;
acc <<= 1;
acc ^= ((fcsbit ^ carry) ? GENERATOR_POLY:0);
}
}
#if 1
if(debugFlag)
fprintf(debug_F,"shreg=%4x fcsshreg=%4x fcsbit=%d bit=%d acc=%4x\n",shreg,fcsshreg,fcsbit,bit,acc);
#endif
fcsshreg <<= 1;
buildByte |= bit;
buildByteBits++;
if(buildByteBits == 8) {
vect_P[byteCount]=buildByte;
buildByte=0;
buildByteBits=0;
byteCount++;
}
buildByte <<= 1;
frameBits++;
/*
* check if received bits exceeds payload size plus address
* control,and FCS
*/
if(frameBits > numberOfBits +8+8+16+8) {
/*
* If exceeds then we have an error condition.
* Possiblility is that end flag corrupted.
* In this case we request retransmission as if error
* occured.
* We must also flush out input buffer.
*/
frameBits=0;
endFrame=1;
first=1;
begFrame=0;
shreg=0;
fcsshreg=0;
acc=0;
startFCS=0;
bit=0;
count=0;
/*
* get ns from transmitted frame
*/
ns=vect_P[1];
ns = ns >> 3;
ns = ns & 0x07;
IT_OUT(1);
rack(0) = 0x90 | nr;
rxFrameFlag=1;
if(debugFlag)
fprintf(debug_F,"Bad End Flag Received NS=%d NR %d\n",ns,nr);
/*
* Flush input port
*/
while(IT_IN(0));
break;
}
}
else
removeZero=0;
/*
* collect the bits
*/
bit=(int)(x(0)+0.001);
totalBitsInput++;
shreg |= bit;
stuffCheck=shreg & 0x3f;
if(stuffCheck == 0x3e ) {
if(debugFlag)
fprintf(debug_F,"Unstuff zero condition\n");
removeZero=1;
}
stuffCheck=shreg & 0x7f;
if(stuffCheck == 0x7f ) {
if(debugFlag)
fprintf(debug_F,"Abort condition\n");
/*
* reset and start over.
* reject frame and request retransmission
*/
frameBits=0;
endFrame=1;
first=1;
begFrame=0;
shreg=0;
fcsshreg=0;
acc=0;
startFCS=0;
bit=0;
count=0;
/*
* get ns from transmitted frame
*/
ns=vect_P[1];
ns = ns >> 3;
ns = ns & 0x07;
IT_OUT(1);
rack(0) = 0x90 | nr;
rxFrameFlag=1;
if(debugFlag)
fprintf(debug_F,"Abort Received NS=%d NR %d\n",ns,nr);
/*
* Flush input port
*/
while(IT_IN(0));
break;
}
/*
* Frame Check Sequence
*/
if(startFCS) {
}
/*
* check if FLAG (beginning or end)
*/
flagCheck=shreg & 0xff;
if(flagCheck == 0x7e) {
/*
* found a flag
*/
removeZero=0;
if(first) {
/*
* beginning of frame flag
*/
acc=0;
first=0;
begFrame=1;
if(debugFlag)
fprintf(debug_F,"Start Flag\n");
frameCount++;
bitCount=0;
/*
* beginning of frame
* allocate vector of bytes
*/
vect_P=(char*)calloc(numberBytes,sizeof(char));
byteCount=0;
buildByte=0;
buildByteBits=0;
#if 1
/*
* a trick to not include last bit of flag in
* output vector
*/
removeZero=1;
#endif
}
else {
/*
* end of frame flag
*/
if(debugFlag)
fprintf(debug_F,"END Flag\n");
if(shreg == 0x7e7e) {
/*
* Two consecutive flags encountered.
* Set state to beginning
* flag
*/
count=0;
fcsshreg = 0;
acc=0;
shreg=0x7e;
first=0;
begFrame=1;
byteCount=0;
buildByte=0;
buildByteBits=0;
removeZero=1;
frameBits=0;
} else {
frameBits=0;
fcsshreg = 0;
for(i=0; i<2; i++) {
/*
* Flush FCS
*/
fcsshreg <<=1;
fcsbit = (0x100 & fcsshreg) ? 1:0;
{
carry=0;
acc <<= 1;
acc ^= ((fcsbit ^ carry) ? GENERATOR_POLY:0);
}
#if 0
fprintf(stderr,"Flushing fcsshreg=%4x acc=%4x \n",fcsshreg,acc);
#endif
}
errorFlag=0;
if( acc) {
errorFlag=1;
if(debugFlag)
fprintf(debug_F,"Frame Error\n");
errorCount++;
}
else
correctCount++;
endFrame=1;
first=1;
begFrame=0;
shreg=0;
fcsshreg=0;
acc=0;
startFCS=0;
bit=0;
count=0;
/*
* get ns from transmitted frame
*/
ns=vect_P[1];
ns = ns >> 3;
ns = ns & 0x07;
if(!errorFlag) {
if(IT_OUT(0) ){ KrnOverflow("rxhdlc",0);return(99);}
codedBlock.vector_P=&vect_P[2];
codedBlock.length=byteCount-2-2;
if(codedBlock.length > numberBytes) {
fprintf(debug_F,"rxhdlc: bad frame length=%d\n",
codedBlock.length);
codedBlock.length=numberBytes;
}
y(0) = codedBlock;
}
IT_OUT(1);
if(errorFlag) {
rxFrameFlag=1;
rack(0) = 0x90 | nr;
} else {
nr++;
nr %= 8;
rxFrameFlag=1;
rack(0) = 0x80 | nr;
}
if(debugFlag)
fprintf(debug_F,"Received NS=%d NR %d\n",ns,nr);
} /*end double flag */
}
}
shreg <<= 1;
if(begFrame ) {
/*
* start FCS after first FLAG
* delay also allows time to check for end of frame flag
* so that it is not used in computation of FCS
*/
if(count == 10) {
startFCS=1;
if(debugFlag)
fprintf(debug_F,"Begin startFCS\n");
}
count++;
}
}
if(!rxFrameFlag && rxBitFlag) {
/*
* Bad news all input bits exhausted but no frame encountered.
* This could be due to the fact that a flag got corrupted
* In this case request retransmission and reset every thing
*/
frameBits=0;
endFrame=1;
first=1;
begFrame=0;
shreg=0;
fcsshreg=0;
acc=0;
startFCS=0;
bit=0;
count=0;
/*
* reject and request retransmission
*/
IT_OUT(1);
rack(0) = 0x90 | nr;
rxFrameFlag=1;
if(debugFlag)
fprintf(debug_F,"Damaged Frame:NR %d\n",nr);
}
rxFrameFlag=0;
rxBitFlag=0;
return(0);