00001 // Copyright (C) 2004 Pablo Mejia 00002 00003 00004 #ifndef SICK_MSG_H 00005 #define SICK_MSG_H 00006 00007 00008 #include "endian.H" 00009 00010 00011 namespace sick 00012 { 00013 using namespace endian; 00014 00015 class msg_builder_t; 00016 00017 class msg_t 00018 { 00019 protected: 00020 friend class msg_builder_t; 00021 00022 static unsigned const ENVELOPE_SIZE = 6; 00023 static unsigned const HEADER_SIZE = 4; 00024 static unsigned const FOOTER_SIZE = 2; 00025 00026 static unsigned const ADDRESS_OFFSET = 1; 00027 static unsigned const MSG_LEN_OFFSET = 2; 00028 static unsigned const MSG_ID_OFFSET = 4; 00029 00030 00031 bool m_valid; 00032 unsigned m_size; 00033 unsigned char const *m_data; 00034 00035 static unsigned calc_crc (unsigned char const *data, 00036 unsigned size); 00037 static unsigned skip_to_stx (unsigned char const *data, 00038 unsigned size); 00039 00040 public: 00041 static unsigned const STX = 0x02; 00042 static unsigned const ACK = 0x06; 00043 static unsigned const NAK = 0x15; 00044 00045 msg_t (); 00046 msg_t (msg_t const *msg); 00047 00048 unsigned decode (unsigned char const *buf, unsigned buf_size); 00049 void print (unsigned char const *buf, unsigned buf_size) const; 00050 00051 bool valid (void) const { return (m_valid); } 00052 unsigned size (void) const { return (m_size); } 00053 00054 bool is_ack (void) const 00055 { 00056 return (m_valid && m_size == 1 && m_data[0] == ACK); 00057 } 00058 00059 bool is_nak (void) const 00060 { 00061 return (m_valid && m_size == 1 && m_data[0] == NAK); 00062 } 00063 00064 unsigned stx (void) const 00065 { 00066 return (m_data[0]); 00067 } 00068 00069 unsigned msg_id (void) const 00070 { 00071 return (m_data[MSG_ID_OFFSET]); 00072 } 00073 unsigned address (void) const 00074 { 00075 return (m_data[ADDRESS_OFFSET]); 00076 } 00077 unsigned msg_len (void) const 00078 { 00079 return (letoh<u16_t> (m_data + MSG_LEN_OFFSET)); 00080 } 00081 unsigned checksum (void) const 00082 { 00083 return (letoh<u16_t> (m_data + HEADER_SIZE + 00084 msg_len ())); 00085 } 00086 }; 00087 }; 00088 00089 00090 #endif