Goby v2
message.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 MESSAGE20091211H
24 #define MESSAGE20091211H
25 
26 #include <algorithm>
27 #include <bitset>
28 #include <iostream>
29 #include <map>
30 #include <set>
31 #include <sstream>
32 #include <vector>
33 
34 #include <boost/foreach.hpp>
35 #include <boost/shared_ptr.hpp>
36 
37 #include "goby/acomms/protobuf/queue.pb.h"
38 
39 #include "message_var.h"
40 #include "message_var_bool.h"
41 #include "message_var_enum.h"
42 #include "message_var_float.h"
43 #include "message_var_head.h"
44 #include "message_var_hex.h"
45 #include "message_var_int.h"
46 #include "message_var_static.h"
47 #include "message_var_string.h"
48 
49 #include "dccl_constants.h"
50 #include "message_publish.h"
51 
52 namespace goby
53 {
54 namespace transitional
55 {
56 // the DCCLMessage itself
58 {
59  public:
60  // Added in Goby2 for transition to new Protobuf structure
61  void write_schema_to_dccl2(std::ofstream* proto_file);
62 
63  DCCLMessage();
64 
65  // set
66  void set_name(const std::string& name) { name_ = name; }
67 
68  void set_id(unsigned id) { id_ = id; }
69  template <typename T> void set_id(const T& t) { set_id(util::as<unsigned>(t)); }
70 
71  void set_trigger(const std::string& trigger_type) { trigger_type_ = trigger_type; }
72 
73  void set_trigger_var(const std::string& trigger_var) { trigger_var_ = trigger_var; }
74 
75  void set_trigger_time(double trigger_time) { trigger_time_ = trigger_time; }
76  template <typename T> void set_trigger_time(const T& t)
77  {
78  set_trigger_time(util::as<double>(t));
79  }
80 
81  void set_trigger_mandatory(const std::string& trigger_mandatory)
82  {
83  trigger_mandatory_ = trigger_mandatory;
84  }
85 
86  void set_in_var(const std::string& in_var) { in_var_ = in_var; }
87 
88  void set_out_var(const std::string& out_var) { out_var_ = out_var; }
89 
90  void set_size(unsigned size) { size_ = size; }
91 
92  template <typename T> void set_size(const T& t) { set_size(util::as<unsigned>(t)); }
93 
94  void set_repeat_enabled(unsigned repeat_enabled) { repeat_enabled_ = repeat_enabled; }
95  template <typename T> void set_repeat_enabled(const T& t)
96  {
97  set_repeat_enabled(util::as<unsigned>(t));
98  }
99 
100  void add_message_var(const std::string& type);
101  void add_publish();
102 
103  void set_descriptor(const google::protobuf::Descriptor* descriptor)
104  {
105  descriptor_ = descriptor;
106  }
107 
108  //get
109  std::string name() const { return name_; }
110  unsigned id() const { return id_; }
111  unsigned size() const { return size_; }
112  std::string trigger_var() const { return trigger_var_; }
113  std::string trigger_mandatory() const { return trigger_mandatory_; }
114  double trigger_time() const { return trigger_time_; }
115  unsigned trigger_number() const { return trigger_number_; }
116  std::string trigger_type() const { return trigger_type_; }
117  std::string in_var() const { return in_var_; }
118  std::string out_var() const { return out_var_; }
119  bool repeat_enabled() const { return repeat_enabled_; }
120  unsigned repeat() const { return repeat_; }
121 
122  DCCLMessageVar& last_message_var() { return *layout_.back(); }
123  DCCLMessageVar& header_var(transitional::DCCLHeaderPart p) { return *header_[p]; }
124  DCCLPublish& last_publish() { return publishes_.back(); }
125 
126  std::vector<boost::shared_ptr<DCCLMessageVar> >& layout() { return layout_; }
127  std::vector<boost::shared_ptr<DCCLMessageVar> >& header() { return header_; }
128 
129  const std::vector<boost::shared_ptr<DCCLMessageVar> >& layout_const() const { return layout_; }
130  const std::vector<boost::shared_ptr<DCCLMessageVar> >& header_const() const { return header_; }
131 
132  std::vector<DCCLPublish>& publishes() { return publishes_; }
133  const std::vector<DCCLPublish>& publishes_const() const { return publishes_; }
134 
135  boost::shared_ptr<DCCLMessageVar> name2message_var(const std::string& name) const;
136 
137  const google::protobuf::Descriptor* descriptor() { return descriptor_; }
138 
139  //other
140  /* std::string get_display() const; */
141  /* std::string get_short_display() const; */
142  std::map<std::string, std::string> message_var_names() const;
143  void preprocess();
144  void set_repeat_array_length();
145  unsigned calc_total_size();
146 
147  void set_head_defaults(std::map<std::string, std::vector<DCCLMessageVal> >& in,
148  unsigned modem_id);
149 
150  // increment, means increase trigger number
151  // prefix ++DCCLMessage
152  DCCLMessage& operator++()
153  {
154  ++trigger_number_;
155  return (*this);
156  }
157  // postfix DCCLMessage++
158  const DCCLMessage operator++(int)
159  {
160  DCCLMessage tmp(*this);
161  ++(*this);
162  return tmp;
163  }
164 
165  private:
166  private:
167  // total request size of message, e.g. 32 bytes
168  unsigned size_;
169 
170  unsigned trigger_number_;
171  // actual used bits in body part of message (not including header bits)
172  // unsigned body_bits_;
173 
174  unsigned id_;
175  unsigned modem_id_;
176  double trigger_time_;
177 
178  bool repeat_enabled_;
179  unsigned repeat_;
180 
181  std::string name_;
182  std::string trigger_type_;
183  std::string trigger_var_;
184  std::string trigger_mandatory_;
185  std::string in_var_;
186  std::string out_var_;
187 
188  std::vector<boost::shared_ptr<DCCLMessageVar> > layout_;
189  std::vector<boost::shared_ptr<DCCLMessageVar> > header_;
190 
191  std::vector<DCCLPublish> publishes_;
192 
193  const google::protobuf::Descriptor* descriptor_;
194 };
195 
196 /* inline void bitset2string(const boost::dynamic_bitset<unsigned char>& body_bits, */
197 /* std::string& body) */
198 /* { */
199 /* body.resize(body_bits.num_blocks()); // resize the string to fit the bitset */
200 /* to_block_range(body_bits, body.rbegin()); */
201 /* } */
202 
203 /* inline void string2bitset(boost::dynamic_bitset<unsigned char>& body_bits, */
204 /* const std::string& body) */
205 /* { */
206 /* from_block_range(body.rbegin(), body.rend(), body_bits); */
207 /* } */
208 
209 // std::ostream& operator<< (std::ostream& out, const DCCLMessage& message);
210 } // namespace transitional
211 } // namespace goby
212 #endif
The global namespace for the Goby project.