1set(BT_ROOT ${AOSP_ROOT}/packages/modules/Bluetooth)
2set(ROOTCANAL_ROOT ${AOSP_ROOT}/packages/modules/Bluetooth/tools/rootcanal)
3set(PDL_ROOT ${AOSP_ROOT}/external/rust/crates/pdl-compiler)
4
5corrosion_import_crate(
6    MANIFEST_PATH ${PDL_ROOT}/Cargo.toml
7    FLAGS --offline --verbose --verbose)
8
9corrosion_set_env_vars(pdlc CARGO_HOME=${Rust_CARGO_HOME})
10corrosion_set_hostbuild(pdlc)
11
12get_property(pdlc_EXECUTABLE TARGET pdlc PROPERTY EXECUTABLE_PATH)
13
14# These tests depend on the tempfile crate which was not imported because
15# the crate remove_dir_all does not have a compatible version.
16set_tests_properties(cargo-test_pdlc PROPERTIES DISABLED True)
17
18android_license(
19    TARGET pdlc
20    LIBNAME None
21    SPDX None
22    LICENSE None
23    LOCAL None)
24
25# Generate the Rust/C++ backend for a .pdl specification file.
26function(pdl_gen)
27  # Parse arguments.
28  set(options)
29  set(oneValueArgs NAME INPUT OUTPUT LANG NAMESPACE)
30  set(multiValueArgs USING INCLUDE)
31  cmake_parse_arguments(pdl "${options}" "${oneValueArgs}"
32                        "${multiValueArgs}" ${ARGN})
33
34  if(NOT pdl_NAME)
35    message(FATAL_ERROR "Error: name not specified")
36  endif()
37
38  if(NOT pdl_INPUT)
39    message(FATAL_ERROR "Error: output file not specified")
40  endif()
41
42  if(NOT pdl_OUTPUT)
43    message(FATAL_ERROR "Error: output file not specified")
44  endif()
45
46  set(pdl_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/pdl_gen)
47  set(pdl_OUTPUT "${pdl_OUTPUT_DIR}/${pdl_OUTPUT}")
48
49  get_filename_component(pdl_INPUT_ABSOLUTE ${pdl_INPUT} ABSOLUTE)
50  get_filename_component(pdl_OUTPUT_ABSOLUTE ${pdl_OUTPUT} ABSOLUTE)
51  get_filename_component(pdl_OUTPUT_DIR ${pdl_OUTPUT_ABSOLUTE} DIRECTORY)
52  set(${pdl_NAME} "${pdl_OUTPUT_ABSOLUTE}" CACHE STRING "PDL output filepath for ${pdl_NAME}" FORCE)
53
54  file(MAKE_DIRECTORY ${pdl_OUTPUT_DIR})
55
56  if((pdl_LANG STREQUAL "rust") OR (pdl_LANG STREQUAL "rust_legacy"))
57    add_custom_command(
58        OUTPUT "${pdl_OUTPUT_ABSOLUTE}"
59        COMMAND
60        ${pdlc_EXECUTABLE}
61                --output-format "${pdl_LANG}"
62                "${pdl_INPUT_ABSOLUTE}"
63                    > "${pdl_OUTPUT_ABSOLUTE}"
64        COMMENT "Generating rust module from ${pdl_INPUT}"
65        VERBATIM
66        DEPENDS pdlc ${pdl_INPUT_ABSOLUTE})
67  endif()
68
69  if(pdl_LANG STREQUAL "c++")
70    if(NOT pdl_NAMESPACE)
71        message(FATAL_ERROR "Error: namespace not specified")
72    endif()
73
74    foreach(namespace ${pdl_USING})
75        list(APPEND pdl_FLAGS --using-namespace)
76        list(APPEND pdl_FLAGS ${namespace})
77    endforeach()
78    foreach(header ${pdl_INCLUDE})
79        list(APPEND pdl_FLAGS --include-header)
80        list(APPEND pdl_FLAGS ${header})
81    endforeach()
82
83    add_custom_command(
84        OUTPUT "${pdl_OUTPUT_ABSOLUTE}.json"
85        COMMAND
86        ${pdlc_EXECUTABLE}
87                --output-format json
88                "${pdl_INPUT_ABSOLUTE}"
89                    > "${pdl_OUTPUT_ABSOLUTE}.json"
90        COMMENT "Analyzing ${pdl_INPUT}"
91        VERBATIM
92        DEPENDS pdlc ${pdl_INPUT_ABSOLUTE})
93
94    add_custom_command(
95        OUTPUT "${pdl_OUTPUT_ABSOLUTE}"
96        COMMAND
97        ${PDL_ROOT}/scripts/generate_cxx_backend.py
98                --input "${pdl_OUTPUT_ABSOLUTE}.json"
99                --output "${pdl_OUTPUT_ABSOLUTE}"
100                --namespace ${pdl_NAMESPACE}
101                ${pdl_FLAGS}
102        COMMENT "Generating c++ header from ${pdl_INPUT}"
103        VERBATIM
104        DEPENDS pdlc ${pdl_OUTPUT_ABSOLUTE}.json)
105  endif()
106
107  add_custom_target("pdl_gen-${pdl_NAME}" DEPENDS ${pdl_OUTPUT_ABSOLUTE})
108endfunction()
109
110pdl_gen(
111  NAME BluetoothGeneratedPackets_h
112  INPUT ${ROOTCANAL_ROOT}/packets/hci_packets.pdl
113  OUTPUT packets/hci_packets.h
114  LANG c++
115  NAMESPACE "bluetooth::hci"
116  INCLUDE "hci/address.h")
117
118pdl_gen(
119  NAME RootCanalGeneratedPackets_h
120  INPUT ${ROOTCANAL_ROOT}/packets/link_layer_packets.pdl
121  OUTPUT packets/link_layer_packets.h
122  LANG c++
123  NAMESPACE model::packets
124  INCLUDE "hci/address.h"
125  USING "bluetooth::hci")
126
127pdl_gen(
128  NAME RootCanalGeneratedPackets_rs
129  INPUT ${ROOTCANAL_ROOT}/packets/link_layer_packets.pdl
130  OUTPUT link_layer_packets.rs
131  LANG rust_legacy)
132
133android_add_library(
134  TARGET libscriptedbeaconpayload-protos-lite LICENSE Apache-2.0
135  SOURCE_DIR ${ROOTCANAL_ROOT} SRC ${libscriptedbeaconpayload_protos_lite_src})
136
137protobuf_generate_with_plugin(
138  TARGET libscriptedbeaconpayload-protos-lite
139  PROTOS ${ROOTCANAL_ROOT}/model/devices/scripted_beacon_ble_payload.proto
140  APPEND_PATH
141  PROTOPATH -I${AOSP_ROOT}/external/protobuf/src
142            -I${ROOTCANAL_ROOT}/model/devices
143  PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/model/devices)
144
145target_include_directories(
146  libscriptedbeaconpayload-protos-lite
147  PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${AOSP_ROOT}/external/protobuf/src)
148
149target_link_libraries(libscriptedbeaconpayload-protos-lite
150                      PRIVATE protobuf::libprotobuf)
151
152android_add_library(
153  TARGET librootcanal_config LICENSE Apache-2.0
154  SOURCE_DIR ${ROOTCANAL_ROOT} SRC ${librootcanal_config_src})
155
156protobuf_generate_with_plugin(
157  TARGET librootcanal_config
158  PROTOS ${ROOTCANAL_ROOT}/proto/rootcanal/configuration.proto
159  APPEND_PATH
160  PROTOPATH -I${AOSP_ROOT}/external/protobuf/src
161  PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/proto/rootcanal)
162
163target_include_directories(
164  librootcanal_config
165  PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/proto ${AOSP_ROOT}/external/protobuf/src)
166
167target_link_libraries(librootcanal_config
168                      PRIVATE protobuf::libprotobuf)
169
170add_library(libbt-rootcanal.headers INTERFACE)
171target_include_directories(libbt-rootcanal.headers INTERFACE ${ROOTCANAL_ROOT})
172target_link_libraries(libbt-rootcanal.headers
173                      INTERFACE android-emu-base-headers)
174android_license(TARGET "libbt-rootcanal.headers" LIBNAME None SPDX Apache-2.0
175                LICENSE Apache-2.0 LOCAL "${BT_ROOT}/NOTICE")
176
177android_add_library(
178  TARGET librootcanal_log
179  LICENSE Apache-2.0
180  SOURCE_DIR ${ROOTCANAL_ROOT}
181  SRC lib/log.cc
182      ${AOSP_ROOT}/external/fmtlib/src/format.cc)
183
184target_include_directories(
185  librootcanal_log PUBLIC
186    ${ROOTCANAL_ROOT}/include
187    ${AOSP_ROOT}/external/fmtlib/include)
188
189android_add_library(
190  TARGET libbt-rootcanal
191  LICENSE Apache-2.0
192  SOURCE_DIR ${ROOTCANAL_ROOT}
193  SRC ${BluetoothGeneratedPackets_h}
194      ${RootCanalGeneratedPackets_h}
195      lib/crypto/crypto.cc
196      lib/hci/address.cc
197      lib/hci/pcap_filter.cc
198      lib/log.cc
199      model/controller/acl_connection.cc
200      model/controller/acl_connection_handler.cc
201      model/controller/controller_properties.cc
202      model/controller/dual_mode_controller.cc
203      model/controller/le_advertiser.cc
204      model/controller/link_layer_controller.cc
205      model/controller/sco_connection.cc
206      model/controller/vendor_commands/le_apcf.cc
207      model/devices/beacon.cc
208      model/devices/beacon_swarm.cc
209      model/devices/device.cc
210      model/devices/hci_device.cc
211      model/devices/link_layer_socket_device.cc
212      model/devices/scripted_beacon.cc
213      model/devices/sniffer.cc
214      model/hci/h4_data_channel_packetizer.cc
215      model/hci/h4_parser.cc
216      model/hci/hci_sniffer.cc
217      model/hci/hci_socket_transport.cc
218      model/setup/device_boutique.cc
219      model/setup/phy_device.cc
220      model/setup/phy_layer.cc
221      model/setup/test_channel_transport.cc
222      model/setup/test_command_handler.cc
223      model/setup/test_model.cc
224  LINUX net/posix/posix_async_socket.cc
225        net/posix/posix_async_socket_connector.cc
226        net/posix/posix_async_socket_server.cc
227  DARWIN net/posix/posix_async_socket.cc
228         net/posix/posix_async_socket_connector.cc
229         net/posix/posix_async_socket_server.cc
230  DEPS android-emu-base
231       android-emu-base-headers
232       android-emu-base-logging
233       crypto
234       librootcanal_config
235       libscriptedbeaconpayload-protos-lite)
236
237target_link_libraries(
238  libbt-rootcanal
239  PUBLIC librootcanal_log)
240
241target_include_directories(
242  libbt-rootcanal
243  PUBLIC ${ROOTCANAL_ROOT}/include
244         ${ROOTCANAL_ROOT}
245         ${PDL_ROOT}/scripts
246         ${CMAKE_CURRENT_BINARY_DIR}/pdl_gen
247         ${CMAKE_CURRENT_BINARY_DIR}/config)
248
249target_compile_options(libbt-rootcanal
250                       PUBLIC -Wno-inconsistent-missing-override)
251
252add_subdirectory(rust)
253