blob: a44a72e7fca24c9086eb83b71554d3f5d7823d49 [file] [log] [blame]
Jack Heff38d892019-10-03 17:11:07 -07001/*
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
23namespace bluetooth {
24namespace l2cap {
25namespace classic {
26namespace internal {
27
28void 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.user_handler_->Post(
33 common::BindOnce(std::move(pending_registration.on_registration_complete_callback_),
34 DynamicChannelManager::RegistrationResult::FAIL_INVALID_SERVICE, std::move(invalid_service)));
35 } else if (IsServiceRegistered(psm)) {
36 std::unique_ptr<DynamicChannelService> invalid_service(new DynamicChannelService());
37 pending_registration.user_handler_->Post(common::BindOnce(
38 std::move(pending_registration.on_registration_complete_callback_),
39 DynamicChannelManager::RegistrationResult::FAIL_DUPLICATE_SERVICE, std::move(invalid_service)));
40 } else {
41 service_map_.try_emplace(psm,
42 DynamicChannelServiceImpl(pending_registration.user_handler_,
Hansong Zhanga6312532019-11-19 14:01:36 -080043 std::move(pending_registration.on_connection_open_callback_),
44 pending_registration.configuration_));
Jack Heff38d892019-10-03 17:11:07 -070045 std::unique_ptr<DynamicChannelService> user_service(new DynamicChannelService(psm, this, l2cap_layer_handler_));
46 pending_registration.user_handler_->Post(
47 common::BindOnce(std::move(pending_registration.on_registration_complete_callback_),
48 DynamicChannelManager::RegistrationResult::SUCCESS, std::move(user_service)));
49 }
50}
51
52void DynamicChannelServiceManagerImpl::Unregister(Psm psm, DynamicChannelService::OnUnregisteredCallback callback,
53 os::Handler* handler) {
54 if (IsServiceRegistered(psm)) {
55 service_map_.erase(psm);
56 handler->Post(std::move(callback));
57 } else {
58 LOG_ERROR("service not registered psm:%d", psm);
59 }
60}
61
62bool DynamicChannelServiceManagerImpl::IsServiceRegistered(Psm psm) const {
63 return service_map_.find(psm) != service_map_.end();
64}
65
66DynamicChannelServiceImpl* DynamicChannelServiceManagerImpl::GetService(Psm psm) {
67 ASSERT(IsServiceRegistered(psm));
68 return &service_map_.find(psm)->second;
69}
70
71std::vector<std::pair<Psm, DynamicChannelServiceImpl*>> DynamicChannelServiceManagerImpl::GetRegisteredServices() {
72 std::vector<std::pair<Psm, DynamicChannelServiceImpl*>> results;
73 for (auto& elem : service_map_) {
74 results.emplace_back(elem.first, &elem.second);
75 }
76 return results;
77}
78
79} // namespace internal
80} // namespace classic
81} // namespace l2cap
82} // namespace bluetooth