]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
uart_posix.c rework
authorpwpiwi <pwpiwi@users.noreply.github.com>
Mon, 27 Jan 2020 18:28:34 +0000 (13:28 -0500)
committerpwpiwi <pwpiwi@users.noreply.github.com>
Mon, 27 Jan 2020 19:14:40 +0000 (14:14 -0500)
* added some LED handling in appmain.c (helped with debugging)
* finally replaced the infamous device unlink by msleep(1000)
* fixed some format strings in comms.c (with -DCOMMS_DEBUG)
* made uart_receive() and uart_send() behave as described in header
* some formating

armsrc/appmain.c
client/comms.c
client/flash.c
uart/uart.h
uart/uart_posix.c

index 2eb54d27449ef2838ed8e9e93505bd2450b39896..4f0a19b9282da939760e155eff77bfea880d0487 100644 (file)
@@ -311,6 +311,7 @@ void set_hw_capabilities(void) {
 
 
 void SendVersion(void) {
 
 
 void SendVersion(void) {
+       LED_A_ON();
        set_hw_capabilities();
 
        char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
        set_hw_capabilities();
 
        char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
@@ -347,6 +348,7 @@ void SendVersion(void) {
        uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
        uint32_t compressed_data_section_size = common_area.arg1;
        cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, hw_capabilities, VersionString, strlen(VersionString) + 1);
        uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
        uint32_t compressed_data_section_size = common_area.arg1;
        cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, hw_capabilities, VersionString, strlen(VersionString) + 1);
+       LED_A_OFF();
 }
 
 // measure the USB Speed by sending SpeedTestBufferSize bytes to client and measuring the elapsed time.
 }
 
 // measure the USB Speed by sending SpeedTestBufferSize bytes to client and measuring the elapsed time.
@@ -362,13 +364,11 @@ void printUSBSpeed(void) {
        uint32_t start_time = end_time = GetTickCount();
        uint32_t bytes_transferred = 0;
 
        uint32_t start_time = end_time = GetTickCount();
        uint32_t bytes_transferred = 0;
 
-       LED_B_ON();
-       while(end_time < start_time + USB_SPEED_TEST_MIN_TIME) {
+       while (end_time < start_time + USB_SPEED_TEST_MIN_TIME) {
                cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, 0, USB_CMD_DATA_SIZE, 0, test_data, USB_CMD_DATA_SIZE);
                end_time = GetTickCount();
                bytes_transferred += USB_CMD_DATA_SIZE;
        }
                cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, 0, USB_CMD_DATA_SIZE, 0, test_data, USB_CMD_DATA_SIZE);
                end_time = GetTickCount();
                bytes_transferred += USB_CMD_DATA_SIZE;
        }
-       LED_B_OFF();
 
        Dbprintf("  Time elapsed:      %dms", end_time - start_time);
        Dbprintf("  Bytes transferred: %d", bytes_transferred);
 
        Dbprintf("  Time elapsed:      %dms", end_time - start_time);
        Dbprintf("  Bytes transferred: %d", bytes_transferred);
@@ -381,6 +381,7 @@ void printUSBSpeed(void) {
   * Prints runtime information about the PM3.
 **/
 void SendStatus(void) {
   * Prints runtime information about the PM3.
 **/
 void SendStatus(void) {
+       LED_A_ON();
        BigBuf_print_status();
        Fpga_print_status();
 #ifdef WITH_SMARTCARD
        BigBuf_print_status();
        Fpga_print_status();
 #ifdef WITH_SMARTCARD
@@ -393,7 +394,8 @@ void SendStatus(void) {
        Dbprintf("  ToSendMax..........%d", ToSendMax);
        Dbprintf("  ToSendBit..........%d", ToSendBit);
 
        Dbprintf("  ToSendMax..........%d", ToSendMax);
        Dbprintf("  ToSendBit..........%d", ToSendBit);
 
-       cmd_send(CMD_ACK,1,0,0,0,0);
+       cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
+       LED_A_OFF();
 }
 
 #if defined(WITH_ISO14443a_StandAlone) || defined(WITH_LF_StandAlone)
 }
 
 #if defined(WITH_ISO14443a_StandAlone) || defined(WITH_LF_StandAlone)
@@ -1334,9 +1336,11 @@ void UsbPacketReceived(UsbCommand *c) {
                        break;
 
                case CMD_FPGA_MAJOR_MODE_OFF:       // ## FPGA Control
                        break;
 
                case CMD_FPGA_MAJOR_MODE_OFF:       // ## FPGA Control
+                       LED_A_ON();
                        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
                        SpinDelay(200);
                        LED_D_OFF(); // LED D indicates field ON or OFF
                        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
                        SpinDelay(200);
                        LED_D_OFF(); // LED D indicates field ON or OFF
+                       LED_A_OFF();
                        break;
 
                case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
                        break;
 
                case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
index 5bb7e69edf848c648c74a5239f45ad9c2772df9b..716a163df24a1f1aeeb0c7492665cf7e89685e73 100644 (file)
@@ -17,9 +17,6 @@
 #include <pthread.h>
 #include <inttypes.h>
 
 #include <pthread.h>
 #include <inttypes.h>
 
-#if defined(__linux__) && !defined(NO_UNLINK)
-#include <unistd.h>     // for unlink()
-#endif
 #include "uart.h"
 #include "ui.h"
 #include "common.h"
 #include "uart.h"
 #include "ui.h"
 #include "common.h"
@@ -74,7 +71,7 @@ bool IsOffline() {
 
 void SendCommand(UsbCommand *c) {
        #ifdef COMMS_DEBUG
 
 void SendCommand(UsbCommand *c) {
        #ifdef COMMS_DEBUG
-       printf("Sending %04x cmd\n", c->cmd);
+       printf("Sending %04" PRIx64 " cmd\n", c->cmd);
        #endif
 
        if (offline) {
        #endif
 
        if (offline) {
@@ -106,8 +103,7 @@ void SendCommand(UsbCommand *c) {
  *  A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
  *  operation. Right now we'll just have to live with this.
  */
  *  A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
  *  operation. Right now we'll just have to live with this.
  */
-void clearCommandBuffer()
-{
+void clearCommandBuffer() {
        //This is a very simple operation
        pthread_mutex_lock(&rxBufferMutex);
        cmd_tail = cmd_head;
        //This is a very simple operation
        pthread_mutex_lock(&rxBufferMutex);
        cmd_tail = cmd_head;
@@ -118,11 +114,9 @@ void clearCommandBuffer()
  * @brief storeCommand stores a USB command in a circular buffer
  * @param UC
  */
  * @brief storeCommand stores a USB command in a circular buffer
  * @param UC
  */
-static void storeCommand(UsbCommand *command)
-{
+static void storeCommand(UsbCommand *command) {
        pthread_mutex_lock(&rxBufferMutex);
        pthread_mutex_lock(&rxBufferMutex);
-       if( (cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
-       {
+       if ((cmd_head + 1) % CMD_BUFFER_SIZE == cmd_tail) {
                // If these two are equal, we're about to overwrite in the
                // circular buffer.
                PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
                // If these two are equal, we're about to overwrite in the
                // circular buffer.
                PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
@@ -132,7 +126,7 @@ static void storeCommand(UsbCommand *command)
        UsbCommand* destination = &rxBuffer[cmd_head];
        memcpy(destination, command, sizeof(UsbCommand));
 
        UsbCommand* destination = &rxBuffer[cmd_head];
        memcpy(destination, command, sizeof(UsbCommand));
 
-       cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
+       cmd_head = (cmd_head + 1) % CMD_BUFFER_SIZE; //increment head and wrap
        pthread_mutex_unlock(&rxBufferMutex);
 }
 
        pthread_mutex_unlock(&rxBufferMutex);
 }
 
@@ -142,19 +136,18 @@ static void storeCommand(UsbCommand *command)
  * @param response location to write command
  * @return 1 if response was returned, 0 if nothing has been received
  */
  * @param response location to write command
  * @return 1 if response was returned, 0 if nothing has been received
  */
-static int getCommand(UsbCommand* response)
-{
+static int getCommand(UsbCommand* response) {
        pthread_mutex_lock(&rxBufferMutex);
        pthread_mutex_lock(&rxBufferMutex);
-       //If head == tail, there's nothing to read, or if we just got initialized
-       if (cmd_head == cmd_tail){
+       // If head == tail, there's nothing to read
+       if (cmd_head == cmd_tail) {
                pthread_mutex_unlock(&rxBufferMutex);
                return 0;
        }
 
                pthread_mutex_unlock(&rxBufferMutex);
                return 0;
        }
 
-       //Pick out the next unread command
+       // Pick out the next unread command
        UsbCommand* last_unread = &rxBuffer[cmd_tail];
        memcpy(response, last_unread, sizeof(UsbCommand));
        UsbCommand* last_unread = &rxBuffer[cmd_tail];
        memcpy(response, last_unread, sizeof(UsbCommand));
-       //Increment tail - this is a circular buffer, so modulo buffer size
+       // Increment tail - this is a circular buffer, so modulo buffer size
        cmd_tail = (cmd_tail + 1) % CMD_BUFFER_SIZE;
 
        pthread_mutex_unlock(&rxBufferMutex);
        cmd_tail = (cmd_tail + 1) % CMD_BUFFER_SIZE;
 
        pthread_mutex_unlock(&rxBufferMutex);
@@ -166,15 +159,14 @@ static int getCommand(UsbCommand* response)
 // Entry point into our code: called whenever we received a packet over USB.
 // Handle debug commands directly, store all other commands in circular buffer.
 //----------------------------------------------------------------------------------
 // Entry point into our code: called whenever we received a packet over USB.
 // Handle debug commands directly, store all other commands in circular buffer.
 //----------------------------------------------------------------------------------
-static void UsbCommandReceived(UsbCommand *UC)
-{
-       switch(UC->cmd) {
+static void UsbCommandReceived(UsbCommand *UC) {
+       switch (UC->cmd) {
                // First check if we are handling a debug message
                case CMD_DEBUG_PRINT_STRING: {
                        char s[USB_CMD_DATA_SIZE+1];
                        memset(s, 0x00, sizeof(s));
                // First check if we are handling a debug message
                case CMD_DEBUG_PRINT_STRING: {
                        char s[USB_CMD_DATA_SIZE+1];
                        memset(s, 0x00, sizeof(s));
-                       size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
-                       memcpy(s,UC->d.asBytes,len);
+                       size_t len = MIN(UC->arg[0], USB_CMD_DATA_SIZE);
+                       memcpy(s, UC->d.asBytes,len);
                        PrintAndLog("#db# %s", s);
                        return;
                } break;
                        PrintAndLog("#db# %s", s);
                        return;
                } break;
@@ -199,7 +191,7 @@ static bool receive_from_serial(serial_port sp, uint8_t *rx_buf, size_t len, siz
        while (uart_receive(sp, rx_buf + *received_len, len - *received_len, &bytes_read) && bytes_read && *received_len < len) {
                #ifdef COMMS_DEBUG
                if (bytes_read != len - *received_len) {
        while (uart_receive(sp, rx_buf + *received_len, len - *received_len, &bytes_read) && bytes_read && *received_len < len) {
                #ifdef COMMS_DEBUG
                if (bytes_read != len - *received_len) {
-                       printf("uart_receive() returned true but not enough bytes could be received. received: %d, wanted to receive: %d, already received before: %d\n",
+                       printf("uart_receive() returned true but not enough bytes could be received. received: %zd, wanted to receive: %zd, already received before: %zd\n",
                                bytes_read, len - *received_len, *received_len);
                }
                #endif
                                bytes_read, len - *received_len, *received_len);
                }
                #endif
@@ -235,8 +227,10 @@ __attribute__((force_align_arg_pointer))
                if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                        prx += rxlen;
                        if (response->cmd & CMD_VARIABLE_SIZE_FLAG) { // new style response with variable size
                if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                        prx += rxlen;
                        if (response->cmd & CMD_VARIABLE_SIZE_FLAG) { // new style response with variable size
-                               // PrintAndLog("received new style response %04" PRIx16 ", datalen = %d, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32 "\n",
-                                       // response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]);
+#ifdef COMMS_DEBUG
+                               PrintAndLog("received new style response %04" PRIx16 ", datalen = %zd, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32,
+                                       response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]);
+#endif
                                bytes_to_read = response->datalen;
                                if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                                        UsbCommand resp;
                                bytes_to_read = response->datalen;
                                if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                                        UsbCommand resp;
@@ -251,7 +245,9 @@ __attribute__((force_align_arg_pointer))
                                        }
                                }
                        } else { // old style response uses same data structure as commands. Fixed size.
                                        }
                                }
                        } else { // old style response uses same data structure as commands. Fixed size.
-                               // PrintAndLog("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64 "\n", command->cmd, command->arg[0]);
+#ifdef COMMS_DEBUG
+                               PrintAndLog("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64, command->cmd, command->arg[0]);
+#endif
                                bytes_to_read = sizeof(UsbCommand) - bytes_to_read;
                                if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                                        UsbCommandReceived(command);
                                bytes_to_read = sizeof(UsbCommand) - bytes_to_read;
                                if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                                        UsbCommandReceived(command);
@@ -302,8 +298,7 @@ __attribute__((force_align_arg_pointer))
  * @param show_warning display message after 2 seconds
  * @return true if command was returned, otherwise false
  */
  * @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)
-{
+bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) {
        UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
        SendCommand(&c);
 
        UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
        SendCommand(&c);
 
@@ -315,7 +310,7 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon
        }
 
        int bytes_completed = 0;
        }
 
        int bytes_completed = 0;
-       while(true) {
+       while (true) {
                if (getCommand(response)) {
                        if (response->cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
                                int copy_bytes = MIN(bytes - bytes_completed, response->arg[1]);
                if (getCommand(response)) {
                        if (response->cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
                                int copy_bytes = MIN(bytes - bytes_completed, response->arg[1]);
@@ -341,8 +336,7 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon
 }
 
 
 }
 
 
-bool GetFromFpgaRAM(uint8_t *dest, int bytes)
-{
+bool GetFromFpgaRAM(uint8_t *dest, int bytes) {
        UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
        SendCommand(&c);
 
        UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
        SendCommand(&c);
 
@@ -352,7 +346,7 @@ bool GetFromFpgaRAM(uint8_t *dest, int bytes)
 
        int bytes_completed = 0;
        bool show_warning = true;
 
        int bytes_completed = 0;
        bool show_warning = true;
-       while(true) {
+       while (true) {
                if (getCommand(&response)) {
                        if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
                                int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
                if (getCommand(&response)) {
                        if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
                                int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
@@ -387,7 +381,7 @@ bool OpenProxmark(void *port, bool wait_for_port, int timeout) {
                        msleep(1000);
                        printf(".");
                        fflush(stdout);
                        msleep(1000);
                        printf(".");
                        fflush(stdout);
-               } while(++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
+               } while (++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
                printf("\n");
        }
 
                printf("\n");
        }
 
@@ -433,15 +427,6 @@ void CloseProxmark(void) {
                uart_close(sp);
        }
 
                uart_close(sp);
        }
 
-#if defined(__linux__) && !defined(NO_UNLINK)
-       // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
-       //
-       // This may be disabled at compile-time with -DNO_UNLINK (used for a JNI-based serial port on Android).
-       if (serial_port_name) {
-               unlink(serial_port_name);
-       }
-#endif
-
        // Clean up our state
        sp = NULL;
        serial_port_name = NULL;
        // Clean up our state
        sp = NULL;
        serial_port_name = NULL;
@@ -493,6 +478,7 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
                        PrintAndLog("You can cancel this operation by pressing the pm3 button");
                        show_warning = false;
                }
                        PrintAndLog("You can cancel this operation by pressing the pm3 button");
                        show_warning = false;
                }
+               msleep(1);
        }
        return false;
 }
        }
        return false;
 }
index 67e371a20b2900591b35d91214c330c07ceb5cc2..f3d2f420934a87c0282571502ebbcc63a536e430 100644 (file)
@@ -185,9 +185,9 @@ static int check_segs(flash_file_t *ctx, int can_write_bl) {
        return 0;
 }
 
        return 0;
 }
 
+
 // Load an ELF file and prepare it for flashing
 // Load an ELF file and prepare it for flashing
-int flash_load(flash_file_t *ctx, const char *name, bool can_write_bl)
-{
+int flash_load(flash_file_t *ctx, const char *name, bool can_write_bl) {
        FILE *fd = NULL;
        Elf32_Ehdr ehdr;
        Elf32_Phdr *phdrs = NULL;
        FILE *fd = NULL;
        Elf32_Ehdr ehdr;
        Elf32_Phdr *phdrs = NULL;
@@ -267,9 +267,9 @@ fail:
        return -1;
 }
 
        return -1;
 }
 
+
 // Get the state of the proxmark, backwards compatible
 // Get the state of the proxmark, backwards compatible
-static int get_proxmark_state(uint32_t *state)
-{
+static int get_proxmark_state(uint32_t *state) {
        UsbCommand c = {0};
        c.cmd = CMD_DEVICE_INFO;
        SendCommand(&c);
        UsbCommand c = {0};
        c.cmd = CMD_DEVICE_INFO;
        SendCommand(&c);
@@ -300,9 +300,9 @@ static int get_proxmark_state(uint32_t *state)
        return 0;
 }
 
        return 0;
 }
 
+
 // Enter the bootloader to be able to start flashing
 // Enter the bootloader to be able to start flashing
-static int enter_bootloader(char *serial_port_name)
-{
+static int enter_bootloader(char *serial_port_name) {
        uint32_t state;
 
        if (get_proxmark_state(&state) < 0)
        uint32_t state;
 
        if (get_proxmark_state(&state) < 0)
@@ -314,7 +314,7 @@ static int enter_bootloader(char *serial_port_name)
        }
 
        if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
        }
 
        if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
-               fprintf(stderr,"Entering bootloader...\n");
+               fprintf(stderr, "Entering bootloader...\n");
                UsbCommand c;
                memset(&c, 0, sizeof (c));
 
                UsbCommand c;
                memset(&c, 0, sizeof (c));
 
@@ -336,6 +336,8 @@ static int enter_bootloader(char *serial_port_name)
                msleep(100);
                CloseProxmark();
 
                msleep(100);
                CloseProxmark();
 
+               msleep(1000); // wait for OS to detect device disconnect.
+
                bool opened = OpenProxmark(serial_port_name, true, 120);   // wait for 2 minutes
                if (opened) {
                        fprintf(stderr," Found.\n");
                bool opened = OpenProxmark(serial_port_name, true, 120);   // wait for 2 minutes
                if (opened) {
                        fprintf(stderr," Found.\n");
@@ -350,6 +352,7 @@ static int enter_bootloader(char *serial_port_name)
        return -1;
 }
 
        return -1;
 }
 
+
 static int wait_for_ack(void)
 {
        UsbCommand ack;
 static int wait_for_ack(void)
 {
        UsbCommand ack;
@@ -361,11 +364,11 @@ static int wait_for_ack(void)
        return 0;
 }
 
        return 0;
 }
 
+
 // Go into flashing mode
 int flash_start_flashing(int enable_bl_writes,char *serial_port_name)
 {
        uint32_t state;
 // Go into flashing mode
 int flash_start_flashing(int enable_bl_writes,char *serial_port_name)
 {
        uint32_t state;
-
        if (enter_bootloader(serial_port_name) < 0)
                return -1;
 
        if (enter_bootloader(serial_port_name) < 0)
                return -1;
 
index ce00bc0b773b94dbd8bad0f39e7e1d2b37b44ffe..c0a709cf05ee2bc0cf97b1900ca25f63fdc55e88 100644 (file)
@@ -88,6 +88,8 @@ extern bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLe
 /* Sends a buffer to a given serial port.
  *   pbtTx: A pointer to a buffer containing the data to send.
  *   szTxLen: The amount of data to be sent.
 /* Sends a buffer to a given serial port.
  *   pbtTx: A pointer to a buffer containing the data to send.
  *   szTxLen: The amount of data to be sent.
+ *
+ * Returns TRUE if all data could be sent within 30ms.
  */
 extern bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
 
  */
 extern bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
 
@@ -99,5 +101,5 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed);
  */
 extern uint32_t uart_get_speed(const serial_port sp);
 
  */
 extern uint32_t uart_get_speed(const serial_port sp);
 
-#endif // _PM3_UART_H_
+#endif // PM3_UART_H__
 
 
index 7eeb1a8385bd871b9dd6640cb63c4b553d970640..f6e6519de885472ff5f138a0cca29ab59d86bd47 100755 (executable)
@@ -52,6 +52,8 @@
 #include <sys/socket.h>\r
 #include <netinet/tcp.h>\r
 #include <netdb.h>\r
 #include <sys/socket.h>\r
 #include <netinet/tcp.h>\r
 #include <netdb.h>\r
+#include <sys/time.h>\r
+#include <errno.h>\r
 \r
 // Fix missing definition on OS X.\r
 // Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7\r
 \r
 // Fix missing definition on OS X.\r
 // Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7\r
@@ -75,8 +77,8 @@ static struct timeval timeout = {
 \r
 void uart_close(const serial_port sp) {\r
        serial_port_unix* spu = (serial_port_unix*)sp;\r
 \r
 void uart_close(const serial_port sp) {\r
        serial_port_unix* spu = (serial_port_unix*)sp;\r
-       tcflush(spu->fd,TCIOFLUSH);\r
-       tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));\r
+       tcflush(spu->fd, TCIOFLUSH);\r
+       tcsetattr(spu->fd, TCSANOW, &(spu->tiOld));\r
        struct flock fl;\r
        fl.l_type   = F_UNLCK;\r
        fl.l_whence = SEEK_SET;\r
        struct flock fl;\r
        fl.l_type   = F_UNLCK;\r
        fl.l_whence = SEEK_SET;\r
@@ -174,7 +176,7 @@ serial_port uart_open(const char* pcPortName) {
        }\r
 \r
        // Try to retrieve the old (current) terminal info struct\r
        }\r
 \r
        // Try to retrieve the old (current) terminal info struct\r
-       if(tcgetattr(sp->fd,&sp->tiOld) == -1) {\r
+       if (tcgetattr(sp->fd,&sp->tiOld) == -1) {\r
                uart_close(sp);\r
                return INVALID_SERIAL_PORT;\r
        }\r
                uart_close(sp);\r
                return INVALID_SERIAL_PORT;\r
        }\r
@@ -183,18 +185,16 @@ serial_port uart_open(const char* pcPortName) {
        sp->tiNew = sp->tiOld;\r
 \r
        // Configure the serial port\r
        sp->tiNew = sp->tiOld;\r
 \r
        // Configure the serial port\r
-       sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;\r
-       sp->tiNew.c_iflag = IGNPAR;\r
-       sp->tiNew.c_oflag = 0;\r
-       sp->tiNew.c_lflag = 0;\r
+       sp->tiNew.c_cflag &= ~(CSIZE | PARENB);\r
+       sp->tiNew.c_cflag |= (CS8 | CLOCAL | CREAD);\r
+       sp->tiNew.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);\r
+       sp->tiNew.c_iflag |= IGNPAR;\r
+       sp->tiNew.c_oflag &= ~OPOST;\r
+       sp->tiNew.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);\r
 \r
 \r
-       // Block until n bytes are received\r
-       sp->tiNew.c_cc[VMIN] = 0;\r
-       // Block until a timer expires (n * 100 mSec.)\r
-       sp->tiNew.c_cc[VTIME] = 0;\r
 \r
        // Try to set the new terminal info struct\r
 \r
        // Try to set the new terminal info struct\r
-       if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {\r
+       if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) {\r
                uart_close(sp);\r
                return INVALID_SERIAL_PORT;\r
        }\r
                uart_close(sp);\r
                return INVALID_SERIAL_PORT;\r
        }\r
@@ -206,94 +206,75 @@ serial_port uart_open(const char* pcPortName) {
 }\r
 \r
 \r
 }\r
 \r
 \r
-bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {\r
-       int byteCount;\r
-       fd_set rfds;\r
-       struct timeval tv;\r
+bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t szMaxRxLen, size_t* pszRxLen) {\r
 \r
 \r
-       // Reset the output count\r
        *pszRxLen = 0;\r
 \r
        *pszRxLen = 0;\r
 \r
-       do {\r
-               // Reset file descriptor\r
-               FD_ZERO(&rfds);\r
-               FD_SET(((serial_port_unix*)sp)->fd,&rfds);\r
-               tv = timeout;\r
-               int res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);\r
+       if (szMaxRxLen == 0) return true;\r
 \r
 \r
-               // Read error\r
-               if (res < 0) {\r
-                       return false;\r
-               }\r
+       struct timeval t_current;\r
+       gettimeofday(&t_current, NULL);\r
+       struct timeval t_end;\r
+       timeradd(&t_current, &timeout, &t_end);\r
 \r
 \r
-               // Read time-out\r
-               if (res == 0) {\r
-                       if (*pszRxLen == 0) {\r
-                               // Error, we received no data\r
-                               return false;\r
-                       } else {\r
-                               // We received some data, but nothing more is available\r
-                               return true;\r
-                       }\r
+       while (true) {\r
+               int res = read(((serial_port_unix*)sp)->fd, pbtRx, szMaxRxLen - *pszRxLen);\r
+               if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) return false;\r
+               if (res > 0) {\r
+                       *pszRxLen += res;\r
+                       pbtRx += res;\r
                }\r
                }\r
-\r
-               // Retrieve the count of the incoming bytes\r
-               res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);\r
+               if (*pszRxLen == szMaxRxLen) return true; // we could read all requested bytes in time\r
+               gettimeofday(&t_current, NULL);\r
+               if (timercmp(&t_current, &t_end, >)) return true; // timeout\r
+               // set next select timeout\r
+               struct timeval t_remains;\r
+               timersub(&t_end, &t_current, &t_remains);\r
+               // Set the file descriptor set\r
+               fd_set rfds;\r
+               FD_ZERO(&rfds);\r
+               FD_SET(((serial_port_unix*)sp)->fd, &rfds);\r
+               // wait for more bytes available\r
+               res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &t_remains);\r
                if (res < 0) return false;\r
                if (res < 0) return false;\r
-\r
-               // Cap the number of bytes, so we don't overrun the buffer\r
-               if (pszMaxRxLen - (*pszRxLen) < byteCount) {\r
-                       byteCount = pszMaxRxLen - (*pszRxLen);\r
-               }\r
-\r
-               // There is something available, read the data\r
-               res = read(((serial_port_unix*)sp)->fd, pbtRx+(*pszRxLen), byteCount);\r
-\r
-               // Stop if the OS has some troubles reading the data\r
-               if (res <= 0) return false;\r
-\r
-               *pszRxLen += res;\r
-\r
-               if (*pszRxLen == pszMaxRxLen) {\r
-                       // We have all the data we wanted.\r
-                       return true;\r
-               }\r
-\r
-       } while (byteCount);\r
-\r
-       return true;\r
+               if (res == 0) return true; // timeout\r
+       }\r
+       return true; // should never come here\r
 }\r
 \r
 \r
 bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {\r
 }\r
 \r
 \r
 bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {\r
-       size_t szPos = 0;\r
-       fd_set rfds;\r
-       struct timeval tv;\r
-\r
-       while (szPos < szTxLen) {\r
-               // Reset file descriptor\r
-               FD_ZERO(&rfds);\r
-               FD_SET(((serial_port_unix*)sp)->fd,&rfds);\r
-               tv = timeout;\r
-               int res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);\r
 \r
 \r
-               // Write error\r
-               if (res < 0) {\r
-                       return false;\r
-               }\r
+       if (szTxLen == 0) return true;\r
 \r
 \r
-               // Write time-out\r
-               if (res == 0) {\r
-                       return false;\r
-               }\r
+       size_t bytes_written = 0;\r
 \r
 \r
-               // Send away the bytes\r
-               res = write(((serial_port_unix*)sp)->fd, pbtTx+szPos, szTxLen-szPos);\r
+       struct timeval t_current;\r
+       gettimeofday(&t_current, NULL);\r
+       struct timeval t_end;\r
+       timeradd(&t_current, &timeout, &t_end);\r
 \r
 \r
-               // Stop if the OS has some troubles sending the data\r
-               if (res <= 0) return false;\r
-\r
-               szPos += res;\r
+       while (true) {\r
+               int res = write(((serial_port_unix*)sp)->fd, pbtTx, szTxLen - bytes_written);\r
+               if (res < 0 && res != EAGAIN && res != EWOULDBLOCK) return false;\r
+               if (res > 0) {\r
+                       pbtTx += res;\r
+                       bytes_written += res;\r
+               }\r
+               if (bytes_written == szTxLen) return true; // we could write all bytes\r
+               gettimeofday(&t_current, NULL);\r
+               if (timercmp(&t_current, &t_end, >)) return false; // timeout\r
+               // set next select timeout\r
+               struct timeval t_remains;\r
+               timersub(&t_end, &t_current, &t_remains);\r
+               // Set the file descriptor set\r
+               fd_set wfds;\r
+               FD_ZERO(&wfds);\r
+               FD_SET(((serial_port_unix*)sp)->fd, &wfds);\r
+               // wait until more bytes can be written\r
+               res = select(((serial_port_unix*)sp)->fd+1, NULL, &wfds, NULL, &t_remains);\r
+               if (res < 0) return false;  // error\r
+               if (res == 0) return false; // timeout\r
        }\r
        return true;\r
 }\r
        }\r
        return true;\r
 }\r
Impressum, Datenschutz