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