summaryrefslogtreecommitdiff
path: root/service/ipc/linux_ipc_host.h
blob: 1ff48820cdd1247b149321764326cdae9f9591aa (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
//
//  Copyright (C) 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 <poll.h>

#include <bluetooth/uuid.h>
#include <memory>
#include <string>
#include <unordered_map>

#include "service/gatt_server_old.h"

namespace bluetooth {
class Adapter;
}  // namespace bluetooth

namespace ipc {

// This implements a single threaded event loop which dispatches
// reads from a set of FDs (pfds_) to a set of handlers.
// Reads from the GATT pipe read end will result in a write to
// to the IPC socket, and vise versa.
class LinuxIPCHost {
 public:
  // LinuxIPCHost owns the passed sockfd.
  LinuxIPCHost(int sockfd, bluetooth::Adapter* adapter);
  ~LinuxIPCHost();

  // Synchronously handle all events on input FDs.
  bool EventLoop();

 private:
  // Handler for IPC message receives.
  // Decodes protocol and dispatches to another handler.
  bool OnMessage();

  // Handler for GATT characteristic writes.
  // Encodes to protocol and transmits IPC.
  bool OnGattWrite();

  // Applies adapter name changes to stack.
  bool OnSetAdapterName(const std::string& name);

  // Handles service creation.
  bool OnCreateService(const std::string& service_uuid);

  // Handles service destruction.
  bool OnDestroyService(const std::string& service_uuid);

  // Creates a characteristic for a service.
  bool OnAddCharacteristic(const std::string& service_uuid,
                           const std::string& characteristic_uuid,
                           const std::string& control_uuid,
                           const std::string& options);

  // Sets the value of a characetistic.
  bool OnSetCharacteristicValue(const std::string& service_uuid,
                                const std::string& characteristic_uuid,
                                const std::string& value);

  // Applies settings to service advertisement.
  bool OnSetAdvertisement(const std::string& service_uuid,
                          const std::string& advertise_uuids,
                          const std::string& advertise_data,
                          const std::string& manufacturer_data,
                          const std::string& transmit_name);

  // Applies settings to scan response.
  bool OnSetScanResponse(const std::string& service_uuid,
                         const std::string& advertise_uuids,
                         const std::string& advertise_data,
                         const std::string& manufacturer_data,
                         const std::string& transmit_name);

  // Starts service (advertisement and connections)
  bool OnStartService(const std::string& service_uuid);

  // Stops service.
  bool OnStopService(const std::string& service_uuid);

  // weak reference.
  bluetooth::Adapter* adapter_;

  // File descripters that we will block against.
  std::vector<struct pollfd> pfds_;

  // Container for multiple GATT servers. Currently only one is supported.
  // TODO(icoolidge): support many to one for real.
  std::unordered_map<std::string, std::unique_ptr<bluetooth::gatt::Server>>
      gatt_servers_;
};

}  // namespace ipc