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 <string.h>
00035 #include <sstream>
00036
00037 #ifdef NULLPTR
00038 #undef NULLPTR
00039 #endif
00040 #define NULLPTR 0
00042 typedef unsigned char uint8_;
00043 typedef unsigned short uint16_;
00044 typedef unsigned int uint32_;
00053 namespace cornelluniversity {
00054
00060 namespace vetserial {
00061
00062 static const uint8_ NUL = 0x00;
00063 static const uint8_ SOH = 0x01;
00064 static const uint8_ STX = 0x02;
00065 static const uint8_ ETX = 0x03;
00066 static const uint8_ EOT = 0x04;
00067 static const uint8_ ENQ = 0x05;
00068 static const uint8_ ACK = 0x06;
00069 static const uint8_ NAK = 0x07;
00070 static const uint8_ SYN = 0x0f;
00072 static const unsigned int MP3_TRACK_MAX = 0xFF;
00078 typedef enum STATUS_TYPE {
00079 STATUS_ERROR = 0x10,
00080 STATUS_READY = 0x20,
00081 STATUS_BUSY = 0x40
00082 } status_t;
00083
00088 typedef enum COMMAND_ID {
00089 CMD_CNTRL = 0x00,
00090 CMD_REQSTAT = 0x01,
00091 CMD_RSPSTAT = 0x02,
00092 CMD_PLAY_MP3 = 0x03,
00093 CMD_STOP_MP3 = 0x04,
00094 CMD_MAXCMD = 0xFF
00095 } cmnd_id;
00096
00101 typedef enum ERROR_CODE {
00102 E_NOERROR = 0x00,
00103 E_DATAOOB = 0x01,
00104 E_NULLPTR = 0x02,
00105 E_CHKSUM = 0x03,
00106 E_NONPROTO = 0xFE,
00107 E_UNKNOWN = 0xFF
00108 } err_code;
00109
00114 typedef enum FLAG_TYPE {
00115 FLAG_REQ = 0x01,
00116 FLAG_ACK = 0x02,
00117 FLAG_NAK = 0x04,
00118 FLAG_DATA = 0x08,
00119 FLAG_ERR = 0x10,
00120 FLAG_RDY = 0x20,
00121 FLAG_BUSY = 0x40,
00122 FLAG_RESRVD = 0x80
00123 } flag_t;
00124
00134 class base_command {
00135 public:
00136 static const unsigned int PKT_HEADER_SZ;
00137 static const unsigned int PKT_TAIL_SZ;
00138 static const unsigned int PKT_MAX_SZ;
00146 base_command( const cmnd_id cid );
00157 base_command( const uint8_ * in_pkt, uint32_ nbytes );
00161 virtual ~base_command( void );
00166 inline const uint8_ * packet( void ) const { return( buffer_ ); }
00171 inline const int packet_size( void ) const { return( buffersz_ ); }
00176 inline const uint8_ flags( void ) const { return(flags_); }
00181 inline void set_flag( const uint8_ f ){ flags_ |= f; }
00187 inline void clr_flag( const flag_t f ){ flags_ -= f; }
00194 inline bool flag_enabled( const flag_t f ) const { return( (flags_ & f) > 0 ? true : false ); }
00200 inline void error_code( const err_code id ){ this->err_ = id; }
00206 inline const err_code error_code( void ) const { return(this->err_); }
00213 inline void data_id( const uint8_ id ){ data_id_ = id; }
00219 inline const unsigned char data_id( void ) const { return(data_id_); }
00225 inline void payload_size( const uint16_ sz ){ this->payloadsz_ = sz; }
00231 inline const uint16_ payload_size( void ) const { return( this->payloadsz_ ); }
00239 virtual void execute( void ) = 0;
00240
00241 protected:
00242 uint8_ * buffer_;
00243 uint8_ * packet_;
00244 uint32_ buffersz_;
00245 uint32_ pktsz_;
00254 uint16_ crc16( const uint8_ * indata, const uint32_ nbytes );
00268 bool verify_crc16( const uint8_ * full_packet, const uint32_ nbytes );
00278 void header( void );
00294 void tail( const uint8_ * payload, const uint32_ payload_sz = 0 );
00295
00296 private:
00297 base_command( void ){;}
00298 base_command( const base_command & cpyref ){;}
00299
00300 uint8_ flags_;
00301 uint8_ data_id_;
00302 uint16_ payloadsz_;
00303 cmnd_id id_;
00304 err_code err_;
00305 };
00306
00314 class control_request : public base_command {
00315 public:
00316 control_request( void ) : base_command(CMD_CNTRL){;}
00317 virtual ~control_request( void ){;}
00318
00320 inline void ack_request( void ){ this->set_flag(FLAG_REQ|FLAG_ACK); }
00321
00323 inline void ack_response( void ){ this->set_flag(FLAG_ACK); }
00324
00326 inline void nak_request( void ){ this->set_flag(FLAG_REQ|FLAG_NAK); }
00327
00329 inline void nak_response( void ){ this->set_flag(FLAG_NAK); }
00330
00332 inline void set_responsive( void ){ this->set_flag(FLAG_REQ|FLAG_ACK|FLAG_NAK); }
00339 virtual void execute( void ){ this->header(); }
00340 };
00341
00348 class status_request : public base_command {
00349 public:
00350 status_request( void ) : base_command(CMD_REQSTAT){;}
00351 virtual ~status_request( void){;}
00359 virtual void execute( void );
00360 };
00361
00370 class status_response : public base_command {
00371 public:
00379 status_response( void ) : base_command(CMD_RSPSTAT), status_(STATUS_READY){;}
00389 status_response( const status_t s );
00400 status_response( const uint8_ * in_packet, uint32_ in_packet_sz );
00401 virtual ~status_response( void){;}
00407 inline const status_t status( void ) const { return(this->status_); }
00413 void status( const status_t t ) { this->status_ = t; this->set_flag(this->status_); }
00421 virtual void execute( void );
00422
00423 private:
00424 status_t status_;
00425 };
00426
00433 class mp3_play_request : public base_command {
00434 public:
00443 mp3_play_request( void ) : base_command(CMD_PLAY_MP3), device_(0), track_(0), duration_(0) {;}
00451 mp3_play_request( const uint8_ dev, const uint8_ trk, const uint16_ dur = 0) : base_command(CMD_PLAY_MP3), device_(dev), track_(trk), duration_(dur) {;}
00452 virtual ~mp3_play_request( void){;}
00457 inline const uint8_ track( void ) const { return(this->track_); }
00463 inline void track( const uint8_ track_number ) { this->track_ = track_number; }
00468 inline const uint8_ device( void ) const { return(this->device_); }
00474 inline void device( const uint8_ device_number ) { this->device_ = device_number; }
00480 inline const uint16_ duration( void ) const { return(this->duration_); }
00487 inline void duration( const uint16_ duration ) { this->duration_ = duration; }
00495 virtual void execute( void );
00496
00497 private:
00498 uint8_ device_;
00499 uint8_ track_;
00500 uint16_ duration_;
00501 };
00502
00510 class mp3_stop_request : public base_command {
00511 public:
00517 mp3_stop_request( void ) : base_command(CMD_STOP_MP3), device_(0), track_(0){;}
00526 mp3_stop_request( const uint8_ dev, const uint8_ trk ) : base_command(CMD_STOP_MP3), device_(dev), track_(trk){;}
00527 virtual ~mp3_stop_request( void){;}
00532 inline const uint8_ track( void ) const { return(this->track_); }
00538 inline void track( const uint8_ track_number ) { this->track_ = track_number; }
00543 inline const uint8_ device( void ) const { return(this->device_); }
00549 inline void device( const uint8_ device_number ) { this->device_ = device_number; }
00557 virtual void execute( void );
00558
00559 private:
00560 uint8_ device_;
00561 uint8_ track_;
00562 };
00563
00564 }
00565 }
00566
00567 #endif
00568
00569
00570
00571
00572
00573
00574