virtual_portal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 int32_t pof_thread_worker(void* context) {
  17. VirtualPortal* virtual_portal = context;
  18. UNUSED(virtual_portal);
  19. while(true) {
  20. uint32_t flags = furi_thread_flags_wait(EventAll, FuriFlagWaitAny, 32);
  21. if(flags) {
  22. if(flags & EventExit) {
  23. FURI_LOG_I(TAG, "exit");
  24. break;
  25. }
  26. }
  27. }
  28. return 0;
  29. }
  30. VirtualPortal* virtual_portal_alloc(NotificationApp* notifications) {
  31. VirtualPortal* virtual_portal = malloc(sizeof(VirtualPortal));
  32. virtual_portal->notifications = notifications;
  33. notification_message(virtual_portal->notifications, &sequence_set_backlight);
  34. notification_message(virtual_portal->notifications, &sequence_set_leds);
  35. for(int i = 0; i < POF_TOKEN_LIMIT; i++) {
  36. virtual_portal->tokens[i] = pof_token_alloc();
  37. }
  38. virtual_portal->sequence_number = 0;
  39. virtual_portal->active = false;
  40. virtual_portal->thread = furi_thread_alloc();
  41. furi_thread_set_name(virtual_portal->thread, "PoFLed");
  42. furi_thread_set_stack_size(virtual_portal->thread, 2 * 1024);
  43. furi_thread_set_context(virtual_portal->thread, virtual_portal);
  44. furi_thread_set_callback(virtual_portal->thread, pof_thread_worker);
  45. furi_thread_start(virtual_portal->thread);
  46. return virtual_portal;
  47. }
  48. void virtual_portal_cleanup(VirtualPortal* virtual_portal) {
  49. notification_message(virtual_portal->notifications, &sequence_reset_rgb);
  50. notification_message(virtual_portal->notifications, &sequence_display_backlight_on);
  51. }
  52. void virtual_portal_free(VirtualPortal* virtual_portal) {
  53. for(int i = 0; i < POF_TOKEN_LIMIT; i++) {
  54. pof_token_free(virtual_portal->tokens[i]);
  55. virtual_portal->tokens[i] = NULL;
  56. }
  57. furi_assert(virtual_portal->thread);
  58. furi_thread_flags_set(furi_thread_get_id(virtual_portal->thread), EventExit);
  59. furi_thread_join(virtual_portal->thread);
  60. furi_thread_free(virtual_portal->thread);
  61. virtual_portal->thread = NULL;
  62. free(virtual_portal);
  63. }
  64. void virtaul_portal_set_leds(uint8_t r, uint8_t g, uint8_t b) {
  65. furi_hal_light_set(LightRed, r);
  66. furi_hal_light_set(LightGreen, g);
  67. furi_hal_light_set(LightBlue, b);
  68. }
  69. void virtaul_portal_set_backlight(uint8_t brightness) {
  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. return virtual_portal_status(virtual_portal, response);
  192. }
  193. return 0;
  194. }
  195. // 4d01ff0000d0077d6c2a77a400000000
  196. int virtual_portal_m(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  197. virtual_portal->speaker = (message[1] == 1);
  198. /*
  199. char display[33] = {0};
  200. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  201. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  202. }
  203. FURI_LOG_I(TAG, "M %s", display);
  204. */
  205. size_t index = 0;
  206. response[index++] = 'M';
  207. response[index++] = message[1];
  208. response[index++] = 0x00;
  209. response[index++] = 0x19;
  210. return index;
  211. }
  212. int virtual_portal_l(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  213. UNUSED(virtual_portal);
  214. /*
  215. char display[33] = {0};
  216. memset(display, 0, sizeof(display));
  217. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  218. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  219. }
  220. FURI_LOG_I(TAG, "L %s", display);
  221. */
  222. uint8_t side = message[1]; // 0: left, 2: right
  223. uint8_t brightness = 0;
  224. switch(side) {
  225. case 0:
  226. case 2:
  227. virtaul_portal_set_leds(message[2], message[3], message[4]);
  228. break;
  229. case 1:
  230. brightness = message[2];
  231. virtaul_portal_set_backlight(brightness);
  232. break;
  233. case 3:
  234. brightness = 0xff;
  235. virtaul_portal_set_backlight(brightness);
  236. break;
  237. }
  238. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  239. size_t index = 0;
  240. response[index++] = 'J';
  241. return index;
  242. }
  243. int virtual_portal_j(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  244. UNUSED(virtual_portal);
  245. /*
  246. char display[33] = {0};
  247. memset(display, 0, sizeof(display));
  248. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  249. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  250. }
  251. FURI_LOG_I(TAG, "J %s", display);
  252. */
  253. uint8_t side = message[1]; // 0: left, 2: right
  254. uint8_t r = message[2]; // 0: left, 2: right
  255. uint8_t g = message[3]; // 0: left, 2: right
  256. uint8_t b = message[4]; // 0: left, 2: right
  257. uint16_t delay = message[6] << 8 | message[5];
  258. switch(side) {
  259. case 0:
  260. case 2:
  261. virtaul_portal_set_leds(r, g, b);
  262. break;
  263. }
  264. // Delay response
  265. // furi_delay_ms(delay); // causes issues
  266. UNUSED(delay);
  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_query(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  273. int index = message[1];
  274. int blockNum = message[2];
  275. int arrayIndex = index & 0x0f;
  276. FURI_LOG_I(TAG, "Query %d %d", arrayIndex, blockNum);
  277. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  278. if(!pof_token->loaded) {
  279. response[0] = 'Q';
  280. response[1] = 0x00 | arrayIndex;
  281. response[2] = blockNum;
  282. return 3;
  283. }
  284. NfcDevice* nfc_device = pof_token->nfc_device;
  285. const MfClassicData* data = nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  286. const MfClassicBlock block = data->block[blockNum];
  287. response[0] = 'Q';
  288. response[1] = 0x10 | arrayIndex;
  289. response[2] = blockNum;
  290. memcpy(response + 3, block.data, BLOCK_SIZE);
  291. return 3 + BLOCK_SIZE;
  292. }
  293. int virtual_portal_write(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  294. int index = message[1];
  295. int blockNum = message[2];
  296. int arrayIndex = index & 0x0f;
  297. char display[33] = {0};
  298. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  299. snprintf(display + (i * 2), sizeof(display), "%02x", message[3 + i]);
  300. }
  301. FURI_LOG_I(TAG, "Write %d %d %s", arrayIndex, blockNum, display);
  302. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  303. if(!pof_token->loaded) {
  304. response[0] = 'W';
  305. response[1] = 0x00 | arrayIndex;
  306. response[2] = blockNum;
  307. return 3;
  308. }
  309. NfcDevice* nfc_device = pof_token->nfc_device;
  310. MfClassicData* data = mf_classic_alloc();
  311. nfc_device_copy_data(nfc_device, NfcProtocolMfClassic, data);
  312. MfClassicBlock* block = &data->block[blockNum];
  313. memcpy(block->data, message + 3, BLOCK_SIZE);
  314. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, data);
  315. mf_classic_free(data);
  316. response[0] = 'W';
  317. response[1] = 0x10 | arrayIndex;
  318. response[2] = blockNum;
  319. return 3;
  320. }
  321. // 32 byte message, 32 byte response;
  322. int virtual_portal_process_message(
  323. VirtualPortal* virtual_portal,
  324. uint8_t* message,
  325. uint8_t* response) {
  326. memset(response, 0, 32);
  327. switch(message[0]) {
  328. case 'A':
  329. return virtual_portal_activate(virtual_portal, message, response);
  330. case 'C': //Ring color R G B
  331. virtaul_portal_set_leds(message[1], message[2], message[3]);
  332. return 0;
  333. case 'J':
  334. // https://github.com/flyandi/flipper_zero_rgb_led
  335. return virtual_portal_j(virtual_portal, message, response);
  336. case 'L':
  337. return virtual_portal_l(virtual_portal, message, response);
  338. case 'M':
  339. return virtual_portal_m(virtual_portal, message, response);
  340. case 'Q': //Query
  341. return virtual_portal_query(virtual_portal, message, response);
  342. case 'R':
  343. return virtual_portal_reset(virtual_portal, message, response);
  344. case 'S': //Status
  345. return virtual_portal_status(virtual_portal, response);
  346. case 'V':
  347. return 0;
  348. case 'W': //Write
  349. return virtual_portal_write(virtual_portal, message, response);
  350. case 'Z':
  351. return 0;
  352. default:
  353. FURI_LOG_W(TAG, "Unhandled command %c", message[0]);
  354. return 0; //No response
  355. }
  356. return 0;
  357. }