heart_rate_server.h revision 514bf6087093375351784b287cb29c5f4603273c
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
17#pragma once
18
19#include <base/macros.h>
20
21#include "service/gatt_identifier.h"
22#include "service/ipc/binder/IBluetooth.h"
23#include "service/ipc/binder/IBluetoothGattServerCallback.h"
24
25namespace heart_rate {
26
27// Implements an example GATT Heart Rate service. This class emulates the
28// behavior of a heart rate service by sending fake heart-rate pulses.
29class HeartRateServer : public ipc::binder::BnBluetoothGattServerCallback {
30 public:
31  explicit HeartRateServer(android::sp<ipc::binder::IBluetooth> bluetooth);
32  ~HeartRateServer() override;
33
34  // Set up the server and register the GATT services with the stack. This
35  // initiates a set of asynchronous procedures. Invokes |callback|
36  // asynchronously with the result of the operation.
37  using RunCallback = std::function<void(bool success)>;
38  bool Run(const RunCallback& callback);
39
40 private:
41  // ipc::binder::IBluetoothGattServerCallback override:
42  void OnServerRegistered(int status, int server_if) override;
43  void OnServiceAdded(
44      int status,
45      const bluetooth::GattIdentifier& service_id) override;
46  void OnCharacteristicReadRequest(
47      const std::string& device_address,
48      int request_id, int offset, bool is_long,
49      const bluetooth::GattIdentifier& characteristic_id) override;
50  void OnDescriptorReadRequest(
51      const std::string& device_address,
52      int request_id, int offset, bool is_long,
53      const bluetooth::GattIdentifier& descriptor_id) override;
54
55  std::mutex mutex_;
56
57  android::sp<ipc::binder::IBluetooth> bluetooth_;
58  android::sp<ipc::binder::IBluetoothGattServer> gatt_;
59  int server_if_;
60  RunCallback pending_run_cb_;
61
62  bluetooth::GattIdentifier hr_service_id_;
63  bluetooth::GattIdentifier hr_measurement_id_;
64  bluetooth::GattIdentifier hr_measurement_cccd_id_;
65  bluetooth::GattIdentifier body_sensor_loc_id_;
66  bluetooth::GattIdentifier hr_control_point_id_;
67
68  DISALLOW_COPY_AND_ASSIGN(HeartRateServer);
69};
70
71}  // namespace heart_rate
72