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 #include "l2cap/classic/internal/fixed_channel_service_manager_impl.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include "common/bind.h"
22 #include "l2cap/cid.h"
23 #include "l2cap/classic/internal/fixed_channel_service_impl.h"
24 #include "os/log.h"
25 
26 namespace bluetooth {
27 namespace l2cap {
28 namespace classic {
29 namespace internal {
30 
Register(Cid cid,FixedChannelServiceImpl::PendingRegistration pending_registration)31 void FixedChannelServiceManagerImpl::Register(Cid cid,
32                                               FixedChannelServiceImpl::PendingRegistration pending_registration) {
33   if (cid < kFirstFixedChannel || cid > kLastFixedChannel || cid == kClassicSignallingCid) {
34     std::unique_ptr<FixedChannelService> invalid_service(new FixedChannelService());
35     pending_registration.user_handler_->Post(
36         common::BindOnce(std::move(pending_registration.on_registration_complete_callback_),
37                          FixedChannelManager::RegistrationResult::FAIL_INVALID_SERVICE, std::move(invalid_service)));
38   } else if (IsServiceRegistered(cid)) {
39     std::unique_ptr<FixedChannelService> invalid_service(new FixedChannelService());
40     pending_registration.user_handler_->Post(
41         common::BindOnce(std::move(pending_registration.on_registration_complete_callback_),
42                          FixedChannelManager::RegistrationResult::FAIL_DUPLICATE_SERVICE, std::move(invalid_service)));
43   } else {
44     service_map_.try_emplace(cid,
45                              FixedChannelServiceImpl(pending_registration.user_handler_,
46                                                      std::move(pending_registration.on_connection_open_callback_)));
47     std::unique_ptr<FixedChannelService> user_service(new FixedChannelService(cid, this, l2cap_layer_handler_));
48     pending_registration.user_handler_->Post(
49         common::BindOnce(std::move(pending_registration.on_registration_complete_callback_),
50                          FixedChannelManager::RegistrationResult::SUCCESS, std::move(user_service)));
51   }
52 }
53 
Unregister(Cid cid,FixedChannelService::OnUnregisteredCallback callback,os::Handler * handler)54 void FixedChannelServiceManagerImpl::Unregister(Cid cid, FixedChannelService::OnUnregisteredCallback callback,
55                                                 os::Handler* handler) {
56   if (IsServiceRegistered(cid)) {
57     service_map_.erase(cid);
58     handler->Post(std::move(callback));
59   } else {
60     log::error("service not registered cid:{}", cid);
61   }
62 }
63 
IsServiceRegistered(Cid cid) const64 bool FixedChannelServiceManagerImpl::IsServiceRegistered(Cid cid) const {
65   return service_map_.find(cid) != service_map_.end();
66 }
67 
GetService(Cid cid)68 FixedChannelServiceImpl* FixedChannelServiceManagerImpl::GetService(Cid cid) {
69   log::assert_that(IsServiceRegistered(cid), "assert failed: IsServiceRegistered(cid)");
70   return &service_map_.find(cid)->second;
71 }
72 
GetRegisteredServices()73 std::vector<std::pair<Cid, FixedChannelServiceImpl*>> FixedChannelServiceManagerImpl::GetRegisteredServices() {
74   std::vector<std::pair<Cid, FixedChannelServiceImpl*>> results;
75   for (auto& elem : service_map_) {
76     results.emplace_back(elem.first, &elem.second);
77   }
78   return results;
79 }
80 
81 namespace {
82 constexpr uint64_t kSignallingChannelMask = 0x02;
83 constexpr uint64_t kConnectionlessReceptionMask = 0x04;
84 constexpr uint64_t kBrEdrSecurityManager = 0x80;
85 }  // namespace
86 
GetSupportedFixedChannelMask()87 uint64_t FixedChannelServiceManagerImpl::GetSupportedFixedChannelMask() {
88   uint64_t result = 0;
89   result |= kSignallingChannelMask;  // Signalling channel is mandatory
90   for (const auto& elem : service_map_) {
91     Cid cid = elem.first;
92     switch (cid) {
93       case kConnectionlessCid:
94         result |= kConnectionlessReceptionMask;
95         continue;
96       case kSmpBrCid:
97         result |= kBrEdrSecurityManager;
98         continue;
99       default:
100         log::warn("Unknown fixed channel is registered: 0x{:x}", cid);
101         continue;
102     }
103   }
104   return result;
105 }
106 }  // namespace internal
107 }  // namespace classic
108 }  // namespace l2cap
109 }  // namespace bluetooth
110