virtual_portal.c 15 KB

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