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