1 /*
2  * Copyright 2020 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 #define LOG_TAG "att"
18 
19 #include "att/att_module.h"
20 
21 #include <memory>
22 
23 #include "l2cap/classic/l2cap_classic_module.h"
24 #include "l2cap/le/l2cap_le_module.h"
25 #include "module.h"
26 #include "os/handler.h"
27 #include "os/log.h"
28 
29 namespace bluetooth {
30 namespace att {
31 
__anon135a67ec0102() 32 const ModuleFactory AttModule::Factory = ModuleFactory([]() { return new AttModule(); });
33 
34 namespace {
OnAttRegistrationCompleteLe(l2cap::le::FixedChannelManager::RegistrationResult result,std::unique_ptr<l2cap::le::FixedChannelService> le_smp_service)35 void OnAttRegistrationCompleteLe(
36     l2cap::le::FixedChannelManager::RegistrationResult result,
37     std::unique_ptr<l2cap::le::FixedChannelService> le_smp_service) {
38   LOG_INFO("ATT channel registration complete");
39 }
40 
OnAttConnectionOpenLe(std::unique_ptr<l2cap::le::FixedChannel> channel)41 void OnAttConnectionOpenLe(std::unique_ptr<l2cap::le::FixedChannel> channel) {
42   LOG_INFO("ATT conneciton opened");
43 }
44 }  // namespace
45 
46 struct AttModule::impl {
implbluetooth::att::AttModule::impl47   impl(
48       os::Handler* att_handler,
49       l2cap::le::L2capLeModule* l2cap_le_module,
50       l2cap::classic::L2capClassicModule* l2cap_classic_module)
51       : att_handler_(att_handler), l2cap_le_module_(l2cap_le_module), l2cap_classic_module_(l2cap_classic_module) {
52     // TODO: move that into a ATT manager, or other proper place
53     std::unique_ptr<bluetooth::l2cap::le::FixedChannelManager> l2cap_manager_le_(
54         l2cap_le_module_->GetFixedChannelManager());
55     // TODO(b/161256497): CID 4 is taken by shim layer ATT module so far. When we migrate to GD ATT module, we use real
56     // CID here.
57     constexpr uint16_t kFakeLeAttributeCid = 50;
58     l2cap_manager_le_->RegisterService(
59         kFakeLeAttributeCid,
60         common::BindOnce(&OnAttRegistrationCompleteLe),
61         common::Bind(&OnAttConnectionOpenLe),
62         att_handler_);
63   }
64 
65   os::Handler* att_handler_;
66   l2cap::le::L2capLeModule* l2cap_le_module_;
67   l2cap::classic::L2capClassicModule* l2cap_classic_module_;
68 };
69 
ListDependencies(ModuleList * list)70 void AttModule::ListDependencies(ModuleList* list) {
71   list->add<l2cap::le::L2capLeModule>();
72   list->add<l2cap::classic::L2capClassicModule>();
73 }
74 
Start()75 void AttModule::Start() {
76   pimpl_ = std::make_unique<impl>(
77       GetHandler(), GetDependency<l2cap::le::L2capLeModule>(), GetDependency<l2cap::classic::L2capClassicModule>());
78 }
79 
Stop()80 void AttModule::Stop() {
81   pimpl_.reset();
82 }
83 
ToString() const84 std::string AttModule::ToString() const {
85   return "Att Module";
86 }
87 
88 // std::unique_ptr<AttManager> AttModule::GetAttManager() {
89 //   return std::unique_ptr<AttManager>(
90 //       new AttManager(pimpl_->att_handler_, &pimpl_->att_manager_impl));
91 // }
92 
93 }  // namespace att
94 }  // namespace bluetooth