virtual_portal.c 15 KB

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