Home | History | Annotate | Download | only in bluetooth
      1 //
      2 //  Copyright (C) 2015 Google, Inc.
      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 //  You may obtain a copy of the License at:
      7 //
      8 //  http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 //  Unless required by applicable law or agreed to in writing, software
     11 //  distributed under the License is distributed on an "AS IS" BASIS,
     12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 //  See the License for the specific language governing permissions and
     14 //  limitations under the License.
     15 //
     16 
     17 #pragma once
     18 
     19 #include <memory>
     20 
     21 #include <bluetooth/uuid.h>
     22 
     23 namespace bluetooth {
     24 
     25 // Used for filtering scan results by allowing clients to restrict scan results
     26 // to only those that are of interest to them.
     27 class ScanFilter {
     28  public:
     29   ScanFilter() = default;
     30   ~ScanFilter() = default;
     31 
     32   // Copy constructor and assignment operator.
     33   ScanFilter(const ScanFilter& other);
     34   ScanFilter& operator=(const ScanFilter& other);
     35 
     36   // The device name used while filtering scan results.
     37   const std::string& device_name() const { return device_name_; }
     38   void set_device_name(const std::string& name) { device_name_ = name; }
     39 
     40   // The device address used while filtering scan results. Address should be in
     41   // the XX:XX:XX:XX:XX:XX where X is a hexadecimal digit.
     42   const std::string& device_address() const { return device_address_; }
     43 
     44   // Sets the device address used for filtering. Returns false if
     45   // |device_address| is in an illegal format.
     46   bool SetDeviceAddress(const std::string& device_address);
     47 
     48   // The service UUID and its mask used while filtering scan results. See
     49   // SetServiceUuidWithMask for what this mask does. The raw pointer returned
     50   // from these getters belongs to the ScanFilter object. nullptr will be
     51   // returned if these fields have not been set on this filter.
     52   UUID* service_uuid() const { return service_uuid_.get(); }
     53   UUID* service_uuid_mask() const { return service_uuid_mask_.get(); }
     54 
     55   // Sets the service UUID for this filter.
     56   void SetServiceUuid(const UUID& service_uuid);
     57 
     58   // Sets the service UUID for this filter with a 128-bit mask. The mask allows
     59   // the caller to partially filter scanned service UUIDs. For any of the
     60   // 128-bits of a UUID, set the corresponding bit in the mask to 1 to match the
     61   // advertised value, and 0 to ignore that bit.
     62   void SetServiceUuidWithMask(const UUID& service_uuid, const UUID& mask);
     63 
     64   // Comparison operator.
     65   bool operator==(const ScanFilter& rhs) const;
     66 
     67  private:
     68   std::string device_name_;
     69   std::string device_address_;
     70 
     71   std::unique_ptr<UUID> service_uuid_;
     72   std::unique_ptr<UUID> service_uuid_mask_;
     73 
     74   // TODO(armansito): Add service and manufacturer data filter fields.
     75 };
     76 
     77 }  // namespace bluetooth
     78