2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <unordered_map>
23 #include "hardware/bluetooth.h"
24 #include "hardware/bt_gatt.h"
25 #include "service/common/bluetooth/uuid.h"
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;
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;
50 // A mapping from string bluetooth addresses to RSSI measurements.
51 typedef std::unordered_map<std::string, int> ScanResults;
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
56 struct ServerInternals;
58 // Server is threadsafe and internally locked.
59 // Asynchronous IO is identified via a gatt_pipe FD,
60 // and synchronously read with 'GetCharacteristicValue'
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
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);
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,
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,
88 // Add an ordinary characteristic for reading and/or writing.
89 bool AddCharacteristic(const UUID &id, int properties, int permissions);
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,
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);
100 // Get the current value of a characteristic.
101 bool GetCharacteristicValue(const UUID &id, std::vector<uint8_t> *value);
103 // Start this service. Activate advertisements, allow connections.
104 // Characteristics should all be created before this.
107 // Cease advertisements and disallow connections.
110 // Enable LE scan. Scan results will be cached internally.
116 // Copy out the cached scan results.
117 bool GetScanResults(ScanResults *results);
121 std::unique_ptr<ServerInternals> internal_;
125 } // namespace bluetooth