Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 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 | |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 18 | #include <bluetooth/uuid.h> |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 19 | #include <array> |
| 20 | #include <memory> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "hardware/bluetooth.h" |
| 25 | #include "hardware/bt_gatt.h" |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 74 | bool Initialize(const Uuid& service_id, int* gatt_pipe); |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 75 | |
| 76 | // Control the content of service advertisement. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 77 | bool SetAdvertisement(const std::vector<Uuid>& ids, |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 83 | bool SetScanResponse(const std::vector<Uuid>& ids, |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 89 | bool AddCharacteristic(const Uuid& id, int properties, int permissions); |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 90 | |
| 91 | // Add a special 'blob' characteristic with a corresponding control |
| 92 | // attribute to manipulate which part of the blob the attribute represents. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 93 | bool AddBlob(const Uuid& id, const Uuid& control_id, int properties, |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 98 | bool SetCharacteristicValue(const Uuid& id, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 99 | const std::vector<uint8_t>& value); |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 100 | |
| 101 | // Get the current value of a characteristic. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 102 | bool GetCharacteristicValue(const Uuid& id, std::vector<uint8_t>* value); |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 103 | |
| 104 | // Start this service. Activate advertisements, allow connections. |
| 105 | // Characteristics should all be created before this. |
| 106 | bool Start(); |
| 107 | |
| 108 | // Cease advertisements and disallow connections. |
| 109 | bool Stop(); |
| 110 | |
| 111 | // Enable LE scan. Scan results will be cached internally. |
| 112 | bool ScanEnable(); |
| 113 | |
| 114 | // Disable LE scan. |
| 115 | bool ScanDisable(); |
| 116 | |
| 117 | // Copy out the cached scan results. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 118 | bool GetScanResults(ScanResults* results); |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | // Internal data. |
| 122 | std::unique_ptr<ServerInternals> internal_; |
| 123 | }; |
| 124 | |
| 125 | } // namespace gatt |
| 126 | } // namespace bluetooth |