File: bluetooth_discovery_filter.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,510 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_DISCOVERY_FILTER_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_DISCOVERY_FILTER_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"

namespace device {

// *****************************************************************************
// BluetoothDiscoveryFilter is a class which stores information used to filter
// out Bluetooth devices at the operating system level while doing discovery.
// If you want to filter by RSSI or path loss set them directly in the class
// with the SetRSSI() and SetPathloss() functions.  However, if you are looking
// for a device with a particular name and/or set of services you must add a
// DeviceInfoFilter.
// Here is an example usage for DeviceInfoFilters:
//
// BluetoothDiscoveryFilter discovery_filter(BLUETOOTH_TRANSPORT_LE);
// BluetoothDiscoveryFilter::DeviceInfoFilter device_filter;
// device_filter.uuids.insert(BluetoothUUID("1019"));
// device_filter.uuids.insert(BluetoothUUID("1020"));
// discovery_filter.AddDeviceFilter(device_filter);
//
// BluetoothDiscoveryFilter::DeviceInfoFilter device_filter2;
// device_filter2.uuids.insert(BluetoothUUID("1021"));
// device_filter2.name = "this device";
// discovery_filter.AddDeviceFilter(device_filter2);
//
// When we add |device_filter| to |discovery_filter| our filter will only return
// devices that have both the uuid 1019 AND 1020.  When we add |device_filter2|
// we will then allow devices though that have either (uuid 1019 AND 1020) OR
// (uuid 1021 and a device name of "this device").
// *****************************************************************************

class DEVICE_BLUETOOTH_EXPORT BluetoothDiscoveryFilter {
 public:
  BluetoothDiscoveryFilter();
  BluetoothDiscoveryFilter(BluetoothTransport transport);

  BluetoothDiscoveryFilter(const BluetoothDiscoveryFilter&) = delete;
  BluetoothDiscoveryFilter& operator=(const BluetoothDiscoveryFilter&) = delete;

  ~BluetoothDiscoveryFilter();

  struct DEVICE_BLUETOOTH_EXPORT DeviceInfoFilter {
    DeviceInfoFilter();
    DeviceInfoFilter(const DeviceInfoFilter& other);
    ~DeviceInfoFilter();
    bool operator==(const DeviceInfoFilter& other) const;
    bool operator<(const DeviceInfoFilter& other) const;
    base::flat_set<device::BluetoothUUID> uuids;
    std::string name;
  };

  // These getters return true when given field is set in filter, and copy this
  // value to |out_*| parameter. If value is not set, returns false.
  // These setters assign given value to proper filter field.
  bool GetRSSI(int16_t* out_rssi) const;
  void SetRSSI(int16_t rssi);
  bool GetPathloss(uint16_t* out_pathloss) const;
  void SetPathloss(uint16_t pathloss);

  // Return and set transport field of this filter.
  BluetoothTransport GetTransport() const;
  void SetTransport(BluetoothTransport transport);

  // Make |out_uuids| represent all uuids in the |device_filters_| set.
  void GetUUIDs(std::set<device::BluetoothUUID>& out_uuids) const;

  // Add new DeviceInfoFilter to our array of DeviceInfoFilters,
  void AddDeviceFilter(const DeviceInfoFilter& device_filter);

  // Returns a const pointer of our list of DeviceInfoFilters, device_filters_.
  const base::flat_set<DeviceInfoFilter>* GetDeviceFilters() const;

  // Copy content of |filter| and assigns it to this filter.
  void CopyFrom(const BluetoothDiscoveryFilter& filter);

  // Check if two filters are equal.
  bool Equals(const BluetoothDiscoveryFilter& filter) const;

  // Returns true if all fields in filter are empty
  bool IsDefault() const;

  void ClearDeviceFilters();

  // Returns result of merging two filters together. If at least one of the
  // filters is NULL this will return an empty filter
  static std::unique_ptr<device::BluetoothDiscoveryFilter> Merge(
      const device::BluetoothDiscoveryFilter* filter_a,
      const device::BluetoothDiscoveryFilter* filter_b);

 private:
  std::optional<int16_t> rssi_;
  std::optional<uint16_t> pathloss_;
  BluetoothTransport transport_;
  base::flat_set<DeviceInfoFilter> device_filters_;
};

}  // namespace device

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_DISCOVERY_FILTER_H_