Ticket #332: windows-vehicle.diff

File windows-vehicle.diff, 21.5 KB (added by richt, 10 years ago)
  • navit/configure.in

    diff -r 14609e8d2be6 navit/configure.in
    a b  
    129129then
    130130        NAVIT_CFLAGS="$NAVIT_CFLAGS -I\$(top_srcdir)/navit/support/win32"
    131131        NAVIT_LIBS="$NAVIT_LIBS -L\$(top_builddir)/navit/support/win32 -lsupport_win32"
     132        NAVIT_SOCKET_LDFLAGS="$MODULE_LDFLAGS -lws2_32"
    132133fi
    133134
    134135
     
    146147AC_SUBST(MODULE_LDFLAGS)
    147148AC_SUBST(MODULE_LIBADD)
    148149AC_SUBST(NAVIT_MODULE_LDFLAGS)
     150AC_SUBST(NAVIT_SOCKET_LDFLAGS)
    149151
    150152LIBS="$LIBS -lm"
    151153if test "$win32" == "no" -a test "$host_os" != "cygwin"; then
     
    359361AC_SUBST(ZLIB_LIBS)
    360362
    361363AC_CHECK_HEADER(sys/socket.h, AC_DEFINE([HAVE_SOCKET],[],Define to 1 if you have sockets))
     364AC_CHECK_HEADER(winsock2.h, AC_DEFINE([HAVE_WINSOCK],[],Define to 1 if you have Windows sockets))
    362365
    363366# gtk
    364367PKG_CHECK_MODULES(GTK2, [gtk+-2.0], [gtk2_pkgconfig=yes], [gtk2_pkgconfig=no])
  • navit/navit/Makefile.am

    diff -r 14609e8d2be6 navit/navit/Makefile.am
    a b  
    3434
    3535if SHARED_LIBNAVIT
    3636  lib_LTLIBRARIES        = lib@LIBNAVIT@.la
    37   lib@LIBNAVIT@_la_LDFLAGS = -avoid-version @MODULE_LDFLAGS@ -no-undefined -Wl,--no-undefined
     37  lib@LIBNAVIT@_la_LDFLAGS = -avoid-version @MODULE_LDFLAGS@ @NAVIT_SOCKET_LDFLAGS@ -no-undefined -Wl,--no-undefined
    3838else
    3939  noinst_LTLIBRARIES        = lib@LIBNAVIT@.la
    4040endif
  • navit/navit/support/win32/Makefile.am

    diff -r 14609e8d2be6 navit/navit/support/win32/Makefile.am
    a b  
    11include $(top_srcdir)/Makefile.inc
    22AM_CPPFLAGS = @NAVIT_CFLAGS@ -I$(top_srcdir)/navit -DMODULE=support_win32
    33noinst_LTLIBRARIES = libsupport_win32.la
    4 libsupport_win32_la_SOURCES = mmap.c win32_init.c sys/mman.h ConvertUTF.c ConvertUTF.h
     4libsupport_win32_la_SOURCES = mmap.c win32_init.c sys/mman.h ConvertUTF.c ConvertUTF.h serial_io.c addwinsock.c
     5
  • new file navit/navit/support/win32/addwinsock.c

    diff -r 14609e8d2be6 navit/navit/support/win32/addwinsock.c
    - +  
     1#include "addwinsock.h"
     2
     3int inet_aton(const char *cp, struct in_addr *inp)
     4{
     5        unsigned long addr = inet_addr(cp);
     6        inp->S_un.S_addr = addr;
     7        return addr != -1;
     8}
  • new file navit/navit/support/win32/addwinsock.h

    diff -r 14609e8d2be6 navit/navit/support/win32/addwinsock.h
    - +  
     1#ifndef ADD_WINSOCK
     2#define ADD_WINSOCK
     3
     4#include <winsock2.h>
     5
     6int inet_aton(const char *cp, struct in_addr *inp);
     7
     8#endif
  • navit/navit/support/win32/serial_io.c

    diff -r 14609e8d2be6 navit/navit/support/win32/serial_io.c
    a b  
    33#include "serial_io.h"
    44#include "debug.h"
    55
    6 //***************************************************************************
    7 /** @fn int serial_io_init( const char* port, const char* strsettings )
    8 *****************************************************************************
    9 * @b Description: initialise a serial port communication
    10 *****************************************************************************
    11 * @param      port : port name
    12 *                 example 'COM7'
    13 * @param      strsettings : port settings
    14 *                 example ; 'baud=115200 parity=N data=8 stop=1'
    15 *****************************************************************************
    16 * @return     file descriptor
    17 *             -1 if error
    18 *****************************************************************************
    19 **/
     6//***************************************************************************
     7/** @fn int serial_io_init( const char* port, const char* strsettings )
     8*****************************************************************************
     9* @b Description: initialise a serial port communication
     10*****************************************************************************
     11* @param      port : port name
     12*                 example 'COM7'
     13* @param      strsettings : port settings
     14*                 example ; 'baud=115200 parity=N data=8 stop=1'
     15*****************************************************************************
     16* @return     file descriptor
     17*             -1 if error
     18*****************************************************************************
     19**/
    2020int serial_io_init( const char* port, const char* strsettings )
    2121{
    2222    HANDLE hCom = NULL;
    23 
     23
    2424        char strport[16];
    2525        snprintf( strport, sizeof( strport ), "\\\\.\\%s", port );
    2626
     
    4848                        0,
    4949                        NULL
    5050                );
    51                 dbg(1, "return (fd) : '-1' : serial_io_init error : '%s'\n", lpMsgBuf);
     51                dbg(1, "return (fd) : '-1' : serial_io_init error : '%s'\n", lpMsgBuf);
    5252
    5353                LocalFree( lpMsgBuf );       // Free the buffer.
    5454                return -1;
     
    7373
    7474        SetCommTimeouts(hCom, &sCT);
    7575
    76         dbg(1, "serial_io_init return (fd) : '%d'\n", (int)hCom);
    77 
     76        dbg(1, "serial_io_init return (fd) : '%d'\n", (int)hCom);
     77
    7878   return (int)hCom;
    7979}
    8080
    81 //***************************************************************************
    82 /** @fn int serial_io_read( int fd, char * buffer, int buffer_size )
    83 *****************************************************************************
    84 * @b Description: Read bytes on the serial port
    85 *****************************************************************************
    86 * @param      fd : file descriptor
    87 * @param      buffer : buffer for data
    88 * @param      buffer_size : size in byte of the buffer
    89 *****************************************************************************
    90 * @return     number of bytes read
    91 *****************************************************************************
    92 * @remarks buffer must be allocated by the caller
    93 *****************************************************************************
    94 **/
     81//***************************************************************************
     82/** @fn int serial_io_read( int fd, char * buffer, int buffer_size )
     83*****************************************************************************
     84* @b Description: Read bytes on the serial port
     85*****************************************************************************
     86* @param      fd : file descriptor
     87* @param      buffer : buffer for data
     88* @param      buffer_size : size in byte of the buffer
     89*****************************************************************************
     90* @return     number of bytes read
     91*****************************************************************************
     92* @remarks buffer must be allocated by the caller
     93*****************************************************************************
     94**/
    9595int serial_io_read( int fd, char * buffer, int buffer_size )
    9696{
    97         dbg(1, "serial_io_read fd = %d buffer_size = %d\n", fd, buffer_size);
    98 
     97        dbg(1, "serial_io_read fd = %d buffer_size = %d\n", fd, buffer_size);
     98
    9999        DWORD dwBytesIn = 0;
    100100
    101101        if (fd <= 0)
    102102        {
    103                dbg(0, "serial_io_read return (dwBytesIn) : '0'\n");
     103               dbg(0, "serial_io_read return (dwBytesIn) : '0'\n");
    104104               *buffer = 0;
    105105                return 0;
    106106        }
     
    111111        {
    112112                buffer[dwBytesIn] = 0;
    113113        }
    114         else
     114        else
    115115        {
    116             dwBytesIn = 0;
     116            dwBytesIn = 0;
    117117                        buffer[0] = 0;
    118118        }
    119         if (dwBytesIn > 0)
    120         {
    121             dbg(1,"GPS < %s\n",buffer );
    122         }
     119        if (dwBytesIn > 0)
     120        {
     121            dbg(1,"GPS < %s\n",buffer );
     122        }
    123123        buffer[buffer_size - 1] = 0;
    124124
    125         dbg(2, "serial_io_read return (dwBytesIn) : '%d'\n", dwBytesIn);
     125        dbg(2, "serial_io_read return (dwBytesIn) : '%d'\n", dwBytesIn);
    126126        return dwBytesIn;
    127127}
    128128
    129 //***************************************************************************
    130 /** @fn int serial_io_write(int fd, const char * buffer)
    131 *****************************************************************************
    132 * @b Description: Write bytes on the serial port
    133 *****************************************************************************
    134 * @param      fd : file descriptor
    135 * @param      buffer : data buffer (null terminated)
    136 *****************************************************************************
    137 * @return     number of bytes written
    138 *****************************************************************************
    139 **/
     129//***************************************************************************
     130/** @fn int serial_io_write(int fd, const char * buffer)
     131*****************************************************************************
     132* @b Description: Write bytes on the serial port
     133*****************************************************************************
     134* @param      fd : file descriptor
     135* @param      buffer : data buffer (null terminated)
     136*****************************************************************************
     137* @return     number of bytes written
     138*****************************************************************************
     139**/
    140140int serial_io_write(int fd, const char * buffer)
    141141{
    142         dbg(1, "serial_io_write fd = %d buffer = '%s'\n",fd, buffer);
    143 
     142        dbg(1, "serial_io_write fd = %d buffer = '%s'\n",fd, buffer);
     143
    144144        DWORD dwBytesOut = 0;
    145145
    146146        WriteFile((HANDLE)fd, buffer, strlen(buffer), &dwBytesOut, NULL);
     
    148148        return dwBytesOut;
    149149}
    150150
    151 //***************************************************************************
    152 /** @fn void serial_io_shutdown(int fd )
    153 *****************************************************************************
    154 * @b Description: Terminate serial communication
    155 *****************************************************************************
    156 * @param      fd : file descriptor
    157 *****************************************************************************
    158 **/
     151//***************************************************************************
     152/** @fn void serial_io_shutdown(int fd )
     153*****************************************************************************
     154* @b Description: Terminate serial communication
     155*****************************************************************************
     156* @param      fd : file descriptor
     157*****************************************************************************
     158**/
    159159void serial_io_shutdown(int fd )
    160160{
    161        dbg(1, "serial_io_shutdown fd = %d\n",fd);
    162 
     161       dbg(1, "serial_io_shutdown fd = %d\n",fd);
     162
    163163        if (fd > 0)
    164164        {
    165165                CloseHandle((HANDLE)fd);
  • navit/navit/support/win32/serial_io.h

    diff -r 14609e8d2be6 navit/navit/support/win32/serial_io.h
    a b  
    33
    44int serial_io_init( const char* port, const char* strsettings );
    55int serial_io_read(int fd, char * buffer, int buffer_size );
    6 int WriteSerial(int fd, const char * buffer);
     6int serial_io_write(int fd, const char * buffer);
    77void serial_io_shutdown(int fd );
     8typedef unsigned int speed_t;
     9
     10#define B0 0000000
     11#define B50 0000001
     12#define B75 0000002
     13#define B110 0000003
     14#define B134 0000004
     15#define B150 0000005
     16#define B200 0000006
     17#define B300 0000007
     18#define B600 0000010
     19#define B1200 0000011
     20#define B1800 0000012
     21#define B2400 0000013
     22#define B4800 0000014
     23#define B9600 0000015
     24#define B19200 0000016
     25#define B38400 0000017
     26#define B57600 0010001
     27#define B115200 0010002
     28#define B230400 0010003
     29#define B460800 0010004
     30#define B500000 0010005
     31#define B576000 0010006
     32#define B921600 0010007
     33#define B1000000 0010010
     34#define B1152000 0010011
     35#define B1500000 0010012
     36#define B2000000 0010013
     37#define B2500000 0010014
     38#define B3000000 0010015
     39#define B3500000 0010016
     40#define B4000000 0010017
    841
    942#endif
  • navit/navit/vehicle/file/Makefile.am

    diff -r 14609e8d2be6 navit/navit/vehicle/file/Makefile.am
    a b  
    11include $(top_srcdir)/Makefile.inc
    22AM_CPPFLAGS = @NAVIT_CFLAGS@ -I$(top_srcdir)/navit
    3 modulevehicle_LTLIBRARIES = libvehicle_file.la libvehicle_pipe.la libvehicle_socket.la
     3modulevehicle_LTLIBRARIES = libvehicle_file.la libvehicle_pipe.la libvehicle_socket.la libvehicle_serial.la
    44libvehicle_file_la_CFLAGS=-DMODULE=vehicle_file
    55libvehicle_file_la_SOURCES = vehicle_file.c
    6 libvehicle_file_la_LDFLAGS = -module -avoid-version @NAVIT_MODULE_LDFLAGS@
     6libvehicle_file_la_LDFLAGS = -module -avoid-version @NAVIT_SOCKET_LDFLAGS@ @NAVIT_MODULE_LDFLAGS@
    77libvehicle_pipe_la_CFLAGS=-DMODULE=vehicle_pipe
    88libvehicle_pipe_la_SOURCES = vehicle_pipe.c
    99libvehicle_pipe_la_LDFLAGS = -module -avoid-version @NAVIT_MODULE_LDFLAGS@
    1010libvehicle_socket_la_CFLAGS=-DMODULE=vehicle_socket
    1111libvehicle_socket_la_SOURCES = vehicle_socket.c
    1212libvehicle_socket_la_LDFLAGS = -module -avoid-version @NAVIT_MODULE_LDFLAGS@
     13libvehicle_serial_la_CFLAGS=-DMODULE=vehicle_serial
     14libvehicle_serial_la_SOURCES = vehicle_serial.c
     15libvehicle_serial_la_LDFLAGS = -module -avoid-version @NAVIT_MODULE_LDFLAGS@
  • navit/navit/vehicle/file/vehicle_file.c

    diff -r 14609e8d2be6 navit/navit/vehicle/file/vehicle_file.c
    a b  
    4444#include <netinet/in.h>
    4545#include <arpa/inet.h>
    4646#endif
     47#ifdef HAVE_WINSOCK
     48#include <winsock2.h>
     49int inet_aton(const char *cp, struct in_addr *inp);
     50
     51int inet_aton(const char *cp, struct in_addr *inp)
     52{
     53        unsigned long addr = inet_addr(cp);
     54        inp->S_un.S_addr = addr;
     55        return addr!=-1;
     56}
     57#endif
    4758
    4859static void vehicle_file_disable_watch(struct vehicle_priv *priv);
    4960static void vehicle_file_enable_watch(struct vehicle_priv *priv);
     
    5364
    5465
    5566enum file_type {
    56         file_type_pipe = 1, file_type_device, file_type_file, file_type_socket
     67        file_type_pipe = 1, file_type_device, file_type_file, file_type_socket, file_type_serial
    5768};
    5869
    5970static int buffer_size = 1024;
     
    96107        int no_data_count;
    97108        struct event_timeout * timeout;
    98109        struct callback *timeout_callback;
    99 #else
     110#endif
    100111        enum file_type file_type;
    101112        FILE *file;
    102113        struct event_watch *watch;
    103 #endif
    104114        speed_t baudrate;
    105115        struct attr ** attrs;
    106116        char fixiso8601[128];
     
    205215static int
    206216vehicle_file_open(struct vehicle_priv *priv)
    207217{
    208 #ifdef _WIN32
    209     dbg(1, "enter, priv->source='%s'\n", priv->source);
    210 
    211     if ( priv->source )
    212     {
    213         char* raw_setting_str = g_strdup( priv->source );
    214 
    215         char* strport = strchr(raw_setting_str, ':' );
    216         char* strsettings = strchr(raw_setting_str, ' ' );
    217 
    218         if ( strport && strsettings )
    219         {
    220             strport++;
    221             *strsettings = '\0';
    222             strsettings++;
    223 
    224             priv->fd=serial_io_init( strport, strsettings );
    225         }
    226         g_free( raw_setting_str );
    227 
    228         // Add the callback
    229         dbg(2, "Add the callback ...\n", priv->source);
    230                 priv->timeout_callback=callback_new_1(callback_cast(vehicle_win32_serial_track), priv);
    231     }
    232 #else
    233218        char *name;
    234219        struct stat st;
     220#ifndef _WIN32
    235221        struct termios tio;
     222#else
     223        #define O_NDELAY 0
     224#endif
    236225
    237226        name = priv->source + 5;
    238227        if (!strncmp(priv->source, "file:", 5)) {
     
    242231                stat(name, &st);
    243232                if (S_ISREG(st.st_mode)) {
    244233                        priv->file_type = file_type_file;
    245                 } else {
     234                }
     235#ifndef _WIN32
     236                else {
    246237                        tcgetattr(priv->fd, &tio);
    247238                        cfmakeraw(&tio);
    248239                        cfsetispeed(&tio, priv->baudrate);
     
    258249                        return 0;
    259250                priv->fd = fileno(priv->file);
    260251                priv->file_type = file_type_pipe;
    261 #ifdef HAVE_SOCKET
     252#endif //!_WIN32
     253#if defined(HAVE_SOCKET) || defined(HAVE_WINSOCK)
    262254        } else if (!strncmp(priv->source,"socket:", 7)) {
     255                #ifdef _WIN32
     256                WSADATA wsi;
     257                WSAStartup(0x00020002,&wsi);
     258                #endif
    263259                char *p,*s=g_strdup(priv->source+7);
    264260                struct sockaddr_in sin;
    265261                p=strchr(s,':');
     
    285281                        }
    286282                }
    287283                priv->file_type = file_type_socket;
    288 #endif
    289         }
    290 #endif
     284#endif //HAVE_SOCKET
     285        } else if (!strncmp(priv->source,"serial:",7)) {
     286#ifdef _WIN32
     287                char* raw_setting_str = g_strdup( priv->source );
     288
     289                char* strport = strchr(raw_setting_str, ':' );
     290                char* strsettings = strchr(raw_setting_str, ' ' );
     291
     292                if ( strport && strsettings )
     293                {
     294                    strport++;
     295                    *strsettings = '\0';
     296                    strsettings++;
     297
     298                    priv->fd=serial_io_init( strport, strsettings );
     299                }
     300                g_free( raw_setting_str );
     301                priv->file_type = file_type_serial;
     302                // Add the callback
     303                dbg(2, "Add the callback ...\n", priv->source);
     304                        priv->timeout_callback=callback_new_1(callback_cast(vehicle_win32_serial_track), priv);
     305#else
     306                //TODO - add linux serial
     307#endif //!_WIN32
     308    }
    291309    return(priv->fd != -1);
    292310}
    293311
     
    305323    dbg(1, "enter, priv->fd='%d'\n", priv->fd);
    306324        vehicle_file_disable_watch(priv);
    307325#ifdef _WIN32
    308     if (priv->timeout_callback) {
     326    if(priv->file_type == file_type_serial)
     327    {
     328        if (priv->timeout_callback) {
    309329                callback_destroy(priv->timeout_callback);
    310330                priv->timeout_callback=NULL;    // dangling pointer! prevent double freeing.
     331        }
     332        serial_io_shutdown( priv->fd );
    311333    }
    312         serial_io_shutdown( priv->fd );
    313 #else
     334    else
     335#endif
     336    {
    314337        if (priv->file)
    315338                pclose(priv->file);
    316339        else if (priv->fd >= 0)
    317340                close(priv->fd);
    318341        priv->file = NULL;
    319 #endif
    320342        priv->fd = -1;
     343    }
    321344}
    322345
    323346//***************************************************************************
     
    393416                return ret;
    394417        }
    395418        if (bcsum != csum && priv->checksum_ignore == 0) {
    396                 dbg(0, "wrong checksum in '%s'\n", buffer);
     419                dbg(0, "wrong checksum in '%s was %x should be %x'\n", buffer,bcsum,csum);
    397420                return ret;
    398421        }
    399422
     
    457480                g_free(priv->nmea_data);
    458481                priv->nmea_data=priv->nmea_data_buf;
    459482                priv->nmea_data_buf=NULL;
    460 #ifndef _WIN32
    461483                if (priv->file_type == file_type_file) {
    462484                        if (priv->watch) {
    463485                                vehicle_file_disable_watch(priv);
    464486                                event_add_timeout(priv->time, 0, priv->cbt);
    465487                        }
    466488                }
    467 #endif
    468489        }
    469490        if (!strncmp(buffer, "$GPVTG", 6)) {
    470491                /* 0      1      2 34 5    6 7   8
     
    583604*****************************************************************************
    584605* @param      priv : pointer on the private data of the plugin
    585606*****************************************************************************
    586 * @remarks Not used on WIN32 operating system
     607* @remarks
    587608*****************************************************************************
    588609**/
    589610static void
    590611vehicle_file_io(struct vehicle_priv *priv)
    591612{
    592613        dbg(1, "vehicle_file_io : enter\n");
    593 #ifndef _WIN32
    594614        int size, rc = 0;
    595615        char *str, *tok;
    596616
     
    635655        }
    636656        if (rc)
    637657                callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
    638 #endif
    639658}
    640659
    641660//***************************************************************************
     
    652671        dbg(1, "enter\n");
    653672#ifdef _WIN32
    654673        // add an event : don't use glib timers and g_timeout_add
     674    if (priv->file_type == file_type_serial)
     675    {
    655676        if (priv->timeout_callback != NULL)
    656         priv->timeout = event_add_timeout(500, 1, priv->timeout_callback);
     677            priv->timeout = event_add_timeout(500, 1, priv->timeout_callback);
     678        else
     679            dbg(1, "error : watch not enabled : priv->timeout_callback is null\n"); }
    657680    else
    658         dbg(1, "error : watch not enabled : priv->timeout_callback is null\n");
    659 #else
     681#endif
     682    {
    660683        if (! priv->watch)
    661684                priv->watch = event_add_watch((void *)priv->fd, event_watch_cond_read, priv->cb);
    662 #endif
     685    }
    663686}
    664687
    665688//***************************************************************************
     
    675698{
    676699        dbg(1, "vehicle_file_disable_watch : enter\n");
    677700#ifdef _WIN32
     701    if(priv->file_type == file_type_serial)
     702    {
    678703    if (priv->timeout) {
    679704                event_remove_timeout(priv->timeout);
    680705                priv->timeout=NULL;             // dangling pointer! prevent double freeing.
     706        }
    681707    }
    682 #else
     708    else
     709#endif //!_WIN32
     710    {
    683711        if (priv->watch)
    684712                event_remove_watch(priv->watch);
    685713        priv->watch = NULL;
    686 #endif
     714    }
    687715}
    688716
    689717//***************************************************************************
     
    969997        plugin_register_vehicle_type("file", vehicle_file_new_file);
    970998        plugin_register_vehicle_type("pipe", vehicle_file_new_file);
    971999        plugin_register_vehicle_type("socket", vehicle_file_new_file);
     1000        plugin_register_vehicle_type("serial", vehicle_file_new_file);
    9721001}
  • new file navit/navit/vehicle/file/vehicle_serial.c

    diff -r 14609e8d2be6 navit/navit/vehicle/file/vehicle_serial.c
    - +  
     1/**
     2 * Navit, a modular navigation system.
     3 * Copyright (C) 2005-2008 Navit Team
     4 *
     5 * This program is free software; you can redistribute it and/or
     6 * modify it under the terms of the GNU General Public License
     7 * version 2 as published by the Free Software Foundation.
     8 *
     9 * This program is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with this program; if not, write to the
     16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17 * Boston, MA  02110-1301, USA.
     18 */
     19
     20#include "config.h"
     21#include "plugin.h"
     22
     23void
     24plugin_init(void)
     25{
     26        plugin_get_vehicle_type("file");
     27}