Loading system/gd/l2cap/Android.bp +2 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ filegroup { "classic_fixed_channel_manager.cc", "classic_fixed_channel_service.cc", "internal/classic_fixed_channel_service_manager_impl.cc", "internal/classic_fixed_channel_allocator.cc", ], } Loading @@ -15,6 +16,7 @@ filegroup { srcs: [ "l2cap_packet_test.cc", "internal/classic_fixed_channel_service_manager_test.cc", "internal/classic_fixed_channel_allocator_test.cc", "signal_id_test.cc", ], } Loading system/gd/l2cap/internal/classic_fixed_channel_allocator.cc 0 → 100644 +56 −0 Original line number Diff line number Diff line /* * 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 system/gd/l2cap/internal/classic_fixed_channel_allocator.h 0 → 100644 +59 −0 Original line number Diff line number Diff line /* * 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 system/gd/l2cap/internal/classic_fixed_channel_allocator_test.cc 0 → 100644 +61 −0 Original line number Diff line number Diff line /* * 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 system/gd/l2cap/internal/classic_fixed_channel_impl.h 0 → 100644 +41 −0 Original line number Diff line number Diff line /* * 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 Loading
system/gd/l2cap/Android.bp +2 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ filegroup { "classic_fixed_channel_manager.cc", "classic_fixed_channel_service.cc", "internal/classic_fixed_channel_service_manager_impl.cc", "internal/classic_fixed_channel_allocator.cc", ], } Loading @@ -15,6 +16,7 @@ filegroup { srcs: [ "l2cap_packet_test.cc", "internal/classic_fixed_channel_service_manager_test.cc", "internal/classic_fixed_channel_allocator_test.cc", "signal_id_test.cc", ], } Loading
system/gd/l2cap/internal/classic_fixed_channel_allocator.cc 0 → 100644 +56 −0 Original line number Diff line number Diff line /* * 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
system/gd/l2cap/internal/classic_fixed_channel_allocator.h 0 → 100644 +59 −0 Original line number Diff line number Diff line /* * 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
system/gd/l2cap/internal/classic_fixed_channel_allocator_test.cc 0 → 100644 +61 −0 Original line number Diff line number Diff line /* * 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
system/gd/l2cap/internal/classic_fixed_channel_impl.h 0 → 100644 +41 −0 Original line number Diff line number Diff line /* * 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