OSDN Git Service

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