esubghz_chat_key_read_popup.c 6.8 KB

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