Goby v2
benthos_atm900_driver.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 BenthosATM900Driver20161221H
24 #define BenthosATM900Driver20161221H
25 
26 #include <dccl/codec.h>
27 #include <dccl/field_codec_fixed.h>
28 #include <dccl/field_codec_manager.h>
29 
30 #include "goby/common/time.h"
31 
32 #include "benthos_atm900_driver_fsm.h"
33 #include "driver_base.h"
34 #include "goby/acomms/acomms_helpers.h"
35 #include "goby/acomms/protobuf/benthos_atm900.pb.h"
36 #include "rudics_packet.h"
37 
38 namespace goby
39 {
40 namespace acomms
41 {
43 {
44  public:
46  void startup(const protobuf::DriverConfig& cfg);
47  void shutdown();
48  void do_work();
50 
51  private:
52  void receive(const protobuf::ModemTransmission& msg);
53  void send(const protobuf::ModemTransmission& msg);
54  void try_serial_tx();
55 
56  private:
57  enum
58  {
59  DEFAULT_BAUD = 9600
60  };
61  static const std::string SERIAL_DELIMITER;
62 
64  protobuf::DriverConfig driver_cfg_; // configuration given to you at launch
65  goby::uint32 next_frame_;
66 };
67 
68 // placeholder id codec that uses no bits, since we're always sending just this message on the wire
69 class NoOpIdentifierCodec : public dccl::TypedFixedFieldCodec<dccl::uint32>
70 {
71  dccl::Bitset encode() { return dccl::Bitset(); }
72  dccl::Bitset encode(const uint32& wire_value) { return dccl::Bitset(); }
73  dccl::uint32 decode(dccl::Bitset* bits) { return 0; }
74  virtual unsigned size() { return 0; }
75 };
76 
77 extern boost::shared_ptr<dccl::Codec> benthos_header_dccl_;
78 
79 inline void init_benthos_dccl()
80 {
81  dccl::FieldCodecManager::add<NoOpIdentifierCodec>("benthos_header_id");
82  benthos_header_dccl_.reset(new dccl::Codec("benthos_header_id"));
83  benthos_header_dccl_->load<benthos::protobuf::BenthosHeader>();
84 }
85 
86 inline void serialize_benthos_modem_message(std::string* out,
88 {
90  header.set_type(in.type());
91  if (in.has_ack_requested())
92  header.set_ack_requested(in.ack_requested());
93 
94  for (int i = 0, n = in.acked_frame_size(); i < n; ++i)
95  header.add_acked_frame(in.acked_frame(i));
96 
97  benthos_header_dccl_->encode(out, header);
98 
99  // frame message
100  for (int i = 0, n = in.frame_size(); i < n; ++i)
101  {
102  if (in.frame(i).empty())
103  break;
104 
105  std::string rudics_packet;
106  serialize_rudics_packet(in.frame(i), &rudics_packet, "\r", false);
107  *out += rudics_packet;
108  }
109 }
110 
111 inline void parse_benthos_modem_message(std::string in,
113 {
115  benthos_header_dccl_->decode(&in, &header);
116 
117  out->set_type(header.type());
118  if (header.has_ack_requested())
119  out->set_ack_requested(header.ack_requested());
120 
121  for (int i = 0, n = header.acked_frame_size(); i < n; ++i)
122  out->add_acked_frame(header.acked_frame(i));
123 
124  std::vector<std::string> encoded_frames;
125  boost::split(encoded_frames, in, boost::is_any_of("\r"), boost::token_compress_on);
126 
127  for (int i = 0, n = encoded_frames.size(); i < n; ++i)
128  {
129  if (!encoded_frames[i].empty())
130  parse_rudics_packet(out->add_frame(), encoded_frames[i] + "\r", "\r", false);
131  }
132 }
133 
134 } // namespace acomms
135 } // namespace goby
136 #endif
void shutdown()
Shuts down the modem driver.
void handle_initiate_transmission(const protobuf::ModemTransmission &m)
Virtual initiate_transmission method. Typically connected to MACManager::signal_initiate_transmission...
google::protobuf::uint32 uint32
an unsigned 32 bit integer
void do_work()
Allows the modem driver to do its work.
The global namespace for the Goby project.
void startup(const protobuf::DriverConfig &cfg)
Starts the modem driver. Must be called before poll().
provides an abstract base class for acoustic modem drivers. This is subclassed by the various drivers...
Definition: driver_base.h:47