SFBPacket.h

Go to the documentation of this file.
00001 /*                                             -*- mode:C++; fill-column:100 -*-
00002   SFBPacket.h - Packet structure and access methods
00003   Copyright (C) 2009 The Regents of the University of New Mexico.  All rights reserved.
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU General Public License
00016   along with this library; if not, write to the Free Software
00017   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00018   USA
00019 */
00020 
00027 #ifndef SFBPACKET_H_
00028 #define SFBPACKET_H_
00029 
00030 #include "SFBTypes.h"
00031 #include "SFBAssert.h"
00032 
00038 struct PacketHeader {
00039   u8 source;                
00040   u8 flags;  /* PK_OVERRUN|PK_PARITY|PK_FRAMING|PK_BREAK|PK_BUFFER|PK_BAD_ESCAPE */
00041   u8 cursor;
00042   u8 length;
00043 };
00044 
00046 struct PacketBuffer {
00047   union {
00048     u32 wh;
00049     PacketHeader ph;
00050   } h;
00051   u8 bbuf[SINGLE_PACKET_BUFFER_SIZE_BYTES];
00052 };
00055 /* SFB packet framing special character codes */
00056 #define PFSC_END  ((u8) '\012')  /* End of packet. AKA 'newline', ^J, 0x0a, NOT carriage return */
00057 #define PFSC_ESC  ((u8) '\333')  /* Indicates escaped byte code follows, 0xdb */
00058 #define PFSC_EEND ((u8) '\334')  /* ESC EEND means END data byte, 0xdc */
00059 #define PFSC_EESC ((u8) '\335')  /* ESC EESC means ESC data byte, 0xdd */
00060 
00061 #define CHECK_BYTE_INIT_VALUE (0xF0)
00062 #define CHECK_BYTE_UPDATE(v,d) ((v) =  ((v<<1)|((v>>7)&1))^d)
00063 
00068 inline const PacketHeader & packetHeaderInternalUnsafeConst(const u8 * packet) {
00069   return *(const PacketHeader *) (packet-4);
00070 }
00071 
00072 inline PacketHeader & packetHeaderInternalUnsafe(u8 * packet) {
00073   return *(PacketHeader *) (packet-4);
00074 }
00075 
00076 extern u8 packetLength(const u8 * packet);
00077 
00078 extern u8 packetReadLength(const u8 * packet);
00079 
00080 extern u8 packetFlags(const u8 * packet);
00081 
00082 extern u8 packetSource(const u8 * packet);
00083 
00084 extern u8 packetCursor(const u8 * packet);
00085 
00086 extern bool packetCheckByteValid(const u8 * packet);
00087 
00088 #define API_ASSERT_VALID_PACKET(u8ptr) API_ASSERT(validPacket(u8ptr),E_API_INVALID_PACKET)
00089 
00090 extern bool validPacket(const u8 * packet) ;
00091 
00092 extern void packetReread(u8 * packet, u32 newIndex = 0) ;
00093 
00094 extern bool packetRead(u8 * packet, int &result, int code = DEC, u32 maxLen = MAX_PACKET_LENGTH) ;
00095 
00096 extern bool packetRead(u8 * packet, u32 &result, int code = DEC, u32 maxLen = MAX_PACKET_LENGTH) ;
00097 
00098 extern bool packetReadCheckByte(u8 * packet) ;
00099 
00100 extern bool packetReadEOF(u8 * packet) ;
00101 
00102 extern bool packetReadPacket(u8 * packet, u8 *& subpacket) ;
00103 
00104 extern bool zpacketPrefix(u8 * packet, const char * to);
00105 
00106 extern u8 * makePacket(u8 * buffer, u32 bufferLength, u8 face, const char * packetData);
00107 
00108 extern u8 * makePacket(u8 * buffer, u32 bufferLength, u8 face, const u8 * packetData, u32 packetDataLength);
00109 
00114 #endif /* SFBPACKET_H_ */

Generated on Sun Oct 18 09:58:55 2009 for SFB by doxygen 1.5.9