wifi_map.c 9.1 KB

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