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/le/internal/dynamic_channel_service_manager_impl.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include "common/bind.h"
22 #include "l2cap/le/internal/dynamic_channel_service_impl.h"
23 #include "l2cap/psm.h"
24 #include "os/log.h"
25 
26 namespace bluetooth {
27 namespace l2cap {
28 namespace le {
29 namespace internal {
30 
Register(Psm psm,DynamicChannelServiceImpl::PendingRegistration pending_registration)31 void DynamicChannelServiceManagerImpl::Register(Psm psm,
32                                                 DynamicChannelServiceImpl::PendingRegistration pending_registration) {
33   if (IsServiceRegistered(psm)) {
34     std::unique_ptr<DynamicChannelService> invalid_service(new DynamicChannelService());
35     pending_registration.user_handler_->Post(common::BindOnce(
36         std::move(pending_registration.on_registration_complete_callback_),
37         DynamicChannelManager::RegistrationResult::FAIL_DUPLICATE_SERVICE, std::move(invalid_service)));
38   } else {
39     service_map_.try_emplace(
40         psm, DynamicChannelServiceImpl(pending_registration.user_handler_,
41                                        std::move(pending_registration.on_connection_open_callback_),
42                                        pending_registration.configuration_, pending_registration.security_policy_));
43     std::unique_ptr<DynamicChannelService> user_service(new DynamicChannelService(psm, this, l2cap_layer_handler_));
44     pending_registration.user_handler_->Post(
45         common::BindOnce(std::move(pending_registration.on_registration_complete_callback_),
46                          DynamicChannelManager::RegistrationResult::SUCCESS, std::move(user_service)));
47   }
48 }
49 
Unregister(Psm psm,DynamicChannelService::OnUnregisteredCallback callback,os::Handler * handler)50 void DynamicChannelServiceManagerImpl::Unregister(Psm psm, DynamicChannelService::OnUnregisteredCallback callback,
51                                                   os::Handler* handler) {
52   if (IsServiceRegistered(psm)) {
53     service_map_.erase(psm);
54     handler->Post(std::move(callback));
55   } else {
56     log::error("service not registered psm:{}", psm);
57   }
58 }
59 
IsServiceRegistered(Psm psm) const60 bool DynamicChannelServiceManagerImpl::IsServiceRegistered(Psm psm) const {
61   return service_map_.find(psm) != service_map_.end();
62 }
63 
GetService(Psm psm)64 DynamicChannelServiceImpl* DynamicChannelServiceManagerImpl::GetService(Psm psm) {
65   log::assert_that(IsServiceRegistered(psm), "assert failed: IsServiceRegistered(psm)");
66   return &service_map_.find(psm)->second;
67 }
68 
GetRegisteredServices()69 std::vector<std::pair<Psm, DynamicChannelServiceImpl*>> DynamicChannelServiceManagerImpl::GetRegisteredServices() {
70   std::vector<std::pair<Psm, DynamicChannelServiceImpl*>> results;
71   for (auto& elem : service_map_) {
72     results.emplace_back(elem.first, &elem.second);
73   }
74   return results;
75 }
76 
77 }  // namespace internal
78 }  // namespace le
79 }  // namespace l2cap
80 }  // namespace bluetooth
81