virtual_portal.c 15 KB

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