Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / net / nmeap / tst / wingps.c
1 /*
2 Copyright (c) 2005, David M Howard (daveh at dmh2000.com)
3 All rights reserved.
4
5 This product is licensed for use and distribution under the BSD Open Source License.
6 see the file COPYING for more details.
7
8 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
9 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
10 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
11 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
12 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
13 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
14 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
15 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
16 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
17 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
18 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
19
20 */
21
22 /*
23 ========================================================================================================
24 EXAMPLE : SETUP FOR GGA AND RMC SENTENCES WITH SERIAL IO FOR WIN32
25 =======================================================================================================
26 */   
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "nmeap.h"
33
34 /** open a serial port */
35 static HANDLE openPort(const char *port,int baud)
36 {
37         HANDLE       h;
38         DCB          dcb;
39         COMMTIMEOUTS tmo;
40         int          status;
41
42         /* open the port */
43         h = CreateFile( port,  
44                     GENERIC_READ | GENERIC_WRITE, 
45                     0, 
46                     0, 
47                     OPEN_EXISTING,
48                     0,
49                     0);
50         if (h == INVALID_HANDLE_VALUE) {
51                 /* quit on error */
52                 return h;
53         }
54    
55
56         /* read current configuration */
57    status = GetCommState(h,&dcb);
58    if (status == 0) {
59            CloseHandle(h);
60            return INVALID_HANDLE_VALUE;
61    }
62
63    /* set the baud rate and other parameters */
64    dcb.BaudRate = baud;
65    dcb.ByteSize = 8;
66    dcb.Parity   = NOPARITY; 
67    dcb.StopBits = ONESTOPBIT;
68
69    /* set configuration */
70    status = SetCommState(h, &dcb);
71    if (status == 0) {
72            CloseHandle(h);
73            return INVALID_HANDLE_VALUE;
74    }
75
76    /* read timeout configuration */
77    status = GetCommTimeouts(h,&tmo);
78    if (status == 0) {
79            CloseHandle(h);
80            return INVALID_HANDLE_VALUE;
81    }
82
83    /* set to indefinite blocking */
84    tmo.ReadIntervalTimeout        = 0;
85    tmo.ReadTotalTimeoutConstant   = 0;
86    tmo.ReadTotalTimeoutMultiplier = 0;
87    status = SetCommTimeouts(h,&tmo);
88    if (status == 0) {
89            CloseHandle(h);
90            return INVALID_HANDLE_VALUE;
91    }
92
93         return h;
94 }
95
96 /** read a byte (blocking) */
97 static int readPort(HANDLE h)
98 {
99         BOOL  status;
100         char  ch;
101         DWORD count;
102         status =  ReadFile(h,&ch,1,&count,0);
103         if (status == 0) {
104                 return -1;
105         }
106
107         return (int)ch;
108 }
109
110
111 static void closePort(HANDLE h)
112 {
113         CloseHandle(h);
114 }
115
116
117 /** print current data */
118 static void printGps(nmeap_gga_t *gga,nmeap_rmc_t *rmc)
119 {
120         printf("%lu %lu %.6f %.6f %.0f %f %f %d %d\n",
121                         gga->time,
122                         rmc->date,
123             gga->latitude  ,
124             gga->longitude, 
125             gga->altitude , 
126                         rmc->course,
127                         rmc->speed,
128             gga->satellites,
129             gga->quality   
130             );
131 }
132
133 /* ---------------------------------------------------------------------------------------*/
134 /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */
135 /*          they need to be live for the duration of the parser                           */
136 /* ---------------------------------------------------------------------------------------*/
137 static nmeap_context_t nmea;       /* parser context */
138 static nmeap_gga_t     gga;                /* this is where the data from GGA messages will show up */
139 static nmeap_rmc_t     rmc;                /* this is where the data from RMC messages will show up */
140 static int             user_data; /* user can pass in anything. typically it will be a pointer to some user data */
141
142 int main(int argc,char *argv[])
143 {
144     int             status;
145     char            ch;
146         const char     *port;
147         int             baud;
148         HANDLE          h;
149
150         /* require both arguments */
151         if (argc != 3) {
152                 printf("%s <comport> <baud>\n",argv[0]);
153                 return 1;
154         }
155
156         /* serial port argument */
157         port = argv[1];
158
159         /* baud rate argument */
160         status = sscanf(argv[2],"%d",&baud);
161         if (status != 1) {
162                 printf("%s <comport> <baud>\n",argv[0]);
163                 printf("invalid <baud> : %s\n",argv[2]);
164                 return 1;
165         }
166
167         /** open the serial port */
168         h = openPort(port,baud);
169         if (h == INVALID_HANDLE_VALUE) {
170                 printf("can't open port : %s\n",port);
171                 return 1;
172         }
173     
174         /* ---------------------------------------*/
175         /*STEP 2 : initialize the nmea context    */                                                
176         /* ---------------------------------------*/
177     status = nmeap_init(&nmea,(void *)&user_data);
178     if (status != 0) {
179         printf("nmeap_init %d\n",status);
180         exit(1);
181     }
182     
183         /* ---------------------------------------*/
184         /*STEP 3 : add standard GPGGA parser      */                                                
185         /* -------------------------------------- */
186     status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,0,&gga);
187     if (status != 0) {
188         printf("nmeap_add %d\n",status);
189         exit(1);
190     }
191
192         /* ---------------------------------------*/
193         /*STEP 4 : add standard GPRMC parser      */                                                
194         /* -------------------------------------- */
195     status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,0,&rmc);
196     if (status != 0) {
197         printf("nmeap_add %d\n",status);
198         exit(1);
199     }
200
201         /* ---------------------------------------*/
202         /*STEP 5 : process input until done       */                                                
203         /* -------------------------------------- */
204     for(;;) {
205                 /* ---------------------------------------*/
206                 /*STEP 6 : get a byte at a time           */                                                
207                 /* -------------------------------------- */
208         ch = readPort(h);
209         if (ch <= 0) {
210             break;
211         }
212         
213                 /* --------------------------------------- */
214                 /*STEP 7 : pass it to the parser           */
215                 /* status indicates whether a complete msg */
216                 /* arrived for this byte                   */
217                 /* NOTE : in addition to the return status */
218                 /* the message callout will be fired when  */
219                 /* a complete message is processed         */
220                 /* --------------------------------------- */
221         status = nmeap_parse(&nmea,ch);
222         
223                 /* ---------------------------------------*/
224                 /*STEP 8 : process the return code        */                                                
225                 /* -------------------------------------- */
226         switch(status) {
227         case NMEAP_GPGGA:
228                         /* GOT A GPGGA MESSAGE */
229             printGps(&gga,&rmc);
230             break;
231         case NMEAP_GPRMC:
232                         /* GOT A GPRMC MESSAGE */
233             printGps(&gga,&rmc);
234             break;
235         default:
236             break;
237         }
238     }
239
240         /* close and quit */
241         closePort(h);
242     
243     return 0;
244 }