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