virtual_portal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #include "virtual_portal.h"
  2. #define TAG "VirtualPortal"
  3. #define BLOCK_SIZE 16
  4. const NotificationSequence sequence_set_backlight = {
  5. &message_display_backlight_on,
  6. &message_do_not_reset,
  7. NULL,
  8. };
  9. const NotificationSequence sequence_set_leds = {
  10. &message_red_0,
  11. &message_blue_0,
  12. &message_green_0,
  13. &message_do_not_reset,
  14. NULL,
  15. };
  16. static float lerp(float start, float end, float t) {
  17. return start + (end - start) * t;
  18. }
  19. static int32_t pof_thread_worker(void* context) {
  20. VirtualPortal* virtual_portal = context;
  21. uint8_t last_r = 0;
  22. uint8_t last_g = 0;
  23. uint8_t last_b = 0;
  24. uint8_t target_r = 0;
  25. uint8_t target_g = 0;
  26. uint8_t target_b = 0;
  27. uint32_t start_time = 0;
  28. uint32_t elapsed = 0;
  29. uint32_t duration = 0;
  30. bool running = false;
  31. bool two_phase = false;
  32. int current_phase = 0;
  33. float t_phase;
  34. uint32_t flags;
  35. while (true) {
  36. if (running) {
  37. flags = furi_thread_flags_get();
  38. } else {
  39. flags = furi_thread_flags_wait(EventAll, FuriFlagWaitAny, FuriWaitForever);
  40. }
  41. if (flags) {
  42. if (flags & EventLed) {
  43. start_time = furi_get_tick();
  44. last_r = target_r;
  45. last_g = target_g;
  46. last_b = target_b;
  47. duration = virtual_portal->left.delay;
  48. target_r = virtual_portal->left.r;
  49. target_g = virtual_portal->left.g;
  50. target_b = virtual_portal->left.b;
  51. running = true;
  52. bool increasing = target_r > last_r || target_g > last_g || target_b > last_b;
  53. bool decreasing = target_r < last_r || target_g < last_g || target_b < last_b;
  54. two_phase = increasing && decreasing;
  55. current_phase = increasing ? 0 : 1;
  56. if (increasing && decreasing) {
  57. duration /= 2;
  58. }
  59. }
  60. if (flags & EventExit) {
  61. FURI_LOG_I(TAG, "exit");
  62. break;
  63. }
  64. }
  65. elapsed = furi_get_tick() - start_time;
  66. if (elapsed < duration) {
  67. t_phase = fminf((float)elapsed / (float)duration, 1);
  68. if (current_phase == 0) {
  69. if (last_r < target_r) {
  70. furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
  71. }
  72. if (last_g < target_g) {
  73. furi_hal_light_set(LightGreen, lerp(last_g, target_g, t_phase));
  74. }
  75. if (last_b < target_b) {
  76. furi_hal_light_set(LightBlue, lerp(last_b, target_b, t_phase));
  77. }
  78. } else {
  79. if (last_r > target_r) {
  80. furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
  81. }
  82. if (last_g > target_g) {
  83. furi_hal_light_set(LightGreen, lerp(last_g, target_g, t_phase));
  84. }
  85. if (last_b > target_b) {
  86. furi_hal_light_set(LightBlue, lerp(last_b, target_b, t_phase));
  87. }
  88. }
  89. } else {
  90. if (two_phase && current_phase == 0) {
  91. start_time = furi_get_tick();
  92. current_phase++;
  93. } else {
  94. last_r = target_r;
  95. last_g = target_g;
  96. last_b = target_b;
  97. furi_hal_light_set(LightRed, target_r);
  98. furi_hal_light_set(LightGreen, target_g);
  99. furi_hal_light_set(LightBlue, target_b);
  100. running = false;
  101. }
  102. }
  103. }
  104. return 0;
  105. }
  106. VirtualPortal* virtual_portal_alloc(NotificationApp* notifications) {
  107. VirtualPortal* virtual_portal = malloc(sizeof(VirtualPortal));
  108. virtual_portal->notifications = notifications;
  109. notification_message(virtual_portal->notifications, &sequence_set_backlight);
  110. notification_message(virtual_portal->notifications, &sequence_set_leds);
  111. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  112. virtual_portal->tokens[i] = pof_token_alloc();
  113. }
  114. virtual_portal->sequence_number = 0;
  115. virtual_portal->active = false;
  116. virtual_portal->thread = furi_thread_alloc();
  117. furi_thread_set_name(virtual_portal->thread, "PoFLed");
  118. furi_thread_set_stack_size(virtual_portal->thread, 2 * 1024);
  119. furi_thread_set_context(virtual_portal->thread, virtual_portal);
  120. furi_thread_set_callback(virtual_portal->thread, pof_thread_worker);
  121. furi_thread_start(virtual_portal->thread);
  122. return virtual_portal;
  123. }
  124. void virtual_portal_cleanup(VirtualPortal* virtual_portal) {
  125. notification_message(virtual_portal->notifications, &sequence_reset_rgb);
  126. notification_message(virtual_portal->notifications, &sequence_display_backlight_on);
  127. }
  128. void virtual_portal_free(VirtualPortal* virtual_portal) {
  129. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  130. pof_token_free(virtual_portal->tokens[i]);
  131. virtual_portal->tokens[i] = NULL;
  132. }
  133. furi_assert(virtual_portal->thread);
  134. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventExit);
  135. furi_thread_join(virtual_portal->thread);
  136. furi_thread_free(virtual_portal->thread);
  137. virtual_portal->thread = NULL;
  138. free(virtual_portal);
  139. }
  140. void virtaul_portal_set_leds(uint8_t r, uint8_t g, uint8_t b) {
  141. furi_hal_light_set(LightRed, r);
  142. furi_hal_light_set(LightGreen, g);
  143. furi_hal_light_set(LightBlue, b);
  144. }
  145. void virtaul_portal_set_backlight(uint8_t brightness) {
  146. furi_hal_light_set(LightBacklight, brightness);
  147. }
  148. void virtual_portal_load_token(VirtualPortal* virtual_portal, PoFToken* pof_token) {
  149. furi_assert(pof_token);
  150. FURI_LOG_D(TAG, "virtual_portal_load_token");
  151. PoFToken* target = NULL;
  152. uint8_t empty[4] = {0, 0, 0, 0};
  153. // first try to "reload" to the same slot it used before based on UID
  154. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  155. if (memcmp(virtual_portal->tokens[i]->UID, pof_token->UID, sizeof(pof_token->UID)) == 0) {
  156. // Found match
  157. if (virtual_portal->tokens[i]->loaded) {
  158. // already loaded, no-op
  159. return;
  160. } else {
  161. FURI_LOG_D(TAG, "Found matching UID at index %d", i);
  162. target = virtual_portal->tokens[i];
  163. break;
  164. }
  165. }
  166. }
  167. // otherwise load into first slot with no set UID
  168. if (target == NULL) {
  169. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  170. if (memcmp(virtual_portal->tokens[i]->UID, empty, sizeof(empty)) == 0) {
  171. FURI_LOG_D(TAG, "Found empty UID at index %d", i);
  172. // By definition an empty UID slot would not be loaded, so I'm not checking. Fight me.
  173. target = virtual_portal->tokens[i];
  174. break;
  175. }
  176. }
  177. }
  178. // Re-use first unloaded slot
  179. if (target == NULL) {
  180. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  181. if (virtual_portal->tokens[i]->loaded == false) {
  182. FURI_LOG_D(TAG, "Re-using previously used slot %d", i);
  183. target = virtual_portal->tokens[i];
  184. break;
  185. }
  186. }
  187. }
  188. if (target == NULL) {
  189. FURI_LOG_W(TAG, "Failed to find slot to token into");
  190. return;
  191. }
  192. furi_assert(target);
  193. // TODO: make pof_token_copy()
  194. target->change = pof_token->change;
  195. target->loaded = pof_token->loaded;
  196. memcpy(target->dev_name, pof_token->dev_name, sizeof(pof_token->dev_name));
  197. memcpy(target->UID, pof_token->UID, sizeof(pof_token->UID));
  198. const NfcDeviceData* data = nfc_device_get_data(pof_token->nfc_device, NfcProtocolMfClassic);
  199. nfc_device_set_data(target->nfc_device, NfcProtocolMfClassic, data);
  200. }
  201. uint8_t virtual_portal_next_sequence(VirtualPortal* virtual_portal) {
  202. if (virtual_portal->sequence_number == 0xff) {
  203. virtual_portal->sequence_number = 0;
  204. }
  205. return virtual_portal->sequence_number++;
  206. }
  207. int virtual_portal_activate(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  208. FURI_LOG_D(TAG, "process %c", message[0]);
  209. virtual_portal->active = message[1] != 0;
  210. response[0] = message[0];
  211. response[1] = message[1];
  212. response[2] = 0xFF;
  213. response[3] = 0x77;
  214. return 4;
  215. }
  216. int virtual_portal_reset(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  217. FURI_LOG_D(TAG, "process %c", message[0]);
  218. virtual_portal->active = false;
  219. // virtual_portal->sequence_number = 0;
  220. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  221. if (virtual_portal->tokens[i]->loaded) {
  222. virtual_portal->tokens[i]->change = true;
  223. }
  224. }
  225. uint8_t index = 0;
  226. response[index++] = 'R';
  227. response[index++] = 0x02;
  228. response[index++] = 0x1B;
  229. // response[index++] = 0x0a;
  230. // response[index++] = 0x03;
  231. // response[index++] = 0x02;
  232. // https://github.com/tresni/PoweredPortals/wiki/USB-Protocols
  233. // Wii Wireless: 01 29 00 00
  234. // Wii Wired: 02 0a 03 02 (Giants: works)
  235. // Arduboy: 02 19 (Trap team: works)
  236. return index;
  237. }
  238. int virtual_portal_status(VirtualPortal* virtual_portal, uint8_t* response) {
  239. response[0] = 'S';
  240. bool update = false;
  241. for (size_t i = 0; i < POF_TOKEN_LIMIT; i++) {
  242. // Can't use bit_lib since it uses the opposite endian
  243. if (virtual_portal->tokens[i]->loaded) {
  244. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 0);
  245. }
  246. if (virtual_portal->tokens[i]->change) {
  247. update = true;
  248. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 1);
  249. }
  250. virtual_portal->tokens[i]->change = false;
  251. }
  252. response[5] = virtual_portal_next_sequence(virtual_portal);
  253. response[6] = 1;
  254. // Let me know when a status that actually has a change is sent
  255. if (update) {
  256. char display[33] = {0};
  257. memset(display, 0, sizeof(display));
  258. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  259. snprintf(display + (i * 2), sizeof(display), "%02x", response[i]);
  260. }
  261. FURI_LOG_I(TAG, "> S %s", display);
  262. }
  263. return 7;
  264. }
  265. int virtual_portal_send_status(VirtualPortal* virtual_portal, uint8_t* response) {
  266. if (virtual_portal->active) {
  267. return virtual_portal_status(virtual_portal, response);
  268. }
  269. return 0;
  270. }
  271. // 4d01ff0000d0077d6c2a77a400000000
  272. int virtual_portal_m(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  273. virtual_portal->speaker = (message[1] == 1);
  274. /*
  275. char display[33] = {0};
  276. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  277. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  278. }
  279. FURI_LOG_I(TAG, "M %s", display);
  280. */
  281. size_t index = 0;
  282. response[index++] = 'M';
  283. response[index++] = message[1];
  284. response[index++] = 0x00;
  285. response[index++] = 0x19;
  286. return index;
  287. }
  288. int virtual_portal_l(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  289. UNUSED(virtual_portal);
  290. /*
  291. char display[33] = {0};
  292. memset(display, 0, sizeof(display));
  293. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  294. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  295. }
  296. FURI_LOG_I(TAG, "L %s", display);
  297. */
  298. uint8_t side = message[1]; // 0: left, 2: right
  299. uint8_t brightness = 0;
  300. switch (side) {
  301. case 0:
  302. case 2:
  303. virtual_portal->left.r = message[2];
  304. virtual_portal->left.g = message[3];
  305. virtual_portal->left.b = message[4];
  306. virtual_portal->left.delay = 0;
  307. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  308. break;
  309. case 1:
  310. brightness = message[2];
  311. virtaul_portal_set_backlight(brightness);
  312. break;
  313. case 3:
  314. brightness = 0xff;
  315. virtaul_portal_set_backlight(brightness);
  316. break;
  317. }
  318. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  319. size_t index = 0;
  320. response[index++] = 'J';
  321. return index;
  322. }
  323. int virtual_portal_j(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  324. /*
  325. char display[33] = {0};
  326. memset(display, 0, sizeof(display));
  327. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  328. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  329. }
  330. FURI_LOG_I(TAG, "J %s", display);
  331. */
  332. uint8_t side = message[1]; // 2: left, 1: right
  333. uint8_t r = message[2]; // 2: left, 1: right
  334. uint8_t g = message[3]; // 2: left, 1: right
  335. uint8_t b = message[4]; // 2: left, 1: right
  336. uint16_t delay = message[6] << 8 | message[5];
  337. switch (side) {
  338. case 0:
  339. virtual_portal->right.r = r;
  340. virtual_portal->right.g = g;
  341. virtual_portal->right.b = b;
  342. virtual_portal->right.delay = delay;
  343. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  344. break;
  345. case 1:
  346. virtual_portal->trap.r = r;
  347. virtual_portal->trap.g = g;
  348. virtual_portal->trap.b = b;
  349. virtual_portal->trap.delay = delay;
  350. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  351. break;
  352. case 2:
  353. virtual_portal->left.r = r;
  354. virtual_portal->left.g = g;
  355. virtual_portal->left.b = b;
  356. virtual_portal->left.delay = delay;
  357. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  358. break;
  359. }
  360. // Delay response
  361. // furi_delay_ms(delay); // causes issues
  362. // UNUSED(delay);
  363. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  364. size_t index = 0;
  365. response[index++] = 'J';
  366. return index;
  367. }
  368. int virtual_portal_query(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  369. int index = message[1];
  370. int blockNum = message[2];
  371. int arrayIndex = index & 0x0f;
  372. FURI_LOG_I(TAG, "Query %d %d", arrayIndex, blockNum);
  373. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  374. if (!pof_token->loaded) {
  375. response[0] = 'Q';
  376. response[1] = 0x00 | arrayIndex;
  377. response[2] = blockNum;
  378. return 3;
  379. }
  380. NfcDevice* nfc_device = pof_token->nfc_device;
  381. const MfClassicData* data = nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  382. const MfClassicBlock block = data->block[blockNum];
  383. response[0] = 'Q';
  384. response[1] = 0x10 | arrayIndex;
  385. response[2] = blockNum;
  386. memcpy(response + 3, block.data, BLOCK_SIZE);
  387. return 3 + BLOCK_SIZE;
  388. }
  389. int virtual_portal_write(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  390. int index = message[1];
  391. int blockNum = message[2];
  392. int arrayIndex = index & 0x0f;
  393. char display[33] = {0};
  394. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  395. snprintf(display + (i * 2), sizeof(display), "%02x", message[3 + i]);
  396. }
  397. FURI_LOG_I(TAG, "Write %d %d %s", arrayIndex, blockNum, display);
  398. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  399. if (!pof_token->loaded) {
  400. response[0] = 'W';
  401. response[1] = 0x00 | arrayIndex;
  402. response[2] = blockNum;
  403. return 3;
  404. }
  405. NfcDevice* nfc_device = pof_token->nfc_device;
  406. MfClassicData* data = mf_classic_alloc();
  407. nfc_device_copy_data(nfc_device, NfcProtocolMfClassic, data);
  408. MfClassicBlock* block = &data->block[blockNum];
  409. memcpy(block->data, message + 3, BLOCK_SIZE);
  410. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, data);
  411. mf_classic_free(data);
  412. response[0] = 'W';
  413. response[1] = 0x10 | arrayIndex;
  414. response[2] = blockNum;
  415. return 3;
  416. }
  417. // 32 byte message, 32 byte response;
  418. int virtual_portal_process_message(
  419. VirtualPortal* virtual_portal,
  420. uint8_t* message,
  421. uint8_t* response) {
  422. memset(response, 0, 32);
  423. switch (message[0]) {
  424. case 'A':
  425. return virtual_portal_activate(virtual_portal, message, response);
  426. case 'C': // Ring color R G B
  427. virtual_portal->left.r = message[1];
  428. virtual_portal->left.g = message[2];
  429. virtual_portal->left.b = message[3];
  430. virtual_portal->left.delay = 0;
  431. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  432. return 0;
  433. case 'J':
  434. // https://github.com/flyandi/flipper_zero_rgb_led
  435. return virtual_portal_j(virtual_portal, message, response);
  436. case 'L':
  437. return virtual_portal_l(virtual_portal, message, response);
  438. case 'M':
  439. return virtual_portal_m(virtual_portal, message, response);
  440. case 'Q': // Query
  441. return virtual_portal_query(virtual_portal, message, response);
  442. case 'R':
  443. return virtual_portal_reset(virtual_portal, message, response);
  444. case 'S': // Status
  445. return virtual_portal_status(virtual_portal, response);
  446. case 'V':
  447. return 0;
  448. case 'W': // Write
  449. return virtual_portal_write(virtual_portal, message, response);
  450. case 'Z':
  451. return 0;
  452. default:
  453. FURI_LOG_W(TAG, "Unhandled command %c", message[0]);
  454. return 0; // No response
  455. }
  456. return 0;
  457. }