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
24 #ifndef TARGET_FLOSS
25 #include "src/core/ffi.rs.h"
26 #include "src/gatt/ffi.rs.h"
27 #endif
28
29 #ifdef TARGET_FLOSS
30
31 // Rust modules don't run on Floss yet (b/277643360)
32 const module_t rust_module = {.name = RUST_MODULE,
33 .init = nullptr,
34 .start_up = nullptr,
35 .shut_down = nullptr,
36 .clean_up = nullptr,
37 .dependencies = {}};
38
39 #else
40
41 extern const btgatt_callbacks_t* bt_gatt_callbacks;
42
43 namespace bluetooth {
44 namespace rust_shim {
45
FutureReady(future_t & future)46 void FutureReady(future_t& future) { future_ready(&future, FUTURE_SUCCESS); }
47
48 } // namespace rust_shim
49 } // namespace bluetooth
50
51 namespace {
Start()52 future_t* Start() {
53 auto fut = future_new();
54
55 auto callbacks = bt_gatt_callbacks;
56 if (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>(*callbacks->server), *fut);
68
69 return fut;
70 }
71
Stop()72 future_t* Stop() {
73 bluetooth::rust_shim::stop();
74 return nullptr;
75 }
76 } // namespace
77
78 const module_t rust_module = {.name = RUST_MODULE,
79 .init = nullptr,
80 .start_up = Start,
81 .shut_down = Stop,
82 .clean_up = nullptr,
83 .dependencies = {}};
84
85 #endif
86