Wiselib
|
00001 /* 00002 * File: spyglass_packet.h 00003 * Author: Krueger 00004 * 00005 */ 00006 #ifndef SPYGLASS_PACKET_H 00007 #define SPYGLASS_PACKET_H 00008 00009 #include "util/delegates/delegate.hpp" 00010 00011 00012 namespace wiselib 00013 { 00014 #define SPYGLASS_MAXIMUM_SIZE 98 00015 #define SPYGLASS_PACKET_STANDARD_PACKET_SIZE 19 00016 00017 template<typename OsModel_P> 00018 00019 class SpyglassPacket { 00020 00021 public: 00022 //typedef OsModel_P OsModel; 00023 00024 00025 //typedef typename OsModel_P::Clock::time_t time_t; 00026 00027 typedef SpyglassPacket<OsModel_P> self_t; 00028 00032 uint16_t length_; 00033 00034 uint8_t version_; 00035 00039 uint8_t syntax_type_; 00040 00044 uint8_t semantic_type_; 00045 00049 uint16_t sender_id_; 00050 00054 uint32_t sec_; 00055 uint16_t millis_; 00056 00060 int16_t x_; 00061 00065 int16_t y_; 00066 00070 int16_t z_; 00071 00072 SpyglassPacket(): 00073 length_(SPYGLASS_PACKET_STANDARD_PACKET_SIZE), 00074 version_(2), 00075 syntax_type_(0), 00076 semantic_type_(0), 00077 sender_id_(0), 00078 sec_(0), 00079 millis_(0), 00080 //time_(Time(0,0)), 00081 x_(0), 00082 y_(0), 00083 z_(0) 00084 { 00085 }; 00086 00087 SpyglassPacket(uint16_t len, uint8_t syntax_type, uint8_t semantic_type, uint16_t sender_id, uint32_t sec, uint16 millis, int16_t x, int16_t y, int16_t z): 00088 length_(len), 00089 version_(2), 00090 syntax_type_(syntax_type), 00091 semantic_type_(semantic_type), 00092 sender_id_(sender_id), 00093 sec_(sec), 00094 millis_(millis), 00095 x_(x), 00096 y_(y), 00097 z_(z) 00098 { 00099 }; 00100 // ---------------------------------------------------------------------- 00101 00102 ~SpyglassPacket() 00103 {}; 00104 /* 00105 initialize the module 00106 */ 00107 void init () { 00108 }; 00109 // ---------------------------------------------------------------------- 00110 uint16_t 00111 serialize(uint8_t * buf, uint8_t buf_size) 00112 { 00113 if (buf_size >= SPYGLASS_PACKET_STANDARD_PACKET_SIZE) 00114 { 00115 buf[0] = ((length_-2)>>8)& 0xFF; 00116 buf[1]=(length_-2)& 0xFF; 00117 buf[2] = version_& 0xFF; 00118 buf[3] = syntax_type_& 0xFF; 00119 buf[4] = semantic_type_& 0xFF; 00120 buf[5] = (sender_id_>>8)& 0xFF; 00121 buf[6] = sender_id_ & 0xFF; 00122 00123 buf[7] = (sec_>>24)& 0xFF; 00124 buf[8] = (sec_>>16)& 0xFF; 00125 buf[9] = (sec_>>8)& 0xFF; 00126 buf[10] = sec_ & 0xFF; 00127 buf[11] = (millis_>>8)& 0xFF; 00128 buf[12]= millis_ & 0xFF; 00129 buf[13] = (x_>>8)& 0xFF; 00130 buf[14] = x_ & 0xFF; 00131 buf[15] = (y_>>8)& 0xFF; 00132 buf[16] = y_ & 0xFF; 00133 buf[17] = (z_>>8)& 0xFF; 00134 buf[18] = z_ & 0xFF; 00135 return SPYGLASS_PACKET_STANDARD_PACKET_SIZE; 00136 } 00137 return 0; 00138 } 00139 // ---------------------------------------------------------------------- 00140 uint16_t 00141 get_size() 00142 { 00143 return SPYGLASS_PACKET_STANDARD_PACKET_SIZE; 00144 } 00145 protected: 00146 /* 00147 uint8_t 00148 serializeInt16(int16_t value,uint8_t * buf, uint8_t buf_size,uint8_t startIndex) 00149 { 00150 if(startIndex+2>=buf_size) return 0; 00151 buf[startIndex] =(value>>8)& 0xFF; 00152 buf[startIndex+1] = value& 0xFF; 00153 return 2; 00154 } 00155 00156 uint8 00157 serializeInt64(int64 value,uint8 * buf, uint8 buf_size,uint8 startIndex) 00158 { 00159 if(startIndex+8>=buf_size) return 0; 00160 buf[startIndex] =(value>>56)& 0xFF; 00161 buf[startIndex+1] =(value>>48)& 0xFF; 00162 buf[startIndex+2] =(value>>40)& 0xFF; 00163 buf[startIndex+3] =(value>>32)& 0xFF; 00164 buf[startIndex+4] =(value>>24)& 0xFF; 00165 buf[startIndex+5] =(value>>16)& 0xFF; 00166 buf[startIndex+6] =(value>>8)& 0xFF; 00167 buf[startIndex+7] = value& 0xFF; 00168 return 8; 00169 } 00170 00171 uint8 00172 serializeUInt16(uint16 value,uint8 * buf, uint8 buf_size,uint8 startIndex) 00173 { 00174 if(startIndex+2>=buf_size) return 0; 00175 buf[startIndex] =(value>>8)& 0xFF; 00176 buf[startIndex+1] = value & 0xFF; 00177 return 2; 00178 } 00179 00180 uint8 00181 serializeUInt32(uint32 value,uint8 * buf, uint8 buf_size,uint8 startIndex) 00182 { 00183 if(startIndex+4>=buf_size) return 0; 00184 buf[startIndex] =(value>>24)& 0xFF; 00185 buf[startIndex+1] =(value>>16)& 0xFF; 00186 buf[startIndex+2] =(value>>8)& 0xFF; 00187 buf[startIndex+3] = value& 0xFF; 00188 return 4; 00189 } 00190 00191 uint8 00192 serializeUInt8(uint8 value,uint8 * buf, uint8 buf_size,uint8 startIndex) 00193 { 00194 if(startIndex+1>=buf_size) return 0; 00195 buf[startIndex] = value; 00196 return 1; 00197 } 00198 00199 uint8 00200 serializeFloat(float value,uint8 * buf, uint8 buf_size,uint8 startIndex) 00201 { 00202 if(startIndex+4>=buf_size) return 0; 00203 int intvalue ; 00204 memcpy(&intvalue, &value, 4); 00205 buf[startIndex] = (uint8) ((intvalue >> 24 )& 0xFF); 00206 buf[startIndex+1] = (uint8) ((intvalue >> 16 )& 0xFF); 00207 buf[startIndex+2] = (uint8) ((intvalue >> 8 )& 0xFF); 00208 buf[startIndex+3] = (uint8) (intvalue & 0xFF); 00209 return 4; 00210 } 00211 00212 */ 00213 }; 00214 } 00215 00216 #endif 00217 00218 /*----------------------------------------------------------------------- 00219 * Source $Source: /cvs/shawn/shawn/apps/spyglass/spyglass_packet.cpp,v $ 00220 * Version $Revision: 38 $ 00221 * Date $Date: 2007-06-08 14:30:12 +0200 (Fr, 08 Jun 2007) $ 00222 *----------------------------------------------------------------------- 00223 * $Log: spyglass_packet.cpp,v $ 00224 *-----------------------------------------------------------------------*/