esubghz_chat_key_read_popup.c 6.8 KB

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