AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
BlueZDeviceManager.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_BLUEZDEVICEMANAGER_H_
17 #define ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_BLUEZDEVICEMANAGER_H_
18 
19 #include <list>
20 #include <map>
21 #include <mutex>
22 
30 
32 #include "BlueZ/BlueZUtils.h"
33 #include "BlueZ/MPRISPlayer.h"
34 #include "BlueZ/PairingAgent.h"
35 
36 #include <gio/gio.h>
37 
38 namespace alexaClientSDK {
39 namespace bluetoothImplementations {
40 namespace blueZ {
41 
42 class MediaEndpoint;
43 class BlueZBluetoothDevice;
44 
51  , public std::enable_shared_from_this<BlueZDeviceManager> {
52 public:
59  static std::shared_ptr<BlueZDeviceManager> create(
60  std::shared_ptr<avsCommon::utils::bluetooth::BluetoothEventBus> eventBus);
61 
65  virtual ~BlueZDeviceManager() override;
66 
69 
70  std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::BluetoothHostControllerInterface> getHostController() override;
71  std::list<std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::BluetoothDeviceInterface>> getDiscoveredDevices()
72  override;
73  std::shared_ptr<avsCommon::utils::bluetooth::BluetoothEventBus> getEventBus() override;
75 
76  // @name RequiresShutdown Functions
78  void doShutdown() override;
80 
86  std::shared_ptr<MediaEndpoint> getMediaEndpoint();
87 
94 
100 
101 private:
107  explicit BlueZDeviceManager(const std::shared_ptr<avsCommon::utils::bluetooth::BluetoothEventBus>& eventBus);
108 
114  bool init();
115 
121  bool initializeMedia();
122 
128  bool finalizeMedia();
129 
137  std::shared_ptr<BlueZBluetoothDevice> addDeviceFromDBusObject(const char* objectPath, GVariant* dbusObject);
138 
150  static void interfacesAddedCallback(
151  GDBusConnection* conn,
152  const gchar* sender_name,
153  const gchar* object_path,
154  const gchar* interface_name,
155  const gchar* signal_name,
156  GVariant* parameters,
157  gpointer data);
158 
170  static void interfacesRemovedCallback(
171  GDBusConnection* conn,
172  const gchar* sender_name,
173  const gchar* object_path,
174  const gchar* interface_name,
175  const gchar* signal_name,
176  GVariant* parameters,
177  gpointer data);
178 
190  static void propertiesChangedCallback(
191  GDBusConnection* conn,
192  const gchar* sender_name,
193  const gchar* object_path,
194  const gchar* interface_name,
195  const gchar* signal_name,
196  GVariant* parameters,
197  gpointer data);
198 
205  void addDevice(const char* devicePath, std::shared_ptr<BlueZBluetoothDevice> device);
206 
212  void removeDevice(const char* devicePath);
213 
219  void notifyDeviceAdded(std::shared_ptr<BlueZBluetoothDevice> device);
220 
227  void onInterfaceAdded(const char* objectPath, ManagedGVariant& interfacesChangedMap);
228 
234  void onInterfaceRemoved(const char* objectPath);
235 
242  void onAdapterPropertyChanged(const std::string& path, const GVariantMapReader& changesMap);
243 
250  void onDevicePropertyChanged(const std::string& path, const GVariantMapReader& changesMap);
251 
258  void onMediaStreamPropertyChanged(const std::string& path, const GVariantMapReader& changesMap);
259 
265  std::shared_ptr<BlueZHostController> initializeHostController();
266 
274  void onPropertiesChanged(
275  const std::string& propertyOwner,
276  const std::string& objectPath,
277  const GVariantMapReader& changesMap);
278 
282  bool getStateFromBlueZ();
283 
290  std::shared_ptr<BlueZBluetoothDevice> getDeviceByPath(const std::string& path) const;
291 
293  void mainLoopThread();
294 
296  std::string m_adapterPath;
297 
299  std::shared_ptr<DBusProxy> m_objectManagerProxy;
300 
302  std::shared_ptr<DBusProxy> m_mediaProxy;
303 
305  std::map<std::string, std::shared_ptr<BlueZBluetoothDevice>> m_devices;
306 
308  std::shared_ptr<MediaEndpoint> m_mediaEndpoint;
309 
311  std::shared_ptr<PairingAgent> m_pairingAgent;
312 
314  std::shared_ptr<MPRISPlayer> m_mediaPlayer;
315 
317  std::shared_ptr<avsCommon::utils::bluetooth::BluetoothEventBus> m_eventBus;
318 
319  // TODO Having a separate thread for event listening is wasteful. Can optimize with a BlueZ bus listener.
321  GMainLoop* m_eventLoop;
322 
324  GMainContext* m_workerContext;
325 
327  std::shared_ptr<DBusConnection> m_connection;
328 
331 
333  mutable std::mutex m_devicesMutex;
334 
336  std::shared_ptr<BlueZHostController> m_hostController;
337 
339  std::promise<bool> m_mainLoopInitPromise;
340 
342  std::thread m_eventThread;
343 };
344 
345 } // namespace blueZ
346 } // namespace bluetoothImplementations
347 } // namespace alexaClientSDK
348 
349 #endif // ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_BLUEZDEVICEMANAGER_H_
std::list< std::shared_ptr< avsCommon::sdkInterfaces::bluetooth::BluetoothDeviceInterface > > getDiscoveredDevices() override
std::shared_ptr< avsCommon::sdkInterfaces::bluetooth::BluetoothHostControllerInterface > getHostController() override
::std::string string
Definition: gtest-port.h:1097
MediaStreamingState
An Enum representing the current state of the stream.
Definition: MediaStreamingState.h:25
static std::shared_ptr< BlueZDeviceManager > create(std::shared_ptr< avsCommon::utils::bluetooth::BluetoothEventBus > eventBus)
std::shared_ptr< avsCommon::utils::bluetooth::BluetoothEventBus > getEventBus() override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
avsCommon::utils::bluetooth::MediaStreamingState getMediaStreamingState()

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