• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 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 #pragma once
17 
18 #include <poll.h>
19 
20 #include <bluetooth/uuid.h>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 
25 #include "service/gatt_server_old.h"
26 
27 namespace bluetooth {
28 class Adapter;
29 }  // namespace bluetooth
30 
31 namespace ipc {
32 
33 // This implements a single threaded event loop which dispatches
34 // reads from a set of FDs (pfds_) to a set of handlers.
35 // Reads from the GATT pipe read end will result in a write to
36 // to the IPC socket, and vise versa.
37 class LinuxIPCHost {
38  public:
39   // LinuxIPCHost owns the passed sockfd.
40   LinuxIPCHost(int sockfd, bluetooth::Adapter* adapter);
41   ~LinuxIPCHost();
42 
43   // Synchronously handle all events on input FDs.
44   bool EventLoop();
45 
46  private:
47   // Handler for IPC message receives.
48   // Decodes protocol and dispatches to another handler.
49   bool OnMessage();
50 
51   // Handler for GATT characteristic writes.
52   // Encodes to protocol and transmits IPC.
53   bool OnGattWrite();
54 
55   // Applies adapter name changes to stack.
56   bool OnSetAdapterName(const std::string& name);
57 
58   // Handles service creation.
59   bool OnCreateService(const std::string& service_uuid);
60 
61   // Handles service destruction.
62   bool OnDestroyService(const std::string& service_uuid);
63 
64   // Creates a characteristic for a service.
65   bool OnAddCharacteristic(const std::string& service_uuid,
66                            const std::string& characteristic_uuid,
67                            const std::string& control_uuid,
68                            const std::string& options);
69 
70   // Sets the value of a characetistic.
71   bool OnSetCharacteristicValue(const std::string& service_uuid,
72                                 const std::string& characteristic_uuid,
73                                 const std::string& value);
74 
75   // Applies settings to service advertisement.
76   bool OnSetAdvertisement(const std::string& service_uuid,
77                           const std::string& advertise_uuids,
78                           const std::string& advertise_data,
79                           const std::string& manufacturer_data,
80                           const std::string& transmit_name);
81 
82   // Applies settings to scan response.
83   bool OnSetScanResponse(const std::string& service_uuid,
84                          const std::string& advertise_uuids,
85                          const std::string& advertise_data,
86                          const std::string& manufacturer_data,
87                          const std::string& transmit_name);
88 
89   // Starts service (advertisement and connections)
90   bool OnStartService(const std::string& service_uuid);
91 
92   // Stops service.
93   bool OnStopService(const std::string& service_uuid);
94 
95   // weak reference.
96   bluetooth::Adapter* adapter_;
97 
98   // File descripters that we will block against.
99   std::vector<struct pollfd> pfds_;
100 
101   // Container for multiple GATT servers. Currently only one is supported.
102   // TODO(icoolidge): support many to one for real.
103   std::unordered_map<std::string, std::unique_ptr<bluetooth::gatt::Server>>
104       gatt_servers_;
105 };
106 
107 }  // namespace ipc
108