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