# SFBByteBuffer.h

Go to the documentation of this file.

    00001 /*                                       -*- mode:C++; fill-column: 100 -*-
    00002
    00003   SFBByteBuffer.h - Circular byte buffering between the UART interrupt and the
    00004                     background packet dispatching levels
    00005   Copyright (C) 2009 The Regents of the University of New Mexico.  All rights reserved.
    00006
    00007   This library is free software; you can redistribute it and/or
    00008   modify it under the terms of the GNU Lesser General Public
    00009   License as published by the Free Software Foundation; either
    00010   version 2.1 of the License, or (at your option) any later version.
    00011
    00012   This library is distributed in the hope that it will be useful,
    00013   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015   Lesser General Public License for more details.
    00016
    00017   You should have received a copy of the GNU General Public License
    00018   along with this library; if not, write to the Free Software
    00019   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00020   USA
    00021
    00022   $Id$
    00023 */
    00024
    00051 #ifndef SFBBYTEBUFFER_H
    00052 #define SFBBYTEBUFFER_H
    00053
    00054 #include "SFBTypes.h"       /* For u8 etc */
    00055 #include "SFBConstants.h"   /* For BYTE_BUFFER_BYTES */
    00056
    00061 class SFBByteBuffer {
    00062
    00063 public:
    00064
    00065   u16 spaceAvailable() {
    00066     return ((BYTE_BUFFER_BYTES-1)-(newIndex-oldIndex))&BYTE_BUFFER_MASK;
    00067   }
    00068
    00069   u16 bytesAvailable() {
    00070     return (newIndex-oldIndex)&BYTE_BUFFER_MASK;
    00071   }
    00072
    00073 protected:
    00074   u8 (&buf)[BYTE_BUFFER_BYTES];
    00075   u16 oldIndex;
    00076   u16 newIndex;
    00077
    00078
    00079   SFBByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES])
    00080     : buf(buffer),
    00081       oldIndex(0),
    00082       newIndex(0)
    00083   { }
    00084
    00085   u16 wrap(u16 index) {
    00086     return index&BYTE_BUFFER_MASK;
    00087   }
    00088   void uncheckedStoreByte(u8 data) {
    00089     buf[newIndex] = data;
    00090     newIndex = wrap(newIndex+1);
    00091   }
    00092   u8 uncheckedFetchByte() {
    00093     u8 val = buf[oldIndex];
    00094     oldIndex = wrap(oldIndex+1);
    00095     return val;
    00096   }
    00097
    00098   enum {
    00099     BB_ESC = 0xA5,
    00100     BB_EESC = 0x00       // BB_ESC+BB_EESC -> single BB_ESC datum
    00101                          // BB_ESC+anything else -> overflow and/or hw error occurred, anything else==flags
    00102   };
    00103
    00104 };
    00105
    00116 class SFBRxByteBuffer : public SFBByteBuffer {
    00117 public:
    00118
    00119   enum {
    00120     BBFLAG_ERRORBITS= PK_OVERRUN|PK_PARITY|PK_FRAMING|PK_BREAK,
    00121     BBFLAG_OVERFLOW=  PK_BUFFER,
    00122     BBFLAG_ZERO=      PK_DELETED|PK_BAD_ESCAPE
    00123   };
    00124
    00125   SFBRxByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) : SFBByteBuffer(buffer), flags(0) { }
    00126
    00130   void resetBG() ;
    00131
    00135   int fetchByteBG() ;
    00136
    00140   void storeErrorStatusIL(u8 status) {
    00141     flags |= status;            // Just add in the bits, will be flushed in storeByteIL
    00142   }
    00143
    00147   void storeByteIL(u8 data) ;
    00148
    00149 private:
    00150   u8 flags;
    00151
    00152 };
    00153
    00164 class SFBTxByteBuffer : public SFBByteBuffer {
    00165 public:
    00166   SFBTxByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) : SFBByteBuffer(buffer), pendingCount(0) { }
    00167
    00171   void resetBG() ;
    00172
    00177   void storeByteBG(u8 data) ;
    00178
    00183   bool canStoreBytesBG(u32 count) ;
    00184
    00194   bool shipBytesBG() ;
    00195
    00196
    00201   int fetchByteIL() ;
    00202
    00203 private:
    00204   int pendingCount;
    00205
    00206 };
    00207
    00208 #endif  /* SFBBYTEBUFFER_H */
    00209