virtual_portal.c 15 KB

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