Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
acomms/libdccl/message_var_hex.h
00001 // copyright 2008, 2009 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of the Dynamic Compact Control Language (DCCL),
00004 // the goby-acomms codec. goby-acomms is a collection of libraries 
00005 // for acoustic underwater networking
00006 //
00007 // This program is free software: you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation, either version 3 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This software is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020 #ifndef MESSAGE_VAR_HEX20100317H
00021 #define MESSAGE_VAR_HEX20100317H
00022 
00023 #include "message_var.h"
00024 #include "dccl_exception.h"
00025 
00026 #include "goby/util/binary.h"
00027 #include "goby/util/string.h"
00028 
00029 namespace goby
00030 {
00031     namespace acomms
00032     {   
00033         class DCCLMessageVarHex : public DCCLMessageVar
00034         {
00035           public:
00036 
00037           DCCLMessageVarHex()
00038               : DCCLMessageVar(),
00039                 num_bytes_(0)
00040                 { }
00041 
00042             int calc_size() const
00043             { return num_bytes_*acomms::BITS_IN_BYTE; }
00044 
00045             void set_num_bytes(unsigned num_bytes) {num_bytes_ = num_bytes;}
00046             void set_num_bytes(const std::string& s) { set_num_bytes(boost::lexical_cast<unsigned>(s)); }
00047 
00048             unsigned num_bytes() const {return num_bytes_;}        
00049             DCCLType type() const { return dccl_hex; }        
00050         
00051           private:
00052             void initialize_specific()
00053             { }
00054         
00055             boost::dynamic_bitset<unsigned char> encode_specific(const DCCLMessageVal& v)
00056             {
00057                 std::string s = v;
00058             
00059                 if(s.length() == bytes2nibs(num_bytes_))
00060                     return util::hex_string2dyn_bitset(s, calc_size());
00061                 else if(s.length() < bytes2nibs(num_bytes_))
00062                     throw(DCCLException(std::string("Passed hex value (" + s + ") is too short. Should be " + util::as<std::string>(num_bytes_) +  " bytes")));
00063                 else if(s.length() > bytes2nibs(num_bytes_))
00064                     throw(DCCLException(std::string("Passed hex value (" + s + ") is too long. Should be " + util::as<std::string>(num_bytes_) +  " bytes")));
00065                 else
00066                     return boost::dynamic_bitset<unsigned char>();
00067             
00068             }        
00069 
00070             DCCLMessageVal decode_specific(boost::dynamic_bitset<unsigned char>& b)
00071             {
00072                 return DCCLMessageVal(util::dyn_bitset2hex_string(b, num_bytes_));
00073             }
00074 
00075             void get_display_specific(std::stringstream& ss) const
00076             { }
00077 
00078           private:
00079             unsigned num_bytes_;
00080 
00081         };
00082     }
00083 }
00084 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends