aboutsummaryrefslogtreecommitdiff
path: root/bta/gatt/database.h
blob: 53c03e5db22c51b769d0ef07c0e6ebb0c864051c (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/******************************************************************************
 *
 *  Copyright 2018 The Android Open Source Project
 *
 *  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.
 *
 ******************************************************************************/

#pragma once

#include <list>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "stack/include/bt_types.h" /* for Octet16 */
#include "types/bluetooth/uuid.h"

namespace gatt {
constexpr uint16_t HANDLE_MIN = 0x0001;
constexpr uint16_t HANDLE_MAX = 0xffff;

/* Representation of GATT attribute for storage */
struct StoredAttribute {
  uint16_t handle;
  bluetooth::Uuid type;

  union {
    /* primary or secondary service definition */
    struct {
      bluetooth::Uuid uuid;
      uint16_t end_handle;
    } service;

    /* included service definition */
    struct {
      uint16_t handle;
      uint16_t end_handle;
      bluetooth::Uuid uuid;
    } included_service;

    /* characteristic definition */
    struct {
      uint8_t properties;
      uint16_t value_handle;
      bluetooth::Uuid uuid;
    } characteristic;

    /* for descriptor we store value only for
     * «Characteristic Extended Properties» */
    uint16_t characteristic_extended_properties;
  } value;
};

struct IncludedService;
struct Characteristic;
struct Descriptor;

struct Service {
  uint16_t handle;
  bluetooth::Uuid uuid;
  bool is_primary;
  uint16_t end_handle;
  std::vector<IncludedService> included_services;
  std::vector<Characteristic> characteristics;
};

struct IncludedService {
  uint16_t handle; /* definition handle */
  bluetooth::Uuid uuid;
  uint16_t start_handle; /* start handle of included service */
  uint16_t end_handle;   /* end handle of included service */
};

struct Characteristic {
  uint16_t declaration_handle;
  bluetooth::Uuid uuid;
  uint16_t value_handle;
  uint8_t properties;
  std::vector<Descriptor> descriptors;
};

struct Descriptor {
  uint16_t handle;
  bluetooth::Uuid uuid;
  /* set and used for «Characteristic Extended Properties» only */
  uint16_t characteristic_extended_properties;
};

class DatabaseBuilder;

class Database {
 public:
  /* Return true if there are no services in this database. */
  bool IsEmpty() const { return services.empty(); }

  /* Clear the GATT database. This method forces relocation to ensure no extra
   * space is used unnecesarly */
  void Clear() { std::list<Service>().swap(services); }

  /* Return list of services available in this database */
  const std::list<Service>& Services() const { return services; }

  std::string ToString() const;

  std::vector<gatt::StoredAttribute> Serialize() const;

  static Database Deserialize(const std::vector<gatt::StoredAttribute>& nv_attr,
                              bool* success);

  /* Return 128 bit unique identifier of this GATT database */
  Octet16 Hash() const;

  friend class DatabaseBuilder;

 private:
  std::list<Service> services;
};

/* Find a service that should contain handle. Helper method for internal use
 * inside gatt namespace.*/
Service* FindService(std::list<Service>& services, uint16_t handle);

}  // namespace gatt