virtual_portal.c 16 KB

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