virtual_portal.c 16 KB

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