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 f[4];
00040   enum Fields {
00041     SOURCE,                   /* NORTH, SOUTH, EAST, WEST, or some more exotic source */ 
00042     FLAGS,                    /* PK_OVERRUN|PK_PARITY|PK_FRAMING|PK_BREAK|PK_BUFFER|PK_BAD_ESCAPE */
00043     CURSOR,                   /* Current read index within the packet */ 
00044     LENGTH,                   /* Overall packet data length (excluding header and trailing null) */ 
00045     FIELD_COUNT
00046   };
00047 };
00048 
00050 struct PacketBuffer {
00051   union {
00052     u32 wh;
00053     PacketHeader ph;
00054   } h;
00055   u8 bbuf[SINGLE_PACKET_BUFFER_SIZE_BYTES];
00056 };
00059 /* SFB packet framing special character codes */
00060 #define PFSC_END  ((u8) '\012')  /* End of packet. AKA 'newline', ^J, 0x0a, NOT carriage return */
00061 #define PFSC_ESC  ((u8) '\333')  /* Indicates escaped byte code follows, 0xdb */
00062 #define PFSC_EEND ((u8) '\334')  /* ESC EEND means END data byte, 0xdc */
00063 #define PFSC_EESC ((u8) '\335')  /* ESC EESC means ESC data byte, 0xdd */
00064 
00065 #define CHECK_BYTE_INIT_VALUE (0xF0)
00066 #define CHECK_BYTE_UPDATE(v,d) ((v) =  ((v<<1)|((v>>7)&1))^d)
00067 
00072 inline const PacketHeader & packetHeaderInternalUnsafeConst(const u8 * packet) {
00073   return *(const PacketHeader *) (packet-4);
00074 }
00075 
00076 inline PacketHeader & packetHeaderInternalUnsafe(u8 * packet) {
00077   return *(PacketHeader *) (packet-4);
00078 }
00079 
00092 extern bool packetEqual(const u8 * packet1, const u8 * packet2);
00093 
00094 extern u8 packetLength(const u8 * packet);
00095 
00096 extern u8 packetReadLength(const u8 * packet);
00097 
00098 extern u8 packetFlags(const u8 * packet);
00099 
00100 extern u8 packetSource(const u8 * packet);
00101 
00102 extern u8 packetCursor(const u8 * packet);
00103 
00104 extern bool packetCheckByteValid(const u8 * packet);
00105 
00106 #define API_ASSERT_VALID_PACKET(u8ptr) API_ASSERT(validPacket(u8ptr),E_API_INVALID_PACKET)
00107 
00108 extern bool validPacket(const u8 * packet) ;
00109 
00110 extern void packetReread(u8 * packet, u32 newIndex = 0) ;
00111 
00112 extern bool packetRead(u8 * packet, int &result, int code = DEC, u32 maxLen = MAX_PACKET_LENGTH) ;
00113 
00114 extern bool packetRead(u8 * packet, u32 &result, int code = DEC, u32 maxLen = MAX_PACKET_LENGTH) ;
00115 
00116 extern bool packetRead(u8 * packet, u64 &result) ;
00117 
00118 extern bool packetRead(u8 * packet, u8 * result, u32 length) ;
00119 
00120 extern bool packetReadCheckByte(u8 * packet) ;
00121 
00122 extern bool packetReadEOF(u8 * packet) ;
00123 
00124 extern bool packetReadPacket(u8 * packet, u8 *& subpacket) ;
00125 
00126 extern bool zpacketPrefix(u8 * packet, const char * to);
00127 
00128 extern u8 * makePacket(u8 * buffer, u32 bufferLength, u8 face, const char * packetData);
00129 
00130 extern u8 * makePacket(u8 * buffer, u32 bufferLength, u8 face, const u8 * packetData, u32 packetDataLength);
00131 
00136 #endif /* SFBPACKET_H_ */

Generated on Fri Apr 22 06:54:11 2011 for SFB by doxygen 1.5.9