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 #include "common/bind.h"
19 #include "l2cap/classic/internal/dynamic_channel_service_impl.h"
20 #include "l2cap/psm.h"
21 #include "os/log.h"
22
23 namespace bluetooth {
24 namespace l2cap {
25 namespace classic {
26 namespace internal {
27
Register(Psm psm,DynamicChannelServiceImpl::PendingRegistration pending_registration)28 void DynamicChannelServiceManagerImpl::Register(Psm psm,
29 DynamicChannelServiceImpl::PendingRegistration pending_registration) {
30 if (!IsPsmValid(psm)) {
31 std::unique_ptr<DynamicChannelService> invalid_service(new DynamicChannelService());
32 pending_registration.on_registration_complete_callback_.Invoke(
33 DynamicChannelManager::RegistrationResult::FAIL_INVALID_SERVICE, std::move(invalid_service));
34 } else if (IsServiceRegistered(psm)) {
35 std::unique_ptr<DynamicChannelService> invalid_service(new DynamicChannelService());
36 pending_registration.on_registration_complete_callback_.Invoke(
37 DynamicChannelManager::RegistrationResult::FAIL_DUPLICATE_SERVICE, std::move(invalid_service));
38 } else {
39 service_map_.try_emplace(
40 psm,
41 DynamicChannelServiceImpl(
42 pending_registration.security_policy_,
43 std::move(pending_registration.on_connection_open_callback_),
44 pending_registration.configuration_));
45 std::unique_ptr<DynamicChannelService> user_service(new DynamicChannelService(psm, this, l2cap_layer_handler_));
46 pending_registration.on_registration_complete_callback_.Invoke(
47 DynamicChannelManager::RegistrationResult::SUCCESS, std::move(user_service));
48 }
49 }
50
Unregister(Psm psm,DynamicChannelService::OnUnregisteredCallback callback)51 void DynamicChannelServiceManagerImpl::Unregister(Psm psm, DynamicChannelService::OnUnregisteredCallback callback) {
52 if (IsServiceRegistered(psm)) {
53 service_map_.erase(psm);
54 callback.Invoke();
55 } else {
56 LOG_ERROR("service not registered psm:%d", 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 ASSERT(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 classic
79 } // namespace l2cap
80 } // namespace bluetooth
81