esp8266_deauth.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include <furi.h>
  2. #include <furi_hal_console.h>
  3. #include <furi_hal_gpio.h>
  4. #include <furi_hal_power.h>
  5. #include <furi_hal_uart.h>
  6. #include <gui/canvas_i.h>
  7. #include <gui/gui.h>
  8. #include <input/input.h>
  9. //#include <math.h>
  10. //#include <notification/notification.h>
  11. //#include <notification/notification_messages.h>
  12. //#include <stdlib.h>
  13. #include <xtreme.h>
  14. #include "FlipperZeroWiFiDeauthModuleDefines.h"
  15. #define UART_CH \
  16. (xtreme_settings.uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)
  17. #define DEAUTH_APP_DEBUG 0
  18. #if DEAUTH_APP_DEBUG
  19. #define APP_NAME_TAG "WiFi_Deauther"
  20. #define DEAUTH_APP_LOG_I(format, ...) FURI_LOG_I(APP_NAME_TAG, format, ##__VA_ARGS__)
  21. #define DEAUTH_APP_LOG_D(format, ...) FURI_LOG_D(APP_NAME_TAG, format, ##__VA_ARGS__)
  22. #define DEAUTH_APP_LOG_E(format, ...) FURI_LOG_E(APP_NAME_TAG, format, ##__VA_ARGS__)
  23. #else
  24. #define DEAUTH_APP_LOG_I(format, ...)
  25. #define DEAUTH_APP_LOG_D(format, ...)
  26. #define DEAUTH_APP_LOG_E(format, ...)
  27. #endif // WIFI_APP_DEBUG
  28. #define ENABLE_MODULE_POWER 1
  29. #define ENABLE_MODULE_DETECTION 1
  30. typedef enum EEventType // app internally defined event types
  31. { EventTypeKey // flipper input.h type
  32. } EEventType;
  33. typedef struct SPluginEvent {
  34. EEventType m_type;
  35. InputEvent m_input;
  36. } SPluginEvent;
  37. typedef enum EAppContext {
  38. Undefined,
  39. WaitingForModule,
  40. Initializing,
  41. ModuleActive,
  42. } EAppContext;
  43. typedef enum EWorkerEventFlags {
  44. WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event
  45. WorkerEventStop = (1 << 1),
  46. WorkerEventRx = (1 << 2),
  47. } EWorkerEventFlags;
  48. typedef struct SGpioButtons {
  49. GpioPin const* pinButtonUp;
  50. GpioPin const* pinButtonDown;
  51. GpioPin const* pinButtonOK;
  52. GpioPin const* pinButtonBack;
  53. } SGpioButtons;
  54. typedef struct SWiFiDeauthApp {
  55. FuriMutex* mutex;
  56. Gui* m_gui;
  57. FuriThread* m_worker_thread;
  58. //NotificationApp* m_notification;
  59. FuriStreamBuffer* m_rx_stream;
  60. SGpioButtons m_GpioButtons;
  61. bool m_wifiDeauthModuleInitialized;
  62. bool m_wifiDeauthModuleAttached;
  63. EAppContext m_context;
  64. uint8_t m_backBuffer[128 * 8 * 8];
  65. //uint8_t m_renderBuffer[128 * 8 * 8];
  66. uint8_t* m_backBufferPtr;
  67. //uint8_t* m_m_renderBufferPtr;
  68. //uint8_t* m_originalBuffer;
  69. //uint8_t** m_originalBufferLocation;
  70. size_t m_canvasSize;
  71. bool m_needUpdateGUI;
  72. } SWiFiDeauthApp;
  73. /////// INIT STATE ///////
  74. static void esp8266_deauth_app_init(SWiFiDeauthApp* const app) {
  75. app->m_context = Undefined;
  76. app->m_canvasSize = 128 * 8 * 8;
  77. memset(app->m_backBuffer, DEAUTH_APP_DEBUG ? 0xFF : 0x00, app->m_canvasSize);
  78. //memset(app->m_renderBuffer, DEAUTH_APP_DEBUG ? 0xFF : 0x00, app->m_canvasSize);
  79. //app->m_originalBuffer = NULL;
  80. //app->m_originalBufferLocation = NULL;
  81. //app->m_m_renderBufferPtr = app->m_renderBuffer;
  82. app->m_backBufferPtr = app->m_backBuffer;
  83. app->m_GpioButtons.pinButtonUp = &gpio_ext_pc3;
  84. app->m_GpioButtons.pinButtonDown = &gpio_ext_pb2;
  85. app->m_GpioButtons.pinButtonOK = &gpio_ext_pb3;
  86. app->m_GpioButtons.pinButtonBack = &gpio_ext_pa4;
  87. app->m_needUpdateGUI = false;
  88. #if ENABLE_MODULE_POWER
  89. app->m_wifiDeauthModuleInitialized = false;
  90. #else
  91. app->m_wifiDeauthModuleInitialized = true;
  92. #endif // ENABLE_MODULE_POWER
  93. #if ENABLE_MODULE_DETECTION
  94. app->m_wifiDeauthModuleAttached = false;
  95. #else
  96. app->m_wifiDeauthModuleAttached = true;
  97. #endif
  98. }
  99. static void esp8266_deauth_module_render_callback(Canvas* const canvas, void* ctx) {
  100. furi_assert(ctx);
  101. SWiFiDeauthApp* app = ctx;
  102. furi_mutex_acquire(app->mutex, FuriWaitForever);
  103. //if(app->m_needUpdateGUI)
  104. //{
  105. // app->m_needUpdateGUI = false;
  106. // //app->m_canvasSize = canvas_get_buffer_size(canvas);
  107. // //app->m_originalBuffer = canvas_get_buffer(canvas);
  108. // //app->m_originalBufferLocation = &u8g2_GetBufferPtr(&canvas->fb);
  109. // //u8g2_GetBufferPtr(&canvas->fb) = app->m_m_renderBufferPtr;
  110. //}
  111. //uint8_t* exchangeBuffers = app->m_m_renderBufferPtr;
  112. //app->m_m_renderBufferPtr = app->m_backBufferPtr;
  113. //app->m_backBufferPtr = exchangeBuffers;
  114. //if(app->m_needUpdateGUI)
  115. //{
  116. // //memcpy(app->m_renderBuffer, app->m_backBuffer, app->m_canvasSize);
  117. // app->m_needUpdateGUI = false;
  118. //}
  119. switch(app->m_context) {
  120. case Undefined: {
  121. canvas_clear(canvas);
  122. canvas_set_font(canvas, FontPrimary);
  123. const char* strInitializing = "Something wrong";
  124. canvas_draw_str(
  125. canvas,
  126. (128 / 2) - (canvas_string_width(canvas, strInitializing) / 2),
  127. (64 / 2) /* - (canvas_current_font_height(canvas) / 2)*/,
  128. strInitializing);
  129. } break;
  130. case WaitingForModule:
  131. #if ENABLE_MODULE_DETECTION
  132. furi_assert(!app->m_wifiDeauthModuleAttached);
  133. if(!app->m_wifiDeauthModuleAttached) {
  134. canvas_clear(canvas);
  135. canvas_set_font(canvas, FontSecondary);
  136. const char* strInitializing = "Attach WiFi Deauther module";
  137. canvas_draw_str(
  138. canvas,
  139. (128 / 2) - (canvas_string_width(canvas, strInitializing) / 2),
  140. (64 / 2) /* - (canvas_current_font_height(canvas) / 2)*/,
  141. strInitializing);
  142. }
  143. #endif
  144. break;
  145. case Initializing:
  146. #if ENABLE_MODULE_POWER
  147. {
  148. furi_assert(!app->m_wifiDeauthModuleInitialized);
  149. if(!app->m_wifiDeauthModuleInitialized) {
  150. canvas_set_font(canvas, FontPrimary);
  151. const char* strInitializing = "Initializing...";
  152. canvas_draw_str(
  153. canvas,
  154. (128 / 2) - (canvas_string_width(canvas, strInitializing) / 2),
  155. (64 / 2) - (canvas_current_font_height(canvas) / 2),
  156. strInitializing);
  157. }
  158. }
  159. #endif // ENABLE_MODULE_POWER
  160. break;
  161. case ModuleActive: {
  162. uint8_t* buffer = canvas->fb.tile_buf_ptr;
  163. app->m_canvasSize = gui_get_framebuffer_size(app->m_gui);
  164. memcpy(buffer, app->m_backBuffer, app->m_canvasSize);
  165. } break;
  166. default:
  167. break;
  168. }
  169. furi_mutex_release(app->mutex);
  170. }
  171. static void
  172. esp8266_deauth_module_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  173. furi_assert(event_queue);
  174. SPluginEvent event = {.m_type = EventTypeKey, .m_input = *input_event};
  175. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  176. }
  177. static void uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  178. furi_assert(context);
  179. SWiFiDeauthApp* app = context;
  180. DEAUTH_APP_LOG_I("uart_echo_on_irq_cb");
  181. if(ev == UartIrqEventRXNE) {
  182. DEAUTH_APP_LOG_I("ev == UartIrqEventRXNE");
  183. furi_stream_buffer_send(app->m_rx_stream, &data, 1, 0);
  184. furi_thread_flags_set(furi_thread_get_id(app->m_worker_thread), WorkerEventRx);
  185. }
  186. }
  187. static int32_t uart_worker(void* context) {
  188. furi_assert(context);
  189. DEAUTH_APP_LOG_I("[UART] Worker thread init");
  190. SWiFiDeauthApp* app = context;
  191. furi_mutex_acquire(app->mutex, FuriWaitForever);
  192. if(app == NULL) {
  193. return 1;
  194. }
  195. FuriStreamBuffer* rx_stream = app->m_rx_stream;
  196. furi_mutex_release(app->mutex);
  197. #if ENABLE_MODULE_POWER
  198. bool initialized = false;
  199. FuriString* receivedString;
  200. receivedString = furi_string_alloc();
  201. #endif // ENABLE_MODULE_POWER
  202. while(true) {
  203. uint32_t events = furi_thread_flags_wait(
  204. WorkerEventStop | WorkerEventRx, FuriFlagWaitAny, FuriWaitForever);
  205. furi_check((events & FuriFlagError) == 0);
  206. if(events & WorkerEventStop) break;
  207. if(events & WorkerEventRx) {
  208. DEAUTH_APP_LOG_I("[UART] Received data");
  209. SWiFiDeauthApp* app = context;
  210. furi_mutex_acquire(app->mutex, FuriWaitForever);
  211. if(app == NULL) {
  212. return 1;
  213. }
  214. size_t dataReceivedLength = 0;
  215. int index = 0;
  216. do {
  217. const uint8_t dataBufferSize = 64;
  218. uint8_t dataBuffer[dataBufferSize];
  219. dataReceivedLength =
  220. furi_stream_buffer_receive(rx_stream, dataBuffer, dataBufferSize, 25);
  221. if(dataReceivedLength > 0) {
  222. #if ENABLE_MODULE_POWER
  223. if(!initialized) {
  224. if(!(dataReceivedLength > strlen(MODULE_CONTEXT_INITIALIZATION))) {
  225. DEAUTH_APP_LOG_I("[UART] Found possible init candidate");
  226. for(uint16_t i = 0; i < dataReceivedLength; i++) {
  227. furi_string_push_back(receivedString, dataBuffer[i]);
  228. }
  229. }
  230. } else
  231. #endif // ENABLE_MODULE_POWER
  232. {
  233. DEAUTH_APP_LOG_I("[UART] Data copied to backbuffer");
  234. memcpy(app->m_backBuffer + index, dataBuffer, dataReceivedLength);
  235. index += dataReceivedLength;
  236. app->m_needUpdateGUI = true;
  237. }
  238. }
  239. } while(dataReceivedLength > 0);
  240. #if ENABLE_MODULE_POWER
  241. if(!app->m_wifiDeauthModuleInitialized) {
  242. if(furi_string_cmp_str(receivedString, MODULE_CONTEXT_INITIALIZATION) == 0) {
  243. DEAUTH_APP_LOG_I("[UART] Initialized");
  244. initialized = true;
  245. app->m_wifiDeauthModuleInitialized = true;
  246. app->m_context = ModuleActive;
  247. furi_string_free(receivedString);
  248. } else {
  249. DEAUTH_APP_LOG_I("[UART] Not an initialization command");
  250. furi_string_reset(receivedString);
  251. }
  252. }
  253. #endif // ENABLE_MODULE_POWER
  254. furi_mutex_release(app->mutex);
  255. }
  256. }
  257. return 0;
  258. }
  259. int32_t esp8266_deauth_app(void* p) {
  260. UNUSED(p);
  261. DEAUTH_APP_LOG_I("Init");
  262. // FuriTimer* timer = furi_timer_alloc(blink_test_update, FuriTimerTypePeriodic, event_queue);
  263. // furi_timer_start(timer, furi_kernel_get_tick_frequency());
  264. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(SPluginEvent));
  265. SWiFiDeauthApp* app = malloc(sizeof(SWiFiDeauthApp));
  266. esp8266_deauth_app_init(app);
  267. furi_hal_gpio_init_simple(app->m_GpioButtons.pinButtonUp, GpioModeOutputPushPull);
  268. furi_hal_gpio_init_simple(app->m_GpioButtons.pinButtonDown, GpioModeOutputPushPull);
  269. furi_hal_gpio_init_simple(app->m_GpioButtons.pinButtonOK, GpioModeOutputPushPull);
  270. furi_hal_gpio_init_simple(app->m_GpioButtons.pinButtonBack, GpioModeOutputPushPull);
  271. furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, true);
  272. furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, true);
  273. furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, true);
  274. furi_hal_gpio_write(
  275. app->m_GpioButtons.pinButtonBack, false); // GPIO15 - Boot fails if pulled HIGH
  276. #if ENABLE_MODULE_DETECTION
  277. furi_hal_gpio_init(
  278. &gpio_ext_pc0,
  279. GpioModeInput,
  280. GpioPullUp,
  281. GpioSpeedLow); // Connect to the Flipper's ground just to be sure
  282. //furi_hal_gpio_add_int_callback(pinD0, input_isr_d0, this);
  283. app->m_context = WaitingForModule;
  284. #else
  285. #if ENABLE_MODULE_POWER
  286. app->m_context = Initializing;
  287. uint8_t attempts = 0;
  288. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  289. furi_hal_power_enable_otg();
  290. furi_delay_ms(10);
  291. }
  292. furi_delay_ms(200);
  293. #else
  294. app->m_context = ModuleActive;
  295. #endif
  296. #endif // ENABLE_MODULE_DETECTION
  297. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  298. if(!app->mutex) {
  299. DEAUTH_APP_LOG_E("cannot create mutex\r\n");
  300. free(app);
  301. return 255;
  302. }
  303. DEAUTH_APP_LOG_I("Mutex created");
  304. //app->m_notification = furi_record_open(RECORD_NOTIFICATION);
  305. ViewPort* view_port = view_port_alloc();
  306. view_port_draw_callback_set(view_port, esp8266_deauth_module_render_callback, app);
  307. view_port_input_callback_set(view_port, esp8266_deauth_module_input_callback, event_queue);
  308. // Open GUI and register view_port
  309. app->m_gui = furi_record_open(RECORD_GUI);
  310. gui_add_view_port(app->m_gui, view_port, GuiLayerFullscreen);
  311. //notification_message(app->notification, &sequence_set_only_blue_255);
  312. app->m_rx_stream = furi_stream_buffer_alloc(1 * 1024, 1);
  313. app->m_worker_thread = furi_thread_alloc();
  314. furi_thread_set_name(app->m_worker_thread, "WiFiDeauthModuleUARTWorker");
  315. furi_thread_set_stack_size(app->m_worker_thread, 1 * 1024);
  316. furi_thread_set_context(app->m_worker_thread, app);
  317. furi_thread_set_callback(app->m_worker_thread, uart_worker);
  318. furi_thread_start(app->m_worker_thread);
  319. DEAUTH_APP_LOG_I("UART thread allocated");
  320. // Enable uart listener
  321. if(UART_CH == FuriHalUartIdUSART1) {
  322. furi_hal_console_disable();
  323. } else if(UART_CH == FuriHalUartIdLPUART1) {
  324. furi_hal_uart_init(UART_CH, FLIPPERZERO_SERIAL_BAUD);
  325. }
  326. furi_hal_uart_set_br(FuriHalUartIdUSART1, FLIPPERZERO_SERIAL_BAUD);
  327. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, uart_on_irq_cb, app);
  328. DEAUTH_APP_LOG_I("UART Listener created");
  329. SPluginEvent event;
  330. for(bool processing = true; processing;) {
  331. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  332. furi_mutex_acquire(app->mutex, FuriWaitForever);
  333. #if ENABLE_MODULE_DETECTION
  334. if(!app->m_wifiDeauthModuleAttached) {
  335. if(furi_hal_gpio_read(&gpio_ext_pc0) == false) {
  336. DEAUTH_APP_LOG_I("Module Attached");
  337. app->m_wifiDeauthModuleAttached = true;
  338. #if ENABLE_MODULE_POWER
  339. app->m_context = Initializing;
  340. uint8_t attempts2 = 0;
  341. while(!furi_hal_power_is_otg_enabled() && attempts2++ < 3) {
  342. furi_hal_power_enable_otg();
  343. furi_delay_ms(10);
  344. }
  345. #else
  346. app->m_context = ModuleActive;
  347. #endif
  348. }
  349. }
  350. #endif // ENABLE_MODULE_DETECTION
  351. if(event_status == FuriStatusOk) {
  352. if(event.m_type == EventTypeKey) {
  353. if(app->m_wifiDeauthModuleInitialized) {
  354. if(app->m_context == ModuleActive) {
  355. switch(event.m_input.key) {
  356. case InputKeyUp:
  357. if(event.m_input.type == InputTypePress) {
  358. DEAUTH_APP_LOG_I("Up Press");
  359. furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, false);
  360. } else if(event.m_input.type == InputTypeRelease) {
  361. DEAUTH_APP_LOG_I("Up Release");
  362. furi_hal_gpio_write(app->m_GpioButtons.pinButtonUp, true);
  363. }
  364. break;
  365. case InputKeyDown:
  366. if(event.m_input.type == InputTypePress) {
  367. DEAUTH_APP_LOG_I("Down Press");
  368. furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, false);
  369. } else if(event.m_input.type == InputTypeRelease) {
  370. DEAUTH_APP_LOG_I("Down Release");
  371. furi_hal_gpio_write(app->m_GpioButtons.pinButtonDown, true);
  372. }
  373. break;
  374. case InputKeyOk:
  375. if(event.m_input.type == InputTypePress) {
  376. DEAUTH_APP_LOG_I("OK Press");
  377. furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, false);
  378. } else if(event.m_input.type == InputTypeRelease) {
  379. DEAUTH_APP_LOG_I("OK Release");
  380. furi_hal_gpio_write(app->m_GpioButtons.pinButtonOK, true);
  381. }
  382. break;
  383. case InputKeyBack:
  384. if(event.m_input.type == InputTypePress) {
  385. DEAUTH_APP_LOG_I("Back Press");
  386. furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, false);
  387. } else if(event.m_input.type == InputTypeRelease) {
  388. DEAUTH_APP_LOG_I("Back Release");
  389. furi_hal_gpio_write(app->m_GpioButtons.pinButtonBack, true);
  390. } else if(event.m_input.type == InputTypeLong) {
  391. DEAUTH_APP_LOG_I("Back Long");
  392. processing = false;
  393. }
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. } else {
  400. if(event.m_input.key == InputKeyBack) {
  401. if(event.m_input.type == InputTypeShort ||
  402. event.m_input.type == InputTypeLong) {
  403. processing = false;
  404. }
  405. }
  406. }
  407. }
  408. }
  409. #if ENABLE_MODULE_DETECTION
  410. if(app->m_wifiDeauthModuleAttached && furi_hal_gpio_read(&gpio_ext_pc0) == true) {
  411. DEAUTH_APP_LOG_D("Module Disconnected - Exit");
  412. processing = false;
  413. app->m_wifiDeauthModuleAttached = false;
  414. app->m_wifiDeauthModuleInitialized = false;
  415. }
  416. #endif
  417. furi_mutex_release(app->mutex);
  418. view_port_update(view_port);
  419. }
  420. DEAUTH_APP_LOG_I("Start exit app");
  421. furi_thread_flags_set(furi_thread_get_id(app->m_worker_thread), WorkerEventStop);
  422. furi_thread_join(app->m_worker_thread);
  423. furi_thread_free(app->m_worker_thread);
  424. DEAUTH_APP_LOG_I("Thread Deleted");
  425. // Reset GPIO pins to default state
  426. furi_hal_gpio_init(&gpio_ext_pc0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  427. furi_hal_gpio_init(&gpio_ext_pc3, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  428. furi_hal_gpio_init(&gpio_ext_pb2, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  429. furi_hal_gpio_init(&gpio_ext_pb3, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  430. furi_hal_gpio_init(&gpio_ext_pa4, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  431. if(UART_CH == FuriHalUartIdLPUART1) {
  432. furi_hal_uart_deinit(UART_CH);
  433. } else {
  434. furi_hal_console_enable();
  435. }
  436. //*app->m_originalBufferLocation = app->m_originalBuffer;
  437. view_port_enabled_set(view_port, false);
  438. gui_remove_view_port(app->m_gui, view_port);
  439. // Close gui record
  440. furi_record_close(RECORD_GUI);
  441. //furi_record_close(RECORD_NOTIFICATION);
  442. app->m_gui = NULL;
  443. view_port_free(view_port);
  444. furi_message_queue_free(event_queue);
  445. furi_stream_buffer_free(app->m_rx_stream);
  446. furi_mutex_free(app->mutex);
  447. // Free rest
  448. free(app);
  449. DEAUTH_APP_LOG_I("App freed");
  450. #if ENABLE_MODULE_POWER
  451. if(furi_hal_power_is_otg_enabled()) {
  452. furi_hal_power_disable_otg();
  453. }
  454. #endif
  455. return 0;
  456. }