-
Notifications
You must be signed in to change notification settings - Fork 3
/
ubx_serial.cpp
executable file
·328 lines (292 loc) · 7.27 KB
/
ubx_serial.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************************
*
* Copyright (C) u-blox AG
* u-blox AG, Thalwil, Switzerland
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR U-BLOX MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*
*******************************************************************************
*
* Project: PE_ANS
*
******************************************************************************/
/*!
\file
\brief Serial utility functions
Utility for accessing the serial port in a easier way
*/
/*******************************************************************************
* $Id: ubx_serial.cpp 63615 2012-11-27 10:12:42Z andrea.foni $
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#if defined (ANDROID_BUILD)
#include <termios.h>
#else
#include <sys/termios.h>
#endif
#if defined serial_icounter_struct
#include <linux/serial.h>
#endif
#include <linux/i2c.h>
#include "std_types.h"
#include "std_lang_def.h"
#include "std_macros.h"
#include "ubx_log.h"
#include "ubx_serial.h"
int CSerialPort::settermios(int ttybaud, int blocksize)
{
struct termios termIos;
if (m_i2c) return 0;
if (tcgetattr(m_fd, &termIos) < 0)
{
return (-1);
}
cfmakeraw (&termIos);
termIos.c_cflag |= CLOCAL;
/* {
int cnt;
for (cnt = 0; cnt < NCCS; cnt++)
{
termIos.c_cc[cnt] = -1;
}
} */
if (blocksize >= 0)
termIos.c_cc[VMIN] = (unsigned char) blocksize;
termIos.c_cc[VTIME] = 0;
#if (B4800 != 4800)
/*
* Only paleolithic systems need this.
*/
switch (ttybaud)
{
case 300:
ttybaud = B300;
break;
case 1200:
ttybaud = B1200;
break;
case 2400:
ttybaud = B2400;
break;
case 4800:
ttybaud = B4800;
break;
case 9600:
ttybaud = B9600;
break;
case 19200:
ttybaud = B19200;
break;
case 38400:
ttybaud = B38400;
break;
case 57600:
ttybaud = B57600;
break;
case 115200:
ttybaud = B115200;
break;
default:
ttybaud = B4800;
break;
}
#endif
if (cfsetispeed(&termIos, (unsigned int) ttybaud) != 0)
{
return (-1);
}
#ifdef BIDIRECTIONAL
if (cfsetospeed(&termIos, ttybaud) != 0)
{
return (-1);
}
#endif
if (tcsetattr(m_fd, TCSANOW, &termIos) < 0)
{
return (-1);
}
return 0;
}
bool CSerialPort::openSerial(const char * pTty, int ttybaud, int blocksize)
{
m_i2c = strncmp(pTty, "/dev/i2c", 8) == 0;
m_fd = open(pTty, (m_i2c ? 0 : O_NOCTTY)
| O_RDWR
#ifdef O_EXLOCK /* for Linux */
| O_EXLOCK
#endif
);
if (m_fd < 0)
{
LOGE("Cannot open serial port %s, return %d %d", pTty, m_fd, m_i2c);
return false;
}
if (m_i2c)
{
#ifndef UNIX_API
int io = ioctl(m_fd, I2C_SLAVE, 0x42);
char adr = 0xFF;
if (io < 0)
{
LOGE("no i2c device");
close (m_fd);
m_fd = -1;
return false;
}
LOGV("GPS detected on I2C %s", pTty);
if (write(m_fd, &adr, 1) != 1)
{
LOGE("set register failed");
close (m_fd);
m_fd = -1;
return false;
}
#endif
}
else
{
if (settermios(ttybaud, blocksize) == -1)
{
LOGE("Cannot invoke settermios");
close (m_fd);
m_fd = -1;
return false;
}
#ifdef BIDIRECTIONAL
tcflush(m_fd, TCIOFLUSH);
#else
tcflush(m_fd, TCIFLUSH);
#endif
fcntl(m_fd, F_SETFL, 0);
#if defined serial_icounter_struct
/* Initialize the error counters */
if (!ioctl(m_fd,TIOCGICOUNT, &einfo))
{
/* Nothing to do... */
}
else
{
/* error, cannot read the error counters */
LOGW("unable to read error counters!!!");
}
#endif
}
LOGV("Serial port opened, fd = %d", m_fd);
return true;
}
int CSerialPort::changeBaudrate(char * pTty, int * pBaudrate, const unsigned char * pBuf, int length)
{
unsigned long newbaudrate = 0;
if (m_i2c)
{
*pBaudrate = (int)newbaudrate;
return 1;
}
if (length != 28) return 0;
if (pBuf[0] != 0xb5) return 0;
if (pBuf[1] != 0x62) return 0;
if (pBuf[2] != 0x06) return 0;
if (pBuf[3] != 0x00) return 0;
if (pBuf[4] != 0x14) return 0;
if (pBuf[5] != 0x00) return 0;
if ((pBuf[6] != 0x01) && (pBuf[6] != 0xff)) return 0;
memcpy(&newbaudrate, pBuf+8+6,4);
if (newbaudrate != (unsigned long) *pBaudrate)
{
int tmp = *pBaudrate;
close (m_fd);
LOGW("Attempting to change baudrate from %d to %lu on %s)",tmp,newbaudrate, pTty);
/* Wait a while... */
usleep(100000);
if (!openSerial(pTty, (int) newbaudrate, 2))
{
// failed ?!?
LOGE("%s: %s", pTty, strerror(errno));
openSerial(pTty, *pBaudrate, 2);
return 0;
} else {
*pBaudrate = (int)newbaudrate;
LOGW("Changed baudrate from to %i on %s)",*pBaudrate, pTty);
return 1;
}
}
return 0;
}
int CSerialPort::retrieveErrors() const
{
int res = 0;
if (m_i2c)
return res;
#if defined serial_icounter_struct
struct serial_icounter_struct einfo_tmp;
/* read the counters */
if (!ioctl(m_fd,TIOCGICOUNT, &einfo_tmp))
{
/* check if something has changed */
if (einfo_tmp.frame != einfo.frame)
{
LOGW("Frame error occurred!!!! (%d)",einfo_tmp.frame - einfo.frame);
/* return the number of frame errors */
res = einfo_tmp.frame - einfo.frame;
}
else if (einfo_tmp.brk != einfo.brk)
{
LOGW("Breaks occurred!!!! (%d)",einfo_tmp.brk - einfo.brk);
/* return the number of frame errors */
res = einfo_tmp.brk - einfo.brk;
}
/* update the stored counters */
memcpy(&einfo, &einfo_tmp, sizeof(einfo));
}
else
{
/* cannot read the counters! ignore, as we might be talking to a device without error counters */
}
#endif
return res;
}
void CSerialPort::baudrateIncrease(int *pBaudrate) const
{
int i;
int oldBaudrate = *pBaudrate;
/* check next baudrate */
for (i = 0; i< BAUDRATE_TABLE_SIZE; i++)
{
if ( *pBaudrate < s_baudrateTable[i])
{
*pBaudrate = s_baudrateTable[i];
break;
}
}
if (i == BAUDRATE_TABLE_SIZE)
{
*pBaudrate = s_baudrateTable[0];
}
LOGW("Attempting to change baudrate from %d to %d", oldBaudrate, *pBaudrate);
return;
}
const int CSerialPort::s_baudrateTable[BAUDRATE_TABLE_SIZE] =
{
4800,
9600,
19200,
38400,
57600,
115200,
230400,
};