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 #define LOG_TAG "bt_gd_shim"
18 
19 #include "shim/stack.h"
20 #include "att/att_module.h"
21 #include "hal/hci_hal.h"
22 #include "hci/acl_manager.h"
23 #include "hci/hci_layer.h"
24 #include "hci/le_advertising_manager.h"
25 #include "hci/le_scanning_manager.h"
26 #include "l2cap/classic/l2cap_classic_module.h"
27 #include "l2cap/le/l2cap_le_module.h"
28 #include "neighbor/connectability.h"
29 #include "neighbor/discoverability.h"
30 #include "neighbor/inquiry.h"
31 #include "neighbor/name.h"
32 #include "neighbor/name_db.h"
33 #include "neighbor/page.h"
34 #include "neighbor/scan.h"
35 #include "os/log.h"
36 #include "os/thread.h"
37 #include "security/security_module.h"
38 #include "shim/dumpsys.h"
39 #include "shim/l2cap.h"
40 #include "stack_manager.h"
41 #include "storage/legacy.h"
42 
43 using ::bluetooth::os::Thread;
44 
45 struct bluetooth::shim::Stack::impl {
46   void Start() {
47     if (is_running_) {
48       LOG_ERROR("%s Gd stack already running", __func__);
49       return;
50     }
51 
52     LOG_INFO("%s Starting Gd stack", __func__);
53     ModuleList modules;
54     modules.add<::bluetooth::att::AttModule>();
55     modules.add<::bluetooth::hal::HciHal>();
56     modules.add<::bluetooth::hci::AclManager>();
57     modules.add<::bluetooth::hci::HciLayer>();
58     modules.add<::bluetooth::hci::LeAdvertisingManager>();
59     modules.add<::bluetooth::hci::LeScanningManager>();
60     modules.add<::bluetooth::l2cap::classic::L2capClassicModule>();
61     modules.add<::bluetooth::l2cap::le::L2capLeModule>();
62     modules.add<::bluetooth::neighbor::ConnectabilityModule>();
63     modules.add<::bluetooth::neighbor::DiscoverabilityModule>();
64     modules.add<::bluetooth::neighbor::InquiryModule>();
65     modules.add<::bluetooth::neighbor::NameModule>();
66     modules.add<::bluetooth::neighbor::NameDbModule>();
67     modules.add<::bluetooth::neighbor::PageModule>();
68     modules.add<::bluetooth::neighbor::ScanModule>();
69     modules.add<::bluetooth::security::SecurityModule>();
70     modules.add<::bluetooth::storage::LegacyModule>();
71     modules.add<::bluetooth::shim::Dumpsys>();
72     modules.add<::bluetooth::shim::L2cap>();
73 
74     stack_thread_ = new Thread("gd_stack_thread", Thread::Priority::NORMAL);
75     stack_manager_.StartUp(&modules, stack_thread_);
76     // TODO(cmanton) Gd stack has spun up another thread with no
77     // ability to ascertain the completion
78     is_running_ = true;
79     LOG_INFO("%s Successfully toggled Gd stack", __func__);
80   }
81 
82   void Stop() {
83     if (!is_running_) {
84       LOG_ERROR("%s Gd stack not running", __func__);
85       return;
86     }
87 
88     stack_manager_.ShutDown();
89     delete stack_thread_;
90     is_running_ = false;
91     LOG_INFO("%s Successfully shut down Gd stack", __func__);
92   }
93 
94   StackManager* GetStackManager() {
95     ASSERT(is_running_);
96     return &stack_manager_;
97   }
98 
99  private:
100   os::Thread* stack_thread_ = nullptr;
101   bool is_running_ = false;
102   StackManager stack_manager_;
103 };
104 
105 bluetooth::shim::Stack::Stack() {
106   pimpl_ = std::make_unique<impl>();
107   LOG_INFO("%s Created gd stack", __func__);
108 }
109 
110 void bluetooth::shim::Stack::Start() {
111   pimpl_->Start();
112 }
113 
114 void bluetooth::shim::Stack::Stop() {
115   pimpl_->Stop();
116 }
117 
118 bluetooth::StackManager* bluetooth::shim::Stack::GetStackManager() {
119   return pimpl_->GetStackManager();
120 }
121 
122 bluetooth::shim::Stack* bluetooth::shim::GetGabeldorscheStack() {
123   static Stack* instance = new Stack();
124   return instance;
125 }
126