mavtables  0.2.1
MAVLink router and firewall.
mavlink.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <cstdint>
19 #include <stdexcept>
20 #include <string>
21 
22 #include "mavlink.hpp"
23 
24 
25 namespace mavlink
26 {
27 
28  /** Get message name from numeric ID.
29  *
30  * \ingroup mavlink
31  * \param id The ID of the MAVLink message to get the name of.
32  * \returns The name of the message.
33  * \throws std::invalid_argument if the given \p id is not valid.
34  */
35  std::string name(unsigned long id)
36  {
37  const mavlink_message_info_t *info =
38  mavlink_get_message_info_by_id(static_cast<uint32_t>(id));
39 
40  if (info)
41  {
42  return std::string(info->name);
43  }
44 
45  throw std::invalid_argument(
46  "Invalid packet ID (#" + std::to_string(id) + ").");
47  }
48 
49 
50  /** Get message ID from message name.
51  *
52  * \ingroup mavlink
53  * \param name The name of the MAVLink message to get the numeric ID of.
54  * \returns The numeric ID of the message.
55  * \throws std::invalid_argument if the given message \p name is not valid.
56  */
57  unsigned long id(std::string name)
58  {
59  const mavlink_message_info_t *info =
60  mavlink_get_message_info_by_name(name.c_str());
61 
62  if (info)
63  {
64  return info->msgid;
65  }
66 
67  throw std::invalid_argument("Invalid packet name (\"" + name + "\").");
68  }
69 
70 }