• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
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 
21 #include <list>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include "stack/include/bt_types.h" /* for Octet16 */
28 #include "types/bluetooth/uuid.h"
29 
30 namespace gatt {
31 constexpr uint16_t HANDLE_MIN = 0x0001;
32 constexpr uint16_t HANDLE_MAX = 0xffff;
33 
34 /* Representation of GATT attribute for storage */
35 struct StoredAttribute {
36   uint16_t handle;
37   bluetooth::Uuid type;
38 
39   union {
40     /* primary or secondary service definition */
41     struct {
42       bluetooth::Uuid uuid;
43       uint16_t end_handle;
44     } service;
45 
46     /* included service definition */
47     struct {
48       uint16_t handle;
49       uint16_t end_handle;
50       bluetooth::Uuid uuid;
51     } included_service;
52 
53     /* characteristic definition */
54     struct {
55       uint8_t properties;
56       uint16_t value_handle;
57       bluetooth::Uuid uuid;
58     } characteristic;
59 
60     /* for descriptor we store value only for
61      * «Characteristic Extended Properties» */
62     uint16_t characteristic_extended_properties;
63   } value;
64 };
65 
66 struct IncludedService;
67 struct Characteristic;
68 struct Descriptor;
69 
70 struct Service {
71   uint16_t handle;
72   bluetooth::Uuid uuid;
73   bool is_primary;
74   uint16_t end_handle;
75   std::vector<IncludedService> included_services;
76   std::vector<Characteristic> characteristics;
77 };
78 
79 struct IncludedService {
80   uint16_t handle; /* definition handle */
81   bluetooth::Uuid uuid;
82   uint16_t start_handle; /* start handle of included service */
83   uint16_t end_handle;   /* end handle of included service */
84 };
85 
86 struct Characteristic {
87   uint16_t declaration_handle;
88   bluetooth::Uuid uuid;
89   uint16_t value_handle;
90   uint8_t properties;
91   std::vector<Descriptor> descriptors;
92 };
93 
94 struct Descriptor {
95   uint16_t handle;
96   bluetooth::Uuid uuid;
97   /* set and used for «Characteristic Extended Properties» only */
98   uint16_t characteristic_extended_properties;
99 };
100 
101 class DatabaseBuilder;
102 
103 class Database {
104  public:
105   /* Return true if there are no services in this database. */
IsEmpty()106   bool IsEmpty() const { return services.empty(); }
107 
108   /* Clear the GATT database. This method forces relocation to ensure no extra
109    * space is used unnecesarly */
Clear()110   void Clear() { std::list<Service>().swap(services); }
111 
112   /* Return list of services available in this database */
Services()113   const std::list<Service>& Services() const { return services; }
114 
115   std::string ToString() const;
116 
117   std::vector<gatt::StoredAttribute> Serialize() const;
118 
119   static Database Deserialize(const std::vector<gatt::StoredAttribute>& nv_attr,
120                               bool* success);
121 
122   /* Return 128 bit unique identifier of this GATT database */
123   Octet16 Hash() const;
124 
125   friend class DatabaseBuilder;
126 
127  private:
128   std::list<Service> services;
129 };
130 
131 /* Find a service that should contain handle. Helper method for internal use
132  * inside gatt namespace.*/
133 Service* FindService(std::list<Service>& services, uint16_t handle);
134 
135 }  // namespace gatt
136