nfc_scene_mf_classic_keys_add.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "../nfc_i.h"
  2. void nfc_scene_mf_classic_keys_add_byte_input_callback(void* context) {
  3. Nfc* nfc = context;
  4. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventByteInputDone);
  5. }
  6. void nfc_scene_mf_classic_keys_add_on_enter(void* context) {
  7. Nfc* nfc = context;
  8. // Setup view
  9. ByteInput* byte_input = nfc->byte_input;
  10. byte_input_set_header_text(byte_input, "Enter the key in hex");
  11. byte_input_set_result_callback(
  12. byte_input,
  13. nfc_scene_mf_classic_keys_add_byte_input_callback,
  14. NULL,
  15. nfc,
  16. nfc->byte_input_store,
  17. 6);
  18. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewByteInput);
  19. }
  20. bool nfc_scene_mf_classic_keys_add_on_event(void* context, SceneManagerEvent event) {
  21. Nfc* nfc = context;
  22. bool consumed = false;
  23. if(event.type == SceneManagerEventTypeCustom) {
  24. if(event.event == NfcCustomEventByteInputDone) {
  25. // Add key to dict
  26. bool key_added = false;
  27. MfClassicDict* dict = mf_classic_dict_alloc(MfClassicDictTypeUser);
  28. if(dict) {
  29. if(mf_classic_dict_add_key(dict, nfc->byte_input_store)) {
  30. key_added = true;
  31. }
  32. }
  33. if(key_added) {
  34. scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
  35. } else {
  36. scene_manager_next_scene(nfc->scene_manager, NfcSceneDictNotFound);
  37. }
  38. mf_classic_dict_free(dict);
  39. consumed = true;
  40. }
  41. }
  42. return consumed;
  43. }
  44. void nfc_scene_mf_classic_keys_add_on_exit(void* context) {
  45. Nfc* nfc = context;
  46. // Clear view
  47. byte_input_set_result_callback(nfc->byte_input, NULL, NULL, NULL, NULL, 0);
  48. byte_input_set_header_text(nfc->byte_input, "");
  49. }