• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2015 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "gatt/gatt_test.h"
20 
21 #include "adapter/bluetooth_test.h"
22 #include "types/bluetooth/uuid.h"
23 #include "types/raw_address.h"
24 
25 namespace bttest {
26 
27 static GattTest* instance = nullptr;
28 
RegisterClientCallback(int status,int clientIf,const bluetooth::Uuid &)29 void RegisterClientCallback(int status, int clientIf, const bluetooth::Uuid& /*app_uuid*/) {
30   instance->status_ = status;
31   instance->client_interface_id_ = clientIf;
32   semaphore_post(instance->register_client_callback_sem_);
33 }
34 
35 // GATT server callbacks
RegisterServerCallback(int status,int server_if,const bluetooth::Uuid &)36 void RegisterServerCallback(int status, int server_if, const bluetooth::Uuid& /*uuid*/) {
37   instance->status_ = status;
38   instance->server_interface_id_ = server_if;
39   semaphore_post(instance->register_server_callback_sem_);
40 }
41 
ServiceAddedCallback(int status,int server_if,const btgatt_db_element_t * service,size_t)42 void ServiceAddedCallback(int status, int server_if, const btgatt_db_element_t* service,
43                           size_t /*service_count*/) {
44   instance->status_ = status;
45   instance->server_interface_id_ = server_if;
46   instance->service_handle_ = service[0].attribute_handle;
47   semaphore_post(instance->service_added_callback_sem_);
48 }
49 
ServiceStoppedCallback(int status,int server_if,int srvc_handle)50 void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
51   instance->status_ = status;
52   instance->server_interface_id_ = server_if;
53   instance->service_handle_ = srvc_handle;
54   semaphore_post(instance->service_stopped_callback_sem_);
55 }
56 
ServiceDeletedCallback(int status,int server_if,int srvc_handle)57 void ServiceDeletedCallback(int status, int server_if, int srvc_handle) {
58   instance->status_ = status;
59   instance->server_interface_id_ = server_if;
60   instance->service_handle_ = srvc_handle;
61   semaphore_post(instance->service_deleted_callback_sem_);
62 }
63 
64 static const btgatt_client_callbacks_t client_callbacks = {
65         .register_client_cb = RegisterClientCallback,
66 };
67 
68 static const btgatt_server_callbacks_t server_callbacks = {
69         .register_server_cb = RegisterServerCallback,
70         .service_added_cb = ServiceAddedCallback,
71         .service_stopped_cb = ServiceStoppedCallback,
72         .service_deleted_cb = ServiceDeletedCallback,
73 };
74 
75 static const btgatt_callbacks_t callbacks = {
76         sizeof(btgatt_callbacks_t),
77         &client_callbacks,
78         &server_callbacks,
79 };
80 
SetUp()81 void GattTest::SetUp() {
82   gatt_interface_ = nullptr;
83 
84   client_interface_id_ = 0;
85   server_interface_id_ = 0;
86   service_handle_ = 0;
87   characteristic_handle_ = 0;
88   descriptor_handle_ = 0;
89   status_ = 0;
90 
91   BluetoothTest::SetUp();
92   ASSERT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
93   semaphore_wait(adapter_state_changed_callback_sem_);
94   EXPECT_TRUE(GetState() == BT_STATE_ON);
95 
96   gatt_interface_ = reinterpret_cast<const btgatt_interface_t*>(
97           bt_interface()->get_profile_interface(BT_PROFILE_GATT_ID));
98   ASSERT_NE(nullptr, gatt_interface_);
99   instance = this;
100   auto status = gatt_interface_->init(&callbacks);
101   ASSERT_EQ(status, BT_STATUS_SUCCESS);
102 }
103 
TearDown()104 void GattTest::TearDown() {
105   instance = nullptr;
106   gatt_interface_ = nullptr;
107 
108   ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
109   semaphore_wait(adapter_state_changed_callback_sem_);
110   BluetoothTest::TearDown();
111 }
112 
gatt_scanner_interface()113 const BleScannerInterface* GattTest::gatt_scanner_interface() { return gatt_interface_->scanner; }
114 
gatt_client_interface()115 const btgatt_client_interface_t* GattTest::gatt_client_interface() {
116   return gatt_interface_->client;
117 }
118 
gatt_server_interface()119 const btgatt_server_interface_t* GattTest::gatt_server_interface() {
120   return gatt_interface_->server;
121 }
122 
123 }  // namespace bttest
124