1 /*
2  * Copyright 2023 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 "module.h"
18 
19 #include <bluetooth/log.h>
20 #include <hardware/bt_gatt.h>
21 
22 #include "btcore/include/module.h"
23 #include "os/log.h"
24 #ifndef TARGET_FLOSS
25 #include "src/connection/ffi/connection_shim.h"
26 #include "src/core/ffi.rs.h"
27 #include "src/gatt/ffi.rs.h"
28 #endif
29 
30 #ifdef TARGET_FLOSS
31 
32 // Rust modules don't run on Floss yet (b/277643360)
33 const module_t rust_module = {.name = RUST_MODULE,
34                               .init = nullptr,
35                               .start_up = nullptr,
36                               .shut_down = nullptr,
37                               .clean_up = nullptr,
38                               .dependencies = {}};
39 
40 #else
41 
42 extern const btgatt_callbacks_t* bt_gatt_callbacks;
43 
44 namespace bluetooth {
45 namespace rust_shim {
46 
FutureReady(future_t & future)47 void FutureReady(future_t& future) { future_ready(&future, FUTURE_SUCCESS); }
48 
49 }  // namespace rust_shim
50 }  // namespace bluetooth
51 
52 namespace {
Start()53 future_t* Start() {
54   auto fut = future_new();
55 
56   if (bt_gatt_callbacks == nullptr) {
57     // We can't crash here since some adapter tests mis-use the stack
58     // startup/cleanup logic and start the stack without GATT, but don't fully
59     // mock out the native layer.
60     bluetooth::log::error(
61         "GATT profile not started, so we cannot start the Rust loop - this "
62         "happens only in tests.");
63     bluetooth::rust_shim::FutureReady(*fut);
64     return fut;
65   }
66   bluetooth::rust_shim::start(
67       std::make_unique<bluetooth::gatt::GattServerCallbacks>(
68           *bt_gatt_callbacks->server),
69       std::make_unique<bluetooth::connection::LeAclManagerShim>(), *fut);
70 
71   return fut;
72 }
73 
Stop()74 future_t* Stop() {
75   bluetooth::rust_shim::stop();
76   return nullptr;
77 }
78 }  // namespace
79 
80 const module_t rust_module = {.name = RUST_MODULE,
81                               .init = nullptr,
82                               .start_up = Start,
83                               .shut_down = Stop,
84                               .clean_up = nullptr,
85                               .dependencies = {}};
86 
87 #endif
88