virtual_portal.c 15 KB

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