expose DBus interface method names as symbols
Enable a way to access the method names of a given DBus interface as a
constexpr symbol via the header.
4 use-cases:
- printing error / debug logs with the method name.
e.g. 'error calling ${METHOD_NAME}'
- accessing DBus methods in applications which do not yet use the PDI
generated bindings.
- preventing typos in DBus method names
- estimating the impact of removing a given DBus method from an
interface. When using these symbols, it would cause a build failure in
applications relying on the existence of that method.
This change is similar to [1] and goes into the same direction.
Tested: Newly written unit test passes.
References:
[1] d2571922bfdb4f6b41ba4fbc45b8a4272793fd40
Change-Id: Id423c3a668dd1a8346040f4380476d5e02468ddb
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/test/gen/server/TestWithMethod/meson.build b/test/gen/server/TestWithMethod/meson.build
new file mode 100644
index 0000000..43b045c
--- /dev/null
+++ b/test/gen/server/TestWithMethod/meson.build
@@ -0,0 +1,38 @@
+# Generated file; do not modify.
+
+sdbusplus_current_path = 'server/TestWithMethod'
+
+generated_sources += custom_target(
+ 'server/TestWithMethod__cpp'.underscorify(),
+ input: ['../../../yaml/server/TestWithMethod.interface.yaml'],
+ output: [
+ 'common.hpp',
+ 'server.hpp',
+ 'server.cpp',
+ 'aserver.hpp',
+ 'client.hpp',
+ ],
+ depend_files: sdbusplusplus_depfiles,
+ command: [
+ sdbuspp_gen_meson_prog,
+ '--command',
+ 'cpp',
+ '--output',
+ meson.current_build_dir(),
+ '--tool',
+ sdbusplusplus_prog,
+ '--directory',
+ meson.current_source_dir() / '../../../yaml',
+ 'server/TestWithMethod',
+ ],
+ install: should_generate_cpp,
+ install_dir: [
+ get_option('includedir') / sdbusplus_current_path,
+ get_option('includedir') / sdbusplus_current_path,
+ false,
+ get_option('includedir') / sdbusplus_current_path,
+ get_option('includedir') / sdbusplus_current_path,
+ ],
+ build_by_default: should_generate_cpp,
+)
+
diff --git a/test/gen/server/meson.build b/test/gen/server/meson.build
index 142903d..3b12a98 100644
--- a/test/gen/server/meson.build
+++ b/test/gen/server/meson.build
@@ -2,6 +2,7 @@
subdir('Test')
subdir('Test2')
subdir('Test3')
+subdir('TestWithMethod')
sdbusplus_current_path = 'server'
@@ -71,3 +72,25 @@
build_by_default: should_generate_markdown,
)
+generated_markdown += custom_target(
+ 'server/TestWithMethod__markdown'.underscorify(),
+ input: ['../../yaml/server/TestWithMethod.interface.yaml'],
+ output: ['TestWithMethod.md'],
+ depend_files: sdbusplusplus_depfiles,
+ command: [
+ sdbuspp_gen_meson_prog,
+ '--command',
+ 'markdown',
+ '--output',
+ meson.current_build_dir(),
+ '--tool',
+ sdbusplusplus_prog,
+ '--directory',
+ meson.current_source_dir() / '../../yaml',
+ 'server/TestWithMethod',
+ ],
+ install: should_generate_markdown,
+ install_dir: [inst_markdown_dir / sdbusplus_current_path],
+ build_by_default: should_generate_markdown,
+)
+
diff --git a/test/gen/test_method_names.cpp b/test/gen/test_method_names.cpp
new file mode 100644
index 0000000..f9145a1
--- /dev/null
+++ b/test/gen/test_method_names.cpp
@@ -0,0 +1,31 @@
+#include "server/TestWithMethod/common.hpp"
+
+#include <print>
+
+#include <gtest/gtest.h>
+
+TEST(MethodNames, TestMethodNames)
+{
+ // We can access the method name as a symbol.
+ // The property name is constexpr.
+
+ constexpr auto methodName =
+ sdbusplus::common::server::TestWithMethod::method_names::update_value;
+
+ // The method name can be used as part of error logs.
+
+ std::println("error calling method {}\n", methodName);
+
+ // If the method is removed from the interface definition, it will cause a
+ // build failure in applications still using that method. That can work
+ // even if the application is not (yet) using PDI-generated bindings for
+ // it's DBus interactions.
+
+ std::println(
+ "using method {} \n",
+ sdbusplus::common::server::TestWithMethod::method_names::update_value);
+
+ EXPECT_EQ(
+ sdbusplus::common::server::TestWithMethod::method_names::update_value,
+ "UpdateValue");
+}
diff --git a/test/meson.build b/test/meson.build
index de7c0e4..ead3508 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -124,6 +124,7 @@
'test_aserver_emit_interfaces_added_signal',
'test_aserver_multiple_interfaces',
'test_property_names',
+ 'test_method_names',
]
foreach t : uninit_tests
diff --git a/test/yaml/server/TestWithMethod.interface.yaml b/test/yaml/server/TestWithMethod.interface.yaml
new file mode 100644
index 0000000..d44a71d
--- /dev/null
+++ b/test/yaml/server/TestWithMethod.interface.yaml
@@ -0,0 +1,10 @@
+description: >
+ A test interface with a method
+methods:
+ - name: UpdateValue
+ parameters:
+ - name: updatedValue
+ type: int64
+ returns:
+ - name: status
+ type: boolean
diff --git a/tools/sdbusplus/templates/interface.common.hpp.mako b/tools/sdbusplus/templates/interface.common.hpp.mako
index 9d2a3b5..da35212 100644
--- a/tools/sdbusplus/templates/interface.common.hpp.mako
+++ b/tools/sdbusplus/templates/interface.common.hpp.mako
@@ -52,6 +52,15 @@
using properties_t = std::nullopt_t;
% endif
+ % if interface.methods:
+ struct method_names
+ {
+ % for method in interface.methods:
+ static constexpr auto ${method.snake_case} = "${method.name}";
+ % endfor
+ };
+ % endif
+
% for p in interface.paths:
% if p.description:
/** ${p.description.strip()} */