www.pudn.com > sliplib.rar > slipLib.c
#include#include "slipLib.h" SLIP_ID slipCreate( const int iMaxBytes, const unsigned int uiOption, int ( * pfDecodeCallBack )( ), int ( * pfEncodeCallBack )( ) ) { SLIP_ID strSlipId = NULL; void *pvMemStart = NULL; if( ( iMaxBytes <= 0 ) || ( ( pfDecodeCallBack == NULL ) && ( pfEncodeCallBack == NULL ) ) ) { printf( "[ slipCreate ]:The para is invalid.\n" ); return( NULL ); } if( ( pvMemStart = ( void* )malloc( sizeof( struct SLIP ) + iMaxBytes * 2 ) ) == NULL ) { printf( "[ slipCreate ]:malloc failed. " ); return( NULL ); } strSlipId = pvMemStart; strSlipId->enumSlipDecodeStatus = SLIP_STATUS_NOSTART; strSlipId->pucSlipDecodeBuff = pvMemStart + sizeof( struct SLIP ); strSlipId->iSlipDecodeBuffMaxLength = iMaxBytes; strSlipId->iSlipDecodeBuffLength = 0; /* strSlipId->uiSlipDecodeErrorNo = 0; */ strSlipId->pucSlipEncodeBuff = pvMemStart + sizeof( struct SLIP ) + iMaxBytes; strSlipId->iSlipEncodeBuffMaxLength = iMaxBytes; strSlipId->iSlipEncodeBuffLength = 0; /* strSlipId->uiOption = 0; */ strSlipId->pfDecodeCallBack = pfDecodeCallBack; strSlipId->pfEncodeCallBack = pfEncodeCallBack; return( strSlipId ); } int slipDelete( SLIP_ID strSlipId ) { if( strSlipId != NULL ) { free( strSlipId ); strSlipId == NULL; } return( 1 ); } int slipDecode( SLIP_ID strSlipId, const unsigned char * pucPackedBuff, const int iPackedBuffLength ) { int i; if( ( strSlipId == NULL ) || ( pucPackedBuff == NULL ) || ( iPackedBuffLength <= 0 ) ) { printf( "[ slipDecode ]:The para is invalid.\n" ); return( 0 ); } for( i = 0; i < iPackedBuffLength; i++ ) { switch( pucPackedBuff[ i ] ) { case SLIP_END: if( strSlipId->enumSlipDecodeStatus == SLIP_STATUS_NOSTART ) { strSlipId->enumSlipDecodeStatus = SLIP_STATUS_START; strSlipId->iSlipDecodeBuffLength = 0; } else if( strSlipId->enumSlipDecodeStatus == SLIP_STATUS_START ) { strSlipId->enumSlipDecodeStatus = SLIP_STATUS_NOSTART; strSlipId->pfDecodeCallBack( strSlipId->pucSlipDecodeBuff, strSlipId->iSlipDecodeBuffLength ); strSlipId->iSlipDecodeBuffLength = 0; } break; case SLIP_ESC: if( pucPackedBuff[ i + 1 ] == SLIP_TRANS_END ) { strSlipId->pucSlipDecodeBuff[ strSlipId->iSlipDecodeBuffLength++ ] = SLIP_END; i++; } else if( pucPackedBuff[ i + 1 ] == SLIP_TRANS_ESC ) { strSlipId->pucSlipDecodeBuff[ strSlipId->iSlipDecodeBuffLength++ ] = SLIP_ESC; i++; } break; default: strSlipId->pucSlipDecodeBuff[ strSlipId->iSlipDecodeBuffLength++ ] = pucPackedBuff[ i ]; break; } } return( strSlipId->iSlipDecodeBuffLength ); } int slipEncode( SLIP_ID strSlipId, const unsigned char * pucUnpackedBuff, const int iUnpackedBuffLength ) { int i; if( ( strSlipId == NULL ) || ( pucUnpackedBuff == NULL ) || ( iUnpackedBuffLength <= 0 ) ) { printf( "[ slipEncode ]:The para is invalid.\n" ); return( 0 ); } strSlipId->pucSlipEncodeBuff[ 0 ] = SLIP_END; strSlipId->iSlipEncodeBuffLength = 1; for( i = 0; i < iUnpackedBuffLength; i++ ) { switch( pucUnpackedBuff[ i ] ) { case SLIP_END: strSlipId->pucSlipEncodeBuff[ strSlipId->iSlipEncodeBuffLength++ ] = SLIP_ESC; strSlipId->pucSlipEncodeBuff[ strSlipId->iSlipEncodeBuffLength++ ] = SLIP_TRANS_END; break; case SLIP_ESC: strSlipId->pucSlipEncodeBuff[ strSlipId->iSlipEncodeBuffLength++ ] = SLIP_ESC; strSlipId->pucSlipEncodeBuff[ strSlipId->iSlipEncodeBuffLength++ ] = SLIP_TRANS_ESC; break; default: strSlipId->pucSlipEncodeBuff[ strSlipId->iSlipEncodeBuffLength++ ] = pucUnpackedBuff[ i ]; break; } } strSlipId->pucSlipEncodeBuff[ strSlipId->iSlipEncodeBuffLength ] = SLIP_END; return( strSlipId->pfEncodeCallBack( strSlipId->pucSlipEncodeBuff, strSlipId->iSlipEncodeBuffLength + 1 ) ); }