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 slave_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(SignalId signal_id, uint16_t interval_min, uint16_t interval_max, 80 uint16_t slave_latency, uint16_t timeout_multiplier) { 81 PendingCommand pending_command; 82 pending_command.signal_id_ = signal_id; 83 pending_command.command_code_ = LeCommandCode::CONNECTION_PARAMETER_UPDATE_REQUEST; 84 pending_command.interval_min_ = interval_min; 85 pending_command.interval_max_ = interval_max; 86 pending_command.slave_latency_ = slave_latency; 87 pending_command.timeout_multiplier_ = timeout_multiplier; 88 return pending_command; 89 } 90 }; 91 92 class Link; 93 94 class LeSignallingManager { 95 public: 96 LeSignallingManager(os::Handler* handler, Link* link, l2cap::internal::DataPipelineManager* data_pipeline_manager, 97 DynamicChannelServiceManagerImpl* dynamic_service_manager, 98 l2cap::internal::DynamicChannelAllocator* channel_allocator); 99 100 virtual ~LeSignallingManager(); 101 102 void SendConnectionRequest(Psm psm, Cid local_cid, Mtu mtu); 103 104 void SendDisconnectRequest(Cid local_cid, Cid remote_cid); 105 106 // Note: Since Core 4.1, LL slave can send this through HCI command. 107 void SendConnectionParameterUpdateRequest(uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, 108 uint16_t timeout_multiplier); 109 110 void SendConnectionParameterUpdateResponse(SignalId signal_id, ConnectionParameterUpdateResponseResult result); 111 112 void SendCredit(Cid local_cid, uint16_t credits); 113 114 void CancelAlarm(); 115 116 void OnCommandReject(LeCommandRejectView command_reject_view); 117 118 void OnConnectionParameterUpdateRequest(SignalId signal_id, uint16_t interval_min, uint16_t interval_max, 119 uint16_t slave_latency, uint16_t timeout_multiplier); 120 void OnConnectionParameterUpdateResponse(SignalId signal_id, ConnectionParameterUpdateResponseResult result); 121 122 void OnConnectionRequest(SignalId signal_id, Psm psm, Cid remote_cid, Mtu mtu, uint16_t mps, 123 uint16_t initial_credits); 124 125 void OnConnectionResponse(SignalId signal_id, Cid remote_cid, Mtu mtu, uint16_t mps, uint16_t initial_credits, 126 LeCreditBasedConnectionResponseResult result); 127 128 void OnDisconnectionRequest(SignalId signal_id, Cid cid, Cid remote_cid); 129 130 void OnDisconnectionResponse(SignalId signal_id, Cid cid, Cid remote_cid); 131 132 void OnCredit(Cid remote_cid, uint16_t credits); 133 134 private: 135 struct PendingConnection { 136 Cid remote_cid; 137 Mtu mtu; 138 uint16_t max_pdu_size; 139 uint16_t initial_credits; 140 SignalId incoming_signal_id; 141 }; 142 143 void on_incoming_packet(); 144 void send_connection_response(SignalId signal_id, Cid local_cid, Mtu mtu, uint16_t mps, uint16_t initial_credit, 145 LeCreditBasedConnectionResponseResult result); 146 void on_command_timeout(); 147 void handle_send_next_command(); 148 void on_security_result_for_incoming(Psm psm, PendingConnection request, bool result); 149 void on_security_result_for_outgoing(Psm psm, Cid local_cid, Mtu mtu, bool result); 150 151 os::Handler* handler_; 152 Link* link_; 153 l2cap::internal::DataPipelineManager* data_pipeline_manager_; 154 std::shared_ptr<le::internal::FixedChannelImpl> signalling_channel_; 155 DynamicChannelServiceManagerImpl* dynamic_service_manager_; 156 l2cap::internal::DynamicChannelAllocator* channel_allocator_; 157 std::unique_ptr<os::EnqueueBuffer<packet::BasePacketBuilder>> enqueue_buffer_; 158 std::queue<PendingCommand> pending_commands_; 159 PendingCommand command_just_sent_; 160 os::Alarm alarm_; 161 SignalId next_signal_id_ = kInitialSignalId; 162 }; 163 164 } // namespace internal 165 } // namespace le 166 } // namespace l2cap 167 } // namespace bluetooth 168