• 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 };
53 
54 class Link;
55 
56 class LeSignallingManager {
57  public:
58   LeSignallingManager(os::Handler* handler, Link* link, l2cap::internal::DataPipelineManager* data_pipeline_manager,
59                       DynamicChannelServiceManagerImpl* dynamic_service_manager,
60                       l2cap::internal::DynamicChannelAllocator* channel_allocator);
61 
62   virtual ~LeSignallingManager();
63 
64   void SendConnectionRequest(Psm psm, Cid local_cid, Mtu mtu);
65 
66   void SendDisconnectRequest(Cid local_cid, Cid remote_cid);
67 
68   void SendConnectionParameterUpdateRequest(uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency,
69                                             uint16_t timeout_multiplier);
70 
71   void SendConnectionParameterUpdateResponse(SignalId signal_id, ConnectionParameterUpdateResponseResult result);
72 
73   void SendCredit(Cid local_cid, uint16_t credits);
74 
75   void CancelAlarm();
76 
77   void OnCommandReject(LeCommandRejectView command_reject_view);
78 
79   void OnConnectionParameterUpdateRequest(uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency,
80                                           uint16_t timeout_multiplier);
81   void OnConnectionParameterUpdateResponse(ConnectionParameterUpdateResponseResult result);
82 
83   void OnConnectionRequest(SignalId signal_id, Psm psm, Cid remote_cid, Mtu mtu, uint16_t mps,
84                            uint16_t initial_credits);
85 
86   void OnConnectionResponse(SignalId signal_id, Cid remote_cid, Mtu mtu, uint16_t mps, uint16_t initial_credits,
87                             LeCreditBasedConnectionResponseResult result);
88 
89   void OnDisconnectionRequest(SignalId signal_id, Cid cid, Cid remote_cid);
90 
91   void OnDisconnectionResponse(SignalId signal_id, Cid cid, Cid remote_cid);
92 
93   void OnCredit(Cid remote_cid, uint16_t credits);
94 
95  private:
96   void on_incoming_packet();
97   void send_connection_response(SignalId signal_id, Cid local_cid, Mtu mtu, uint16_t mps, uint16_t initial_credit,
98                                 LeCreditBasedConnectionResponseResult result);
99   void on_command_timeout();
100   void handle_send_next_command();
101 
102   os::Handler* handler_;
103   Link* link_;
104   l2cap::internal::DataPipelineManager* data_pipeline_manager_;
105   std::shared_ptr<le::internal::FixedChannelImpl> signalling_channel_;
106   DynamicChannelServiceManagerImpl* dynamic_service_manager_;
107   l2cap::internal::DynamicChannelAllocator* channel_allocator_;
108   std::unique_ptr<os::EnqueueBuffer<packet::BasePacketBuilder>> enqueue_buffer_;
109   std::queue<PendingCommand> pending_commands_;
110   PendingCommand command_just_sent_;
111   os::Alarm alarm_;
112   SignalId next_signal_id_ = kInitialSignalId;
113 };
114 
115 }  // namespace internal
116 }  // namespace le
117 }  // namespace l2cap
118 }  // namespace bluetooth
119