00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00026 #pragma once
00027 #pragma warning( disable : 4290 )
00028 #pragma warning( disable : 4996 )
00029 #endif
00030
00031 #ifndef VET_SERIAL_H_INCLUDED
00032 #define VET_SERIAL_H_INCLUDED
00033 #include "excepts.h"
00034 #include <math.h>
00035 #include <string.h>
00036 #include <sstream>
00037 #include <queue>
00038
00039 extern "C" {
00040 #ifdef INTMAX_C
00041 #undef INTMAX_C
00042 #undef UINTMAX_C
00043 #endif
00044 #include "messaging.h"
00045 }
00046
00047 #ifdef NULLPTR
00048 #undef NULLPTR
00049 #endif
00050 #define NULLPTR 0
00052 typedef char int8_;
00053 typedef unsigned char uint8_;
00054 typedef short int16_;
00055 typedef unsigned short uint16_;
00056 typedef int int32_;
00057 typedef unsigned int uint32_;
00058 typedef int int64_;
00059 typedef unsigned int uint64_;
00069 namespace cornelluniversity {
00070
00076 namespace vetserial {
00077
00078 static const uint8_ NUL = 0x00;
00079 static const uint8_ SOH = 0x01;
00080 static const uint8_ STX = 0x02;
00081 static const uint8_ ETX = 0x03;
00082 static const uint8_ EOT = 0x04;
00083 static const uint8_ ENQ = 0x05;
00084 static const uint8_ ACK = 0x06;
00085 static const uint8_ NAK = 0x07;
00086 static const uint8_ SYN = 0x0f;
00088 static const unsigned int MP3_TRACK_MAX = 0xFF;
00094 typedef enum STATUS_TYPE {
00095 STATUS_ERROR = 0x10,
00096 STATUS_READY = 0x20,
00097 STATUS_BUSY = 0x40
00098 } status_t;
00099
00104 typedef enum COMMAND_ID {
00105 CMD_CNTRL = 0x00,
00106 CMD_REQSTAT = 0x01,
00107 CMD_RSPSTAT = 0x02,
00108 CMD_PLAY_MP3 = 0x03,
00109 CMD_STOP_MP3 = 0x04,
00110 CMD_MSG_PRXY = 0x05,
00111 CMD_CPR_CMPR = 0x06,
00112 CMD_MAXCMD = 0xFF
00113 } cmnd_id;
00114
00119 typedef enum ERROR_CODE {
00120 E_NOERROR = 0x00,
00121 E_DATAOOB = 0x01,
00122 E_NULLPTR = 0x02,
00123 E_CHKSUM = 0x03,
00124 E_NONPROTO = 0xFE,
00125 E_UNKNOWN = 0xFF
00126 } err_code;
00127
00132 typedef enum FLAG_TYPE {
00133 FLAG_REQ = 0x01,
00134 FLAG_ACK = 0x02,
00135 FLAG_NAK = 0x04,
00136 FLAG_DATA = 0x08,
00137 FLAG_ERR = 0x10,
00138 FLAG_RDY = 0x20,
00139 FLAG_BUSY = 0x40,
00140 FLAG_RESRVD = 0x80
00141 } flag_t;
00142
00152 class base_command {
00153 public:
00154 static const unsigned int PKT_HEADER_SZ;
00155 static const unsigned int PKT_TAIL_SZ;
00156 static const unsigned int PKT_MAX_SZ;
00164 base_command( const cmnd_id cid );
00175 base_command( const uint8_ * in_pkt, uint32_ nbytes );
00179 virtual ~base_command( void );
00184 inline const uint8_ * packet( void ) const { return( buffer_ ); }
00189 inline const int packet_size( void ) const { return( buffersz_ ); }
00190 inline void command_id( const cmnd_id cid ) { this->id_ = cid; }
00191 inline const cmnd_id command_id( void ) const { return(this->id_); }
00192
00197 inline const uint8_ flags( void ) const { return(flags_); }
00202 inline void set_flag( const uint8_ f ){ flags_ |= f; }
00208 inline void clr_flag( const flag_t f ){ flags_ -= f; }
00215 inline bool flag_enabled( const flag_t f ) const { return( (flags_ & f) > 0 ? true : false ); }
00221 inline void error_code( const err_code id ){ this->err_ = id; }
00227 inline const err_code error_code( void ) const { return(this->err_); }
00234 inline void data_id( const uint8_ id ){ data_id_ = id; }
00240 inline const unsigned char data_id( void ) const { return(data_id_); }
00246 inline void payload_size( const uint16_ sz ){ this->payloadsz_ = sz; }
00252 inline const uint16_ payload_size( void ) const { return( this->payloadsz_ ); }
00266 bool verify_crc16( const uint8_ * full_packet, const uint32_ nbytes );
00273 inline const bool failed_tail_crc( void ) const { return( tail_crc_err_ ); }
00280 virtual void serialize( void ) = 0;
00287 virtual void deserialize( void ) = 0;
00288
00289 protected:
00290 uint8_ * buffer_;
00291 uint8_ * packet_;
00292 uint32_ buffersz_;
00293 uint32_ pktsz_;
00302 uint16_ crc16( const uint8_ * indata, const uint32_ nbytes );
00312 void header( void );
00328 void tail( const uint8_ * payload, const uint32_ payload_sz = 0 );
00329
00330 private:
00331 base_command( void ){;}
00332 base_command( const base_command & cpyref ){;}
00333
00334 bool tail_crc_err_;
00335 bool head_crc_err_;
00336 uint8_ flags_;
00337 uint8_ data_id_;
00338 uint16_ payloadsz_;
00339 cmnd_id id_;
00340 err_code err_;
00341 };
00342
00350 class control_request : public base_command {
00351 public:
00352 control_request( void ) : base_command(CMD_CNTRL){;}
00353 control_request( const uint8_ * in_pkt, uint32_ nbytes ) : base_command(in_pkt, nbytes){;}
00354 virtual ~control_request( void ){;}
00355
00357 inline void ack_request( void ){ this->set_flag(FLAG_REQ|FLAG_ACK); }
00358
00360 inline void ack_response( void ){ this->set_flag(FLAG_ACK); }
00361
00363 inline void nak_request( void ){ this->set_flag(FLAG_REQ|FLAG_NAK); }
00364
00366 inline void nak_response( void ){ this->set_flag(FLAG_NAK); }
00367
00369 inline void set_responsive( void ){ this->set_flag(FLAG_REQ|FLAG_ACK|FLAG_NAK); }
00376 virtual void serialize( void ){ this->header(); }
00384 virtual void deserialize( void );
00385
00386 };
00387
00394 class status_request : public base_command {
00395 public:
00396 status_request( void ) : base_command(CMD_REQSTAT){;}
00397 status_request( const uint8_ * in_pkt, uint32_ nbytes ) : base_command(in_pkt, nbytes){;}
00398 virtual ~status_request( void){;}
00406 virtual void serialize( void );
00414 virtual void deserialize( void );
00415 };
00416
00425 class status_response : public base_command {
00426 public:
00434 status_response( void ) : base_command(CMD_RSPSTAT), status_(STATUS_READY){;}
00444 status_response( const status_t s );
00455 status_response( const uint8_ * in_packet, uint32_ in_packet_sz ) : base_command(in_packet, in_packet_sz){;}
00456 virtual ~status_response( void){;}
00462 inline const status_t status( void ) const { return(this->status_); }
00468 void status( const status_t t ) { this->status_ = t; this->set_flag(this->status_); }
00476 virtual void serialize( void );
00484 virtual void deserialize( void );
00485
00486 private:
00487 status_t status_;
00488 };
00489
00496 class mp3_play_request : public base_command {
00497 public:
00506 mp3_play_request( void ) : base_command(CMD_PLAY_MP3), device_(0), track_(0), duration_(0) {;}
00514 mp3_play_request( const uint8_ dev, const uint8_ trk, const uint16_ dur = 0) : base_command(CMD_PLAY_MP3), device_(dev), track_(trk), duration_(dur) {;}
00521 mp3_play_request( const uint8_ * in_pkt, uint32_ nbytes ) : base_command(in_pkt, nbytes), device_(0), track_(0), duration_(0) {;}
00522
00523 virtual ~mp3_play_request( void){;}
00528 inline const uint8_ track( void ) const { return(this->track_); }
00534 inline void track( const uint8_ track_number ) { this->track_ = track_number; }
00539 inline const uint8_ device( void ) const { return(this->device_); }
00545 inline void device( const uint8_ device_number ) { this->device_ = device_number; }
00551 inline const uint16_ duration( void ) const { return(this->duration_); }
00558 inline void duration( const uint16_ duration ) { this->duration_ = duration; }
00566 virtual void serialize( void );
00574 virtual void deserialize( void );
00575
00576 private:
00577 uint8_ device_;
00578 uint8_ track_;
00579 uint16_ duration_;
00580 };
00581
00589 class mp3_stop_request : public base_command {
00590 public:
00596 mp3_stop_request( void ) : base_command(CMD_STOP_MP3), device_(0), track_(0){;}
00605 mp3_stop_request( const uint8_ dev, const uint8_ trk ) : base_command(CMD_STOP_MP3), device_(dev), track_(trk){;}
00612 mp3_stop_request( const uint8_ * in_pkt, uint32_ nbytes ) : base_command(in_pkt, nbytes), device_(0), track_(0){;}
00613
00614 virtual ~mp3_stop_request( void){;}
00619 inline const uint8_ track( void ) const { return(this->track_); }
00625 inline void track( const uint8_ track_number ) { this->track_ = track_number; }
00630 inline const uint8_ device( void ) const { return(this->device_); }
00636 inline void device( const uint8_ device_number ) { this->device_ = device_number; }
00644 virtual void serialize( void );
00652 virtual void deserialize( void );
00653
00654 private:
00655 uint8_ device_;
00656 uint8_ track_;
00657 };
00658
00666 class pubsub_message : public base_command {
00667
00668 public:
00675 pubsub_message( void ) : base_command(CMD_MSG_PRXY), msg_(NULLPTR), msg_byte_count_(0){;}
00684 pubsub_message( Message * m );
00690 pubsub_message( const uint8_ * in_pkt, uint32_ nbytes ) : base_command( in_pkt, nbytes ), msg_(NULLPTR), msg_byte_count_(0){;}
00694 virtual ~pubsub_message( void);
00701 void set_message( Message * m );
00710 Message * get_message( void );
00711 inline const uint16_ message_length( void ) const { return(msg_byte_count_); }
00720 virtual void serialize( void );
00729 virtual void deserialize( void );
00730
00731 private:
00739 uint16_ byte_size( const Message_content_type mct );
00746 uint16_ msg_size( void );
00755 void msgcpy( const Message * orig, Message * copy );
00756 inline void msgcpy( const Message * orig ){ this->msgcpy( orig, this->msg_ ); }
00762 void msgdestroy( Message * m );
00763 inline void msgdestroy( void ){ this->msgdestroy( this->msg_ ); }
00764
00765 Message * msg_;
00766 uint16_ msg_byte_count_;
00768 };
00769
00775 class cpr_compression_state : public base_command {
00776
00777 public:
00785 cpr_compression_state( void );
00794 cpr_compression_state( const uint16_ pos );
00803 cpr_compression_state( const uint16_ * cpy, const int arraysz );
00809 cpr_compression_state( const uint8_ * in_pkt, uint32_ nbytes );
00810
00814 virtual ~cpr_compression_state( void ) { if(positions_) delete positions_; positions_ = NULLPTR; }
00820 void position( const uint16_ pos );
00821 inline void position( const int pos ) { this->position( static_cast<uint16_>(pos) ); }
00828 void position( const uint16_ * cpy, const int arraysz );
00838 const uint16_ position( const bool pop_position = false );
00843 inline const std::queue<uint16_>* all_positions( void ) const { return(positions_); }
00848 inline const size_t position_count( void ) const { return(positions_->size()); }
00849
00858 virtual void serialize( void );
00867 void deserialize( void );
00868
00869 private:
00870 std::queue<uint16_> *positions_;
00871
00872 };
00873
00874 }
00875 }
00876
00877 #endif
00878
00879
00880
00881
00882
00883
00884