virtual_portal.c 15 KB

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