AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
DBusObject.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_DBUSOBJECT_H_
17 #define ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_DBUSOBJECT_H_
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "BlueZ/DBusObjectBase.h"
24 
25 namespace alexaClientSDK {
26 namespace bluetoothImplementations {
27 namespace blueZ {
28 
40 template <class T>
41 class DBusObject : public DBusObjectBase {
42 public:
43  virtual ~DBusObject() = default;
50  using commandHandler_t = void (T::*)(GVariant* parameters, GDBusMethodInvocation* invocation);
51 
52 protected:
63  std::shared_ptr<DBusConnection> connection,
64  std::string xmlInterfaceIntrospection,
65  std::string objectPath,
66  std::unordered_map<std::string, commandHandler_t> methodMap) :
67  DBusObjectBase(connection, xmlInterfaceIntrospection, objectPath, &onMethodCallStatic),
68  m_commands{methodMap} {
69  }
70 
71 private:
86  static void onMethodCallStatic(
87  GDBusConnection* conn,
88  const gchar* sender_name,
89  const gchar* object_path,
90  const gchar* interface_name,
91  const gchar* method_name,
92  GVariant* parameters,
93  GDBusMethodInvocation* invocation,
94  gpointer data) {
95  auto thisPtr = static_cast<T*>(data);
96  thisPtr->onMethodCall(method_name, parameters, invocation);
97  }
98 
108  void onMethodCall(const gchar* method_name, GVariant* parameters, GDBusMethodInvocation* invocation);
109 
110  // Map between method names and member functions implementing them
111  std::unordered_map<std::string, commandHandler_t> m_commands;
112 };
113 
114 template <class T>
115 void DBusObject<T>::onMethodCall(const gchar* method_name, GVariant* parameters, GDBusMethodInvocation* invocation) {
116  onMethodCalledInternal(method_name);
117 
118  auto iter = m_commands.find(method_name);
119  if (iter != m_commands.end()) {
120  commandHandler_t handler = iter->second;
121  (static_cast<T*>(this)->*handler)(parameters, invocation);
122  } else {
123  g_dbus_method_invocation_return_error(
124  invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method: ");
125  }
126 }
127 
128 } // namespace blueZ
129 } // namespace bluetoothImplementations
130 } // namespace alexaClientSDK
131 
132 #endif // ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_DBUSOBJECT_H_
DBusObject(std::shared_ptr< DBusConnection > connection, std::string xmlInterfaceIntrospection, std::string objectPath, std::unordered_map< std::string, commandHandler_t > methodMap)
Definition: DBusObject.h:62
::std::string string
Definition: gtest-port.h:1097
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void(MediaEndpoint ::*)(GVariant *parameters, GDBusMethodInvocation *invocation) commandHandler_t
Definition: DBusObject.h:50

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0