Goby v2
message_var_head.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef MESSAGE_VAR_HEAD20100317H
24 #define MESSAGE_VAR_HEAD20100317H
25 
26 #include "message_var.h"
27 #include "message_var_int.h"
28 
29 #include "goby/common/time.h"
30 #include "goby/util/sci.h"
31 
32 namespace goby
33 {
34 namespace transitional
35 {
37 {
38  public:
39  DCCLMessageVarHead(const std::string& default_name, int bit_size)
40  : DCCLMessageVarInt(pow(2, bit_size) - 1, 0), bit_size_(bit_size),
41  default_name_(default_name)
42  {
43  set_name(default_name);
44  }
45 
46  private:
47  void initialize_specific()
48  {
49  if (default_name_ == name_)
50  source_var_.clear();
51  }
52 
53  virtual void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id) {}
54 
55  protected:
56  int bit_size_;
57  std::string default_name_;
58 };
59 
61 {
62  public:
64  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_TIME],
65  transitional::HEAD_TIME_SIZE)
66  {
67  }
68 
69  void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id)
70  {
71  double d;
72  v = (!v.empty() && v.get(d))
73  ? v
75  }
76 
77  void pre_encode(DCCLMessageVal& v)
78  {
79  double d;
80  v = (!v.empty() && v.get(d))
81  ? DCCLMessageVal(goby::util::as<std::string>(goby::common::unix_double2ptime(d)))
82  : v;
83  }
84 
85  void post_decode(DCCLMessageVal& v)
86  {
87  std::string s;
89  goby::util::as<boost::posix_time::ptime>(s)))
90  : v;
91  }
92 
93  void write_schema_to_dccl2(std::ofstream* proto_file, int sequence_number)
94  {
95  sequence_number_ = sequence_number;
96  *proto_file << "\t"
97  << "optional double " << name() << " = " << sequence_number
98  << " [(dccl.field).codec=\"_time\", (dccl.field).in_head=true];" << std::endl;
99  }
100 };
101 
103 {
104  public:
106  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_CCL_ID],
107  transitional::HEAD_CCL_ID_SIZE)
108  {
109  }
110 
111  void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id)
112  {
113  v = long(acomms::DCCL_CCL_HEADER);
114  }
115 };
116 
118 {
119  public:
121  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_DCCL_ID],
122  transitional::HEAD_DCCL_ID_SIZE)
123  {
124  }
125 
126  void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id)
127  {
128  v = (!v.empty()) ? v : DCCLMessageVal(long(id));
129  }
130 };
131 
133 {
134  public:
136  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_SRC_ID],
137  transitional::HEAD_SRC_ID_SIZE)
138  {
139  }
140 
141  void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id)
142  {
143  v = (!v.empty()) ? v : DCCLMessageVal(long(modem_id));
144  }
145 
146  virtual std::string additional_option_extensions() { return "(dccl.field).in_head=true"; }
147 };
148 
150 {
151  public:
153  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_DEST_ID],
154  transitional::HEAD_DEST_ID_SIZE)
155  {
156  }
157 
158  void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id)
159  {
160  v = (!v.empty()) ? v : DCCLMessageVal(long(acomms::BROADCAST_ID));
161  }
162 
163  virtual std::string additional_option_extensions() { return "(dccl.field).in_head=true"; }
164 };
165 
167 {
168  public:
170  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_MULTIMESSAGE_FLAG],
171  transitional::HEAD_FLAG_SIZE)
172  {
173  }
174 };
175 
177 {
178  public:
180  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_BROADCAST_FLAG],
181  transitional::HEAD_FLAG_SIZE)
182  {
183  }
184 };
185 
187 {
188  public:
190  : DCCLMessageVarHead(transitional::DCCL_HEADER_NAMES[transitional::HEAD_UNUSED],
191  transitional::HEAD_UNUSED_SIZE)
192  {
193  }
194 };
195 
196 } // namespace transitional
197 } // namespace goby
198 #endif
bool empty() const
was this just constructed with DCCLMessageVal() ?
Definition: message_val.h:142
bool get(std::string &s) const
extract as std::string (all reasonable casts are done)
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
Definition: time.h:104
const int BROADCAST_ID
special modem id for the broadcast destination - no one is assigned this address. Analogous to 192...
The global namespace for the Goby project.
double ptime2unix_double(boost::posix_time::ptime given_time)
convert from boost date_time ptime to the number of seconds (including fractional) since 1/1/1970 0:0...
Definition: time.cpp:28
boost::posix_time::ptime unix_double2ptime(double given_time)
convert to boost date_time ptime from the number of seconds (including fractional) since 1/1/1970 0:0...
Definition: time.cpp:47