aboutsummaryrefslogtreecommitdiff
path: root/test/suite/gatt/gatt_unittest.cc
blob: e91e7f6dc6ea3ba8f5439998fed7898a25aa9dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/******************************************************************************
 *
 *  Copyright 2015 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "gatt/gatt_test.h"

namespace bttest {

TEST_F(GattTest, GattClientRegister) {
  // Registers gatt client.
  bluetooth::Uuid gatt_client_uuid = bluetooth::Uuid::GetRandom();
  gatt_client_interface()->register_client(gatt_client_uuid, false);
  semaphore_wait(register_client_callback_sem_);
  EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
      << "Error registering GATT client app callback.";

  // Unregisters gatt client. No callback is expected.
  gatt_client_interface()->unregister_client(client_interface_id());
}

TEST_F(GattTest, GattServerRegister) {
  // Registers gatt server.
  bluetooth::Uuid gatt_server_uuid = bluetooth::Uuid::GetRandom();
  gatt_server_interface()->register_server(gatt_server_uuid, false);
  semaphore_wait(register_server_callback_sem_);
  EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
      << "Error registering GATT server app callback.";

  // Unregisters gatt server. No callback is expected.
  gatt_server_interface()->unregister_server(server_interface_id());
}

TEST_F(GattTest, GattServerBuild) {
  // Registers gatt server.
  bluetooth::Uuid gatt_server_uuid = bluetooth::Uuid::GetRandom();
  gatt_server_interface()->register_server(gatt_server_uuid, false);
  semaphore_wait(register_server_callback_sem_);
  EXPECT_TRUE(status() == BT_STATUS_SUCCESS)
      << "Error registering GATT server app callback.";

  // Service UUID.
  bluetooth::Uuid srvc_uuid = bluetooth::Uuid::GetRandom();

  // Characteristics UUID.
  bluetooth::Uuid char_uuid = bluetooth::Uuid::GetRandom();

  // Descriptor UUID.
  bluetooth::Uuid desc_uuid = bluetooth::Uuid::GetRandom();

  // Adds service.
  int server_if = server_interface_id();

  std::vector<btgatt_db_element_t> service = {
      {
          .uuid = srvc_uuid,
          .type = BTGATT_DB_PRIMARY_SERVICE,
      },
      {
          .uuid = char_uuid,
          .type = BTGATT_DB_CHARACTERISTIC,
          .properties = 0x10,  /* notification */
          .permissions = 0x01, /* read only */
      },
      {
          .uuid = desc_uuid,
          .type = BTGATT_DB_DESCRIPTOR,
          .permissions = 0x01,
      }};

  gatt_server_interface()->add_service(server_if, service);
  semaphore_wait(service_added_callback_sem_);
  EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error adding service.";
  EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if added.";
  int service_handle_added = service_handle();

  // Stops server.
  gatt_server_interface()->stop_service(server_if, service_handle());
  semaphore_wait(service_stopped_callback_sem_);
  EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error stopping server.";
  EXPECT_TRUE(service_handle() == service_handle_added)
      << "Wrong service handle stopped.";
  EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if stopped.";

  // Deletes service.
  gatt_server_interface()->delete_service(server_if, service_handle());
  semaphore_wait(service_deleted_callback_sem_);
  EXPECT_TRUE(status() == BT_STATUS_SUCCESS) << "Error deleting service.";
  EXPECT_TRUE(service_handle() == service_handle_added)
      << "Wrong service handle deleted.";
  EXPECT_TRUE(server_interface_id() == server_if) << "Wrong server_if deleted.";

  // Unregisters gatt server. No callback is expected.
  gatt_server_interface()->unregister_server(server_if);
}

}  // bttest