esubghz_chat_key_read_popup.c 8.2 KB

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