Recreate the navit git/gerrit project that vanished
[profile/ivi/navit.git] / navit / support / win32 / serial_io.c
1 #include <stdio.h>
2 #include <windows.h>
3 #include <glib.h>
4 #include "serial_io.h"
5 #include "debug.h"
6
7 //***************************************************************************
8 /** @fn int serial_io_init( const char* port, const char* strsettings )
9 *****************************************************************************
10 * @b Description: initialise a serial port communication
11 *****************************************************************************
12 * @param      port : port name
13 *                 example 'COM7'
14 * @param      strsettings : port settings
15 *                 example ; 'baud=115200 parity=N data=8 stop=1'
16 *****************************************************************************
17 * @return     file descriptor
18 *             -1 if error
19 *****************************************************************************
20 **/
21 int serial_io_init( const char* port, const char* strsettings )
22 {
23     HANDLE hCom = NULL;
24     DCB dcb;
25     COMMTIMEOUTS sCT;
26
27         char strport[16];
28         g_snprintf( strport, sizeof( strport ), "\\\\.\\%s", port );
29
30         hCom = CreateFile(
31                         strport,
32                         GENERIC_WRITE | GENERIC_READ,
33                         0,
34                         0,
35                         OPEN_EXISTING,
36                         0,
37                         NULL);
38
39         if (hCom == INVALID_HANDLE_VALUE)
40         {
41                 LPVOID lpMsgBuf;
42
43                 FormatMessage(
44                         FORMAT_MESSAGE_ALLOCATE_BUFFER |
45                         FORMAT_MESSAGE_FROM_SYSTEM |
46                         FORMAT_MESSAGE_IGNORE_INSERTS,
47                         NULL,
48                         GetLastError(),
49                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
50                         (LPTSTR) &lpMsgBuf,
51                         0,
52                         NULL
53                 );
54                 dbg(1, "return (fd) : '-1' : serial_io_init error : '%s'\n", lpMsgBuf);
55
56                 LocalFree( lpMsgBuf );       // Free the buffer.
57                 return -1;
58         }
59
60         ZeroMemory(&dcb, sizeof(DCB));
61
62         GetCommState(hCom, &dcb);
63
64         BuildCommDCB( strsettings, &dcb);
65
66         SetupComm(hCom, 4096, 4096);
67
68         SetCommState(hCom, &dcb);
69
70         memset(&sCT, 0, sizeof(sCT));
71         sCT.ReadTotalTimeoutConstant = 10;
72
73         SetCommTimeouts(hCom, &sCT);
74
75         dbg(1, "serial_io_init return (fd) : '%d'\n", (int)hCom);
76
77    return (int)hCom;
78 }
79
80 //***************************************************************************
81 /** @fn int serial_io_read( int fd, char * buffer, int buffer_size )
82 *****************************************************************************
83 * @b Description: Read bytes on the serial port
84 *****************************************************************************
85 * @param      fd : file descriptor
86 * @param      buffer : buffer for data
87 * @param      buffer_size : size in byte of the buffer
88 *****************************************************************************
89 * @return     number of bytes read
90 *****************************************************************************
91 * @remarks buffer must be allocated by the caller
92 *****************************************************************************
93 **/
94 int serial_io_read( int fd, char * buffer, int buffer_size )
95 {
96         DWORD dwBytesIn = 0;
97         dbg(1, "serial_io_read fd = %d buffer_size = %d\n", fd, buffer_size);
98
99
100         if (fd <= 0)
101         {
102                dbg(0, "serial_io_read return (dwBytesIn) : '0'\n");
103                *buffer = 0;
104                 return 0;
105         }
106
107         ReadFile( (HANDLE)fd, buffer, buffer_size - 1, &dwBytesIn, NULL);
108
109         if (dwBytesIn >= 0)
110         {
111                 buffer[dwBytesIn] = 0;
112         }
113         else
114         {
115             dwBytesIn = 0;
116                         buffer[0] = 0;
117         }
118         if (dwBytesIn > 0)
119         {
120             dbg(1,"GPS < %s\n",buffer );
121         }
122         buffer[buffer_size - 1] = 0;
123
124         dbg(2, "serial_io_read return (dwBytesIn) : '%d'\n", dwBytesIn);
125         return dwBytesIn;
126 }
127
128 //***************************************************************************
129 /** @fn int serial_io_write(int fd, const char * buffer)
130 *****************************************************************************
131 * @b Description: Write bytes on the serial port
132 *****************************************************************************
133 * @param      fd : file descriptor
134 * @param      buffer : data buffer (null terminated)
135 *****************************************************************************
136 * @return     number of bytes written
137 *****************************************************************************
138 **/
139 int serial_io_write(int fd, const char * buffer)
140 {
141         DWORD dwBytesOut = 0;
142         dbg(1, "serial_io_write fd = %d buffer = '%s'\n",fd, buffer);
143
144
145         WriteFile((HANDLE)fd, buffer, strlen(buffer), &dwBytesOut, NULL);
146
147         return dwBytesOut;
148 }
149
150 //***************************************************************************
151 /** @fn void serial_io_shutdown(int fd )
152 *****************************************************************************
153 * @b Description: Terminate serial communication
154 *****************************************************************************
155 * @param      fd : file descriptor
156 *****************************************************************************
157 **/
158 void serial_io_shutdown(int fd )
159 {
160        dbg(1, "serial_io_shutdown fd = %d\n",fd);
161
162         if (fd > 0)
163         {
164                 CloseHandle((HANDLE)fd);
165         }
166 }