blob: d1ba836e57d8634bba80e063b68bb1593b1838a2 [file] [log] [blame]
Jakub Pawlowski3ee08ac2018-04-18 07:31:30 -07001/******************************************************************************
2 *
3 * Copyright 2018 The Android Open Source Project
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#pragma once
20
Myles Watson1bff6982019-03-22 16:51:39 -070021#include <list>
Jakub Pawlowski3ee08ac2018-04-18 07:31:30 -070022#include <set>
23#include <string>
24#include <utility>
25#include <vector>
26
27#include "types/bluetooth/uuid.h"
28
29namespace gatt {
30constexpr uint16_t HANDLE_MIN = 0x0001;
31constexpr uint16_t HANDLE_MAX = 0xffff;
32
33/* Representation of GATT attribute for storage */
34struct StoredAttribute {
35 uint16_t handle;
36 bluetooth::Uuid type;
37
38 union {
39 /* primary or secondary service definition */
40 struct {
41 bluetooth::Uuid uuid;
42 uint16_t end_handle;
43 } service;
44
45 /* included service definition */
46 struct {
47 uint16_t handle;
48 uint16_t end_handle;
49 bluetooth::Uuid uuid;
50 } included_service;
51
52 /* characteristic deifnition */
53 struct {
54 uint8_t properties;
55 uint16_t value_handle;
56 bluetooth::Uuid uuid;
57 } characteristic;
58
59 /* for descriptor definition we don't store value*/
60 } value;
61};
62
63struct IncludedService;
64struct Characteristic;
65struct Descriptor;
66
67struct Service {
68 uint16_t handle;
69 bluetooth::Uuid uuid;
70 bool is_primary;
71 uint16_t end_handle;
72 std::vector<IncludedService> included_services;
73 std::vector<Characteristic> characteristics;
74};
75
76struct IncludedService {
77 uint16_t handle; /* definition handle */
78 bluetooth::Uuid uuid;
79 uint16_t start_handle; /* start handle of included service */
80 uint16_t end_handle; /* end handle of included service */
81};
82
83struct Characteristic {
84 uint16_t declaration_handle;
85 bluetooth::Uuid uuid;
86 uint16_t value_handle;
87 uint8_t properties;
88 std::vector<Descriptor> descriptors;
89};
90
91struct Descriptor {
92 uint16_t handle;
93 bluetooth::Uuid uuid;
94};
95
96class DatabaseBuilder;
97
98class Database {
99 public:
100 /* Return true if there are no services in this database. */
101 bool IsEmpty() const { return services.empty(); }
102
103 /* Clear the GATT database. This method forces relocation to ensure no extra
104 * space is used unnecesarly */
Myles Watson1bff6982019-03-22 16:51:39 -0700105 void Clear() { std::list<Service>().swap(services); }
Jakub Pawlowski3ee08ac2018-04-18 07:31:30 -0700106
107 /* Return list of services available in this database */
Myles Watson1bff6982019-03-22 16:51:39 -0700108 const std::list<Service>& Services() const { return services; }
Jakub Pawlowski3ee08ac2018-04-18 07:31:30 -0700109
110 std::string ToString() const;
111
112 std::vector<gatt::StoredAttribute> Serialize() const;
113
114 static Database Deserialize(const std::vector<gatt::StoredAttribute>& nv_attr,
115 bool* success);
116
117 friend class DatabaseBuilder;
118
119 private:
Myles Watson1bff6982019-03-22 16:51:39 -0700120 std::list<Service> services;
Jakub Pawlowski3ee08ac2018-04-18 07:31:30 -0700121};
122
123/* Find a service that should contain handle. Helper method for internal use
124 * inside gatt namespace.*/
Myles Watson1bff6982019-03-22 16:51:39 -0700125Service* FindService(std::list<Service>& services, uint16_t handle);
Jakub Pawlowski3ee08ac2018-04-18 07:31:30 -0700126
127} // namespace gatt