Goby v2
iridium_driver_common.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 IridiumDriverCommon20150508H
24 #define IridiumDriverCommon20150508H
25 
26 #include <dccl/codec.h>
27 #include <dccl/field_codec_fixed.h>
28 #include <dccl/field_codec_manager.h>
29 
30 #include "goby/acomms/protobuf/iridium_driver.pb.h"
31 
32 namespace goby
33 {
34 namespace acomms
35 {
36 enum
37 {
38  RATE_RUDICS = 1,
39  RATE_SBD = 0
40 };
41 
43 {
44  public:
45  OnCallBase()
46  : last_tx_time_(goby::common::goby_time<double>()), last_rx_time_(0), bye_received_(false),
47  bye_sent_(false), total_bytes_sent_(0), last_bytes_sent_(0)
48  {
49  }
50  double last_rx_tx_time() const { return std::max(last_tx_time_, last_rx_time_); }
51  double last_rx_time() const { return last_rx_time_; }
52  double last_tx_time() const { return last_tx_time_; }
53 
54  int last_bytes_sent() const { return last_bytes_sent_; }
55  int total_bytes_sent() const { return total_bytes_sent_; }
56 
57  void set_bye_received(bool b) { bye_received_ = b; }
58  void set_bye_sent(bool b) { bye_sent_ = b; }
59 
60  bool bye_received() const { return bye_received_; }
61  bool bye_sent() const { return bye_sent_; }
62 
63  void set_last_tx_time(double d) { last_tx_time_ = d; }
64  void set_last_rx_time(double d) { last_rx_time_ = d; }
65 
66  void set_last_bytes_sent(int i)
67  {
68  last_bytes_sent_ = i;
69  total_bytes_sent_ += i;
70  }
71 
72  private:
73  double last_tx_time_;
74  double last_rx_time_;
75  bool bye_received_;
76  bool bye_sent_;
77  int total_bytes_sent_;
78  int last_bytes_sent_;
79 };
80 
81 // placeholder id codec that uses no bits, since we're always sending just this message on the wire
82 class IridiumHeaderIdentifierCodec : public dccl::TypedFixedFieldCodec<dccl::uint32>
83 {
84  dccl::Bitset encode() { return dccl::Bitset(); }
85  dccl::Bitset encode(const uint32& wire_value) { return dccl::Bitset(); }
86  dccl::uint32 decode(dccl::Bitset* bits) { return 0; }
87  virtual unsigned size() { return 0; }
88 };
89 
90 extern boost::shared_ptr<dccl::Codec> iridium_header_dccl_;
91 
92 inline void init_iridium_dccl()
93 {
94  dccl::FieldCodecManager::add<IridiumHeaderIdentifierCodec>("iridium_header_id");
95  iridium_header_dccl_.reset(new dccl::Codec("iridium_header_id"));
96  iridium_header_dccl_->load<IridiumHeader>();
97 }
98 
99 inline void serialize_iridium_modem_message(std::string* out,
101 {
102  IridiumHeader header;
103  header.set_src(in.src());
104  header.set_dest(in.dest());
105  if (in.has_rate())
106  header.set_rate(in.rate());
107  header.set_type(in.type());
108  if (in.has_ack_requested())
109  header.set_ack_requested(in.ack_requested());
110  if (in.has_frame_start())
111  header.set_frame_start(in.frame_start());
112  if (in.acked_frame_size())
113  header.set_acked_frame(in.acked_frame(0));
114 
115  iridium_header_dccl_->encode(out, header);
116  if (in.frame_size())
117  *out += in.frame(0);
118 }
119 
120 inline void parse_iridium_modem_message(std::string in,
122 {
123  IridiumHeader header;
124  iridium_header_dccl_->decode(&in, &header);
125 
126  out->set_src(header.src());
127  out->set_dest(header.dest());
128  if (header.has_rate())
129  out->set_rate(header.rate());
130  out->set_type(header.type());
131  if (header.has_ack_requested())
132  out->set_ack_requested(header.ack_requested());
133  if (header.has_frame_start())
134  out->set_frame_start(header.frame_start());
135  if (header.has_acked_frame())
136  out->add_acked_frame(header.acked_frame());
137 
138  if (in.size())
139  out->add_frame(in);
140 }
141 
142 } // namespace acomms
143 } // namespace goby
144 
145 #endif
google::protobuf::uint32 uint32
an unsigned 32 bit integer
double goby_time< double >()
Returns current UTC time as seconds and fractional seconds since 1970-01-01 00:00:00.
Definition: time.h:130
The global namespace for the Goby project.