Merge branch 'ofw-dev' into dev

This commit is contained in:
MX 2023-07-12 21:19:16 +03:00
commit c4b04a06bb
No known key found for this signature in database
GPG key ID: 7CCC66B7DBDD1C83
68 changed files with 431 additions and 375 deletions

View file

@ -87,6 +87,7 @@ static bool subghz_device_cc1101_ext_check_init() {
subghz_device_cc1101_ext->state = SubGhzDeviceCC1101ExtStateIdle;
bool ret = false;
CC1101Status cc1101_status = {0};
furi_hal_spi_acquire(subghz_device_cc1101_ext->spi_bus_handle);
FuriHalCortexTimer timer = furi_hal_cortex_timer_get(100 * 1000);
@ -94,16 +95,34 @@ static bool subghz_device_cc1101_ext_check_init() {
// Reset
furi_hal_gpio_init(
subghz_device_cc1101_ext->g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
cc1101_reset(subghz_device_cc1101_ext->spi_bus_handle);
cc1101_write_reg(
subghz_device_cc1101_ext->spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
furi_hal_gpio_init(
subghz_device_cc1101_ext->spi_bus_handle->miso,
GpioModeInput,
GpioPullUp,
GpioSpeedLow);
cc1101_status = cc1101_reset(subghz_device_cc1101_ext->spi_bus_handle);
if(cc1101_status.CHIP_RDYn != 0) {
//timeout or error
break;
}
cc1101_status = cc1101_write_reg(
subghz_device_cc1101_ext->spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
if(cc1101_status.CHIP_RDYn != 0) {
//timeout or error
break;
}
// Prepare GD0 for power on self test
furi_hal_gpio_init(
subghz_device_cc1101_ext->g0_pin, GpioModeInput, GpioPullNo, GpioSpeedLow);
subghz_device_cc1101_ext->g0_pin, GpioModeInput, GpioPullUp, GpioSpeedLow);
// GD0 low
cc1101_write_reg(subghz_device_cc1101_ext->spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHW);
cc1101_status = cc1101_write_reg(
subghz_device_cc1101_ext->spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHW);
if(cc1101_status.CHIP_RDYn != 0) {
//timeout or error
break;
}
while(furi_hal_gpio_read(subghz_device_cc1101_ext->g0_pin) != false) {
if(furi_hal_cortex_timer_is_expired(timer)) {
//timeout
@ -116,10 +135,16 @@ static bool subghz_device_cc1101_ext_check_init() {
}
// GD0 high
cc1101_write_reg(
furi_hal_gpio_init(
subghz_device_cc1101_ext->g0_pin, GpioModeInput, GpioPullDown, GpioSpeedLow);
cc1101_status = cc1101_write_reg(
subghz_device_cc1101_ext->spi_bus_handle,
CC1101_IOCFG0,
CC1101IocfgHW | CC1101_IOCFG_INV);
if(cc1101_status.CHIP_RDYn != 0) {
//timeout or error
break;
}
while(furi_hal_gpio_read(subghz_device_cc1101_ext->g0_pin) != true) {
if(furi_hal_cortex_timer_is_expired(timer)) {
//timeout
@ -132,17 +157,21 @@ static bool subghz_device_cc1101_ext_check_init() {
}
// Reset GD0 to floating state
cc1101_write_reg(
cc1101_status = cc1101_write_reg(
subghz_device_cc1101_ext->spi_bus_handle, CC1101_IOCFG0, CC1101IocfgHighImpedance);
if(cc1101_status.CHIP_RDYn != 0) {
//timeout or error
break;
}
furi_hal_gpio_init(
subghz_device_cc1101_ext->g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
// RF switches
furi_hal_gpio_init(&gpio_rf_sw_0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
cc1101_write_reg(subghz_device_cc1101_ext->spi_bus_handle, CC1101_IOCFG2, CC1101IocfgHW);
// Go to sleep
cc1101_shutdown(subghz_device_cc1101_ext->spi_bus_handle);
cc1101_status = cc1101_shutdown(subghz_device_cc1101_ext->spi_bus_handle);
if(cc1101_status.CHIP_RDYn != 0) {
//timeout or error
break;
}
ret = true;
} while(false);
@ -152,6 +181,8 @@ static bool subghz_device_cc1101_ext_check_init() {
FURI_LOG_I(TAG, "Init OK");
} else {
FURI_LOG_E(TAG, "Init failed");
furi_hal_gpio_init(
subghz_device_cc1101_ext->g0_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}
return ret;
}

View file

@ -70,16 +70,23 @@ static void archive_list_item_cb(
if(!is_last) {
archive_add_file_item(browser, is_folder, furi_string_get_cstr(item_path));
} else {
bool load_again = false;
with_view_model(
browser->view,
ArchiveBrowserViewModel * model,
{
model->list_loading = false;
if(model->item_cnt <= BROWSER_SORT_THRESHOLD) {
files_array_sort(model->files);
}
model->list_loading = false;
if(archive_is_file_list_load_required(model)) {
load_again = true;
}
},
true);
if(load_again) {
archive_file_array_load(browser, 0);
}
}
}
@ -125,6 +132,26 @@ bool archive_is_item_in_array(ArchiveBrowserViewModel* model, uint32_t idx) {
return true;
}
bool archive_is_file_list_load_required(ArchiveBrowserViewModel* model) {
size_t array_size = files_array_size(model->files);
if((model->list_loading) || (array_size >= model->item_cnt)) {
return false;
}
if((model->array_offset > 0) &&
(model->item_idx < (model->array_offset + FILE_LIST_BUF_LEN / 4))) {
return true;
}
if(((model->array_offset + array_size) < model->item_cnt) &&
(model->item_idx > (int32_t)(model->array_offset + array_size - FILE_LIST_BUF_LEN / 4))) {
return true;
}
return false;
}
void archive_update_offset(ArchiveBrowserView* browser) {
furi_assert(browser);

View file

@ -66,6 +66,7 @@ inline bool archive_is_known_app(ArchiveFileTypeEnum type) {
}
bool archive_is_item_in_array(ArchiveBrowserViewModel* model, uint32_t idx);
bool archive_is_file_list_load_required(ArchiveBrowserViewModel* model);
void archive_update_offset(ArchiveBrowserView* browser);
void archive_update_focus(ArchiveBrowserView* browser, const char* target);

View file

@ -370,24 +370,10 @@ View* archive_browser_get_view(ArchiveBrowserView* browser) {
return browser->view;
}
static bool is_file_list_load_required(ArchiveBrowserViewModel* model) {
size_t array_size = files_array_size(model->files);
if((model->list_loading) || (array_size >= model->item_cnt)) {
return false;
static void file_list_rollover(ArchiveBrowserViewModel* model) {
if(!model->list_loading && files_array_size(model->files) < model->item_cnt) {
files_array_reset(model->files);
}
if((model->array_offset > 0) &&
(model->item_idx < (model->array_offset + FILE_LIST_BUF_LEN / 4))) {
return true;
}
if(((model->array_offset + array_size) < model->item_cnt) &&
(model->item_idx > (int32_t)(model->array_offset + array_size - FILE_LIST_BUF_LEN / 4))) {
return true;
}
return false;
}
static bool archive_view_input(InputEvent* event, void* context) {
@ -477,12 +463,13 @@ static bool archive_view_input(InputEvent* event, void* context) {
if(model->item_idx < scroll_speed) {
model->button_held_for_ticks = 0;
model->item_idx = model->item_cnt - 1;
file_list_rollover(model);
} else {
model->item_idx =
((model->item_idx - scroll_speed) + model->item_cnt) %
model->item_cnt;
}
if(is_file_list_load_required(model)) {
if(archive_is_file_list_load_required(model)) {
model->list_loading = true;
browser->callback(ArchiveBrowserEventLoadPrevItems, browser->context);
}
@ -496,10 +483,11 @@ static bool archive_view_input(InputEvent* event, void* context) {
if(model->item_idx + scroll_speed >= count) {
model->button_held_for_ticks = 0;
model->item_idx = 0;
file_list_rollover(model);
} else {
model->item_idx = (model->item_idx + scroll_speed) % model->item_cnt;
}
if(is_file_list_load_required(model)) {
if(archive_is_file_list_load_required(model)) {
model->list_loading = true;
browser->callback(ArchiveBrowserEventLoadNextItems, browser->context);
}

View file

@ -28,12 +28,20 @@
// Rx RAW | only internal module
// Chat | both
#define TAG "SubGhz CLI"
static void subghz_cli_radio_device_power_on() {
uint8_t attempts = 0;
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
furi_hal_power_enable_otg();
//CC1101 power-up time
furi_delay_ms(10);
uint8_t attempts = 5;
while(--attempts > 0) {
if(furi_hal_power_enable_otg()) break;
}
if(attempts == 0) {
if(furi_hal_power_get_usb_voltage() < 4.5f) {
FURI_LOG_E(
"TAG",
"Error power otg enable. BQ2589 check otg fault = %d",
furi_hal_power_check_otg_fault() ? 1 : 0);
}
}
}
@ -126,9 +134,9 @@ void subghz_cli_command_rx_carrier(Cli* cli, FuriString* args, void* context) {
furi_hal_subghz_sleep();
}
static const SubGhzDevice* subghz_cli_command_get_device(uint32_t device_ind) {
static const SubGhzDevice* subghz_cli_command_get_device(uint32_t* device_ind) {
const SubGhzDevice* device = NULL;
switch(device_ind) {
switch(*device_ind) {
case 1:
subghz_cli_radio_device_power_on();
device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
@ -138,6 +146,12 @@ static const SubGhzDevice* subghz_cli_command_get_device(uint32_t device_ind) {
device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
break;
}
//check if the device is connected
if(!subghz_devices_is_connect(device)) {
subghz_cli_radio_device_power_off();
device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
*device_ind = 0;
}
return device;
}
@ -175,7 +189,7 @@ void subghz_cli_command_tx(Cli* cli, FuriString* args, void* context) {
}
}
subghz_devices_init();
const SubGhzDevice* device = subghz_cli_command_get_device(device_ind);
const SubGhzDevice* device = subghz_cli_command_get_device(&device_ind);
if(!subghz_devices_is_frequency_valid(device, frequency)) {
printf(
"Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n", frequency);
@ -294,7 +308,7 @@ void subghz_cli_command_rx(Cli* cli, FuriString* args, void* context) {
}
}
subghz_devices_init();
const SubGhzDevice* device = subghz_cli_command_get_device(device_ind);
const SubGhzDevice* device = subghz_cli_command_get_device(&device_ind);
if(!subghz_devices_is_frequency_valid(device, frequency)) {
printf(
"Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n", frequency);
@ -681,7 +695,7 @@ static void subghz_cli_command_chat(Cli* cli, FuriString* args) {
}
}
subghz_devices_init();
const SubGhzDevice* device = subghz_cli_command_get_device(device_ind);
const SubGhzDevice* device = subghz_cli_command_get_device(&device_ind);
if(!subghz_devices_is_frequency_valid(device, frequency)) {
printf(
"Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n", frequency);

View file

@ -335,6 +335,12 @@ static bool browser_is_list_load_required(FileBrowserModel* model) {
return false;
}
static void browser_list_rollover(FileBrowserModel* model) {
if(!model->list_loading && items_array_size(model->items) < model->item_cnt) {
items_array_reset(model->items);
}
}
static void browser_update_offset(FileBrowser* browser) {
furi_assert(browser);
@ -417,7 +423,7 @@ static void browser_list_load_cb(void* context, uint32_t list_load_offset) {
}
}
},
true);
false);
BrowserItem_t_clear(&back_item);
}
@ -462,14 +468,15 @@ static void browser_list_item_cb(
(browser->hide_ext) && (item.type == BrowserItemTypeFile));
}
// We shouldn't update screen on each item if custom callback is not set
// Otherwise it will cause screen flickering
bool instant_update = (browser->item_callback != NULL);
with_view_model(
browser->view,
FileBrowserModel * model,
{
items_array_push_back(model->items, item);
// TODO: calculate if element is visible
},
false);
{ items_array_push_back(model->items, item); },
instant_update);
furi_string_free(item.display_name);
furi_string_free(item.path);
if(item.custom_icon_data) {
@ -674,11 +681,13 @@ static bool file_browser_view_input_callback(InputEvent* event, void* context) {
if(model->item_idx < scroll_speed) {
model->button_held_for_ticks = 0;
model->item_idx = model->item_cnt - 1;
browser_list_rollover(model);
} else {
model->item_idx =
((model->item_idx - scroll_speed) + model->item_cnt) %
model->item_cnt;
}
if(browser_is_list_load_required(model)) {
model->list_loading = true;
int32_t load_offset = CLAMP(
@ -692,13 +701,14 @@ static bool file_browser_view_input_callback(InputEvent* event, void* context) {
model->button_held_for_ticks += 1;
} else if(event->key == InputKeyDown) {
int32_t count = model->item_cnt;
if(model->item_idx + scroll_speed >= count) {
if(model->item_idx + scroll_speed >= (int32_t)model->item_cnt) {
model->button_held_for_ticks = 0;
model->item_idx = 0;
browser_list_rollover(model);
} else {
model->item_idx = (model->item_idx + scroll_speed) % model->item_cnt;
}
if(browser_is_list_load_required(model)) {
model->list_loading = true;
int32_t load_offset = CLAMP(

View file

@ -38,6 +38,11 @@ typedef struct {
FuriString* fap_path;
DialogsApp* dialogs;
Storage* storage;
Loader* loader;
Gui* gui;
ViewHolder* view_holder;
Loading* loading;
} LoaderApplicationsApp;
static LoaderApplicationsApp* loader_applications_app_alloc() {
@ -45,15 +50,30 @@ static LoaderApplicationsApp* loader_applications_app_alloc() {
app->fap_path = furi_string_alloc_set(EXT_PATH("apps"));
app->dialogs = furi_record_open(RECORD_DIALOGS);
app->storage = furi_record_open(RECORD_STORAGE);
app->loader = furi_record_open(RECORD_LOADER);
app->gui = furi_record_open(RECORD_GUI);
app->view_holder = view_holder_alloc();
app->loading = loading_alloc();
view_holder_attach_to_gui(app->view_holder, app->gui);
view_holder_set_view(app->view_holder, loading_get_view(app->loading));
return app;
} //-V773
static void loader_applications_app_free(LoaderApplicationsApp* loader_applications_app) {
furi_assert(loader_applications_app);
static void loader_applications_app_free(LoaderApplicationsApp* app) {
furi_assert(app);
view_holder_free(app->view_holder);
loading_free(app->loading);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_LOADER);
furi_record_close(RECORD_DIALOGS);
furi_record_close(RECORD_STORAGE);
furi_string_free(loader_applications_app->fap_path);
free(loader_applications_app);
furi_string_free(app->fap_path);
free(app);
}
static bool loader_applications_item_callback(
@ -96,47 +116,38 @@ static void loader_pubsub_callback(const void* message, void* context) {
}
}
static void loader_applications_start_app(const char* name) {
// start loading animation
Gui* gui = furi_record_open(RECORD_GUI);
ViewHolder* view_holder = view_holder_alloc();
Loading* loading = loading_alloc();
view_holder_attach_to_gui(view_holder, gui);
view_holder_set_view(view_holder, loading_get_view(loading));
view_holder_start(view_holder);
static void loader_applications_start_app(LoaderApplicationsApp* app) {
const char* name = furi_string_get_cstr(app->fap_path);
// load app
FuriThreadId thread_id = furi_thread_get_current_id();
Loader* loader = furi_record_open(RECORD_LOADER);
FuriPubSubSubscription* subscription =
furi_pubsub_subscribe(loader_get_pubsub(loader), loader_pubsub_callback, thread_id);
furi_pubsub_subscribe(loader_get_pubsub(app->loader), loader_pubsub_callback, thread_id);
LoaderStatus status = loader_start_with_gui_error(loader, name, NULL);
LoaderStatus status = loader_start_with_gui_error(app->loader, name, NULL);
if(status == LoaderStatusOk) {
furi_thread_flags_wait(APPLICATION_STOP_EVENT, FuriFlagWaitAny, FuriWaitForever);
}
furi_pubsub_unsubscribe(loader_get_pubsub(loader), subscription);
furi_record_close(RECORD_LOADER);
// stop loading animation
view_holder_stop(view_holder);
view_holder_free(view_holder);
loading_free(loading);
furi_record_close(RECORD_GUI);
furi_pubsub_unsubscribe(loader_get_pubsub(app->loader), subscription);
}
static int32_t loader_applications_thread(void* p) {
LoaderApplications* loader_applications = p;
LoaderApplicationsApp* loader_applications_app = loader_applications_app_alloc();
LoaderApplicationsApp* app = loader_applications_app_alloc();
while(loader_applications_select_app(loader_applications_app)) {
loader_applications_start_app(furi_string_get_cstr(loader_applications_app->fap_path));
// start loading animation
view_holder_start(app->view_holder);
while(loader_applications_select_app(app)) {
loader_applications_start_app(app);
}
loader_applications_app_free(loader_applications_app);
// stop loading animation
view_holder_stop(app->view_holder);
loader_applications_app_free(app);
if(loader_applications->closed_cb) {
loader_applications->closed_cb(loader_applications->context);

View file

@ -19,8 +19,7 @@ struct File {
FileType type;
FS_Error error_id; /**< Standard API error from FS_Error enum */
int32_t internal_error_id; /**< Internal API error value */
void* storage; /**< Storage API pointer */
void* sort_data; /**< Sorted file list for directory */
void* storage;
};
/** File api structure

View file

@ -1,5 +1,4 @@
#include "storage_processing.h"
#include "storage_sorting.h"
#include <m-list.h>
#include <m-dict.h>
@ -101,7 +100,7 @@ static FS_Error storage_get_data(Storage* app, FuriString* path, StorageData** s
/******************* File Functions *******************/
static bool storage_process_file_open(
bool storage_process_file_open(
Storage* app,
File* file,
FuriString* path,
@ -128,7 +127,7 @@ static bool storage_process_file_open(
return ret;
}
static bool storage_process_file_close(Storage* app, File* file) {
bool storage_process_file_close(Storage* app, File* file) {
bool ret = false;
StorageData* storage = get_storage_by_file(file, app->storage);
@ -261,149 +260,9 @@ static bool storage_process_file_eof(Storage* app, File* file) {
return ret;
}
/*************** Sorting Dir Functions ***************/
static bool storage_process_dir_rewind_internal(StorageData* storage, File* file);
static bool storage_process_dir_read_internal(
StorageData* storage,
File* file,
FileInfo* fileinfo,
char* name,
const uint16_t name_length);
static int storage_sorted_file_record_compare(const void* sorted_a, const void* sorted_b) {
SortedFileRecord* a = (SortedFileRecord*)sorted_a;
SortedFileRecord* b = (SortedFileRecord*)sorted_b;
if(a->info.flags & FSF_DIRECTORY && !(b->info.flags & FSF_DIRECTORY))
return -1;
else if(!(a->info.flags & FSF_DIRECTORY) && b->info.flags & FSF_DIRECTORY)
return 1;
else
return furi_string_cmpi(a->name, b->name);
}
static bool storage_sorted_dir_read_next(
SortedDir* dir,
FileInfo* fileinfo,
char* name,
const uint16_t name_length) {
bool ret = false;
if(dir->index < dir->count) {
SortedFileRecord* sorted = &dir->sorted[dir->index];
if(fileinfo) {
*fileinfo = sorted->info;
}
if(name) {
strncpy(name, furi_string_get_cstr(sorted->name), name_length);
}
dir->index++;
ret = true;
}
return ret;
}
static void storage_sorted_dir_rewind(SortedDir* dir) {
dir->index = 0;
}
static bool storage_sorted_dir_prepare(SortedDir* dir, StorageData* storage, File* file) {
bool ret = true;
dir->count = 0;
dir->index = 0;
FileInfo info;
char name[SORTING_MAX_NAME_LENGTH + 1] = {0};
furi_check(!dir->sorted);
while(storage_process_dir_read_internal(storage, file, &info, name, SORTING_MAX_NAME_LENGTH)) {
if(memmgr_get_free_heap() < SORTING_MIN_FREE_MEMORY) {
ret = false;
break;
}
if(dir->count == 0) { //-V547
dir->sorted = malloc(sizeof(SortedFileRecord));
} else {
// Our realloc actually mallocs a new block and copies the data over,
// so we need to check if we have enough memory for the new block
size_t size = sizeof(SortedFileRecord) * (dir->count + 1);
if(memmgr_heap_get_max_free_block() >= size) {
dir->sorted =
realloc(dir->sorted, sizeof(SortedFileRecord) * (dir->count + 1)); //-V701
} else {
ret = false;
break;
}
}
dir->sorted[dir->count].name = furi_string_alloc_set(name);
dir->sorted[dir->count].info = info;
dir->count++;
}
return ret;
}
static void storage_sorted_dir_sort(SortedDir* dir) {
qsort(dir->sorted, dir->count, sizeof(SortedFileRecord), storage_sorted_file_record_compare);
}
static void storage_sorted_dir_clear_data(SortedDir* dir) {
if(dir->sorted != NULL) {
for(size_t i = 0; i < dir->count; i++) {
furi_string_free(dir->sorted[i].name);
}
free(dir->sorted);
dir->sorted = NULL;
}
}
static void storage_file_remove_sort_data(File* file) {
if(file->sort_data != NULL) {
storage_sorted_dir_clear_data(file->sort_data);
free(file->sort_data);
file->sort_data = NULL;
}
}
static void storage_file_add_sort_data(File* file, StorageData* storage) {
file->sort_data = malloc(sizeof(SortedDir));
if(storage_sorted_dir_prepare(file->sort_data, storage, file)) {
storage_sorted_dir_sort(file->sort_data);
} else {
storage_file_remove_sort_data(file);
storage_process_dir_rewind_internal(storage, file);
}
}
static bool storage_file_has_sort_data(File* file) {
return file->sort_data != NULL;
}
/******************* Dir Functions *******************/
static bool storage_process_dir_read_internal(
StorageData* storage,
File* file,
FileInfo* fileinfo,
char* name,
const uint16_t name_length) {
bool ret = false;
FS_CALL(storage, dir.read(storage, file, fileinfo, name, name_length));
return ret;
}
static bool storage_process_dir_rewind_internal(StorageData* storage, File* file) {
bool ret = false;
FS_CALL(storage, dir.rewind(storage, file));
return ret;
}
static bool storage_process_dir_open(Storage* app, File* file, FuriString* path) {
bool storage_process_dir_open(Storage* app, File* file, FuriString* path) {
bool ret = false;
StorageData* storage;
file->error_id = storage_get_data(app, path, &storage);
@ -414,17 +273,13 @@ static bool storage_process_dir_open(Storage* app, File* file, FuriString* path)
} else {
storage_push_storage_file(file, path, storage);
FS_CALL(storage, dir.open(storage, file, cstr_path_without_vfs_prefix(path)));
if(file->error_id == FSE_OK) {
storage_file_add_sort_data(file, storage);
}
}
}
return ret;
}
static bool storage_process_dir_close(Storage* app, File* file) {
bool storage_process_dir_close(Storage* app, File* file) {
bool ret = false;
StorageData* storage = get_storage_by_file(file, app->storage);
@ -432,7 +287,6 @@ static bool storage_process_dir_close(Storage* app, File* file) {
file->error_id = FSE_INVALID_PARAMETER;
} else {
FS_CALL(storage, dir.close(storage, file));
storage_file_remove_sort_data(file);
storage_pop_storage_file(file, storage);
StorageEvent event = {.type = StorageEventTypeDirClose};
@ -442,7 +296,7 @@ static bool storage_process_dir_close(Storage* app, File* file) {
return ret;
}
static bool storage_process_dir_read(
bool storage_process_dir_read(
Storage* app,
File* file,
FileInfo* fileinfo,
@ -454,34 +308,20 @@ static bool storage_process_dir_read(
if(storage == NULL) {
file->error_id = FSE_INVALID_PARAMETER;
} else {
if(storage_file_has_sort_data(file)) {
ret = storage_sorted_dir_read_next(file->sort_data, fileinfo, name, name_length);
if(ret) {
file->error_id = FSE_OK;
} else {
file->error_id = FSE_NOT_EXIST;
}
} else {
ret = storage_process_dir_read_internal(storage, file, fileinfo, name, name_length);
}
FS_CALL(storage, dir.read(storage, file, fileinfo, name, name_length));
}
return ret;
}
static bool storage_process_dir_rewind(Storage* app, File* file) {
bool storage_process_dir_rewind(Storage* app, File* file) {
bool ret = false;
StorageData* storage = get_storage_by_file(file, app->storage);
if(storage == NULL) {
file->error_id = FSE_INVALID_PARAMETER;
} else {
if(storage_file_has_sort_data(file)) {
storage_sorted_dir_rewind(file->sort_data);
ret = true;
} else {
ret = storage_process_dir_rewind_internal(storage, file);
}
FS_CALL(storage, dir.rewind(storage, file));
}
return ret;
@ -621,7 +461,7 @@ static FS_Error storage_process_sd_status(Storage* app) {
/******************** Aliases processing *******************/
static void storage_process_alias(
void storage_process_alias(
Storage* app,
FuriString* path,
FuriThreadId thread_id,
@ -665,7 +505,7 @@ static void storage_process_alias(
/****************** API calls processing ******************/
static void storage_process_message_internal(Storage* app, StorageMessage* message) {
void storage_process_message_internal(Storage* app, StorageMessage* message) {
FuriString* path = NULL;
switch(message->command) {

View file

@ -1,22 +0,0 @@
#pragma once
#include <furi.h>
#define SORTING_MAX_NAME_LENGTH 255
#define SORTING_MIN_FREE_MEMORY (1024 * 40)
/**
* @brief Sorted file record, holds file name and info
*/
typedef struct {
FuriString* name;
FileInfo info;
} SortedFileRecord;
/**
* @brief Sorted directory, holds sorted file records, count and current index
*/
typedef struct {
SortedFileRecord* sorted;
size_t count;
size_t index;
} SortedDir;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 990 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 939 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 831 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 623 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 928 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,019 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,32 @@
Filetype: Flipper Animation
Version: 1
Width: 128
Height: 64
Passive frames: 19
Active frames: 51
Frames order: 0 1 2 3 4 5 6 0 1 2 7 8 9 10 11 12 7 8 9 13 14 15 14 13 14 15 7 8 9 16 17 18 13 14 19 20 21 22 23 24 21 25 26 27 28 29 30 31 32 33 32 34 35 36 35 34 37 38 39 40 41 42 43 44 45 46 17 47 48 7
Active cycles: 1
Frame rate: 2
Duration: 3600
Active cooldown: 7
Bubble slots: 1
Slot: 0
X: 41
Y: 43
Text: My dude
AlignH: Right
AlignV: Top
StartFrame: 50
EndFrame: 50
Slot: 0
X: 59
Y: 43
Text: My dude
AlignH: Left
AlignV: Top
StartFrame: 54
EndFrame: 54

View file

@ -97,6 +97,13 @@ Min butthurt: 0
Max butthurt: 10
Min level: 1
Max level: 3
Weight: 3
Name: L1_My_dude_128x64
Min butthurt: 0
Max butthurt: 8
Min level: 1
Max level: 3
Weight: 4
Name: L2_Wake_up_128x64

View file

@ -192,6 +192,52 @@ inline static void furi_hal_spi_bus_r_handle_event_callback(
}
}
inline static void furi_hal_spi_bus_external_handle_event_callback(
FuriHalSpiBusHandle* handle,
FuriHalSpiBusHandleEvent event,
const LL_SPI_InitTypeDef* preset) {
if(event == FuriHalSpiBusHandleEventInit) {
furi_hal_gpio_write(handle->cs, true);
furi_hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
} else if(event == FuriHalSpiBusHandleEventDeinit) {
furi_hal_gpio_write(handle->cs, true);
furi_hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
} else if(event == FuriHalSpiBusHandleEventActivate) {
LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
LL_SPI_Enable(handle->bus->spi);
furi_hal_gpio_init_ex(
handle->miso,
GpioModeAltFunctionPushPull,
GpioPullDown,
GpioSpeedVeryHigh,
GpioAltFn5SPI1);
furi_hal_gpio_init_ex(
handle->mosi,
GpioModeAltFunctionPushPull,
GpioPullDown,
GpioSpeedVeryHigh,
GpioAltFn5SPI1);
furi_hal_gpio_init_ex(
handle->sck,
GpioModeAltFunctionPushPull,
GpioPullDown,
GpioSpeedVeryHigh,
GpioAltFn5SPI1);
furi_hal_gpio_write(handle->cs, false);
} else if(event == FuriHalSpiBusHandleEventDeactivate) {
furi_hal_gpio_write(handle->cs, true);
furi_hal_gpio_init(handle->miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(handle->mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_init(handle->sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
LL_SPI_Disable(handle->bus->spi);
}
}
inline static void furi_hal_spi_bus_nfc_handle_event_callback(
FuriHalSpiBusHandle* handle,
FuriHalSpiBusHandleEvent event,
@ -291,7 +337,8 @@ FuriHalSpiBusHandle furi_hal_spi_bus_handle_nfc = {
static void furi_hal_spi_bus_handle_external_event_callback(
FuriHalSpiBusHandle* handle,
FuriHalSpiBusHandleEvent event) {
furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
furi_hal_spi_bus_external_handle_event_callback(
handle, event, &furi_hal_spi_preset_1edge_low_2m);
}
FuriHalSpiBusHandle furi_hal_spi_bus_handle_external = {

View file

@ -1,14 +1,27 @@
#include "cc1101.h"
#include <assert.h>
#include <string.h>
#include <furi_hal_cortex.h>
static bool cc1101_spi_trx(FuriHalSpiBusHandle* handle, uint8_t* tx, uint8_t* rx, uint8_t size) {
FuriHalCortexTimer timer = furi_hal_cortex_timer_get(CC1101_TIMEOUT * 1000);
while(furi_hal_gpio_read(handle->miso)) {
if(furi_hal_cortex_timer_is_expired(timer)) {
//timeout
return false;
}
}
if(!furi_hal_spi_bus_trx(handle, tx, rx, size, CC1101_TIMEOUT)) return false;
return true;
}
CC1101Status cc1101_strobe(FuriHalSpiBusHandle* handle, uint8_t strobe) {
uint8_t tx[1] = {strobe};
CC1101Status rx[1] = {0};
rx[0].CHIP_RDYn = 1;
while(furi_hal_gpio_read(handle->miso))
;
furi_hal_spi_bus_trx(handle, tx, (uint8_t*)rx, 1, CC1101_TIMEOUT);
cc1101_spi_trx(handle, tx, (uint8_t*)rx, 1);
assert(rx[0].CHIP_RDYn == 0);
return rx[0];
@ -17,10 +30,10 @@ CC1101Status cc1101_strobe(FuriHalSpiBusHandle* handle, uint8_t strobe) {
CC1101Status cc1101_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data) {
uint8_t tx[2] = {reg, data};
CC1101Status rx[2] = {0};
rx[0].CHIP_RDYn = 1;
rx[1].CHIP_RDYn = 1;
while(furi_hal_gpio_read(handle->miso))
;
furi_hal_spi_bus_trx(handle, tx, (uint8_t*)rx, 2, CC1101_TIMEOUT);
cc1101_spi_trx(handle, tx, (uint8_t*)rx, 2);
assert((rx[0].CHIP_RDYn | rx[1].CHIP_RDYn) == 0);
return rx[1];
@ -30,10 +43,9 @@ CC1101Status cc1101_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t*
assert(sizeof(CC1101Status) == 1);
uint8_t tx[2] = {reg | CC1101_READ, 0};
CC1101Status rx[2] = {0};
rx[0].CHIP_RDYn = 1;
while(furi_hal_gpio_read(handle->miso))
;
furi_hal_spi_bus_trx(handle, tx, (uint8_t*)rx, 2, CC1101_TIMEOUT);
cc1101_spi_trx(handle, tx, (uint8_t*)rx, 2);
assert((rx[0].CHIP_RDYn) == 0);
*data = *(uint8_t*)&rx[1];
@ -58,40 +70,40 @@ uint8_t cc1101_get_rssi(FuriHalSpiBusHandle* handle) {
return rssi;
}
void cc1101_reset(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SRES);
CC1101Status cc1101_reset(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SRES);
}
CC1101Status cc1101_get_status(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SNOP);
}
void cc1101_shutdown(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SPWD);
CC1101Status cc1101_shutdown(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SPWD);
}
void cc1101_calibrate(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SCAL);
CC1101Status cc1101_calibrate(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SCAL);
}
void cc1101_switch_to_idle(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SIDLE);
CC1101Status cc1101_switch_to_idle(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SIDLE);
}
void cc1101_switch_to_rx(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SRX);
CC1101Status cc1101_switch_to_rx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SRX);
}
void cc1101_switch_to_tx(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_STX);
CC1101Status cc1101_switch_to_tx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_STX);
}
void cc1101_flush_rx(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SFRX);
CC1101Status cc1101_flush_rx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SFRX);
}
void cc1101_flush_tx(FuriHalSpiBusHandle* handle) {
cc1101_strobe(handle, CC1101_STROBE_SFTX);
CC1101Status cc1101_flush_tx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SFTX);
}
uint32_t cc1101_set_frequency(FuriHalSpiBusHandle* handle, uint32_t value) {
@ -123,12 +135,12 @@ uint32_t cc1101_set_intermediate_frequency(FuriHalSpiBusHandle* handle, uint32_t
void cc1101_set_pa_table(FuriHalSpiBusHandle* handle, const uint8_t value[8]) {
uint8_t tx[9] = {CC1101_PATABLE | CC1101_BURST}; //-V1009
CC1101Status rx[9] = {0};
rx[0].CHIP_RDYn = 1;
rx[8].CHIP_RDYn = 1;
memcpy(&tx[1], &value[0], 8);
while(furi_hal_gpio_read(handle->miso))
;
furi_hal_spi_bus_trx(handle, tx, (uint8_t*)rx, sizeof(rx), CC1101_TIMEOUT);
cc1101_spi_trx(handle, tx, (uint8_t*)rx, sizeof(rx));
assert((rx[0].CHIP_RDYn | rx[8].CHIP_RDYn) == 0);
}
@ -139,12 +151,7 @@ uint8_t cc1101_write_fifo(FuriHalSpiBusHandle* handle, const uint8_t* data, uint
buff_tx[0] = CC1101_FIFO | CC1101_BURST;
memcpy(&buff_tx[1], data, size);
// Start transaction
// Wait IC to become ready
while(furi_hal_gpio_read(handle->miso))
;
// Tell IC what we want
furi_hal_spi_bus_trx(handle, buff_tx, (uint8_t*)buff_rx, size + 1, CC1101_TIMEOUT);
cc1101_spi_trx(handle, buff_tx, (uint8_t*)buff_rx, size + 1);
return size;
}
@ -153,13 +160,7 @@ uint8_t cc1101_read_fifo(FuriHalSpiBusHandle* handle, uint8_t* data, uint8_t* si
uint8_t buff_trx[2];
buff_trx[0] = CC1101_FIFO | CC1101_READ | CC1101_BURST;
// Start transaction
// Wait IC to become ready
while(furi_hal_gpio_read(handle->miso))
;
// First byte - packet length
furi_hal_spi_bus_trx(handle, buff_trx, buff_trx, 2, CC1101_TIMEOUT);
cc1101_spi_trx(handle, buff_trx, buff_trx, 2);
// Check that the packet is placed in the receive buffer
if(buff_trx[1] > 64) {

View file

@ -46,8 +46,10 @@ CC1101Status cc1101_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t*
/** Reset
*
* @param handle - pointer to FuriHalSpiHandle
*
* @return CC1101Status structure
*/
void cc1101_reset(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_reset(FuriHalSpiBusHandle* handle);
/** Get status
*
@ -60,8 +62,10 @@ CC1101Status cc1101_get_status(FuriHalSpiBusHandle* handle);
/** Enable shutdown mode
*
* @param handle - pointer to FuriHalSpiHandle
*
* @return CC1101Status structure
*/
void cc1101_shutdown(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_shutdown(FuriHalSpiBusHandle* handle);
/** Get Partnumber
*
@ -90,38 +94,46 @@ uint8_t cc1101_get_rssi(FuriHalSpiBusHandle* handle);
/** Calibrate oscillator
*
* @param handle - pointer to FuriHalSpiHandle
*
* @return CC1101Status structure
*/
void cc1101_calibrate(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_calibrate(FuriHalSpiBusHandle* handle);
/** Switch to idle
*
* @param handle - pointer to FuriHalSpiHandle
*/
void cc1101_switch_to_idle(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_switch_to_idle(FuriHalSpiBusHandle* handle);
/** Switch to RX
*
* @param handle - pointer to FuriHalSpiHandle
*
* @return CC1101Status structure
*/
void cc1101_switch_to_rx(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_switch_to_rx(FuriHalSpiBusHandle* handle);
/** Switch to TX
*
* @param handle - pointer to FuriHalSpiHandle
*
* @return CC1101Status structure
*/
void cc1101_switch_to_tx(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_switch_to_tx(FuriHalSpiBusHandle* handle);
/** Flush RX FIFO
*
* @param handle - pointer to FuriHalSpiHandle
*
* @return CC1101Status structure
*/
void cc1101_flush_rx(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_flush_rx(FuriHalSpiBusHandle* handle);
/** Flush TX FIFO
*
* @param handle - pointer to FuriHalSpiHandle
*/
void cc1101_flush_tx(FuriHalSpiBusHandle* handle);
CC1101Status cc1101_flush_tx(FuriHalSpiBusHandle* handle);
/** Set Frequency
*

View file

@ -14,7 +14,7 @@ extern "C" {
#define CC1101_IFDIV 0x400
/* IO Bus constants */
#define CC1101_TIMEOUT 500
#define CC1101_TIMEOUT 250
/* Bits and pieces */
#define CC1101_READ (1 << 7) /** Read Bit */

View file

@ -852,16 +852,20 @@ bool mf_classic_emulator(
bool is_reader_analyzer) {
furi_assert(emulator);
furi_assert(tx_rx);
bool command_processed = false;
bool is_encrypted = false;
uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
MfClassicKey access_key = MfClassicKeyA;
bool need_reset = false;
bool need_nack = false;
bool is_encrypted = false;
uint8_t sector = 0;
// Used for decrement and increment - copy to block on transfer
uint8_t transfer_buf[MF_CLASSIC_BLOCK_SIZE] = {};
uint8_t transfer_buf[MF_CLASSIC_BLOCK_SIZE];
bool transfer_buf_valid = false;
// Read command
while(!command_processed) { //-V654
// Process commands
while(!need_reset && !need_nack) { //-V654
memset(plain_data, 0, MF_CLASSIC_MAX_DATA_SIZE);
if(!is_encrypted) {
crypto1_reset(&emulator->crypto);
memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
@ -872,6 +876,7 @@ bool mf_classic_emulator(
"Error in tx rx. Tx: %d bits, Rx: %d bits",
tx_rx->tx_bits,
tx_rx->rx_bits);
need_reset = true;
break;
}
crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
@ -880,17 +885,20 @@ bool mf_classic_emulator(
// After increment, decrement or restore the only allowed command is transfer
uint8_t cmd = plain_data[0];
if(transfer_buf_valid && cmd != MF_CLASSIC_TRANSFER_CMD) {
need_nack = true;
break;
}
if(cmd == NFCA_CMD_HALT && plain_data[1] == 0x00) {
FURI_LOG_T(TAG, "Halt received");
return false;
need_reset = true;
break;
}
if(cmd == NFCA_CMD_RATS && !is_encrypted) {
if(cmd == NFCA_CMD_RATS) {
// Mifare Classic doesn't support ATS, NACK it and start listening again
FURI_LOG_T(TAG, "RATS received");
need_nack = true;
break;
}
@ -898,6 +906,7 @@ bool mf_classic_emulator(
uint8_t block = plain_data[1];
uint64_t key = 0;
uint8_t sector_trailer_block = mf_classic_get_sector_trailer_num_by_block(block);
sector = mf_classic_get_sector_by_block(block);
MfClassicSectorTrailer* sector_trailer =
(MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
if(cmd == MF_CLASSIC_AUTH_KEY_A_CMD) {
@ -908,7 +917,8 @@ bool mf_classic_emulator(
access_key = MfClassicKeyA;
} else {
FURI_LOG_D(TAG, "Key not known");
return false;
need_nack = true;
break;
}
} else {
if(mf_classic_is_key_found(
@ -918,7 +928,8 @@ bool mf_classic_emulator(
access_key = MfClassicKeyB;
} else {
FURI_LOG_D(TAG, "Key not known");
return false;
need_nack = true;
break;
}
}
@ -949,11 +960,13 @@ bool mf_classic_emulator(
if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
FURI_LOG_E(TAG, "Error in NT exchange");
return false;
need_reset = true;
break;
}
if(tx_rx->rx_bits != 64) {
return false;
need_reset = true;
break;
}
uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
@ -962,9 +975,15 @@ bool mf_classic_emulator(
crypto1_word(&emulator->crypto, nr, 1);
uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
if(cardRr != prng_successor(nonce, 64)) {
FURI_LOG_T(TAG, "Wrong AUTH! %08lX != %08lX", cardRr, prng_successor(nonce, 64));
FURI_LOG_T(
TAG,
"Wrong AUTH on block %u! %08lX != %08lX",
block,
cardRr,
prng_successor(nonce, 64));
// Don't send NACK, as the tag doesn't send it
return false;
need_reset = true;
break;
}
uint32_t ans = prng_successor(nonce, 96);
@ -986,11 +1005,25 @@ bool mf_classic_emulator(
if(!is_encrypted) {
FURI_LOG_T(TAG, "Invalid command before auth session established: %02X", cmd);
need_nack = true;
break;
}
if(cmd == MF_CLASSIC_READ_BLOCK_CMD) {
uint8_t block = plain_data[1];
// Mifare Classic commands always have block number after command
uint8_t block = plain_data[1];
if(mf_classic_get_sector_by_block(block) != sector) {
// Don't allow access to sectors other than authorized
FURI_LOG_T(
TAG,
"Trying to access block %u from not authorized sector (command: %02X)",
block,
cmd);
need_nack = true;
break;
}
switch(cmd) {
case MF_CLASSIC_READ_BLOCK_CMD: {
uint8_t block_data[MF_CLASSIC_BLOCK_SIZE + 2] = {};
memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
if(mf_classic_is_sector_trailer(block)) {
@ -1006,17 +1039,14 @@ bool mf_classic_emulator(
emulator, block, access_key, MfClassicActionACRead)) {
memset(&block_data[6], 0, 4);
}
} else if(!mf_classic_is_allowed_access(
emulator, block, access_key, MfClassicActionDataRead)) {
// Send NACK
uint8_t nack = 0x04;
crypto1_encrypt(
&emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
furi_hal_nfc_tx_rx(tx_rx, 300);
} else if(
!mf_classic_is_allowed_access(
emulator, block, access_key, MfClassicActionDataRead) ||
!mf_classic_is_block_read(&emulator->data, block)) {
need_nack = true;
break;
}
nfca_append_crc16(block_data, 16);
crypto1_encrypt(
@ -1028,23 +1058,36 @@ bool mf_classic_emulator(
tx_rx->tx_parity);
tx_rx->tx_bits = (MF_CLASSIC_BLOCK_SIZE + 2) * 8;
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
} else if(cmd == MF_CLASSIC_WRITE_BLOCK_CMD) {
uint8_t block = plain_data[1];
if(block > mf_classic_get_total_block_num(emulator->data.type)) {
break;
}
break;
}
case MF_CLASSIC_WRITE_BLOCK_CMD: {
// Send ACK
uint8_t ack = MF_CLASSIC_ACK_CMD;
crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
if(tx_rx->rx_bits != (MF_CLASSIC_BLOCK_SIZE + 2) * 8) break;
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
need_reset = true;
break;
}
if(tx_rx->rx_bits != (MF_CLASSIC_BLOCK_SIZE + 2) * 8) {
need_reset = true;
break;
}
crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
uint8_t block_data[MF_CLASSIC_BLOCK_SIZE] = {};
memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
if(!mf_classic_is_block_read(&emulator->data, block)) {
// Don't allow writing to the block for which we haven't read data yet
need_nack = true;
break;
}
if(mf_classic_is_sector_trailer(block)) {
if(mf_classic_is_allowed_access(
emulator, block, access_key, MfClassicActionKeyAWrite)) {
@ -1063,38 +1106,39 @@ bool mf_classic_emulator(
emulator, block, access_key, MfClassicActionDataWrite)) {
memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
} else {
need_nack = true;
break;
}
}
if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE) != 0) {
memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
emulator->data_changed = true;
}
// Send ACK
ack = MF_CLASSIC_ACK_CMD;
crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
} else if(
cmd == MF_CLASSIC_DECREMENT_CMD || cmd == MF_CLASSIC_INCREMENT_CMD ||
cmd == MF_CLASSIC_RESTORE_CMD) {
uint8_t block = plain_data[1];
break;
}
if(block > mf_classic_get_total_block_num(emulator->data.type)) {
break;
}
case MF_CLASSIC_DECREMENT_CMD:
case MF_CLASSIC_INCREMENT_CMD:
case MF_CLASSIC_RESTORE_CMD: {
MfClassicAction action = (cmd == MF_CLASSIC_INCREMENT_CMD) ? MfClassicActionDataInc :
MfClassicActionDataDec;
MfClassicAction action = MfClassicActionDataDec;
if(cmd == MF_CLASSIC_INCREMENT_CMD) {
action = MfClassicActionDataInc;
}
if(!mf_classic_is_allowed_access(emulator, block, access_key, action)) {
need_nack = true;
break;
}
int32_t prev_value;
uint8_t addr;
if(!mf_classic_block_to_value(emulator->data.block[block].value, &prev_value, &addr)) {
need_nack = true;
break;
}
@ -1104,8 +1148,15 @@ bool mf_classic_emulator(
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
if(tx_rx->rx_bits != (sizeof(int32_t) + 2) * 8) break;
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
need_reset = true;
break;
}
if(tx_rx->rx_bits != (sizeof(int32_t) + 2) * 8) {
need_reset = true;
break;
}
crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
int32_t value = *(int32_t*)&plain_data[0];
@ -1122,9 +1173,12 @@ bool mf_classic_emulator(
transfer_buf_valid = true;
// Commands do not ACK
tx_rx->tx_bits = 0;
} else if(cmd == MF_CLASSIC_TRANSFER_CMD) {
uint8_t block = plain_data[1];
break;
}
case MF_CLASSIC_TRANSFER_CMD: {
if(!mf_classic_is_allowed_access(emulator, block, access_key, MfClassicActionDataDec)) {
need_nack = true;
break;
}
if(memcmp(transfer_buf, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE) !=
@ -1138,13 +1192,17 @@ bool mf_classic_emulator(
crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
} else {
break;
}
default:
FURI_LOG_T(TAG, "Unknown command: %02X", cmd);
need_nack = true;
break;
}
}
if(!command_processed) {
if(need_nack && !need_reset) {
// Send NACK
uint8_t nack = transfer_buf_valid ? MF_CLASSIC_NACK_BUF_VALID_CMD :
MF_CLASSIC_NACK_BUF_INVALID_CMD;
@ -1156,10 +1214,10 @@ bool mf_classic_emulator(
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
furi_hal_nfc_tx_rx(tx_rx, 300);
return false;
need_reset = true;
}
return true;
return !need_reset;
}
void mf_classic_halt(FuriHalNfcTxRxContext* tx_rx, Crypto1* crypto) {

View file

@ -13,7 +13,7 @@ if not ["%FBT_NOENV%"] == [""] (
exit /b 0
)
set "FLIPPER_TOOLCHAIN_VERSION=22"
set "FLIPPER_TOOLCHAIN_VERSION=23"
if ["%FBT_TOOLCHAIN_PATH%"] == [""] (
set "FBT_TOOLCHAIN_PATH=%FBT_ROOT%"

View file

@ -4,7 +4,7 @@
# public variables
DEFAULT_SCRIPT_PATH="$(pwd -P)";
FBT_TOOLCHAIN_VERSION="${FBT_TOOLCHAIN_VERSION:-"22"}";
FBT_TOOLCHAIN_VERSION="${FBT_TOOLCHAIN_VERSION:-"23"}";
if [ -z ${FBT_TOOLCHAIN_PATH+x} ] ; then
FBT_TOOLCHAIN_PATH_WAS_SET=0;