1 /*
2  * Copyright 2020 The Android Open Source Project
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 // Authors: corbin.souffrant@leviathansecurity.com
17 //          dylan.katz@leviathansecurity.com
18 
19 #pragma once
20 
21 #include <gd/l2cap/classic/internal/dynamic_channel_service_manager_impl.h>
22 #include <future>
23 #include <memory>
24 
25 #include "hci/address.h"
26 #include "l2cap/classic/dynamic_channel_configuration_option.h"
27 #include "l2cap/classic/dynamic_channel_manager.h"
28 #include "l2cap/classic/security_policy.h"
29 #include "l2cap/psm.h"
30 #include "os/handler.h"
31 
32 namespace bluetooth {
33 namespace shim {
34 namespace {
35 class FuzzDynamicChannelManagerImpl {
36  public:
ConnectChannel(hci::Address device,l2cap::classic::DynamicChannelConfigurationOption configuration_option,l2cap::Psm,l2cap::classic::DynamicChannelManager::OnConnectionOpenCallback on_open_callback,l2cap::classic::DynamicChannelManager::OnConnectionFailureCallback on_fail_callback)37   void ConnectChannel(
38       hci::Address device,
39       l2cap::classic::DynamicChannelConfigurationOption configuration_option,
40       l2cap::Psm,
41       l2cap::classic::DynamicChannelManager::OnConnectionOpenCallback on_open_callback,
42       l2cap::classic::DynamicChannelManager::OnConnectionFailureCallback on_fail_callback) {
43     connections_++;
44     on_open_callback_ = std::move(on_open_callback);
45     on_fail_callback_ = std::move(on_fail_callback);
46 
47     connected_promise_.set_value();
48   }
49   int connections_{0};
50 
RegisterService(l2cap::Psm,l2cap::classic::DynamicChannelConfigurationOption,const l2cap::classic::SecurityPolicy &,l2cap::classic::DynamicChannelManager::OnRegistrationCompleteCallback on_registration_complete,l2cap::classic::DynamicChannelManager::OnConnectionOpenCallback on_open_callback)51   void RegisterService(
52       l2cap::Psm,
53       l2cap::classic::DynamicChannelConfigurationOption,
54       const l2cap::classic::SecurityPolicy&,
55       l2cap::classic::DynamicChannelManager::OnRegistrationCompleteCallback on_registration_complete,
56       l2cap::classic::DynamicChannelManager::OnConnectionOpenCallback on_open_callback) {
57     services_++;
58     on_registration_complete_ = std::move(on_registration_complete);
59     on_open_callback_ = std::move(on_open_callback);
60 
61     register_promise_.set_value();
62   }
63   int services_{0};
64 
SetConnectionFuture()65   void SetConnectionFuture() {
66     connected_promise_ = std::promise<void>();
67   }
68 
WaitConnectionFuture()69   void WaitConnectionFuture() {
70     connected_future_ = connected_promise_.get_future();
71     connected_future_.wait();
72   }
73 
SetRegistrationFuture()74   void SetRegistrationFuture() {
75     register_promise_ = std::promise<void>();
76   }
77 
WaitRegistrationFuture()78   void WaitRegistrationFuture() {
79     register_future_ = register_promise_.get_future();
80     register_future_.wait();
81   }
82 
SetConnectionOnFail(l2cap::classic::DynamicChannelManager::ConnectionResult result,std::promise<void> promise)83   void SetConnectionOnFail(l2cap::classic::DynamicChannelManager::ConnectionResult result, std::promise<void> promise) {
84     std::move(on_fail_callback_).Invoke(result);
85     promise.set_value();
86   }
87 
SetConnectionOnOpen(std::unique_ptr<l2cap::DynamicChannel> channel,std::promise<void> promise)88   void SetConnectionOnOpen(std::unique_ptr<l2cap::DynamicChannel> channel, std::promise<void> promise) {
89     std::move(on_open_callback_).Invoke(std::move(channel));
90     promise.set_value();
91   }
92 
93   l2cap::classic::DynamicChannelManager::OnRegistrationCompleteCallback on_registration_complete_{};
94   l2cap::classic::DynamicChannelManager::OnConnectionOpenCallback on_open_callback_{};
95   l2cap::classic::DynamicChannelManager::OnConnectionFailureCallback on_fail_callback_{};
96 
97   FuzzDynamicChannelManagerImpl() = default;
98   ~FuzzDynamicChannelManagerImpl() = default;
99 
100  private:
101   std::promise<void> connected_promise_;
102   std::future<void> connected_future_;
103 
104   std::promise<void> register_promise_;
105   std::future<void> register_future_;
106 };
107 }  // namespace
108 }  // namespace shim
109 }  // namespace bluetooth
110