virtual_portal.c 15 KB

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