aboutsummaryrefslogtreecommitdiff
path: root/bta/gatt/database_builder.h
blob: d2cc72054fd1c29254cf46ede64af79b5757b3cf (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
/******************************************************************************
 *
 *  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 "gatt/database.h"

#include <utility>

namespace gatt {

class DatabaseBuilder {
 public:
  constexpr static std::pair<uint16_t, uint16_t> EXPLORE_END =
      std::make_pair(0xFFFF, 0xFFFF);

  void AddService(uint16_t handle, uint16_t end_handle,
                  const bluetooth::Uuid& uuid, bool is_primary);
  void AddIncludedService(uint16_t handle, const bluetooth::Uuid& uuid,
                          uint16_t start_handle, uint16_t end_handle);
  void AddCharacteristic(uint16_t handle, uint16_t value_handle,
                         const bluetooth::Uuid& uuid, uint8_t properties);
  void AddDescriptor(uint16_t handle, const bluetooth::Uuid& uuid);

  /* Returns true if next service exploration started, false if there are no
   * more services to explore. */
  bool StartNextServiceExploration();

  /* Return pair with start and end handle of the currently explored service.
   */
  const std::pair<uint16_t, uint16_t>& CurrentlyExploredService();

  /* Return pair with start and end handle of the descriptor range to discover,
   * or DatabaseBuilder::EXPLORE_END if no more descriptors left.
   */
  std::pair<uint16_t, uint16_t> NextDescriptorRangeToExplore();

  /* Returns true, if GATT discovery is in progress, false if discovery was not
   * started, or is already finished.
   */
  // TODO(jpawlowski): in the future, we might create this object only for the
  // time of discovery, in such case InProgress won't be needed, because object
  // existence will mean discovery is pending
  bool InProgress() const;

  /* Call this method at end of GATT discovery, to obtain object representing
   * the database of remote device */
  Database Build();

  void Clear();

  /* Return text representation of internal state for debugging purposes */
  std::string ToString() const;

 private:
  Database database;
  /* Start and end handle of service that is currently being discovered on the
   * remote device */
  std::pair<uint16_t, uint16_t> pending_service;
  /* Characteristic inside pending_service that is currently being explored */
  uint16_t pending_characteristic;

  /* sorted, unique set of start_handle, end_handle pair of all services that
   * have not yet been discovered */
  std::set<std::pair<uint16_t, uint16_t>> services_to_discover;
};

}  // namespace gatt