2 // Copyright 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
20 #include "service/gatt_server.h"
21 #include "service/hal/fake_bluetooth_gatt_interface.h"
24 using ::testing::Return;
30 : public hal::FakeBluetoothGattInterface::TestServerHandler {
32 MockGattHandler() = default;
33 ~MockGattHandler() override = default;
35 MOCK_METHOD1(RegisterServer, bt_status_t(const bluetooth::Uuid&));
36 MOCK_METHOD1(UnregisterServer, bt_status_t(int));
37 MOCK_METHOD2(AddService, bt_status_t(int, std::vector<btgatt_db_element_t>));
38 MOCK_METHOD5(AddCharacteristic,
39 bt_status_t(int, int, bluetooth::Uuid*, int, int));
40 MOCK_METHOD4(AddDescriptor, bt_status_t(int, int, bluetooth::Uuid*, int));
41 MOCK_METHOD3(StartService, bt_status_t(int, int, int));
42 MOCK_METHOD2(DeleteService, bt_status_t(int, int));
43 MOCK_METHOD5(SendIndication,
44 bt_status_t(int, int, int, int, std::vector<uint8_t>));
45 MOCK_METHOD4(SendResponse,
46 bt_status_t(int, int, int, const btgatt_response_t&));
49 DISALLOW_COPY_AND_ASSIGN(MockGattHandler);
52 class TestDelegate : public GattServer::Delegate {
54 TestDelegate() = default;
55 ~TestDelegate() override = default;
67 ~RequestData() = default;
69 std::string device_address;
78 std::vector<uint8_t> write_value;
82 void OnCharacteristicReadRequest(GattServer* gatt_server,
83 const std::string& device_address,
84 int request_id, int offset, bool is_long,
85 uint16_t handle) override {
86 ASSERT_TRUE(gatt_server);
87 char_read_req_.device_address = device_address;
88 char_read_req_.id = request_id;
89 char_read_req_.offset = offset;
90 char_read_req_.is_long = is_long;
91 char_read_req_.handle = handle;
92 char_read_req_.count++;
95 void OnDescriptorReadRequest(GattServer* gatt_server,
96 const std::string& device_address,
97 int request_id, int offset, bool is_long,
98 uint16_t handle) override {
99 ASSERT_TRUE(gatt_server);
100 desc_read_req_.device_address = device_address;
101 desc_read_req_.id = request_id;
102 desc_read_req_.offset = offset;
103 desc_read_req_.is_long = is_long;
104 desc_read_req_.handle = handle;
105 desc_read_req_.count++;
108 void OnCharacteristicWriteRequest(GattServer* gatt_server,
109 const std::string& device_address,
110 int request_id, int offset,
111 bool is_prepare_write, bool need_response,
112 const std::vector<uint8_t>& value,
113 uint16_t handle) override {
114 ASSERT_TRUE(gatt_server);
115 char_write_req_.device_address = device_address;
116 char_write_req_.id = request_id;
117 char_write_req_.offset = offset;
118 char_write_req_.is_prep = is_prepare_write;
119 char_write_req_.need_rsp = need_response;
120 char_write_req_.handle = handle;
121 char_write_req_.count++;
122 char_write_req_.write_value = value;
125 void OnDescriptorWriteRequest(GattServer* gatt_server,
126 const std::string& device_address,
127 int request_id, int offset,
128 bool is_prepare_write, bool need_response,
129 const std::vector<uint8_t>& value,
130 uint16_t handle) override {
131 ASSERT_TRUE(gatt_server);
132 desc_write_req_.device_address = device_address;
133 desc_write_req_.id = request_id;
134 desc_write_req_.offset = offset;
135 desc_write_req_.is_prep = is_prepare_write;
136 desc_write_req_.need_rsp = need_response;
137 desc_write_req_.handle = handle;
138 desc_write_req_.count++;
139 desc_write_req_.write_value = value;
142 void OnExecuteWriteRequest(GattServer* gatt_server,
143 const std::string& device_address, int request_id,
144 bool is_execute) override {
145 ASSERT_TRUE(gatt_server);
146 exec_req_.device_address = device_address;
147 exec_req_.id = request_id;
148 exec_req_.is_exec = is_execute;
152 void OnConnectionStateChanged(GattServer* gatt_server,
153 const std::string& device_address,
154 bool connected) override {
155 ASSERT_TRUE(gatt_server);
156 conn_state_changed_.device_address = device_address;
157 conn_state_changed_.connected = connected;
158 conn_state_changed_.count++;
161 const RequestData& char_read_req() const { return char_read_req_; }
162 const RequestData& desc_read_req() const { return desc_read_req_; }
163 const RequestData& char_write_req() const { return char_write_req_; }
164 const RequestData& desc_write_req() const { return desc_write_req_; }
165 const RequestData& conn_state_changed() const { return conn_state_changed_; }
168 RequestData char_read_req_;
169 RequestData desc_read_req_;
170 RequestData char_write_req_;
171 RequestData desc_write_req_;
172 RequestData exec_req_;
173 RequestData conn_state_changed_;
176 class GattServerTest : public ::testing::Test {
178 GattServerTest() = default;
179 ~GattServerTest() override = default;
181 void SetUp() override {
182 mock_handler_.reset(new MockGattHandler());
183 fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface(
184 nullptr, nullptr, nullptr,
185 std::static_pointer_cast<
186 hal::FakeBluetoothGattInterface::TestServerHandler>(mock_handler_));
188 hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_);
189 factory_.reset(new GattServerFactory());
192 void TearDown() override {
194 hal::BluetoothGattInterface::CleanUp();
198 hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_;
199 std::shared_ptr<MockGattHandler> mock_handler_;
200 std::unique_ptr<GattServerFactory> factory_;
203 DISALLOW_COPY_AND_ASSIGN(GattServerTest);
206 const int kDefaultServerId = 4;
208 class GattServerPostRegisterTest : public GattServerTest {
210 GattServerPostRegisterTest() = default;
211 ~GattServerPostRegisterTest() override = default;
213 void SetUp() override {
214 GattServerTest::SetUp();
215 Uuid uuid = Uuid::GetRandom();
216 auto callback = [&](BLEStatus status, const Uuid& in_uuid,
217 std::unique_ptr<BluetoothInstance> in_client) {
218 CHECK(in_uuid == uuid);
219 CHECK(in_client.get());
220 CHECK(status == BLE_STATUS_SUCCESS);
222 gatt_server_ = std::unique_ptr<GattServer>(
223 static_cast<GattServer*>(in_client.release()));
226 EXPECT_CALL(*mock_handler_, RegisterServer(_))
228 .WillOnce(Return(BT_STATUS_SUCCESS));
230 factory_->RegisterInstance(uuid, callback);
232 fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_SUCCESS,
233 kDefaultServerId, uuid);
236 void TearDown() override {
237 EXPECT_CALL(*mock_handler_, UnregisterServer(_))
239 .WillOnce(Return(BT_STATUS_SUCCESS));
240 gatt_server_ = nullptr;
241 GattServerTest::TearDown();
244 void SetUpTestService() {
245 EXPECT_CALL(*mock_handler_, AddService(_, _))
247 .WillOnce(Return(BT_STATUS_SUCCESS));
249 Uuid uuid0 = Uuid::GetRandom();
250 Uuid uuid1 = Uuid::GetRandom();
251 Uuid uuid2 = Uuid::GetRandom();
253 bool register_success = false;
255 Service service(0, true, uuid0, {}, {});
257 ASSERT_TRUE(gatt_server_->AddService(
258 service, [&](BLEStatus status, const Service& added_service) {
259 ASSERT_EQ(BLE_STATUS_SUCCESS, status);
260 ASSERT_TRUE(Uuid(added_service.uuid()) == Uuid(service.uuid()));
261 ASSERT_TRUE(added_service.handle() == 0x0001);
262 register_success = true;
265 srvc_handle_ = 0x0001;
266 char_handle_ = 0x0002;
267 desc_handle_ = 0x0004;
269 std::vector<btgatt_db_element_t> service_with_handles = {
270 {.type = BTGATT_DB_PRIMARY_SERVICE,
272 .attribute_handle = srvc_handle_},
273 {.type = BTGATT_DB_CHARACTERISTIC,
275 .attribute_handle = char_handle_},
276 {.type = BTGATT_DB_DESCRIPTOR,
278 .attribute_handle = desc_handle_},
281 fake_hal_gatt_iface_->NotifyServiceAddedCallback(
282 BT_STATUS_SUCCESS, kDefaultServerId, service_with_handles);
284 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
286 ASSERT_TRUE(register_success);
290 std::unique_ptr<GattServer> gatt_server_;
292 uint16_t srvc_handle_;
293 uint16_t char_handle_;
294 uint16_t desc_handle_;
297 DISALLOW_COPY_AND_ASSIGN(GattServerPostRegisterTest);
300 TEST_F(GattServerTest, RegisterServer) {
301 EXPECT_CALL(*mock_handler_, RegisterServer(_))
303 .WillOnce(Return(BT_STATUS_FAIL))
304 .WillOnce(Return(BT_STATUS_SUCCESS));
306 // These will be asynchronously populate with a result when the callback
308 BLEStatus status = BLE_STATUS_SUCCESS;
310 std::unique_ptr<GattServer> server;
311 int callback_count = 0;
313 auto callback = [&](BLEStatus in_status, const Uuid& uuid,
314 std::unique_ptr<BluetoothInstance> in_server) {
317 server = std::unique_ptr<GattServer>(
318 static_cast<GattServer*>(in_server.release()));
322 Uuid uuid0 = Uuid::GetRandom();
324 // HAL returns failure.
325 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
326 EXPECT_EQ(0, callback_count);
328 // HAL returns success.
329 EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback));
330 EXPECT_EQ(0, callback_count);
332 // Calling twice with the same Uuid should fail with no additional calls into
334 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
336 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
338 // Call with a different Uuid while one is pending.
339 Uuid uuid1 = Uuid::GetRandom();
340 EXPECT_CALL(*mock_handler_, RegisterServer(_))
342 .WillOnce(Return(BT_STATUS_SUCCESS));
343 EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback));
345 // Trigger callback with an unknown Uuid. This should get ignored.
346 bluetooth::Uuid hal_uuid = bluetooth::Uuid::GetRandom();
347 fake_hal_gatt_iface_->NotifyRegisterServerCallback(0, 0, hal_uuid);
348 EXPECT_EQ(0, callback_count);
351 int server_if0 = 2; // Pick something that's not 0.
352 fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_SUCCESS,
355 EXPECT_EQ(1, callback_count);
356 ASSERT_TRUE(server.get() != nullptr); // Assert to terminate in case of error
357 EXPECT_EQ(BLE_STATUS_SUCCESS, status);
358 EXPECT_EQ(server_if0, server->GetInstanceId());
359 EXPECT_EQ(uuid0, server->GetAppIdentifier());
360 EXPECT_EQ(uuid0, cb_uuid);
362 // The server should unregister itself when deleted.
363 EXPECT_CALL(*mock_handler_, UnregisterServer(server_if0))
365 .WillOnce(Return(BT_STATUS_SUCCESS));
368 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
372 fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_FAIL, server_if1,
375 EXPECT_EQ(2, callback_count);
376 ASSERT_TRUE(server.get() == nullptr); // Assert to terminate in case of error
377 EXPECT_EQ(BLE_STATUS_FAILURE, status);
378 EXPECT_EQ(uuid1, cb_uuid);
381 TEST_F(GattServerPostRegisterTest, RequestRead) {
384 TestDelegate test_delegate;
385 gatt_server_->SetDelegate(&test_delegate);
387 const std::vector<uint8_t> kTestValue = {0x01, 0x02, 0x03};
388 const std::vector<uint8_t> kTestValueTooLarge(BTGATT_MAX_ATTR_LEN + 1, 0);
389 const std::string kTestAddress0 = "01:23:45:67:89:AB";
390 const std::string kTestAddress1 = "CD:EF:01:23:45:67";
391 const int kReqId0 = 0;
392 const int kReqId1 = 1;
393 const int kConnId0 = 1;
395 // No pending request.
396 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
397 GATT_ERROR_NONE, 0, kTestValue));
399 RawAddress hal_addr0, hal_addr1;
400 ASSERT_TRUE(RawAddress::FromString(kTestAddress0, hal_addr0));
401 ASSERT_TRUE(RawAddress::FromString(kTestAddress1, hal_addr1));
403 // Send a connection callback. The GattServer should store the connection
404 // information and be able to process the incoming read requests for this
406 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
407 kConnId0, kDefaultServerId, true, hal_addr0);
409 // Unknown connection ID shouldn't trigger anything.
410 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
411 kConnId0 + 1, kReqId0, hal_addr0, char_handle_, 0, false);
412 EXPECT_EQ(0, test_delegate.char_read_req().count);
413 EXPECT_EQ(0, test_delegate.desc_read_req().count);
415 // Unknown device address shouldn't trigger anything.
416 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
417 kConnId0, kReqId0, hal_addr1, char_handle_, 0, false);
418 EXPECT_EQ(0, test_delegate.char_read_req().count);
419 EXPECT_EQ(0, test_delegate.desc_read_req().count);
421 // Characteristic and descriptor handles should trigger correct callbacks.
422 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
423 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false);
424 EXPECT_EQ(1, test_delegate.char_read_req().count);
425 EXPECT_EQ(kTestAddress0, test_delegate.char_read_req().device_address);
426 EXPECT_EQ(kReqId0, test_delegate.char_read_req().id);
427 EXPECT_EQ(0, test_delegate.char_read_req().offset);
428 EXPECT_FALSE(test_delegate.char_read_req().is_long);
429 EXPECT_TRUE(char_handle_ == test_delegate.char_read_req().handle);
430 EXPECT_EQ(0, test_delegate.desc_read_req().count);
432 fake_hal_gatt_iface_->NotifyRequestReadDescriptorCallback(
433 kConnId0, kReqId1, hal_addr0, desc_handle_, 2, true);
434 EXPECT_EQ(1, test_delegate.char_read_req().count);
435 EXPECT_EQ(1, test_delegate.desc_read_req().count);
436 EXPECT_EQ(kTestAddress0, test_delegate.desc_read_req().device_address);
437 EXPECT_EQ(kReqId1, test_delegate.desc_read_req().id);
438 EXPECT_EQ(2, test_delegate.desc_read_req().offset);
439 EXPECT_TRUE(test_delegate.desc_read_req().is_long);
440 EXPECT_TRUE(desc_handle_ == test_delegate.desc_read_req().handle);
442 // Callback with a pending request ID will be ignored.
443 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
444 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false);
445 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
446 kConnId0, kReqId1, hal_addr0, char_handle_, 0, false);
447 EXPECT_EQ(1, test_delegate.char_read_req().count);
448 EXPECT_EQ(1, test_delegate.desc_read_req().count);
450 // Send response for wrong device address.
451 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress1, kReqId0,
452 GATT_ERROR_NONE, 0, kTestValue));
454 // Send response for a value that's too large.
455 EXPECT_FALSE(gatt_server_->SendResponse(
456 kTestAddress0, kReqId0, GATT_ERROR_NONE, 0, kTestValueTooLarge));
458 EXPECT_CALL(*mock_handler_,
459 SendResponse(kConnId0, kReqId0, BT_STATUS_SUCCESS, _))
461 .WillOnce(Return(BT_STATUS_FAIL))
462 .WillOnce(Return(BT_STATUS_SUCCESS));
465 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
466 GATT_ERROR_NONE, 0, kTestValue));
468 // Successful send response for characteristic.
469 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
470 GATT_ERROR_NONE, 0, kTestValue));
472 // Characteristic request ID no longer pending.
473 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
474 GATT_ERROR_NONE, 0, kTestValue));
476 EXPECT_CALL(*mock_handler_,
477 SendResponse(kConnId0, kReqId1, BT_STATUS_SUCCESS, _))
479 .WillOnce(Return(BT_STATUS_SUCCESS));
481 // Successful send response for descriptor.
482 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
483 GATT_ERROR_NONE, 0, kTestValue));
485 // Descriptor request ID no longer pending.
486 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
487 GATT_ERROR_NONE, 0, kTestValue));
489 gatt_server_->SetDelegate(nullptr);
492 TEST_F(GattServerPostRegisterTest, RequestWrite) {
495 TestDelegate test_delegate;
496 gatt_server_->SetDelegate(&test_delegate);
498 const std::vector<uint8_t> kTestValue = {0x01, 0x02, 0x03};
499 const std::string kTestAddress0 = "01:23:45:67:89:AB";
500 const std::string kTestAddress1 = "CD:EF:01:23:45:67";
501 const int kReqId0 = 0;
502 const int kReqId1 = 1;
503 const int kConnId0 = 1;
505 // No pending request.
506 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
507 GATT_ERROR_NONE, 0, kTestValue));
509 RawAddress hal_addr0, hal_addr1;
510 ASSERT_TRUE(RawAddress::FromString(kTestAddress0, hal_addr0));
511 ASSERT_TRUE(RawAddress::FromString(kTestAddress1, hal_addr1));
513 // Send a connection callback. The GattServer should store the connection
514 // information and be able to process the incoming read requests for this
516 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
517 kConnId0, kDefaultServerId, true, hal_addr0);
519 // Unknown connection ID shouldn't trigger anything.
520 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
521 kConnId0 + 1, kReqId0, hal_addr0, char_handle_, 0, true, false,
523 EXPECT_EQ(0, test_delegate.char_write_req().count);
524 EXPECT_EQ(0, test_delegate.desc_write_req().count);
526 // Unknown device address shouldn't trigger anything.
527 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
528 kConnId0, kReqId0, hal_addr1, char_handle_, 0, true, false, kTestValue);
529 EXPECT_EQ(0, test_delegate.char_write_req().count);
530 EXPECT_EQ(0, test_delegate.desc_write_req().count);
532 // Characteristic and descriptor handles should trigger correct callbacks.
533 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
534 kConnId0, kReqId0, hal_addr0, char_handle_, 0, true, false, kTestValue);
535 EXPECT_EQ(1, test_delegate.char_write_req().count);
536 EXPECT_EQ(kTestAddress0, test_delegate.char_write_req().device_address);
537 EXPECT_EQ(kReqId0, test_delegate.char_write_req().id);
538 EXPECT_EQ(0, test_delegate.char_write_req().offset);
539 EXPECT_EQ(true, test_delegate.char_write_req().need_rsp);
540 EXPECT_EQ(false, test_delegate.char_write_req().is_exec);
541 EXPECT_EQ(kTestValue, test_delegate.char_write_req().write_value);
542 EXPECT_TRUE(char_handle_ == test_delegate.char_write_req().handle);
543 EXPECT_EQ(0, test_delegate.desc_write_req().count);
545 fake_hal_gatt_iface_->NotifyRequestWriteDescriptorCallback(
546 kConnId0, kReqId1, hal_addr0, desc_handle_, 2, true, false, kTestValue);
547 EXPECT_EQ(1, test_delegate.char_write_req().count);
548 EXPECT_EQ(1, test_delegate.desc_write_req().count);
549 EXPECT_EQ(kTestAddress0, test_delegate.desc_write_req().device_address);
550 EXPECT_EQ(kReqId1, test_delegate.desc_write_req().id);
551 EXPECT_EQ(2, test_delegate.desc_write_req().offset);
552 EXPECT_EQ(true, test_delegate.desc_write_req().need_rsp);
553 EXPECT_EQ(false, test_delegate.desc_write_req().is_exec);
554 EXPECT_EQ(kTestValue, test_delegate.desc_write_req().write_value);
555 EXPECT_TRUE(desc_handle_ == test_delegate.desc_write_req().handle);
557 // Callback with a pending request ID will be ignored.
558 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
559 kConnId0, kReqId0, hal_addr0, char_handle_, 0, true, false, kTestValue);
560 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
561 kConnId0, kReqId1, hal_addr0, char_handle_, 0, true, false, kTestValue);
562 EXPECT_EQ(1, test_delegate.char_write_req().count);
563 EXPECT_EQ(1, test_delegate.desc_write_req().count);
565 // Send response for wrong device address.
566 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress1, kReqId0,
567 GATT_ERROR_NONE, 0, kTestValue));
569 EXPECT_CALL(*mock_handler_,
570 SendResponse(kConnId0, kReqId0, BT_STATUS_SUCCESS, _))
572 .WillOnce(Return(BT_STATUS_FAIL))
573 .WillOnce(Return(BT_STATUS_SUCCESS));
576 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
577 GATT_ERROR_NONE, 0, kTestValue));
579 // Successful send response for characteristic.
580 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
581 GATT_ERROR_NONE, 0, kTestValue));
583 // Characteristic request ID no longer pending.
584 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
585 GATT_ERROR_NONE, 0, kTestValue));
587 EXPECT_CALL(*mock_handler_,
588 SendResponse(kConnId0, kReqId1, BT_STATUS_SUCCESS, _))
590 .WillOnce(Return(BT_STATUS_SUCCESS));
592 // Successful send response for descriptor.
593 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
594 GATT_ERROR_NONE, 0, kTestValue));
596 // Descriptor request ID no longer pending.
597 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
598 GATT_ERROR_NONE, 0, kTestValue));
600 // SendResponse should fail for a "Write Without Response".
601 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
602 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false, false, kTestValue);
603 EXPECT_EQ(false, test_delegate.char_write_req().need_rsp);
604 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
605 GATT_ERROR_NONE, 0, kTestValue));
607 gatt_server_->SetDelegate(nullptr);
610 TEST_F(GattServerPostRegisterTest, SendNotification) {
613 const std::string kTestAddress0 = "01:23:45:67:89:AB";
614 const std::string kTestAddress1 = "cd:ef:01:23:45:67";
615 const std::string kInvalidAddress = "thingamajig blabbidyboop";
616 const int kConnId0 = 0;
617 const int kConnId1 = 1;
618 std::vector<uint8_t> value;
619 RawAddress hal_addr0;
620 ASSERT_TRUE(RawAddress::FromString(kTestAddress0, hal_addr0));
622 // Set up two connections with the same address.
623 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
624 kConnId0, kDefaultServerId, true, hal_addr0);
625 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
626 kConnId1, kDefaultServerId, true, hal_addr0);
628 // Set up a test callback.
629 GATTError gatt_error;
630 int callback_count = 0;
631 auto callback = [&](GATTError in_error) {
632 gatt_error = in_error;
636 // Bad device address.
637 EXPECT_FALSE(gatt_server_->SendNotification(kInvalidAddress, char_handle_,
638 false, value, callback));
641 EXPECT_FALSE(gatt_server_->SendNotification(kTestAddress1, char_handle_,
642 false, value, callback));
644 // We should get a HAL call for each connection for this address. The calls
646 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
649 .WillOnce(Return(BT_STATUS_FAIL));
650 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
653 .WillOnce(Return(BT_STATUS_FAIL));
654 EXPECT_FALSE(gatt_server_->SendNotification(kTestAddress0, char_handle_,
655 false, value, callback));
657 // One of the calls succeeds.
658 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
661 .WillOnce(Return(BT_STATUS_SUCCESS));
662 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
665 .WillOnce(Return(BT_STATUS_FAIL));
666 EXPECT_TRUE(gatt_server_->SendNotification(kTestAddress0, char_handle_, false,
669 // One of the connections is already pending so there should be only one call.
670 // This one we send with confirm=true.
671 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
674 .WillOnce(Return(BT_STATUS_SUCCESS));
675 EXPECT_TRUE(gatt_server_->SendNotification(kTestAddress0, char_handle_, true,
678 // Calls are already pending.
679 EXPECT_FALSE(gatt_server_->SendNotification(kTestAddress0, char_handle_, true,
682 // Trigger one confirmation callback. We should get calls for two callbacks
683 // since we have two separate calls pending.
684 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0,
686 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId1,
688 EXPECT_EQ(2, callback_count);
689 EXPECT_EQ(GATT_ERROR_NONE, gatt_error);
693 // Restart. Both calls succeed now.
694 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
697 .WillOnce(Return(BT_STATUS_SUCCESS));
698 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
701 .WillOnce(Return(BT_STATUS_SUCCESS));
702 EXPECT_TRUE(gatt_server_->SendNotification(kTestAddress0, char_handle_, false,
705 // Trigger one confirmation callback. The callback we passed should still be
706 // pending. The first callback is for the wrong connection ID.
707 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0 + 50,
709 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0,
711 EXPECT_EQ(0, callback_count);
713 // This should be ignored since |kConnId0| was already processed.
714 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0,
716 EXPECT_EQ(0, callback_count);
718 // Run the callback with failure. Since the previous callback reported
719 // success, we should report success.
720 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId1,
722 EXPECT_EQ(1, callback_count);
723 EXPECT_EQ(GATT_ERROR_NONE, gatt_error);
727 } // namespace bluetooth