summaryrefslogtreecommitdiff
path: root/mbglib/common/mbgserio.h
blob: 2367e1468cdfbda2a8b0b675700818ac9d4e1278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

/**************************************************************************
 *
 *  $Id: mbgserio.h 1.4 2009/09/01 10:54:29 martin REL_M $
 *
 *  Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
 *
 *  Description:
 *    Definitions and prototypes for mbgserio.c.
 *
 * -----------------------------------------------------------------------
 *  $Log: mbgserio.h $
 *  Revision 1.4  2009/09/01 10:54:29  martin
 *  Include mbg_tmo.h for the new portable timeout functions.
 *  Added symbols for return codes in case of an error.
 *  Code cleanup.
 *  Revision 1.3  2009/04/01 14:17:31  martin
 *  Cleanup for CVI.
 *  Revision 1.2  2008/09/04 15:11:36Z  martin
 *  Preliminary support for device lists.
 *  Updated function prototypes.
 *  Revision 1.1  2007/11/12 16:48:02  martin
 *  Initial revision.
 *
 **************************************************************************/

#ifndef _MBGSERIO_H
#define _MBGSERIO_H


/* Other headers to be included */

#include <mbg_tmo.h>

#include <stdlib.h>
#include <string.h>

#if defined( MBG_TGT_UNIX )
  #include <termios.h>
#endif

#if _USE_CHK_TSTR
  #include <chk_tstr.h>
#endif

#if !defined( _USE_SELECT_FOR_SERIAL_IO )
  #if defined( MBG_TGT_UNIX )
    #define _USE_SELECT_FOR_SERIAL_IO  1
  #else
    #define _USE_SELECT_FOR_SERIAL_IO  0
  #endif
#endif


#ifdef _MBGSERIO
 #define _ext
 #define _DO_INIT
#else
 #define _ext extern
#endif


/* Start of header body */

#define MBGSERIO_FAIL     -1   // Generic I/O error
#define MBGSERIO_TIMEOUT  -2   // timeout
#define MBGSERIO_INV_CFG  -3   // invalid configuration parameters


#if !defined( DEFAULT_DEV_NAME )
  #if defined( MBG_TGT_WIN32 ) || defined( MBG_TGT_DOS )
    #define DEFAULT_DEV_NAME   "COM1"
  #elif defined( MBG_TGT_LINUX )
    #define DEFAULT_DEV_NAME   "/dev/ttyS0"
  #endif
#endif


/*
 * The following macros control parts of the build process.
 * The default values are suitable for most cases but can be
 * overridden by global definitions, if required.
 */

#if _IS_MBG_FIRMWARE

  // This handle type in not used by the firmware.
  // However, we define it to avoid build errors.
  typedef int MBG_HANDLE;

#else

  #if defined( MBG_TGT_CVI )

    #include <rs232.h>

    #define _mbg_open        _open
    #define _mbg_close       _close
    #define _mbg_read        _read
    #define _mbg_write       _write

    #define _mbgserio_write( _dh, _p, _sz ) \
            ComWrt( _dh, (char *) (_p), _sz )

    #define _mbgserio_read( _dh, _p, _sz ) \
            ComRd( _dh, (char *) (_p), _sz )

  #elif defined( MBG_TGT_WIN32 )

    #include <windows.h>
    #include <io.h>

    #define _mbg_open        _open
    #define _mbg_close       _close
    #define _mbg_read        _read
    #define _mbg_write       _write

    #define _mbgserio_write  mbgserio_write
    #define _mbgserio_read   mbgserio_read

  #elif defined( MBG_TGT_UNIX )

    #include <unistd.h>

    #define _mbg_open     open
    #define _mbg_close    close
    #define _mbg_read     read
    #define _mbg_write    write

  #elif defined( MBG_TGT_DOS )

    #if defined( _USE_V24TOOLS )
      #include <v24tools.h>

      #define _mbgserio_open   v24open
      #define _mbgserio_read   v24read
      #define _mbgserio_write  v24write
    #endif

  #endif

  #if !defined( _mbgserio_open )
    #define _mbgserio_open   _mbg_open
  #endif
  #if !defined( _mbgserio_write )
    #define _mbgserio_write  _mbg_write
  #endif
  #if !defined( _mbgserio_read )
    #define _mbgserio_read   _mbg_read
  #endif

#endif



typedef struct _MBG_STR_LIST
{
  char *s;
  struct _MBG_STR_LIST *next;

} MBG_STR_LIST;



typedef struct
{
  MBG_PORT_HANDLE port_handle;   // the handle that will be used for the device

  #if defined( MBG_TGT_WIN32 )
    DCB old_dcb;
    COMMTIMEOUTS old_commtimeouts;
  #endif
  #if defined( MBG_TGT_UNIX )
    struct termios oldtio;
    //##++ struct termios newtio;
  #endif

} SERIAL_IO_STATUS;



/* function prototypes: */

#ifdef __cplusplus
extern "C" {
#endif

/* ----- function prototypes begin ----- */

/* This section was generated automatically */
/* by MAKEHDR, do not remove the comments. */

 _MBG_API_ATTR int _MBG_API mbgserio_open( SERIAL_IO_STATUS *pst, const char *dev ) ;
 _MBG_API_ATTR int _MBG_API mbgserio_close( SERIAL_IO_STATUS *pst ) ;
 _MBG_API_ATTR int _MBG_API mbgserio_setup_port_str_list( MBG_STR_LIST **list, int max_devs ) ;
 _MBG_API_ATTR void _MBG_API _MBG_API mbgserio_free_str_list( MBG_STR_LIST *list ) ;
 _MBG_API_ATTR int _MBG_API mbgserio_set_parms( SERIAL_IO_STATUS *pst,  uint32_t baud_rate, const char *framing ) ;
 _MBG_API_ATTR int _MBG_API mbgserio_read( MBG_PORT_HANDLE h, void *buffer, unsigned int count ) ;
 _MBG_API_ATTR int _MBG_API  mbgserio_write( MBG_PORT_HANDLE h, const void *buffer, unsigned int count ) ;
 _MBG_API_ATTR int _MBG_API mbgserio_read_wait( MBG_PORT_HANDLE h, void *buffer, uint count, ulong char_timeout ) ;

/* ----- function prototypes end ----- */

#ifdef __cplusplus
}
#endif

/* End of header body */

#undef _ext
#undef _DO_INIT

#endif  /* _MBGSERIO_H */