virtual_portal.c 13 KB

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