update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include <update/update.h>
  2. #include <flip_storage/flip_wifi_storage.h>
  3. #include <storage/storage.h>
  4. static bool update_is_str(const char *src, const char *dst) { return strcmp(src, dst) == 0; }
  5. static bool update_json_to_datetime(DateTime *rtc_time, FuriString *str)
  6. {
  7. if (!rtc_time || !str)
  8. {
  9. FURI_LOG_E(TAG, "rtc_time or str is NULL");
  10. return false;
  11. }
  12. FuriString *hour = get_json_value_furi("hour", str);
  13. if (hour)
  14. {
  15. rtc_time->hour = atoi(furi_string_get_cstr(hour));
  16. furi_string_free(hour);
  17. }
  18. FuriString *minute = get_json_value_furi("minute", str);
  19. if (minute)
  20. {
  21. rtc_time->minute = atoi(furi_string_get_cstr(minute));
  22. furi_string_free(minute);
  23. }
  24. FuriString *second = get_json_value_furi("second", str);
  25. if (second)
  26. {
  27. rtc_time->second = atoi(furi_string_get_cstr(second));
  28. furi_string_free(second);
  29. }
  30. FuriString *day = get_json_value_furi("day", str);
  31. if (day)
  32. {
  33. rtc_time->day = atoi(furi_string_get_cstr(day));
  34. furi_string_free(day);
  35. }
  36. FuriString *month = get_json_value_furi("month", str);
  37. if (month)
  38. {
  39. rtc_time->month = atoi(furi_string_get_cstr(month));
  40. furi_string_free(month);
  41. }
  42. FuriString *year = get_json_value_furi("year", str);
  43. if (year)
  44. {
  45. rtc_time->year = atoi(furi_string_get_cstr(year));
  46. furi_string_free(year);
  47. }
  48. FuriString *weekday = get_json_value_furi("weekday", str);
  49. if (weekday)
  50. {
  51. rtc_time->weekday = atoi(furi_string_get_cstr(weekday));
  52. furi_string_free(weekday);
  53. }
  54. return datetime_validate_datetime(rtc_time);
  55. }
  56. static FuriString *update_datetime_to_json(DateTime *rtc_time)
  57. {
  58. if (!rtc_time)
  59. {
  60. FURI_LOG_E(TAG, "rtc_time is NULL");
  61. return NULL;
  62. }
  63. char json[256];
  64. snprintf(
  65. json,
  66. sizeof(json),
  67. "{\"hour\":%d,\"minute\":%d,\"second\":%d,\"day\":%d,\"month\":%d,\"year\":%d,\"weekday\":%d}",
  68. rtc_time->hour,
  69. rtc_time->minute,
  70. rtc_time->second,
  71. rtc_time->day,
  72. rtc_time->month,
  73. rtc_time->year,
  74. rtc_time->weekday);
  75. return furi_string_alloc_set_str(json);
  76. }
  77. static bool update_save_rtc_time(DateTime *rtc_time)
  78. {
  79. if (!rtc_time)
  80. {
  81. FURI_LOG_E(TAG, "rtc_time is NULL");
  82. return false;
  83. }
  84. FuriString *json = update_datetime_to_json(rtc_time);
  85. if (!json)
  86. {
  87. FURI_LOG_E(TAG, "Failed to convert DateTime to JSON");
  88. return false;
  89. }
  90. save_char("last_checked", furi_string_get_cstr(json));
  91. furi_string_free(json);
  92. return true;
  93. }
  94. //
  95. // Returns true if time_current is one hour (or more) later than the stored last_updated time
  96. //
  97. static bool update_is_update_time(DateTime *time_current)
  98. {
  99. if (!time_current)
  100. {
  101. FURI_LOG_E(TAG, "time_current is NULL");
  102. return false;
  103. }
  104. char last_updated_old[128];
  105. if (!load_char("last_updated", last_updated_old, sizeof(last_updated_old)))
  106. {
  107. FURI_LOG_E(TAG, "Failed to load last_updated");
  108. FuriString *json = update_datetime_to_json(time_current);
  109. if (json)
  110. {
  111. save_char("last_updated", furi_string_get_cstr(json));
  112. furi_string_free(json);
  113. }
  114. return false;
  115. }
  116. DateTime last_updated_time;
  117. FuriString *last_updated_furi = char_to_furi_string(last_updated_old);
  118. if (!last_updated_furi)
  119. {
  120. FURI_LOG_E(TAG, "Failed to convert char to FuriString");
  121. return false;
  122. }
  123. if (!update_json_to_datetime(&last_updated_time, last_updated_furi))
  124. {
  125. FURI_LOG_E(TAG, "Failed to convert JSON to DateTime");
  126. furi_string_free(last_updated_furi);
  127. return false;
  128. }
  129. furi_string_free(last_updated_furi); // Free after usage.
  130. bool time_diff = false;
  131. // If the date is different assume more than one hour has passed.
  132. if (time_current->year != last_updated_time.year ||
  133. time_current->month != last_updated_time.month ||
  134. time_current->day != last_updated_time.day)
  135. {
  136. time_diff = true;
  137. }
  138. else
  139. {
  140. // For the same day, compute seconds from midnight.
  141. int seconds_current = time_current->hour * 3600 + time_current->minute * 60 + time_current->second;
  142. int seconds_last = last_updated_time.hour * 3600 + last_updated_time.minute * 60 + last_updated_time.second;
  143. if ((seconds_current - seconds_last) >= 3600)
  144. {
  145. time_diff = true;
  146. }
  147. }
  148. return time_diff;
  149. }
  150. // Sends a request to fetch the last updated date of the app.
  151. static bool update_last_app_update(FlipperHTTP *fhttp, bool flipper_server)
  152. {
  153. if (!fhttp)
  154. {
  155. FURI_LOG_E(TAG, "fhttp is NULL");
  156. return false;
  157. }
  158. char url[256];
  159. if (flipper_server)
  160. {
  161. // make sure folder is created
  162. char directory_path[256];
  163. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store");
  164. // Create the directory
  165. Storage *storage = furi_record_open(RECORD_STORAGE);
  166. storage_common_mkdir(storage, directory_path);
  167. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data");
  168. storage_common_mkdir(storage, directory_path);
  169. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/last_update_request.txt");
  170. storage_simply_remove_recursive(storage, directory_path); // ensure the file is empty
  171. furi_record_close(RECORD_STORAGE);
  172. fhttp->save_received_data = true;
  173. fhttp->is_bytes_request = false;
  174. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/last_update_request.txt");
  175. snprintf(url, sizeof(url), "https://catalog.flipperzero.one/api/v0/0/application/%s?is_latest_release_version=true", BUILD_ID);
  176. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  177. }
  178. else
  179. {
  180. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/app/last-updated/flip_downloader/");
  181. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  182. }
  183. }
  184. // Parses the server response and returns true if an update is available.
  185. static bool update_parse_last_app_update(FlipperHTTP *fhttp, DateTime *time_current, bool flipper_server)
  186. {
  187. if (!fhttp)
  188. {
  189. FURI_LOG_E(TAG, "fhttp is NULL");
  190. return false;
  191. }
  192. if (fhttp->state == ISSUE)
  193. {
  194. FURI_LOG_E(TAG, "Failed to fetch last app update");
  195. return false;
  196. }
  197. char version_str[32];
  198. if (!flipper_server)
  199. {
  200. if (fhttp->last_response == NULL || strlen(fhttp->last_response) == 0)
  201. {
  202. FURI_LOG_E(TAG, "fhttp->last_response is NULL or empty");
  203. return false;
  204. }
  205. char *app_version = get_json_value("version", fhttp->last_response);
  206. if (app_version)
  207. {
  208. // Save the server app version: it should save something like: 0.8
  209. save_char("server_app_version", app_version);
  210. snprintf(version_str, sizeof(version_str), "%s", app_version);
  211. free(app_version);
  212. }
  213. else
  214. {
  215. FURI_LOG_E(TAG, "Failed to get app version");
  216. return false;
  217. }
  218. }
  219. else
  220. {
  221. FuriString *app_data = flipper_http_load_from_file_with_limit(fhttp->file_path, memmgr_heap_get_max_free_block());
  222. if (!app_data)
  223. {
  224. FURI_LOG_E(TAG, "Failed to load app data");
  225. return false;
  226. }
  227. FuriString *current_version = get_json_value_furi("current_version", app_data);
  228. if (!current_version)
  229. {
  230. FURI_LOG_E(TAG, "Failed to get current version");
  231. furi_string_free(app_data);
  232. return false;
  233. }
  234. furi_string_free(app_data);
  235. FuriString *version = get_json_value_furi("version", current_version);
  236. if (!version)
  237. {
  238. FURI_LOG_E(TAG, "Failed to get version");
  239. furi_string_free(current_version);
  240. furi_string_free(app_data);
  241. return false;
  242. }
  243. // Save the server app version: it should save something like: 0.8
  244. save_char("server_app_version", furi_string_get_cstr(version));
  245. snprintf(version_str, sizeof(version_str), "%s", furi_string_get_cstr(version));
  246. furi_string_free(current_version);
  247. furi_string_free(version);
  248. // furi_string_free(app_data);
  249. }
  250. // Only check for an update if an hour or more has passed.
  251. if (update_is_update_time(time_current))
  252. {
  253. char app_version[32];
  254. if (!load_char("app_version", app_version, sizeof(app_version)))
  255. {
  256. FURI_LOG_E(TAG, "Failed to load app version");
  257. return false;
  258. }
  259. FURI_LOG_I(TAG, "App version: %s", app_version);
  260. FURI_LOG_I(TAG, "Server version: %s", version_str);
  261. // Check if the app version is different from the server version.
  262. if (!update_is_str(app_version, version_str))
  263. {
  264. easy_flipper_dialog("Update available", "New update available!\nPress BACK to download.");
  265. return true; // Update available.
  266. }
  267. FURI_LOG_I(TAG, "No update available");
  268. return false; // No update available.
  269. }
  270. FURI_LOG_I(TAG, "Not enough time has passed since the last update check");
  271. return false; // Not yet time to update.
  272. }
  273. static bool update_get_fap_file(FlipperHTTP *fhttp, bool flipper_server)
  274. {
  275. if (!fhttp)
  276. {
  277. FURI_LOG_E(TAG, "FlipperHTTP is NULL.");
  278. return false;
  279. }
  280. char url[256];
  281. fhttp->save_received_data = false;
  282. fhttp->is_bytes_request = true;
  283. #ifndef FW_ORIGIN_Momentum
  284. snprintf(
  285. fhttp->file_path,
  286. sizeof(fhttp->file_path),
  287. STORAGE_EXT_PATH_PREFIX "/apps/GPIO/flip_downloader.fap");
  288. #else
  289. snprintf(
  290. fhttp->file_path,
  291. sizeof(fhttp->file_path),
  292. STORAGE_EXT_PATH_PREFIX "/apps/GPIO/FlipperHTTP/flip_downloader.fap");
  293. #endif
  294. if (flipper_server)
  295. {
  296. char build_id[32];
  297. snprintf(build_id, sizeof(build_id), "%s", BUILD_ID);
  298. uint8_t target;
  299. target = furi_hal_version_get_hw_target();
  300. uint16_t api_major, api_minor;
  301. furi_hal_info_get_api_version(&api_major, &api_minor);
  302. snprintf(
  303. url,
  304. sizeof(url),
  305. "https://catalog.flipperzero.one/api/v0/application/version/%s/build/compatible?target=f%d&api=%d.%d",
  306. build_id,
  307. target,
  308. api_major,
  309. api_minor);
  310. }
  311. else
  312. {
  313. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/app/download/flip_downloader/");
  314. }
  315. return flipper_http_request(fhttp, BYTES, url, "{\"Content-Type\": \"application/octet-stream\"}", NULL);
  316. }
  317. // Updates the app. Uses the supplied current time for validating if update check should proceed.
  318. static bool update_update_app(FlipperHTTP *fhttp, DateTime *time_current, bool use_flipper_api)
  319. {
  320. if (!fhttp)
  321. {
  322. FURI_LOG_E(TAG, "fhttp is NULL");
  323. return false;
  324. }
  325. if (!update_last_app_update(fhttp, use_flipper_api))
  326. {
  327. FURI_LOG_E(TAG, "Failed to fetch last app update");
  328. return false;
  329. }
  330. fhttp->state = RECEIVING;
  331. furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
  332. while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
  333. {
  334. furi_delay_ms(100);
  335. }
  336. furi_timer_stop(fhttp->get_timeout_timer);
  337. if (update_parse_last_app_update(fhttp, time_current, use_flipper_api))
  338. {
  339. if (!update_get_fap_file(fhttp, false))
  340. {
  341. FURI_LOG_E(TAG, "Failed to fetch fap file 1");
  342. return false;
  343. }
  344. fhttp->state = RECEIVING;
  345. while (fhttp->state == RECEIVING)
  346. {
  347. furi_delay_ms(100);
  348. }
  349. if (fhttp->state == ISSUE)
  350. {
  351. FURI_LOG_E(TAG, "Failed to fetch fap file 2");
  352. easy_flipper_dialog("Update Error", "Failed to download the\nupdate file.\nPlease try again.");
  353. return false;
  354. }
  355. return true;
  356. }
  357. FURI_LOG_I(TAG, "No update available");
  358. return false; // No update available.
  359. }
  360. // Handles the app update routine. This function obtains the current RTC time,
  361. // checks the "last_checked" value, and if it is more than one hour old, calls for an update.
  362. bool update_is_ready(FlipperHTTP *fhttp, bool use_flipper_api)
  363. {
  364. if (!fhttp)
  365. {
  366. FURI_LOG_E(TAG, "fhttp is NULL");
  367. return false;
  368. }
  369. DateTime rtc_time;
  370. furi_hal_rtc_get_datetime(&rtc_time);
  371. char last_checked[32];
  372. if (!load_char("last_checked", last_checked, sizeof(last_checked)))
  373. {
  374. // First time – save the current time and check for an update.
  375. if (!update_save_rtc_time(&rtc_time))
  376. {
  377. FURI_LOG_E(TAG, "Failed to save RTC time");
  378. return false;
  379. }
  380. return update_update_app(fhttp, &rtc_time, use_flipper_api);
  381. }
  382. else
  383. {
  384. // Check if the current RTC time is at least one hour past the stored time.
  385. if (update_is_update_time(&rtc_time))
  386. {
  387. if (!update_update_app(fhttp, &rtc_time, use_flipper_api))
  388. {
  389. FURI_LOG_E(TAG, "Failed to update app");
  390. // save the current time for the next check.
  391. if (!update_save_rtc_time(&rtc_time))
  392. {
  393. FURI_LOG_E(TAG, "Failed to save RTC time");
  394. return false;
  395. }
  396. return false;
  397. }
  398. // Save the current time for the next check.
  399. if (!update_save_rtc_time(&rtc_time))
  400. {
  401. FURI_LOG_E(TAG, "Failed to save RTC time");
  402. return false;
  403. }
  404. return true;
  405. }
  406. return false; // No update necessary.
  407. }
  408. }