• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/internal/data_pipeline_manager.h"
25 #include "l2cap/internal/dynamic_channel_allocator.h"
26 #include "l2cap/l2cap_packets.h"
27 #include "l2cap/le/internal/dynamic_channel_service_manager_impl.h"
28 #include "l2cap/le/internal/fixed_channel_impl.h"
29 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h"
30 #include "l2cap/mtu.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 le {
41 namespace internal {
42 
43 struct PendingCommand {
44   SignalId signal_id_ = kInvalidSignalId;
45   LeCommandCode command_code_;
46   Psm psm_;
47   Cid source_cid_;
48   Cid destination_cid_;
49   Mtu mtu_;
50   uint16_t mps_;
51   uint16_t credits_;
52   uint16_t interval_min_;
53   uint16_t interval_max_;
54   uint16_t peripheral_latency_;
55   uint16_t timeout_multiplier_;
56 
CreditBasedConnectionRequestPendingCommand57   static PendingCommand CreditBasedConnectionRequest(SignalId signal_id, Psm psm, Cid scid, Mtu mtu, uint16_t mps,
58                                                      uint16_t initial_credits) {
59     PendingCommand pending_command;
60     pending_command.signal_id_ = signal_id;
61     pending_command.command_code_ = LeCommandCode::LE_CREDIT_BASED_CONNECTION_REQUEST;
62     pending_command.psm_ = psm;
63     pending_command.source_cid_ = scid;
64     pending_command.mtu_ = mtu;
65     pending_command.mps_ = mps;
66     pending_command.credits_ = initial_credits;
67     return pending_command;
68   }
69 
DisconnectionRequestPendingCommand70   static PendingCommand DisconnectionRequest(SignalId signal_id, Cid scid, Cid dcid) {
71     PendingCommand pending_command;
72     pending_command.signal_id_ = signal_id;
73     pending_command.command_code_ = LeCommandCode::DISCONNECTION_REQUEST;
74     pending_command.source_cid_ = scid;
75     pending_command.destination_cid_ = dcid;
76     return pending_command;
77   }
78 
ConnectionParameterUpdatePendingCommand79   static PendingCommand ConnectionParameterUpdate(
80       SignalId signal_id,
81       uint16_t interval_min,
82       uint16_t interval_max,
83       uint16_t peripheral_latency,
84       uint16_t timeout_multiplier) {
85     PendingCommand pending_command;
86     pending_command.signal_id_ = signal_id;
87     pending_command.command_code_ = LeCommandCode::CONNECTION_PARAMETER_UPDATE_REQUEST;
88     pending_command.interval_min_ = interval_min;
89     pending_command.interval_max_ = interval_max;
90     pending_command.peripheral_latency_ = peripheral_latency;
91     pending_command.timeout_multiplier_ = timeout_multiplier;
92     return pending_command;
93   }
94 };
95 
96 class Link;
97 
98 class LeSignallingManager {
99  public:
100   LeSignallingManager(os::Handler* handler, Link* link, l2cap::internal::DataPipelineManager* data_pipeline_manager,
101                       DynamicChannelServiceManagerImpl* dynamic_service_manager,
102                       l2cap::internal::DynamicChannelAllocator* channel_allocator);
103 
104   virtual ~LeSignallingManager();
105 
106   void SendConnectionRequest(Psm psm, Cid local_cid, Mtu mtu);
107 
108   void SendDisconnectRequest(Cid local_cid, Cid remote_cid);
109 
110   // Note: Since Core 4.1, LL peripheral can send this through HCI command.
111   void SendConnectionParameterUpdateRequest(
112       uint16_t interval_min, uint16_t interval_max, uint16_t peripheral_latency, uint16_t timeout_multiplier);
113 
114   void SendConnectionParameterUpdateResponse(SignalId signal_id, ConnectionParameterUpdateResponseResult result);
115 
116   void SendCredit(Cid local_cid, uint16_t credits);
117 
118   void SendEnhancedConnectionRequest(Psm psm, std::vector<Cid> local_cid, Mtu mtu);
119 
120   void SendEnhancedReconfigureRequest(std::vector<Cid> local_cid, Mtu mtu);
121 
122   void CancelAlarm();
123 
124   void OnCommandReject(LeCommandRejectView command_reject_view);
125 
126   void OnConnectionParameterUpdateRequest(
127       SignalId signal_id,
128       uint16_t interval_min,
129       uint16_t interval_max,
130       uint16_t peripheral_latency,
131       uint16_t timeout_multiplier);
132   void OnConnectionParameterUpdateResponse(SignalId signal_id, ConnectionParameterUpdateResponseResult result);
133 
134   void OnConnectionRequest(SignalId signal_id, Psm psm, Cid remote_cid, Mtu mtu, uint16_t mps,
135                            uint16_t initial_credits);
136 
137   void OnConnectionResponse(SignalId signal_id, Cid remote_cid, Mtu mtu, uint16_t mps, uint16_t initial_credits,
138                             LeCreditBasedConnectionResponseResult result);
139 
140   void OnDisconnectionRequest(SignalId signal_id, Cid cid, Cid remote_cid);
141 
142   void OnDisconnectionResponse(SignalId signal_id, Cid cid, Cid remote_cid);
143 
144   void OnCredit(Cid remote_cid, uint16_t credits);
145 
146  private:
147   struct PendingConnection {
148     Cid remote_cid;
149     Mtu mtu;
150     uint16_t max_pdu_size;
151     uint16_t initial_credits;
152     SignalId incoming_signal_id;
153   };
154 
155   void on_incoming_packet();
156   void send_connection_response(SignalId signal_id, Cid local_cid, Mtu mtu, uint16_t mps, uint16_t initial_credit,
157                                 LeCreditBasedConnectionResponseResult result);
158   void on_command_timeout();
159   void handle_send_next_command();
160   void on_security_result_for_incoming(Psm psm, PendingConnection request, bool result);
161   void on_security_result_for_outgoing(Psm psm, Cid local_cid, Mtu mtu, bool result);
162 
163   os::Handler* handler_;
164   Link* link_;
165   l2cap::internal::DataPipelineManager* data_pipeline_manager_;
166   std::shared_ptr<le::internal::FixedChannelImpl> signalling_channel_;
167   DynamicChannelServiceManagerImpl* dynamic_service_manager_;
168   l2cap::internal::DynamicChannelAllocator* channel_allocator_;
169   std::unique_ptr<os::EnqueueBuffer<packet::BasePacketBuilder>> enqueue_buffer_;
170   std::queue<PendingCommand> pending_commands_;
171   PendingCommand command_just_sent_;
172   os::Alarm alarm_;
173   SignalId next_signal_id_ = kInitialSignalId;
174 };
175 
176 }  // namespace internal
177 }  // namespace le
178 }  // namespace l2cap
179 }  // namespace bluetooth
180