nfc_scene_device_info.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "../nfc_i.h"
  2. #include "../helpers/nfc_emv_parser.h"
  3. enum {
  4. NfcSceneDeviceInfoUid,
  5. NfcSceneDeviceInfoData,
  6. };
  7. void nfc_scene_device_info_widget_callback(GuiButtonType result, InputType type, void* context) {
  8. Nfc* nfc = context;
  9. if(type == InputTypeShort) {
  10. view_dispatcher_send_custom_event(nfc->view_dispatcher, result);
  11. }
  12. }
  13. void nfc_scene_device_info_dialog_callback(DialogExResult result, void* context) {
  14. Nfc* nfc = context;
  15. if(result == DialogExResultLeft) {
  16. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
  17. }
  18. }
  19. void nfc_scene_device_info_bank_card_callback(GuiButtonType result, InputType type, void* context) {
  20. Nfc* nfc = context;
  21. if(type == InputTypeShort) {
  22. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
  23. }
  24. }
  25. void nfc_scene_device_info_on_enter(void* context) {
  26. Nfc* nfc = context;
  27. bool data_display_supported = (nfc->dev->format == NfcDeviceSaveFormatUid) ||
  28. (nfc->dev->format == NfcDeviceSaveFormatMifareUl) ||
  29. (nfc->dev->format == NfcDeviceSaveFormatMifareDesfire) ||
  30. (nfc->dev->format == NfcDeviceSaveFormatBankCard);
  31. // Setup Custom Widget view
  32. widget_add_text_box_element(
  33. nfc->widget, 0, 0, 128, 22, AlignCenter, AlignTop, nfc->dev->dev_name);
  34. widget_add_button_element(
  35. nfc->widget, GuiButtonTypeLeft, "Back", nfc_scene_device_info_widget_callback, nfc);
  36. if(data_display_supported) {
  37. widget_add_button_element(
  38. nfc->widget, GuiButtonTypeRight, "Data", nfc_scene_device_info_widget_callback, nfc);
  39. }
  40. char uid_str[32];
  41. NfcDeviceCommonData* data = &nfc->dev->dev_data.nfc_data;
  42. if(data->uid_len == 4) {
  43. snprintf(
  44. uid_str,
  45. sizeof(uid_str),
  46. "UID: %02X %02X %02X %02X",
  47. data->uid[0],
  48. data->uid[1],
  49. data->uid[2],
  50. data->uid[3]);
  51. } else if(data->uid_len == 7) {
  52. snprintf(
  53. uid_str,
  54. sizeof(uid_str),
  55. "UID: %02X %02X %02X %02X %02X %02X %02X",
  56. data->uid[0],
  57. data->uid[1],
  58. data->uid[2],
  59. data->uid[3],
  60. data->uid[4],
  61. data->uid[5],
  62. data->uid[6]);
  63. }
  64. widget_add_string_element(nfc->widget, 64, 21, AlignCenter, AlignTop, FontSecondary, uid_str);
  65. const char* protocol_name = NULL;
  66. if(data->protocol == NfcDeviceProtocolEMV ||
  67. data->protocol == NfcDeviceProtocolMifareDesfire) {
  68. protocol_name = nfc_guess_protocol(data->protocol);
  69. } else if(data->protocol == NfcDeviceProtocolMifareUl) {
  70. protocol_name = nfc_mf_ul_type(nfc->dev->dev_data.mf_ul_data.type, false);
  71. } else if(data->protocol == NfcDeviceProtocolMifareClassic) {
  72. protocol_name = nfc_mf_classic_type(nfc->dev->dev_data.mf_classic_data.type);
  73. }
  74. if(protocol_name) {
  75. widget_add_string_element(
  76. nfc->widget, 10, 32, AlignLeft, AlignTop, FontSecondary, protocol_name);
  77. }
  78. // TODO change dinamically
  79. widget_add_string_element(nfc->widget, 118, 32, AlignRight, AlignTop, FontSecondary, "NFC-A");
  80. char sak_str[16];
  81. snprintf(sak_str, sizeof(sak_str), "SAK: %02X", data->sak);
  82. widget_add_string_element(nfc->widget, 10, 42, AlignLeft, AlignTop, FontSecondary, sak_str);
  83. char atqa_str[16];
  84. snprintf(atqa_str, sizeof(atqa_str), "ATQA: %02X%02X", data->atqa[0], data->atqa[1]);
  85. widget_add_string_element(nfc->widget, 118, 42, AlignRight, AlignTop, FontSecondary, atqa_str);
  86. // Setup Data View
  87. if(nfc->dev->format == NfcDeviceSaveFormatUid) {
  88. DialogEx* dialog_ex = nfc->dialog_ex;
  89. dialog_ex_set_left_button_text(dialog_ex, "Back");
  90. dialog_ex_set_text(dialog_ex, "No data", 64, 32, AlignCenter, AlignCenter);
  91. dialog_ex_set_context(dialog_ex, nfc);
  92. dialog_ex_set_result_callback(dialog_ex, nfc_scene_device_info_dialog_callback);
  93. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
  94. MifareUlData* mf_ul_data = &nfc->dev->dev_data.mf_ul_data;
  95. TextBox* text_box = nfc->text_box;
  96. text_box_set_font(text_box, TextBoxFontHex);
  97. for(uint16_t i = 0; i < mf_ul_data->data_size; i += 2) {
  98. if(!(i % 8) && i) {
  99. string_push_back(nfc->text_box_store, '\n');
  100. }
  101. string_cat_printf(
  102. nfc->text_box_store, "%02X%02X ", mf_ul_data->data[i], mf_ul_data->data[i + 1]);
  103. }
  104. text_box_set_text(text_box, string_get_cstr(nfc->text_box_store));
  105. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareDesfire) {
  106. MifareDesfireData* mf_df_data = &nfc->dev->dev_data.mf_df_data;
  107. uint16_t n_apps = 0;
  108. uint16_t n_files = 0;
  109. for(MifareDesfireApplication* app = mf_df_data->app_head; app; app = app->next) {
  110. n_apps++;
  111. for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
  112. n_files++;
  113. }
  114. }
  115. nfc_text_store_set(
  116. nfc,
  117. "%d application%s, %d file%s",
  118. n_apps,
  119. n_apps == 1 ? "" : "s",
  120. n_files,
  121. n_files == 1 ? "" : "s");
  122. widget_add_string_element(
  123. nfc->widget, 64, 17, AlignCenter, AlignBottom, FontSecondary, nfc->text_store);
  124. } else if(nfc->dev->format == NfcDeviceSaveFormatBankCard) {
  125. NfcEmvData* emv_data = &nfc->dev->dev_data.emv_data;
  126. BankCard* bank_card = nfc->bank_card;
  127. bank_card_set_name(bank_card, emv_data->name);
  128. bank_card_set_number(bank_card, emv_data->number, emv_data->number_len);
  129. bank_card_set_back_callback(bank_card, nfc_scene_device_info_bank_card_callback, nfc);
  130. if(emv_data->exp_mon) {
  131. bank_card_set_exp_date(bank_card, emv_data->exp_mon, emv_data->exp_year);
  132. }
  133. string_t display_str;
  134. string_init(display_str);
  135. if(emv_data->country_code) {
  136. string_t country_name;
  137. string_init(country_name);
  138. if(nfc_emv_parser_get_country_name(
  139. nfc->dev->storage, emv_data->country_code, country_name)) {
  140. string_printf(display_str, "Reg:%s", string_get_cstr(country_name));
  141. bank_card_set_country_name(bank_card, string_get_cstr(display_str));
  142. }
  143. string_clear(country_name);
  144. }
  145. if(emv_data->currency_code) {
  146. string_t currency_name;
  147. string_init(currency_name);
  148. if(nfc_emv_parser_get_currency_name(
  149. nfc->dev->storage, emv_data->country_code, currency_name)) {
  150. string_printf(display_str, "Cur:%s", string_get_cstr(currency_name));
  151. bank_card_set_currency_name(bank_card, string_get_cstr(display_str));
  152. }
  153. string_clear(currency_name);
  154. }
  155. string_clear(display_str);
  156. }
  157. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneDeviceInfo, NfcSceneDeviceInfoUid);
  158. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget);
  159. }
  160. bool nfc_scene_device_info_on_event(void* context, SceneManagerEvent event) {
  161. Nfc* nfc = context;
  162. bool consumed = false;
  163. uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneDeviceInfo);
  164. if(event.type == SceneManagerEventTypeCustom) {
  165. if((state == NfcSceneDeviceInfoUid) && (event.event == GuiButtonTypeLeft)) {
  166. consumed = scene_manager_previous_scene(nfc->scene_manager);
  167. } else if((state == NfcSceneDeviceInfoUid) && (event.event == GuiButtonTypeRight)) {
  168. if(nfc->dev->format == NfcDeviceSaveFormatUid) {
  169. scene_manager_set_scene_state(
  170. nfc->scene_manager, NfcSceneDeviceInfo, NfcSceneDeviceInfoData);
  171. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDialogEx);
  172. consumed = true;
  173. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
  174. scene_manager_set_scene_state(
  175. nfc->scene_manager, NfcSceneDeviceInfo, NfcSceneDeviceInfoData);
  176. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
  177. consumed = true;
  178. } else if(nfc->dev->format == NfcDeviceSaveFormatBankCard) {
  179. scene_manager_set_scene_state(
  180. nfc->scene_manager, NfcSceneDeviceInfo, NfcSceneDeviceInfoData);
  181. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewBankCard);
  182. consumed = true;
  183. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareDesfire) {
  184. scene_manager_next_scene(nfc->scene_manager, NfcSceneMifareDesfireData);
  185. consumed = true;
  186. }
  187. } else if(state == NfcSceneDeviceInfoData && event.event == NfcCustomEventViewExit) {
  188. scene_manager_set_scene_state(
  189. nfc->scene_manager, NfcSceneDeviceInfo, NfcSceneDeviceInfoUid);
  190. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget);
  191. consumed = true;
  192. }
  193. } else if(event.type == SceneManagerEventTypeBack) {
  194. if(state == NfcSceneDeviceInfoData) {
  195. scene_manager_set_scene_state(
  196. nfc->scene_manager, NfcSceneDeviceInfo, NfcSceneDeviceInfoUid);
  197. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget);
  198. consumed = true;
  199. }
  200. }
  201. return consumed;
  202. }
  203. void nfc_scene_device_info_on_exit(void* context) {
  204. Nfc* nfc = (Nfc*)context;
  205. // Clear Custom Widget
  206. widget_reset(nfc->widget);
  207. if(nfc->dev->format == NfcDeviceSaveFormatUid) {
  208. // Clear Dialog
  209. DialogEx* dialog_ex = nfc->dialog_ex;
  210. dialog_ex_reset(dialog_ex);
  211. } else if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
  212. // Clear TextBox
  213. text_box_reset(nfc->text_box);
  214. string_reset(nfc->text_box_store);
  215. } else if(nfc->dev->format == NfcDeviceSaveFormatBankCard) {
  216. // Clear Bank Card
  217. bank_card_clear(nfc->bank_card);
  218. }
  219. }