nfc_maker_scene_result.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include "../nfc_maker.h"
  2. #include <furi_hal_random.h>
  3. enum PopupEvent {
  4. PopupEventExit,
  5. };
  6. static void nfc_maker_scene_result_popup_callback(void* context) {
  7. NfcMaker* app = context;
  8. view_dispatcher_send_custom_event(app->view_dispatcher, PopupEventExit);
  9. }
  10. void nfc_maker_scene_result_on_enter(void* context) {
  11. NfcMaker* app = context;
  12. Popup* popup = app->popup;
  13. bool success = false;
  14. FlipperFormat* file = flipper_format_file_alloc(furi_record_open(RECORD_STORAGE));
  15. FuriString* path = furi_string_alloc();
  16. furi_string_printf(path, NFC_MK_APP_FOLDER "/%s" NFC_MK_APP_EXTENSION, app->save_buf);
  17. uint32_t pages = 135;
  18. size_t size = pages * 4;
  19. uint8_t* buf = malloc(size);
  20. do {
  21. if(!flipper_format_file_open_new(file, furi_string_get_cstr(path))) break;
  22. if(!flipper_format_write_header_cstr(file, "Flipper NFC device", 3)) break;
  23. if(!flipper_format_write_string_cstr(file, "Device type", "NTAG215")) break;
  24. // Serial number
  25. size_t i = 0;
  26. buf[i++] = 0x04;
  27. furi_hal_random_fill_buf(&buf[i], 8);
  28. i += 8;
  29. uint8_t uid[7];
  30. memcpy(&uid[0], &buf[0], 3);
  31. memcpy(&uid[3], &buf[4], 4);
  32. if(!flipper_format_write_hex(file, "UID", uid, sizeof(uid))) break;
  33. if(!flipper_format_write_string_cstr(file, "ATQA", "00 44")) break;
  34. if(!flipper_format_write_string_cstr(file, "SAK", "00")) break;
  35. // TODO: Maybe randomize?
  36. if(!flipper_format_write_string_cstr(
  37. file,
  38. "Signature",
  39. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"))
  40. break;
  41. if(!flipper_format_write_string_cstr(file, "Mifare version", "00 04 04 02 01 00 11 03"))
  42. break;
  43. if(!flipper_format_write_string_cstr(file, "Counter 0", "0")) break;
  44. if(!flipper_format_write_string_cstr(file, "Tearing 0", "00")) break;
  45. if(!flipper_format_write_string_cstr(file, "Counter 1", "0")) break;
  46. if(!flipper_format_write_string_cstr(file, "Tearing 1", "00")) break;
  47. if(!flipper_format_write_string_cstr(file, "Counter 2", "0")) break;
  48. if(!flipper_format_write_string_cstr(file, "Tearing 2", "00")) break;
  49. if(!flipper_format_write_uint32(file, "Pages total", &pages, 1)) break;
  50. // Static data
  51. buf[i++] = 0x48; // Internal
  52. buf[i++] = 0x00; // Lock bytes
  53. buf[i++] = 0x00; // ...
  54. buf[i++] = 0xE1; // Capability container
  55. buf[i++] = 0x10; // ...
  56. buf[i++] = 0x3E; // ...
  57. buf[i++] = 0x00; // ...
  58. buf[i++] = 0x03; // Container flags
  59. // NDEF Docs: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/protocols/nfc/index.html#nfc-data-exchange-format-ndef
  60. uint8_t tnf = 0x00;
  61. const char* type = "";
  62. uint8_t* payload = NULL;
  63. size_t payload_len = 0;
  64. size_t data_len = 0;
  65. size_t j = 0;
  66. switch(scene_manager_get_scene_state(app->scene_manager, NfcMakerSceneStart)) {
  67. case NfcMakerSceneBluetooth: {
  68. tnf = 0x02; // Media-type [RFC 2046]
  69. type = "application/vnd.bluetooth.ep.oob";
  70. data_len = MAC_INPUT_LEN;
  71. payload_len = data_len + 2;
  72. payload = malloc(payload_len);
  73. payload[j++] = 0x08;
  74. payload[j++] = 0x00;
  75. memcpy(&payload[j], app->mac_buf, data_len);
  76. j += data_len;
  77. break;
  78. }
  79. case NfcMakerSceneContact: {
  80. tnf = 0x02; // Media-type [RFC 2046]
  81. type = "text/vcard";
  82. FuriString* vcard = furi_string_alloc_set("BEGIN:VCARD\r\nVERSION:3.0\r\n");
  83. furi_string_cat_printf(
  84. vcard, "PRODID:-//Flipper Xtreme//%s//EN\r\n", version_get_version(NULL));
  85. furi_string_cat_printf(vcard, "N:%s;%s;;;\r\n", app->small_buf2, app->small_buf1);
  86. furi_string_cat_printf(
  87. vcard,
  88. "FN:%s%s%s\r\n",
  89. app->small_buf1,
  90. newstrnlen(app->small_buf2, SMALL_INPUT_LEN) ? " " : "",
  91. app->small_buf2);
  92. if(newstrnlen(app->mail_buf, MAIL_INPUT_LEN)) {
  93. furi_string_cat_printf(vcard, "EMAIL:%s\r\n", app->mail_buf);
  94. }
  95. if(newstrnlen(app->phone_buf, PHONE_INPUT_LEN)) {
  96. furi_string_cat_printf(vcard, "TEL:%s\r\n", app->phone_buf);
  97. }
  98. if(newstrnlen(app->big_buf, BIG_INPUT_LEN)) {
  99. furi_string_cat_printf(vcard, "URL:%s\r\n", app->big_buf);
  100. }
  101. furi_string_cat_printf(vcard, "END:VCARD\r\n");
  102. payload_len = furi_string_size(vcard);
  103. payload = malloc(payload_len);
  104. memcpy(payload, furi_string_get_cstr(vcard), payload_len);
  105. furi_string_free(vcard);
  106. break;
  107. }
  108. case NfcMakerSceneHttps: {
  109. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  110. type = "\x55";
  111. data_len = newstrnlen(app->big_buf, BIG_INPUT_LEN);
  112. payload_len = data_len + 1;
  113. payload = malloc(payload_len);
  114. payload[j++] = 0x04; // Prepend "https://"
  115. memcpy(&payload[j], app->big_buf, data_len);
  116. j += data_len;
  117. break;
  118. }
  119. case NfcMakerSceneMail: {
  120. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  121. type = "\x55";
  122. data_len = newstrnlen(app->mail_buf, MAIL_INPUT_LEN);
  123. payload_len = data_len + 1;
  124. payload = malloc(payload_len);
  125. payload[j++] = 0x06; // Prepend "mailto:"
  126. memcpy(&payload[j], app->mail_buf, data_len);
  127. j += data_len;
  128. break;
  129. }
  130. case NfcMakerScenePhone: {
  131. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  132. type = "\x55";
  133. data_len = newstrnlen(app->phone_buf, PHONE_INPUT_LEN);
  134. payload_len = data_len + 1;
  135. payload = malloc(payload_len);
  136. payload[j++] = 0x05; // Prepend "tel:"
  137. memcpy(&payload[j], app->phone_buf, data_len);
  138. j += data_len;
  139. break;
  140. }
  141. case NfcMakerSceneText: {
  142. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  143. type = "\x54";
  144. data_len = newstrnlen(app->big_buf, BIG_INPUT_LEN);
  145. payload_len = data_len + 3;
  146. payload = malloc(payload_len);
  147. payload[j++] = 0x02;
  148. payload[j++] = 0x65; // e
  149. payload[j++] = 0x6E; // n
  150. memcpy(&payload[j], app->big_buf, data_len);
  151. j += data_len;
  152. break;
  153. }
  154. case NfcMakerSceneUrl: {
  155. tnf = 0x01; // NFC Forum well-known type [NFC RTD]
  156. type = "\x55";
  157. data_len = newstrnlen(app->big_buf, BIG_INPUT_LEN);
  158. payload_len = data_len + 1;
  159. payload = malloc(payload_len);
  160. payload[j++] = 0x00; // No prepend
  161. memcpy(&payload[j], app->big_buf, data_len);
  162. j += data_len;
  163. break;
  164. }
  165. case NfcMakerSceneWifi: {
  166. tnf = 0x02; // Media-type [RFC 2046]
  167. type = "application/vnd.wfa.wsc";
  168. uint8_t ssid_len = newstrnlen(app->small_buf1, SMALL_INPUT_LEN);
  169. uint8_t pass_len = newstrnlen(app->small_buf2, SMALL_INPUT_LEN);
  170. uint8_t data_len = ssid_len + pass_len;
  171. payload_len = data_len + 39;
  172. payload = malloc(payload_len);
  173. payload[j++] = 0x10;
  174. payload[j++] = 0x0E;
  175. payload[j++] = 0x00;
  176. payload[j++] = data_len + 43;
  177. payload[j++] = 0x10;
  178. payload[j++] = 0x26;
  179. payload[j++] = 0x00;
  180. payload[j++] = 0x01;
  181. payload[j++] = 0x01;
  182. payload[j++] = 0x10;
  183. payload[j++] = 0x45;
  184. payload[j++] = 0x00;
  185. payload[j++] = ssid_len;
  186. memcpy(&payload[j], app->small_buf1, ssid_len);
  187. j += ssid_len;
  188. payload[j++] = 0x10;
  189. payload[j++] = 0x03;
  190. payload[j++] = 0x00;
  191. payload[j++] = 0x02;
  192. payload[j++] = 0x00;
  193. payload[j++] =
  194. scene_manager_get_scene_state(app->scene_manager, NfcMakerSceneWifiAuth);
  195. payload[j++] = 0x10;
  196. payload[j++] = 0x0F;
  197. payload[j++] = 0x00;
  198. payload[j++] = 0x02;
  199. payload[j++] = 0x00;
  200. payload[j++] =
  201. scene_manager_get_scene_state(app->scene_manager, NfcMakerSceneWifiEncr);
  202. payload[j++] = 0x10;
  203. payload[j++] = 0x27;
  204. payload[j++] = 0x00;
  205. payload[j++] = pass_len;
  206. memcpy(&payload[j], app->small_buf2, pass_len);
  207. j += pass_len;
  208. payload[j++] = 0x10;
  209. payload[j++] = 0x20;
  210. payload[j++] = 0x00;
  211. payload[j++] = 0x06;
  212. payload[j++] = 0xFF;
  213. payload[j++] = 0xFF;
  214. payload[j++] = 0xFF;
  215. payload[j++] = 0xFF;
  216. payload[j++] = 0xFF;
  217. payload[j++] = 0xFF;
  218. break;
  219. }
  220. default:
  221. break;
  222. }
  223. // Record header
  224. uint8_t flags = 0;
  225. flags |= 1 << 7; // MB (Message Begin)
  226. flags |= 1 << 6; // ME (Message End)
  227. flags |= tnf; // TNF (Type Name Format)
  228. size_t type_len = strlen(type);
  229. size_t header_len = 0;
  230. header_len += 1; // Flags and TNF
  231. header_len += 1; // Type length
  232. if(payload_len < 0xFF) {
  233. flags |= 1 << 4; // SR (Short Record)
  234. header_len += 1; // Payload length
  235. } else {
  236. header_len += 4; // Payload length
  237. }
  238. header_len += type_len; // Payload type
  239. size_t record_len = header_len + payload_len;
  240. if(record_len < 0xFF) {
  241. buf[i++] = record_len; // Record length
  242. } else {
  243. buf[i++] = 0xFF; // Record length
  244. buf[i++] = record_len >> 8; // ...
  245. buf[i++] = record_len & 0xFF; // ...
  246. }
  247. buf[i++] = flags; // Flags and TNF
  248. buf[i++] = type_len; // Type length
  249. if(flags & 1 << 4) { // SR (Short Record)
  250. buf[i++] = payload_len; // Payload length
  251. } else {
  252. buf[i++] = 0x00; // Payload length
  253. buf[i++] = 0x00; // ...
  254. buf[i++] = payload_len >> 8; // ...
  255. buf[i++] = payload_len & 0xFF; // ...
  256. }
  257. memcpy(&buf[i], type, type_len); // Payload type
  258. i += type_len;
  259. // Record payload
  260. memcpy(&buf[i], payload, payload_len);
  261. i += payload_len;
  262. free(payload);
  263. // Record terminator
  264. buf[i++] = 0xFE;
  265. // Padding until last 5 pages
  266. for(; i < size - 20; i++) {
  267. buf[i] = 0x00;
  268. }
  269. // Last 5 static pages
  270. buf[i++] = 0x00;
  271. buf[i++] = 0x00;
  272. buf[i++] = 0x00;
  273. buf[i++] = 0xBD;
  274. buf[i++] = 0x04;
  275. buf[i++] = 0x00;
  276. buf[i++] = 0x00;
  277. buf[i++] = 0xFF;
  278. buf[i++] = 0x00;
  279. buf[i++] = 0x05;
  280. buf[i++] = 0x00;
  281. buf[i++] = 0x00;
  282. buf[i++] = 0xFF;
  283. buf[i++] = 0xFF;
  284. buf[i++] = 0xFF;
  285. buf[i++] = 0xFF;
  286. buf[i++] = 0x00;
  287. buf[i++] = 0x00;
  288. buf[i++] = 0x00;
  289. buf[i++] = 0x00;
  290. // Write pages
  291. char str[16];
  292. bool ok = true;
  293. for(size_t page = 0; page < pages; page++) {
  294. snprintf(str, sizeof(str), "Page %u", page);
  295. if(!flipper_format_write_hex(file, str, &buf[page * 4], 4)) {
  296. ok = false;
  297. break;
  298. }
  299. }
  300. if(!ok) break;
  301. success = true;
  302. } while(false);
  303. free(buf);
  304. furi_string_free(path);
  305. flipper_format_free(file);
  306. furi_record_close(RECORD_STORAGE);
  307. if(success) {
  308. popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
  309. popup_set_header(popup, "Saved!", 13, 22, AlignLeft, AlignBottom);
  310. } else {
  311. popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
  312. popup_set_header(popup, "Saved!", 13, 22, AlignLeft, AlignBottom);
  313. }
  314. popup_set_timeout(popup, 1500);
  315. popup_set_context(popup, app);
  316. popup_set_callback(popup, nfc_maker_scene_result_popup_callback);
  317. popup_enable_timeout(popup);
  318. view_dispatcher_switch_to_view(app->view_dispatcher, NfcMakerViewPopup);
  319. }
  320. bool nfc_maker_scene_result_on_event(void* context, SceneManagerEvent event) {
  321. NfcMaker* app = context;
  322. bool consumed = false;
  323. if(event.type == SceneManagerEventTypeCustom) {
  324. consumed = true;
  325. switch(event.event) {
  326. case PopupEventExit:
  327. scene_manager_search_and_switch_to_previous_scene(
  328. app->scene_manager, NfcMakerSceneStart);
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. return consumed;
  335. }
  336. void nfc_maker_scene_result_on_exit(void* context) {
  337. NfcMaker* app = context;
  338. popup_reset(app->popup);
  339. }