L2CAP: ClassicFixedChannelAllocator

This is a utility class for L2CAP link to manage channels

Test: bluetooth_test_gd
Bug: 138261142
Change-Id: I1adaaf29817371bb3f0e68d64f6aec3d3eea2c85
diff --git a/system/gd/l2cap/Android.bp b/system/gd/l2cap/Android.bp
index a4d419c..1b0036f 100644
--- a/system/gd/l2cap/Android.bp
+++ b/system/gd/l2cap/Android.bp
@@ -7,6 +7,7 @@
         "classic_fixed_channel_manager.cc",
         "classic_fixed_channel_service.cc",
         "internal/classic_fixed_channel_service_manager_impl.cc",
+        "internal/classic_fixed_channel_allocator.cc",
     ],
 }
 
@@ -15,6 +16,7 @@
     srcs: [
         "l2cap_packet_test.cc",
         "internal/classic_fixed_channel_service_manager_test.cc",
+        "internal/classic_fixed_channel_allocator_test.cc",
         "signal_id_test.cc",
     ],
 }
diff --git a/system/gd/l2cap/internal/classic_fixed_channel_allocator.cc b/system/gd/l2cap/internal/classic_fixed_channel_allocator.cc
new file mode 100644
index 0000000..6c87cc5
--- /dev/null
+++ b/system/gd/l2cap/internal/classic_fixed_channel_allocator.cc
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+
+#include <unordered_map>
+
+#include "classic_fixed_channel_allocator.h"
+#include "l2cap/cid.h"
+#include "l2cap/internal/classic_fixed_channel_allocator.h"
+#include "l2cap/security_policy.h"
+#include "os/handler.h"
+#include "os/log.h"
+
+namespace bluetooth {
+namespace l2cap {
+namespace internal {
+
+ClassicFixedChannelImpl* ClassicFixedChannelAllocator::AllocateChannel(Cid cid, SecurityPolicy security_policy) {
+  ASSERT_LOG(!IsChannelInUse((cid)), "Cid %d is already in use", cid);
+  ASSERT_LOG(cid >= kFirstFixedChannel && cid <= kLastFixedChannel, "Cid %d out of bound", cid);
+  channels_.try_emplace(cid, cid, handler_);
+  return &channels_.find(cid)->second;
+}
+
+bool ClassicFixedChannelAllocator::FreeChannel(Cid cid) {
+  ASSERT_LOG(IsChannelInUse(cid), "Channel is not in use: cid %d", cid);
+  channels_.erase(cid);
+  return true;
+}
+
+bool ClassicFixedChannelAllocator::IsChannelInUse(Cid cid) const {
+  return channels_.find(cid) != channels_.end();
+}
+
+ClassicFixedChannelImpl* ClassicFixedChannelAllocator::FindChannel(Cid cid) {
+  ASSERT_LOG(IsChannelInUse(cid), "Channel is not in use: cid %d", cid);
+  return &channels_.find(cid)->second;
+}
+size_t ClassicFixedChannelAllocator::NumberOfChannels() const {
+  return channels_.size();
+}
+}  // namespace internal
+}  // namespace l2cap
+}  // namespace bluetooth
diff --git a/system/gd/l2cap/internal/classic_fixed_channel_allocator.h b/system/gd/l2cap/internal/classic_fixed_channel_allocator.h
new file mode 100644
index 0000000..917dec2
--- /dev/null
+++ b/system/gd/l2cap/internal/classic_fixed_channel_allocator.h
@@ -0,0 +1,59 @@
+/*
+ * 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 <unordered_map>
+
+#include "l2cap/cid.h"
+#include "l2cap/internal/classic_fixed_channel_impl.h"
+#include "l2cap/security_policy.h"
+#include "os/handler.h"
+#include "os/log.h"
+
+namespace bluetooth {
+namespace l2cap {
+namespace internal {
+
+// Helper class for keeping channels in a Link. It allocates and frees Channel object, and supports querying whether a
+// channel is in use
+class ClassicFixedChannelAllocator {
+ public:
+  explicit ClassicFixedChannelAllocator(os::Handler* handler) : handler_(handler) {
+    ASSERT(handler_ != nullptr);
+  }
+
+  // Allocates a channel. If cid is used, return nullptr. NOTE: The returned ClassicFixedChannelImpl object is still
+  // owned by the channel cllocator, NOT the client.
+  ClassicFixedChannelImpl* AllocateChannel(Cid cid, SecurityPolicy security_policy);
+
+  // Frees a channel. If cid doesn't exist, return false
+  bool FreeChannel(Cid cid);
+
+  bool IsChannelInUse(Cid cid) const;
+
+  ClassicFixedChannelImpl* FindChannel(Cid cid);
+
+  size_t NumberOfChannels() const;
+
+ private:
+  os::Handler* handler_ = nullptr;
+  std::unordered_map<Cid, ClassicFixedChannelImpl> channels_;
+};
+
+}  // namespace internal
+}  // namespace l2cap
+}  // namespace bluetooth
diff --git a/system/gd/l2cap/internal/classic_fixed_channel_allocator_test.cc b/system/gd/l2cap/internal/classic_fixed_channel_allocator_test.cc
new file mode 100644
index 0000000..8249a4c
--- /dev/null
+++ b/system/gd/l2cap/internal/classic_fixed_channel_allocator_test.cc
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+
+#include "l2cap/internal/classic_fixed_channel_allocator.h"
+
+#include <gtest/gtest.h>
+
+namespace bluetooth {
+namespace l2cap {
+namespace internal {
+
+class L2capClassicFixedChannelAllocatorTest : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
+    handler_ = new os::Handler(thread_);
+    channel_allocator_ = std::make_unique<ClassicFixedChannelAllocator>(handler_);
+  }
+
+  void TearDown() override {
+    channel_allocator_.reset();
+    handler_->Clear();
+    delete handler_;
+    delete thread_;
+  }
+
+  os::Thread* thread_{nullptr};
+  os::Handler* handler_{nullptr};
+  std::unique_ptr<ClassicFixedChannelAllocator> channel_allocator_;
+};
+
+TEST_F(L2capClassicFixedChannelAllocatorTest, precondition) {
+  Cid cid = kFirstFixedChannel;
+  EXPECT_FALSE(channel_allocator_->IsChannelInUse(cid));
+}
+
+TEST_F(L2capClassicFixedChannelAllocatorTest, allocate_and_free_channel) {
+  Cid cid = kFirstFixedChannel;
+  auto* channel = channel_allocator_->AllocateChannel(cid, {});
+  EXPECT_TRUE(channel_allocator_->IsChannelInUse(cid));
+  EXPECT_EQ(channel, channel_allocator_->FindChannel(cid));
+  EXPECT_TRUE(channel_allocator_->FreeChannel(cid));
+  EXPECT_FALSE(channel_allocator_->IsChannelInUse(cid));
+}
+
+}  // namespace internal
+}  // namespace l2cap
+}  // namespace bluetooth
diff --git a/system/gd/l2cap/internal/classic_fixed_channel_impl.h b/system/gd/l2cap/internal/classic_fixed_channel_impl.h
new file mode 100644
index 0000000..d396aaa
--- /dev/null
+++ b/system/gd/l2cap/internal/classic_fixed_channel_impl.h
@@ -0,0 +1,41 @@
+/*
+ * 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 "l2cap/cid.h"
+#include "os/handler.h"
+#include "os/log.h"
+
+namespace bluetooth {
+namespace l2cap {
+namespace internal {
+
+class ClassicFixedChannelImpl {
+ public:
+  ClassicFixedChannelImpl(Cid cid, os::Handler* handler) : cid_(cid), handler_(handler) {
+    ASSERT_LOG(cid_ >= kFirstFixedChannel && cid_ <= kLastFixedChannel, "Invalid cid: %d", cid_);
+    ASSERT(handler_ != nullptr);
+  }
+
+ private:
+  Cid cid_;
+  os::Handler* handler_;
+};
+
+}  // namespace internal
+}  // namespace l2cap
+}  // namespace bluetooth
diff --git a/system/gd/l2cap/internal/classic_link.h b/system/gd/l2cap/internal/classic_link.h
new file mode 100644
index 0000000..20a4a61
--- /dev/null
+++ b/system/gd/l2cap/internal/classic_link.h
@@ -0,0 +1,23 @@
+/*
+ * 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
+
+namespace bluetooth {
+namespace l2cap {
+namespace internal {}
+}  // namespace l2cap
+}  // namespace bluetooth