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 #pragma once
18 
19 #include <mutex>
20 
21 #include "main/shim/btm.h"
22 
23 #include "gd/module.h"
24 #include "gd/os/handler.h"
25 #include "gd/os/thread.h"
26 #include "gd/os/utils.h"
27 #include "gd/stack_manager.h"
28 
29 // The shim layer implementation on the Gd stack side.
30 namespace bluetooth {
31 namespace shim {
32 
33 // GD shim stack, having modes corresponding to legacy stack
34 class Stack {
35  public:
36   static Stack* GetInstance();
37 
38   Stack() = default;
39   ~Stack() = default;
40 
41   // Idle mode, config is loaded, but controller is not enabled
42   void StartIdleMode();
43   // Running mode, everything is up
44   void StartEverything();
45 
46   void Stop();
47   bool IsRunning();
48 
49   StackManager* GetStackManager();
50   Btm* GetBtm();
51   os::Handler* GetHandler();
52 
53   DISALLOW_COPY_AND_ASSIGN(Stack);
54 
55  private:
56   std::mutex mutex_;
57   StackManager stack_manager_;
58   bool is_running_ = false;
59   os::Thread* stack_thread_ = nullptr;
60   os::Handler* stack_handler_ = nullptr;
61   Btm* btm_ = nullptr;
62 
63   void Start(ModuleList* modules);
64 };
65 
66 }  // namespace shim
67 }  // namespace bluetooth
68