Wiselib
wiselib.testing/external_interface/pc/com_isense_packet.h
Go to the documentation of this file.
00001 // vim: set noexpandtab ts=4 sw=4:
00002 
00003 #ifndef COM_UART_PACKET_H
00004 #define COM_UART_PACKET_H
00005 
00006 #include <stdint.h>
00007 #include <cassert>
00008 #include "util/pstl/vector_static.h"
00009 
00010 //#include "external_interface/pc/pc_os_model.h"
00011 
00012 namespace wiselib {
00013    
00014    template<
00015       typename OsModel_P,
00016       int MaxPacketSize = 255
00017    >
00018    class ComISensePacket {
00019       public:
00020          typedef OsModel_P OsModel;
00021          typedef typename OsModel::size_t size_t;
00022          
00023          enum MessageType {
00024             MESSAGE_TYPE_RESET = 1, MESSAGE_TYPE_SERAERIAL, MESSAGE_TYPE_TIME,
00025             MESSAGE_TYPE_CAMERA_APP, MESSAGE_TYPE_AMR_APP, MESSAGE_TYPE_ACC_APP,
00026             MESSAGE_TYPE_IN_RESERVED_1, MESSAGE_TYPE_IN_RESERVED_2, MESSAGE_TYPE_IN_RESERVED_3,
00027             MESSAGE_TYPE_CUSTOM_IN_1, MESSAGE_TYPE_CUSTOM_IN_2, MESSAGE_TYPE_CUSTOM_IN_3,
00028             
00029             MESSAGE_TYPE_DEBUG = 100, MESSAGE_TYPE_INFO, MESSAGE_TYPE_WARNING, MESSAGE_TYPE_ERROR, MESSAGE_TYPE_LOG,
00030             MESSAGE_TYPE_PLOT, MESSAGE_TYPE_CUSTOM_OUT,
00031             
00032             MESSAGE_TYPE_TIMEREQUEST = 109, MESSAGE_TYPE_AUDIO, MESSAGE_TYPE_SPYGLASS, MESSAGE_TYPE_FLOAT_BUFFER,
00033             MESSAGE_TYPE_SQL
00034          };
00035          
00036          enum SubType {
00037             SUB_NONE = 0,
00038             SUB_RADIO_GET_ADDRESS = 'a',
00039             SUB_RADIO_ADDRESS = 'N',
00040             SUB_RADIO_DISTANCE_TEST = 'D',
00041             SUB_RADIO_IN = 'I', SUB_RADIO_OUT = 'O',
00042             SUB_SET_TX_POWER = 'T',
00043             SUB_GET_TX_POWER = 't',
00044             SUB_TX_POWER = 'P'
00045          };
00046          
00047          ComISensePacket(SubType st, MessageType t = MESSAGE_TYPE_CUSTOM_IN_1);
00048          ComISensePacket(size_t, uint8_t*);
00049          
00050          uint8_t* header();
00051          size_t header_size();
00052          uint8_t* data();
00053          size_t data_size();
00054          
00055          void push_header(uint8_t);
00056          void push_header16(uint16_t);
00057          
00058          void set_data(size_t, uint8_t*);
00059          
00060          MessageType type();
00061          SubType subtype();
00062          
00063       private:
00064          MessageType type_;
00065          SubType subtype_;
00066          
00067          vector_static<OsModel, uint8_t, MaxPacketSize> header_;
00068          uint8_t* data_;
00069          size_t data_size_;
00070          
00071          static size_t header_size(SubType);
00072    }; // ComISensePacket
00073    
00074    template<typename OsModel_P, int MaxPacketSize>
00075    ComISensePacket<OsModel_P, MaxPacketSize>::
00076 	ComISensePacket(SubType st, MessageType t) {
00077       push_header(t);
00078       if(st != SUB_NONE) {
00079          push_header(st);
00080          push_header(0);
00081       }
00082       
00083       data_size_ = 0;
00084       data_ = 0;
00085    }
00086    
00087    template<typename OsModel_P, int MaxPacketSize>
00088    ComISensePacket<OsModel_P, MaxPacketSize>::
00089 	ComISensePacket(size_t size, uint8_t* data) {
00090       assert(size >= 1);
00091       header_.push_back(data[0]);
00092       if(type() == MESSAGE_TYPE_CUSTOM_IN_1) {
00093          header_.push_back(data[1]);
00094          size_t size_h = header_size(subtype()) - 1;
00095          for(size_t i=0; i<size_h; i++) {
00096             header_.push_back(data[i+2]);
00097          }
00098       }
00099    }
00100    
00101    template<typename OsModel_P, int MaxPacketSize>
00102    typename ComISensePacket<OsModel_P, MaxPacketSize>::size_t ComISensePacket<OsModel_P, MaxPacketSize>::
00103 	data_size() { return data_size_; }
00104    
00105    template<typename OsModel_P, int MaxPacketSize>
00106    uint8_t* ComISensePacket<OsModel_P, MaxPacketSize>::
00107 	data() { return data_; }
00108    
00109    template<typename OsModel_P, int MaxPacketSize>
00110    typename ComISensePacket<OsModel_P, MaxPacketSize>::size_t ComISensePacket<OsModel_P, MaxPacketSize>::
00111 	header_size() { return header_.size(); }
00112    
00113    template<typename OsModel_P, int MaxPacketSize>
00114    uint8_t* ComISensePacket<OsModel_P, MaxPacketSize>::
00115 	header() { return header_.data(); }
00116    
00117    template<typename OsModel_P, int MaxPacketSize>
00118    void ComISensePacket<OsModel_P, MaxPacketSize>::
00119 	push_header(uint8_t byte) {
00120       header_.push_back(byte);
00121    }
00122    
00123    template<typename OsModel_P, int MaxPacketSize>
00124    void ComISensePacket<OsModel_P, MaxPacketSize>::
00125 	push_header16(uint16_t word) {
00126       header_.push_back(word >> 8);
00127       header_.push_back(word & 0xff);
00128    }
00129    
00130    template<typename OsModel_P, int MaxPacketSize>
00131    void ComISensePacket<OsModel_P, MaxPacketSize>::
00132 	set_data(size_t size, uint8_t* data) {
00133       data_size_ = size;
00134       data_ = data;
00135    }
00136    
00137    template<typename OsModel_P, int MaxPacketSize>
00138    typename ComISensePacket<OsModel_P, MaxPacketSize>::MessageType ComISensePacket<OsModel_P, MaxPacketSize>::
00139 	type() {
00140       return static_cast<MessageType>(header_[0]);
00141    }
00142    
00143    template<typename OsModel_P, int MaxPacketSize>
00144    typename ComISensePacket<OsModel_P, MaxPacketSize>::SubType ComISensePacket<OsModel_P, MaxPacketSize>::
00145 	subtype() {
00146       return static_cast<SubType>(header_[1]);
00147    }
00148    
00153    template<typename OsModel_P, int MaxPacketSize>
00154    typename ComISensePacket<OsModel_P, MaxPacketSize>::size_t ComISensePacket<OsModel_P, MaxPacketSize>::
00155 	header_size(SubType s) {
00156       switch(s) {
00157          case SUB_NONE:
00158          case SUB_RADIO_GET_ADDRESS: return 1;
00159          case SUB_RADIO_ADDRESS:
00160          case SUB_RADIO_OUT: return 3;
00161          case SUB_RADIO_IN: return 0; // TODO!
00162          case SUB_RADIO_DISTANCE_TEST: return 0; // TODO!
00163       }
00164       return 0;
00165    }
00166    
00167 } // ns wiselib
00168 
00169 #endif // COM_UART_PACKET_H
00170 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines