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