+/**
+ * Data transfer from Proxmark to client. This method times out after
+ * ms_timeout milliseconds.
+ * @brief GetFromBigBuf
+ * @param dest Destination address for transfer
+ * @param bytes number of bytes to be transferred
+ * @param start_index offset into Proxmark3 BigBuf[]
+ * @param response struct to copy last command (CMD_ACK) into
+ * @param ms_timeout timeout in milliseconds
+ * @param show_warning display message after 2 seconds
+ * @return true if command was returned, otherwise false
+ */
+bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) {
+
+ uint64_t start_time = msclock();
+
+ UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
+ SendCommand(&c);
+
+ UsbCommand resp;
+ if (response == NULL) {
+ response = &resp;
+ }
+
+ int bytes_completed = 0;
+ while (true) {
+ if (msclock() - start_time > ms_timeout) {
+ break; // timeout
+ }
+ if (msclock() - start_time > 2000 && show_warning) {
+ // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
+ PrintAndLog("Waiting for a response from the proxmark...");
+ PrintAndLog("You can cancel this operation by pressing the pm3 button");
+ show_warning = false;
+ }
+ if (getCommand(response, CMD_BUFFER_CHECK_TIME)) {
+ if (response->cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
+ int copy_bytes = MIN(bytes - bytes_completed, response->arg[1]);
+ memcpy(dest + response->arg[0], response->d.asBytes, copy_bytes);
+ bytes_completed += copy_bytes;
+ } else if (response->cmd == CMD_ACK) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+
+bool GetFromFpgaRAM(uint8_t *dest, int bytes) {
+
+ uint64_t start_time = msclock();
+
+ UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
+ SendCommand(&c);
+
+ UsbCommand response;
+
+ int bytes_completed = 0;
+ bool show_warning = true;
+ while (true) {
+ if (msclock() - start_time > 2000 && show_warning) {
+ PrintAndLog("Waiting for a response from the proxmark...");
+ PrintAndLog("You can cancel this operation by pressing the pm3 button");
+ show_warning = false;
+ }
+ if (getCommand(&response, CMD_BUFFER_CHECK_TIME)) {
+ if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
+ int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
+ memcpy(dest + response.arg[0], response.d.asBytes, copy_bytes);
+ bytes_completed += copy_bytes;
+ } else if (response.cmd == CMD_ACK) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+
+bool OpenProxmark(void *port, bool wait_for_port, int timeout) {
+ char *portname = (char *)port;
+ if (!wait_for_port) {
+ sp = uart_open(portname);
+ } else {
+ printf("Waiting for Proxmark to appear on %s ", portname);
+ fflush(stdout);
+ int openCount = 0;
+ do {
+ sp = uart_open(portname);
+ msleep(1000);
+ printf(".");
+ fflush(stdout);
+ } while (++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
+ printf("\n");
+ }
+
+ // check result of uart opening
+ if (sp == INVALID_SERIAL_PORT) {
+ printf("ERROR: invalid serial port\n");
+ sp = NULL;
+ serial_port_name = NULL;
+ return false;
+ } else if (sp == CLAIMED_SERIAL_PORT) {
+ printf("ERROR: serial port is claimed by another process\n");
+ sp = NULL;
+ serial_port_name = NULL;
+ return false;
+ } else {
+ // start the USB communication thread
+ serial_port_name = portname;
+ conn.run = true;
+ pthread_create(&USB_communication_thread, NULL, &uart_communication, &conn);
+ return true;
+ }
+}
+
+
+void CloseProxmark(void) {
+ conn.run = false;
+
+#ifdef __BIONIC__
+ // In Android O and later, if an invalid pthread_t is passed to pthread_join, it calls fatal().
+ // https://github.com/aosp-mirror/platform_bionic/blob/ed16b344e75f422fb36fbfd91fb30de339475880/libc/bionic/pthread_internal.cpp#L116-L128
+ //
+ // In Bionic libc, pthread_t is an integer.
+
+ if (USB_communication_thread != 0) {
+ pthread_join(USB_communication_thread, NULL);
+ }
+#else
+ // pthread_t is a struct on other libc, treat as an opaque memory reference
+ pthread_join(USB_communication_thread, NULL);
+#endif
+
+ if (sp) {
+ uart_close(sp);
+ }
+
+ // Clean up our state
+ sp = NULL;
+ serial_port_name = NULL;
+#ifdef __BIONIC__
+ memset(&USB_communication_thread, 0, sizeof(pthread_t));
+#endif
+}
+
+