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 <set>
22 #include <utility>
23 #include <vector>
24 
25 #include "bta/gatt/database.h"
26 #include "types/bluetooth/uuid.h"
27 
28 namespace gatt {
29 
30 class DatabaseBuilder {
31  public:
32   constexpr static std::pair<uint16_t, uint16_t> EXPLORE_END =
33       std::make_pair(0xFFFF, 0xFFFF);
34 
35   void AddService(uint16_t handle, uint16_t end_handle,
36                   const bluetooth::Uuid& uuid, bool is_primary);
37   void AddIncludedService(uint16_t handle, const bluetooth::Uuid& uuid,
38                           uint16_t start_handle, uint16_t end_handle);
39   void AddCharacteristic(uint16_t handle, uint16_t value_handle,
40                          const bluetooth::Uuid& uuid, uint8_t properties);
41   void AddDescriptor(uint16_t handle, const bluetooth::Uuid& uuid);
42 
43   /* Returns true if next service exploration started, false if there are no
44    * more services to explore. */
45   bool StartNextServiceExploration();
46 
47   /* Return pair with start and end handle of the currently explored service.
48    */
49   const std::pair<uint16_t, uint16_t>& CurrentlyExploredService();
50 
51   /* Return pair with start and end handle of the descriptor range to discover,
52    * or DatabaseBuilder::EXPLORE_END if no more descriptors left.
53    */
54   std::pair<uint16_t, uint16_t> NextDescriptorRangeToExplore();
55 
56   /* Return vector of "Characteristic Extended Properties" descriptors that must
57    * be read as part of service discovery process */
DescriptorHandlesToRead()58   std::vector<uint16_t> DescriptorHandlesToRead() {
59     return descriptor_handles_to_read;
60   }
61 
62   /* Assign value to descriptors from |DescriptorHandlesToRead()|. Values must
63    * be in same order. Returns |true| if all goes well, |false| if there is
64    * problem mapping values to descriptors. */
65   bool SetValueOfDescriptors(const std::vector<uint16_t>& values);
66 
67   /* Returns true, if GATT discovery is in progress, false if discovery was not
68    * started, or is already finished.
69    */
70   // TODO(jpawlowski): in the future, we might create this object only for the
71   // time of discovery, in such case InProgress won't be needed, because object
72   // existence will mean discovery is pending
73   bool InProgress() const;
74 
75   /* Call this method at end of GATT discovery, to obtain object representing
76    * the database of remote device */
77   Database Build();
78 
79   void Clear();
80 
81   /* Return text representation of internal state for debugging purposes */
82   std::string ToString() const;
83 
84  private:
85   Database database;
86   /* Start and end handle of service that is currently being discovered on the
87    * remote device */
88   std::pair<uint16_t, uint16_t> pending_service;
89   /* Characteristic inside pending_service that is currently being explored */
90   uint16_t pending_characteristic;
91 
92   /* sorted, unique set of start_handle, end_handle pair of all services that
93    * have not yet been discovered */
94   std::set<std::pair<uint16_t, uint16_t>> services_to_discover;
95 
96   /* handles of "Characteristic Extended Properties" descriptors that must be
97    * read as part of service discovery process */
98   std::vector<uint16_t> descriptor_handles_to_read;
99 };
100 
101 }  // namespace gatt
102