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 "types/bluetooth/uuid.h"
28 
29 namespace gatt {
30 constexpr uint16_t HANDLE_MIN = 0x0001;
31 constexpr uint16_t HANDLE_MAX = 0xffff;
32 
33 /* Representation of GATT attribute for storage */
34 struct 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 
63 struct IncludedService;
64 struct Characteristic;
65 struct Descriptor;
66 
67 struct 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 
76 struct 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 
83 struct 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 
91 struct Descriptor {
92   uint16_t handle;
93   bluetooth::Uuid uuid;
94 };
95 
96 class DatabaseBuilder;
97 
98 class Database {
99  public:
100   /* Return true if there are no services in this database. */
IsEmpty()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 */
Clear()105   void Clear() { std::list<Service>().swap(services); }
106 
107   /* Return list of services available in this database */
Services()108   const std::list<Service>& Services() const { return services; }
109 
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:
120   std::list<Service> services;
121 };
122 
123 /* Find a service that should contain handle. Helper method for internal use
124  * inside gatt namespace.*/
125 Service* FindService(std::list<Service>& services, uint16_t handle);
126 
127 }  // namespace gatt
128