esp8266_deauth.c 19 KB

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