wifi_map.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <storage/storage.h>
  4. #define TAG "WIFI_MAP"
  5. #define FILE_NAME "wifi_map_data.csv"
  6. #include <gui/gui.h>
  7. #include <notification/notification.h>
  8. #include <notification/notification_messages.h>
  9. #include <gui/elements.h>
  10. #include <furi_hal_uart.h>
  11. #include <furi_hal_console.h>
  12. #include <gui/view_dispatcher.h>
  13. #include <gui/modules/dialog_ex.h>
  14. #define LINES_ON_SCREEN 6
  15. #define COLUMNS_ON_SCREEN 21
  16. #define WORKER_EVENTS_MASK (WorkerEventStop | WorkerEventRx)
  17. typedef struct UartDumpModel UartDumpModel;
  18. typedef struct {
  19. Gui* gui;
  20. NotificationApp* notification;
  21. ViewDispatcher* view_dispatcher;
  22. View* view;
  23. FuriThread* worker_thread;
  24. FuriStreamBuffer* rx_stream;
  25. File* file;
  26. } WiFiMapApp;
  27. typedef struct {
  28. FuriString* text;
  29. } ListElement;
  30. struct UartDumpModel {
  31. ListElement* list[LINES_ON_SCREEN];
  32. uint8_t line;
  33. char last_char;
  34. bool escape;
  35. File* file;
  36. };
  37. typedef enum {
  38. WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event
  39. WorkerEventStop = (1 << 1),
  40. WorkerEventRx = (1 << 2),
  41. } WorkerEventFlags;
  42. const NotificationSequence sequence_notification = {
  43. &message_display_backlight_on,
  44. &message_green_255,
  45. &message_delay_10,
  46. NULL,
  47. };
  48. File* open_file(){
  49. Storage* storage = furi_record_open(RECORD_STORAGE);
  50. File* file = storage_file_alloc(storage);
  51. if(!storage_file_open(file, APP_DATA_PATH(FILE_NAME), FSAM_WRITE, FSOM_OPEN_APPEND)) {
  52. FURI_LOG_E(TAG, "Failed to open file");
  53. }
  54. return file;
  55. }
  56. int32_t write_to_file(char data_line, File *file) {
  57. char *data = (char *)malloc(sizeof(char) + 1);
  58. data[0] = data_line;
  59. if(!storage_file_write(file, data, (uint16_t)strlen(data))) {
  60. FURI_LOG_E(TAG, "Failed to write to file");
  61. }
  62. free(data);
  63. return 0;
  64. }
  65. int32_t close_file(File *file) {
  66. storage_file_close(file);
  67. storage_file_free(file);
  68. furi_record_close(RECORD_STORAGE);
  69. return 0;
  70. }
  71. static void uart_echo_view_draw_callback(Canvas* canvas, void* _model) {
  72. UartDumpModel* model = _model;
  73. // Prepare canvas
  74. canvas_clear(canvas);
  75. canvas_set_color(canvas, ColorBlack);
  76. canvas_set_font(canvas, FontKeyboard);
  77. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  78. canvas_draw_str(
  79. canvas,
  80. 0,
  81. (i + 1) * (canvas_current_font_height(canvas) - 1),
  82. furi_string_get_cstr(model->list[i]->text));
  83. if(i == model->line) {
  84. uint8_t width =
  85. canvas_string_width(canvas, furi_string_get_cstr(model->list[i]->text));
  86. canvas_draw_box(
  87. canvas,
  88. width,
  89. (i) * (canvas_current_font_height(canvas) - 1) + 2,
  90. 2,
  91. canvas_current_font_height(canvas) - 2);
  92. }
  93. }
  94. }
  95. static bool uart_echo_view_input_callback(InputEvent* event, void* context) {
  96. UNUSED(event);
  97. UNUSED(context);
  98. return false;
  99. }
  100. static uint32_t uart_echo_exit(void* context) {
  101. UNUSED(context);
  102. return VIEW_NONE;
  103. }
  104. static void uart_echo_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  105. furi_assert(context);
  106. WiFiMapApp* app = context;
  107. if(ev == UartIrqEventRXNE) {
  108. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  109. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  110. }
  111. }
  112. static void uart_echo_push_to_list(UartDumpModel* model, const char data , WiFiMapApp* app) {
  113. if(model->escape) {
  114. // escape code end with letter
  115. if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z')) {
  116. model->escape = false;
  117. }
  118. } else if(data == '[' && model->last_char == '\e') {
  119. // "Esc[" is a escape code
  120. model->escape = true;
  121. } else if((data >= ' ' && data <= '~') || (data == '\n' || data == '\r')) {
  122. write_to_file((char) data, app->file);
  123. bool new_string_needed = false;
  124. if(furi_string_size(model->list[model->line]->text) >= COLUMNS_ON_SCREEN) {
  125. new_string_needed = true;
  126. } else if((data == '\n' || data == '\r')) {
  127. // pack line breaks
  128. if(model->last_char != '\n' && model->last_char != '\r') {
  129. new_string_needed = true;
  130. }
  131. }
  132. if(new_string_needed) {
  133. if((model->line + 1) < LINES_ON_SCREEN) {
  134. model->line += 1;
  135. } else {
  136. ListElement* first = model->list[0];
  137. for(size_t i = 1; i < LINES_ON_SCREEN; i++) {
  138. model->list[i - 1] = model->list[i];
  139. }
  140. furi_string_reset(first->text);
  141. model->list[model->line] = first;
  142. }
  143. }
  144. if(data != '\n' && data != '\r') {
  145. furi_string_push_back(model->list[model->line]->text, data);
  146. }
  147. }
  148. model->last_char = data;
  149. }
  150. static int32_t uart_echo_worker(void* context) {
  151. furi_assert(context);
  152. WiFiMapApp* app = context;
  153. while(1) {
  154. uint32_t events =
  155. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  156. furi_check((events & FuriFlagError) == 0);
  157. if(events & WorkerEventStop) break;
  158. if(events & WorkerEventRx) {
  159. size_t length = 0;
  160. do {
  161. uint8_t data[64];
  162. length = furi_stream_buffer_receive(app->rx_stream, data, 64, 0);
  163. if(length > 0) {
  164. furi_hal_uart_tx(FuriHalUartIdUSART1, data, length);
  165. with_view_model(
  166. app->view,
  167. UartDumpModel * model,
  168. {
  169. for(size_t i = 0; i < length; i++) {
  170. uart_echo_push_to_list(model, data[i], app);
  171. }
  172. },
  173. false);
  174. }
  175. } while(length > 0);
  176. notification_message(app->notification, &sequence_notification);
  177. with_view_model(
  178. app->view, UartDumpModel * model, { UNUSED(model); }, true);
  179. }
  180. }
  181. return 0;
  182. }
  183. static WiFiMapApp* uart_echo_app_alloc() {
  184. WiFiMapApp* app = malloc(sizeof(WiFiMapApp));
  185. app->file = open_file();
  186. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  187. // Gui
  188. app->gui = furi_record_open(RECORD_GUI);
  189. app->notification = furi_record_open(RECORD_NOTIFICATION);
  190. // View dispatcher
  191. app->view_dispatcher = view_dispatcher_alloc();
  192. view_dispatcher_enable_queue(app->view_dispatcher);
  193. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  194. // Views
  195. app->view = view_alloc();
  196. view_set_draw_callback(app->view, uart_echo_view_draw_callback);
  197. view_set_input_callback(app->view, uart_echo_view_input_callback);
  198. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  199. with_view_model(
  200. app->view,
  201. UartDumpModel * model,
  202. {
  203. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  204. model->line = 0;
  205. model->escape = false;
  206. model->list[i] = malloc(sizeof(ListElement));
  207. model->list[i]->text = furi_string_alloc();
  208. }
  209. },
  210. true);
  211. view_set_previous_callback(app->view, uart_echo_exit);
  212. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  213. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  214. app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
  215. furi_thread_start(app->worker_thread);
  216. // Enable uart listener
  217. furi_hal_console_disable();
  218. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  219. furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, uart_echo_on_irq_cb, app);
  220. return app;
  221. }
  222. static void uart_echo_app_free(WiFiMapApp* app) {
  223. furi_assert(app);
  224. furi_hal_console_enable(); // this will also clear IRQ callback so thread is no longer referenced
  225. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  226. furi_thread_join(app->worker_thread);
  227. furi_thread_free(app->worker_thread);
  228. // Free views
  229. view_dispatcher_remove_view(app->view_dispatcher, 0);
  230. with_view_model(
  231. app->view,
  232. UartDumpModel * model,
  233. {
  234. for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
  235. furi_string_free(model->list[i]->text);
  236. free(model->list[i]);
  237. }
  238. },
  239. true);
  240. view_free(app->view);
  241. view_dispatcher_free(app->view_dispatcher);
  242. // Close gui record
  243. furi_record_close(RECORD_GUI);
  244. furi_record_close(RECORD_NOTIFICATION);
  245. app->gui = NULL;
  246. furi_stream_buffer_free(app->rx_stream);
  247. close_file(app->file);
  248. // Free rest
  249. free(app);
  250. }
  251. int32_t wifi_map_app(void *p){
  252. UNUSED(p);
  253. FURI_LOG_D(TAG, "wifi_map_app");
  254. WiFiMapApp* app = uart_echo_app_alloc();
  255. view_dispatcher_run(app->view_dispatcher);
  256. uart_echo_app_free(app);
  257. return 0;
  258. }