-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
usage.rst
275 lines (176 loc) · 8.31 KB
/
usage.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
=====
Usage
=====
SimpleBLE works on Windows, Linux, MacOS and iOS. Please follow the instructions below
to build and run SimpleBLE in your specific environment.
System Requirements
===================
When building SimpleBLE from source, you will need some dependencies based on your
current operating system.
**NOTE:** WSL does not support Bluetooth.
General Requirements
--------------------
- `CMake`_ (Version 3.21 or higher)
Linux
-----
APT-based Distros
~~~~~~~~~~~~~~~~~
- `libdbus-1-dev` (install via ``sudo apt install libdbus-1-dev``)
RPM-based Distros
~~~~~~~~~~~~~~~~~
- `dbus-devel`
- On Fedora, install via ``sudo dnf install dbus-devel``
- On CentOS, install via ``sudo yum install dbus-devel``
Windows
-------
- `Windows SDK` (Version 10.0.19041.0 or higher)
MacOS
-----
- `Xcode Command Line Tools` (install via ``xcode-select --install``)
Android
-------
- `Android Studio`
- `Android NDK` (Version 25 or higher. Older versions might work but haven't been thoroughly tested.)
Building and Installing SimpleBLE (Source)
============================================
Compiling the library is done using `CMake`_ and relies heavily on plenty of CMake
functionality. It is strongly suggested that you get familiarized with CMake before
blindly following the instructions below.
Building SimpleBLE
------------------
You can use the following commands to build SimpleBLE: ::
cmake -S <path-to-simpleble> -B build_simpleble
cmake --build build_simpleble -j7
Note that if you want to modify the build configuration, you can do so by passing
additional arguments to the ``cmake`` command. For example, to build a shared library
set the ``BUILD_SHARED_LIBS`` CMake variable to ``TRUE`` ::
cmake -S <path-to-simpleble> -B build_simpleble -DBUILD_SHARED_LIBS=TRUE
To build a plain-flavored version of the library, set the ``SIMPLEBLE_PLAIN`` CMake
variable to ``TRUE`` ::
cmake -S <path-to-simpleble> -B build_simpleble -DSIMPLEBLE_PLAIN=TRUE
To modify the log level, set the ``SIMPLEBLE_LOG_LEVEL`` CMake variable to one of the
following values: ``VERBOSE``, ``DEBUG``, ``INFO``, ``WARN``, ``ERROR``, ``FATAL`` ::
cmake -S <path-to-simpleble> -B build_simpleble -DSIMPLEBLE_LOG_LEVEL=DEBUG
**(Linux only)** To force the usage of the DBus session bus, enable the ``SIMPLEBLE_USE_SESSION_DBUS`` flag ::
cmake -S <path-to-simplebluez> -B build_simplebluez -DSIMPLEBLE_USE_SESSION_DBUS=TRUE
Installing SimpleBLE
--------------------
To install SimpleBLE, you can use the following commands: ::
cmake --install build_simpleble
Note that if you want to modify the installation configuration, you can do so by passing
additional arguments to the ``cmake`` command. For example, to install the library to
a specific location, set the ``CMAKE_INSTALL_PREFIX`` CMake variable to the desired
location ::
cmake --install build_simpleble --prefix /usr/local
Note that on Linux and MacOS, you will need to run the ``cmake --install`` command
with ``sudo`` privileges. ::
sudo cmake --install build_simpleble
Usage with CMake (Installed)
============================
Once SimpleBLE has been installed, it can be consumed from within CMake::
find_package(simpleble REQUIRED CONFIG)
target_link_libraries(<your-target> simpleble::simpleble)
Note that this example assumes that SimpleBLE has been installed to a location
that is part of the default CMake module path.
Usage with CMake (Local)
=============================
You can add the ``simpleble`` library directory into your project and include it in
your ``CMakeLists.txt`` file ::
add_subdirectory(<path-to-simpleble> ${CMAKE_BINARY_DIR}/simpleble)
target_link_libraries(<your-target> simpleble::simpleble)
Usage with CMake (Vendorized)
=============================
If you want to use a vendorized copy of SimpleBLE, you can do so by using FetchContent
and specifying the location from where SimpleBLE should be consumed from. ::
include(FetchContent)
FetchContent_Declare(
simpleble
GIT_REPOSITORY <simpleble-git-repository>
GIT_TAG <simpleble-git-tag>
GIT_SHALLOW YES
)
# Note that here we manually do what FetchContent_MakeAvailable() would do,
# except to ensure that the dependency can also get what it needs, we add
# custom logic between the FetchContent_Populate() and add_subdirectory()
# calls.
FetchContent_GetProperties(simpleble)
if(NOT simpleble_POPULATED)
FetchContent_Populate(simpleble)
list(APPEND CMAKE_MODULE_PATH "${simpleble_SOURCE_DIR}/cmake/find")
add_subdirectory("${simpleble_SOURCE_DIR}/simpleble" "${simpleble_BINARY_DIR}")
endif()
set(simpleble_FOUND 1)
You can put this code inside ``Findsimpleble.cmake`` and add it to your CMake
module path, as depicted in `cmake-init-fetchcontent`_.
Once vendorized using the above approach, you can consume SimpleBLE from
within CMake as you'd normally do ::
find_package(simpleble REQUIRED)
target_link_libraries(<your-target> simpleble::simpleble)
One key security feature of SimpleBLE is that it allows the user to specify
the URLs and tags of all internal dependencies, thus allowing compilation
from internal or secure sources without the risk of those getting compromised.
Currently, the following libraries are included as part of SimpleBLE, with
the following CMake options available:
- `fmtlib`_
- ``LIBFMT_VENDORIZE``: Enable vendorization of fmtlib. *(Default: True)*
- ``LIBFMT_GIT_REPOSITORY``: The git repository to use for fmtlib.
- ``LIBFMT_GIT_TAG``: The git tag to use for fmtlib. *(Default: v9.1.0)*
- ``LIBFMT_LOCAL_PATH``: The local path to use for fmtlib. *(Default: None)*
Usage alongside native code in Android
======================================
When using SimpleBLE alongside native code in Android, you must include a small
Android dependency module that includes some necessary bridge classes used by SimpleBLE.
This is required because the Android JVM doesn't allow programatic definition of
derived classes, which forces us to bring these definitions in externally.
To include this dependency module, add the following to your `settings.gradle` file:
```groovy
includeBuild("path/to/simpleble/src/backends/android/simpleble-bridge") {
dependencySubstitution {
substitute module("org.simpleble.android.bridge:simpleble-bridge") with project(":")
}
}
```
```kotlin
includeBuild("path/to/simpleble/src/backends/android/simpleble-bridge") {
dependencySubstitution {
substitute(module("org.simpleble.android.bridge:simpleble-bridge")).using(project(":"))
}
}
```
**NOTE:** We will provide Maven packages in the future.
Build Examples
==============
Use the following instructions to build the provided SimpleBLE examples: ::
cmake -S <path-to-simpleble>/examples/simpleble -B build_simpleble_examples -DSIMPLEBLE_LOCAL=ON
cmake --build build_simpleble_examples -j7
Testing
=======
To build and run unit and integration tests, the following packages are
required: ::
sudo apt install libgtest-dev libgmock-dev python3-dev
pip3 install -r <path-to-simpleble>/test/requirements.txt
Unit Tests
----------
To run the unit tests, run the following command: ::
cmake -S <path-to-simpleble> -B build_simpleble_test -DSIMPLEBLE_TEST=ON
cmake --build build_simpleble_test -j7
./build_simpleble_test/bin/simpleble_test
Address Sanitizer Tests
-----------------------
To run the address sanitizer tests, run the following command: ::
cmake -S <path-to-simpleble> -B build_simpleble_test -DSIMPLEBLE_SANITIZE=Address -DSIMPLEBLE_TEST=ON
cmake --build build_simpleble_test -j7
PYTHONMALLOC=malloc ./build_simpleble_test/bin/simpleble_test
It's important for ``PYTHONMALLOC`` to be set to ``malloc``, otherwise the tests will
fail due to Python's memory allocator from triggering false positives.
Thread Sanitizer Tests
----------------------
To run the thread sanitizer tests, run the following command: ::
cmake -S <path-to-simpleble> -B build_simpleble_test -DSIMPLEBLE_SANITIZE=Thread -DSIMPLEBLE_TEST=ON
cmake --build build_simpleble_test -j7
./build_simpleble_test/bin/simpleble_test
.. Links
.. _CMake: https://cmake.org/
.. _Windows SDK: https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk
.. _cmake-init-fetchcontent: https://github.com/friendlyanon/cmake-init-fetchcontent
.. _fmtlib: https://github.com/fmtlib/fmt