loader.c 18 KB

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