update.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #include <update/update.h>
  2. #include <storage/storage.h>
  3. static bool update_is_str(const char *src, const char *dst) { return strcmp(src, dst) == 0; }
  4. static bool update_save_char(
  5. const char *path_name, const char *value)
  6. {
  7. if (!value)
  8. {
  9. return false;
  10. }
  11. // Create the directory for saving settings
  12. char directory_path[256];
  13. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s", APP_ID);
  14. // Create the directory
  15. Storage *storage = furi_record_open(RECORD_STORAGE);
  16. storage_common_mkdir(storage, directory_path);
  17. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data", APP_ID);
  18. storage_common_mkdir(storage, directory_path);
  19. // Open the settings file
  20. File *file = storage_file_alloc(storage);
  21. char file_path[256];
  22. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/%s.txt", APP_ID, path_name);
  23. // Open the file in write mode
  24. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  25. {
  26. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  27. storage_file_free(file);
  28. furi_record_close(RECORD_STORAGE);
  29. return false;
  30. }
  31. // Write the data to the file
  32. size_t data_size = strlen(value) + 1; // Include null terminator
  33. if (storage_file_write(file, value, data_size) != data_size)
  34. {
  35. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  36. storage_file_close(file);
  37. storage_file_free(file);
  38. furi_record_close(RECORD_STORAGE);
  39. return false;
  40. }
  41. storage_file_close(file);
  42. storage_file_free(file);
  43. furi_record_close(RECORD_STORAGE);
  44. return true;
  45. }
  46. static bool update_load_char(
  47. const char *path_name,
  48. char *value,
  49. size_t value_size)
  50. {
  51. if (!value)
  52. {
  53. return false;
  54. }
  55. Storage *storage = furi_record_open(RECORD_STORAGE);
  56. File *file = storage_file_alloc(storage);
  57. char file_path[256];
  58. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/%s.txt", APP_ID, path_name);
  59. // Open the file for reading
  60. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  61. {
  62. storage_file_free(file);
  63. furi_record_close(RECORD_STORAGE);
  64. return false;
  65. }
  66. // Read data into the buffer
  67. size_t read_count = storage_file_read(file, value, value_size);
  68. if (storage_file_get_error(file) != FSE_OK)
  69. {
  70. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  71. storage_file_close(file);
  72. storage_file_free(file);
  73. furi_record_close(RECORD_STORAGE);
  74. return false;
  75. }
  76. // Ensure null-termination
  77. value[read_count - 1] = '\0';
  78. storage_file_close(file);
  79. storage_file_free(file);
  80. furi_record_close(RECORD_STORAGE);
  81. return strlen(value) > 0;
  82. }
  83. static bool update_json_to_datetime(DateTime *rtc_time, FuriString *str)
  84. {
  85. if (!rtc_time || !str)
  86. {
  87. FURI_LOG_E(TAG, "rtc_time or str is NULL");
  88. return false;
  89. }
  90. FuriString *hour = get_json_value_furi("hour", str);
  91. if (hour)
  92. {
  93. rtc_time->hour = atoi(furi_string_get_cstr(hour));
  94. furi_string_free(hour);
  95. }
  96. FuriString *minute = get_json_value_furi("minute", str);
  97. if (minute)
  98. {
  99. rtc_time->minute = atoi(furi_string_get_cstr(minute));
  100. furi_string_free(minute);
  101. }
  102. FuriString *second = get_json_value_furi("second", str);
  103. if (second)
  104. {
  105. rtc_time->second = atoi(furi_string_get_cstr(second));
  106. furi_string_free(second);
  107. }
  108. FuriString *day = get_json_value_furi("day", str);
  109. if (day)
  110. {
  111. rtc_time->day = atoi(furi_string_get_cstr(day));
  112. furi_string_free(day);
  113. }
  114. FuriString *month = get_json_value_furi("month", str);
  115. if (month)
  116. {
  117. rtc_time->month = atoi(furi_string_get_cstr(month));
  118. furi_string_free(month);
  119. }
  120. FuriString *year = get_json_value_furi("year", str);
  121. if (year)
  122. {
  123. rtc_time->year = atoi(furi_string_get_cstr(year));
  124. furi_string_free(year);
  125. }
  126. FuriString *weekday = get_json_value_furi("weekday", str);
  127. if (weekday)
  128. {
  129. rtc_time->weekday = atoi(furi_string_get_cstr(weekday));
  130. furi_string_free(weekday);
  131. }
  132. return datetime_validate_datetime(rtc_time);
  133. }
  134. static FuriString *update_datetime_to_json(DateTime *rtc_time)
  135. {
  136. if (!rtc_time)
  137. {
  138. FURI_LOG_E(TAG, "rtc_time is NULL");
  139. return NULL;
  140. }
  141. char json[256];
  142. snprintf(
  143. json,
  144. sizeof(json),
  145. "{\"hour\":%d,\"minute\":%d,\"second\":%d,\"day\":%d,\"month\":%d,\"year\":%d,\"weekday\":%d}",
  146. rtc_time->hour,
  147. rtc_time->minute,
  148. rtc_time->second,
  149. rtc_time->day,
  150. rtc_time->month,
  151. rtc_time->year,
  152. rtc_time->weekday);
  153. return furi_string_alloc_set_str(json);
  154. }
  155. static bool update_save_rtc_time(DateTime *rtc_time)
  156. {
  157. if (!rtc_time)
  158. {
  159. FURI_LOG_E(TAG, "rtc_time is NULL");
  160. return false;
  161. }
  162. FuriString *json = update_datetime_to_json(rtc_time);
  163. if (!json)
  164. {
  165. FURI_LOG_E(TAG, "Failed to convert DateTime to JSON");
  166. return false;
  167. }
  168. update_save_char("last_checked", furi_string_get_cstr(json));
  169. furi_string_free(json);
  170. return true;
  171. }
  172. //
  173. // Returns true if time_current is one hour (or more) later than the stored last_checked time
  174. //
  175. static bool update_is_update_time(DateTime *time_current)
  176. {
  177. if (!time_current)
  178. {
  179. FURI_LOG_E(TAG, "time_current is NULL");
  180. return false;
  181. }
  182. char last_checked_old[128];
  183. if (!update_load_char("last_checked", last_checked_old, sizeof(last_checked_old)))
  184. {
  185. FURI_LOG_E(TAG, "Failed to load last_checked");
  186. FuriString *json = update_datetime_to_json(time_current);
  187. if (json)
  188. {
  189. update_save_char("last_checked", furi_string_get_cstr(json));
  190. furi_string_free(json);
  191. }
  192. return false;
  193. }
  194. DateTime last_updated_time;
  195. FuriString *last_updated_furi = char_to_furi_string(last_checked_old);
  196. if (!last_updated_furi)
  197. {
  198. FURI_LOG_E(TAG, "Failed to convert char to FuriString");
  199. return false;
  200. }
  201. if (!update_json_to_datetime(&last_updated_time, last_updated_furi))
  202. {
  203. FURI_LOG_E(TAG, "Failed to convert JSON to DateTime");
  204. furi_string_free(last_updated_furi);
  205. return false;
  206. }
  207. furi_string_free(last_updated_furi); // Free after usage.
  208. bool time_diff = false;
  209. // If the date is different assume more than one hour has passed.
  210. if (time_current->year != last_updated_time.year ||
  211. time_current->month != last_updated_time.month ||
  212. time_current->day != last_updated_time.day)
  213. {
  214. time_diff = true;
  215. }
  216. else
  217. {
  218. // For the same day, compute seconds from midnight.
  219. int seconds_current = time_current->hour * 3600 + time_current->minute * 60 + time_current->second;
  220. int seconds_last = last_updated_time.hour * 3600 + last_updated_time.minute * 60 + last_updated_time.second;
  221. if ((seconds_current - seconds_last) >= 3600)
  222. {
  223. time_diff = true;
  224. }
  225. }
  226. return time_diff;
  227. }
  228. // Sends a request to fetch the last updated date of the app.
  229. static bool update_last_app_update(FlipperHTTP *fhttp, bool flipper_server)
  230. {
  231. if (!fhttp)
  232. {
  233. FURI_LOG_E(TAG, "fhttp is NULL");
  234. return false;
  235. }
  236. char url[256];
  237. if (flipper_server)
  238. {
  239. // make sure folder is created
  240. char directory_path[256];
  241. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s", APP_ID);
  242. // Create the directory
  243. Storage *storage = furi_record_open(RECORD_STORAGE);
  244. storage_common_mkdir(storage, directory_path);
  245. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data", APP_ID);
  246. storage_common_mkdir(storage, directory_path);
  247. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/last_update_request.txt", APP_ID);
  248. storage_simply_remove_recursive(storage, directory_path); // ensure the file is empty
  249. furi_record_close(RECORD_STORAGE);
  250. fhttp->save_received_data = false;
  251. fhttp->is_bytes_request = true;
  252. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/last_update_request.txt", APP_ID);
  253. snprintf(url, sizeof(url), "https://raw.githubusercontent.com/flipperdevices/flipper-application-catalog/main/applications/%s/%s/manifest.yml", APP_FOLDER, FAP_ID);
  254. return flipper_http_request(fhttp, BYTES, url, "{\"Content-Type\":\"application/json\"}", NULL);
  255. }
  256. else
  257. {
  258. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/app/last-updated/%s/", FAP_ID);
  259. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  260. }
  261. }
  262. static bool parse_yaml_version(const char *yaml, char *out_version, size_t out_len)
  263. {
  264. const char *p = strstr(yaml, "\nversion:");
  265. if (!p)
  266. {
  267. // maybe it's the very first line
  268. p = yaml;
  269. }
  270. else
  271. {
  272. // skip the “\n”
  273. p++;
  274. }
  275. // skip the key name and colon
  276. p = strstr(p, "version");
  277. if (!p)
  278. return false;
  279. p += strlen("version");
  280. // skip whitespace and colon
  281. while (*p == ' ' || *p == ':')
  282. p++;
  283. // handle optional quote
  284. bool quoted = (*p == '"');
  285. if (quoted)
  286. p++;
  287. // copy up until end‐quote or newline/space
  288. size_t i = 0;
  289. while (*p && i + 1 < out_len)
  290. {
  291. if ((quoted && *p == '"') ||
  292. (!quoted && (*p == '\n' || *p == ' ')))
  293. {
  294. break;
  295. }
  296. out_version[i++] = *p++;
  297. }
  298. out_version[i] = '\0';
  299. return (i > 0);
  300. }
  301. // Parses the server response and returns true if an update is available.
  302. static bool update_parse_last_app_update(FlipperHTTP *fhttp, DateTime *time_current, bool flipper_server)
  303. {
  304. if (!fhttp)
  305. {
  306. FURI_LOG_E(TAG, "fhttp is NULL");
  307. return false;
  308. }
  309. if (fhttp->state == ISSUE)
  310. {
  311. FURI_LOG_E(TAG, "Failed to fetch last app update");
  312. return false;
  313. }
  314. char version_str[32];
  315. if (!flipper_server)
  316. {
  317. if (fhttp->last_response == NULL || strlen(fhttp->last_response) == 0)
  318. {
  319. FURI_LOG_E(TAG, "fhttp->last_response is NULL or empty");
  320. return false;
  321. }
  322. char *app_version = get_json_value("version", fhttp->last_response);
  323. if (app_version)
  324. {
  325. // Save the server app version: it should save something like: 0.8
  326. update_save_char("server_app_version", app_version);
  327. snprintf(version_str, sizeof(version_str), "%s", app_version);
  328. free(app_version);
  329. }
  330. else
  331. {
  332. FURI_LOG_E(TAG, "Failed to get app version");
  333. return false;
  334. }
  335. }
  336. else
  337. {
  338. FuriString *manifest_data = flipper_http_load_from_file(fhttp->file_path);
  339. if (!manifest_data)
  340. {
  341. FURI_LOG_E(TAG, "Failed to load app data");
  342. return false;
  343. }
  344. // parse version out of the YAML
  345. if (!parse_yaml_version(furi_string_get_cstr(manifest_data), version_str, sizeof(version_str)))
  346. {
  347. FURI_LOG_E(TAG, "Failed to parse version from YAML manifest");
  348. return false;
  349. }
  350. update_save_char("server_app_version", version_str);
  351. furi_string_free(manifest_data);
  352. }
  353. // Only check for an update if an hour or more has passed.
  354. if (update_is_update_time(time_current))
  355. {
  356. char app_version[32];
  357. if (!update_load_char("app_version", app_version, sizeof(app_version)))
  358. {
  359. FURI_LOG_E(TAG, "Failed to load app version");
  360. return false;
  361. }
  362. // Check if the app version is different from the server version.
  363. if (!update_is_str(app_version, version_str))
  364. {
  365. easy_flipper_dialog("Update available", "New update available!\nPress BACK to download.");
  366. return true; // Update available.
  367. }
  368. FURI_LOG_I(TAG, "No update available");
  369. return false; // No update available.
  370. }
  371. FURI_LOG_I(TAG, "Not enough time has passed since the last update check");
  372. return false; // Not yet time to update.
  373. }
  374. static bool update_get_fap_file(FlipperHTTP *fhttp, bool flipper_server)
  375. {
  376. if (!fhttp)
  377. {
  378. FURI_LOG_E(TAG, "FlipperHTTP is NULL.");
  379. return false;
  380. }
  381. char url[256];
  382. fhttp->save_received_data = false;
  383. fhttp->is_bytes_request = true;
  384. #ifndef FW_ORIGIN_Momentum
  385. snprintf(
  386. fhttp->file_path,
  387. sizeof(fhttp->file_path),
  388. STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", APP_FOLDER, FAP_ID);
  389. #else
  390. if (strlen(MOM_FOLDER) == 0)
  391. snprintf(
  392. fhttp->file_path,
  393. sizeof(fhttp->file_path),
  394. STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", APP_FOLDER, FAP_ID);
  395. else
  396. snprintf(
  397. fhttp->file_path,
  398. sizeof(fhttp->file_path),
  399. STORAGE_EXT_PATH_PREFIX "/apps/%s/%s/%s.fap", APP_FOLDER, MOM_FOLDER, FAP_ID);
  400. #endif
  401. if (flipper_server)
  402. {
  403. uint8_t target;
  404. target = furi_hal_version_get_hw_target();
  405. uint16_t api_major, api_minor;
  406. furi_hal_info_get_api_version(&api_major, &api_minor);
  407. snprintf(
  408. url,
  409. sizeof(url),
  410. "https://catalog.flipperzero.one/api/v0/application/version/%s/build/compatible?target=f%d&api=%d.%d",
  411. BUILD_ID,
  412. target,
  413. api_major,
  414. api_minor);
  415. }
  416. else
  417. {
  418. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/app/download/%s/", FAP_ID);
  419. }
  420. return flipper_http_request(fhttp, BYTES, url, "{\"Content-Type\": \"application/octet-stream\"}", NULL);
  421. }
  422. // Updates the app. Uses the supplied current time for validating if update check should proceed.
  423. static bool update_update_app(FlipperHTTP *fhttp, DateTime *time_current, bool use_flipper_api)
  424. {
  425. if (!fhttp)
  426. {
  427. FURI_LOG_E(TAG, "fhttp is NULL");
  428. return false;
  429. }
  430. if (!update_last_app_update(fhttp, use_flipper_api))
  431. {
  432. FURI_LOG_E(TAG, "Failed to fetch last app update");
  433. return false;
  434. }
  435. fhttp->state = RECEIVING;
  436. furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
  437. while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
  438. {
  439. furi_delay_ms(100);
  440. }
  441. furi_timer_stop(fhttp->get_timeout_timer);
  442. if (update_parse_last_app_update(fhttp, time_current, use_flipper_api))
  443. {
  444. if (!update_get_fap_file(fhttp, false))
  445. {
  446. FURI_LOG_E(TAG, "Failed to fetch fap file 1");
  447. return false;
  448. }
  449. fhttp->state = RECEIVING;
  450. while (fhttp->state == RECEIVING)
  451. {
  452. furi_delay_ms(100);
  453. }
  454. if (fhttp->state == ISSUE)
  455. {
  456. FURI_LOG_E(TAG, "Failed to fetch fap file 2");
  457. easy_flipper_dialog("Update Error", "Failed to download the\nupdate file.\nPlease try again.");
  458. return false;
  459. }
  460. return true;
  461. }
  462. FURI_LOG_I(TAG, "No update available");
  463. return false; // No update available.
  464. }
  465. // Handles the app update routine. This function obtains the current RTC time,
  466. // checks the "last_checked" value, and if it is more than one hour old, calls for an update.
  467. bool update_is_ready(FlipperHTTP *fhttp, bool use_flipper_api)
  468. {
  469. if (!fhttp)
  470. {
  471. FURI_LOG_E(TAG, "fhttp is NULL");
  472. return false;
  473. }
  474. DateTime rtc_time;
  475. furi_hal_rtc_get_datetime(&rtc_time);
  476. char last_checked[32];
  477. if (!update_load_char("last_checked", last_checked, sizeof(last_checked)))
  478. {
  479. // First time – save the current time and check for an update.
  480. if (!update_save_rtc_time(&rtc_time))
  481. {
  482. FURI_LOG_E(TAG, "Failed to save RTC time");
  483. return false;
  484. }
  485. return update_update_app(fhttp, &rtc_time, use_flipper_api);
  486. }
  487. else
  488. {
  489. // Check if the current RTC time is at least one hour past the stored time.
  490. if (update_is_update_time(&rtc_time))
  491. {
  492. if (!update_update_app(fhttp, &rtc_time, use_flipper_api))
  493. {
  494. // save the last_checked for the next check.
  495. if (!update_save_rtc_time(&rtc_time))
  496. {
  497. FURI_LOG_E(TAG, "Failed to save RTC time");
  498. return false;
  499. }
  500. return false;
  501. }
  502. // Save the current time for the next check.
  503. if (!update_save_rtc_time(&rtc_time))
  504. {
  505. FURI_LOG_E(TAG, "Failed to save RTC time");
  506. return false;
  507. }
  508. return true;
  509. }
  510. return false; // No update necessary.
  511. }
  512. }