virtual_portal.c 14 KB

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