aboutsummaryrefslogtreecommitdiff
path: root/service/gatt_server_old.h
blob: 546f164f3a956cbdf521e506bf426e1f3cc37032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  Copyright 2015 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
#pragma once

#include <bluetooth/uuid.h>
#include <array>
#include <memory>
#include <unordered_map>
#include <vector>

#include "hardware/bluetooth.h"
#include "hardware/bt_gatt.h"

namespace bluetooth {
namespace gatt {

// Attribute permission values
const int kPermissionRead = 0x1;
const int kPermissionReadEncrypted = 0x2;
const int kPermissionReadEncryptedMitm = 0x4;
const int kPermissionWrite = 0x10;
const int kPermissionWriteEnecrypted = 0x20;
const int KPermissionWriteEncryptedMitm = 0x40;
const int kPermissionWriteSigned = 0x80;
const int kPermissionWriteSignedMitm = 0x100;

// GATT characteristic properties bit-field values
const int kPropertyBroadcast = 0x1;
const int kPropertyRead = 0x2;
const int kPropertyWriteNoResponse = 0x4;
const int kPropertyWrite = 0x8;
const int kPropertyNotify = 0x10;
const int kPropertyIndicate = 0x20;
const int kPropertySignedWrite = 0x40;
const int kPropertyExtendedProps = 0x80;

// A mapping from string bluetooth addresses to RSSI measurements.
typedef std::unordered_map<std::string, int> ScanResults;

// TODO(armansito): This should be a private internal class though I don't see
// why we even need this class. Instead it should probably be merged into
// Server.
struct ServerInternals;

// Server is threadsafe and internally locked.
// Asynchronous IO is identified via a gatt_pipe FD,
// and synchronously read with 'GetCharacteristicValue'
//
// ****DEPRECATED****
//
// TODO(armansito): This class has been deprecated and is being replaced by
// bluetooth::GattServer. We will remove this entirely once the new code is
// ready.
class Server {
 public:
  Server();
  ~Server();

  // Register GATT interface, initialize internal state,
  // and open a pipe for characteristic write notification.
  bool Initialize(const Uuid& service_id, int* gatt_pipe);

  // Control the content of service advertisement.
  bool SetAdvertisement(const std::vector<Uuid>& ids,
                        const std::vector<uint8_t>& service_data,
                        const std::vector<uint8_t>& manufacturer_data,
                        bool transmit_name);

  // Control the content of service scan response.
  bool SetScanResponse(const std::vector<Uuid>& ids,
                       const std::vector<uint8_t>& service_data,
                       const std::vector<uint8_t>& manufacturer_data,
                       bool transmit_name);

  // Add an ordinary characteristic for reading and/or writing.
  bool AddCharacteristic(const Uuid& id, int properties, int permissions);

  // Add a special 'blob' characteristic with a corresponding control
  // attribute to manipulate which part of the blob the attribute represents.
  bool AddBlob(const Uuid& id, const Uuid& control_id, int properties,
               int permissions);

  // Put a new value into a characeteristic.
  // It will be read from a client starting at the next 0-offset read.
  bool SetCharacteristicValue(const Uuid& id,
                              const std::vector<uint8_t>& value);

  // Get the current value of a characteristic.
  bool GetCharacteristicValue(const Uuid& id, std::vector<uint8_t>* value);

  // Start this service. Activate advertisements, allow connections.
  // Characteristics should all be created before this.
  bool Start();

  // Cease advertisements and disallow connections.
  bool Stop();

  // Enable LE scan. Scan results will be cached internally.
  bool ScanEnable();

  // Disable LE scan.
  bool ScanDisable();

  // Copy out the cached scan results.
  bool GetScanResults(ScanResults* results);

 private:
  // Internal data.
  std::unique_ptr<ServerInternals> internal_;
};

}  // namespace gatt
}  // namespace bluetooth