blob: 81c8c315b0852c1b6e18ddb28e0deba0abef4e9e [file] [log] [blame]
Hansong Zhangcd0c3192019-09-20 15:50:42 -07001/*
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>
Hansong Zhangc83e1202019-10-16 13:31:52 -070021#include <vector>
Hansong Zhangcd0c3192019-09-20 15:50:42 -070022
23#include "l2cap/cid.h"
Hansong Zhangf0a16922019-11-22 21:27:08 -080024#include "l2cap/classic/internal/channel_configuration_state.h"
Hansong Zhangcd0c3192019-09-20 15:50:42 -070025#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"
Hansong Zhangf0a16922019-11-22 21:27:08 -080028#include "l2cap/internal/data_pipeline_manager.h"
Hansong Zhang47dec642019-11-25 12:01:15 -080029#include "l2cap/internal/dynamic_channel_allocator.h"
Hansong Zhangcd0c3192019-09-20 15:50:42 -070030#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
38namespace bluetooth {
39namespace l2cap {
40namespace classic {
41namespace internal {
42
43struct PendingCommand {
44 SignalId signal_id_;
45 CommandCode command_code_;
46 Psm psm_;
47 Cid source_cid_;
48 Cid destination_cid_;
49 InformationRequestInfoType info_type_;
Hansong Zhangc83e1202019-10-16 13:31:52 -070050 std::vector<std::unique_ptr<ConfigurationOption>> config_;
Hansong Zhangcd0c3192019-09-20 15:50:42 -070051};
52
53class Link;
54
55class ClassicSignallingManager {
56 public:
57 ClassicSignallingManager(os::Handler* handler, Link* link,
Hansong Zhangf0a16922019-11-22 21:27:08 -080058 l2cap::internal::DataPipelineManager* data_pipeline_manager,
Hansong Zhangcd0c3192019-09-20 15:50:42 -070059 classic::internal::DynamicChannelServiceManagerImpl* dynamic_service_manager,
Hansong Zhang47dec642019-11-25 12:01:15 -080060 l2cap::internal::DynamicChannelAllocator* channel_allocator,
Hansong Zhangcd0c3192019-09-20 15:50:42 -070061 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
Hansong Zhangc83e1202019-10-16 13:31:52 -070069 void SendConfigurationRequest(Cid remote_cid, std::vector<std::unique_ptr<ConfigurationOption>> config);
Hansong Zhangcd0c3192019-09-20 15:50:42 -070070
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 OnConnectionRequest(SignalId signal_id, Psm psm, Cid remote_cid);
78
Chris Manton976f62c2019-10-31 16:19:14 -070079 void OnConnectionResponse(SignalId signal_id, Cid remote_cid, Cid cid, ConnectionResponseResult result,
Hansong Zhangcd0c3192019-09-20 15:50:42 -070080 ConnectionResponseStatus status);
81
82 void OnDisconnectionRequest(SignalId signal_id, Cid cid, Cid remote_cid);
83
84 void OnDisconnectionResponse(SignalId signal_id, Cid cid, Cid remote_cid);
85
86 void OnConfigurationRequest(SignalId signal_id, Cid cid, Continuation is_continuation,
87 std::vector<std::unique_ptr<ConfigurationOption>>);
88
89 void OnConfigurationResponse(SignalId signal_id, Cid cid, Continuation is_continuation,
90 ConfigurationResponseResult result, std::vector<std::unique_ptr<ConfigurationOption>>);
91
92 void OnEchoRequest(SignalId signal_id, const PacketView<kLittleEndian>& packet);
93
94 void OnEchoResponse(SignalId signal_id, const PacketView<kLittleEndian>& packet);
95
96 void OnInformationRequest(SignalId signal_id, InformationRequestInfoType type);
97
Hansong Zhang70fb4642019-11-20 17:33:06 -080098 void OnInformationResponse(SignalId signal_id, const InformationResponseView& response);
Hansong Zhangcd0c3192019-09-20 15:50:42 -070099
100 private:
101 void on_incoming_packet();
102 void send_connection_response(SignalId signal_id, Cid remote_cid, Cid local_cid, ConnectionResponseResult result,
103 ConnectionResponseStatus status);
104 void on_command_timeout();
105 void handle_send_next_command();
106
107 os::Handler* handler_;
108 Link* link_;
Hansong Zhangf0a16922019-11-22 21:27:08 -0800109 [[maybe_unused]] l2cap::internal::DataPipelineManager* data_pipeline_manager_;
Hansong Zhangcd0c3192019-09-20 15:50:42 -0700110 std::shared_ptr<classic::internal::FixedChannelImpl> signalling_channel_;
111 DynamicChannelServiceManagerImpl* dynamic_service_manager_;
Hansong Zhang47dec642019-11-25 12:01:15 -0800112 l2cap::internal::DynamicChannelAllocator* channel_allocator_;
Hansong Zhangcd0c3192019-09-20 15:50:42 -0700113 FixedChannelServiceManagerImpl* fixed_service_manager_;
114 std::unique_ptr<os::EnqueueBuffer<packet::BasePacketBuilder>> enqueue_buffer_;
Hansong Zhangad57a6e2019-10-24 17:23:18 -0700115 PendingCommand last_sent_command_;
Hansong Zhangcd0c3192019-09-20 15:50:42 -0700116 std::queue<PendingCommand> pending_commands_;
117 os::Alarm alarm_;
Chris Manton7bad4d22019-10-30 11:14:09 -0700118 SignalId next_signal_id_ = kInitialSignalId;
Hansong Zhangf0a16922019-11-22 21:27:08 -0800119 std::unordered_map<Cid, ChannelConfigurationState> channel_configuration_;
Hansong Zhangcd0c3192019-09-20 15:50:42 -0700120};
121
122} // namespace internal
123} // namespace classic
124} // namespace l2cap
125} // namespace bluetooth