PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
Loading...
Searching...
No Matches
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
10namespace px4_ros2::events {
11
12using EventType = px4_msgs::msg::Event;
13
14enum 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
29enum 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
44using Log = LogLevel;
45using LogInternal = LogLevelInternal;
46
47struct 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
62namespace util {
63
64// source: https://gist.github.com/ruby0x1/81308642d0325fd386237cfa3b44785c
65constexpr uint32_t kVal32Const = 0x811c9dc5;
66constexpr uint32_t kPrime32Const = 0x1000193;
67inline 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
75template <typename T>
76inline 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
82template <typename T, typename... Args>
83inline constexpr void fillEventArguments(uint8_t* buf, T arg, Args... args)
84{
85 fillEventArguments(buf, arg);
86 fillEventArguments(buf + sizeof(T), args...);
87}
88
89constexpr unsigned sizeofArguments()
90{
91 return 0;
92}
93
94template <typename T, typename... Args>
95constexpr unsigned sizeofArguments(const T& t, const Args&... args)
96{
97 return sizeof(T) + sizeofArguments(args...);
98}
99
100} // namespace util
101
105template <size_t N>
106constexpr 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