summaryrefslogtreecommitdiff
path: root/system/gd/hci/acl_manager/round_robin_scheduler.h
blob: d78bf511dc55f4e783d679708f624b35bb49c31f (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
73
74
75
76
77
78
79
80
81
/*
 * Copyright 2020 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 <stdint.h>

#include "common/bidi_queue.h"
#include "common/multi_priority_queue.h"
#include "hci/acl_manager.h"
#include "hci/controller.h"
#include "hci/hci_packets.h"
#include "os/handler.h"

namespace bluetooth {
namespace hci {
namespace acl_manager {

class RoundRobinScheduler {
 public:
  RoundRobinScheduler(
      os::Handler* handler, Controller* controller, common::BidiQueueEnd<AclBuilder, AclView>* hci_queue_end);
  ~RoundRobinScheduler();

  enum ConnectionType { CLASSIC, LE };

  struct acl_queue_handler {
    ConnectionType connection_type_;
    std::shared_ptr<acl_manager::AclConnection::Queue> queue_;
    bool dequeue_is_registered_ = false;
    uint16_t number_of_sent_packets_ = 0;  // Track credits
    bool high_priority_ = false;           // For A2dp use
  };

  void Register(ConnectionType connection_type, uint16_t handle,
                std::shared_ptr<acl_manager::AclConnection::Queue> queue);
  void Unregister(uint16_t handle);
  void SetLinkPriority(uint16_t handle, bool high_priority);
  uint16_t GetCredits();
  uint16_t GetLeCredits();

 private:
  void start_round_robin();
  void buffer_packet(std::map<uint16_t, acl_queue_handler>::iterator acl_queue_handler);
  void unregister_all_connections();
  void send_next_fragment();
  std::unique_ptr<AclBuilder> handle_enqueue_next_fragment();
  void incoming_acl_credits(uint16_t handle, uint16_t credits);

  os::Handler* handler_ = nullptr;
  Controller* controller_ = nullptr;
  std::map<uint16_t, acl_queue_handler> acl_queue_handlers_;
  common::MultiPriorityQueue<std::pair<ConnectionType, std::unique_ptr<AclBuilder>>, 2> fragments_to_send_;
  uint16_t max_acl_packet_credits_ = 0;
  uint16_t acl_packet_credits_ = 0;
  uint16_t le_max_acl_packet_credits_ = 0;
  uint16_t le_acl_packet_credits_ = 0;
  size_t hci_mtu_{0};
  size_t le_hci_mtu_{0};
  std::atomic_bool enqueue_registered_ = false;
  common::BidiQueueEnd<AclBuilder, AclView>* hci_queue_end_ = nullptr;
  // first register queue end for the Round-robin schedule
  std::map<uint16_t, acl_queue_handler>::iterator starting_point_;
};

}  // namespace acl_manager
}  // namespace hci
}  // namespace bluetooth