bluetooth_remote_gatt_service.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_remote_gatt_service.h"
  5. #include <utility>
  6. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  7. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  8. namespace device {
  9. BluetoothRemoteGattService::BluetoothRemoteGattService() = default;
  10. BluetoothRemoteGattService::~BluetoothRemoteGattService() = default;
  11. std::vector<BluetoothRemoteGattCharacteristic*>
  12. BluetoothRemoteGattService::GetCharacteristics() const {
  13. std::vector<BluetoothRemoteGattCharacteristic*> characteristics;
  14. characteristics.reserve(characteristics_.size());
  15. for (const auto& characteristic : characteristics_)
  16. characteristics.push_back(characteristic.second.get());
  17. return characteristics;
  18. }
  19. BluetoothRemoteGattCharacteristic*
  20. BluetoothRemoteGattService::GetCharacteristic(
  21. const std::string& identifier) const {
  22. auto iter = characteristics_.find(identifier);
  23. return iter != characteristics_.end() ? iter->second.get() : nullptr;
  24. }
  25. std::vector<BluetoothRemoteGattCharacteristic*>
  26. BluetoothRemoteGattService::GetCharacteristicsByUUID(
  27. const BluetoothUUID& characteristic_uuid) const {
  28. std::vector<BluetoothRemoteGattCharacteristic*> result;
  29. for (const auto& characteristic : characteristics_) {
  30. if (characteristic.second->GetUUID() == characteristic_uuid)
  31. result.push_back(characteristic.second.get());
  32. }
  33. return result;
  34. }
  35. bool BluetoothRemoteGattService::IsDiscoveryComplete() const {
  36. return discovery_complete_;
  37. }
  38. void BluetoothRemoteGattService::SetDiscoveryComplete(bool complete) {
  39. discovery_complete_ = complete;
  40. }
  41. bool BluetoothRemoteGattService::AddCharacteristic(
  42. std::unique_ptr<BluetoothRemoteGattCharacteristic> characteristic) {
  43. if (!characteristic)
  44. return false;
  45. const auto& characteristic_raw = *characteristic;
  46. return characteristics_
  47. .try_emplace(characteristic_raw.GetIdentifier(),
  48. std::move(characteristic))
  49. .second;
  50. }
  51. } // namespace device