1611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
2611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  Copyright (C) 2015 Google, Inc.
3611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
4611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  Licensed under the Apache License, Version 2.0 (the "License");
5611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  you may not use this file except in compliance with the License.
6611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  You may obtain a copy of the License at:
7611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
8611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  http://www.apache.org/licenses/LICENSE-2.0
9611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
10611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  Unless required by applicable law or agreed to in writing, software
11611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  distributed under the License is distributed on an "AS IS" BASIS,
12611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  See the License for the specific language governing permissions and
14611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//  limitations under the License.
15611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge//
16611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge#pragma once
17611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
18611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge#include <poll.h>
19611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
20611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge#include <memory>
21611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge#include <string>
22611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge#include <unordered_map>
23611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
24234138e2606dd7a54fbcc540643511abc0a3598dArman Uguray#include "service/common/bluetooth/uuid.h"
250f2d4897046f037a9f181f47f3d349a9dd646478Arman Uguray#include "service/gatt_server_old.h"
26611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
27611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidgenamespace bluetooth {
28d6a4b0c950f44d3eab34825880d26c19e362d22bArman Ugurayclass Adapter;
29fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray}  // namespace bluetooth
30fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray
31fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguraynamespace ipc {
32611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
33611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge// This implements a single threaded event loop which dispatches
34611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge// reads from a set of FDs (pfds_) to a set of handlers.
35611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge// Reads from the GATT pipe read end will result in a write to
36611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge// to the IPC socket, and vise versa.
37e415c050edbb2710e8807dd2602c851412953268Scott James Remnantclass LinuxIPCHost {
38611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge public:
39e415c050edbb2710e8807dd2602c851412953268Scott James Remnant  // LinuxIPCHost owns the passed sockfd.
40e415c050edbb2710e8807dd2602c851412953268Scott James Remnant  LinuxIPCHost(int sockfd, bluetooth::Adapter* adapter);
41e415c050edbb2710e8807dd2602c851412953268Scott James Remnant  ~LinuxIPCHost();
42611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
43611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Synchronously handle all events on input FDs.
44611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool EventLoop();
45611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
46611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge private:
47611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Handler for IPC message receives.
48611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Decodes protocol and dispatches to another handler.
49611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnMessage();
50611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
51611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Handler for GATT characteristic writes.
52611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Encodes to protocol and transmits IPC.
53611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnGattWrite();
54611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
55611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Applies adapter name changes to stack.
56611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnSetAdapterName(const std::string& name);
57611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
58611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Handles service creation.
59611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnCreateService(const std::string& service_uuid);
60611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
61611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Handles service destruction.
62611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnDestroyService(const std::string& service_uuid);
63611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
64611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Creates a characteristic for a service.
65611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnAddCharacteristic(const std::string& service_uuid,
66611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                           const std::string& characteristic_uuid,
67611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                           const std::string& control_uuid,
68611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                           const std::string& options);
69611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
70611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Sets the value of a characetistic.
71611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnSetCharacteristicValue(const std::string& service_uuid,
72611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                                const std::string& characteristic_uuid,
73611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                                const std::string& value);
74611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
75611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Applies settings to service advertisement.
76611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnSetAdvertisement(const std::string& service_uuid,
77611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                          const std::string& advertise_uuids,
78611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                          const std::string& advertise_data,
79aa9e17bc61324446b9e0199630fb772b9afb1109Ian Coolidge                          const std::string& manufacturer_data,
80611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                          const std::string& transmit_name);
81611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
82611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Applies settings to scan response.
83611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnSetScanResponse(const std::string& service_uuid,
84611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                         const std::string& advertise_uuids,
85611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                         const std::string& advertise_data,
86aa9e17bc61324446b9e0199630fb772b9afb1109Ian Coolidge                         const std::string& manufacturer_data,
87611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge                         const std::string& transmit_name);
88611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
89611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Starts service (advertisement and connections)
90611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnStartService(const std::string& service_uuid);
91611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
92611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Stops service.
93611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  bool OnStopService(const std::string& service_uuid);
94611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
95611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // weak reference.
96d6a4b0c950f44d3eab34825880d26c19e362d22bArman Uguray  bluetooth::Adapter *adapter_;
97611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
98611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // File descripters that we will block against.
99611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  std::vector<struct pollfd> pfds_;
100611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
101611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // Container for multiple GATT servers. Currently only one is supported.
102611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge  // TODO(icoolidge): support many to one for real.
103fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray  std::unordered_map<std::string, std::unique_ptr<bluetooth::gatt::Server>>
104fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray      gatt_servers_;
105611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge};
106611fcf98316e28425abe28dbcc07b8d037653ceeIan Coolidge
107fe65fb7978bc9257a36d1e5eae59c5f412dbdb49Arman Uguray}  // namespace ipc
108