Skip to content

Commit 9a580e5

Browse files
committed
Fix NULL pointer dereference
Also, don't start a tracking thread on app launch: it should be started when the view is active.
1 parent f383c5f commit 9a580e5

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

views/bt_mouse.c

+49-45
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ void bt_mouse_connection_status_changed_callback(BtStatus status, void* context)
132132
BtMouse* bt_mouse = context;
133133

134134
bt_mouse->connected = (status == BtStatusConnected);
135+
if(!bt_mouse->notifications) {
136+
tracking_end();
137+
return;
138+
}
139+
135140
if(bt_mouse->connected) {
136141
notification_internal_message(bt_mouse->notifications, &sequence_set_blue_255);
137142
tracking_begin();
@@ -140,9 +145,6 @@ void bt_mouse_connection_status_changed_callback(BtStatus status, void* context)
140145
tracking_end();
141146
notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
142147
}
143-
144-
//with_view_model(
145-
// bt_mouse->view, void * model, { model->connected = connected; }, true);
146148
}
147149

148150
bool bt_mouse_move(int8_t dx, int8_t dy, void* context) {
@@ -160,46 +162,6 @@ bool bt_mouse_move(int8_t dx, int8_t dy, void* context) {
160162
return true;
161163
}
162164

163-
void bt_mouse_enter_callback(void* context) {
164-
furi_assert(context);
165-
BtMouse* bt_mouse = context;
166-
167-
bt_mouse->bt = furi_record_open(RECORD_BT);
168-
bt_mouse->notifications = furi_record_open(RECORD_NOTIFICATION);
169-
bt_set_status_changed_callback(
170-
bt_mouse->bt, bt_mouse_connection_status_changed_callback, bt_mouse);
171-
furi_assert(bt_set_profile(bt_mouse->bt, BtProfileHidKeyboard));
172-
furi_hal_bt_start_advertising();
173-
}
174-
175-
bool bt_mouse_custom_callback(uint32_t event, void* context) {
176-
UNUSED(event);
177-
furi_assert(context);
178-
BtMouse* bt_mouse = context;
179-
180-
tracking_step(bt_mouse_move, context);
181-
furi_delay_ms(3); // Magic! Removing this will break the buttons
182-
183-
view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
184-
return true;
185-
}
186-
187-
void bt_mouse_exit_callback(void* context) {
188-
furi_assert(context);
189-
BtMouse* bt_mouse = context;
190-
191-
tracking_end();
192-
notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
193-
194-
furi_hal_bt_stop_advertising();
195-
bt_set_profile(bt_mouse->bt, BtProfileSerial);
196-
197-
furi_record_close(RECORD_NOTIFICATION);
198-
bt_mouse->notifications = NULL;
199-
furi_record_close(RECORD_BT);
200-
bt_mouse->bt = NULL;
201-
}
202-
203165
static int8_t clamp(int t) {
204166
if(t < -128) {
205167
return -128;
@@ -279,6 +241,50 @@ void bt_mouse_thread_stop(BtMouse* bt_mouse) {
279241
furi_thread_join(bt_mouse->thread);
280242
furi_thread_free(bt_mouse->thread);
281243
furi_mutex_free(bt_mouse->mutex);
244+
bt_mouse->mutex = NULL;
245+
bt_mouse->thread = NULL;
246+
}
247+
248+
void bt_mouse_enter_callback(void* context) {
249+
furi_assert(context);
250+
BtMouse* bt_mouse = context;
251+
252+
bt_mouse->bt = furi_record_open(RECORD_BT);
253+
bt_mouse->notifications = furi_record_open(RECORD_NOTIFICATION);
254+
bt_set_status_changed_callback(
255+
bt_mouse->bt, bt_mouse_connection_status_changed_callback, bt_mouse);
256+
furi_assert(bt_set_profile(bt_mouse->bt, BtProfileHidKeyboard));
257+
furi_hal_bt_start_advertising();
258+
bt_mouse_thread_start(bt_mouse);
259+
}
260+
261+
bool bt_mouse_custom_callback(uint32_t event, void* context) {
262+
UNUSED(event);
263+
furi_assert(context);
264+
BtMouse* bt_mouse = context;
265+
266+
tracking_step(bt_mouse_move, context);
267+
furi_delay_ms(3); // Magic! Removing this will break the buttons
268+
269+
view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
270+
return true;
271+
}
272+
273+
void bt_mouse_exit_callback(void* context) {
274+
furi_assert(context);
275+
BtMouse* bt_mouse = context;
276+
277+
bt_mouse_thread_stop(bt_mouse);
278+
tracking_end();
279+
notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
280+
281+
furi_hal_bt_stop_advertising();
282+
bt_set_profile(bt_mouse->bt, BtProfileSerial);
283+
284+
furi_record_close(RECORD_NOTIFICATION);
285+
bt_mouse->notifications = NULL;
286+
furi_record_close(RECORD_BT);
287+
bt_mouse->bt = NULL;
282288
}
283289

284290
BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher) {
@@ -293,13 +299,11 @@ BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher) {
293299
view_set_enter_callback(bt_mouse->view, bt_mouse_enter_callback);
294300
view_set_custom_callback(bt_mouse->view, bt_mouse_custom_callback);
295301
view_set_exit_callback(bt_mouse->view, bt_mouse_exit_callback);
296-
bt_mouse_thread_start(bt_mouse);
297302
return bt_mouse;
298303
}
299304

300305
void bt_mouse_free(BtMouse* bt_mouse) {
301306
furi_assert(bt_mouse);
302-
bt_mouse_thread_stop(bt_mouse);
303307
view_free(bt_mouse->view);
304308
free(bt_mouse);
305309
}

0 commit comments

Comments
 (0)