aboutsummaryrefslogtreecommitdiff
path: root/gd/l2cap/internal/scheduler.h
blob: 32d00cfc9ce08695240e94f440070a4e2fad8b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "common/bidi_queue.h"
#include "l2cap/cid.h"
#include "l2cap/l2cap_packets.h"
#include "packet/base_packet_builder.h"
#include "packet/packet_view.h"

namespace bluetooth {
namespace l2cap {
namespace internal {

/**
 * Handle the scheduling of packets through the l2cap stack.
 * For each attached channel, dequeue its outgoing packets and enqueue it to the given LinkQueueUpEnd, according to some
 * policy (cid). Dequeue incoming packets from LinkQueueUpEnd, and enqueue it to ChannelQueueDownEnd. Note: If a channel
 * cannot dequeue from ChannelQueueDownEnd so that the buffer for incoming packet is full, further incoming packets will
 * be dropped.
 */
class Scheduler {
 public:
  using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
  using UpperDequeue = packet::BasePacketBuilder;
  using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
  using LowerEnqueue = UpperDequeue;
  using LowerDequeue = UpperEnqueue;
  using LowerQueueUpEnd = common::BidiQueueEnd<LowerEnqueue, LowerDequeue>;
  using DemuxPolicy = common::Callback<Cid(const UpperEnqueue&)>;

  /**
   * Attach the channel with the specified ChannelQueueDownEnd into the scheduler.
   *
   * @param cid The channel to attach to the scheduler.
   * @param channel_down_end The ChannelQueueDownEnd associated with the channel to attach to the scheduler.
   * @param remote_cid The destination endpoint of the packet.
   */
  virtual void AttachChannel(Cid cid, UpperQueueDownEnd* channel_down_end, Cid remote_cid) {}

  /**
   * Detach the channel from the scheduler.
   *
   * @param cid The channel to detach to the scheduler.
   */
  virtual void DetachChannel(Cid cid) {}

  /**
   * Return the lower queue up end, which can be used to enqueue or dequeue.
   */
  virtual LowerQueueUpEnd* GetLowerQueueUpEnd() const = 0;

  virtual ~Scheduler() = default;
};

}  // namespace internal
}  // namespace l2cap
}  // namespace bluetooth