formatted

This commit is contained in:
MX
2022-08-03 22:11:32 +03:00
parent 3cf13c71d9
commit 2475b039dd
3 changed files with 168 additions and 204 deletions

View File

@@ -34,7 +34,8 @@ static void bad_usb_draw_callback(Canvas* canvas, void* _model) {
elements_button_center(canvas, "Stop"); elements_button_center(canvas, "Stop");
} }
if((model->state.state == BadUsbStateNotConnected) || (model->state.state == BadUsbStateIdle) || (model->state.state == BadUsbStateDone)) { if((model->state.state == BadUsbStateNotConnected) ||
(model->state.state == BadUsbStateIdle) || (model->state.state == BadUsbStateDone)) {
elements_button_left(canvas, "Config"); elements_button_left(canvas, "Config");
} }

View File

@@ -34,41 +34,35 @@
#define ENABLE_MODULE_DETECTION 1 #define ENABLE_MODULE_DETECTION 1
typedef enum EEventType // app internally defined event types typedef enum EEventType // app internally defined event types
{ { EventTypeKey // flipper input.h type
EventTypeKey // flipper input.h type
} EEventType; } EEventType;
typedef struct SPluginEvent typedef struct SPluginEvent {
{
EEventType m_type; EEventType m_type;
InputEvent m_input; InputEvent m_input;
} SPluginEvent; } SPluginEvent;
typedef enum EAppContext typedef enum EAppContext {
{
Undefined, Undefined,
WaitingForModule, WaitingForModule,
Initializing, Initializing,
ModuleActive, ModuleActive,
} EAppContext; } EAppContext;
typedef enum EWorkerEventFlags typedef enum EWorkerEventFlags {
{
WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event
WorkerEventStop = (1 << 1), WorkerEventStop = (1 << 1),
WorkerEventRx = (1 << 2), WorkerEventRx = (1 << 2),
} EWorkerEventFlags; } EWorkerEventFlags;
typedef struct SGpioButtons typedef struct SGpioButtons {
{
GpioPin const* pinButtonUp; GpioPin const* pinButtonUp;
GpioPin const* pinButtonDown; GpioPin const* pinButtonDown;
GpioPin const* pinButtonOK; GpioPin const* pinButtonOK;
GpioPin const* pinButtonBack; GpioPin const* pinButtonBack;
} SGpioButtons; } SGpioButtons;
typedef struct SWiFiDeauthApp typedef struct SWiFiDeauthApp {
{
Gui* m_gui; Gui* m_gui;
FuriThread* m_worker_thread; FuriThread* m_worker_thread;
//NotificationApp* m_notification; //NotificationApp* m_notification;
@@ -94,8 +88,7 @@ typedef struct SWiFiDeauthApp
} SWiFiDeauthApp; } SWiFiDeauthApp;
/////// INIT STATE /////// /////// INIT STATE ///////
static void esp8266_deauth_app_init(SWiFiDeauthApp* const app) static void esp8266_deauth_app_init(SWiFiDeauthApp* const app) {
{
app->m_context = Undefined; app->m_context = Undefined;
app->m_canvasSize = 128 * 8 * 8; app->m_canvasSize = 128 * 8 * 8;
@@ -128,11 +121,9 @@ static void esp8266_deauth_app_init(SWiFiDeauthApp* const app)
#endif #endif
} }
static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ctx) static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ctx) {
{
SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)ctx, 25); SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)ctx, 25);
if (app == NULL) if(app == NULL) {
{
return; return;
} }
@@ -156,27 +147,35 @@ static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ct
// app->m_needUpdateGUI = false; // app->m_needUpdateGUI = false;
//} //}
switch (app->m_context) switch(app->m_context) {
{ case Undefined: {
case Undefined:
{
canvas_clear(canvas); canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary); canvas_set_font(canvas, FontPrimary);
const char* strInitializing = "Something wrong"; const char* strInitializing = "Something wrong";
canvas_draw_str(canvas, (u8g2_GetDisplayWidth(&canvas->fb) / 2) - (canvas_string_width(canvas, strInitializing) / 2), (u8g2_GetDisplayHeight(&canvas->fb) / 2)/* - (u8g2_GetMaxCharHeight(&canvas->fb) / 2)*/, strInitializing); canvas_draw_str(
} canvas,
break; (u8g2_GetDisplayWidth(&canvas->fb) / 2) -
(canvas_string_width(canvas, strInitializing) / 2),
(u8g2_GetDisplayHeight(&canvas->fb) /
2) /* - (u8g2_GetMaxCharHeight(&canvas->fb) / 2)*/,
strInitializing);
} break;
case WaitingForModule: case WaitingForModule:
#if ENABLE_MODULE_DETECTION #if ENABLE_MODULE_DETECTION
furi_assert(!app->m_wifiDeauthModuleAttached); furi_assert(!app->m_wifiDeauthModuleAttached);
if (!app->m_wifiDeauthModuleAttached) if(!app->m_wifiDeauthModuleAttached) {
{
canvas_clear(canvas); canvas_clear(canvas);
canvas_set_font(canvas, FontSecondary); canvas_set_font(canvas, FontSecondary);
const char* strInitializing = "Attach WiFi scanner module"; const char* strInitializing = "Attach WiFi scanner module";
canvas_draw_str(canvas, (u8g2_GetDisplayWidth(&canvas->fb) / 2) - (canvas_string_width(canvas, strInitializing) / 2), (u8g2_GetDisplayHeight(&canvas->fb) / 2)/* - (u8g2_GetMaxCharHeight(&canvas->fb) / 2)*/, strInitializing); canvas_draw_str(
canvas,
(u8g2_GetDisplayWidth(&canvas->fb) / 2) -
(canvas_string_width(canvas, strInitializing) / 2),
(u8g2_GetDisplayHeight(&canvas->fb) /
2) /* - (u8g2_GetMaxCharHeight(&canvas->fb) / 2)*/,
strInitializing);
} }
#endif #endif
break; break;
@@ -184,23 +183,26 @@ static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ct
#if ENABLE_MODULE_POWER #if ENABLE_MODULE_POWER
{ {
furi_assert(!app->m_wifiDeauthModuleInitialized); furi_assert(!app->m_wifiDeauthModuleInitialized);
if (!app->m_wifiDeauthModuleInitialized) if(!app->m_wifiDeauthModuleInitialized) {
{
canvas_set_font(canvas, FontPrimary); canvas_set_font(canvas, FontPrimary);
const char* strInitializing = "Initializing..."; const char* strInitializing = "Initializing...";
canvas_draw_str(canvas, (u8g2_GetDisplayWidth(&canvas->fb) / 2) - (canvas_string_width(canvas, strInitializing) / 2), (u8g2_GetDisplayHeight(&canvas->fb) / 2) - (u8g2_GetMaxCharHeight(&canvas->fb) / 2), strInitializing); canvas_draw_str(
canvas,
(u8g2_GetDisplayWidth(&canvas->fb) / 2) -
(canvas_string_width(canvas, strInitializing) / 2),
(u8g2_GetDisplayHeight(&canvas->fb) / 2) -
(u8g2_GetMaxCharHeight(&canvas->fb) / 2),
strInitializing);
} }
} }
#endif // ENABLE_MODULE_POWER #endif // ENABLE_MODULE_POWER
break; break;
case ModuleActive: case ModuleActive: {
{
uint8_t* buffer = canvas_get_buffer(canvas); uint8_t* buffer = canvas_get_buffer(canvas);
app->m_canvasSize = canvas_get_buffer_size(canvas); app->m_canvasSize = canvas_get_buffer_size(canvas);
memcpy(buffer, app->m_backBuffer, app->m_canvasSize); memcpy(buffer, app->m_backBuffer, app->m_canvasSize);
} } break;
break;
default: default:
break; break;
} }
@@ -208,7 +210,8 @@ static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ct
release_mutex((ValueMutex*)ctx, app); release_mutex((ValueMutex*)ctx, app);
} }
static void esp8266_deauth_module_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { static void
esp8266_deauth_module_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue); furi_assert(event_queue);
SPluginEvent event = {.m_type = EventTypeKey, .m_input = *input_event}; SPluginEvent event = {.m_type = EventTypeKey, .m_input = *input_event};
@@ -229,8 +232,6 @@ static void uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
furi_thread_flags_set(furi_thread_get_id(app->m_worker_thread), WorkerEventRx); furi_thread_flags_set(furi_thread_get_id(app->m_worker_thread), WorkerEventRx);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
} }
} }
static int32_t uart_worker(void* context) { static int32_t uart_worker(void* context) {
@@ -238,8 +239,7 @@ static int32_t uart_worker(void* context) {
DEAUTH_APP_LOG_I("[UART] Worker thread init"); DEAUTH_APP_LOG_I("[UART] Worker thread init");
SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)context, 25); SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)context, 25);
if (app == NULL) if(app == NULL) {
{
return 1; return 1;
} }
@@ -254,43 +254,36 @@ static int32_t uart_worker(void* context) {
string_init(receivedString); string_init(receivedString);
#endif // ENABLE_MODULE_POWER #endif // ENABLE_MODULE_POWER
while(true) while(true) {
{ uint32_t events = furi_thread_flags_wait(
uint32_t events = furi_thread_flags_wait(WorkerEventStop | WorkerEventRx, FuriFlagWaitAny, FuriWaitForever); WorkerEventStop | WorkerEventRx, FuriFlagWaitAny, FuriWaitForever);
furi_check((events & FuriFlagError) == 0); furi_check((events & FuriFlagError) == 0);
if(events & WorkerEventStop) break; if(events & WorkerEventStop) break;
if(events & WorkerEventRx) if(events & WorkerEventRx) {
{
DEAUTH_APP_LOG_I("[UART] Received data"); DEAUTH_APP_LOG_I("[UART] Received data");
SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)context, 25); SWiFiDeauthApp* app = acquire_mutex((ValueMutex*)context, 25);
if (app == NULL) if(app == NULL) {
{
return 1; return 1;
} }
size_t dataReceivedLength = 0; size_t dataReceivedLength = 0;
int index = 0; int index = 0;
do do {
{
const uint8_t dataBufferSize = 64; const uint8_t dataBufferSize = 64;
uint8_t dataBuffer[dataBufferSize]; uint8_t dataBuffer[dataBufferSize];
dataReceivedLength = xStreamBufferReceive(rx_stream, dataBuffer, dataBufferSize, 25); dataReceivedLength =
if (dataReceivedLength > 0) xStreamBufferReceive(rx_stream, dataBuffer, dataBufferSize, 25);
{ if(dataReceivedLength > 0) {
#if ENABLE_MODULE_POWER #if ENABLE_MODULE_POWER
if (!initialized) if(!initialized) {
{ if(!(dataReceivedLength > strlen(MODULE_CONTEXT_INITIALIZATION))) {
if(!(dataReceivedLength > strlen(MODULE_CONTEXT_INITIALIZATION)))
{
DEAUTH_APP_LOG_I("[UART] Found possible init candidate"); DEAUTH_APP_LOG_I("[UART] Found possible init candidate");
for (uint16_t i = 0; i < dataReceivedLength; i++) for(uint16_t i = 0; i < dataReceivedLength; i++) {
{
string_push_back(receivedString, dataBuffer[i]); string_push_back(receivedString, dataBuffer[i]);
} }
} }
} } else
else
#endif // ENABLE_MODULE_POWER #endif // ENABLE_MODULE_POWER
{ {
DEAUTH_APP_LOG_I("[UART] Data copied to backbuffer"); DEAUTH_APP_LOG_I("[UART] Data copied to backbuffer");
@@ -303,18 +296,14 @@ static int32_t uart_worker(void* context) {
} while(dataReceivedLength > 0); } while(dataReceivedLength > 0);
#if ENABLE_MODULE_POWER #if ENABLE_MODULE_POWER
if (!app->m_wifiDeauthModuleInitialized) if(!app->m_wifiDeauthModuleInitialized) {
{ if(string_cmp_str(receivedString, MODULE_CONTEXT_INITIALIZATION) == 0) {
if (string_cmp_str(receivedString, MODULE_CONTEXT_INITIALIZATION) == 0)
{
DEAUTH_APP_LOG_I("[UART] Initialized"); DEAUTH_APP_LOG_I("[UART] Initialized");
initialized = true; initialized = true;
app->m_wifiDeauthModuleInitialized = true; app->m_wifiDeauthModuleInitialized = true;
app->m_context = ModuleActive; app->m_context = ModuleActive;
string_clear(receivedString); string_clear(receivedString);
} } else {
else
{
DEAUTH_APP_LOG_I("[UART] Not an initialization command"); DEAUTH_APP_LOG_I("[UART] Not an initialization command");
string_reset(receivedString); string_reset(receivedString);
} }
@@ -328,8 +317,7 @@ static int32_t uart_worker(void* context) {
return 0; return 0;
} }
int32_t esp8266_deauth_app(void* p) int32_t esp8266_deauth_app(void* p) {
{
UNUSED(p); UNUSED(p);
DEAUTH_APP_LOG_I("Init"); DEAUTH_APP_LOG_I("Init");
@@ -351,10 +339,15 @@ int32_t esp8266_deauth_app(void* p)
furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, true);
furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, true);
furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, true);
furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, false); // GPIO15 - Boot fails if pulled HIGH furi_hal_gpio_write(
app->m_GpioButtons.pinButtonBack, false); // GPIO15 - Boot fails if pulled HIGH
#if ENABLE_MODULE_DETECTION #if ENABLE_MODULE_DETECTION
furi_hal_gpio_init(&gpio_ext_pc0, GpioModeInput, GpioPullUp, GpioSpeedLow); // Connect to the Flipper's ground just to be sure furi_hal_gpio_init(
&gpio_ext_pc0,
GpioModeInput,
GpioPullUp,
GpioSpeedLow); // Connect to the Flipper's ground just to be sure
//furi_hal_gpio_add_int_callback(pinD0, input_isr_d0, this); //furi_hal_gpio_add_int_callback(pinD0, input_isr_d0, this);
app->m_context = WaitingForModule; app->m_context = WaitingForModule;
#else #else
@@ -367,7 +360,7 @@ int32_t esp8266_deauth_app(void* p)
#endif // ENABLE_MODULE_DETECTION #endif // ENABLE_MODULE_DETECTION
ValueMutex app_data_mutex; ValueMutex app_data_mutex;
if (!init_mutex(&app_data_mutex, app, sizeof(SWiFiDeauthApp))) { if(!init_mutex(&app_data_mutex, app, sizeof(SWiFiDeauthApp))) {
DEAUTH_APP_LOG_E("cannot create mutex\r\n"); DEAUTH_APP_LOG_E("cannot create mutex\r\n");
free(app); free(app);
return 255; return 255;
@@ -406,16 +399,13 @@ int32_t esp8266_deauth_app(void* p)
DEAUTH_APP_LOG_I("UART thread allocated"); DEAUTH_APP_LOG_I("UART thread allocated");
SPluginEvent event; SPluginEvent event;
for(bool processing = true; processing;) for(bool processing = true; processing;) {
{
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
SWiFiDeauthApp* app = (SWiFiDeauthApp*)acquire_mutex_block(&app_data_mutex); SWiFiDeauthApp* app = (SWiFiDeauthApp*)acquire_mutex_block(&app_data_mutex);
#if ENABLE_MODULE_DETECTION #if ENABLE_MODULE_DETECTION
if(!app->m_wifiDeauthModuleAttached) if(!app->m_wifiDeauthModuleAttached) {
{ if(furi_hal_gpio_read(&gpio_ext_pc0) == false) {
if(furi_hal_gpio_read(&gpio_ext_pc0) == false)
{
DEAUTH_APP_LOG_I("Module Attached"); DEAUTH_APP_LOG_I("Module Attached");
app->m_wifiDeauthModuleAttached = true; app->m_wifiDeauthModuleAttached = true;
#if ENABLE_MODULE_POWER #if ENABLE_MODULE_POWER
@@ -428,65 +418,46 @@ int32_t esp8266_deauth_app(void* p)
} }
#endif // ENABLE_MODULE_DETECTION #endif // ENABLE_MODULE_DETECTION
if(event_status == FuriStatusOk) if(event_status == FuriStatusOk) {
{ if(event.m_type == EventTypeKey) {
if(event.m_type == EventTypeKey) if(app->m_wifiDeauthModuleInitialized) {
{ if(app->m_context == ModuleActive) {
if (app->m_wifiDeauthModuleInitialized) switch(event.m_input.key) {
{
if (app->m_context == ModuleActive)
{
switch (event.m_input.key)
{
case InputKeyUp: case InputKeyUp:
if (event.m_input.type == InputTypePress) if(event.m_input.type == InputTypePress) {
{
DEAUTH_APP_LOG_I("Up Press"); DEAUTH_APP_LOG_I("Up Press");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, false); furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, false);
} } else if(event.m_input.type == InputTypeRelease) {
else if (event.m_input.type == InputTypeRelease)
{
DEAUTH_APP_LOG_I("Up Release"); DEAUTH_APP_LOG_I("Up Release");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, true);
} }
break; break;
case InputKeyDown: case InputKeyDown:
if (event.m_input.type == InputTypePress) if(event.m_input.type == InputTypePress) {
{
DEAUTH_APP_LOG_I("Down Press"); DEAUTH_APP_LOG_I("Down Press");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, false); furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, false);
} } else if(event.m_input.type == InputTypeRelease) {
else if (event.m_input.type == InputTypeRelease)
{
DEAUTH_APP_LOG_I("Down Release"); DEAUTH_APP_LOG_I("Down Release");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, true);
} }
break; break;
case InputKeyOk: case InputKeyOk:
if (event.m_input.type == InputTypePress) if(event.m_input.type == InputTypePress) {
{
DEAUTH_APP_LOG_I("OK Press"); DEAUTH_APP_LOG_I("OK Press");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, false); furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, false);
} } else if(event.m_input.type == InputTypeRelease) {
else if (event.m_input.type == InputTypeRelease)
{
DEAUTH_APP_LOG_I("OK Release"); DEAUTH_APP_LOG_I("OK Release");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, true);
} }
break; break;
case InputKeyBack: case InputKeyBack:
if (event.m_input.type == InputTypePress) if(event.m_input.type == InputTypePress) {
{
DEAUTH_APP_LOG_I("Back Press"); DEAUTH_APP_LOG_I("Back Press");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, false); furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, false);
} } else if(event.m_input.type == InputTypeRelease) {
else if (event.m_input.type == InputTypeRelease)
{
DEAUTH_APP_LOG_I("Back Release"); DEAUTH_APP_LOG_I("Back Release");
furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, true); furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, true);
} } else if(event.m_input.type == InputTypeLong) {
else if (event.m_input.type == InputTypeLong)
{
DEAUTH_APP_LOG_I("Back Long"); DEAUTH_APP_LOG_I("Back Long");
processing = false; processing = false;
} }
@@ -495,27 +466,21 @@ int32_t esp8266_deauth_app(void* p)
break; break;
} }
} }
} } else {
else if(event.m_input.key == InputKeyBack) {
{ if(event.m_input.type == InputTypeShort ||
if(event.m_input.key == InputKeyBack) event.m_input.type == InputTypeLong) {
{
if(event.m_input.type == InputTypeShort || event.m_input.type == InputTypeLong)
{
processing = false; processing = false;
} }
} }
} }
} }
} } else {
else
{
DEAUTH_APP_LOG_D("osMessageQueue: event timeout"); DEAUTH_APP_LOG_D("osMessageQueue: event timeout");
} }
#if ENABLE_MODULE_DETECTION #if ENABLE_MODULE_DETECTION
if(app->m_wifiDeauthModuleAttached && furi_hal_gpio_read(&gpio_ext_pc0) == true) if(app->m_wifiDeauthModuleAttached && furi_hal_gpio_read(&gpio_ext_pc0) == true) {
{
DEAUTH_APP_LOG_D("Module Disconnected - Exit"); DEAUTH_APP_LOG_D("Module Disconnected - Exit");
processing = false; processing = false;
app->m_wifiDeauthModuleAttached = false; app->m_wifiDeauthModuleAttached = false;
@@ -558,8 +523,6 @@ int32_t esp8266_deauth_app(void* p)
delete_mutex(&app_data_mutex); delete_mutex(&app_data_mutex);
// Free rest // Free rest
free(app); free(app);

View File

@@ -136,7 +136,7 @@ static void tetris_game_render_callback(Canvas* const canvas, void* ctx) {
if(tetris_state->gameState == GameStatePlaying) { if(tetris_state->gameState == GameStatePlaying) {
char buffer2[6]; char buffer2[6];
snprintf(buffer2, sizeof(buffer2), "%u", tetris_state->numLines); snprintf(buffer2, sizeof(buffer2), "%u", tetris_state->numLines);
canvas_draw_str_aligned(canvas, 58,10, AlignRight, AlignBottom, buffer2); canvas_draw_str_aligned(canvas, 58, 10, AlignRight, AlignBottom, buffer2);
} }
if(tetris_state->gameState == GameStateGameOver) { if(tetris_state->gameState == GameStateGameOver) {