suica.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Suica Scene
  3. *
  4. * This program is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "../../api/suica/suica_drawings.h"
  18. #include "../../metroflip_plugins.h"
  19. #include <lib/nfc/protocols/felica/felica.h>
  20. #include <lib/nfc/protocols/felica/felica_poller.h>
  21. // #include <lib/nfc/protocols/felica/felica_poller_i.h>
  22. #include <lib/bit_lib/bit_lib.h>
  23. #include <applications/services/locale/locale.h>
  24. #include <datetime.h>
  25. // Probably not needed after upstream include this in their suica_i.h
  26. #define TAG "Metroflip:Scene:Suica"
  27. const char* suica_service_names[] = {
  28. "Travel History",
  29. "Taps Log",
  30. };
  31. static void suica_model_initialize(SuicaHistoryViewModel* model, size_t initial_capacity) {
  32. model->travel_history =
  33. (uint8_t*)malloc(initial_capacity * FELICA_DATA_BLOCK_SIZE); // Each entry is 16 bytes
  34. model->size = 0;
  35. model->capacity = initial_capacity;
  36. model->entry = 1;
  37. model->page = 0;
  38. model->animator_tick = 0;
  39. model->history.entry_station.name = furi_string_alloc_set("Unknown");
  40. model->history.entry_station.jr_header = furi_string_alloc_set("0");
  41. model->history.exit_station.name = furi_string_alloc_set("Unknown");
  42. model->history.exit_station.jr_header = furi_string_alloc_set("0");
  43. model->history.entry_line = RailwaysList[SUICA_RAILWAY_NUM];
  44. model->history.exit_line = RailwaysList[SUICA_RAILWAY_NUM];
  45. }
  46. static void suica_model_initialize_after_load(SuicaHistoryViewModel* model) {
  47. model->entry = 1;
  48. model->page = 0;
  49. model->animator_tick = 0;
  50. model->history.entry_station.name = furi_string_alloc_set("Unknown");
  51. model->history.entry_station.jr_header = furi_string_alloc_set("0");
  52. model->history.exit_station.name = furi_string_alloc_set("Unknown");
  53. model->history.exit_station.jr_header = furi_string_alloc_set("0");
  54. model->history.entry_line = RailwaysList[SUICA_RAILWAY_NUM];
  55. model->history.exit_line = RailwaysList[SUICA_RAILWAY_NUM];
  56. }
  57. static void suica_add_entry(SuicaHistoryViewModel* model, const uint8_t* entry) {
  58. if(model->size <= 0) {
  59. suica_model_initialize(model, 3);
  60. }
  61. // Check if resizing is needed
  62. if(model->size == model->capacity) {
  63. size_t new_capacity = model->capacity * 2; // Double the capacity
  64. uint8_t* new_data =
  65. (uint8_t*)realloc(model->travel_history, new_capacity * FELICA_DATA_BLOCK_SIZE);
  66. model->travel_history = new_data;
  67. model->capacity = new_capacity;
  68. }
  69. // Copy the 16-byte entry to the next slot
  70. for(size_t i = 0; i < FELICA_DATA_BLOCK_SIZE; i++) {
  71. model->travel_history[(model->size * FELICA_DATA_BLOCK_SIZE) + i] = entry[i];
  72. }
  73. model->size++;
  74. }
  75. static void suica_parse_train_code(
  76. uint8_t line_code,
  77. uint8_t station_code,
  78. SuicaTrainRideType ride_type,
  79. SuicaHistoryViewModel* model) {
  80. Storage* storage = furi_record_open(RECORD_STORAGE);
  81. Stream* stream = file_stream_alloc(storage);
  82. FuriString* line = furi_string_alloc();
  83. FuriString* line_code_str = furi_string_alloc();
  84. FuriString* line_and_station_code_str = furi_string_alloc();
  85. furi_string_printf(line_code_str, "0x%02X", line_code);
  86. furi_string_printf(line_and_station_code_str, "0x%02X,0x%02X", line_code, station_code);
  87. FuriString* line_candidate = furi_string_alloc_set(SUICA_RAILWAY_UNKNOWN_NAME);
  88. FuriString* station_candidate = furi_string_alloc_set(SUICA_RAILWAY_UNKNOWN_NAME);
  89. FuriString* station_num_candidate = furi_string_alloc_set("0");
  90. FuriString* station_JR_header_candidate = furi_string_alloc_set("0");
  91. FuriString* line_copy = furi_string_alloc();
  92. size_t line_comma_ind = 0;
  93. size_t station_comma_ind = 0;
  94. size_t station_num_comma_ind = 0;
  95. size_t station_JR_header_comma_ind = 0;
  96. bool station_found = false;
  97. FuriString* file_name = furi_string_alloc();
  98. furi_string_printf(file_name, "%s0x%02X.txt", SUICA_STATION_LIST_PATH, line_code);
  99. if(file_stream_open(stream, furi_string_get_cstr(file_name), FSAM_READ, FSOM_OPEN_EXISTING)) {
  100. while(stream_read_line(stream, line) && !station_found) {
  101. // file is in csv format: station_group_id,station_id,station_sub_id,station_name
  102. // search for the station
  103. furi_string_replace_all(line, "\r", "");
  104. furi_string_replace_all(line, "\n", "");
  105. furi_string_set(line_copy, line); // 0xD5,0x02,Keikyu Main,Shinagawa,1,0
  106. if(furi_string_start_with(line, line_code_str)) {
  107. // set line name here
  108. furi_string_right(line_copy, 10); // Keikyu Main,Shinagawa,1,0
  109. furi_string_set(line_candidate, line_copy);
  110. line_comma_ind = furi_string_search_char(line_candidate, ',', 0);
  111. furi_string_left(line_candidate, line_comma_ind); // Keikyu Main
  112. // we cut the line and station code in the line line copy
  113. // and we leave only the line name for the line candidate
  114. if(furi_string_start_with(line, line_and_station_code_str)) {
  115. furi_string_set(station_candidate, line_copy); // Keikyu Main,Shinagawa,1,0
  116. furi_string_right(station_candidate, line_comma_ind + 1);
  117. station_comma_ind =
  118. furi_string_search_char(station_candidate, ',', 0); // Shinagawa,1,0
  119. furi_string_left(station_candidate, station_comma_ind); // Shinagawa
  120. station_found = true;
  121. break;
  122. }
  123. }
  124. }
  125. } else {
  126. FURI_LOG_E(TAG, "Failed to open stations.txt");
  127. }
  128. furi_string_set(station_num_candidate, line_copy); // Keikyu Main,Shinagawa,1,0
  129. furi_string_right(station_num_candidate, line_comma_ind + station_comma_ind + 2); // 1,0
  130. station_num_comma_ind = furi_string_search_char(station_num_candidate, ',', 0);
  131. furi_string_left(station_num_candidate, station_num_comma_ind); // 1
  132. furi_string_set(station_JR_header_candidate, line_copy); // Keikyu Main,Shinagawa,1,0
  133. furi_string_right(
  134. station_JR_header_candidate,
  135. line_comma_ind + station_comma_ind + station_num_comma_ind + 3); // 0
  136. station_JR_header_comma_ind = furi_string_search_char(station_JR_header_candidate, ',', 0);
  137. furi_string_left(station_JR_header_candidate, station_JR_header_comma_ind); // 0
  138. switch(ride_type) {
  139. case SuicaTrainRideEntry:
  140. model->history.entry_station.name = furi_string_alloc_set("Unknown");
  141. model->history.entry_station.jr_header = furi_string_alloc_set("0");
  142. model->history.entry_line = RailwaysList[SUICA_RAILWAY_NUM];
  143. for(size_t i = 0; i < SUICA_RAILWAY_NUM; i++) {
  144. if(furi_string_equal_str(line_candidate, RailwaysList[i].long_name)) {
  145. model->history.entry_line = RailwaysList[i];
  146. furi_string_set(model->history.entry_station.name, station_candidate);
  147. model->history.entry_station.station_number =
  148. atoi(furi_string_get_cstr(station_num_candidate));
  149. furi_string_set(
  150. model->history.entry_station.jr_header, station_JR_header_candidate);
  151. break;
  152. }
  153. }
  154. break;
  155. case SuicaTrainRideExit:
  156. model->history.exit_station.name = furi_string_alloc_set("Unknown");
  157. model->history.exit_station.jr_header = furi_string_alloc_set("0");
  158. model->history.exit_line = RailwaysList[SUICA_RAILWAY_NUM];
  159. for(size_t i = 0; i < SUICA_RAILWAY_NUM; i++) {
  160. if(furi_string_equal_str(line_candidate, RailwaysList[i].long_name)) {
  161. model->history.exit_line = RailwaysList[i];
  162. furi_string_set(model->history.exit_station.name, station_candidate);
  163. model->history.exit_station.station_number =
  164. atoi(furi_string_get_cstr(station_num_candidate));
  165. furi_string_set(
  166. model->history.exit_station.jr_header, station_JR_header_candidate);
  167. break;
  168. }
  169. }
  170. break;
  171. default:
  172. UNUSED(model);
  173. break;
  174. }
  175. furi_string_free(line);
  176. furi_string_free(line_copy);
  177. furi_string_free(line_code_str);
  178. furi_string_free(line_and_station_code_str);
  179. furi_string_free(line_candidate);
  180. furi_string_free(station_candidate);
  181. furi_string_free(station_num_candidate);
  182. furi_string_free(station_JR_header_candidate);
  183. file_stream_close(stream);
  184. stream_free(stream);
  185. furi_record_close(RECORD_STORAGE);
  186. }
  187. static void suica_parse(SuicaHistoryViewModel* my_model) {
  188. uint8_t current_block[FELICA_DATA_BLOCK_SIZE];
  189. // Parse the current block/entry
  190. for(size_t i = 0; i < FELICA_DATA_BLOCK_SIZE; i++) {
  191. current_block[i] = my_model->travel_history[((my_model->entry - 1) * 16) + i];
  192. }
  193. if(((uint8_t)current_block[4] + (uint8_t)current_block[5]) != 0) {
  194. my_model->history.year = ((uint8_t)current_block[4] & 0xFE) >> 1;
  195. my_model->history.month = (((uint8_t)current_block[4] & 0x01) << 3) |
  196. (((uint8_t)current_block[5] & 0xE0) >> 5);
  197. my_model->history.day = (uint8_t)current_block[5] & 0x1F;
  198. } else {
  199. my_model->history.year = 0;
  200. my_model->history.month = 0;
  201. my_model->history.day = 0;
  202. }
  203. my_model->history.balance = ((uint16_t)current_block[11] << 8) | (uint16_t)current_block[10];
  204. my_model->history.area_code = current_block[15];
  205. if((uint8_t)current_block[0] >= TERMINAL_TICKET_VENDING_MACHINE &&
  206. (uint8_t)current_block[0] <= TERMINAL_IN_CAR_SUPP_MACHINE) {
  207. // Train rides
  208. // Will be overwritton is is ticket sale (TERMINAL_TICKET_VENDING_MACHINE)
  209. my_model->history.history_type = SuicaHistoryTrain;
  210. uint8_t entry_line = current_block[6];
  211. uint8_t entry_station = current_block[7];
  212. uint8_t exit_line = current_block[8];
  213. uint8_t exit_station = current_block[9];
  214. suica_parse_train_code(entry_line, entry_station, SuicaTrainRideEntry, my_model);
  215. if((uint8_t)current_block[14] != 0x01) {
  216. suica_parse_train_code(exit_line, exit_station, SuicaTrainRideExit, my_model);
  217. }
  218. if(((uint8_t)current_block[4] + (uint8_t)current_block[5]) != 0) {
  219. my_model->history.year = ((uint8_t)current_block[4] & 0xFE) >> 1;
  220. my_model->history.month = (((uint8_t)current_block[4] & 0x01) << 3) |
  221. (((uint8_t)current_block[5] & 0xE0) >> 5);
  222. my_model->history.day = (uint8_t)current_block[5] & 0x1F;
  223. }
  224. }
  225. switch((uint8_t)current_block[0]) {
  226. case TERMINAL_BUS:
  227. // 6 & 7 bus line code
  228. // 8 & 9 bus stop code
  229. my_model->history.history_type = SuicaHistoryBus;
  230. break;
  231. case TERMINAL_POS_AND_TAXI:
  232. case TERMINAL_VENDING_MACHINE:
  233. // 6 & 7 are hour and minute
  234. my_model->history.history_type = ((uint8_t)current_block[0] == TERMINAL_POS_AND_TAXI) ?
  235. SuicaHistoryPosAndTaxi :
  236. SuicaHistoryVendingMachine;
  237. my_model->history.hour = ((uint8_t)current_block[6] & 0xF8) >> 3;
  238. my_model->history.minute = (((uint8_t)current_block[6] & 0x07) << 3) |
  239. (((uint8_t)current_block[7] & 0xE0) >> 5);
  240. my_model->history.shop_code = (uint8_t*)malloc(2);
  241. my_model->history.shop_code[0] = current_block[8];
  242. my_model->history.shop_code[1] = current_block[9];
  243. break;
  244. case TERMINAL_MOBILE_PHONE:
  245. break;
  246. case TERMINAL_TICKET_VENDING_MACHINE:
  247. my_model->history.history_type = SuicaHistoryHappyBirthday;
  248. break;
  249. default:
  250. if((uint8_t)current_block[0] <= TERMINAL_NULL) {
  251. my_model->history.history_type = SuicaHistoryNull;
  252. }
  253. break;
  254. }
  255. if((uint8_t)current_block[14] == 0x01) {
  256. my_model->history.history_type = SuicaHistoryHappyBirthday;
  257. }
  258. }
  259. static void suica_parse_detail_callback(GuiButtonType result, InputType type, void* context) {
  260. Metroflip* app = context;
  261. UNUSED(result);
  262. if(type == InputTypeShort) {
  263. SuicaHistoryViewModel* my_model = view_get_model(app->suica_context->view_history);
  264. suica_parse(my_model);
  265. FURI_LOG_I(TAG, "Draw Callback: We have %d entries", my_model->size);
  266. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewCanvas);
  267. }
  268. }
  269. static NfcCommand suica_poller_callback(NfcGenericEvent event, void* context) {
  270. furi_assert(event.protocol == NfcProtocolFelica);
  271. NfcCommand command = NfcCommandContinue;
  272. MetroflipPollerEventType stage = MetroflipPollerEventTypeStart;
  273. Metroflip* app = context;
  274. FuriString* parsed_data = furi_string_alloc();
  275. SuicaHistoryViewModel* model = view_get_model(app->suica_context->view_history);
  276. Widget* widget = app->widget;
  277. const uint16_t service_code[2] = {SERVICE_CODE_HISTORY_IN_LE, SERVICE_CODE_TAPS_LOG_IN_LE};
  278. const FelicaPollerEvent* felica_event = event.event_data;
  279. FelicaPollerReadCommandResponse* rx_resp;
  280. rx_resp->SF1 = 0;
  281. rx_resp->SF2 = 0;
  282. uint8_t blocks[1] = {0x00};
  283. FelicaPoller* felica_poller = event.instance;
  284. const FelicaData* felica_data = nfc_poller_get_data(app->poller);
  285. FURI_LOG_I(TAG, "Poller set");
  286. if(felica_event->type == FelicaPollerEventTypeRequestAuthContext) {
  287. view_dispatcher_send_custom_event(app->view_dispatcher, MetroflipCustomEventCardDetected);
  288. command = NfcCommandContinue;
  289. if(stage == MetroflipPollerEventTypeStart) {
  290. nfc_device_set_data(
  291. app->nfc_device, NfcProtocolFelica, nfc_poller_get_data(app->poller));
  292. furi_string_printf(parsed_data, "\e#Suica\n");
  293. FelicaError error = FelicaErrorNone;
  294. int service_code_index = 0;
  295. // Authenticate with the card
  296. // Iterate through the two services
  297. while(service_code_index < 2 && error == FelicaErrorNone) {
  298. furi_string_cat_printf(
  299. parsed_data, "%s: \n", suica_service_names[service_code_index]);
  300. rx_resp->SF1 = 0;
  301. rx_resp->SF2 = 0;
  302. blocks[0] = 0; // firmware api requires this to be a list
  303. while((rx_resp->SF1 + rx_resp->SF2) == 0 &&
  304. blocks[0] < SUICA_MAX_HISTORY_ENTRIES && error == FelicaErrorNone) {
  305. uint8_t block_data[16] = {0};
  306. error = felica_poller_read_blocks(
  307. felica_poller, 1, blocks, service_code[service_code_index], &rx_resp);
  308. if(error != FelicaErrorNone) {
  309. view_dispatcher_send_custom_event(
  310. app->view_dispatcher, MetroflipCustomEventCardLost);
  311. command = NfcCommandStop;
  312. break;
  313. }
  314. furi_string_cat_printf(parsed_data, "Block %02X\n", blocks[0]);
  315. blocks[0]++;
  316. for(size_t i = 0; i < FELICA_DATA_BLOCK_SIZE; i++) {
  317. furi_string_cat_printf(parsed_data, "%02X ", rx_resp->data[i]);
  318. block_data[i] = rx_resp->data[i];
  319. }
  320. furi_string_cat_printf(parsed_data, "\n");
  321. if(service_code_index == 0) {
  322. FURI_LOG_I(
  323. TAG,
  324. "Service code %d, adding entry %x",
  325. service_code_index,
  326. model->size);
  327. suica_add_entry(model, block_data);
  328. }
  329. }
  330. service_code_index++;
  331. }
  332. metroflip_app_blink_stop(app);
  333. if(model->size == 1) { // Have to let the poller run once before knowing we failed
  334. furi_string_printf(
  335. parsed_data,
  336. "\e#Suica\nSorry, no data found.\nPlease let the developers know and we will add support.");
  337. }
  338. if(model->size == 1 && felica_data->pmm.data[1] != SUICA_IC_TYPE_CODE) {
  339. furi_string_printf(parsed_data, "\e#Suica\nSorry, not a Suica.\n");
  340. }
  341. widget_add_text_scroll_element(
  342. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  343. widget_add_button_element(
  344. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  345. widget_add_button_element(
  346. widget, GuiButtonTypeLeft, "Del", metroflip_delete_widget_callback, app);
  347. if(model->size > 1) {
  348. widget_add_button_element(
  349. widget, GuiButtonTypeCenter, "Parse", suica_parse_detail_callback, app);
  350. }
  351. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  352. }
  353. }
  354. furi_string_free(parsed_data);
  355. command = NfcCommandStop;
  356. return command;
  357. }
  358. static bool suica_history_input_callback(InputEvent* event, void* context) {
  359. Metroflip* app = (Metroflip*)context;
  360. if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
  361. switch(event->key) {
  362. case InputKeyLeft: {
  363. bool redraw = true;
  364. with_view_model(
  365. app->suica_context->view_history,
  366. SuicaHistoryViewModel * model,
  367. {
  368. if(model->entry > 1) {
  369. model->entry--;
  370. }
  371. suica_parse(model);
  372. FURI_LOG_I(TAG, "Viewing entry %d", model->entry);
  373. },
  374. redraw);
  375. break;
  376. }
  377. case InputKeyRight: {
  378. bool redraw = true;
  379. with_view_model(
  380. app->suica_context->view_history,
  381. SuicaHistoryViewModel * model,
  382. {
  383. if(model->entry < model->size) {
  384. model->entry++;
  385. }
  386. suica_parse(model);
  387. FURI_LOG_I(TAG, "Viewing entry %d", model->entry);
  388. },
  389. redraw);
  390. break;
  391. }
  392. case InputKeyUp: {
  393. bool redraw = true;
  394. with_view_model(
  395. app->suica_context->view_history,
  396. SuicaHistoryViewModel * model,
  397. {
  398. if(model->page > 0) {
  399. model->page--;
  400. }
  401. },
  402. redraw);
  403. break;
  404. }
  405. case InputKeyDown: {
  406. bool redraw = true;
  407. with_view_model(
  408. app->suica_context->view_history,
  409. SuicaHistoryViewModel * model,
  410. {
  411. if(model->page < HISTORY_VIEW_PAGE_NUM - 1) {
  412. model->page++;
  413. }
  414. },
  415. redraw);
  416. break;
  417. }
  418. default:
  419. // Handle other keys or do nothing
  420. break;
  421. }
  422. }
  423. return false;
  424. }
  425. static void suica_on_enter(Metroflip* app) {
  426. // Gui* gui = furi_record_open(RECORD_GUI);
  427. dolphin_deed(DolphinDeedNfcRead);
  428. if(app->data_loaded == false) {
  429. app->suica_context = malloc(sizeof(SuicaContext));
  430. app->suica_context->view_history = view_alloc();
  431. view_set_context(app->suica_context->view_history, app);
  432. view_allocate_model(
  433. app->suica_context->view_history,
  434. ViewModelTypeLockFree,
  435. sizeof(SuicaHistoryViewModel));
  436. }
  437. view_set_input_callback(app->suica_context->view_history, suica_history_input_callback);
  438. view_set_previous_callback(app->suica_context->view_history, suica_navigation_raw_callback);
  439. view_set_enter_callback(app->suica_context->view_history, suica_view_history_enter_callback);
  440. view_set_exit_callback(app->suica_context->view_history, suica_view_history_exit_callback);
  441. view_set_custom_callback(
  442. app->suica_context->view_history, suica_view_history_custom_event_callback);
  443. view_set_draw_callback(app->suica_context->view_history, suica_history_draw_callback);
  444. view_dispatcher_add_view(
  445. app->view_dispatcher, MetroflipViewCanvas, app->suica_context->view_history);
  446. if(app->data_loaded == false) {
  447. popup_set_header(app->popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  448. popup_set_icon(app->popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  449. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  450. nfc_scanner_alloc(app->nfc);
  451. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolFelica);
  452. nfc_poller_start(app->poller, suica_poller_callback, app);
  453. FURI_LOG_I(TAG, "Poller started");
  454. metroflip_app_blink_start(app);
  455. } else {
  456. SuicaHistoryViewModel* model = view_get_model(app->suica_context->view_history);
  457. suica_model_initialize_after_load(model);
  458. Widget* widget = app->widget;
  459. FuriString* parsed_data = furi_string_alloc();
  460. furi_string_printf(parsed_data, "\e#Suica\n");
  461. for(uint8_t i = 0; i < model->size; i++) {
  462. furi_string_cat_printf(parsed_data, "Block %02X\n", i);
  463. for(size_t j = 0; j < FELICA_DATA_BLOCK_SIZE; j++) {
  464. furi_string_cat_printf(parsed_data, "%02X ", model->travel_history[i * 16 + j]);
  465. }
  466. furi_string_cat_printf(parsed_data, "\n");
  467. }
  468. widget_add_text_scroll_element(widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  469. widget_add_button_element(
  470. widget, GuiButtonTypeRight, "Exit", metroflip_exit_widget_callback, app);
  471. widget_add_button_element(
  472. widget, GuiButtonTypeLeft, "Del", metroflip_delete_widget_callback, app);
  473. if(model->size > 1) {
  474. widget_add_button_element(
  475. widget, GuiButtonTypeCenter, "Parse", suica_parse_detail_callback, app);
  476. }
  477. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  478. furi_string_free(parsed_data);
  479. }
  480. }
  481. static bool suica_on_event(Metroflip* app, SceneManagerEvent event) {
  482. bool consumed = false;
  483. Popup* popup = app->popup;
  484. if(event.type == SceneManagerEventTypeCustom) {
  485. if(event.event == MetroflipCustomEventCardDetected) {
  486. popup_set_header(popup, "DON'T\nMOVE", 68, 30, AlignLeft, AlignTop);
  487. consumed = true;
  488. } else if(event.event == MetroflipCustomEventCardLost) {
  489. popup_set_header(popup, "Card \n lost", 68, 30, AlignLeft, AlignTop);
  490. // popup_set_timeout(popup, 2000);
  491. // popup_enable_timeout(popup);
  492. // view_dispatcher_switch_to_view(app->view_dispatcher, SuicaViewPopup);
  493. // popup_disable_timeout(popup);
  494. scene_manager_search_and_switch_to_previous_scene(
  495. app->scene_manager, MetroflipSceneStart);
  496. consumed = true;
  497. } else if(event.event == MetroflipCustomEventWrongCard) {
  498. popup_set_header(popup, "WRONG \n CARD", 68, 30, AlignLeft, AlignTop);
  499. scene_manager_search_and_switch_to_previous_scene(
  500. app->scene_manager, MetroflipSceneStart);
  501. consumed = true;
  502. } else if(event.event == MetroflipCustomEventPollerFail) {
  503. popup_set_header(popup, "Failed", 68, 30, AlignLeft, AlignTop);
  504. scene_manager_search_and_switch_to_previous_scene(
  505. app->scene_manager, MetroflipSceneStart);
  506. consumed = true;
  507. }
  508. } else if(event.type == SceneManagerEventTypeBack) {
  509. UNUSED(popup);
  510. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  511. consumed = true;
  512. }
  513. return consumed;
  514. }
  515. static void suica_on_exit(Metroflip* app) {
  516. widget_reset(app->widget);
  517. with_view_model(
  518. app->suica_context->view_history,
  519. SuicaHistoryViewModel * model,
  520. {
  521. if(model->travel_history) { // Check if memory was allocated
  522. free(model->travel_history);
  523. model->travel_history = NULL; // Set pointer to NULL to prevent dangling references
  524. }
  525. },
  526. false);
  527. view_free_model(app->suica_context->view_history);
  528. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewCanvas);
  529. view_free(app->suica_context->view_history);
  530. free(app->suica_context);
  531. if(app->poller && !app->data_loaded) {
  532. nfc_poller_stop(app->poller);
  533. nfc_poller_free(app->poller);
  534. }
  535. }
  536. /* Actual implementation of app<>plugin interface */
  537. static const MetroflipPlugin suica_plugin = {
  538. .card_name = "Suica",
  539. .plugin_on_enter = suica_on_enter,
  540. .plugin_on_event = suica_on_event,
  541. .plugin_on_exit = suica_on_exit,
  542. };
  543. /* Plugin descriptor to comply with basic plugin specification */
  544. static const FlipperAppPluginDescriptor suica_plugin_descriptor = {
  545. .appid = METROFLIP_SUPPORTED_CARD_PLUGIN_APP_ID,
  546. .ep_api_version = METROFLIP_SUPPORTED_CARD_PLUGIN_API_VERSION,
  547. .entry_point = &suica_plugin,
  548. };
  549. /* Plugin entry point - must return a pointer to const descriptor */
  550. const FlipperAppPluginDescriptor* suica_plugin_ep(void) {
  551. return &suica_plugin_descriptor;
  552. }