virtual_portal.c 15 KB

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