]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - uart/uart_win32.c
fixing iso15693
[proxmark3-svn] / uart / uart_win32.c
index af521ead183741637b9a56fb203c8607bd878971..0e4227de4caf0929fc3d175b8d8ecc6dfc970b3d 100644 (file)
 
 #include "uart.h"
 
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+
 // The windows serial port implementation
 #ifdef _WIN32
 #include <windows.h>
 
 typedef struct {
-  HANDLE hPort;     // Serial port handle
-  DCB dcb;          // Device control settings
-  COMMTIMEOUTS ct;  // Serial port time-out configuration
+       HANDLE hPort;     // Serial port handle
+       DCB dcb;          // Device control settings
+       COMMTIMEOUTS ct;  // Serial port time-out configuration
 } serial_port_windows;
 
+
 void upcase(char *p) {
-  while(*p != '\0') {
-    if(*p >= 97 && *p <= 122) {
-      *p -= 32;
-    }
-    ++p;
-  }
+       while(*p != '\0') {
+               if(*p >= 97 && *p <= 122) {
+                       *p -= 32;
+               }
+               ++p;
+       }
 }
 
-serial_port uart_open(const char* pcPortName) {
-  char acPortName[255];
-  serial_port_windows* sp = malloc(sizeof(serial_port_windows));
-  
-  // Copy the input "com?" to "\\.\COM?" format
-  sprintf(acPortName,"\\\\.\\%s",pcPortName);
-  upcase(acPortName);
-  
-  // Try to open the serial port
-  sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
-  if (sp->hPort == INVALID_HANDLE_VALUE) {
-    uart_close(sp);
-    return INVALID_SERIAL_PORT;
-  }
-  
-  // Prepare the device control
-  memset(&sp->dcb, 0, sizeof(DCB));
-  sp->dcb.DCBlength = sizeof(DCB);
-  if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
-    uart_close(sp);
-    return INVALID_SERIAL_PORT;
-  }
-  
-  // Update the active serial port
-  if(!SetCommState(sp->hPort,&sp->dcb)) {
-    uart_close(sp);
-    return INVALID_SERIAL_PORT;
-  }
-  
-  sp->ct.ReadIntervalTimeout         = 0;
-  sp->ct.ReadTotalTimeoutMultiplier  = 0;
-  sp->ct.ReadTotalTimeoutConstant    = 30;
-  sp->ct.WriteTotalTimeoutMultiplier = 0;
-  sp->ct.WriteTotalTimeoutConstant   = 30;
-  
-  if(!SetCommTimeouts(sp->hPort,&sp->ct)) {
-    uart_close(sp);
-    return INVALID_SERIAL_PORT;
-  }
-  
-  PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
-  
-  return sp;
-}
 
 void uart_close(const serial_port sp) {
-  CloseHandle(((serial_port_windows*)sp)->hPort);
-  free(sp);
+       CloseHandle(((serial_port_windows*)sp)->hPort);
+       free(sp);
 }
 
-bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
-  ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,pszMaxRxLen,(LPDWORD)pszRxLen,NULL);
-  return (*pszRxLen != 0);
+
+serial_port uart_open(const char* pcPortName) {
+       char acPortName[255];
+       serial_port_windows* sp = malloc(sizeof(serial_port_windows));
+
+       // Copy the input "com?" to "\\.\COM?" format
+       sprintf(acPortName,"\\\\.\\%s",pcPortName);
+       upcase(acPortName);
+
+       // Try to open the serial port
+       sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
+       if (sp->hPort == INVALID_HANDLE_VALUE) {
+               uart_close(sp);
+               return INVALID_SERIAL_PORT;
+       }
+
+       // Prepare the device control
+       memset(&sp->dcb, 0, sizeof(DCB));
+       sp->dcb.DCBlength = sizeof(DCB);
+       if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
+               uart_close(sp);
+               return INVALID_SERIAL_PORT;
+       }
+
+       // Update the active serial port
+       if (!SetCommState(sp->hPort,&sp->dcb)) {
+               uart_close(sp);
+               return INVALID_SERIAL_PORT;
+       }
+
+       sp->ct.ReadIntervalTimeout         = 0;
+       sp->ct.ReadTotalTimeoutMultiplier  = 0;
+       sp->ct.ReadTotalTimeoutConstant    = 30;
+       sp->ct.WriteTotalTimeoutMultiplier = 0;
+       sp->ct.WriteTotalTimeoutConstant   = 30;
+
+       if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
+               uart_close(sp);
+               return INVALID_SERIAL_PORT;
+       }
+
+       PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
+
+       return sp;
+}
+
+
+bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
+       return ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
 }
 
-bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) {
-  DWORD dwTxLen = 0;
-  return WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,szTxLen,&dwTxLen,NULL);
-  return (dwTxLen != 0);
+
+bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
+       DWORD dwTxLen = 0;
+       return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
 }
 
+
 bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
-  serial_port_windows* spw;
-  spw = (serial_port_windows*)sp;
-  spw->dcb.BaudRate = uiPortSpeed;
-  return SetCommState(spw->hPort, &spw->dcb);
+       serial_port_windows* spw;
+       spw = (serial_port_windows*)sp;
+       spw->dcb.BaudRate = uiPortSpeed;
+       return SetCommState(spw->hPort, &spw->dcb);
 }
 
+
 uint32_t uart_get_speed(const serial_port sp) {
-  const serial_port_windows* spw = (serial_port_windows*)sp;
-  if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
-    return spw->dcb.BaudRate;
-  }
-  return 0;
+       const serial_port_windows* spw = (serial_port_windows*)sp;
+       if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
+               return spw->dcb.BaudRate;
+       }
+       return 0;
 }
 
 #endif
Impressum, Datenschutz