loader.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include <callback/loader.h>
  2. #include <callback/utils.h>
  3. #include <callback/callback.h>
  4. #include <alloc/flip_wifi_alloc.h>
  5. bool loader_view_alloc(void *context)
  6. {
  7. FlipWiFiApp *app = (FlipWiFiApp *)context;
  8. furi_check(app, "FlipWiFiApp is NULL");
  9. if (app->view_loader)
  10. {
  11. FURI_LOG_E(TAG, "View loader already allocated");
  12. return false;
  13. }
  14. if (app->widget_result)
  15. {
  16. FURI_LOG_E(TAG, "Widget result already allocated");
  17. return false;
  18. }
  19. view_dispatcher_set_custom_event_callback(app->view_dispatcher, loader_custom_event_callback);
  20. if (!easy_flipper_set_view(&app->view_loader, FlipWiFiViewLoader, loader_draw_callback, NULL, callback_to_submenu_main, &app->view_dispatcher, app))
  21. {
  22. return false;
  23. }
  24. loader_init(app->view_loader);
  25. return easy_flipper_set_widget(&app->widget_result, FlipWiFiViewWidgetResult, "", callback_to_submenu_main, &app->view_dispatcher);
  26. }
  27. void loader_view_free(void *context)
  28. {
  29. FlipWiFiApp *app = (FlipWiFiApp *)context;
  30. furi_check(app, "FlipWiFiApp is NULL");
  31. // Free Widget(s)
  32. if (app->widget_result)
  33. {
  34. view_dispatcher_remove_view(app->view_dispatcher, FlipWiFiViewWidgetResult);
  35. widget_free(app->widget_result);
  36. app->widget_result = NULL;
  37. }
  38. // Free View(s)
  39. if (app->view_loader)
  40. {
  41. view_dispatcher_remove_view(app->view_dispatcher, FlipWiFiViewLoader);
  42. loader_free_model(app->view_loader);
  43. view_free(app->view_loader);
  44. app->view_loader = NULL;
  45. }
  46. }
  47. static void loader_error_draw(Canvas *canvas, DataLoaderModel *model)
  48. {
  49. if (canvas == NULL)
  50. {
  51. FURI_LOG_E(TAG, "error_draw - canvas is NULL");
  52. DEV_CRASH();
  53. return;
  54. }
  55. if (model->fhttp->last_response != NULL)
  56. {
  57. if (strstr(model->fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  58. {
  59. canvas_clear(canvas);
  60. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  61. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  62. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  63. }
  64. else if (strstr(model->fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  65. {
  66. canvas_clear(canvas);
  67. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  68. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  69. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  70. }
  71. else if (strstr(model->fhttp->last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  72. {
  73. canvas_clear(canvas);
  74. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  75. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  76. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  77. }
  78. else if (strstr(model->fhttp->last_response, "[PONG]") != NULL)
  79. {
  80. canvas_clear(canvas);
  81. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  82. }
  83. else
  84. {
  85. canvas_clear(canvas);
  86. FURI_LOG_E(TAG, "Received an error: %s", model->fhttp->last_response);
  87. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  88. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  89. }
  90. }
  91. else
  92. {
  93. canvas_clear(canvas);
  94. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  95. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  96. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  97. }
  98. }
  99. static void loader_process_callback(void *context)
  100. {
  101. if (context == NULL)
  102. {
  103. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  104. DEV_CRASH();
  105. return;
  106. }
  107. FlipWiFiApp *app = (FlipWiFiApp *)context;
  108. View *view = app->view_loader;
  109. DataState current_data_state;
  110. DataLoaderModel *loader_model = NULL;
  111. with_view_model(
  112. view,
  113. DataLoaderModel * model,
  114. {
  115. current_data_state = model->data_state;
  116. loader_model = model;
  117. },
  118. false);
  119. if (!loader_model || !loader_model->fhttp)
  120. {
  121. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  122. DEV_CRASH();
  123. return;
  124. }
  125. if (current_data_state == DataStateInitial)
  126. {
  127. with_view_model(
  128. view,
  129. DataLoaderModel * model,
  130. {
  131. model->data_state = DataStateRequested;
  132. DataLoaderFetch fetch = model->fetcher;
  133. if (fetch == NULL)
  134. {
  135. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  136. model->data_state = DataStateError;
  137. return;
  138. }
  139. // Clear any previous responses
  140. strncpy(model->fhttp->last_response, "", 1);
  141. bool request_status = fetch(model);
  142. if (!request_status)
  143. {
  144. model->data_state = DataStateError;
  145. }
  146. },
  147. true);
  148. }
  149. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  150. {
  151. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  152. {
  153. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  154. {
  155. FURI_LOG_DEV(TAG, "PONG received.");
  156. }
  157. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  158. {
  159. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  160. }
  161. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  162. {
  163. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  164. }
  165. else if (strlen(loader_model->fhttp->last_response))
  166. {
  167. // Still waiting on response
  168. }
  169. else
  170. {
  171. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  172. }
  173. }
  174. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  175. {
  176. // continue waiting
  177. }
  178. else if (loader_model->fhttp->state == INACTIVE)
  179. {
  180. // inactive. try again
  181. }
  182. else if (loader_model->fhttp->state == ISSUE)
  183. {
  184. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  185. }
  186. else
  187. {
  188. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  189. DEV_CRASH();
  190. }
  191. }
  192. else if (current_data_state == DataStateReceived)
  193. {
  194. with_view_model(
  195. view,
  196. DataLoaderModel * model,
  197. {
  198. char *data_text;
  199. if (model->parser == NULL)
  200. {
  201. data_text = NULL;
  202. FURI_LOG_DEV(TAG, "Parser is NULL");
  203. DEV_CRASH();
  204. }
  205. else
  206. {
  207. data_text = model->parser(model);
  208. }
  209. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  210. model->data_text = data_text;
  211. if (data_text == NULL)
  212. {
  213. model->data_state = DataStateParseError;
  214. }
  215. else
  216. {
  217. model->data_state = DataStateParsed;
  218. }
  219. },
  220. true);
  221. }
  222. else if (current_data_state == DataStateParsed)
  223. {
  224. with_view_model(
  225. view,
  226. DataLoaderModel * model,
  227. {
  228. if (++model->request_index < model->request_count)
  229. {
  230. model->data_state = DataStateInitial;
  231. }
  232. else
  233. {
  234. loader_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  235. if (model->data_text != NULL)
  236. {
  237. free(model->data_text);
  238. model->data_text = NULL;
  239. }
  240. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  241. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewWidgetResult);
  242. }
  243. },
  244. true);
  245. }
  246. }
  247. bool loader_custom_event_callback(void *context, uint32_t index)
  248. {
  249. if (context == NULL)
  250. {
  251. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  252. DEV_CRASH();
  253. return false;
  254. }
  255. switch (index)
  256. {
  257. case FlipWiFiCustomEventProcess:
  258. loader_process_callback(context);
  259. return true;
  260. default:
  261. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  262. return false;
  263. }
  264. }
  265. void loader_draw_callback(Canvas *canvas, void *model)
  266. {
  267. if (!canvas || !model)
  268. {
  269. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  270. return;
  271. }
  272. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  273. HTTPState http_state = data_loader_model->fhttp->state;
  274. DataState data_state = data_loader_model->data_state;
  275. char *title = data_loader_model->title;
  276. canvas_set_font(canvas, FontSecondary);
  277. if (http_state == INACTIVE)
  278. {
  279. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  280. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  281. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  282. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  283. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  284. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  285. return;
  286. }
  287. if (data_state == DataStateError || data_state == DataStateParseError)
  288. {
  289. loader_error_draw(canvas, data_loader_model);
  290. return;
  291. }
  292. canvas_draw_str(canvas, 0, 7, title);
  293. canvas_draw_str(canvas, 0, 17, "Loading...");
  294. if (data_state == DataStateInitial)
  295. {
  296. return;
  297. }
  298. if (http_state == SENDING)
  299. {
  300. canvas_draw_str(canvas, 0, 27, "Fetching...");
  301. return;
  302. }
  303. if (http_state == RECEIVING || data_state == DataStateRequested)
  304. {
  305. canvas_draw_str(canvas, 0, 27, "Receiving...");
  306. return;
  307. }
  308. if (http_state == IDLE && data_state == DataStateReceived)
  309. {
  310. canvas_draw_str(canvas, 0, 27, "Processing...");
  311. return;
  312. }
  313. if (http_state == IDLE && data_state == DataStateParsed)
  314. {
  315. canvas_draw_str(canvas, 0, 27, "Processed...");
  316. return;
  317. }
  318. }
  319. static void loader_timer_callback(void *context)
  320. {
  321. if (context == NULL)
  322. {
  323. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  324. DEV_CRASH();
  325. return;
  326. }
  327. FlipWiFiApp *app = (FlipWiFiApp *)context;
  328. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWiFiCustomEventProcess);
  329. }
  330. static void loader_on_enter(void *context)
  331. {
  332. if (context == NULL)
  333. {
  334. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  335. DEV_CRASH();
  336. return;
  337. }
  338. FlipWiFiApp *app = (FlipWiFiApp *)context;
  339. View *view = app->view_loader;
  340. with_view_model(
  341. view,
  342. DataLoaderModel * model,
  343. {
  344. view_set_previous_callback(view, model->back_callback);
  345. if (model->timer == NULL)
  346. {
  347. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  348. }
  349. furi_timer_start(model->timer, 250);
  350. },
  351. true);
  352. }
  353. static void loader_on_exit(void *context)
  354. {
  355. if (context == NULL)
  356. {
  357. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  358. DEV_CRASH();
  359. return;
  360. }
  361. FlipWiFiApp *app = (FlipWiFiApp *)context;
  362. View *view = app->view_loader;
  363. with_view_model(
  364. view,
  365. DataLoaderModel * model,
  366. {
  367. if (model->timer)
  368. {
  369. furi_timer_stop(model->timer);
  370. }
  371. },
  372. false);
  373. }
  374. void loader_init(View *view)
  375. {
  376. if (view == NULL)
  377. {
  378. FURI_LOG_E(TAG, "loader_init - view is NULL");
  379. DEV_CRASH();
  380. return;
  381. }
  382. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  383. view_set_enter_callback(view, loader_on_enter);
  384. view_set_exit_callback(view, loader_on_exit);
  385. }
  386. void loader_free_model(View *view)
  387. {
  388. if (view == NULL)
  389. {
  390. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  391. DEV_CRASH();
  392. return;
  393. }
  394. with_view_model(
  395. view,
  396. DataLoaderModel * model,
  397. {
  398. if (model->timer)
  399. {
  400. furi_timer_free(model->timer);
  401. model->timer = NULL;
  402. }
  403. if (model->parser_context)
  404. {
  405. // do not free the context here, it is the app context
  406. // free(model->parser_context);
  407. // model->parser_context = NULL;
  408. }
  409. if (model->fhttp)
  410. {
  411. flipper_http_free(model->fhttp);
  412. model->fhttp = NULL;
  413. }
  414. },
  415. false);
  416. }
  417. void loader_switch_to_view(FlipWiFiApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  418. {
  419. if (app == NULL)
  420. {
  421. FURI_LOG_E(TAG, "loader_switch_to_view - app is NULL");
  422. DEV_CRASH();
  423. return;
  424. }
  425. View *view = app->view_loader;
  426. if (view == NULL)
  427. {
  428. FURI_LOG_E(TAG, "loader_switch_to_view - view is NULL");
  429. DEV_CRASH();
  430. return;
  431. }
  432. with_view_model(
  433. view,
  434. DataLoaderModel * model,
  435. {
  436. model->title = title;
  437. model->fetcher = fetcher;
  438. model->parser = parser;
  439. model->request_index = 0;
  440. model->request_count = request_count;
  441. model->back_callback = back;
  442. model->data_state = DataStateInitial;
  443. model->data_text = NULL;
  444. //
  445. model->parser_context = app;
  446. if (!model->fhttp)
  447. {
  448. model->fhttp = flipper_http_alloc();
  449. }
  450. },
  451. true);
  452. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  453. }
  454. void loader_widget_set_text(char *message, Widget **widget)
  455. {
  456. if (widget == NULL)
  457. {
  458. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  459. DEV_CRASH();
  460. return;
  461. }
  462. if (message == NULL)
  463. {
  464. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  465. DEV_CRASH();
  466. return;
  467. }
  468. widget_reset(*widget);
  469. uint32_t message_length = strlen(message); // Length of the message
  470. uint32_t i = 0; // Index tracker
  471. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  472. char *formatted_message; // Buffer to hold the final formatted message
  473. // Allocate buffer with double the message length plus one for safety
  474. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  475. {
  476. return;
  477. }
  478. while (i < message_length)
  479. {
  480. uint32_t max_line_length = 31; // Maximum characters per line
  481. uint32_t remaining_length = message_length - i; // Remaining characters
  482. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  483. // Check for newline character within the current segment
  484. uint32_t newline_pos = i;
  485. bool found_newline = false;
  486. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  487. {
  488. if (message[newline_pos] == '\n')
  489. {
  490. found_newline = true;
  491. break;
  492. }
  493. }
  494. if (found_newline)
  495. {
  496. // If newline found, set line_length up to the newline
  497. line_length = newline_pos - i;
  498. }
  499. // Temporary buffer to hold the current line
  500. char line[32];
  501. strncpy(line, message + i, line_length);
  502. line[line_length] = '\0';
  503. // If newline was found, skip it for the next iteration
  504. if (found_newline)
  505. {
  506. i += line_length + 1; // +1 to skip the '\n' character
  507. }
  508. else
  509. {
  510. // Check if the line ends in the middle of a word and adjust accordingly
  511. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  512. {
  513. // Find the last space within the current line to avoid breaking a word
  514. char *last_space = strrchr(line, ' ');
  515. if (last_space != NULL)
  516. {
  517. // Adjust the line_length to avoid cutting the word
  518. line_length = last_space - line;
  519. line[line_length] = '\0'; // Null-terminate at the space
  520. }
  521. }
  522. // Move the index forward by the determined line_length
  523. i += line_length;
  524. // Skip any spaces at the beginning of the next line
  525. while (i < message_length && message[i] == ' ')
  526. {
  527. i++;
  528. }
  529. }
  530. // Manually copy the fixed line into the formatted_message buffer
  531. for (uint32_t j = 0; j < line_length; j++)
  532. {
  533. formatted_message[formatted_index++] = line[j];
  534. }
  535. // Add a newline character for line spacing
  536. formatted_message[formatted_index++] = '\n';
  537. }
  538. // Null-terminate the formatted_message
  539. formatted_message[formatted_index] = '\0';
  540. // Add the formatted message to the widget
  541. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  542. }