esp8266_deauth.c 19 KB

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