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 "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 /* Returns true, if GATT discovery is in progress, false if discovery was not 54 * started, or is already finished. 55 */ 56 // TODO(jpawlowski): in the future, we might create this object only for the 57 // time of discovery, in such case InProgress won't be needed, because object 58 // existence will mean discovery is pending 59 bool InProgress() const; 60 61 /* Call this method at end of GATT discovery, to obtain object representing 62 * the database of remote device */ 63 Database Build(); 64 65 void Clear(); 66 67 /* Return text representation of internal state for debugging purposes */ 68 std::string ToString() const; 69 70 private: 71 Database database; 72 /* Start and end handle of service that is currently being discovered on the 73 * remote device */ 74 std::pair<uint16_t, uint16_t> pending_service; 75 /* Characteristic inside pending_service that is currently being explored */ 76 uint16_t pending_characteristic; 77 78 /* sorted, unique set of start_handle, end_handle pair of all services that 79 * have not yet been discovered */ 80 std::set<std::pair<uint16_t, uint16_t>> services_to_discover; 81 }; 82 83 } // namespace gatt 84