blob: 546f164f3a956cbdf521e506bf426e1f3cc37032 [file] [log] [blame]
Arman Uguray0f2d4892015-09-22 14:20:42 -07001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Arman Uguray0f2d4892015-09-22 14:20:42 -07003//
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 Pawlowski819e2ec2017-07-10 09:56:09 -070018#include <bluetooth/uuid.h>
Arman Uguray0f2d4892015-09-22 14:20:42 -070019#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 Uguray0f2d4892015-09-22 14:20:42 -070026
27namespace bluetooth {
28namespace gatt {
29
30// Attribute permission values
31const int kPermissionRead = 0x1;
32const int kPermissionReadEncrypted = 0x2;
33const int kPermissionReadEncryptedMitm = 0x4;
34const int kPermissionWrite = 0x10;
35const int kPermissionWriteEnecrypted = 0x20;
36const int KPermissionWriteEncryptedMitm = 0x40;
37const int kPermissionWriteSigned = 0x80;
38const int kPermissionWriteSignedMitm = 0x100;
39
40// GATT characteristic properties bit-field values
41const int kPropertyBroadcast = 0x1;
42const int kPropertyRead = 0x2;
43const int kPropertyWriteNoResponse = 0x4;
44const int kPropertyWrite = 0x8;
45const int kPropertyNotify = 0x10;
46const int kPropertyIndicate = 0x20;
47const int kPropertySignedWrite = 0x40;
48const int kPropertyExtendedProps = 0x80;
49
50// A mapping from string bluetooth addresses to RSSI measurements.
51typedef 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.
56struct 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.
67class 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 Pawlowski819e2ec2017-07-10 09:56:09 -070074 bool Initialize(const Uuid& service_id, int* gatt_pipe);
Arman Uguray0f2d4892015-09-22 14:20:42 -070075
76 // Control the content of service advertisement.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070077 bool SetAdvertisement(const std::vector<Uuid>& ids,
Arman Uguray0f2d4892015-09-22 14:20:42 -070078 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 Pawlowski819e2ec2017-07-10 09:56:09 -070083 bool SetScanResponse(const std::vector<Uuid>& ids,
Arman Uguray0f2d4892015-09-22 14:20:42 -070084 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 Pawlowski819e2ec2017-07-10 09:56:09 -070089 bool AddCharacteristic(const Uuid& id, int properties, int permissions);
Arman Uguray0f2d4892015-09-22 14:20:42 -070090
91 // Add a special 'blob' characteristic with a corresponding control
92 // attribute to manipulate which part of the blob the attribute represents.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070093 bool AddBlob(const Uuid& id, const Uuid& control_id, int properties,
Arman Uguray0f2d4892015-09-22 14:20:42 -070094 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 Pawlowski819e2ec2017-07-10 09:56:09 -070098 bool SetCharacteristicValue(const Uuid& id,
Myles Watson911d1ae2016-11-28 16:44:40 -080099 const std::vector<uint8_t>& value);
Arman Uguray0f2d4892015-09-22 14:20:42 -0700100
101 // Get the current value of a characteristic.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700102 bool GetCharacteristicValue(const Uuid& id, std::vector<uint8_t>* value);
Arman Uguray0f2d4892015-09-22 14:20:42 -0700103
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 Watson911d1ae2016-11-28 16:44:40 -0800118 bool GetScanResults(ScanResults* results);
Arman Uguray0f2d4892015-09-22 14:20:42 -0700119
120 private:
121 // Internal data.
122 std::unique_ptr<ServerInternals> internal_;
123};
124
125} // namespace gatt
126} // namespace bluetooth