virtual_portal.c 17 KB

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