esp8266_deauth.c 19 KB

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