[Tinyos-2-commits] CVS: tinyos-2.x/support/sdk/c/sf serialsource.c, 1.2, 1.3 serialsource.h, 1.1, 1.2

David Gay idgay at users.sourceforge.net
Fri Sep 19 12:36:27 PDT 2008


Update of /cvsroot/tinyos/tinyos-2.x/support/sdk/c/sf
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv26236

Modified Files:
	serialsource.c serialsource.h 
Log Message:
win32 support from Akos Maroy (darkeye at tyrell.hu)

Index: serialsource.c
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/c/sf/serialsource.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** serialsource.c	5 Dec 2007 23:22:11 -0000	1.2
--- serialsource.c	19 Sep 2008 19:36:23 -0000	1.3
***************
*** 1,2 ****
--- 1,3 ----
+ #ifndef _WIN32
  #include <sys/types.h>
  #include <sys/stat.h>
***************
*** 16,19 ****
--- 17,26 ----
  #include <stdint.h>
  #endif
+ #endif
+ 
+ #ifdef _WIN32
+ #include <windows.h>
+ #include <stdint.h>
+ #endif
  
  /* C implementation of the mote serial protocol. See
***************
*** 29,35 ****
--- 36,44 ----
  enum {
  #ifndef __CYGWIN__
+ #ifndef _WIN32
    FALSE = 0,
    TRUE = 1,
  #endif
+ #endif
    BUFSIZE = 256,
    MTU = 256,
***************
*** 51,56 ****
  };
  
! struct serial_source {
    int fd;
    bool non_blocking;
    void (*message)(serial_source_msg problem);
--- 60,69 ----
  };
  
! struct serial_source_t {
! #ifndef _WIN32
    int fd;
+ #else
+   HANDLE hComm;
+ #endif
    bool non_blocking;
    void (*message)(serial_source_msg problem);
***************
*** 73,76 ****
--- 86,90 ----
  };
  
+ #ifndef _WIN32
  static tcflag_t parse_baudrate(int requested)
  {
***************
*** 174,177 ****
--- 188,192 ----
    return baudrate;
  }
+ #endif
  
  #ifdef DEBUG
***************
*** 195,198 ****
--- 210,214 ----
  static int serial_read(serial_source src, int non_blocking, void *buffer, int n)
  {
+ #ifndef _WIN32
    fd_set fds;
    int cnt;
***************
*** 226,229 ****
--- 242,269 ----
  	  return cnt;
        }
+ #else // _WIN32
+   int cnt;
+ 
+   if (non_blocking) {
+     ReadFile(src->hComm, buffer, n, &cnt, NULL);
+ 
+     return cnt;
+ 
+   } else {
+       for (;;) {
+           DWORD eventMask;
+           SetCommMask(src->hComm, EV_RXCHAR);
+           if (!WaitCommEvent(src->hComm, &eventMask, NULL)) {
+               return -1;
+           }
+ 
+           ReadFile(src->hComm, buffer, n, &cnt, NULL);
+ 
+           if (cnt != 0) {
+             return cnt;
+           }
+       }
+   }
+ #endif
  }
  
***************
*** 238,241 ****
--- 278,282 ----
   */
  {
+ #ifndef _WIN32
    struct termios newtio;
    int fd;
***************
*** 289,294 ****
--- 330,379 ----
  
    return NULL;
+ #else // _WIN32
+ 	LPCTSTR       ComName = (LPCTSTR)device;
+     HANDLE        hComm;
+ 	DCB           dcb;
+     serial_source src;
+ 
+ 	int buflen = MultiByteToWideChar(CP_ACP,0,(PCSTR)device,-1,(LPWSTR)ComName,0);
+ 	MultiByteToWideChar(CP_ACP,0,(PCSTR)device,-1,(LPWSTR)ComName,buflen);
+ 	
+ 	//syncronize
+ 	hComm = CreateFile(ComName,  GENERIC_READ | GENERIC_WRITE,  0,  NULL,  OPEN_EXISTING,
+ 					FILE_ATTRIBUTE_NORMAL, NULL);
+ 
+     if (hComm == INVALID_HANDLE_VALUE) {
+         return NULL;
+     }
+ 
+     PurgeComm(hComm, PURGE_RXCLEAR);
+ 
+ 	GetCommState(hComm, &dcb); 
+ 	dcb.BaudRate = baud_rate;
+ 	dcb.ByteSize = 8;
+ 	dcb.Parity = NOPARITY;
+ 	dcb.fParity = FALSE;
+ 	dcb.StopBits = ONESTOPBIT;
+     if (SetCommState(hComm, &dcb) == 0) {
+         return NULL;
+     }
+ 
+     src = malloc(sizeof *src);
+ 
+     if (src) {
+ 	  memset(src, 0, sizeof *src);
+ 	  src->hComm = hComm;
+ 	  src->non_blocking = non_blocking;
+ 	  src->message = message;
+ 	  src->send.seqno = 37;
+ 
+ 	}
+ 
+ 	return src;
+ 
+ #endif // _WIN32
  }
  
+ #ifndef _WIN32
  int serial_source_fd(serial_source src)
  /* Returns: the file descriptor used by serial source src (useful when
***************
*** 298,301 ****
--- 383,397 ----
    return src->fd;
  }
+ #endif
+ 
+ #ifdef _WIN32
+ HANDLE serial_source_handle(serial_source src)
+ /* Returns: the file descriptor used by serial source src (useful when
+      non-blocking reads were requested)
+ */
+ {
+   return src->hComm;
+ }
+ #endif
  
  int close_serial_source(serial_source src)
***************
*** 305,309 ****
--- 401,409 ----
   */
  {
+ #ifndef _WIN32
    int ok = close(src->fd);
+ #else
+   int ok = CloseHandle(src->hComm);
+ #endif
  
    free(src);
***************
*** 318,321 ****
--- 418,422 ----
  */
  {
+ #ifndef _WIN32
    struct timeval tv;
    fd_set fds;
***************
*** 355,362 ****
--- 456,476 ----
        return 0;
      }
+ #else // _WIN32
+     // FIXME: the deadline is ignored here
+ 
+     DWORD eventMask;
+     SetCommMask(src->hComm, EV_RXCHAR);
+     if (!WaitCommEvent(src->hComm, &eventMask, NULL)) {
+         return -1;
+     }
+ 
+     return 0;
+ 
+ #endif
  }
  
  static int source_write(serial_source src, const void *buffer, int count)
  {
+ #ifndef _WIN32
    int actual = 0;
  
***************
*** 389,392 ****
--- 503,525 ----
      }
    return actual;
+ #else // _WIN32
+     int actual = 0;
+     int n;
+     const unsigned char * b = buffer;
+ 
+     while (count > 0) {
+       if (!WriteFile(src->hComm, b, count, &n, NULL)) {
+ 	      message(src, msg_unix_error);
+           actual = -1;
+           break;
+       }
+ 
+       count  -= n;
+       actual += n;
+       b      += n;
+     }
+ 
+     return actual;
+ #endif
  }
  
***************
*** 495,503 ****
  	      break;
  	    }
  	  if (errno == EAGAIN)
  	    return -1;
  	  if (errno != EINTR)
  	    message(src, msg_unix_error);
! 	}
      }
    //printf("in %02x\n", src->recv.buffer[src->recv.bufpos]);
--- 628,638 ----
  	      break;
  	    }
+ #ifndef _WIN32
  	  if (errno == EAGAIN)
  	    return -1;
  	  if (errno != EINTR)
  	    message(src, msg_unix_error);
! #endif
!     }
      }
    //printf("in %02x\n", src->recv.buffer[src->recv.bufpos]);
***************
*** 759,764 ****
--- 894,904 ----
      return -1;
  
+   // FIXME: the WIN32 implementation of source_wait()
+   //        disregards the deadline parameter anyway
+ #ifndef _WIN32
    gettimeofday(&deadline, NULL);
    add_timeval(&deadline, ACK_TIMEOUT);
+ #endif
+ 
    for (;;) 
      {

Index: serialsource.h
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/c/sf/serialsource.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** serialsource.h	5 Dec 2007 22:49:46 -0000	1.1
--- serialsource.h	19 Sep 2008 19:36:23 -0000	1.2
***************
*** 2,10 ****
  #define SERIALSOURCE_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
! typedef struct serial_source *serial_source;
  
  typedef enum {
--- 2,14 ----
  #define SERIALSOURCE_H
  
+ #ifdef _WIN32
+ #include <windows.h>
+ #endif
+ 
  #ifdef __cplusplus
  extern "C" {
  #endif
  
! typedef struct serial_source_t *serial_source;
  
  typedef enum {
***************
*** 33,40 ****
--- 37,53 ----
   */
  
+ #ifndef _WIN32
  int serial_source_fd(serial_source src);
  /* Returns: the file descriptor used by serial source src (useful when
       non-blocking reads were requested)
  */
+ #endif
+ 
+ #ifdef _WIN32
+ HANDLE serial_source_handle(serial_source src);
+ /* Returns: the file descriptor used by serial source src (useful when
+      non-blocking reads were requested)
+ */
+ #endif
  
  int serial_source_empty(serial_source src);



More information about the Tinyos-2-commits mailing list