esubghz_chat_key_read_popup.c 7.9 KB

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