1 // 2 // Copyright (C) 2015 Google, Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 #pragma once 17 18 #include <array> 19 #include <memory> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "hardware/bluetooth.h" 24 #include "hardware/bt_gatt.h" 25 #include "service/common/bluetooth/uuid.h" 26 27 namespace bluetooth { 28 namespace gatt { 29 30 // Attribute permission values 31 const int kPermissionRead = 0x1; 32 const int kPermissionReadEncrypted = 0x2; 33 const int kPermissionReadEncryptedMitm = 0x4; 34 const int kPermissionWrite = 0x10; 35 const int kPermissionWriteEnecrypted = 0x20; 36 const int KPermissionWriteEncryptedMitm = 0x40; 37 const int kPermissionWriteSigned = 0x80; 38 const int kPermissionWriteSignedMitm = 0x100; 39 40 // GATT characteristic properties bit-field values 41 const int kPropertyBroadcast = 0x1; 42 const int kPropertyRead = 0x2; 43 const int kPropertyWriteNoResponse = 0x4; 44 const int kPropertyWrite = 0x8; 45 const int kPropertyNotify = 0x10; 46 const int kPropertyIndicate = 0x20; 47 const int kPropertySignedWrite = 0x40; 48 const int kPropertyExtendedProps = 0x80; 49 50 // A mapping from string bluetooth addresses to RSSI measurements. 51 typedef std::unordered_map<std::string, int> ScanResults; 52 53 // TODO(armansito): This should be a private internal class though I don't see 54 // why we even need this class. Instead it should probably be merged into 55 // Server. 56 struct ServerInternals; 57 58 // Server is threadsafe and internally locked. 59 // Asynchronous IO is identified via a gatt_pipe FD, 60 // and synchronously read with 'GetCharacteristicValue' 61 // 62 // ****DEPRECATED**** 63 // 64 // TODO(armansito): This class has been deprecated and is being replaced by 65 // bluetooth::GattServer. We will remove this entirely once the new code is 66 // ready. 67 class Server { 68 public: 69 Server(); 70 ~Server(); 71 72 // Register GATT interface, initialize internal state, 73 // and open a pipe for characteristic write notification. 74 bool Initialize(const UUID& service_id, int* gatt_pipe); 75 76 // Control the content of service advertisement. 77 bool SetAdvertisement(const std::vector<UUID>& ids, 78 const std::vector<uint8_t>& service_data, 79 const std::vector<uint8_t>& manufacturer_data, 80 bool transmit_name); 81 82 // Control the content of service scan response. 83 bool SetScanResponse(const std::vector<UUID>& ids, 84 const std::vector<uint8_t>& service_data, 85 const std::vector<uint8_t>& manufacturer_data, 86 bool transmit_name); 87 88 // Add an ordinary characteristic for reading and/or writing. 89 bool AddCharacteristic(const UUID &id, int properties, int permissions); 90 91 // Add a special 'blob' characteristic with a corresponding control 92 // attribute to manipulate which part of the blob the attribute represents. 93 bool AddBlob(const UUID &id, const UUID &control_id, int properties, 94 int permissions); 95 96 // Put a new value into a characeteristic. 97 // It will be read from a client starting at the next 0-offset read. 98 bool SetCharacteristicValue(const UUID &id, const std::vector<uint8_t> &value); 99 100 // Get the current value of a characteristic. 101 bool GetCharacteristicValue(const UUID &id, std::vector<uint8_t> *value); 102 103 // Start this service. Activate advertisements, allow connections. 104 // Characteristics should all be created before this. 105 bool Start(); 106 107 // Cease advertisements and disallow connections. 108 bool Stop(); 109 110 // Enable LE scan. Scan results will be cached internally. 111 bool ScanEnable(); 112 113 // Disable LE scan. 114 bool ScanDisable(); 115 116 // Copy out the cached scan results. 117 bool GetScanResults(ScanResults *results); 118 119 private: 120 // Internal data. 121 std::unique_ptr<ServerInternals> internal_; 122 }; 123 124 } // namespace gatt 125 } // namespace bluetooth 126