testexecmgmt/ucc/Source/MobileTermination/CPppFrame.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 * System Includes
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <string.h>
       
    22 #include <assert.h>
       
    23 
       
    24 
       
    25 /*******************************************************************************
       
    26  *
       
    27  * Local Includes
       
    28  *
       
    29  ******************************************************************************/
       
    30 #include "CPppFrame.h"
       
    31 
       
    32 
       
    33 /*******************************************************************************
       
    34  *
       
    35  * Definitions
       
    36  *
       
    37  ******************************************************************************/
       
    38 
       
    39 
       
    40 /*******************************************************************************
       
    41  *
       
    42  * PUBLIC METHODS: Constructor / Destructor
       
    43  *
       
    44  ******************************************************************************/
       
    45 CPppFrame::CPppFrame( char *aFrameName, int aRemoveHDLCFraming )
       
    46 {	
       
    47 	int slen;
       
    48 
       
    49 	// check params
       
    50 	assert( aFrameName != NULL );
       
    51 	slen = strlen(aFrameName);
       
    52 	assert( slen < MAXFRAMENAME );
       
    53 
       
    54 	// init members
       
    55 	strcpy( iFrameName, aFrameName );
       
    56 	iCurrentFrameSize = 0;
       
    57 	iCurrentBufferSize = 0;
       
    58 	iFrameBufferStatus = FS_EMPTY;
       
    59 	iRemoveHDLCFraming = aRemoveHDLCFraming;
       
    60 	iEscapedChar = 0;
       
    61 }
       
    62 
       
    63 
       
    64 CPppFrame::~CPppFrame()
       
    65 {
       
    66 }
       
    67 
       
    68 
       
    69 /*******************************************************************************
       
    70  *
       
    71  * PUBLIC METHODS: AddByteToFrame
       
    72  *
       
    73  ******************************************************************************/
       
    74 TFrameError CPppFrame::AddByteToFrame( char c )
       
    75 {
       
    76 	int escaped_char_flag_on_entry;
       
    77 
       
    78 	// check state
       
    79 	assert( iFrameBufferStatus != FS_COMPLETE );
       
    80 
       
    81 	// save the escaped char flag in case we have to restore it 
       
    82 	escaped_char_flag_on_entry = iEscapedChar;
       
    83 
       
    84 	// (1)  undo hdlc frameing if configured to do so
       
    85 	if( iRemoveHDLCFraming != 0 ) {
       
    86 
       
    87 		// check to see if this is the escape char, if so we set a flag and stop processing this byte
       
    88 		if( (c == 0x7D) && (iEscapedChar == 0) ) {
       
    89 			iEscapedChar = 1;
       
    90 			return FE_NONE;
       
    91 		}
       
    92 
       
    93 		// if this byte was escaped then we have to XOR it with 0x20, then continue processesing
       
    94 		if( iEscapedChar == 1 ) {
       
    95 			iEscapedChar = 0;
       
    96 			c = c ^ 0x20;
       
    97 		}
       
    98 	}
       
    99 
       
   100 	// sanity checks between hdlc and non
       
   101 	assert( iEscapedChar == 0 );
       
   102 
       
   103 	// (2) process the byte as normal
       
   104 
       
   105 	// if this is the start of the frame then set the length, state, and copy the char. Note that PPP
       
   106 	// only requires one frame sequence (0x7e) between two adjacent frames. We take this flag as the
       
   107 	// end of the previous frame, which means that a frame may not have an openning frame sequence. The
       
   108 	// upshot of all this is that any byte may start a new frame.
       
   109 	if( iFrameBufferStatus == FS_EMPTY ) {
       
   110 	  assert( iCurrentFrameSize == 0 );
       
   111 	  assert( iCurrentBufferSize == 0 );
       
   112 	  (iFrameBuffer)[0] = c;
       
   113 	  iCurrentFrameSize = iCurrentBufferSize = 1;
       
   114 	  iFrameBufferStatus = FS_IN_PROGRESS;
       
   115 	  return FE_NONE;
       
   116 	} 
       
   117 
       
   118 	// If we are in a frame then buffer the byte. If there is no room then discard the byte and return 
       
   119 	// a warning. One byte is always kept for the end of frame sequence.
       
   120 	if( (iFrameBufferStatus == FS_IN_PROGRESS) && (c != 0x7E) ) {
       
   121 	  if( iCurrentBufferSize < (MAXFRAMESIZE-1) ) {
       
   122 	    (iFrameBuffer)[iCurrentBufferSize] = c;
       
   123 		iCurrentFrameSize++;
       
   124 		iCurrentBufferSize++;
       
   125 		return FE_NONE;
       
   126 	  } else {
       
   127 	    iEscapedChar = escaped_char_flag_on_entry;
       
   128 		return FE_OVERFLOW;
       
   129 	  }
       
   130 	}
       
   131 
       
   132 	// If we are in a frame and this is then buffer it and update the state
       
   133 	if( (iFrameBufferStatus == FS_IN_PROGRESS) && (c == 0x7E) ) {
       
   134 
       
   135 		// save the final byte
       
   136 		assert( iCurrentBufferSize < MAXFRAMESIZE );
       
   137 		(iFrameBuffer)[iCurrentBufferSize] = c;
       
   138 		iCurrentFrameSize++;
       
   139 		iCurrentBufferSize++;
       
   140 
       
   141 		// update the state
       
   142 		iFrameBufferStatus = FS_COMPLETE;
       
   143 		return FE_NONE;
       
   144 	}
       
   145 
       
   146 	// done (just to keep compiler happy)
       
   147 	return FE_NONE;
       
   148 }
       
   149 
       
   150 
       
   151 /*******************************************************************************
       
   152  *
       
   153  * PUBLIC METHODS: ClearFrame
       
   154  *
       
   155  ******************************************************************************/
       
   156 TFrameError CPppFrame::ClearFrame()
       
   157 {
       
   158 	iCurrentBufferSize = 0;
       
   159 	iCurrentFrameSize = 0;
       
   160 	iFrameBufferStatus = FS_EMPTY;
       
   161 	return FE_NONE;
       
   162 }
       
   163 
       
   164 
       
   165 /*******************************************************************************
       
   166  *
       
   167  * PUBLIC METHODS: ClearFrameMemoryButNotState
       
   168  *
       
   169  ******************************************************************************/
       
   170 TFrameError CPppFrame::ClearFrameMemoryButNotState()
       
   171 {
       
   172 	iCurrentBufferSize = 0;
       
   173 	return FE_NONE;
       
   174 }
       
   175 
       
   176 /*******************************************************************************
       
   177  *
       
   178  * PUBLIC METHODS: GetFrameStatus
       
   179  *
       
   180  ******************************************************************************/
       
   181 TFrameStatus CPppFrame::GetFrameStatus()
       
   182 {
       
   183 	return iFrameBufferStatus;
       
   184 }
       
   185 
       
   186 
       
   187 /*******************************************************************************
       
   188  *
       
   189  * PUBLIC METHODS: GetOverflowCount
       
   190  *
       
   191  ******************************************************************************/
       
   192 int CPppFrame::GetOverflowCount()
       
   193 {
       
   194 	return 0;
       
   195 }
       
   196 
       
   197 
       
   198 /*******************************************************************************
       
   199  *
       
   200  * PUBLIC METHODS: GetFrameBuffer
       
   201  *
       
   202  ******************************************************************************/
       
   203 char *CPppFrame::GetFrameBuffer( int *aLen )
       
   204 {
       
   205 	assert( aLen != NULL );
       
   206 	*aLen = iCurrentBufferSize;
       
   207 	return (char*)iFrameBuffer;
       
   208 }
       
   209 
       
   210 
       
   211 /*******************************************************************************
       
   212  *
       
   213  * PUBLIC METHODS: GetFrameName
       
   214  *
       
   215  ******************************************************************************/
       
   216 char *CPppFrame::GetFrameName()
       
   217 {
       
   218 	return iFrameName;
       
   219 }