AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
BlueZBluetoothDevice.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_BLUEZBLUETOOTHDEVICE_H_
17 #define ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_BLUEZBLUETOOTHDEVICE_H_
18 
19 #include <future>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 #include <unordered_map>
25 #include <unordered_set>
26 
27 #include <gio/gio.h>
28 
33 
34 namespace alexaClientSDK {
35 namespace bluetoothImplementations {
36 namespace blueZ {
37 
38 class BlueZDeviceManager;
39 
43  , public std::enable_shared_from_this<BlueZBluetoothDevice> {
44 public:
45  enum class BlueZDeviceState {
47  FOUND,
49  UNPAIRED,
51  PAIRED,
53  IDLE,
57  CONNECTED,
64  };
65 
68  ~BlueZBluetoothDevice() override;
69 
70  std::string getMac() const override;
71  std::string getFriendlyName() const override;
73  MetaData getDeviceMetaData() override;
74 
75  bool isPaired() override;
76  std::future<bool> pair() override;
77  std::future<bool> unpair() override;
78 
79  bool isConnected() override;
80  std::future<bool> connect() override;
81  std::future<bool> disconnect() override;
82 
83  bool setPairingPin(const std::string& pin) override;
84 
85  std::vector<std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::services::SDPRecordInterface>>
86  getSupportedServices() override;
87  std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::services::BluetoothServiceInterface> getService(
88  std::string uuid) override;
91  bool enabled,
92  std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::services::BluetoothServiceInterface> service) override;
94 
103  static std::shared_ptr<BlueZBluetoothDevice> create(
104  const std::string& mac,
105  const std::string& objectPath,
106  std::shared_ptr<BlueZDeviceManager> deviceManager);
107 
114  std::string getObjectPath() const;
115 
122  void onPropertyChanged(const GVariantMapReader& changesMap);
123 
124 private:
127  const std::string& mac,
128  const std::string& objectPath,
129  std::shared_ptr<BlueZDeviceManager> deviceManager);
130 
136  bool init();
137 
143  bool updateFriendlyName();
144 
150  std::unordered_set<std::string> getServiceUuids();
151 
159  std::unordered_set<std::string> getServiceUuids(GVariant* array);
160 
166  bool initializeServices(const std::unordered_set<std::string>& uuids);
167 
173  bool executePair();
174 
181  bool executeUnpair();
182 
188  bool executeConnect();
189 
195  bool executeDisconnect();
196 
203  bool executeIsPaired();
204 
211  bool executeIsConnected();
212 
219  bool executeToggleServiceConnection(
220  bool enabled,
221  std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::services::BluetoothServiceInterface> service);
222 
230  bool queryDeviceProperty(const std::string& name, bool* value);
231 
235  avsCommon::sdkInterfaces::bluetooth::DeviceState convertToDeviceState(BlueZDeviceState bluezDeviceState);
236 
243  void transitionToState(BlueZDeviceState newState, bool sendEvent);
244 
245  /*
246  * Querying BlueZ for whether the Connect property is set to true does not guarantee
247  * that a device has established a connection with its services.
248  * The Connect property can be set to true when pairing:
249  *
250  * 1) Pairing (BlueZ sends Connect = true).
251  * 2) Pair Successful.
252  * 3) Connect multimedia services.
253  * 4) Connect multimedia services successful (BlueZ sends Paired = true, UUIDs = [array of
254  * uuids]).
255  *
256  * Use a combination of Connect, Paired, and the availability of certain UUIDs to
257  * determine if a service has been connected to. A relevant service currently encompasses the set of
258  * A2DP services that AVS uses to understand connectedness. This should be done in the executor.
259  *
260  * @return Whether the device has established a connection with at least one service of interest.
261  */
262  bool executeIsConnectedToRelevantServices();
263 
270  bool serviceExists(const std::string& uuid);
271 
278  bool insertService(
279  std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::services::BluetoothServiceInterface> service);
280 
287  template <typename ServiceType>
288  std::shared_ptr<ServiceType> getService();
289 
296  template <typename BlueZServiceType>
297  bool initializeService();
298 
300  std::shared_ptr<DBusProxy> m_deviceProxy;
301 
303  std::shared_ptr<DBusPropertiesProxy> m_propertiesProxy;
304 
306  const std::string m_mac;
307 
309  const std::string m_objectPath;
310 
312  std::string m_friendlyName;
313 
315  std::mutex m_servicesMapMutex;
316 
318  std::unordered_map<
319  std::string,
320  std::shared_ptr<avsCommon::sdkInterfaces::bluetooth::services::BluetoothServiceInterface>>
321  m_servicesMap;
322 
324  BlueZDeviceState m_deviceState;
325 
327  std::unique_ptr<avsCommon::sdkInterfaces::bluetooth::BluetoothDeviceInterface::MetaData> m_metaData;
328 
330  std::shared_ptr<BlueZDeviceManager> m_deviceManager;
331 
334 };
335 
343  switch (state) {
345  return "FOUND";
347  return "UNPAIRED";
349  return "PAIRED";
351  return "IDLE";
353  return "DISCONNECTED";
355  return "CONNECTED";
357  return "CONNECTION_FAILED";
358  }
359 
360  return "UNKNOWN";
361 }
362 
371 inline std::ostream& operator<<(std::ostream& stream, const BlueZBluetoothDevice::BlueZDeviceState& state) {
372  return stream << deviceStateToString(state);
373 }
374 
375 } // namespace blueZ
376 } // namespace bluetoothImplementations
377 } // namespace alexaClientSDK
378 
379 #endif // ALEXA_CLIENT_SDK_BLUETOOTHIMPLEMENTATIONS_BLUEZ_INCLUDE_BLUEZ_BLUEZBLUETOOTHDEVICE_H_
std::vector< std::shared_ptr< avsCommon::sdkInterfaces::bluetooth::services::SDPRecordInterface > > getSupportedServices() override
std::ostream & operator<<(std::ostream &stream, const BlueZBluetoothDevice::BlueZDeviceState &state)
Definition: BlueZBluetoothDevice.h:371
::std::string string
Definition: gtest-port.h:1097
MediaStreamingState
An Enum representing the current state of the stream.
Definition: MediaStreamingState.h:25
Single-thread executor implementation.
Definition: Executor.h:45
A BlueZ implementation of the BluetoothDeviceInterface.
Definition: BlueZBluetoothDevice.h:41
bool toggleServiceConnection(bool enabled, std::shared_ptr< avsCommon::sdkInterfaces::bluetooth::services::BluetoothServiceInterface > service) override
Represents a Bluetooth Device.
Definition: BluetoothDeviceInterface.h:100
void onPropertyChanged(const GVariantMapReader &changesMap)
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
DeviceState
Definition: BluetoothDeviceInterface.h:48
avsCommon::sdkInterfaces::bluetooth::DeviceState getDeviceState() override
static std::shared_ptr< BlueZBluetoothDevice > create(const std::string &mac, const std::string &objectPath, std::shared_ptr< BlueZDeviceManager > deviceManager)
std::string deviceStateToString(BlueZBluetoothDevice::BlueZDeviceState state)
Definition: BlueZBluetoothDevice.h:342
avsCommon::utils::bluetooth::MediaStreamingState getStreamingState() override

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