virtual_portal.c 15 KB

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