1 /*
2  * Copyright 2019 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 
17 #pragma once
18 
19 #include <cstdint>
20 #include <queue>
21 #include <vector>
22 
23 #include "l2cap/cid.h"
24 #include "l2cap/classic/internal/channel_configuration_state.h"
25 #include "l2cap/classic/internal/dynamic_channel_service_manager_impl.h"
26 #include "l2cap/classic/internal/fixed_channel_impl.h"
27 #include "l2cap/classic/internal/fixed_channel_service_manager_impl.h"
28 #include "l2cap/internal/data_pipeline_manager.h"
29 #include "l2cap/internal/dynamic_channel_allocator.h"
30 #include "l2cap/l2cap_packets.h"
31 #include "l2cap/psm.h"
32 #include "l2cap/signal_id.h"
33 #include "os/alarm.h"
34 #include "os/handler.h"
35 #include "os/queue.h"
36 #include "packet/raw_builder.h"
37 
38 namespace bluetooth {
39 namespace l2cap {
40 namespace classic {
41 namespace internal {
42 
43 struct PendingCommand {
44   SignalId signal_id_ = kInvalidSignalId;
45   CommandCode command_code_;
46   Psm psm_;
47   Cid source_cid_;
48   Cid destination_cid_;
49   InformationRequestInfoType info_type_;
50   std::vector<std::unique_ptr<ConfigurationOption>> config_;
51 };
52 
53 class Link;
54 
55 class ClassicSignallingManager {
56  public:
57   ClassicSignallingManager(os::Handler* handler, Link* link,
58                            l2cap::internal::DataPipelineManager* data_pipeline_manager,
59                            classic::internal::DynamicChannelServiceManagerImpl* dynamic_service_manager,
60                            l2cap::internal::DynamicChannelAllocator* channel_allocator,
61                            classic::internal::FixedChannelServiceManagerImpl* fixed_service_manager);
62 
63   virtual ~ClassicSignallingManager();
64 
65   void OnCommandReject(CommandRejectView command_reject_view);
66 
67   void SendConnectionRequest(Psm psm, Cid local_cid);
68 
69   void SendInitialConfigRequest(Cid local_cid);
70 
71   void SendDisconnectionRequest(Cid local_cid, Cid remote_cid);
72 
73   void SendInformationRequest(InformationRequestInfoType type);
74 
75   void SendEchoRequest(std::unique_ptr<packet::RawBuilder> payload);
76 
77   void CancelAlarm();
78 
79   void OnConnectionRequest(SignalId signal_id, Psm psm, Cid remote_cid);
80 
81   void OnConnectionResponse(SignalId signal_id, Cid remote_cid, Cid cid, ConnectionResponseResult result,
82                             ConnectionResponseStatus status);
83 
84   void OnDisconnectionRequest(SignalId signal_id, Cid cid, Cid remote_cid);
85 
86   void OnDisconnectionResponse(SignalId signal_id, Cid cid, Cid remote_cid);
87 
88   void OnConfigurationRequest(SignalId signal_id, Cid cid, Continuation is_continuation,
89                               std::vector<std::unique_ptr<ConfigurationOption>>);
90 
91   void OnConfigurationResponse(SignalId signal_id, Cid cid, Continuation is_continuation,
92                                ConfigurationResponseResult result, std::vector<std::unique_ptr<ConfigurationOption>>);
93 
94   void OnEchoRequest(SignalId signal_id, const PacketView<kLittleEndian>& packet);
95 
96   void OnEchoResponse(SignalId signal_id, const PacketView<kLittleEndian>& packet);
97 
98   void OnInformationRequest(SignalId signal_id, InformationRequestInfoType type);
99 
100   void OnInformationResponse(SignalId signal_id, const InformationResponseView& response);
101 
102  private:
103   void on_incoming_packet();
104   void handle_one_command(ControlView control_view);
105   void send_connection_response(SignalId signal_id, Cid remote_cid, Cid local_cid, ConnectionResponseResult result,
106                                 ConnectionResponseStatus status);
107   void on_command_timeout();
108   void handle_send_next_command();
109 
110   void negotiate_configuration(Cid cid, Continuation is_continuation,
111                                std::vector<std::unique_ptr<ConfigurationOption>>);
112 
113   void send_configuration_request(Cid remote_cid, std::vector<std::unique_ptr<ConfigurationOption>> config);
114   void on_security_result_for_incoming(Psm psm, Cid remote_cid, SignalId signal_id, bool result);
115   void on_security_result_for_outgoing(Psm psm, Cid local_cid, bool result);
116 
117   os::Handler* handler_;
118   Link* link_;
119   l2cap::internal::DataPipelineManager* data_pipeline_manager_;
120   std::shared_ptr<classic::internal::FixedChannelImpl> signalling_channel_;
121   DynamicChannelServiceManagerImpl* dynamic_service_manager_;
122   l2cap::internal::DynamicChannelAllocator* channel_allocator_;
123   FixedChannelServiceManagerImpl* fixed_service_manager_;
124   std::unique_ptr<os::EnqueueBuffer<packet::BasePacketBuilder>> enqueue_buffer_;
125   std::queue<PendingCommand> pending_commands_;
126   PendingCommand command_just_sent_;
127   os::Alarm alarm_;
128   SignalId next_signal_id_ = kInitialSignalId;
129   std::unordered_map<Cid, ChannelConfigurationState> channel_configuration_;
130 };
131 
132 }  // namespace internal
133 }  // namespace classic
134 }  // namespace l2cap
135 }  // namespace bluetooth
136