Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit a32e2e4

Browse files
Merge pull request #1 from letsdev/bugfix/crash_on_find
Bugfix/crash on find
2 parents ac6d308 + cd8fcd4 commit a32e2e4

4 files changed

Lines changed: 120 additions & 152 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ include(GNUInstallDirs)
1919
set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
2020

2121
# Make a version file containing the current version from git.
22-
include (GetGitRevisionDescription)
23-
git_describe (VERSION "--tags")
24-
22+
#include (GetGitRevisionDescription)
23+
#git_describe (VERSION "--tags")
24+
set (VERSION "v0.5.3")
2525
if ("x_${VERSION}" STREQUAL "x_GIT-NOTFOUND" OR "x_${VERSION}" STREQUAL "x_HEAD-HASH-NOTFOUND" OR "x_${VERSION}" STREQUAL "x_-128-NOTFOUND")
2626
message (WARNING " - Install git to compile a production libtinyb!")
2727
set (VERSION "v0.5.0-dirty")

api/tinyb/BluetoothManager.hpp

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,40 @@
2323
*/
2424

2525
#pragma once
26+
2627
#include "BluetoothObject.hpp"
2728
#include "BluetoothEvent.hpp"
2829
#include <vector>
2930
#include <list>
31+
#include <mutex>
32+
33+
class tinyb::BluetoothManager : public BluetoothObject {
34+
friend class BluetoothAdapter;
35+
36+
friend class BluetoothDevice;
37+
38+
friend class BluetoothGattService;
39+
40+
friend class BluetoothGattCharacteristic;
3041

31-
class tinyb::BluetoothManager: public BluetoothObject
32-
{
33-
friend class BluetoothAdapter;
34-
friend class BluetoothDevice;
35-
friend class BluetoothGattService;
36-
friend class BluetoothGattCharacteristic;
37-
friend class BluetoothGattDescriptor;
38-
friend class BluetoothEventManager;
42+
friend class BluetoothGattDescriptor;
43+
44+
friend class BluetoothEventManager;
3945

4046
private:
41-
std::unique_ptr<BluetoothAdapter> default_adapter;
47+
std::unique_ptr <BluetoothAdapter> default_adapter;
4248
static BluetoothManager *bluetooth_manager;
43-
std::list<std::shared_ptr<BluetoothEvent>> event_list;
49+
std::list <std::shared_ptr<BluetoothEvent>> event_list;
50+
std::mutex mutex;
4451

4552
BluetoothManager();
53+
4654
BluetoothManager(const BluetoothManager &object);
4755

4856
protected:
4957

50-
void handle_event(BluetoothType type, std::string *name,
51-
std::string *identifier, BluetoothObject *parent, BluetoothObject &object);
58+
void handle_event(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent,
59+
BluetoothObject &object);
5260

5361
public:
5462

@@ -57,14 +65,19 @@ friend class BluetoothEventManager;
5765
}
5866

5967
static std::string get_api_version();
68+
6069
static std::string get_library_version();
6170

6271
virtual std::string get_java_class() const;
72+
6373
virtual std::string get_class_name() const;
74+
6475
virtual std::string get_object_path() const;
76+
6577
virtual BluetoothType get_bluetooth_type() const;
6678

6779
~BluetoothManager();
80+
6881
/** Returns an instance of BluetoothManager, to be used instead of constructor.
6982
* @return An initialized BluetoothManager instance.
7083
*/
@@ -73,23 +86,29 @@ friend class BluetoothEventManager;
7386
/** Add event to checked against events generated by BlueZ. If an the event
7487
* matches an incoming event its' callback will be triggered. Events can be
7588
* the addition of a new Device, GattService, GattCharacteristic, etc. */
76-
void add_event(std::shared_ptr<BluetoothEvent> &event) {
89+
void add_event(std::shared_ptr <BluetoothEvent> &event) {
7790
event_list.push_back(event);
7891
}
7992

8093
/** Remove event to checked against events generated by BlueZ.
8194
*/
82-
void remove_event(std::shared_ptr<BluetoothEvent> &event) {
95+
void remove_event(std::shared_ptr <BluetoothEvent> &event) {
96+
97+
8398
event_list.remove(event);
99+
100+
84101
}
85102

86103
void remove_event(BluetoothEvent &event) {
87-
for(auto it = event_list.begin(); it != event_list.end(); ++it) {
104+
std::lock_guard<std::mutex> guard(mutex);
105+
for (auto it = event_list.begin(); it != event_list.end(); ++it) {
88106
if ((*it).get() == &event) {
89107
event_list.remove(*it);
90108
break;
91109
}
92110
}
111+
93112
}
94113

95114

@@ -110,11 +129,9 @@ friend class BluetoothEventManager;
110129
* timeout expires or event is canceled.
111130
*/
112131
template<class T>
113-
std::unique_ptr<T> find(std::string *name,
114-
std::string* identifier, BluetoothObject *parent,
115-
std::chrono::milliseconds timeout = std::chrono::milliseconds::zero())
116-
{
117-
std::unique_ptr<BluetoothObject> obj = find(T::class_type(), name, identifier, parent, timeout);
132+
std::unique_ptr <T> find(std::string *name, std::string *identifier, BluetoothObject *parent,
133+
std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()) {
134+
std::unique_ptr <BluetoothObject> obj = find(T::class_type(), name, identifier, parent, timeout);
118135
T *t = dynamic_cast<T *>(obj.release());
119136
return std::unique_ptr<T>(t);
120137
}
@@ -138,9 +155,9 @@ friend class BluetoothEventManager;
138155
* @return An object matching the name, identifier, parent or null if not found before
139156
* timeout expires or event is canceled.
140157
*/
141-
std::unique_ptr<BluetoothObject> find(BluetoothType type, std::string *name,
142-
std::string* identifier, BluetoothObject *parent,
143-
std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
158+
std::unique_ptr <BluetoothObject>
159+
find(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent,
160+
std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
144161

145162
/** Find a BluetoothObject of a type matching type. If parameters name,
146163
* identifier and parent are not null, the found object will have to
@@ -159,10 +176,9 @@ friend class BluetoothEventManager;
159176
* value of zero means wait forever. If object is not found during this time null will be returned.
160177
* @return It returns the BluetoothEvent generated by this function, allowing to manage the parameters or cancel the event.
161178
*/
162-
std::weak_ptr<BluetoothEvent> find(BluetoothType type, std::string *name,
163-
std::string* identifier, BluetoothObject *parent, BluetoothCallback cb,
164-
bool execute_once = true,
165-
std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
179+
std::weak_ptr <BluetoothEvent>
180+
find(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, BluetoothCallback cb,
181+
bool execute_once = true, std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
166182

167183
/** Return a BluetoothObject of a type matching type. If parameters name,
168184
* identifier and parent are not null, the returned object will have to
@@ -178,8 +194,8 @@ friend class BluetoothEventManager;
178194
* waiting for
179195
* @return An object matching the name, identifier, parent or null if not found.
180196
*/
181-
std::unique_ptr<BluetoothObject> get_object(BluetoothType type,
182-
std::string *name, std::string *identifier, BluetoothObject *parent);
197+
std::unique_ptr <BluetoothObject>
198+
get_object(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent);
183199

184200
/** Return a vector of BluetoothObject of a type matching type. If parameters name,
185201
* identifier and parent are not null, the returned object will have to
@@ -195,53 +211,44 @@ friend class BluetoothEventManager;
195211
* waiting for
196212
* @return A vector of object matching the name, identifier, parent.
197213
*/
198-
std::vector<std::unique_ptr<BluetoothObject>> get_objects(
199-
BluetoothType type = BluetoothType::NONE,
200-
std::string *name = nullptr, std::string *identifier = nullptr,
201-
BluetoothObject *parent = nullptr);
214+
std::vector <std::unique_ptr<BluetoothObject>>
215+
get_objects(BluetoothType type = BluetoothType::NONE, std::string *name = nullptr,
216+
std::string *identifier = nullptr, BluetoothObject *parent = nullptr);
202217

203218
/** Returns a list of BluetoothAdapters available in the system
204219
* @return A list of BluetoothAdapters available in the system
205220
*/
206-
std::vector<std::unique_ptr<BluetoothAdapter>> get_adapters(
207-
);
221+
std::vector <std::unique_ptr<BluetoothAdapter>> get_adapters();
208222

209223
/** Returns a list of discovered BluetoothDevices
210224
* @return A list of discovered BluetoothDevices
211225
*/
212-
std::vector<std::unique_ptr<BluetoothDevice>> get_devices(
213-
);
226+
std::vector <std::unique_ptr<BluetoothDevice>> get_devices();
214227

215228
/** Returns a list of available BluetoothGattServices
216229
* @return A list of available BluetoothGattServices
217230
*/
218-
std::vector<std::unique_ptr<BluetoothGattService>> get_services(
219-
);
231+
std::vector <std::unique_ptr<BluetoothGattService>> get_services();
220232

221233
/** Sets a default adapter to use for discovery.
222234
* @return TRUE if the device was set
223235
*/
224-
bool set_default_adapter(
225-
BluetoothAdapter &adapter
226-
);
236+
bool set_default_adapter(BluetoothAdapter &adapter);
227237

228-
std::unique_ptr<BluetoothAdapter> get_default_adapter();
238+
std::unique_ptr <BluetoothAdapter> get_default_adapter();
229239

230240
/** Turns on device discovery on the default adapter if it is disabled.
231241
* @return TRUE if discovery was successfully enabled
232242
*/
233-
bool start_discovery(
234-
);
243+
bool start_discovery();
235244

236245
/** Turns off device discovery on the default adapter if it is enabled.
237246
* @return TRUE if discovery was successfully disabled
238247
*/
239-
bool stop_discovery(
240-
);
248+
bool stop_discovery();
241249

242250
/** Returns if the discovers is running or not.
243251
* @return TRUE if discovery is running
244252
*/
245-
bool get_discovering(
246-
);
253+
bool get_discovering();
247254
};

java/BluetoothManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public static synchronized BluetoothManager getBluetoothManager() throws Runtime
303303
{
304304
String nativeAPIVersion = getNativeAPIVersion();
305305
String APIVersion = BluetoothManager.class.getPackage().getSpecificationVersion();
306-
if (APIVersion.equals(nativeAPIVersion) == false) {
306+
if (APIVersion != null && APIVersion.equals(nativeAPIVersion) == false) {
307307
String[] nativeAPIVersionCode = nativeAPIVersion.split("\\D");
308308
String[] APIVersionCode = APIVersion.split("\\D");
309309
if (APIVersionCode[0].equals(nativeAPIVersionCode[0]) == false) {

0 commit comments

Comments
 (0)