virtual_portal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. static int32_t pof_thread_worker(void* context) {
  20. VirtualPortal* virtual_portal = context;
  21. uint8_t last_r = 0;
  22. uint8_t last_g = 0;
  23. uint8_t last_b = 0;
  24. uint8_t target_r = 0;
  25. uint8_t target_g = 0;
  26. uint8_t target_b = 0;
  27. uint32_t start_time = 0;
  28. uint32_t elapsed = 0;
  29. uint32_t duration = 0;
  30. bool running = false;
  31. bool two_phase = false;
  32. int current_phase = 0;
  33. float t_phase;
  34. uint32_t flags;
  35. while (true) {
  36. if (running) {
  37. flags = furi_thread_flags_get();
  38. } else {
  39. flags = furi_thread_flags_wait(EventAll, FuriFlagWaitAny, FuriWaitForever);
  40. }
  41. if (flags) {
  42. if (flags & EventLed) {
  43. start_time = furi_get_tick();
  44. last_r = target_r;
  45. last_g = target_g;
  46. last_b = target_b;
  47. duration = virtual_portal->left.delay;
  48. target_r = virtual_portal->left.r;
  49. target_g = virtual_portal->left.g;
  50. target_b = virtual_portal->left.b;
  51. running = true;
  52. bool increasing = target_r > last_r || target_g > last_g || target_b > last_b;
  53. bool decreasing = target_r < last_r || target_g < last_g || target_b < last_b;
  54. two_phase = increasing && decreasing;
  55. current_phase = increasing ? 0 : 1;
  56. if (increasing && decreasing) {
  57. duration /= 2;
  58. }
  59. }
  60. if (flags & EventExit) {
  61. FURI_LOG_I(TAG, "exit");
  62. break;
  63. }
  64. }
  65. elapsed = furi_get_tick() - start_time;
  66. if (elapsed < duration) {
  67. t_phase = fminf((float)elapsed / (float)duration, 1);
  68. if (current_phase == 0) {
  69. if (last_r < target_r) {
  70. furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
  71. }
  72. if (last_g < target_g) {
  73. furi_hal_light_set(LightGreen, lerp(last_g, target_g, t_phase));
  74. }
  75. if (last_b < target_b) {
  76. furi_hal_light_set(LightBlue, lerp(last_b, target_b, t_phase));
  77. }
  78. } else {
  79. if (last_r > target_r) {
  80. furi_hal_light_set(LightRed, lerp(last_r, target_r, t_phase));
  81. }
  82. if (last_g > target_g) {
  83. furi_hal_light_set(LightGreen, lerp(last_g, target_g, t_phase));
  84. }
  85. if (last_b > target_b) {
  86. furi_hal_light_set(LightBlue, lerp(last_b, target_b, t_phase));
  87. }
  88. }
  89. } else if (two_phase && current_phase == 0) {
  90. start_time = furi_get_tick();
  91. current_phase++;
  92. } else {
  93. last_r = target_r;
  94. last_g = target_g;
  95. last_b = target_b;
  96. furi_hal_light_set(LightRed, target_r);
  97. furi_hal_light_set(LightGreen, target_g);
  98. furi_hal_light_set(LightBlue, target_b);
  99. running = false;
  100. }
  101. }
  102. return 0;
  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->thread = furi_thread_alloc();
  115. furi_thread_set_name(virtual_portal->thread, "PoFLed");
  116. furi_thread_set_stack_size(virtual_portal->thread, 2 * 1024);
  117. furi_thread_set_context(virtual_portal->thread, virtual_portal);
  118. furi_thread_set_callback(virtual_portal->thread, pof_thread_worker);
  119. furi_thread_start(virtual_portal->thread);
  120. return virtual_portal;
  121. }
  122. void virtual_portal_cleanup(VirtualPortal* virtual_portal) {
  123. notification_message(virtual_portal->notifications, &sequence_reset_rgb);
  124. notification_message(virtual_portal->notifications, &sequence_display_backlight_on);
  125. }
  126. void virtual_portal_free(VirtualPortal* virtual_portal) {
  127. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  128. pof_token_free(virtual_portal->tokens[i]);
  129. virtual_portal->tokens[i] = NULL;
  130. }
  131. furi_assert(virtual_portal->thread);
  132. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventExit);
  133. furi_thread_join(virtual_portal->thread);
  134. furi_thread_free(virtual_portal->thread);
  135. virtual_portal->thread = NULL;
  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, uint8_t* response) {
  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. virtual_portal->left.r = message[2];
  302. virtual_portal->left.g = message[3];
  303. virtual_portal->left.b = message[4];
  304. virtual_portal->left.delay = 0;
  305. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  306. break;
  307. case 1:
  308. brightness = message[2];
  309. virtaul_portal_set_backlight(brightness);
  310. break;
  311. case 3:
  312. brightness = 0xff;
  313. virtaul_portal_set_backlight(brightness);
  314. break;
  315. }
  316. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  317. size_t index = 0;
  318. response[index++] = 'J';
  319. return index;
  320. }
  321. int virtual_portal_j(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  322. /*
  323. char display[33] = {0};
  324. memset(display, 0, sizeof(display));
  325. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  326. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  327. }
  328. FURI_LOG_I(TAG, "J %s", display);
  329. */
  330. uint8_t side = message[1]; // 2: left, 1: right
  331. uint8_t r = message[2]; // 2: left, 1: right
  332. uint8_t g = message[3]; // 2: left, 1: right
  333. uint8_t b = message[4]; // 2: left, 1: right
  334. uint16_t delay = message[6] << 8 | message[5];
  335. switch (side) {
  336. case 0:
  337. virtual_portal->right.r = r;
  338. virtual_portal->right.g = g;
  339. virtual_portal->right.b = b;
  340. virtual_portal->right.delay = delay;
  341. // furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  342. break;
  343. case 1:
  344. virtual_portal->trap.r = r;
  345. virtual_portal->trap.g = g;
  346. virtual_portal->trap.b = b;
  347. virtual_portal->trap.delay = delay;
  348. // furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  349. break;
  350. case 2:
  351. virtual_portal->left.r = r;
  352. virtual_portal->left.g = g;
  353. virtual_portal->left.b = b;
  354. virtual_portal->left.delay = delay;
  355. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  356. break;
  357. }
  358. // Delay response
  359. // furi_delay_ms(delay); // causes issues
  360. // UNUSED(delay);
  361. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  362. size_t index = 0;
  363. response[index++] = 'J';
  364. return index;
  365. }
  366. int virtual_portal_query(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  367. int index = message[1];
  368. int blockNum = message[2];
  369. int arrayIndex = index & 0x0f;
  370. FURI_LOG_I(TAG, "Query %d %d", arrayIndex, blockNum);
  371. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  372. if (!pof_token->loaded) {
  373. response[0] = 'Q';
  374. response[1] = 0x00 | arrayIndex;
  375. response[2] = blockNum;
  376. return 3;
  377. }
  378. NfcDevice* nfc_device = pof_token->nfc_device;
  379. const MfClassicData* data = nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  380. const MfClassicBlock block = data->block[blockNum];
  381. response[0] = 'Q';
  382. response[1] = 0x10 | arrayIndex;
  383. response[2] = blockNum;
  384. memcpy(response + 3, block.data, BLOCK_SIZE);
  385. return 3 + BLOCK_SIZE;
  386. }
  387. int virtual_portal_write(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  388. int index = message[1];
  389. int blockNum = message[2];
  390. int arrayIndex = index & 0x0f;
  391. char display[33] = {0};
  392. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  393. snprintf(display + (i * 2), sizeof(display), "%02x", message[3 + i]);
  394. }
  395. FURI_LOG_I(TAG, "Write %d %d %s", arrayIndex, blockNum, display);
  396. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  397. if (!pof_token->loaded) {
  398. response[0] = 'W';
  399. response[1] = 0x00 | arrayIndex;
  400. response[2] = blockNum;
  401. return 3;
  402. }
  403. NfcDevice* nfc_device = pof_token->nfc_device;
  404. MfClassicData* data = mf_classic_alloc();
  405. nfc_device_copy_data(nfc_device, NfcProtocolMfClassic, data);
  406. MfClassicBlock* block = &data->block[blockNum];
  407. memcpy(block->data, message + 3, BLOCK_SIZE);
  408. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, data);
  409. mf_classic_free(data);
  410. response[0] = 'W';
  411. response[1] = 0x10 | arrayIndex;
  412. response[2] = blockNum;
  413. return 3;
  414. }
  415. // 32 byte message, 32 byte response;
  416. int virtual_portal_process_message(
  417. VirtualPortal* virtual_portal,
  418. uint8_t* message,
  419. uint8_t* response) {
  420. memset(response, 0, 32);
  421. switch (message[0]) {
  422. case 'A':
  423. return virtual_portal_activate(virtual_portal, message, response);
  424. case 'C': // Ring color R G B
  425. virtual_portal->left.r = message[1];
  426. virtual_portal->left.g = message[2];
  427. virtual_portal->left.b = message[3];
  428. virtual_portal->left.delay = 0;
  429. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventLed);
  430. return 0;
  431. case 'J':
  432. // https://github.com/flyandi/flipper_zero_rgb_led
  433. return virtual_portal_j(virtual_portal, message, response);
  434. case 'L':
  435. return virtual_portal_l(virtual_portal, message, response);
  436. case 'M':
  437. return virtual_portal_m(virtual_portal, message, response);
  438. case 'Q': // Query
  439. return virtual_portal_query(virtual_portal, message, response);
  440. case 'R':
  441. return virtual_portal_reset(virtual_portal, message, response);
  442. case 'S': // Status
  443. return virtual_portal_status(virtual_portal, response);
  444. case 'V':
  445. return 0;
  446. case 'W': // Write
  447. return virtual_portal_write(virtual_portal, message, response);
  448. case 'Z':
  449. return 0;
  450. default:
  451. FURI_LOG_W(TAG, "Unhandled command %c", message[0]);
  452. return 0; // No response
  453. }
  454. return 0;
  455. }