nfc_maker_scene_result.c 13 KB

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