1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CORE_INTERNAL_PCP_MANAGER_H_
16 #define CORE_INTERNAL_PCP_MANAGER_H_
17 
18 #include <string>
19 
20 #include "core/internal/base_pcp_handler.h"
21 #include "core/internal/bwu_manager.h"
22 #include "core/internal/client_proxy.h"
23 #include "core/internal/endpoint_channel_manager.h"
24 #include "core/internal/endpoint_manager.h"
25 #include "core/internal/injected_bluetooth_device_store.h"
26 #include "core/internal/mediums/mediums.h"
27 #include "core/listeners.h"
28 #include "core/options.h"
29 #include "core/status.h"
30 #include "core/strategy.h"
31 #include "platform/public/atomic_boolean.h"
32 #include "absl/container/flat_hash_map.h"
33 
34 namespace location {
35 namespace nearby {
36 namespace connections {
37 
38 // Manages all known PcpHandler implementations, delegating operations to the
39 // appropriate one as per the parameters passed in.
40 //
41 // This will only ever be used by the OfflineServiceController, which has all
42 // of its entrypoints invoked serially, so there's no synchronization needed.
43 // Public method semantics matches definition in the
44 // cpp/core/internal/service_controller.h
45 class PcpManager {
46  public:
47   PcpManager(Mediums& mediums, EndpointChannelManager& channel_manager,
48              EndpointManager& endpoint_manager, BwuManager& bwu_manager,
49              InjectedBluetoothDeviceStore& injected_bluetooth_device_store);
50   ~PcpManager();
51 
52   Status StartAdvertising(ClientProxy* client, const string& service_id,
53                           const ConnectionOptions& options,
54                           const ConnectionRequestInfo& info);
55   void StopAdvertising(ClientProxy* client);
56 
57   Status StartDiscovery(ClientProxy* client, const string& service_id,
58                         const ConnectionOptions& options,
59                         DiscoveryListener listener);
60   void StopDiscovery(ClientProxy* client);
61 
62   void InjectEndpoint(ClientProxy* client,
63                       const std::string& service_id,
64                       const OutOfBandConnectionMetadata& metadata);
65 
66   Status RequestConnection(ClientProxy* client, const string& endpoint_id,
67                            const ConnectionRequestInfo& info,
68                            const ConnectionOptions& options);
69   Status AcceptConnection(ClientProxy* client, const string& endpoint_id,
70                           const PayloadListener& payload_listener);
71   Status RejectConnection(ClientProxy* client, const string& endpoint_id);
72 
73   proto::connections::Medium GetBandwidthUpgradeMedium();
74   void DisconnectFromEndpointManager();
75 
76  private:
77   bool SetCurrentPcpHandler(Strategy strategy);
78   PcpHandler* GetPcpHandler(Pcp pcp) const;
79 
80   AtomicBoolean shutdown_{false};
81   absl::flat_hash_map<Pcp, std::unique_ptr<BasePcpHandler>> handlers_;
82   PcpHandler* current_ = nullptr;
83 };
84 
85 }  // namespace connections
86 }  // namespace nearby
87 }  // namespace location
88 
89 #endif  // CORE_INTERNAL_PCP_MANAGER_H_
90