eth_troubleshooter_app.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "eth_troubleshooter_app.h"
  2. #include <furi.h>
  3. #include <furi_hal_power.h>
  4. #include <gui/gui.h>
  5. #include <gui/elements.h>
  6. #include <input/input.h>
  7. #include <notification/notification_messages.h>
  8. #include "eth_worker.h"
  9. #include "eth_worker_i.h"
  10. #include "eth_view_process.h"
  11. #define TAG "EthTroubleshooterApp"
  12. static void draw_process_selector(Canvas* canvas, DrawProcess selector, CursorPosition cursor) {
  13. uint8_t y = 0;
  14. if(selector == PROCESS_INIT) y = 11;
  15. if(selector == PROCESS_DHCP) y = 22;
  16. if(selector == PROCESS_STATIC) y = 34;
  17. if(selector == PROCESS_PING) y = 44;
  18. if(selector == PROCESS_RESET) y = 55;
  19. if(cursor == CURSOR_INSIDE_PROCESS) {
  20. canvas_invert_color(canvas);
  21. canvas_draw_line(canvas, 25, y + 1, 25, y + 7);
  22. canvas_invert_color(canvas);
  23. canvas_draw_line(canvas, 0, y + 1, 0, y + 7);
  24. canvas_draw_line(canvas, 1, y, 25, y);
  25. canvas_draw_line(canvas, 1, y + 8, 25, y + 8);
  26. } else {
  27. canvas_draw_line(canvas, 0, y + 1, 0, y + 7);
  28. canvas_draw_line(canvas, 23, y + 1, 23, y + 7);
  29. canvas_draw_line(canvas, 1, y, 22, y);
  30. canvas_draw_line(canvas, 1, y + 8, 22, y + 8);
  31. }
  32. if(cursor == CURSOR_CLICK_PROCESS) {
  33. canvas_draw_box(canvas, 1, y, 22, 9);
  34. }
  35. }
  36. static void draw_module_status(Canvas* canvas, EthWorkerState state) {
  37. FuriString* string = furi_string_alloc_set("aaaaaaaaa");
  38. switch(state) {
  39. case EthWorkerStateNotInited:
  40. furi_string_printf(string, "no init");
  41. break;
  42. case EthWorkerStateDefaultNext:
  43. furi_string_printf(string, "df next");
  44. break;
  45. case EthWorkerStateInited:
  46. furi_string_printf(string, "init ok");
  47. break;
  48. case EthWorkerStateInit:
  49. furi_string_printf(string, "init");
  50. break;
  51. case EthWorkerStateModulePowerOn:
  52. furi_string_printf(string, "pwr on");
  53. break;
  54. case EthWorkerStateModuleConnect:
  55. furi_string_printf(string, "connect");
  56. break;
  57. case EthWorkerStateMACInit:
  58. furi_string_printf(string, "mac init");
  59. break;
  60. case EthWorkerStateStaticIp:
  61. furi_string_printf(string, "static ip");
  62. break;
  63. case EthWorkerStateDHCP:
  64. furi_string_printf(string, "dhcp req.");
  65. break;
  66. case EthWorkerStateOnline:
  67. furi_string_printf(string, "online");
  68. break;
  69. case EthWorkerStatePing:
  70. furi_string_printf(string, "ping");
  71. break;
  72. case EthWorkerStateStop:
  73. furi_string_printf(string, "stop");
  74. break;
  75. case EthWorkerStateReset:
  76. furi_string_printf(string, "reset");
  77. break;
  78. default:
  79. furi_string_printf(string, "unknown");
  80. break;
  81. }
  82. canvas_draw_str(canvas, 45, 7, furi_string_get_cstr(string));
  83. furi_string_free(string);
  84. }
  85. static void draw_battery_consumption(Canvas* canvas, double cons) {
  86. FuriString* string = furi_string_alloc_set("aaaaaaaa");
  87. if(cons >= 0) {
  88. furi_string_printf(string, "--");
  89. } else if(cons < -1) {
  90. furi_string_printf(string, "%1.1fk", -cons);
  91. } else {
  92. furi_string_printf(string, "%3.f", -(cons * 1000));
  93. }
  94. canvas_draw_str(canvas, 112, 7, furi_string_get_cstr(string));
  95. furi_string_free(string);
  96. }
  97. static void eth_troubleshooter_app_draw_callback(Canvas* canvas, void* ctx) {
  98. furi_assert(ctx);
  99. EthTroubleshooterApp* app = ctx;
  100. canvas_clear(canvas);
  101. DrawProcess process = app->draw_process;
  102. CursorPosition cursor = app->cursor_position;
  103. float consumption = app->info.current_gauge;
  104. canvas_set_font(canvas, FontSecondary);
  105. if(cursor == CURSOR_EXIT_ICON) {
  106. canvas_draw_icon(canvas, 0, 0, &I_exit_128x64px);
  107. } else {
  108. canvas_draw_icon(canvas, 0, 0, &I_main_128x64px);
  109. draw_process_selector(canvas, process, cursor);
  110. draw_battery_consumption(canvas, (double)consumption);
  111. ethernet_view_process_draw(app->eth_worker->active_process, canvas);
  112. draw_module_status(canvas, app->eth_worker->state);
  113. if(furi_hal_power_is_otg_enabled()) {
  114. canvas_draw_str(canvas, 22, 6, "+");
  115. } else {
  116. canvas_draw_str(canvas, 22, 6, "-");
  117. }
  118. }
  119. }
  120. static void eth_troubleshooter_battery_info_update_model(void* ctx) {
  121. furi_assert(ctx);
  122. EthTroubleshooterApp* app = ctx;
  123. power_get_info(app->power, &app->info);
  124. }
  125. static void eth_troubleshooter_app_input_callback(InputEvent* input_event, void* ctx) {
  126. furi_assert(ctx);
  127. FuriMessageQueue* event_queue = ctx;
  128. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  129. }
  130. EthTroubleshooterApp* eth_troubleshooter_app_alloc() {
  131. EthTroubleshooterApp* app = malloc(sizeof(EthTroubleshooterApp));
  132. app->view_port = view_port_alloc();
  133. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  134. app->eth_worker = eth_worker_alloc();
  135. view_port_draw_callback_set(app->view_port, eth_troubleshooter_app_draw_callback, app);
  136. view_port_input_callback_set(
  137. app->view_port, eth_troubleshooter_app_input_callback, app->event_queue);
  138. app->gui = furi_record_open(RECORD_GUI);
  139. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  140. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  141. app->power = furi_record_open(RECORD_POWER);
  142. return app;
  143. }
  144. void eth_troubleshooter_app_free(EthTroubleshooterApp* app) {
  145. furi_assert(app);
  146. view_port_enabled_set(app->view_port, false);
  147. gui_remove_view_port(app->gui, app->view_port);
  148. view_port_free(app->view_port);
  149. furi_message_queue_free(app->event_queue);
  150. eth_worker_free(app->eth_worker);
  151. furi_record_close(RECORD_GUI);
  152. furi_record_close(RECORD_NOTIFICATION);
  153. furi_record_close(RECORD_POWER);
  154. }
  155. void eth_troubleshooter_app_key_handler(EthTroubleshooterApp* app, InputKey key) {
  156. if(app->cursor_position == CURSOR_CHOOSE_PROCESS) {
  157. if(key == InputKeyUp || key == InputKeyDown) {
  158. app->draw_process =
  159. (app->draw_process + PROCESS_RESET + (key == InputKeyDown ? 2 : 0)) %
  160. (PROCESS_RESET + 1);
  161. eth_worker_set_active_process(app->eth_worker, (EthWorkerProcess)app->draw_process);
  162. } else if(key == InputKeyOk) {
  163. ethernet_view_process_move(app->eth_worker->active_process, 0);
  164. app->cursor_position = CURSOR_CLICK_PROCESS;
  165. view_port_update(app->view_port);
  166. furi_delay_ms(150);
  167. eth_run(app->eth_worker, (EthWorkerProcess)app->draw_process);
  168. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  169. } else if(key == InputKeyRight) {
  170. eth_worker_set_active_process(app->eth_worker, (EthWorkerProcess)app->draw_process);
  171. app->cursor_position = CURSOR_INSIDE_PROCESS;
  172. } else if(key == InputKeyBack) {
  173. app->cursor_position = CURSOR_EXIT_ICON;
  174. }
  175. } else if(app->cursor_position == CURSOR_INSIDE_PROCESS) {
  176. if(app->eth_worker->active_process->editing) {
  177. ethernet_view_process_keyevent(app->eth_worker->active_process, key);
  178. } else if(key == InputKeyLeft) {
  179. app->eth_worker->active_process->editing = 0;
  180. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  181. } else if(key == InputKeyBack) {
  182. ethernet_view_process_move(app->eth_worker->active_process, 0);
  183. app->eth_worker->active_process->editing = 0;
  184. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  185. } else if(key == InputKeyUp) {
  186. ethernet_view_process_move(app->eth_worker->active_process, -1);
  187. } else if(key == InputKeyDown) {
  188. ethernet_view_process_move(app->eth_worker->active_process, 1);
  189. } else if(key == InputKeyOk || key == InputKeyRight) {
  190. app->eth_worker->active_process->editing = 1;
  191. }
  192. } else if(app->cursor_position == CURSOR_EXIT_ICON) {
  193. if(key == InputKeyBack) {
  194. app->cursor_position = CURSOR_EXIT;
  195. } else if(key == InputKeyOk) {
  196. app->cursor_position = CURSOR_CHOOSE_PROCESS;
  197. }
  198. }
  199. }
  200. int32_t eth_troubleshooter_app(void* p) {
  201. UNUSED(p);
  202. EthTroubleshooterApp* app = eth_troubleshooter_app_alloc();
  203. InputEvent event;
  204. uint8_t long_press = 0;
  205. InputKey long_press_key = 0;
  206. while(1) {
  207. eth_troubleshooter_battery_info_update_model(app);
  208. if(furi_message_queue_get(app->event_queue, &event, 200) == FuriStatusOk) {
  209. if(event.type == InputTypePress) {
  210. eth_troubleshooter_app_key_handler(app, event.key);
  211. } else if(event.type == InputTypeLong) {
  212. long_press = 1;
  213. long_press_key = event.key;
  214. } else if(event.type == InputTypeRelease) {
  215. long_press = 0;
  216. long_press_key = 0;
  217. }
  218. }
  219. if(long_press && long_press_key != InputKeyBack) {
  220. eth_troubleshooter_app_key_handler(app, long_press_key);
  221. }
  222. if(app->cursor_position == CURSOR_EXIT) {
  223. eth_run(app->eth_worker, EthWorkerProcessExit);
  224. break;
  225. }
  226. view_port_update(app->view_port);
  227. }
  228. eth_troubleshooter_app_free(app);
  229. return 0;
  230. }