Skip to content

Commit

Permalink
BT hid navigation fix (flipperdevices#911)
Browse files Browse the repository at this point in the history
* bt: fix bt hid navigation
* Cli: change datetime format to more ISO-ish, add datetime validation.

Co-authored-by: Aleksandr Kutuzov <[email protected]>
  • Loading branch information
gornekich and skotopes authored Dec 17, 2021
1 parent e109e2e commit 93871f9
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 39 deletions.
9 changes: 1 addition & 8 deletions applications/bt/bt_hid_app/bt_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void bt_hid_dialog_callback(DialogExResult result, void* context) {
// TODO switch to Submenu after Media is done
view_dispatcher_stop(app->view_dispatcher);
} else if(result == DialogExResultRight) {
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id);
view_dispatcher_switch_to_view(app->view_dispatcher, BtHidViewKeynote);
}
}

Expand Down Expand Up @@ -56,9 +56,6 @@ void bt_hid_connection_status_changed_callback(BtStatus status, void* context) {
BtHid* bt_hid_app_alloc() {
BtHid* app = furi_alloc(sizeof(BtHid));

// Load Bluetooth settings
bt_settings_load(&app->bt_settings);

// Gui
app->gui = furi_record_open("gui");

Expand Down Expand Up @@ -156,10 +153,6 @@ int32_t bt_hid_app(void* p) {
view_dispatcher_run(app->view_dispatcher);

bt_set_status_changed_callback(app->bt, NULL, NULL);
// Stop advertising if bt was off
if(app->bt_settings.enabled) {
furi_hal_bt_stop_advertising();
}
// Change back profile to Serial
bt_set_profile(app->bt, BtProfileSerial);

Expand Down
2 changes: 0 additions & 2 deletions applications/bt/bt_hid_app/bt_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
#include <gui/view.h>
#include <gui/view_dispatcher.h>
#include <applications/notification/notification.h>
#include <applications/bt/bt_settings.h>

#include <gui/modules/submenu.h>
#include <gui/modules/dialog_ex.h>
#include "views/bt_hid_keynote.h"
#include "views/bt_hid_media.h"

typedef struct {
BtSettings bt_settings;
Bt* bt;
Gui* gui;
NotificationApp* notifications;
Expand Down
1 change: 1 addition & 0 deletions applications/bt/bt_service/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ static void bt_statusbar_update(Bt* bt) {
}

static void bt_change_profile(Bt* bt, BtMessage* message) {
bt_settings_load(&bt->bt_settings);
if(bt->profile == BtProfileSerial && bt->rpc_session) {
FURI_LOG_I(TAG, "Close RPC connection");
osEventFlagsSet(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
Expand Down
71 changes: 42 additions & 29 deletions applications/cli/cli_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <time.h>
#include <notification/notification-messages.h>

// Close to ISO, `date +'%Y-%m-%d %H:%M:%S %u'`
#define CLI_DATE_FORMAT "%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d"

void cli_command_device_info_callback(const char* key, const char* value, bool last, void* context) {
printf("%-24s: %s\r\n", key, value);
}
Expand Down Expand Up @@ -63,51 +66,61 @@ void cli_command_date(Cli* cli, string_t args, void* context) {
uint16_t hours, minutes, seconds, month, day, year, weekday;
int ret = sscanf(
string_get_cstr(args),
"%hu:%hu:%hu %hu-%hu-%hu %hu",
"%hu-%hu-%hu %hu:%hu:%hu %hu",
&year,
&month,
&day,
&hours,
&minutes,
&seconds,
&month,
&day,
&year,
&weekday);
if(ret == 7) {
datetime.hour = hours;
datetime.minute = minutes;
datetime.second = seconds;
datetime.weekday = weekday;
datetime.month = month;
datetime.day = day;
datetime.year = year;
furi_hal_rtc_set_datetime(&datetime);
// Verification
furi_hal_rtc_get_datetime(&datetime);
printf(
"New time is: %.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
datetime.hour,
datetime.minute,
datetime.second,
datetime.month,
datetime.day,
datetime.year,
datetime.weekday);
} else {

// Some variables are going to discard upper byte
// There will be some funky behaviour which is not breaking anything
datetime.hour = hours;
datetime.minute = minutes;
datetime.second = seconds;
datetime.weekday = weekday;
datetime.month = month;
datetime.day = day;
datetime.year = year;

if(ret != 7) {
printf(
"Invalid time format, use `hh:mm:ss MM-DD-YYYY WD`. sscanf %d %s",
"Invalid datetime format, use `%s`. sscanf %d %s",
"%Y-%m-%d %H:%M:%S %u",
ret,
string_get_cstr(args));
return;
}
} else {

if(!furi_hal_rtc_validate_datetime(&datetime)) {
printf("Invalid datetime data");
return;
}

furi_hal_rtc_set_datetime(&datetime);
// Verification
furi_hal_rtc_get_datetime(&datetime);
printf(
"%.2d:%.2d:%.2d %.2d-%.2d-%.2d %d",
"New datetime is: " CLI_DATE_FORMAT,
datetime.year,
datetime.month,
datetime.day,
datetime.hour,
datetime.minute,
datetime.second,
datetime.weekday);
} else {
furi_hal_rtc_get_datetime(&datetime);
printf(
CLI_DATE_FORMAT,
datetime.year,
datetime.month,
datetime.day,
datetime.year,
datetime.hour,
datetime.minute,
datetime.second,
datetime.weekday);
}
}
Expand Down
1 change: 1 addition & 0 deletions firmware/targets/f6/furi-hal/furi-hal-bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ bool furi_hal_bt_change_app(FuriHalBtProfile profile, BleEventCallback event_cb,
gap_thread_stop();
FURI_LOG_I(TAG, "Reset SHCI");
SHCI_C2_Reinit();
osDelay(100);
ble_glue_thread_stop();
FURI_LOG_I(TAG, "Start BT initialization");
furi_hal_bt_init();
Expand Down
22 changes: 22 additions & 0 deletions firmware/targets/f6/furi-hal/furi-hal-rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,25 @@ void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) {
datetime->day = __LL_RTC_CONVERT_BCD2BIN((date >> 16) & 0xFF);
datetime->weekday = __LL_RTC_CONVERT_BCD2BIN((date >> 24) & 0xFF);
}

bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime) {
bool invalid = false;

invalid |= (datetime->second > 59);
invalid |= (datetime->minute > 59);
invalid |= (datetime->hour > 23);

invalid |= (datetime->year < 2000);
invalid |= (datetime->year > 2099);

invalid |= (datetime->month == 0);
invalid |= (datetime->month > 12);

invalid |= (datetime->day == 0);
invalid |= (datetime->day > 31);

invalid |= (datetime->weekday == 0);
invalid |= (datetime->weekday > 7);

return !invalid;
}
1 change: 1 addition & 0 deletions firmware/targets/f7/furi-hal/furi-hal-bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ bool furi_hal_bt_change_app(FuriHalBtProfile profile, BleEventCallback event_cb,
gap_thread_stop();
FURI_LOG_I(TAG, "Reset SHCI");
SHCI_C2_Reinit();
osDelay(100);
ble_glue_thread_stop();
FURI_LOG_I(TAG, "Start BT initialization");
furi_hal_bt_init();
Expand Down
22 changes: 22 additions & 0 deletions firmware/targets/f7/furi-hal/furi-hal-rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,25 @@ void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) {
datetime->day = __LL_RTC_CONVERT_BCD2BIN((date >> 16) & 0xFF);
datetime->weekday = __LL_RTC_CONVERT_BCD2BIN((date >> 24) & 0xFF);
}

bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime) {
bool invalid = false;

invalid |= (datetime->second > 59);
invalid |= (datetime->minute > 59);
invalid |= (datetime->hour > 23);

invalid |= (datetime->year < 2000);
invalid |= (datetime->year > 2099);

invalid |= (datetime->month == 0);
invalid |= (datetime->month > 12);

invalid |= (datetime->day == 0);
invalid |= (datetime->day > 31);

invalid |= (datetime->weekday == 0);
invalid |= (datetime->weekday > 7);

return !invalid;
}
2 changes: 2 additions & 0 deletions firmware/targets/furi-hal-include/furi-hal-rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime);

void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime);

bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime);

#ifdef __cplusplus
}
#endif

0 comments on commit 93871f9

Please sign in to comment.