virtual_portal.c 12 KB

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