PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
events.hpp
1 /****************************************************************************
2  * Copyright (c) 2023 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <px4_msgs/msg/arming_check_reply.hpp>
9 
10 namespace px4_ros2::events {
11 
12 using EventType = px4_msgs::msg::Event;
13 
14 enum class LogLevel : uint8_t {
15  Emergency = 0,
16  Alert = 1,
17  Critical = 2,
18  Error = 3,
19  Warning = 4,
20  Notice = 5,
21  Info = 6,
22  Debug = 7,
23  Protocol = 8,
24  Disabled = 9,
25 
26  Count
27 };
28 
29 enum class LogLevelInternal : uint8_t {
30  Emergency = 0,
31  Alert = 1,
32  Critical = 2,
33  Error = 3,
34  Warning = 4,
35  Notice = 5,
36  Info = 6,
37  Debug = 7,
38  Protocol = 8,
39  Disabled = 9,
40 
41  Count
42 };
43 
44 using Log = LogLevel;
45 using LogInternal = LogLevelInternal;
46 
47 struct LogLevels {
48  LogLevels() = default;
49  LogLevels(Log external_level) // NOLINT
50  : external(external_level), internal(static_cast<LogInternal>(external_level))
51  {
52  }
53  LogLevels(Log external_level, LogInternal internal_level)
54  : external(external_level), internal(internal_level)
55  {
56  }
57 
58  Log external{Log::Info};
59  LogInternal internal{LogInternal::Info};
60 };
61 
62 namespace util {
63 
64 // source: https://gist.github.com/ruby0x1/81308642d0325fd386237cfa3b44785c
65 constexpr uint32_t kVal32Const = 0x811c9dc5;
66 constexpr uint32_t kPrime32Const = 0x1000193;
67 inline constexpr uint32_t hash32Fnv1aConst(const char* const str,
68  const uint32_t value = kVal32Const) noexcept
69 {
70  return (str[0] == '\0')
71  ? value
72  : hash32Fnv1aConst(&str[1], (value ^ static_cast<uint32_t>(str[0])) * kPrime32Const);
73 }
74 
75 template <typename T>
76 inline constexpr void fillEventArguments(uint8_t* buf, T arg)
77 {
78  // This assumes we're on little-endian
79  memcpy(buf, &arg, sizeof(T));
80 }
81 
82 template <typename T, typename... Args>
83 inline constexpr void fillEventArguments(uint8_t* buf, T arg, Args... args)
84 {
85  fillEventArguments(buf, arg);
86  fillEventArguments(buf + sizeof(T), args...);
87 }
88 
89 constexpr unsigned sizeofArguments()
90 {
91  return 0;
92 }
93 
94 template <typename T, typename... Args>
95 constexpr unsigned sizeofArguments(const T& t, const Args&... args)
96 {
97  return sizeof(T) + sizeofArguments(args...);
98 }
99 
100 } // namespace util
101 
105 template <size_t N>
106 constexpr uint32_t ID(const char (&name)[N]) // NOLINT(readability-identifier-naming)
107 {
108  // Note: the generated ID must match with the python generator under Tools/px4events
109  const uint32_t component_id = 1U << 24; // autopilot component
110  return (0xffffff & util::hash32Fnv1aConst(name)) | component_id;
111 }
112 
113 } // namespace px4_ros2::events
Definition: events.hpp:47