virtual_portal.c 14 KB

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