metroflip_scene_navigo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "../metroflip_i.h"
  2. #include <datetime.h>
  3. #include <dolphin/dolphin.h>
  4. #include <locale/locale.h>
  5. #include "navigo.h"
  6. #include <nfc/protocols/iso14443_4b/iso14443_4b_poller.h>
  7. #define TAG "Metroflip:Scene:Navigo"
  8. void metroflip_navigo_widget_callback(GuiButtonType result, InputType type, void* context) {
  9. Metroflip* app = context;
  10. UNUSED(result);
  11. if(type == InputTypeShort) {
  12. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  13. }
  14. }
  15. static NfcCommand metroflip_scene_navigo_poller_callback(NfcGenericEvent event, void* context) {
  16. furi_assert(event.protocol == NfcProtocolIso14443_4b);
  17. NfcCommand next_command = NfcCommandContinue;
  18. MetroflipPollerEventType stage = MetroflipPollerEventTypeStart;
  19. Metroflip* app = context;
  20. FuriString* parsed_data = furi_string_alloc();
  21. Widget* widget = app->widget;
  22. furi_string_reset(app->text_box_store);
  23. const Iso14443_4bPollerEvent* iso14443_4b_event = event.event_data;
  24. Iso14443_4bPoller* iso14443_4b_poller = event.instance;
  25. BitBuffer* tx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  26. BitBuffer* rx_buffer = bit_buffer_alloc(Metroflip_POLLER_MAX_BUFFER_SIZE);
  27. if(iso14443_4b_event->type == Iso14443_4bPollerEventTypeReady) {
  28. if(stage == MetroflipPollerEventTypeStart) {
  29. nfc_device_set_data(
  30. app->nfc_device, NfcProtocolIso14443_4b, nfc_poller_get_data(app->poller));
  31. Iso14443_4bError error;
  32. size_t response_length = 0;
  33. do {
  34. // Select app for contracts
  35. select_app[6] = 32;
  36. bit_buffer_reset(tx_buffer);
  37. bit_buffer_append_bytes(tx_buffer, select_app, sizeof(select_app));
  38. error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  39. if(error != Iso14443_4bErrorNone) {
  40. FURI_LOG_I(TAG, "Select File: iso14443_4b_poller_send_block error %d", error);
  41. stage = MetroflipPollerEventTypeFail;
  42. view_dispatcher_send_custom_event(
  43. app->view_dispatcher, MetroflipCustomEventPollerFail);
  44. break;
  45. }
  46. // Check the response after selecting app
  47. response_length = bit_buffer_get_size_bytes(rx_buffer);
  48. if(bit_buffer_get_byte(rx_buffer, response_length - 2) != apdu_success[0] ||
  49. bit_buffer_get_byte(rx_buffer, response_length - 1) != apdu_success[1]) {
  50. FURI_LOG_I(
  51. TAG,
  52. "Select profile app failed: %02x%02x",
  53. bit_buffer_get_byte(rx_buffer, response_length - 2),
  54. bit_buffer_get_byte(rx_buffer, response_length - 1));
  55. stage = MetroflipPollerEventTypeFail;
  56. view_dispatcher_send_custom_event(
  57. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  58. break;
  59. }
  60. // read file 1
  61. read_file[2] = 1;
  62. bit_buffer_reset(tx_buffer);
  63. bit_buffer_append_bytes(tx_buffer, read_file, sizeof(read_file));
  64. error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  65. if(error != Iso14443_4bErrorNone) {
  66. FURI_LOG_I(TAG, "Read File: iso14443_4b_poller_send_block error %d", error);
  67. stage = MetroflipPollerEventTypeFail;
  68. view_dispatcher_send_custom_event(
  69. app->view_dispatcher, MetroflipCustomEventPollerFail);
  70. break;
  71. }
  72. // Check the response after reading the file
  73. response_length = bit_buffer_get_size_bytes(rx_buffer);
  74. if(bit_buffer_get_byte(rx_buffer, response_length - 2) != apdu_success[0] ||
  75. bit_buffer_get_byte(rx_buffer, response_length - 1) != apdu_success[1]) {
  76. FURI_LOG_I(
  77. TAG,
  78. "Read file failed: %02x%02x",
  79. bit_buffer_get_byte(rx_buffer, response_length - 2),
  80. bit_buffer_get_byte(rx_buffer, response_length - 1));
  81. stage = MetroflipPollerEventTypeFail;
  82. view_dispatcher_send_custom_event(
  83. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  84. break;
  85. }
  86. char bit_representation[response_length * 8 + 1];
  87. bit_representation[0] = '\0';
  88. for(size_t i = 0; i < response_length; i++) {
  89. char bits[9];
  90. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  91. byte_to_binary(byte, bits);
  92. for(int j = 0; j < 8; j++) {
  93. bit_representation[i * 8 + j] = bits[j];
  94. }
  95. }
  96. bit_representation[response_length * 8] = '\0';
  97. int start = 55, end = 70;
  98. float decimal_value = bit_slice_to_dec(bit_representation, start, end);
  99. float balance = decimal_value / 100;
  100. furi_string_printf(parsed_data, "\e#Navigo:\n\n");
  101. furi_string_cat_printf(parsed_data, "\e#Contract 1:\n");
  102. furi_string_cat_printf(parsed_data, "Balance: %.2f EUR\n", (double)balance);
  103. start = 80, end = 93;
  104. decimal_value = bit_slice_to_dec(bit_representation, start, end);
  105. uint64_t start_date_timestamp = (decimal_value * 24 * 3600) + (float)epoch + 3600;
  106. DateTime start_dt = {0};
  107. datetime_timestamp_to_datetime(start_date_timestamp, &start_dt);
  108. furi_string_cat_printf(parsed_data, "\nStart Date:\n");
  109. locale_format_datetime_cat(parsed_data, &start_dt, false);
  110. furi_string_cat_printf(parsed_data, "\n");
  111. // Select app for environment
  112. select_app[6] = 1;
  113. bit_buffer_reset(tx_buffer);
  114. bit_buffer_append_bytes(tx_buffer, select_app, sizeof(select_app));
  115. error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  116. if(error != Iso14443_4bErrorNone) {
  117. FURI_LOG_I(TAG, "Select File: iso14443_4b_poller_send_block error %d", error);
  118. stage = MetroflipPollerEventTypeFail;
  119. view_dispatcher_send_custom_event(
  120. app->view_dispatcher, MetroflipCustomEventPollerFail);
  121. break;
  122. }
  123. // Check the response after selecting app
  124. response_length = bit_buffer_get_size_bytes(rx_buffer);
  125. if(bit_buffer_get_byte(rx_buffer, response_length - 2) != apdu_success[0] ||
  126. bit_buffer_get_byte(rx_buffer, response_length - 1) != apdu_success[1]) {
  127. FURI_LOG_I(
  128. TAG,
  129. "Select profile app failed: %02x%02x",
  130. bit_buffer_get_byte(rx_buffer, response_length - 2),
  131. bit_buffer_get_byte(rx_buffer, response_length - 1));
  132. stage = MetroflipPollerEventTypeFail;
  133. view_dispatcher_send_custom_event(
  134. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  135. break;
  136. }
  137. // read file 1
  138. read_file[2] = 1;
  139. bit_buffer_reset(tx_buffer);
  140. bit_buffer_append_bytes(tx_buffer, read_file, sizeof(read_file));
  141. error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  142. if(error != Iso14443_4bErrorNone) {
  143. FURI_LOG_I(TAG, "Read File: iso14443_4b_poller_send_block error %d", error);
  144. stage = MetroflipPollerEventTypeFail;
  145. view_dispatcher_send_custom_event(
  146. app->view_dispatcher, MetroflipCustomEventPollerFail);
  147. break;
  148. }
  149. // Check the response after reading the file
  150. response_length = bit_buffer_get_size_bytes(rx_buffer);
  151. if(bit_buffer_get_byte(rx_buffer, response_length - 2) != apdu_success[0] ||
  152. bit_buffer_get_byte(rx_buffer, response_length - 1) != apdu_success[1]) {
  153. FURI_LOG_I(
  154. TAG,
  155. "Read file failed: %02x%02x",
  156. bit_buffer_get_byte(rx_buffer, response_length - 2),
  157. bit_buffer_get_byte(rx_buffer, response_length - 1));
  158. stage = MetroflipPollerEventTypeFail;
  159. view_dispatcher_send_custom_event(
  160. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  161. break;
  162. }
  163. char environment_bit_representation[response_length * 8 + 1];
  164. environment_bit_representation[0] = '\0';
  165. for(size_t i = 0; i < response_length; i++) {
  166. char bits[9];
  167. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  168. byte_to_binary(byte, bits);
  169. for(int j = 0; j < 8; j++) {
  170. environment_bit_representation[i * 8 + j] = bits[j];
  171. }
  172. }
  173. start = 45;
  174. end = 58;
  175. decimal_value = bit_slice_to_dec(environment_bit_representation, start, end);
  176. uint64_t end_validity_timestamp =
  177. (decimal_value * 24 * 3600) + (float)epoch + 3600;
  178. DateTime end_dt = {0};
  179. datetime_timestamp_to_datetime(end_validity_timestamp, &end_dt);
  180. furi_string_cat_printf(parsed_data, "\nEnd Validity:\n");
  181. locale_format_datetime_cat(parsed_data, &end_dt, false);
  182. furi_string_cat_printf(parsed_data, "\n\n");
  183. // Select app for events
  184. select_app[6] = 16;
  185. bit_buffer_reset(tx_buffer);
  186. bit_buffer_append_bytes(tx_buffer, select_app, sizeof(select_app));
  187. error = iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  188. if(error != Iso14443_4bErrorNone) {
  189. FURI_LOG_I(TAG, "Select File: iso14443_4b_poller_send_block error %d", error);
  190. stage = MetroflipPollerEventTypeFail;
  191. view_dispatcher_send_custom_event(
  192. app->view_dispatcher, MetroflipCustomEventPollerFail);
  193. break;
  194. }
  195. // Check the response after selecting app
  196. response_length = bit_buffer_get_size_bytes(rx_buffer);
  197. if(bit_buffer_get_byte(rx_buffer, response_length - 2) != apdu_success[0] ||
  198. bit_buffer_get_byte(rx_buffer, response_length - 1) != apdu_success[1]) {
  199. FURI_LOG_I(
  200. TAG,
  201. "Select events app failed: %02x%02x",
  202. bit_buffer_get_byte(rx_buffer, response_length - 2),
  203. bit_buffer_get_byte(rx_buffer, response_length - 1));
  204. stage = MetroflipPollerEventTypeFail;
  205. view_dispatcher_send_custom_event(
  206. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  207. break;
  208. }
  209. furi_string_cat_printf(parsed_data, "\e#Events:\n");
  210. // Now send the read command
  211. for(size_t i = 1; i < 4; i++) {
  212. read_file[2] = i;
  213. bit_buffer_reset(tx_buffer);
  214. bit_buffer_append_bytes(tx_buffer, read_file, sizeof(read_file));
  215. error =
  216. iso14443_4b_poller_send_block(iso14443_4b_poller, tx_buffer, rx_buffer);
  217. if(error != Iso14443_4bErrorNone) {
  218. FURI_LOG_I(
  219. TAG, "Read File: iso14443_4b_poller_send_block error %d", error);
  220. stage = MetroflipPollerEventTypeFail;
  221. view_dispatcher_send_custom_event(
  222. app->view_dispatcher, MetroflipCustomEventPollerFail);
  223. break;
  224. }
  225. // Check the response after reading the file
  226. response_length = bit_buffer_get_size_bytes(rx_buffer);
  227. if(bit_buffer_get_byte(rx_buffer, response_length - 2) != apdu_success[0] ||
  228. bit_buffer_get_byte(rx_buffer, response_length - 1) != apdu_success[1]) {
  229. FURI_LOG_I(
  230. TAG,
  231. "Read file failed: %02x%02x",
  232. bit_buffer_get_byte(rx_buffer, response_length - 2),
  233. bit_buffer_get_byte(rx_buffer, response_length - 1));
  234. stage = MetroflipPollerEventTypeFail;
  235. view_dispatcher_send_custom_event(
  236. app->view_dispatcher, MetroflipCustomEventPollerFileNotFound);
  237. break;
  238. }
  239. char event_bit_representation[response_length * 8 + 1];
  240. event_bit_representation[0] = '\0';
  241. for(size_t i = 0; i < response_length; i++) {
  242. char bits[9];
  243. uint8_t byte = bit_buffer_get_byte(rx_buffer, i);
  244. byte_to_binary(byte, bits);
  245. for(int j = 0; j < 8; j++) {
  246. event_bit_representation[i * 8 + j] = bits[j];
  247. }
  248. }
  249. furi_string_cat_printf(parsed_data, "\nEvent 0%d:\n", i);
  250. int start = 53, end = 60;
  251. int decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  252. int transport_type = decimal_value >> 4;
  253. int transition = decimal_value & 15;
  254. furi_string_cat_printf(
  255. parsed_data,
  256. "%s - %s\n",
  257. TRANSPORT_LIST[transport_type],
  258. TRANSITION_LIST[transition]);
  259. start = 69, end = 84;
  260. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  261. int line_id = (decimal_value >> 9) - 1;
  262. int station_id = ((decimal_value >> 4) & 31) - 1;
  263. furi_string_cat_printf(
  264. parsed_data,
  265. "Line: %s\nStation: %s\n",
  266. METRO_LIST[line_id].name,
  267. METRO_LIST[line_id].stations[station_id]);
  268. start = 61, end = 68;
  269. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  270. furi_string_cat_printf(
  271. parsed_data, "Provider: %s\n", SERVICE_PROVIDERS[decimal_value]);
  272. start = 0, end = 13;
  273. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  274. uint64_t date_timestamp = (decimal_value * 24 * 3600) + epoch + 3600;
  275. DateTime dt = {0};
  276. datetime_timestamp_to_datetime(date_timestamp, &dt);
  277. furi_string_cat_printf(parsed_data, "Time: ");
  278. locale_format_datetime_cat(parsed_data, &dt, false);
  279. start = 14, end = 24;
  280. decimal_value = bit_slice_to_dec(event_bit_representation, start, end);
  281. furi_string_cat_printf(
  282. parsed_data,
  283. " %02d:%02d:%02d\n\n",
  284. ((decimal_value * 60) / 3600),
  285. (((decimal_value * 60) % 3600) / 60),
  286. (((decimal_value * 60) % 3600) % 60));
  287. }
  288. widget_add_text_scroll_element(
  289. widget, 0, 0, 128, 64, furi_string_get_cstr(parsed_data));
  290. widget_add_button_element(
  291. widget, GuiButtonTypeRight, "Exit", metroflip_navigo_widget_callback, app);
  292. furi_string_free(parsed_data);
  293. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewWidget);
  294. metroflip_app_blink_stop(app);
  295. stage = MetroflipPollerEventTypeSuccess;
  296. next_command = NfcCommandStop;
  297. } while(false);
  298. if(stage != MetroflipPollerEventTypeSuccess) {
  299. next_command = NfcCommandStop;
  300. }
  301. }
  302. }
  303. bit_buffer_free(tx_buffer);
  304. bit_buffer_free(rx_buffer);
  305. return next_command;
  306. }
  307. void metroflip_scene_navigo_on_enter(void* context) {
  308. Metroflip* app = context;
  309. dolphin_deed(DolphinDeedNfcRead);
  310. // Setup view
  311. Popup* popup = app->popup;
  312. popup_set_header(popup, "Apply\n card to\nthe back", 68, 30, AlignLeft, AlignTop);
  313. popup_set_icon(popup, 0, 3, &I_RFIDDolphinReceive_97x61);
  314. // Start worker
  315. view_dispatcher_switch_to_view(app->view_dispatcher, MetroflipViewPopup);
  316. nfc_scanner_alloc(app->nfc);
  317. app->poller = nfc_poller_alloc(app->nfc, NfcProtocolIso14443_4b);
  318. nfc_poller_start(app->poller, metroflip_scene_navigo_poller_callback, app);
  319. metroflip_app_blink_start(app);
  320. }
  321. bool metroflip_scene_navigo_on_event(void* context, SceneManagerEvent event) {
  322. Metroflip* app = context;
  323. bool consumed = false;
  324. if(event.type == SceneManagerEventTypeCustom) {
  325. if(event.event == MetroflipPollerEventTypeCardDetect) {
  326. Popup* popup = app->popup;
  327. popup_set_header(popup, "Scanning..", 68, 30, AlignLeft, AlignTop);
  328. consumed = true;
  329. } else if(event.event == MetroflipCustomEventPollerFileNotFound) {
  330. Popup* popup = app->popup;
  331. popup_set_header(popup, "Read Error,\n wrong card", 68, 30, AlignLeft, AlignTop);
  332. consumed = true;
  333. } else if(event.event == MetroflipCustomEventPollerFail) {
  334. Popup* popup = app->popup;
  335. popup_set_header(popup, "Error, try\n again", 68, 30, AlignLeft, AlignTop);
  336. consumed = true;
  337. }
  338. } else if(event.type == SceneManagerEventTypeBack) {
  339. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  340. consumed = true;
  341. }
  342. return consumed;
  343. }
  344. void metroflip_scene_navigo_on_exit(void* context) {
  345. Metroflip* app = context;
  346. if(app->poller) {
  347. nfc_poller_stop(app->poller);
  348. nfc_poller_free(app->poller);
  349. }
  350. metroflip_app_blink_stop(app);
  351. widget_reset(app->widget);
  352. // Clear view
  353. popup_reset(app->popup);
  354. }