esubghz_chat_key_read_popup.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "../esubghz_chat_i.h"
  2. #include "../helpers/nfc_helpers.h"
  3. typedef enum {
  4. KeyReadPopupState_Idle,
  5. KeyReadPopupState_Detecting,
  6. KeyReadPopupState_Reading,
  7. KeyReadPopupState_Fail,
  8. KeyReadPopupState_Success,
  9. } KeyReadPopupState;
  10. static bool read_worker_cb(NfcWorkerEvent event, void* context) {
  11. furi_assert(context);
  12. ESubGhzChatState* state = context;
  13. view_dispatcher_send_custom_event(state->view_dispatcher, event);
  14. return true;
  15. }
  16. static void key_read_popup_timeout_cb(void* context) {
  17. furi_assert(context);
  18. ESubGhzChatState* state = context;
  19. uint32_t cur_state =
  20. scene_manager_get_scene_state(state->scene_manager, ESubGhzChatScene_KeyReadPopup);
  21. /* done displaying our failure */
  22. if(cur_state == KeyReadPopupState_Fail) {
  23. view_dispatcher_send_custom_event(
  24. state->view_dispatcher, ESubGhzChatEvent_KeyReadPopupFailed);
  25. /* done displaying our success */
  26. } else if(cur_state == KeyReadPopupState_Success) {
  27. view_dispatcher_send_custom_event(
  28. state->view_dispatcher, ESubGhzChatEvent_KeyReadPopupSucceeded);
  29. }
  30. }
  31. struct ReplayDictNfcReaderContext {
  32. uint8_t* cur;
  33. uint8_t* max;
  34. };
  35. static bool replay_dict_nfc_reader(uint64_t* run_id, uint32_t* counter, void* context) {
  36. struct ReplayDictNfcReaderContext* ctx = (struct ReplayDictNfcReaderContext*)context;
  37. if(ctx->cur + sizeof(struct ReplayDictNfcEntry) > ctx->max) {
  38. return false;
  39. }
  40. struct ReplayDictNfcEntry* entry = (struct ReplayDictNfcEntry*)ctx->cur;
  41. *run_id = entry->run_id;
  42. *counter = __ntohl(entry->counter);
  43. ctx->cur += sizeof(struct ReplayDictNfcEntry);
  44. return true;
  45. }
  46. static bool key_read_popup_handle_key_read(ESubGhzChatState* state) {
  47. NfcDeviceData* dev_data = state->nfc_dev_data;
  48. /* check for config pages */
  49. if(dev_data->mf_ul_data.data_read < NFC_CONFIG_PAGES * 4) {
  50. return false;
  51. }
  52. size_t data_read = dev_data->mf_ul_data.data_read - (NFC_CONFIG_PAGES * 4);
  53. /* check if key was transmitted */
  54. if(data_read < KEY_BITS / 8) {
  55. return false;
  56. }
  57. /* initiate the crypto context */
  58. bool ret = crypto_ctx_set_key(
  59. state->crypto_ctx, dev_data->mf_ul_data.data, state->name_prefix, furi_get_tick());
  60. /* cleanup */
  61. crypto_explicit_bzero(dev_data->mf_ul_data.data, KEY_BITS / 8);
  62. if(!ret) {
  63. crypto_ctx_clear(state->crypto_ctx);
  64. return false;
  65. }
  66. /* read the frequency */
  67. if(data_read >= (KEY_BITS / 8) + sizeof(struct FreqNfcEntry)) {
  68. struct FreqNfcEntry* freq_entry =
  69. (struct FreqNfcEntry*)(dev_data->mf_ul_data.data + (KEY_BITS / 8));
  70. state->frequency = __ntohl(freq_entry->frequency);
  71. }
  72. /* read the replay dict */
  73. struct ReplayDictNfcReaderContext rd_ctx = {
  74. .cur = dev_data->mf_ul_data.data + (KEY_BITS / 8) + sizeof(struct FreqNfcEntry),
  75. .max =
  76. dev_data->mf_ul_data.data + (data_read < NFC_MAX_BYTES ? data_read : NFC_MAX_BYTES)};
  77. crypto_ctx_read_replay_dict(state->crypto_ctx, replay_dict_nfc_reader, &rd_ctx);
  78. /* set encrypted flag */
  79. state->encrypted = true;
  80. return true;
  81. }
  82. static void key_read_popup_set_state(ESubGhzChatState* state, KeyReadPopupState new_state) {
  83. uint32_t cur_state =
  84. scene_manager_get_scene_state(state->scene_manager, ESubGhzChatScene_KeyReadPopup);
  85. if(cur_state == new_state) {
  86. return;
  87. }
  88. if(new_state == KeyReadPopupState_Detecting) {
  89. popup_reset(state->nfc_popup);
  90. popup_disable_timeout(state->nfc_popup);
  91. popup_set_text(state->nfc_popup, "Tap Flipper\n to sender", 97, 24, AlignCenter, AlignTop);
  92. popup_set_icon(state->nfc_popup, 0, 8, &I_NFC_manual_60x50);
  93. notification_message(state->notification, &sequence_blink_start_cyan);
  94. } else if(new_state == KeyReadPopupState_Reading) {
  95. popup_reset(state->nfc_popup);
  96. popup_disable_timeout(state->nfc_popup);
  97. popup_set_header(
  98. state->nfc_popup,
  99. "Reading key\nDon't "
  100. "move...",
  101. 85,
  102. 24,
  103. AlignCenter,
  104. AlignTop);
  105. popup_set_icon(state->nfc_popup, 12, 23, &I_Loading_24);
  106. notification_message(state->notification, &sequence_blink_start_yellow);
  107. } else if(new_state == KeyReadPopupState_Fail) {
  108. nfc_worker_stop(state->nfc_worker);
  109. popup_reset(state->nfc_popup);
  110. popup_set_header(state->nfc_popup, "Failure!", 64, 2, AlignCenter, AlignTop);
  111. popup_set_text(state->nfc_popup, "Failed\nto read\nkey.", 78, 16, AlignLeft, AlignTop);
  112. popup_set_icon(state->nfc_popup, 21, 13, &I_Cry_dolph_55x52);
  113. popup_set_timeout(state->nfc_popup, KEY_READ_POPUP_MS);
  114. popup_set_context(state->nfc_popup, state);
  115. popup_set_callback(state->nfc_popup, key_read_popup_timeout_cb);
  116. popup_enable_timeout(state->nfc_popup);
  117. notification_message(state->notification, &sequence_blink_stop);
  118. } else if(new_state == KeyReadPopupState_Success) {
  119. nfc_worker_stop(state->nfc_worker);
  120. popup_reset(state->nfc_popup);
  121. popup_set_header(state->nfc_popup, "Key\nread!", 13, 22, AlignLeft, AlignBottom);
  122. popup_set_icon(state->nfc_popup, 32, 5, &I_DolphinNice_96x59);
  123. popup_set_timeout(state->nfc_popup, KEY_READ_POPUP_MS);
  124. popup_set_context(state->nfc_popup, state);
  125. popup_set_callback(state->nfc_popup, key_read_popup_timeout_cb);
  126. popup_enable_timeout(state->nfc_popup);
  127. notification_message(state->notification, &sequence_success);
  128. notification_message(state->notification, &sequence_blink_stop);
  129. }
  130. scene_manager_set_scene_state(state->scene_manager, ESubGhzChatScene_KeyReadPopup, new_state);
  131. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_NfcPopup);
  132. }
  133. /* Prepares the key share read scene. */
  134. void scene_on_enter_key_read_popup(void* context) {
  135. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_read_popup");
  136. furi_assert(context);
  137. ESubGhzChatState* state = context;
  138. key_read_popup_set_state(state, KeyReadPopupState_Detecting);
  139. state->nfc_dev_data->parsed_data = furi_string_alloc();
  140. if(state->nfc_dev_data->parsed_data == NULL) {
  141. /* can't do anything here, crash */
  142. furi_check(0);
  143. }
  144. nfc_worker_start(
  145. state->nfc_worker, NfcWorkerStateRead, state->nfc_dev_data, read_worker_cb, state);
  146. }
  147. /* Handles scene manager events for the key read popup scene. */
  148. bool scene_on_event_key_read_popup(void* context, SceneManagerEvent event) {
  149. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_read_popup");
  150. furi_assert(context);
  151. ESubGhzChatState* state = context;
  152. bool consumed = false;
  153. switch(event.type) {
  154. case SceneManagerEventTypeCustom:
  155. switch(event.event) {
  156. /* card detected */
  157. case NfcWorkerEventCardDetected:
  158. key_read_popup_set_state(state, KeyReadPopupState_Reading);
  159. consumed = true;
  160. break;
  161. /* no card detected */
  162. case NfcWorkerEventNoCardDetected:
  163. key_read_popup_set_state(state, KeyReadPopupState_Detecting);
  164. consumed = true;
  165. break;
  166. /* key probably read */
  167. case NfcWorkerEventReadMfUltralight:
  168. if(key_read_popup_handle_key_read(state)) {
  169. key_read_popup_set_state(state, KeyReadPopupState_Success);
  170. } else {
  171. key_read_popup_set_state(state, KeyReadPopupState_Fail);
  172. }
  173. consumed = true;
  174. break;
  175. /* close the popup and go back */
  176. case ESubGhzChatEvent_KeyReadPopupFailed:
  177. if(!scene_manager_previous_scene(state->scene_manager)) {
  178. view_dispatcher_stop(state->view_dispatcher);
  179. }
  180. consumed = true;
  181. break;
  182. /* success, go to frequency input */
  183. case ESubGhzChatEvent_KeyReadPopupSucceeded:
  184. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  185. consumed = true;
  186. break;
  187. /* something else happend, treat as failure */
  188. default:
  189. key_read_popup_set_state(state, KeyReadPopupState_Fail);
  190. consumed = true;
  191. break;
  192. }
  193. break;
  194. default:
  195. consumed = false;
  196. break;
  197. }
  198. return consumed;
  199. }
  200. /* Cleans up the key read popup scene. */
  201. void scene_on_exit_key_read_popup(void* context) {
  202. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_read_popup");
  203. furi_assert(context);
  204. ESubGhzChatState* state = context;
  205. popup_reset(state->nfc_popup);
  206. scene_manager_set_scene_state(
  207. state->scene_manager, ESubGhzChatScene_KeyReadPopup, KeyReadPopupState_Idle);
  208. notification_message(state->notification, &sequence_blink_stop);
  209. nfc_worker_stop(state->nfc_worker);
  210. crypto_explicit_bzero(state->nfc_dev_data->mf_ul_data.data, KEY_BITS / 8);
  211. if(state->nfc_dev_data->parsed_data != NULL) {
  212. furi_string_free(state->nfc_dev_data->parsed_data);
  213. }
  214. memset(state->nfc_dev_data, 0, sizeof(NfcDeviceData));
  215. }