OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 |
| 5 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chromeos/dbus/dbus_thread_manager.h" |
| 9 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" |
| 10 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 11 |
| 12 namespace chromeos { |
| 13 |
| 14 // static |
| 15 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = |
| 16 "service0000"; |
| 17 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = |
| 18 "0000180d-0000-1000-8000-00805f9b34fb"; |
| 19 |
| 20 FakeBluetoothGattServiceClient::Properties::Properties( |
| 21 const PropertyChangedCallback& callback) |
| 22 : BluetoothGattServiceClient::Properties( |
| 23 NULL, |
| 24 bluetooth_gatt_service::kBluetoothGattServiceInterface, |
| 25 callback) { |
| 26 } |
| 27 |
| 28 FakeBluetoothGattServiceClient::Properties::~Properties() { |
| 29 } |
| 30 |
| 31 void FakeBluetoothGattServiceClient::Properties::Get( |
| 32 dbus::PropertyBase* property, |
| 33 dbus::PropertySet::GetCallback callback) { |
| 34 VLOG(1) << "Get " << property->name(); |
| 35 callback.Run(false); |
| 36 } |
| 37 |
| 38 void FakeBluetoothGattServiceClient::Properties::GetAll() { |
| 39 VLOG(1) << "GetAll"; |
| 40 } |
| 41 |
| 42 void FakeBluetoothGattServiceClient::Properties::Set( |
| 43 dbus::PropertyBase* property, |
| 44 dbus::PropertySet::GetCallback callback) { |
| 45 VLOG(1) << "Set " << property->name(); |
| 46 callback.Run(false); |
| 47 } |
| 48 |
| 49 FakeBluetoothGattServiceClient::FakeBluetoothGattServiceClient() { |
| 50 } |
| 51 |
| 52 FakeBluetoothGattServiceClient::~FakeBluetoothGattServiceClient() { |
| 53 } |
| 54 |
| 55 void FakeBluetoothGattServiceClient::Init(dbus::Bus* bus) { |
| 56 } |
| 57 |
| 58 void FakeBluetoothGattServiceClient::AddObserver(Observer* observer) { |
| 59 observers_.AddObserver(observer); |
| 60 } |
| 61 |
| 62 void FakeBluetoothGattServiceClient::RemoveObserver(Observer* observer) { |
| 63 observers_.RemoveObserver(observer); |
| 64 } |
| 65 |
| 66 std::vector<dbus::ObjectPath> FakeBluetoothGattServiceClient::GetServices() { |
| 67 std::vector<dbus::ObjectPath> paths; |
| 68 if (heart_rate_service_properties_.get()) { |
| 69 DCHECK(!heart_rate_service_path_.empty()); |
| 70 paths.push_back(dbus::ObjectPath(heart_rate_service_path_)); |
| 71 } |
| 72 return paths; |
| 73 } |
| 74 |
| 75 FakeBluetoothGattServiceClient::Properties* |
| 76 FakeBluetoothGattServiceClient::GetProperties( |
| 77 const dbus::ObjectPath& object_path) { |
| 78 if (object_path.value() == heart_rate_service_path_) |
| 79 return heart_rate_service_properties_.get(); |
| 80 return NULL; |
| 81 } |
| 82 |
| 83 void FakeBluetoothGattServiceClient::ExposeHeartRateService( |
| 84 const dbus::ObjectPath& device_path) { |
| 85 if (heart_rate_service_properties_.get()) { |
| 86 DCHECK(!heart_rate_service_path_.empty()); |
| 87 VLOG(1) << "Fake Heart Rate Service already exposed."; |
| 88 return; |
| 89 } |
| 90 VLOG(2) << "Exposing fake Heart Rate Service."; |
| 91 heart_rate_service_path_ = |
| 92 device_path.value() + "/" + kHeartRateServicePathComponent; |
| 93 heart_rate_service_properties_.reset(new Properties(base::Bind( |
| 94 &FakeBluetoothGattServiceClient::OnPropertyChanged, |
| 95 base::Unretained(this), |
| 96 dbus::ObjectPath(heart_rate_service_path_)))); |
| 97 heart_rate_service_properties_->uuid.ReplaceValue(heart_rate_service_path_); |
| 98 |
| 99 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); |
| 100 |
| 101 FakeBluetoothGattCharacteristicClient* char_client = |
| 102 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 103 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); |
| 104 char_client->ExposeHeartRateCharacteristics( |
| 105 dbus::ObjectPath(heart_rate_service_path_)); |
| 106 } |
| 107 |
| 108 void FakeBluetoothGattServiceClient::HideHeartRateService() { |
| 109 if (!heart_rate_service_properties_.get()) { |
| 110 DCHECK(heart_rate_service_path_.empty()); |
| 111 VLOG(1) << "Fake Heart Rate Service already hidden."; |
| 112 return; |
| 113 } |
| 114 VLOG(2) << "Hiding fake Heart Rate Service."; |
| 115 FakeBluetoothGattCharacteristicClient* char_client = |
| 116 static_cast<FakeBluetoothGattCharacteristicClient*>( |
| 117 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); |
| 118 char_client->HideHeartRateCharacteristics(); |
| 119 |
| 120 heart_rate_service_properties_.reset(); |
| 121 std::string hrs_path = heart_rate_service_path_; |
| 122 heart_rate_service_path_.clear(); |
| 123 |
| 124 NotifyServiceRemoved(dbus::ObjectPath(hrs_path)); |
| 125 } |
| 126 |
| 127 void FakeBluetoothGattServiceClient::OnPropertyChanged( |
| 128 const dbus::ObjectPath& object_path, |
| 129 const std::string& property_name) { |
| 130 VLOG(2) << "Fake GATT Service property changed: " << object_path.value() |
| 131 << ": " << property_name; |
| 132 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, |
| 133 GattServicePropertyChanged(object_path, property_name)); |
| 134 } |
| 135 |
| 136 void FakeBluetoothGattServiceClient::NotifyServiceAdded( |
| 137 const dbus::ObjectPath& object_path) { |
| 138 VLOG(2) << "GATT service added: " << object_path.value(); |
| 139 FOR_EACH_OBSERVER( |
| 140 BluetoothGattServiceClient::Observer, observers_, |
| 141 GattServiceAdded(object_path)); |
| 142 } |
| 143 |
| 144 void FakeBluetoothGattServiceClient::NotifyServiceRemoved( |
| 145 const dbus::ObjectPath& object_path) { |
| 146 VLOG(2) << "GATT service removed: " << object_path.value(); |
| 147 FOR_EACH_OBSERVER( |
| 148 BluetoothGattServiceClient::Observer, observers_, |
| 149 GattServiceRemoved(object_path)); |
| 150 } |
| 151 |
| 152 } // namespace chromeos |
OLD | NEW |