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